1d96309c9Sopenharmony_ci/* 2d96309c9Sopenharmony_ci * Copyright (c) 2020 Huawei Device Co., Ltd. 3d96309c9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4d96309c9Sopenharmony_ci * you may not use this file except in compliance with the License. 5d96309c9Sopenharmony_ci * You may obtain a copy of the License at 6d96309c9Sopenharmony_ci * 7d96309c9Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8d96309c9Sopenharmony_ci * 9d96309c9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10d96309c9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11d96309c9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12d96309c9Sopenharmony_ci * See the License for the specific language governing permissions and 13d96309c9Sopenharmony_ci * limitations under the License. 14d96309c9Sopenharmony_ci */ 15d96309c9Sopenharmony_ci 16d96309c9Sopenharmony_ci#include "stdio.h" 17d96309c9Sopenharmony_ci#include <stdarg.h> 18d96309c9Sopenharmony_ci#include <securec.h> 19d96309c9Sopenharmony_ci#include "fcntl.h" 20d96309c9Sopenharmony_ci#include "unistd.h" 21d96309c9Sopenharmony_ci#include <log.h> 22d96309c9Sopenharmony_ci 23d96309c9Sopenharmony_ci#ifndef LOSCFG_BASE_CORE_HILOG 24d96309c9Sopenharmony_ci#include <stdatomic.h> 25d96309c9Sopenharmony_ci#include "hilog_trace.h" 26d96309c9Sopenharmony_ci#endif 27d96309c9Sopenharmony_ci 28d96309c9Sopenharmony_citypedef int SecInt32; 29d96309c9Sopenharmony_citypedef unsigned int SecUnsignedInt32; 30d96309c9Sopenharmony_citypedef long long SecInt64; 31d96309c9Sopenharmony_citypedef unsigned long long SecUnsignedInt64; 32d96309c9Sopenharmony_ci 33d96309c9Sopenharmony_ci#ifndef LOSCFG_BASE_CORE_HILOG 34d96309c9Sopenharmony_cistatic RegisterFunc g_registerFunc = NULL; 35d96309c9Sopenharmony_cistatic atomic_int g_hiLogGetIdCallCount = 0; 36d96309c9Sopenharmony_ci#endif 37d96309c9Sopenharmony_ci 38d96309c9Sopenharmony_ci#ifndef SECUREC_BUFFER_SIZE 39d96309c9Sopenharmony_ci#define SECUREC_BUFFER_SIZE 256 40d96309c9Sopenharmony_ci#endif 41d96309c9Sopenharmony_ci 42d96309c9Sopenharmony_ci#define SECUREC_MAX_PRECISION SECUREC_BUFFER_SIZE 43d96309c9Sopenharmony_ci/* max. # bytes in multibyte char ,see MB_LEN_MAX */ 44d96309c9Sopenharmony_ci#define SECUREC_MB_LEN 16 45d96309c9Sopenharmony_ci 46d96309c9Sopenharmony_ci/* state definitions */ 47d96309c9Sopenharmony_ci#define SECUREC_FLAG_SIGN 0x00001U 48d96309c9Sopenharmony_ci#define SECUREC_FLAG_SIGN_SPACE 0x00002U 49d96309c9Sopenharmony_ci#define SECUREC_FLAG_LEFT 0x00004U 50d96309c9Sopenharmony_ci#define SECUREC_FLAG_LEADZERO 0x00008U 51d96309c9Sopenharmony_ci#define SECUREC_FLAG_LONG 0x00010U 52d96309c9Sopenharmony_ci#define SECUREC_FLAG_SHORT 0x00020U 53d96309c9Sopenharmony_ci#define SECUREC_FLAG_SIGNED 0x00040U 54d96309c9Sopenharmony_ci#define SECUREC_FLAG_ALTERNATE 0x00080U 55d96309c9Sopenharmony_ci#define SECUREC_FLAG_NEGATIVE 0x00100U 56d96309c9Sopenharmony_ci#define SECUREC_FLAG_FORCE_OCTAL 0x00200U 57d96309c9Sopenharmony_ci#define SECUREC_FLAG_LONG_DOUBLE 0x00400U 58d96309c9Sopenharmony_ci#define SECUREC_FLAG_WIDECHAR 0x00800U 59d96309c9Sopenharmony_ci#define SECUREC_FLAG_LONGLONG 0x01000U 60d96309c9Sopenharmony_ci#define SECUREC_FLAG_CHAR 0x02000U 61d96309c9Sopenharmony_ci#define SECUREC_FLAG_POINTER 0x04000U 62d96309c9Sopenharmony_ci#define SECUREC_FLAG_I64 0x08000U 63d96309c9Sopenharmony_ci#define SECUREC_FLAG_PTRDIFF 0x10000U 64d96309c9Sopenharmony_ci#define SECUREC_FLAG_SIZE 0x20000U 65d96309c9Sopenharmony_ci#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT 66d96309c9Sopenharmony_ci#define SECUREC_FLAG_INTMAX 0x40000U 67d96309c9Sopenharmony_ci#endif 68d96309c9Sopenharmony_ci 69d96309c9Sopenharmony_ci 70d96309c9Sopenharmony_citypedef enum { 71d96309c9Sopenharmony_ci STAT_NORMAL, 72d96309c9Sopenharmony_ci STAT_PERCENT, 73d96309c9Sopenharmony_ci STAT_FLAG, 74d96309c9Sopenharmony_ci STAT_WIDTH, 75d96309c9Sopenharmony_ci STAT_DOT, 76d96309c9Sopenharmony_ci STAT_PRECIS, 77d96309c9Sopenharmony_ci STAT_SIZE, 78d96309c9Sopenharmony_ci STAT_TYPE, 79d96309c9Sopenharmony_ci STAT_INVALID 80d96309c9Sopenharmony_ci} SecFmtState; 81d96309c9Sopenharmony_ci 82d96309c9Sopenharmony_ci#define SECUREC_PRINTF_TRUNCATE (-2) 83d96309c9Sopenharmony_citypedef struct { 84d96309c9Sopenharmony_ci int count; 85d96309c9Sopenharmony_ci char *cur; 86d96309c9Sopenharmony_ci} SecPrintfStream; 87d96309c9Sopenharmony_ci#define SECUREC_ERROR_INVALID_PARAMTER(msg) printf("%s invalid argument\n", msg) 88d96309c9Sopenharmony_ci#define SECUREC_ERROR_INVALID_RANGE(msg) printf("%s invalid dest buffer size\n", msg) 89d96309c9Sopenharmony_ci#define SECUREC_ERROR_BUFFER_OVERLAP(msg) printf("%s buffer overlap\n", msg) 90d96309c9Sopenharmony_ci 91d96309c9Sopenharmony_ci// Log type 92d96309c9Sopenharmony_ci#define LOG_BUF_SIZE (1024) 93d96309c9Sopenharmony_ci#define MAX_DOMAIN_TAG_SIZE 64 94d96309c9Sopenharmony_ci#define IOV_SIZE (3) 95d96309c9Sopenharmony_ci#define HILOG_IOV_SIZE 4 96d96309c9Sopenharmony_ci#define HITRACE_BUF_SIZE 128 97d96309c9Sopenharmony_ci#define MAX_INIT_RETRIES (500) 98d96309c9Sopenharmony_ci#define DOMAIN_MIN 0x0D000000 99d96309c9Sopenharmony_ci#define DOMAIN_MAX 0x0E000000 100d96309c9Sopenharmony_ci 101d96309c9Sopenharmony_ci#define SECUREC_FLOAT_BUFSIZE (309 + 40) /* max float point value */ 102d96309c9Sopenharmony_ci#define SECUREC_FLOAT_BUFSIZE_LB (4932 + 40) /* max long double value */ 103d96309c9Sopenharmony_ci 104d96309c9Sopenharmony_ci#define SECUREC_INT_MAX 2147483647 105d96309c9Sopenharmony_ci 106d96309c9Sopenharmony_ci#define SECUREC_MUL10(x) ((((x) << 2) + (x)) << 1) 107d96309c9Sopenharmony_ci#define SECUREC_INT_MAX_DIV_TEN 21474836 108d96309c9Sopenharmony_ci#define SECUREC_MUL10_ADD_BEYOND_MAX(val) (((val) > SECUREC_INT_MAX_DIV_TEN)) 109d96309c9Sopenharmony_ci 110d96309c9Sopenharmony_ci#ifdef SECUREC_STACK_SIZE_LESS_THAN_1K 111d96309c9Sopenharmony_ci#define SECUREC_FMT_STR_LEN (8) 112d96309c9Sopenharmony_ci#else 113d96309c9Sopenharmony_ci#define SECUREC_FMT_STR_LEN (16) 114d96309c9Sopenharmony_ci#endif 115d96309c9Sopenharmony_ci 116d96309c9Sopenharmony_ci#ifndef SECUREC_PUTC 117d96309c9Sopenharmony_ci#define SECUREC_PUTC(_c, _stream) ((--(_stream)->count >= 0) ? (*(_stream)->cur++ = (char)(_c)) : EOF) 118d96309c9Sopenharmony_ci#endif 119d96309c9Sopenharmony_ci/* to clear e835 */ 120d96309c9Sopenharmony_ci#ifndef SECUREC_PUTC_ZERO 121d96309c9Sopenharmony_ci#define SECUREC_PUTC_ZERO(_stream) ((--(_stream)->count >= 0) ? ((*(_stream)->cur++ = (char)('\0'))) : EOF) 122d96309c9Sopenharmony_ci#endif 123d96309c9Sopenharmony_ci 124d96309c9Sopenharmony_ci#ifndef SECUREC_MALLOC 125d96309c9Sopenharmony_ci#define SECUREC_MALLOC(x) malloc((size_t)(x)) 126d96309c9Sopenharmony_ci#endif 127d96309c9Sopenharmony_ci 128d96309c9Sopenharmony_ci#ifndef SECUREC_FREE 129d96309c9Sopenharmony_ci#define SECUREC_FREE(x) free((void *)(x)) 130d96309c9Sopenharmony_ci#endif 131d96309c9Sopenharmony_ci 132d96309c9Sopenharmony_citypedef char SecChar; 133d96309c9Sopenharmony_ci#define SECUREC_CHAR(x) x 134d96309c9Sopenharmony_ci/* put a char to output */ 135d96309c9Sopenharmony_ci#define SECUREC_PUTC(_c, _stream) ((--(_stream)->count >= 0) ? (*(_stream)->cur++ = (char)(_c)) : EOF) 136d96309c9Sopenharmony_ci/* to clear e835 */ 137d96309c9Sopenharmony_ci 138d96309c9Sopenharmony_ci#define SECUREC_PUTC_ZERO(_stream) ((--(_stream)->count >= 0) ? ((*(_stream)->cur++ = (char)('\0'))) : EOF) 139d96309c9Sopenharmony_ci 140d96309c9Sopenharmony_ciint SecPutWcharStrEndingZero(SecPrintfStream *str, int zeroNum) 141d96309c9Sopenharmony_ci{ 142d96309c9Sopenharmony_ci int succeed = 0; 143d96309c9Sopenharmony_ci int i; 144d96309c9Sopenharmony_ci 145d96309c9Sopenharmony_ci for (i = 0; i < zeroNum && (SECUREC_PUTC_ZERO(str) != EOF); ++i) { 146d96309c9Sopenharmony_ci } 147d96309c9Sopenharmony_ci if (i == zeroNum) { 148d96309c9Sopenharmony_ci succeed = 1; 149d96309c9Sopenharmony_ci } 150d96309c9Sopenharmony_ci return succeed; 151d96309c9Sopenharmony_ci} 152d96309c9Sopenharmony_ci 153d96309c9Sopenharmony_civoid SecWriteMultiChar(char ch, int num, SecPrintfStream *f, int *pnumwritten) 154d96309c9Sopenharmony_ci{ 155d96309c9Sopenharmony_ci int count = num; 156d96309c9Sopenharmony_ci while (count-- > 0) { 157d96309c9Sopenharmony_ci if (SECUREC_PUTC(ch, f) == EOF) { 158d96309c9Sopenharmony_ci *pnumwritten = -1; 159d96309c9Sopenharmony_ci break; 160d96309c9Sopenharmony_ci } else { 161d96309c9Sopenharmony_ci ++(*pnumwritten); 162d96309c9Sopenharmony_ci } 163d96309c9Sopenharmony_ci } 164d96309c9Sopenharmony_ci} 165d96309c9Sopenharmony_ci 166d96309c9Sopenharmony_civoid SecWriteString(const char *string, int len, SecPrintfStream *f, int *pnumwritten) 167d96309c9Sopenharmony_ci{ 168d96309c9Sopenharmony_ci const char *str = string; 169d96309c9Sopenharmony_ci int count = len; 170d96309c9Sopenharmony_ci while (count-- > 0) { 171d96309c9Sopenharmony_ci if (SECUREC_PUTC(*str, f) == EOF) { 172d96309c9Sopenharmony_ci *pnumwritten = -1; 173d96309c9Sopenharmony_ci break; 174d96309c9Sopenharmony_ci } else { 175d96309c9Sopenharmony_ci ++(*pnumwritten); 176d96309c9Sopenharmony_ci ++str; 177d96309c9Sopenharmony_ci } 178d96309c9Sopenharmony_ci } 179d96309c9Sopenharmony_ci} 180d96309c9Sopenharmony_ci 181d96309c9Sopenharmony_ci#ifndef LOSCFG_BASE_CORE_HILOG 182d96309c9Sopenharmony_ciint HiLogRegisterGetIdFun(RegisterFunc registerFunc) 183d96309c9Sopenharmony_ci{ 184d96309c9Sopenharmony_ci if (g_registerFunc != NULL) { 185d96309c9Sopenharmony_ci return -1; 186d96309c9Sopenharmony_ci } 187d96309c9Sopenharmony_ci g_registerFunc = registerFunc; 188d96309c9Sopenharmony_ci return 0; 189d96309c9Sopenharmony_ci} 190d96309c9Sopenharmony_ci 191d96309c9Sopenharmony_civoid HiLogUnregisterGetIdFun(RegisterFunc registerFunc) 192d96309c9Sopenharmony_ci{ 193d96309c9Sopenharmony_ci if (g_registerFunc != registerFunc) { 194d96309c9Sopenharmony_ci return; 195d96309c9Sopenharmony_ci } 196d96309c9Sopenharmony_ci 197d96309c9Sopenharmony_ci g_registerFunc = NULL; 198d96309c9Sopenharmony_ci while (atomic_load(&g_hiLogGetIdCallCount) != 0) { 199d96309c9Sopenharmony_ci /* do nothing, just wait current callback return */ 200d96309c9Sopenharmony_ci } 201d96309c9Sopenharmony_ci 202d96309c9Sopenharmony_ci return; 203d96309c9Sopenharmony_ci} 204d96309c9Sopenharmony_ci#endif 205d96309c9Sopenharmony_ci 206d96309c9Sopenharmony_ci#define SECUREC_WRITE_MULTI_CHAR SecWriteMultiChar 207d96309c9Sopenharmony_ci#define SECUREC_WRITE_STRING SecWriteString 208d96309c9Sopenharmony_ci 209d96309c9Sopenharmony_citypedef struct { 210d96309c9Sopenharmony_ci unsigned int flags; 211d96309c9Sopenharmony_ci int fldWidth; 212d96309c9Sopenharmony_ci int precision; 213d96309c9Sopenharmony_ci int bufferIsWide; /* flag for buffer contains wide chars */ 214d96309c9Sopenharmony_ci int dynWidth; /* %* 1 width from variable parameter ;0 not */ 215d96309c9Sopenharmony_ci int dynPrecision; /* %.* 1 precision from variable parameter ;0 not */ 216d96309c9Sopenharmony_ci} SecFormatAttr; 217d96309c9Sopenharmony_ci 218d96309c9Sopenharmony_citypedef union { 219d96309c9Sopenharmony_ci char *str; /* not a null terminated string */ 220d96309c9Sopenharmony_ci wchar_t *wStr; 221d96309c9Sopenharmony_ci} SecFormatBuf; 222d96309c9Sopenharmony_ci 223d96309c9Sopenharmony_citypedef union { 224d96309c9Sopenharmony_ci char str[SECUREC_BUFFER_SIZE + 1]; 225d96309c9Sopenharmony_ci#ifdef SECUREC_FOR_WCHAR 226d96309c9Sopenharmony_ci wchar_t wStr[SECUREC_BUFFER_SIZE + 1]; 227d96309c9Sopenharmony_ci#endif 228d96309c9Sopenharmony_ci} SecBuffer; 229d96309c9Sopenharmony_ci 230d96309c9Sopenharmony_ci#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT 231d96309c9Sopenharmony_ci/* to clear e506 warning */ 232d96309c9Sopenharmony_cistatic int SecIsSameSize(size_t sizeA, size_t sizeB) 233d96309c9Sopenharmony_ci{ 234d96309c9Sopenharmony_ci return sizeA == sizeB; 235d96309c9Sopenharmony_ci} 236d96309c9Sopenharmony_ci#endif 237d96309c9Sopenharmony_ci#define SECUREC_WHILE_ZERO while (0) 238d96309c9Sopenharmony_ci 239d96309c9Sopenharmony_ci#define SECUREC_SPECIAL(_val, Base) case Base: \ 240d96309c9Sopenharmony_ci do { \ 241d96309c9Sopenharmony_ci *--formatBuf.str = digits[_val % Base]; \ 242d96309c9Sopenharmony_ci } while ((_val /= Base) != 0) 243d96309c9Sopenharmony_ci 244d96309c9Sopenharmony_ci#define SECUREC_SAFE_WRITE_PREFIX(src, txtLen, _stream, outChars) \ 245d96309c9Sopenharmony_ci do { \ 246d96309c9Sopenharmony_ci for (ii = 0; ii < txtLen; ++ii) { \ 247d96309c9Sopenharmony_ci *((SecChar *)(void *)(_stream->cur)) = *(src); \ 248d96309c9Sopenharmony_ci _stream->cur += sizeof(SecChar); \ 249d96309c9Sopenharmony_ci ++(src); \ 250d96309c9Sopenharmony_ci } \ 251d96309c9Sopenharmony_ci _stream->count -= txtLen * (int)(sizeof(SecChar)); \ 252d96309c9Sopenharmony_ci *(outChars) = *(outChars) + (txtLen); \ 253d96309c9Sopenharmony_ci } \ 254d96309c9Sopenharmony_ci SECUREC_WHILE_ZERO 255d96309c9Sopenharmony_ci#define MOBILE_NUMBER_LENGTH 12 256d96309c9Sopenharmony_ci#define SECUREC_SAFE_WRITE_STR(src, txtLen, _stream, outChars) \ 257d96309c9Sopenharmony_ci do { \ 258d96309c9Sopenharmony_ci if (txtLen < MOBILE_NUMBER_LENGTH) { \ 259d96309c9Sopenharmony_ci for (ii = 0; ii < txtLen; ++ii) { \ 260d96309c9Sopenharmony_ci *((SecChar *)(void *)(_stream->cur)) = *(src); \ 261d96309c9Sopenharmony_ci _stream->cur += sizeof(SecChar); \ 262d96309c9Sopenharmony_ci ++(src); \ 263d96309c9Sopenharmony_ci } \ 264d96309c9Sopenharmony_ci } else { \ 265d96309c9Sopenharmony_ci (void)memcpy_s(_stream->cur, ((size_t)(unsigned int)txtLen * (sizeof(SecChar))), src, \ 266d96309c9Sopenharmony_ci ((size_t)(unsigned int)txtLen * (sizeof(SecChar)))); \ 267d96309c9Sopenharmony_ci _stream->cur += (size_t)(unsigned int)txtLen * (sizeof(SecChar)); \ 268d96309c9Sopenharmony_ci } \ 269d96309c9Sopenharmony_ci _stream->count -= txtLen * (int)(sizeof(SecChar)); \ 270d96309c9Sopenharmony_ci *(outChars) = *(outChars) + (txtLen); \ 271d96309c9Sopenharmony_ci } \ 272d96309c9Sopenharmony_ci SECUREC_WHILE_ZERO 273d96309c9Sopenharmony_ci 274d96309c9Sopenharmony_ci#define SECUREC_SAFE_WRITE_CHAR(_ch, _stream, outChars) do { \ 275d96309c9Sopenharmony_ci *((SecChar *)(void *)(_stream->cur)) = (SecChar)_ch; \ 276d96309c9Sopenharmony_ci _stream->cur += sizeof(SecChar); \ 277d96309c9Sopenharmony_ci _stream->count -= (int)(sizeof(SecChar)); \ 278d96309c9Sopenharmony_ci *(outChars) = *(outChars) + 1; \ 279d96309c9Sopenharmony_ci } \ 280d96309c9Sopenharmony_ci SECUREC_WHILE_ZERO 281d96309c9Sopenharmony_ci 282d96309c9Sopenharmony_ci#define SECUREC_SAFE_PADDING(padChar, padLen, _stream, outChars) \ 283d96309c9Sopenharmony_ci do { \ 284d96309c9Sopenharmony_ci for (ii = 0; ii < padLen; ++ii) { \ 285d96309c9Sopenharmony_ci *((SecChar *)(void *)(_stream->cur)) = (SecChar)padChar; \ 286d96309c9Sopenharmony_ci _stream->cur += sizeof(SecChar); \ 287d96309c9Sopenharmony_ci } \ 288d96309c9Sopenharmony_ci _stream->count -= padLen * (int)(sizeof(SecChar)); \ 289d96309c9Sopenharmony_ci *(outChars) = *(outChars) + (padLen); \ 290d96309c9Sopenharmony_ci } \ 291d96309c9Sopenharmony_ci SECUREC_WHILE_ZERO 292d96309c9Sopenharmony_ci 293d96309c9Sopenharmony_ci/* The count variable can be reduced to 0, and the external function complements the \0 terminator. */ 294d96309c9Sopenharmony_ci#define SECUREC_IS_REST_BUF_ENOUGH(needLen) ((int)(stream->count - (int)needLen * (int)(sizeof(SecChar))) >= 0) 295d96309c9Sopenharmony_ci 296d96309c9Sopenharmony_ci#define SECUREC_FMT_STATE_OFFSET 256 297d96309c9Sopenharmony_ci#ifdef SECUREC_FOR_WCHAR 298d96309c9Sopenharmony_ci#define SECUREC_FMT_TYPE(c, fmtTable) \ 299d96309c9Sopenharmony_ci ((((unsigned int)(int)(c)) <= (unsigned int)(int)SECUREC_CHAR('~')) ? (fmtTable[(unsigned char)(c)]) : 0) 300d96309c9Sopenharmony_ci#define SECUREC_DECODE_STATE(c, fmtTable, laststate) \ 301d96309c9Sopenharmony_ci (SecFmtState)(((fmtTable[(SECUREC_FMT_TYPE(c, fmtTable)) * ((unsigned char)STAT_INVALID + 1) + \ 302d96309c9Sopenharmony_ci (unsigned char)(laststate) + SECUREC_FMT_STATE_OFFSET]))) 303d96309c9Sopenharmony_ci#else 304d96309c9Sopenharmony_ci#define SECUREC_DECODE_STATE(c, fmtTable, laststate) \ 305d96309c9Sopenharmony_ci (SecFmtState)((fmtTable[(fmtTable[(unsigned char)(c)]) * ((unsigned char)STAT_INVALID + 1) + \ 306d96309c9Sopenharmony_ci (unsigned char)(laststate) + SECUREC_FMT_STATE_OFFSET])) 307d96309c9Sopenharmony_ci#endif 308d96309c9Sopenharmony_ci 309d96309c9Sopenharmony_cistatic void SecDecodeFlags(SecChar ch, SecFormatAttr *attr) 310d96309c9Sopenharmony_ci{ 311d96309c9Sopenharmony_ci switch (ch) { 312d96309c9Sopenharmony_ci case SECUREC_CHAR(' '): 313d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_SIGN_SPACE; 314d96309c9Sopenharmony_ci break; 315d96309c9Sopenharmony_ci case SECUREC_CHAR('+'): 316d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_SIGN; 317d96309c9Sopenharmony_ci break; 318d96309c9Sopenharmony_ci case SECUREC_CHAR('-'): 319d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_LEFT; 320d96309c9Sopenharmony_ci break; 321d96309c9Sopenharmony_ci case SECUREC_CHAR('0'): 322d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_LEADZERO; /* add zero th the front */ 323d96309c9Sopenharmony_ci break; 324d96309c9Sopenharmony_ci case SECUREC_CHAR('#'): 325d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_ALTERNATE; /* output %x with 0x */ 326d96309c9Sopenharmony_ci break; 327d96309c9Sopenharmony_ci default: 328d96309c9Sopenharmony_ci break; 329d96309c9Sopenharmony_ci } 330d96309c9Sopenharmony_ci return; 331d96309c9Sopenharmony_ci} 332d96309c9Sopenharmony_ci 333d96309c9Sopenharmony_cistatic int SecDecodeSize(SecChar ch, SecFormatAttr *attr, const SecChar **format) 334d96309c9Sopenharmony_ci{ 335d96309c9Sopenharmony_ci switch (ch) { 336d96309c9Sopenharmony_ci#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT 337d96309c9Sopenharmony_ci case SECUREC_CHAR('j'): 338d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_INTMAX; 339d96309c9Sopenharmony_ci break; 340d96309c9Sopenharmony_ci#endif 341d96309c9Sopenharmony_ci case SECUREC_CHAR('q'): /* fall-through */ /* FALLTHRU */ 342d96309c9Sopenharmony_ci case SECUREC_CHAR('L'): 343d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_LONGLONG | SECUREC_FLAG_LONG_DOUBLE; 344d96309c9Sopenharmony_ci break; 345d96309c9Sopenharmony_ci case SECUREC_CHAR('l'): 346d96309c9Sopenharmony_ci if (**format == SECUREC_CHAR('l')) { 347d96309c9Sopenharmony_ci ++(*format); 348d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_LONGLONG; /* long long */ 349d96309c9Sopenharmony_ci } else { 350d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_LONG; /* long int or wchar_t */ 351d96309c9Sopenharmony_ci } 352d96309c9Sopenharmony_ci break; 353d96309c9Sopenharmony_ci case SECUREC_CHAR('t'): 354d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_PTRDIFF; 355d96309c9Sopenharmony_ci break; 356d96309c9Sopenharmony_ci#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT 357d96309c9Sopenharmony_ci case SECUREC_CHAR('z'): 358d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_SIZE; 359d96309c9Sopenharmony_ci break; 360d96309c9Sopenharmony_ci case SECUREC_CHAR('Z'): 361d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_SIZE; 362d96309c9Sopenharmony_ci break; 363d96309c9Sopenharmony_ci#endif 364d96309c9Sopenharmony_ci 365d96309c9Sopenharmony_ci case SECUREC_CHAR('I'): 366d96309c9Sopenharmony_ci#ifdef SECUREC_ON_64BITS 367d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_I64; /* %I to INT64 */ 368d96309c9Sopenharmony_ci#endif 369d96309c9Sopenharmony_ci if ((**format == SECUREC_CHAR('6')) && (*((*format) + 1) == SECUREC_CHAR('4'))) { 370d96309c9Sopenharmony_ci (*format) += 2; /* Add 2 to skip I64 */ 371d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_I64; /* %I64 to INT64 */ 372d96309c9Sopenharmony_ci } else if ((**format == SECUREC_CHAR('3')) && (*((*format) + 1) == SECUREC_CHAR('2'))) { 373d96309c9Sopenharmony_ci (*format) += 2; /* Add 2 to skip I32 */ 374d96309c9Sopenharmony_ci attr->flags &= ~SECUREC_FLAG_I64; /* %I64 to INT32 */ 375d96309c9Sopenharmony_ci } else if ((**format == SECUREC_CHAR('d')) || (**format == SECUREC_CHAR('i')) || 376d96309c9Sopenharmony_ci (**format == SECUREC_CHAR('o')) || (**format == SECUREC_CHAR('u')) || (**format == SECUREC_CHAR('x')) || 377d96309c9Sopenharmony_ci (**format == SECUREC_CHAR('X'))) { 378d96309c9Sopenharmony_ci /* do nothing */ 379d96309c9Sopenharmony_ci } else { 380d96309c9Sopenharmony_ci /* Compatibility code for "%I" just print I */ 381d96309c9Sopenharmony_ci return -1; 382d96309c9Sopenharmony_ci } 383d96309c9Sopenharmony_ci break; 384d96309c9Sopenharmony_ci 385d96309c9Sopenharmony_ci case SECUREC_CHAR('h'): 386d96309c9Sopenharmony_ci if (**format == SECUREC_CHAR('h')) 387d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_CHAR; /* char */ 388d96309c9Sopenharmony_ci else 389d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_SHORT; /* short int */ 390d96309c9Sopenharmony_ci break; 391d96309c9Sopenharmony_ci 392d96309c9Sopenharmony_ci case SECUREC_CHAR('w'): 393d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_WIDECHAR; /* wide char */ 394d96309c9Sopenharmony_ci break; 395d96309c9Sopenharmony_ci default: 396d96309c9Sopenharmony_ci break; 397d96309c9Sopenharmony_ci } 398d96309c9Sopenharmony_ci 399d96309c9Sopenharmony_ci return 0; 400d96309c9Sopenharmony_ci} 401d96309c9Sopenharmony_ci 402d96309c9Sopenharmony_cistatic int SecDecodeTypeC(SecFormatAttr *attr, unsigned int cValue, SecFormatBuf *formatBuf, SecBuffer *buffer) 403d96309c9Sopenharmony_ci{ 404d96309c9Sopenharmony_ci int textLen; 405d96309c9Sopenharmony_ci wchar_t wchar; 406d96309c9Sopenharmony_ci 407d96309c9Sopenharmony_ci#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT)) && !(defined(__hpux)) && !(defined(SECUREC_ON_SOLARIS)) 408d96309c9Sopenharmony_ci attr->flags &= ~SECUREC_FLAG_LEADZERO; 409d96309c9Sopenharmony_ci#endif 410d96309c9Sopenharmony_ci 411d96309c9Sopenharmony_ci#ifdef SECUREC_FOR_WCHAR 412d96309c9Sopenharmony_ci attr->bufferIsWide = 1; 413d96309c9Sopenharmony_ci wchar = (wchar_t)cValue; 414d96309c9Sopenharmony_ci if (attr->flags & SECUREC_FLAG_SHORT) { 415d96309c9Sopenharmony_ci /* multibyte character to wide character */ 416d96309c9Sopenharmony_ci char tempchar[2]; 417d96309c9Sopenharmony_ci tempchar[0] = (char)(wchar & 0x00ff); 418d96309c9Sopenharmony_ci tempchar[1] = '\0'; 419d96309c9Sopenharmony_ci 420d96309c9Sopenharmony_ci if (mbtowc(buffer->wStr, tempchar, sizeof(tempchar)) < 0) { 421d96309c9Sopenharmony_ci return -1; 422d96309c9Sopenharmony_ci } 423d96309c9Sopenharmony_ci } else { 424d96309c9Sopenharmony_ci buffer->wStr[0] = wchar; 425d96309c9Sopenharmony_ci } 426d96309c9Sopenharmony_ci formatBuf->wStr = buffer->wStr; 427d96309c9Sopenharmony_ci textLen = 1; /* only 1 wide character */ 428d96309c9Sopenharmony_ci#else 429d96309c9Sopenharmony_ci attr->bufferIsWide = 0; 430d96309c9Sopenharmony_ci if (attr->flags & (SECUREC_FLAG_LONG | SECUREC_FLAG_WIDECHAR)) { 431d96309c9Sopenharmony_ci wchar = (wchar_t)cValue; 432d96309c9Sopenharmony_ci /* wide character to multibyte character */ 433d96309c9Sopenharmony_ci // SECUREC_MASK_MSVC_CRT_WARNING 434d96309c9Sopenharmony_ci textLen = wctomb(buffer->str, wchar); 435d96309c9Sopenharmony_ci // SECUREC_END_MASK_MSVC_CRT_WARNING 436d96309c9Sopenharmony_ci if (textLen < 0) { 437d96309c9Sopenharmony_ci return -1; 438d96309c9Sopenharmony_ci } 439d96309c9Sopenharmony_ci } else { 440d96309c9Sopenharmony_ci /* get multibyte character from argument */ 441d96309c9Sopenharmony_ci unsigned short temp; 442d96309c9Sopenharmony_ci temp = (unsigned short)cValue; 443d96309c9Sopenharmony_ci buffer->str[0] = (char)temp; 444d96309c9Sopenharmony_ci textLen = 1; 445d96309c9Sopenharmony_ci } 446d96309c9Sopenharmony_ci formatBuf->str = buffer->str; 447d96309c9Sopenharmony_ci#endif 448d96309c9Sopenharmony_ci 449d96309c9Sopenharmony_ci return textLen; 450d96309c9Sopenharmony_ci} 451d96309c9Sopenharmony_ci 452d96309c9Sopenharmony_cistatic int SecDecodeTypeS(SecFormatAttr *attr, char *argPtr, SecFormatBuf *formatBuf) 453d96309c9Sopenharmony_ci{ 454d96309c9Sopenharmony_ci /* literal string to print null ptr, define it on stack rather than const text area 455d96309c9Sopenharmony_ci is to avoid gcc warning with pointing const text with variable */ 456d96309c9Sopenharmony_ci static char strNullString[8] = "(null)"; 457d96309c9Sopenharmony_ci static wchar_t wStrNullString[8] = { L'(', L'n', L'u', L'l', L'l', L')', L'\0', L'\0' }; 458d96309c9Sopenharmony_ci 459d96309c9Sopenharmony_ci int finalPrecision; 460d96309c9Sopenharmony_ci char *strEnd = NULL; 461d96309c9Sopenharmony_ci wchar_t *wStrEnd = NULL; 462d96309c9Sopenharmony_ci int textLen; 463d96309c9Sopenharmony_ci 464d96309c9Sopenharmony_ci#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT)) && (!defined(SECUREC_ON_UNIX)) 465d96309c9Sopenharmony_ci attr->flags &= ~SECUREC_FLAG_LEADZERO; 466d96309c9Sopenharmony_ci#endif 467d96309c9Sopenharmony_ci finalPrecision = (attr->precision == -1) ? SECUREC_INT_MAX : attr->precision; 468d96309c9Sopenharmony_ci formatBuf->str = argPtr; 469d96309c9Sopenharmony_ci 470d96309c9Sopenharmony_ci#ifdef SECUREC_FOR_WCHAR 471d96309c9Sopenharmony_ci#if defined(SECUREC_COMPATIBLE_LINUX_FORMAT) 472d96309c9Sopenharmony_ci if (!(attr->flags & SECUREC_FLAG_LONG)) { 473d96309c9Sopenharmony_ci attr->flags |= SECUREC_FLAG_SHORT; 474d96309c9Sopenharmony_ci } 475d96309c9Sopenharmony_ci#endif 476d96309c9Sopenharmony_ci if (attr->flags & SECUREC_FLAG_SHORT) { 477d96309c9Sopenharmony_ci if (formatBuf->str == NULL) { /* NULL passed, use special string */ 478d96309c9Sopenharmony_ci formatBuf->str = strNullString; 479d96309c9Sopenharmony_ci } 480d96309c9Sopenharmony_ci strEnd = formatBuf->str; 481d96309c9Sopenharmony_ci for (textLen = 0; textLen < finalPrecision && *strEnd; textLen++) { 482d96309c9Sopenharmony_ci ++strEnd; 483d96309c9Sopenharmony_ci } 484d96309c9Sopenharmony_ci /* textLen now contains length in multibyte chars */ 485d96309c9Sopenharmony_ci } else { 486d96309c9Sopenharmony_ci if (formatBuf->wStr == NULL) { /* NULL passed, use special string */ 487d96309c9Sopenharmony_ci formatBuf->wStr = wStrNullString; 488d96309c9Sopenharmony_ci } 489d96309c9Sopenharmony_ci attr->bufferIsWide = 1; 490d96309c9Sopenharmony_ci wStrEnd = formatBuf->wStr; 491d96309c9Sopenharmony_ci while (finalPrecision-- && *wStrEnd) { 492d96309c9Sopenharmony_ci ++wStrEnd; 493d96309c9Sopenharmony_ci } 494d96309c9Sopenharmony_ci textLen = (int)(wStrEnd - formatBuf->wStr); /* in wchar_ts */ 495d96309c9Sopenharmony_ci /* textLen now contains length in wide chars */ 496d96309c9Sopenharmony_ci } 497d96309c9Sopenharmony_ci#else /* SECUREC_FOR_WCHAR */ 498d96309c9Sopenharmony_ci if (attr->flags & (SECUREC_FLAG_LONG | SECUREC_FLAG_WIDECHAR)) { 499d96309c9Sopenharmony_ci if (formatBuf->wStr == NULL) { /* NULL passed, use special string */ 500d96309c9Sopenharmony_ci formatBuf->wStr = wStrNullString; 501d96309c9Sopenharmony_ci } 502d96309c9Sopenharmony_ci attr->bufferIsWide = 1; 503d96309c9Sopenharmony_ci wStrEnd = formatBuf->wStr; 504d96309c9Sopenharmony_ci while (finalPrecision-- && *wStrEnd) { 505d96309c9Sopenharmony_ci ++wStrEnd; 506d96309c9Sopenharmony_ci } 507d96309c9Sopenharmony_ci textLen = (int)(wStrEnd - formatBuf->wStr); 508d96309c9Sopenharmony_ci } else { 509d96309c9Sopenharmony_ci if (formatBuf->str == NULL) { /* meet NULL, use special string */ 510d96309c9Sopenharmony_ci formatBuf->str = strNullString; 511d96309c9Sopenharmony_ci } 512d96309c9Sopenharmony_ci 513d96309c9Sopenharmony_ci if (finalPrecision == SECUREC_INT_MAX) { 514d96309c9Sopenharmony_ci /* precision NOT assigned */ 515d96309c9Sopenharmony_ci /* The strlen performance is high when the string length is greater than 32 */ 516d96309c9Sopenharmony_ci textLen = (int)strlen(formatBuf->str); 517d96309c9Sopenharmony_ci } else { 518d96309c9Sopenharmony_ci /* precision assigned */ 519d96309c9Sopenharmony_ci strEnd = formatBuf->str; 520d96309c9Sopenharmony_ci while (finalPrecision-- && *strEnd) { 521d96309c9Sopenharmony_ci ++strEnd; 522d96309c9Sopenharmony_ci } 523d96309c9Sopenharmony_ci textLen = (int)(strEnd - formatBuf->str); /* length of the string */ 524d96309c9Sopenharmony_ci } 525d96309c9Sopenharmony_ci } 526d96309c9Sopenharmony_ci 527d96309c9Sopenharmony_ci#endif /* SECUREC_FOR_WCHAR */ 528d96309c9Sopenharmony_ci return textLen; 529d96309c9Sopenharmony_ci} 530d96309c9Sopenharmony_ci 531d96309c9Sopenharmony_ci#define PUBLIC_FLAG_LEN (8) 532d96309c9Sopenharmony_ci#define PRIVATE_FLAG_LEN (9) 533d96309c9Sopenharmony_ci#define PUBLIC_FLAG "{public}" 534d96309c9Sopenharmony_ci#define PRIVATE_FLAG "{private}" 535d96309c9Sopenharmony_ci#define bool _Bool 536d96309c9Sopenharmony_cistatic int HILOG_TRUE = 1; 537d96309c9Sopenharmony_cistatic int HILOG_FALSE = 0; 538d96309c9Sopenharmony_ci 539d96309c9Sopenharmony_ciint HiLogSecOutputS(SecPrintfStream *stream, bool isDebugMode, const char *cformat, va_list arglist) 540d96309c9Sopenharmony_ci{ 541d96309c9Sopenharmony_ci const SecChar *format = cformat; 542d96309c9Sopenharmony_ci 543d96309c9Sopenharmony_ci char *floatBuf = NULL; 544d96309c9Sopenharmony_ci SecFormatBuf formatBuf; 545d96309c9Sopenharmony_ci static const char *ITOA_UPPER_DIGITS = "0123456789ABCDEFX"; 546d96309c9Sopenharmony_ci static const char *ITOA_LOWER_DIGITS = "0123456789abcdefx"; 547d96309c9Sopenharmony_ci const char *digits = ITOA_UPPER_DIGITS; 548d96309c9Sopenharmony_ci int ii = 0; 549d96309c9Sopenharmony_ci 550d96309c9Sopenharmony_ci unsigned int radix; 551d96309c9Sopenharmony_ci int charsOut; /* characters written */ 552d96309c9Sopenharmony_ci 553d96309c9Sopenharmony_ci int prefixLen = 0; 554d96309c9Sopenharmony_ci int padding = 0; 555d96309c9Sopenharmony_ci 556d96309c9Sopenharmony_ci int textLen; /* length of the text */ 557d96309c9Sopenharmony_ci int bufferSize = 0; /* size of formatBuf.str */ 558d96309c9Sopenharmony_ci int noOutput = 0; 559d96309c9Sopenharmony_ci 560d96309c9Sopenharmony_ci SecFmtState state; 561d96309c9Sopenharmony_ci SecFmtState laststate; 562d96309c9Sopenharmony_ci 563d96309c9Sopenharmony_ci SecChar prefix[2] = { 0 }; 564d96309c9Sopenharmony_ci SecChar ch; /* currently read character */ 565d96309c9Sopenharmony_ci static char PRIVACY_STRING[] = "<private>"; 566d96309c9Sopenharmony_ci static wchar_t WPRIVACY_STRING[] = { L'<', L'p', L'r', L'i', L'v', L'a', L't', L'e', L'>', L'\0'}; 567d96309c9Sopenharmony_ci bool isPrivacy = HILOG_TRUE; 568d96309c9Sopenharmony_ci 569d96309c9Sopenharmony_ci static const unsigned char fmtCharTable[337] = { 570d96309c9Sopenharmony_ci /* type 0: nospecial meanin; 571d96309c9Sopenharmony_ci 1: '%'; 572d96309c9Sopenharmony_ci 2: '.' 573d96309c9Sopenharmony_ci 3: '*' 574d96309c9Sopenharmony_ci 4: '0' 575d96309c9Sopenharmony_ci 5: '1' ... '9' 576d96309c9Sopenharmony_ci 6: ' ', '+', '-', '#' 577d96309c9Sopenharmony_ci 7: 'h', 'l', 'L', 'F', 'w' , 'N','z','q','t','j' 578d96309c9Sopenharmony_ci 8: 'd','o','u','i','x','X','e','f','g' 579d96309c9Sopenharmony_ci */ 580d96309c9Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 581d96309c9Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 582d96309c9Sopenharmony_ci 0x06, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0x06, 0x00, 0x06, 0x02, 0x00, 583d96309c9Sopenharmony_ci 0x04, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 584d96309c9Sopenharmony_ci 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x08, 0x08, 0x00, 0x07, 0x00, 0x00, 0x07, 0x00, 0x07, 0x00, 585d96309c9Sopenharmony_ci 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 586d96309c9Sopenharmony_ci 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x07, 0x08, 0x07, 0x00, 0x07, 0x00, 0x00, 0x08, 587d96309c9Sopenharmony_ci 0x08, 0x07, 0x00, 0x08, 0x07, 0x08, 0x00, 0x07, 0x08, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 588d96309c9Sopenharmony_ci /* fill zero for normal char 128 byte for 0x80 - 0xff */ 589d96309c9Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 590d96309c9Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 591d96309c9Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 592d96309c9Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 593d96309c9Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 594d96309c9Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 595d96309c9Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 596d96309c9Sopenharmony_ci 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 597d96309c9Sopenharmony_ci /* state 0: normal 598d96309c9Sopenharmony_ci 1: percent 599d96309c9Sopenharmony_ci 2: flag 600d96309c9Sopenharmony_ci 3: width 601d96309c9Sopenharmony_ci 4: dot 602d96309c9Sopenharmony_ci 5: precis 603d96309c9Sopenharmony_ci 6: size 604d96309c9Sopenharmony_ci 7: type 605d96309c9Sopenharmony_ci 8: invalid 606d96309c9Sopenharmony_ci */ 607d96309c9Sopenharmony_ci 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 0x01, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 608d96309c9Sopenharmony_ci 0x01, 0x00, 0x00, 0x04, 0x04, 0x04, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 0x03, 0x03, 0x08, 0x05, 609d96309c9Sopenharmony_ci 0x08, 0x08, 0x00, 0x00, 0x00, 0x02, 0x02, 0x03, 0x05, 0x05, 0x08, 0x00, 0x00, 0x00, 0x03, 0x03, 610d96309c9Sopenharmony_ci 0x03, 0x05, 0x05, 0x08, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 611d96309c9Sopenharmony_ci 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x00, 0x00, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 612d96309c9Sopenharmony_ci 0x00 613d96309c9Sopenharmony_ci }; 614d96309c9Sopenharmony_ci 615d96309c9Sopenharmony_ci SecFormatAttr formatAttr; 616d96309c9Sopenharmony_ci SecBuffer buffer; 617d96309c9Sopenharmony_ci formatAttr.flags = 0; 618d96309c9Sopenharmony_ci formatAttr.bufferIsWide = 0; /* flag for buffer contains wide chars */ 619d96309c9Sopenharmony_ci formatAttr.fldWidth = 0; 620d96309c9Sopenharmony_ci formatAttr.precision = 0; 621d96309c9Sopenharmony_ci formatAttr.dynWidth = 0; 622d96309c9Sopenharmony_ci formatAttr.dynPrecision = 0; 623d96309c9Sopenharmony_ci 624d96309c9Sopenharmony_ci charsOut = 0; 625d96309c9Sopenharmony_ci textLen = 0; 626d96309c9Sopenharmony_ci state = STAT_NORMAL; /* starting state */ 627d96309c9Sopenharmony_ci formatBuf.str = NULL; 628d96309c9Sopenharmony_ci 629d96309c9Sopenharmony_ci /* loop each format character */ 630d96309c9Sopenharmony_ci /* remove format != NULL */ 631d96309c9Sopenharmony_ci while ((ch = *format++) != SECUREC_CHAR('\0') && charsOut >= 0) { 632d96309c9Sopenharmony_ci laststate = state; 633d96309c9Sopenharmony_ci state = SECUREC_DECODE_STATE(ch, fmtCharTable, laststate); 634d96309c9Sopenharmony_ci 635d96309c9Sopenharmony_ci switch (state) { 636d96309c9Sopenharmony_ci case STAT_NORMAL: 637d96309c9Sopenharmony_ci 638d96309c9Sopenharmony_ci NORMAL_CHAR: 639d96309c9Sopenharmony_ci 640d96309c9Sopenharmony_ci /* normal state, write character */ 641d96309c9Sopenharmony_ci if (SECUREC_IS_REST_BUF_ENOUGH(1 /* only one char */)) { 642d96309c9Sopenharmony_ci SECUREC_SAFE_WRITE_CHAR(ch, stream, &charsOut); /* char * cast to wchar * */ 643d96309c9Sopenharmony_ci } else { 644d96309c9Sopenharmony_ci#ifdef SECUREC_FOR_WCHAR 645d96309c9Sopenharmony_ci SECUREC_WRITE_CHAR(ch, stream, &charsOut); 646d96309c9Sopenharmony_ci#else 647d96309c9Sopenharmony_ci /* optimize function call to code */ 648d96309c9Sopenharmony_ci charsOut = -1; 649d96309c9Sopenharmony_ci stream->count = -1; 650d96309c9Sopenharmony_ci#endif 651d96309c9Sopenharmony_ci } 652d96309c9Sopenharmony_ci 653d96309c9Sopenharmony_ci continue; 654d96309c9Sopenharmony_ci 655d96309c9Sopenharmony_ci case STAT_PERCENT: 656d96309c9Sopenharmony_ci /* set default values */ 657d96309c9Sopenharmony_ci prefixLen = 0; 658d96309c9Sopenharmony_ci noOutput = 0; 659d96309c9Sopenharmony_ci formatAttr.flags = 0; 660d96309c9Sopenharmony_ci formatAttr.fldWidth = 0; 661d96309c9Sopenharmony_ci formatAttr.precision = -1; 662d96309c9Sopenharmony_ci formatAttr.bufferIsWide = 0; 663d96309c9Sopenharmony_ci isPrivacy = HILOG_TRUE; 664d96309c9Sopenharmony_ci if (0 == strncmp(format, PUBLIC_FLAG, PUBLIC_FLAG_LEN)) { 665d96309c9Sopenharmony_ci isPrivacy = HILOG_FALSE; 666d96309c9Sopenharmony_ci format += PUBLIC_FLAG_LEN; 667d96309c9Sopenharmony_ci break; 668d96309c9Sopenharmony_ci } 669d96309c9Sopenharmony_ci if (0 == strncmp(format, PRIVATE_FLAG, PRIVATE_FLAG_LEN)) { 670d96309c9Sopenharmony_ci isPrivacy = HILOG_TRUE; 671d96309c9Sopenharmony_ci format += PRIVATE_FLAG_LEN; 672d96309c9Sopenharmony_ci break; 673d96309c9Sopenharmony_ci } 674d96309c9Sopenharmony_ci 675d96309c9Sopenharmony_ci if (*format == SECUREC_CHAR('%')) { 676d96309c9Sopenharmony_ci isPrivacy = HILOG_FALSE; 677d96309c9Sopenharmony_ci } 678d96309c9Sopenharmony_ci 679d96309c9Sopenharmony_ci break; 680d96309c9Sopenharmony_ci 681d96309c9Sopenharmony_ci case STAT_FLAG: 682d96309c9Sopenharmony_ci /* set flag based on which flag character */ 683d96309c9Sopenharmony_ci SecDecodeFlags(ch, &formatAttr); 684d96309c9Sopenharmony_ci break; 685d96309c9Sopenharmony_ci 686d96309c9Sopenharmony_ci case STAT_WIDTH: 687d96309c9Sopenharmony_ci /* update width value */ 688d96309c9Sopenharmony_ci if (ch == SECUREC_CHAR('*')) { 689d96309c9Sopenharmony_ci /* get width */ 690d96309c9Sopenharmony_ci formatAttr.fldWidth = (int)va_arg(arglist, int); 691d96309c9Sopenharmony_ci if (formatAttr.fldWidth < 0) { 692d96309c9Sopenharmony_ci formatAttr.flags |= SECUREC_FLAG_LEFT; 693d96309c9Sopenharmony_ci formatAttr.fldWidth = -formatAttr.fldWidth; 694d96309c9Sopenharmony_ci } 695d96309c9Sopenharmony_ci formatAttr.dynWidth = 1; 696d96309c9Sopenharmony_ci } else { 697d96309c9Sopenharmony_ci if (laststate != STAT_WIDTH) { 698d96309c9Sopenharmony_ci formatAttr.fldWidth = 0; 699d96309c9Sopenharmony_ci } 700d96309c9Sopenharmony_ci if (SECUREC_MUL10_ADD_BEYOND_MAX(formatAttr.fldWidth)) { 701d96309c9Sopenharmony_ci return -1; 702d96309c9Sopenharmony_ci } 703d96309c9Sopenharmony_ci formatAttr.fldWidth = 704d96309c9Sopenharmony_ci (int)SECUREC_MUL10((unsigned int)formatAttr.fldWidth) + (ch - SECUREC_CHAR('0')); 705d96309c9Sopenharmony_ci formatAttr.dynWidth = 0; 706d96309c9Sopenharmony_ci } 707d96309c9Sopenharmony_ci break; 708d96309c9Sopenharmony_ci 709d96309c9Sopenharmony_ci case STAT_DOT: 710d96309c9Sopenharmony_ci formatAttr.precision = 0; 711d96309c9Sopenharmony_ci break; 712d96309c9Sopenharmony_ci 713d96309c9Sopenharmony_ci case STAT_PRECIS: 714d96309c9Sopenharmony_ci /* update precison value */ 715d96309c9Sopenharmony_ci if (ch == SECUREC_CHAR('*')) { 716d96309c9Sopenharmony_ci /* get precision from arg list */ 717d96309c9Sopenharmony_ci formatAttr.precision = (int)va_arg(arglist, int); 718d96309c9Sopenharmony_ci if (formatAttr.precision < 0) { 719d96309c9Sopenharmony_ci formatAttr.precision = -1; 720d96309c9Sopenharmony_ci } 721d96309c9Sopenharmony_ci formatAttr.dynPrecision = 1; 722d96309c9Sopenharmony_ci } else { 723d96309c9Sopenharmony_ci /* add digit to current precision */ 724d96309c9Sopenharmony_ci if (SECUREC_MUL10_ADD_BEYOND_MAX(formatAttr.precision)) { 725d96309c9Sopenharmony_ci return -1; 726d96309c9Sopenharmony_ci } 727d96309c9Sopenharmony_ci formatAttr.precision = 728d96309c9Sopenharmony_ci (int)SECUREC_MUL10((unsigned int)formatAttr.precision) + (ch - SECUREC_CHAR('0')); 729d96309c9Sopenharmony_ci formatAttr.dynPrecision = 0; 730d96309c9Sopenharmony_ci } 731d96309c9Sopenharmony_ci break; 732d96309c9Sopenharmony_ci 733d96309c9Sopenharmony_ci case STAT_SIZE: 734d96309c9Sopenharmony_ci /* read a size specifier, set the formatAttr.flags based on it */ 735d96309c9Sopenharmony_ci if (SecDecodeSize(ch, &formatAttr, &format) != 0) { 736d96309c9Sopenharmony_ci /* Compatibility code for "%I" just print I */ 737d96309c9Sopenharmony_ci state = STAT_NORMAL; 738d96309c9Sopenharmony_ci goto NORMAL_CHAR; 739d96309c9Sopenharmony_ci } 740d96309c9Sopenharmony_ci break; 741d96309c9Sopenharmony_ci 742d96309c9Sopenharmony_ci case STAT_TYPE: 743d96309c9Sopenharmony_ci 744d96309c9Sopenharmony_ci switch (ch) { 745d96309c9Sopenharmony_ci case SECUREC_CHAR('C'): 746d96309c9Sopenharmony_ci /* wide char */ 747d96309c9Sopenharmony_ci if (!(formatAttr.flags & (SECUREC_FLAG_SHORT | SECUREC_FLAG_LONG | SECUREC_FLAG_WIDECHAR))) { 748d96309c9Sopenharmony_ci#ifdef SECUREC_FOR_WCHAR 749d96309c9Sopenharmony_ci formatAttr.flags |= SECUREC_FLAG_SHORT; 750d96309c9Sopenharmony_ci#else 751d96309c9Sopenharmony_ci formatAttr.flags |= SECUREC_FLAG_WIDECHAR; 752d96309c9Sopenharmony_ci#endif 753d96309c9Sopenharmony_ci } 754d96309c9Sopenharmony_ci /* fall-through */ 755d96309c9Sopenharmony_ci /* FALLTHRU */ 756d96309c9Sopenharmony_ci case SECUREC_CHAR('c'): { 757d96309c9Sopenharmony_ci unsigned int cValue = (unsigned int)va_arg(arglist, int); 758d96309c9Sopenharmony_ci textLen = SecDecodeTypeC(&formatAttr, cValue, &formatBuf, &buffer); 759d96309c9Sopenharmony_ci if (textLen < 0) { 760d96309c9Sopenharmony_ci noOutput = 1; 761d96309c9Sopenharmony_ci } 762d96309c9Sopenharmony_ci } break; 763d96309c9Sopenharmony_ci case SECUREC_CHAR('S'): /* wide char string */ 764d96309c9Sopenharmony_ci#ifndef SECUREC_FOR_WCHAR 765d96309c9Sopenharmony_ci if (!(formatAttr.flags & (SECUREC_FLAG_SHORT | SECUREC_FLAG_LONG | SECUREC_FLAG_WIDECHAR))) { 766d96309c9Sopenharmony_ci formatAttr.flags |= SECUREC_FLAG_WIDECHAR; 767d96309c9Sopenharmony_ci } 768d96309c9Sopenharmony_ci#else 769d96309c9Sopenharmony_ci if (!(formatAttr.flags & (SECUREC_FLAG_SHORT | SECUREC_FLAG_LONG | SECUREC_FLAG_WIDECHAR))) { 770d96309c9Sopenharmony_ci formatAttr.flags |= SECUREC_FLAG_SHORT; 771d96309c9Sopenharmony_ci } 772d96309c9Sopenharmony_ci#endif 773d96309c9Sopenharmony_ci /* fall-through */ 774d96309c9Sopenharmony_ci /* FALLTHRU */ 775d96309c9Sopenharmony_ci case SECUREC_CHAR('s'): { 776d96309c9Sopenharmony_ci char *argPtr = (char *)va_arg(arglist, char *); 777d96309c9Sopenharmony_ci textLen = SecDecodeTypeS(&formatAttr, argPtr, &formatBuf); 778d96309c9Sopenharmony_ci } break; 779d96309c9Sopenharmony_ci 780d96309c9Sopenharmony_ci case SECUREC_CHAR('n'): 781d96309c9Sopenharmony_ci /* higher risk disable it */ 782d96309c9Sopenharmony_ci return -1; 783d96309c9Sopenharmony_ci 784d96309c9Sopenharmony_ci case SECUREC_CHAR('E'): /* fall-through */ /* FALLTHRU */ 785d96309c9Sopenharmony_ci case SECUREC_CHAR('F'): /* fall-through */ /* FALLTHRU */ 786d96309c9Sopenharmony_ci case SECUREC_CHAR('G'): /* fall-through */ /* FALLTHRU */ 787d96309c9Sopenharmony_ci case SECUREC_CHAR('A'): /* fall-through */ /* FALLTHRU */ 788d96309c9Sopenharmony_ci /* convert format char to lower , use Explicit conversion to clean up compilation warning */ 789d96309c9Sopenharmony_ci ch = (SecChar)(ch + ((SecChar)(SECUREC_CHAR('a')) - (SECUREC_CHAR('A')))); 790d96309c9Sopenharmony_ci /* fall-through */ 791d96309c9Sopenharmony_ci /* FALLTHRU */ 792d96309c9Sopenharmony_ci case SECUREC_CHAR('e'): /* fall-through */ /* FALLTHRU */ 793d96309c9Sopenharmony_ci case SECUREC_CHAR('f'): /* fall-through */ /* FALLTHRU */ 794d96309c9Sopenharmony_ci case SECUREC_CHAR('g'): /* fall-through */ /* FALLTHRU */ 795d96309c9Sopenharmony_ci case SECUREC_CHAR('a'): { 796d96309c9Sopenharmony_ci /* floating point conversion */ 797d96309c9Sopenharmony_ci formatBuf.str = buffer.str; /* output buffer for float string with default size */ 798d96309c9Sopenharmony_ci 799d96309c9Sopenharmony_ci /* compute the precision value */ 800d96309c9Sopenharmony_ci if (formatAttr.precision < 0) { 801d96309c9Sopenharmony_ci formatAttr.precision = 6; /* securec float default precision 6 */ 802d96309c9Sopenharmony_ci } else if (formatAttr.precision == 0 && ch == SECUREC_CHAR('g')) { 803d96309c9Sopenharmony_ci formatAttr.precision = 1; 804d96309c9Sopenharmony_ci } 805d96309c9Sopenharmony_ci 806d96309c9Sopenharmony_ci /* calc buffer size to store long double value */ 807d96309c9Sopenharmony_ci if (formatAttr.flags & SECUREC_FLAG_LONG_DOUBLE) { 808d96309c9Sopenharmony_ci if (formatAttr.precision > (SECUREC_INT_MAX - SECUREC_FLOAT_BUFSIZE_LB)) { 809d96309c9Sopenharmony_ci noOutput = 1; 810d96309c9Sopenharmony_ci break; 811d96309c9Sopenharmony_ci } 812d96309c9Sopenharmony_ci bufferSize = SECUREC_FLOAT_BUFSIZE_LB + formatAttr.precision; 813d96309c9Sopenharmony_ci } else { 814d96309c9Sopenharmony_ci if (formatAttr.precision > (SECUREC_INT_MAX - SECUREC_FLOAT_BUFSIZE)) { 815d96309c9Sopenharmony_ci noOutput = 1; 816d96309c9Sopenharmony_ci break; 817d96309c9Sopenharmony_ci } 818d96309c9Sopenharmony_ci bufferSize = SECUREC_FLOAT_BUFSIZE + formatAttr.precision; 819d96309c9Sopenharmony_ci } 820d96309c9Sopenharmony_ci if (formatAttr.fldWidth > bufferSize) { 821d96309c9Sopenharmony_ci bufferSize = formatAttr.fldWidth; 822d96309c9Sopenharmony_ci } 823d96309c9Sopenharmony_ci 824d96309c9Sopenharmony_ci if (bufferSize >= SECUREC_BUFFER_SIZE) { 825d96309c9Sopenharmony_ci /* the current vlaue of SECUREC_BUFFER_SIZE could NOT store the formatted float string */ 826d96309c9Sopenharmony_ci /* size include '+' and '\0' */ 827d96309c9Sopenharmony_ci floatBuf = (char *)SECUREC_MALLOC(((size_t)(unsigned int)bufferSize + (size_t)2)); 828d96309c9Sopenharmony_ci if (floatBuf != NULL) { 829d96309c9Sopenharmony_ci formatBuf.str = floatBuf; 830d96309c9Sopenharmony_ci } else { 831d96309c9Sopenharmony_ci noOutput = 1; 832d96309c9Sopenharmony_ci break; 833d96309c9Sopenharmony_ci } 834d96309c9Sopenharmony_ci } 835d96309c9Sopenharmony_ci 836d96309c9Sopenharmony_ci { 837d96309c9Sopenharmony_ci /* add following code to call system sprintf API for float number */ 838d96309c9Sopenharmony_ci const SecChar *pFltFmt = format - 2; /* point to the position before 'f' or 'g' */ 839d96309c9Sopenharmony_ci int k = 0; 840d96309c9Sopenharmony_ci int fltFmtStrLen; 841d96309c9Sopenharmony_ci char fltFmtBuf[SECUREC_FMT_STR_LEN]; 842d96309c9Sopenharmony_ci char *fltFmtStr = fltFmtBuf; 843d96309c9Sopenharmony_ci char *fltFmtHeap = NULL; /* to clear warning */ 844d96309c9Sopenharmony_ci 845d96309c9Sopenharmony_ci while (SECUREC_CHAR('%') != *pFltFmt && SECUREC_CHAR('}') != *pFltFmt) { /* must meet '%' */ 846d96309c9Sopenharmony_ci --pFltFmt; 847d96309c9Sopenharmony_ci } 848d96309c9Sopenharmony_ci 849d96309c9Sopenharmony_ci fltFmtStrLen = (int)((format - pFltFmt) + 1); /* with ending terminator */ 850d96309c9Sopenharmony_ci if (fltFmtStrLen > SECUREC_FMT_STR_LEN) { 851d96309c9Sopenharmony_ci /* if SECUREC_FMT_STR_LEN is NOT enough, alloc a new buffer */ 852d96309c9Sopenharmony_ci fltFmtHeap = (char *)SECUREC_MALLOC((size_t)((unsigned int)fltFmtStrLen)); 853d96309c9Sopenharmony_ci if (fltFmtHeap == NULL) { 854d96309c9Sopenharmony_ci noOutput = 1; 855d96309c9Sopenharmony_ci break; 856d96309c9Sopenharmony_ci } else { 857d96309c9Sopenharmony_ci if (SECUREC_CHAR('%') != *pFltFmt) { 858d96309c9Sopenharmony_ci fltFmtHeap[0] = '%'; 859d96309c9Sopenharmony_ci ++k; 860d96309c9Sopenharmony_ci } 861d96309c9Sopenharmony_ci for (; k < fltFmtStrLen - 1; ++k) { 862d96309c9Sopenharmony_ci /* convert wchar to char */ 863d96309c9Sopenharmony_ci fltFmtHeap[k] = (char)(pFltFmt[k]); /* copy the format string */ 864d96309c9Sopenharmony_ci } 865d96309c9Sopenharmony_ci fltFmtHeap[k] = '\0'; 866d96309c9Sopenharmony_ci 867d96309c9Sopenharmony_ci fltFmtStr = fltFmtHeap; 868d96309c9Sopenharmony_ci } 869d96309c9Sopenharmony_ci } else { 870d96309c9Sopenharmony_ci if (SECUREC_CHAR('%') != *pFltFmt) { 871d96309c9Sopenharmony_ci fltFmtBuf[0] = '%'; 872d96309c9Sopenharmony_ci ++k; 873d96309c9Sopenharmony_ci } 874d96309c9Sopenharmony_ci /* purpose of the repeat code is to solve the tool alarm Redundant_Null_Check */ 875d96309c9Sopenharmony_ci for (; k < fltFmtStrLen - 1; ++k) { 876d96309c9Sopenharmony_ci /* convert wchar to char */ 877d96309c9Sopenharmony_ci fltFmtBuf[k] = (char)(pFltFmt[k]); /* copy the format string */ 878d96309c9Sopenharmony_ci } 879d96309c9Sopenharmony_ci fltFmtBuf[k] = '\0'; 880d96309c9Sopenharmony_ci } 881d96309c9Sopenharmony_ci 882d96309c9Sopenharmony_ci#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT 883d96309c9Sopenharmony_ci if (formatAttr.flags & SECUREC_FLAG_LONG_DOUBLE) { 884d96309c9Sopenharmony_ci long double tmp = (long double)va_arg(arglist, long double); 885d96309c9Sopenharmony_ci /* call system sprintf to format float value */ 886d96309c9Sopenharmony_ci if (formatAttr.dynWidth && formatAttr.dynPrecision) { 887d96309c9Sopenharmony_ci textLen = sprintf_s(formatBuf.str, SECUREC_BUFFER_SIZE + 1, (char *)fltFmtStr, 888d96309c9Sopenharmony_ci formatAttr.fldWidth, formatAttr.precision, tmp); 889d96309c9Sopenharmony_ci } else if (formatAttr.dynWidth) { 890d96309c9Sopenharmony_ci textLen = sprintf_s(formatBuf.str, SECUREC_BUFFER_SIZE + 1, (char *)fltFmtStr, 891d96309c9Sopenharmony_ci formatAttr.fldWidth, tmp); 892d96309c9Sopenharmony_ci } else if (formatAttr.dynPrecision) { 893d96309c9Sopenharmony_ci textLen = sprintf_s(formatBuf.str, SECUREC_BUFFER_SIZE + 1, (char *)fltFmtStr, 894d96309c9Sopenharmony_ci formatAttr.precision, tmp); 895d96309c9Sopenharmony_ci } else { 896d96309c9Sopenharmony_ci textLen = sprintf_s(formatBuf.str, SECUREC_BUFFER_SIZE + 1, (char *)fltFmtStr, tmp); 897d96309c9Sopenharmony_ci } 898d96309c9Sopenharmony_ci } else 899d96309c9Sopenharmony_ci#endif 900d96309c9Sopenharmony_ci { 901d96309c9Sopenharmony_ci double tmp = (double)va_arg(arglist, double); 902d96309c9Sopenharmony_ci if (formatAttr.dynWidth && formatAttr.dynPrecision) { 903d96309c9Sopenharmony_ci textLen = sprintf_s(formatBuf.str, SECUREC_BUFFER_SIZE + 1, (char *)fltFmtStr, 904d96309c9Sopenharmony_ci formatAttr.fldWidth, formatAttr.precision, tmp); 905d96309c9Sopenharmony_ci } else if (formatAttr.dynWidth) { 906d96309c9Sopenharmony_ci textLen = sprintf_s(formatBuf.str, SECUREC_BUFFER_SIZE + 1, (char *)fltFmtStr, 907d96309c9Sopenharmony_ci formatAttr.fldWidth, tmp); 908d96309c9Sopenharmony_ci } else if (formatAttr.dynPrecision) { 909d96309c9Sopenharmony_ci textLen = sprintf_s(formatBuf.str, SECUREC_BUFFER_SIZE + 1, (char *)fltFmtStr, 910d96309c9Sopenharmony_ci formatAttr.precision, tmp); 911d96309c9Sopenharmony_ci } else { 912d96309c9Sopenharmony_ci textLen = sprintf_s(formatBuf.str, SECUREC_BUFFER_SIZE + 1, (char *)fltFmtStr, tmp); 913d96309c9Sopenharmony_ci } 914d96309c9Sopenharmony_ci } 915d96309c9Sopenharmony_ci 916d96309c9Sopenharmony_ci if (fltFmtHeap != NULL) { 917d96309c9Sopenharmony_ci /* if buffer is alloced on heap, free it */ 918d96309c9Sopenharmony_ci SECUREC_FREE(fltFmtHeap); 919d96309c9Sopenharmony_ci fltFmtHeap = NULL; 920d96309c9Sopenharmony_ci } 921d96309c9Sopenharmony_ci if (textLen < 0) { 922d96309c9Sopenharmony_ci /* bufferSize is large enough,just validation the return value */ 923d96309c9Sopenharmony_ci noOutput = 1; 924d96309c9Sopenharmony_ci break; 925d96309c9Sopenharmony_ci } 926d96309c9Sopenharmony_ci 927d96309c9Sopenharmony_ci formatAttr.fldWidth = 928d96309c9Sopenharmony_ci textLen; /* no padding ,this variable to calculate amount of padding */ 929d96309c9Sopenharmony_ci prefixLen = 0; /* no padding ,this variable to calculate amount of padding */ 930d96309c9Sopenharmony_ci formatAttr.flags = 0; /* clear all internal formatAttr.flags */ 931d96309c9Sopenharmony_ci break; 932d96309c9Sopenharmony_ci } 933d96309c9Sopenharmony_ci } 934d96309c9Sopenharmony_ci 935d96309c9Sopenharmony_ci case SECUREC_CHAR('p'): 936d96309c9Sopenharmony_ci /* print a pointer */ 937d96309c9Sopenharmony_ci#if defined(SECUREC_COMPATIBLE_WIN_FORMAT) 938d96309c9Sopenharmony_ci formatAttr.flags &= ~SECUREC_FLAG_LEADZERO; 939d96309c9Sopenharmony_ci#else 940d96309c9Sopenharmony_ci formatAttr.flags |= SECUREC_FLAG_POINTER; 941d96309c9Sopenharmony_ci#endif 942d96309c9Sopenharmony_ci 943d96309c9Sopenharmony_ci#ifdef SECUREC_ON_64BITS 944d96309c9Sopenharmony_ci formatAttr.flags |= SECUREC_FLAG_I64; /* converting an int64 */ 945d96309c9Sopenharmony_ci#else 946d96309c9Sopenharmony_ci formatAttr.flags |= SECUREC_FLAG_LONG; /* converting a long */ 947d96309c9Sopenharmony_ci#endif 948d96309c9Sopenharmony_ci 949d96309c9Sopenharmony_ci#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) || defined(SECUREC_VXWORKS_PLATFORM)) && (!defined(SECUREC_ON_UNIX)) 950d96309c9Sopenharmony_ci 951d96309c9Sopenharmony_ci#if defined(SECUREC_VXWORKS_PLATFORM) 952d96309c9Sopenharmony_ci formatAttr.precision = 1; 953d96309c9Sopenharmony_ci#else 954d96309c9Sopenharmony_ci formatAttr.precision = 0; 955d96309c9Sopenharmony_ci#endif 956d96309c9Sopenharmony_ci formatAttr.flags |= SECUREC_FLAG_ALTERNATE; /* "0x" is not default prefix in UNIX */ 957d96309c9Sopenharmony_ci digits = ITOA_LOWER_DIGITS; 958d96309c9Sopenharmony_ci goto OUTPUT_HEX; 959d96309c9Sopenharmony_ci#else 960d96309c9Sopenharmony_ci /* not linux vxwoks */ 961d96309c9Sopenharmony_ci#if defined(_AIX) || defined(SECUREC_ON_SOLARIS) 962d96309c9Sopenharmony_ci formatAttr.precision = 1; 963d96309c9Sopenharmony_ci#else 964d96309c9Sopenharmony_ci formatAttr.precision = 2 * sizeof(void *); /* 2 x byte number is the length of hex */ 965d96309c9Sopenharmony_ci#endif 966d96309c9Sopenharmony_ci 967d96309c9Sopenharmony_ci#endif 968d96309c9Sopenharmony_ci 969d96309c9Sopenharmony_ci#if defined(SECUREC_ON_UNIX) 970d96309c9Sopenharmony_ci digits = ITOA_LOWER_DIGITS; 971d96309c9Sopenharmony_ci goto OUTPUT_HEX; 972d96309c9Sopenharmony_ci#endif 973d96309c9Sopenharmony_ci 974d96309c9Sopenharmony_ci /* fall-through */ 975d96309c9Sopenharmony_ci /* FALLTHRU */ 976d96309c9Sopenharmony_ci case SECUREC_CHAR('X'): 977d96309c9Sopenharmony_ci /* unsigned upper hex output */ 978d96309c9Sopenharmony_ci digits = ITOA_UPPER_DIGITS; 979d96309c9Sopenharmony_ci goto OUTPUT_HEX; 980d96309c9Sopenharmony_ci 981d96309c9Sopenharmony_ci case SECUREC_CHAR('x'): 982d96309c9Sopenharmony_ci /* unsigned lower hex output */ 983d96309c9Sopenharmony_ci digits = ITOA_LOWER_DIGITS; 984d96309c9Sopenharmony_ci 985d96309c9Sopenharmony_ci OUTPUT_HEX: 986d96309c9Sopenharmony_ci radix = 16; /* The radix number of 'x' or 'X' is 16 */ 987d96309c9Sopenharmony_ci if (formatAttr.flags & SECUREC_FLAG_ALTERNATE) { 988d96309c9Sopenharmony_ci /* alternate form means '0x' prefix */ 989d96309c9Sopenharmony_ci prefix[0] = SECUREC_CHAR('0'); 990d96309c9Sopenharmony_ci prefix[1] = (SecChar)(digits[16]); /* The serial number of 'x' or 'X' is 16 */ 991d96309c9Sopenharmony_ci 992d96309c9Sopenharmony_ci#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT) || defined(SECUREC_VXWORKS_PLATFORM)) 993d96309c9Sopenharmony_ci if (ch == 'p') { 994d96309c9Sopenharmony_ci prefix[1] = SECUREC_CHAR('x'); 995d96309c9Sopenharmony_ci } 996d96309c9Sopenharmony_ci#endif 997d96309c9Sopenharmony_ci#if defined(_AIX) || defined(SECUREC_ON_SOLARIS) 998d96309c9Sopenharmony_ci if (ch == 'p') { 999d96309c9Sopenharmony_ci prefixLen = 0; 1000d96309c9Sopenharmony_ci } else { 1001d96309c9Sopenharmony_ci prefixLen = 2; /* securec prefix len is 2 */ 1002d96309c9Sopenharmony_ci } 1003d96309c9Sopenharmony_ci#else 1004d96309c9Sopenharmony_ci prefixLen = 2; /* securec prefix len is 2 */ 1005d96309c9Sopenharmony_ci#endif 1006d96309c9Sopenharmony_ci } 1007d96309c9Sopenharmony_ci goto OUTPUT_INT; 1008d96309c9Sopenharmony_ci case SECUREC_CHAR('i'): /* fall-through */ /* FALLTHRU */ 1009d96309c9Sopenharmony_ci case SECUREC_CHAR('d'): /* fall-through */ /* FALLTHRU */ 1010d96309c9Sopenharmony_ci /* signed decimal output */ 1011d96309c9Sopenharmony_ci formatAttr.flags |= SECUREC_FLAG_SIGNED; 1012d96309c9Sopenharmony_ci /* fall-through */ 1013d96309c9Sopenharmony_ci /* FALLTHRU */ 1014d96309c9Sopenharmony_ci case SECUREC_CHAR('u'): 1015d96309c9Sopenharmony_ci radix = 10; /* The radix number of 'u' is 10 */ 1016d96309c9Sopenharmony_ci goto OUTPUT_INT; 1017d96309c9Sopenharmony_ci case SECUREC_CHAR('o'): 1018d96309c9Sopenharmony_ci /* unsigned octal output */ 1019d96309c9Sopenharmony_ci radix = 8; /* The radix number of 'o' is 8 */ 1020d96309c9Sopenharmony_ci if (formatAttr.flags & SECUREC_FLAG_ALTERNATE) { 1021d96309c9Sopenharmony_ci /* alternate form means force a leading 0 */ 1022d96309c9Sopenharmony_ci formatAttr.flags |= SECUREC_FLAG_FORCE_OCTAL; 1023d96309c9Sopenharmony_ci } 1024d96309c9Sopenharmony_ci OUTPUT_INT : { 1025d96309c9Sopenharmony_ci SecUnsignedInt64 number = 0; /* number to convert */ 1026d96309c9Sopenharmony_ci SecInt64 l; /* temp long value */ 1027d96309c9Sopenharmony_ci unsigned char tch; 1028d96309c9Sopenharmony_ci#if defined(SECUREC_VXWORKS_VERSION_5_4) && !defined(SECUREC_ON_64BITS) 1029d96309c9Sopenharmony_ci SecUnsignedInt32 digit = 0; /* ascii value of digit */ 1030d96309c9Sopenharmony_ci SecUnsignedInt32 quotientHigh = 0; 1031d96309c9Sopenharmony_ci SecUnsignedInt32 quotientLow = 0; 1032d96309c9Sopenharmony_ci#endif 1033d96309c9Sopenharmony_ci 1034d96309c9Sopenharmony_ci /* read argument into variable l */ 1035d96309c9Sopenharmony_ci if (formatAttr.flags & SECUREC_FLAG_I64) { 1036d96309c9Sopenharmony_ci l = (SecInt64)va_arg(arglist, SecInt64); 1037d96309c9Sopenharmony_ci } else if (formatAttr.flags & SECUREC_FLAG_LONGLONG) { 1038d96309c9Sopenharmony_ci l = (SecInt64)va_arg(arglist, SecInt64); 1039d96309c9Sopenharmony_ci } else 1040d96309c9Sopenharmony_ci#ifdef SECUREC_ON_64BITS 1041d96309c9Sopenharmony_ci if (formatAttr.flags & SECUREC_FLAG_LONG) { 1042d96309c9Sopenharmony_ci l = (long)va_arg(arglist, long); 1043d96309c9Sopenharmony_ci } else 1044d96309c9Sopenharmony_ci#endif /* SECUREC_ON_64BITS */ 1045d96309c9Sopenharmony_ci if (formatAttr.flags & SECUREC_FLAG_CHAR) { 1046d96309c9Sopenharmony_ci if (formatAttr.flags & SECUREC_FLAG_SIGNED) { 1047d96309c9Sopenharmony_ci l = (char)va_arg(arglist, int); /* sign extend */ 1048d96309c9Sopenharmony_ci if (l >= 128) { /* 128 on some platform, char is always unsigned */ 1049d96309c9Sopenharmony_ci SecUnsignedInt64 tmpL = (SecUnsignedInt64)l; 1050d96309c9Sopenharmony_ci formatAttr.flags |= SECUREC_FLAG_NEGATIVE; 1051d96309c9Sopenharmony_ci tch = (unsigned char)(~(tmpL)); 1052d96309c9Sopenharmony_ci l = tch + 1; 1053d96309c9Sopenharmony_ci } 1054d96309c9Sopenharmony_ci } else { 1055d96309c9Sopenharmony_ci l = (unsigned char)va_arg(arglist, int); /* zero-extend */ 1056d96309c9Sopenharmony_ci } 1057d96309c9Sopenharmony_ci } else if (formatAttr.flags & SECUREC_FLAG_SHORT) { 1058d96309c9Sopenharmony_ci if (formatAttr.flags & SECUREC_FLAG_SIGNED) { 1059d96309c9Sopenharmony_ci l = (short)va_arg(arglist, int); /* sign extend */ 1060d96309c9Sopenharmony_ci } else { 1061d96309c9Sopenharmony_ci l = (unsigned short)va_arg(arglist, int); /* zero-extend */ 1062d96309c9Sopenharmony_ci } 1063d96309c9Sopenharmony_ci } 1064d96309c9Sopenharmony_ci#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT 1065d96309c9Sopenharmony_ci else if (formatAttr.flags & SECUREC_FLAG_PTRDIFF) { 1066d96309c9Sopenharmony_ci l = (ptrdiff_t)va_arg(arglist, ptrdiff_t); /* sign extend */ 1067d96309c9Sopenharmony_ci } else if (formatAttr.flags & SECUREC_FLAG_SIZE) { 1068d96309c9Sopenharmony_ci if (formatAttr.flags & SECUREC_FLAG_SIGNED) { 1069d96309c9Sopenharmony_ci /* No suitable macros were found to handle the branch */ 1070d96309c9Sopenharmony_ci if (SecIsSameSize(sizeof(size_t), sizeof(long))) { 1071d96309c9Sopenharmony_ci l = va_arg(arglist, long); /* sign extend */ 1072d96309c9Sopenharmony_ci } else if (SecIsSameSize(sizeof(size_t), sizeof(long long))) { 1073d96309c9Sopenharmony_ci l = va_arg(arglist, long long); /* sign extend */ 1074d96309c9Sopenharmony_ci } else { 1075d96309c9Sopenharmony_ci l = va_arg(arglist, int); /* sign extend */ 1076d96309c9Sopenharmony_ci } 1077d96309c9Sopenharmony_ci } else { 1078d96309c9Sopenharmony_ci l = (SecInt64)(size_t)va_arg(arglist, size_t); /* sign extend */ 1079d96309c9Sopenharmony_ci } 1080d96309c9Sopenharmony_ci } else if (formatAttr.flags & SECUREC_FLAG_INTMAX) { 1081d96309c9Sopenharmony_ci if (formatAttr.flags & SECUREC_FLAG_SIGNED) { 1082d96309c9Sopenharmony_ci l = va_arg(arglist, SecInt64); /* sign extend */ 1083d96309c9Sopenharmony_ci } else { 1084d96309c9Sopenharmony_ci l = (SecInt64)(SecUnsignedInt64)va_arg(arglist, SecUnsignedInt64); /* sign extend */ 1085d96309c9Sopenharmony_ci } 1086d96309c9Sopenharmony_ci } 1087d96309c9Sopenharmony_ci#endif 1088d96309c9Sopenharmony_ci else { 1089d96309c9Sopenharmony_ci if (formatAttr.flags & SECUREC_FLAG_SIGNED) { 1090d96309c9Sopenharmony_ci l = va_arg(arglist, int); /* sign extend */ 1091d96309c9Sopenharmony_ci } else { 1092d96309c9Sopenharmony_ci l = (unsigned int)va_arg(arglist, int); /* zero-extend */ 1093d96309c9Sopenharmony_ci } 1094d96309c9Sopenharmony_ci } 1095d96309c9Sopenharmony_ci 1096d96309c9Sopenharmony_ci /* check for negative; copy into number */ 1097d96309c9Sopenharmony_ci if ((formatAttr.flags & SECUREC_FLAG_SIGNED) && l < 0) { 1098d96309c9Sopenharmony_ci number = (SecUnsignedInt64)(-l); 1099d96309c9Sopenharmony_ci formatAttr.flags |= SECUREC_FLAG_NEGATIVE; 1100d96309c9Sopenharmony_ci } else { 1101d96309c9Sopenharmony_ci number = (SecUnsignedInt64)l; 1102d96309c9Sopenharmony_ci } 1103d96309c9Sopenharmony_ci 1104d96309c9Sopenharmony_ci if (((formatAttr.flags & SECUREC_FLAG_I64) == 0) && 1105d96309c9Sopenharmony_ci#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT 1106d96309c9Sopenharmony_ci ((formatAttr.flags & SECUREC_FLAG_INTMAX) == 0) && 1107d96309c9Sopenharmony_ci#endif 1108d96309c9Sopenharmony_ci#ifdef SECUREC_ON_64BITS 1109d96309c9Sopenharmony_ci ((formatAttr.flags & SECUREC_FLAG_PTRDIFF) == 0) && 1110d96309c9Sopenharmony_ci ((formatAttr.flags & SECUREC_FLAG_SIZE) == 0) && 1111d96309c9Sopenharmony_ci#if !defined(SECUREC_COMPATIBLE_WIN_FORMAT) /* on window 64 system sizeof long is 32bit */ 1112d96309c9Sopenharmony_ci ((formatAttr.flags & SECUREC_FLAG_LONG) == 0) && 1113d96309c9Sopenharmony_ci#endif 1114d96309c9Sopenharmony_ci#endif 1115d96309c9Sopenharmony_ci ((formatAttr.flags & SECUREC_FLAG_LONGLONG) == 0)) { 1116d96309c9Sopenharmony_ci 1117d96309c9Sopenharmony_ci number &= 0xffffffff; 1118d96309c9Sopenharmony_ci } 1119d96309c9Sopenharmony_ci 1120d96309c9Sopenharmony_ci /* check precision value for default */ 1121d96309c9Sopenharmony_ci if (formatAttr.precision < 0) { 1122d96309c9Sopenharmony_ci formatAttr.precision = 1; /* default precision */ 1123d96309c9Sopenharmony_ci } else { 1124d96309c9Sopenharmony_ci#if defined(SECUREC_COMPATIBLE_WIN_FORMAT) 1125d96309c9Sopenharmony_ci formatAttr.flags &= ~SECUREC_FLAG_LEADZERO; 1126d96309c9Sopenharmony_ci#else 1127d96309c9Sopenharmony_ci if (!(formatAttr.flags & SECUREC_FLAG_POINTER)) { 1128d96309c9Sopenharmony_ci formatAttr.flags &= ~SECUREC_FLAG_LEADZERO; 1129d96309c9Sopenharmony_ci } 1130d96309c9Sopenharmony_ci#endif 1131d96309c9Sopenharmony_ci if (formatAttr.precision > SECUREC_MAX_PRECISION) { 1132d96309c9Sopenharmony_ci formatAttr.precision = SECUREC_MAX_PRECISION; 1133d96309c9Sopenharmony_ci } 1134d96309c9Sopenharmony_ci } 1135d96309c9Sopenharmony_ci 1136d96309c9Sopenharmony_ci /* Check if data is 0; if so, turn off hex prefix,if 'p',add 0x prefix,else not add prefix */ 1137d96309c9Sopenharmony_ci if (number == 0) { 1138d96309c9Sopenharmony_ci#if !(defined(SECUREC_VXWORKS_PLATFORM) || defined(__hpux)) 1139d96309c9Sopenharmony_ci prefixLen = 0; 1140d96309c9Sopenharmony_ci#else 1141d96309c9Sopenharmony_ci if ((ch == 'p') && (formatAttr.flags & SECUREC_FLAG_ALTERNATE)) 1142d96309c9Sopenharmony_ci prefixLen = 2; 1143d96309c9Sopenharmony_ci else 1144d96309c9Sopenharmony_ci prefixLen = 0; 1145d96309c9Sopenharmony_ci#endif 1146d96309c9Sopenharmony_ci } 1147d96309c9Sopenharmony_ci 1148d96309c9Sopenharmony_ci /* Convert data to ASCII */ 1149d96309c9Sopenharmony_ci formatBuf.str = &buffer.str[SECUREC_BUFFER_SIZE]; 1150d96309c9Sopenharmony_ci 1151d96309c9Sopenharmony_ci if (number > 0) { 1152d96309c9Sopenharmony_ci#ifdef SECUREC_ON_64BITS 1153d96309c9Sopenharmony_ci switch (radix) { 1154d96309c9Sopenharmony_ci /* the compiler will optimize each one */ 1155d96309c9Sopenharmony_ci SECUREC_SPECIAL(number, 10); 1156d96309c9Sopenharmony_ci break; 1157d96309c9Sopenharmony_ci SECUREC_SPECIAL(number, 16); 1158d96309c9Sopenharmony_ci break; 1159d96309c9Sopenharmony_ci SECUREC_SPECIAL(number, 8); 1160d96309c9Sopenharmony_ci break; 1161d96309c9Sopenharmony_ci default: 1162d96309c9Sopenharmony_ci break; 1163d96309c9Sopenharmony_ci } 1164d96309c9Sopenharmony_ci#else /* for 32 bits system */ 1165d96309c9Sopenharmony_ci if (number <= 0xFFFFFFFFUL) { 1166d96309c9Sopenharmony_ci /* in most case, the value to be converted is small value */ 1167d96309c9Sopenharmony_ci SecUnsignedInt32 n32Tmp = (SecUnsignedInt32)number; 1168d96309c9Sopenharmony_ci switch (radix) { 1169d96309c9Sopenharmony_ci SECUREC_SPECIAL(n32Tmp, 16); 1170d96309c9Sopenharmony_ci break; 1171d96309c9Sopenharmony_ci SECUREC_SPECIAL(n32Tmp, 8); 1172d96309c9Sopenharmony_ci break; 1173d96309c9Sopenharmony_ci 1174d96309c9Sopenharmony_ci#ifdef _AIX 1175d96309c9Sopenharmony_ci /* the compiler will optimize div 10 */ 1176d96309c9Sopenharmony_ci SECUREC_SPECIAL(n32Tmp, 10); 1177d96309c9Sopenharmony_ci break; 1178d96309c9Sopenharmony_ci#else 1179d96309c9Sopenharmony_ci case 10: { /* securec radix decimal is 10 */ 1180d96309c9Sopenharmony_ci /* fast div 10 */ 1181d96309c9Sopenharmony_ci SecUnsignedInt32 q; 1182d96309c9Sopenharmony_ci SecUnsignedInt32 r; 1183d96309c9Sopenharmony_ci do { 1184d96309c9Sopenharmony_ci *--formatBuf.str = digits[n32Tmp % 10]; /* securec radix decimal is 10 */ 1185d96309c9Sopenharmony_ci q = (n32Tmp >> 1) + (n32Tmp >> 2); /* Fast div magic 2 */ 1186d96309c9Sopenharmony_ci q = q + (q >> 4); /* Fast div magic 4 */ 1187d96309c9Sopenharmony_ci q = q + (q >> 8); /* Fast div magic 8 */ 1188d96309c9Sopenharmony_ci q = q + (q >> 16); /* Fast div magic 16 */ 1189d96309c9Sopenharmony_ci q = q >> 3; /* Fast div magic 3 */ 1190d96309c9Sopenharmony_ci r = n32Tmp - (((q << 2) + q) << 1); 1191d96309c9Sopenharmony_ci n32Tmp = (r > 9) ? (q + 1) : q; /* Fast div magic 9 */ 1192d96309c9Sopenharmony_ci } while (n32Tmp != 0); 1193d96309c9Sopenharmony_ci } break; 1194d96309c9Sopenharmony_ci#endif 1195d96309c9Sopenharmony_ci default: 1196d96309c9Sopenharmony_ci break; 1197d96309c9Sopenharmony_ci } /* end switch */ 1198d96309c9Sopenharmony_ci } else { 1199d96309c9Sopenharmony_ci /* the value to be converted is greater than 4G */ 1200d96309c9Sopenharmony_ci#if defined(SECUREC_VXWORKS_VERSION_5_4) 1201d96309c9Sopenharmony_ci do { 1202d96309c9Sopenharmony_ci if (0 != SecU64Div32((SecUnsignedInt32)((number >> 16) >> 16), /* 16, High 32 bit mask */ 1203d96309c9Sopenharmony_ci (SecUnsignedInt32)number, (SecUnsignedInt32)radix, "ientHigh, "ientLow, 1204d96309c9Sopenharmony_ci &digit)) { 1205d96309c9Sopenharmony_ci noOutput = 1; 1206d96309c9Sopenharmony_ci break; 1207d96309c9Sopenharmony_ci } 1208d96309c9Sopenharmony_ci *--formatBuf.str = digits[digit]; 1209d96309c9Sopenharmony_ci 1210d96309c9Sopenharmony_ci number = (SecUnsignedInt64)quotientHigh; 1211d96309c9Sopenharmony_ci number = (number << 32) + quotientLow; /* High 32 bit mask */ 1212d96309c9Sopenharmony_ci } while (number != 0); 1213d96309c9Sopenharmony_ci#else 1214d96309c9Sopenharmony_ci switch (radix) { 1215d96309c9Sopenharmony_ci /* the compiler will optimize div 10 */ 1216d96309c9Sopenharmony_ci SECUREC_SPECIAL(number, 10); 1217d96309c9Sopenharmony_ci break; 1218d96309c9Sopenharmony_ci SECUREC_SPECIAL(number, 16); 1219d96309c9Sopenharmony_ci break; 1220d96309c9Sopenharmony_ci SECUREC_SPECIAL(number, 8); 1221d96309c9Sopenharmony_ci break; 1222d96309c9Sopenharmony_ci default: 1223d96309c9Sopenharmony_ci break; 1224d96309c9Sopenharmony_ci } 1225d96309c9Sopenharmony_ci#endif 1226d96309c9Sopenharmony_ci } 1227d96309c9Sopenharmony_ci#endif 1228d96309c9Sopenharmony_ci } /* END if (number > 0) */ 1229d96309c9Sopenharmony_ci /* compute length of number,.if textLen > 0, then formatBuf.str must be in buffer.str */ 1230d96309c9Sopenharmony_ci textLen = (int)((char *)&buffer.str[SECUREC_BUFFER_SIZE] - formatBuf.str); 1231d96309c9Sopenharmony_ci if (formatAttr.precision > textLen) { 1232d96309c9Sopenharmony_ci for (ii = 0; ii < formatAttr.precision - textLen; ++ii) { 1233d96309c9Sopenharmony_ci *--formatBuf.str = '0'; 1234d96309c9Sopenharmony_ci } 1235d96309c9Sopenharmony_ci textLen = formatAttr.precision; 1236d96309c9Sopenharmony_ci } 1237d96309c9Sopenharmony_ci 1238d96309c9Sopenharmony_ci /* Force a leading zero if FORCEOCTAL flag set */ 1239d96309c9Sopenharmony_ci if ((formatAttr.flags & SECUREC_FLAG_FORCE_OCTAL) && 1240d96309c9Sopenharmony_ci (textLen == 0 || formatBuf.str[0] != '0')) { 1241d96309c9Sopenharmony_ci *--formatBuf.str = '0'; 1242d96309c9Sopenharmony_ci ++textLen; /* add a zero */ 1243d96309c9Sopenharmony_ci } 1244d96309c9Sopenharmony_ci } 1245d96309c9Sopenharmony_ci break; 1246d96309c9Sopenharmony_ci default: 1247d96309c9Sopenharmony_ci break; 1248d96309c9Sopenharmony_ci } 1249d96309c9Sopenharmony_ci 1250d96309c9Sopenharmony_ci if (noOutput == 0) { 1251d96309c9Sopenharmony_ci if (formatAttr.flags & SECUREC_FLAG_SIGNED) { 1252d96309c9Sopenharmony_ci if (formatAttr.flags & SECUREC_FLAG_NEGATIVE) { 1253d96309c9Sopenharmony_ci /* prefix is a '-' */ 1254d96309c9Sopenharmony_ci prefix[0] = SECUREC_CHAR('-'); 1255d96309c9Sopenharmony_ci prefixLen = 1; 1256d96309c9Sopenharmony_ci } else if (formatAttr.flags & SECUREC_FLAG_SIGN) { 1257d96309c9Sopenharmony_ci /* prefix is '+' */ 1258d96309c9Sopenharmony_ci prefix[0] = SECUREC_CHAR('+'); 1259d96309c9Sopenharmony_ci prefixLen = 1; 1260d96309c9Sopenharmony_ci } else if (formatAttr.flags & SECUREC_FLAG_SIGN_SPACE) { 1261d96309c9Sopenharmony_ci /* prefix is ' ' */ 1262d96309c9Sopenharmony_ci prefix[0] = SECUREC_CHAR(' '); 1263d96309c9Sopenharmony_ci prefixLen = 1; 1264d96309c9Sopenharmony_ci } 1265d96309c9Sopenharmony_ci } 1266d96309c9Sopenharmony_ci 1267d96309c9Sopenharmony_ci#if defined(SECUREC_COMPATIBLE_LINUX_FORMAT) && (!defined(SECUREC_ON_UNIX)) 1268d96309c9Sopenharmony_ci if ((formatAttr.flags & SECUREC_FLAG_POINTER) && (0 == textLen)) { 1269d96309c9Sopenharmony_ci formatAttr.flags &= ~SECUREC_FLAG_LEADZERO; 1270d96309c9Sopenharmony_ci formatBuf.str = &buffer.str[SECUREC_BUFFER_SIZE - 1]; 1271d96309c9Sopenharmony_ci *formatBuf.str-- = '\0'; 1272d96309c9Sopenharmony_ci *formatBuf.str-- = ')'; 1273d96309c9Sopenharmony_ci *formatBuf.str-- = 'l'; 1274d96309c9Sopenharmony_ci *formatBuf.str-- = 'i'; 1275d96309c9Sopenharmony_ci *formatBuf.str-- = 'n'; 1276d96309c9Sopenharmony_ci *formatBuf.str = '('; 1277d96309c9Sopenharmony_ci textLen = 5; /* Length of (nil) is 5 */ 1278d96309c9Sopenharmony_ci } 1279d96309c9Sopenharmony_ci#endif 1280d96309c9Sopenharmony_ci 1281d96309c9Sopenharmony_ci /* calculate amount of padding */ 1282d96309c9Sopenharmony_ci padding = (formatAttr.fldWidth - textLen) - prefixLen; 1283d96309c9Sopenharmony_ci 1284d96309c9Sopenharmony_ci /* put out the padding, prefix, and text, in the correct order */ 1285d96309c9Sopenharmony_ci 1286d96309c9Sopenharmony_ci if (!(formatAttr.flags & (SECUREC_FLAG_LEFT | SECUREC_FLAG_LEADZERO)) && padding > 0) { 1287d96309c9Sopenharmony_ci /* pad on left with blanks */ 1288d96309c9Sopenharmony_ci if (SECUREC_IS_REST_BUF_ENOUGH(padding)) { 1289d96309c9Sopenharmony_ci /* char * cast to wchar * */ 1290d96309c9Sopenharmony_ci SECUREC_SAFE_PADDING(SECUREC_CHAR(' '), padding, stream, &charsOut); 1291d96309c9Sopenharmony_ci } else { 1292d96309c9Sopenharmony_ci SECUREC_WRITE_MULTI_CHAR(SECUREC_CHAR(' '), padding, stream, &charsOut); 1293d96309c9Sopenharmony_ci } 1294d96309c9Sopenharmony_ci } 1295d96309c9Sopenharmony_ci 1296d96309c9Sopenharmony_ci /* write prefix */ 1297d96309c9Sopenharmony_ci if (prefixLen > 0) { 1298d96309c9Sopenharmony_ci SecChar *pPrefix = prefix; 1299d96309c9Sopenharmony_ci if (SECUREC_IS_REST_BUF_ENOUGH(prefixLen)) { 1300d96309c9Sopenharmony_ci /* max prefix len is 2, use loop copy */ /* char * cast to wchar * */ 1301d96309c9Sopenharmony_ci SECUREC_SAFE_WRITE_PREFIX(pPrefix, prefixLen, stream, &charsOut); 1302d96309c9Sopenharmony_ci } else { 1303d96309c9Sopenharmony_ci SECUREC_WRITE_STRING(prefix, prefixLen, stream, &charsOut); 1304d96309c9Sopenharmony_ci } 1305d96309c9Sopenharmony_ci } 1306d96309c9Sopenharmony_ci 1307d96309c9Sopenharmony_ci if ((formatAttr.flags & SECUREC_FLAG_LEADZERO) && !(formatAttr.flags & SECUREC_FLAG_LEFT) && 1308d96309c9Sopenharmony_ci padding > 0) { 1309d96309c9Sopenharmony_ci /* write leading zeros */ 1310d96309c9Sopenharmony_ci if (SECUREC_IS_REST_BUF_ENOUGH(padding)) { 1311d96309c9Sopenharmony_ci /* char * cast to wchar * */ 1312d96309c9Sopenharmony_ci SECUREC_SAFE_PADDING(SECUREC_CHAR('0'), padding, stream, &charsOut); 1313d96309c9Sopenharmony_ci } else { 1314d96309c9Sopenharmony_ci SECUREC_WRITE_MULTI_CHAR(SECUREC_CHAR('0'), padding, stream, &charsOut); 1315d96309c9Sopenharmony_ci } 1316d96309c9Sopenharmony_ci } 1317d96309c9Sopenharmony_ci 1318d96309c9Sopenharmony_ci /* write text */ 1319d96309c9Sopenharmony_ci#ifndef SECUREC_FOR_WCHAR 1320d96309c9Sopenharmony_ci if (!isDebugMode && isPrivacy) { 1321d96309c9Sopenharmony_ci if (formatAttr.bufferIsWide) { 1322d96309c9Sopenharmony_ci formatBuf.wStr = WPRIVACY_STRING; 1323d96309c9Sopenharmony_ci textLen = sizeof(WPRIVACY_STRING) / sizeof(wchar_t) - 1; 1324d96309c9Sopenharmony_ci } else { 1325d96309c9Sopenharmony_ci formatBuf.str = PRIVACY_STRING; 1326d96309c9Sopenharmony_ci textLen = strlen(PRIVACY_STRING); 1327d96309c9Sopenharmony_ci } 1328d96309c9Sopenharmony_ci } 1329d96309c9Sopenharmony_ci if (formatAttr.bufferIsWide && (textLen > 0)) { 1330d96309c9Sopenharmony_ci wchar_t *p = formatBuf.wStr; 1331d96309c9Sopenharmony_ci int count = textLen; 1332d96309c9Sopenharmony_ci while (count--) { 1333d96309c9Sopenharmony_ci char tmpBuf[SECUREC_MB_LEN + 1]; 1334d96309c9Sopenharmony_ci // SECUREC_MASK_MSVC_CRT_WARNING 1335d96309c9Sopenharmony_ci int retVal = wctomb(tmpBuf, *p++); 1336d96309c9Sopenharmony_ci // SECUREC_END_MASK_MSVC_CRT_WARNING 1337d96309c9Sopenharmony_ci if (retVal <= 0) { 1338d96309c9Sopenharmony_ci charsOut = -1; 1339d96309c9Sopenharmony_ci break; 1340d96309c9Sopenharmony_ci } 1341d96309c9Sopenharmony_ci SECUREC_WRITE_STRING(tmpBuf, retVal, stream, &charsOut); 1342d96309c9Sopenharmony_ci } 1343d96309c9Sopenharmony_ci } else { 1344d96309c9Sopenharmony_ci if (SECUREC_IS_REST_BUF_ENOUGH(textLen)) { 1345d96309c9Sopenharmony_ci SECUREC_SAFE_WRITE_STR(formatBuf.str, textLen, stream, &charsOut); 1346d96309c9Sopenharmony_ci } else { 1347d96309c9Sopenharmony_ci SECUREC_WRITE_STRING(formatBuf.str, textLen, stream, &charsOut); 1348d96309c9Sopenharmony_ci } 1349d96309c9Sopenharmony_ci } 1350d96309c9Sopenharmony_ci#else /* SECUREC_FOR_WCHAR */ 1351d96309c9Sopenharmony_ci if (formatAttr.bufferIsWide == 0 && textLen > 0) { 1352d96309c9Sopenharmony_ci int count = textLen; 1353d96309c9Sopenharmony_ci char *p = formatBuf.str; 1354d96309c9Sopenharmony_ci 1355d96309c9Sopenharmony_ci while (count > 0) { 1356d96309c9Sopenharmony_ci wchar_t wchar = L'\0'; 1357d96309c9Sopenharmony_ci int retVal = mbtowc(&wchar, p, (size_t)MB_CUR_MAX); 1358d96309c9Sopenharmony_ci if (retVal <= 0) { 1359d96309c9Sopenharmony_ci charsOut = -1; 1360d96309c9Sopenharmony_ci break; 1361d96309c9Sopenharmony_ci } 1362d96309c9Sopenharmony_ci SECUREC_WRITE_CHAR(wchar, stream, &charsOut); 1363d96309c9Sopenharmony_ci p += retVal; 1364d96309c9Sopenharmony_ci count -= retVal; 1365d96309c9Sopenharmony_ci } 1366d96309c9Sopenharmony_ci } else { 1367d96309c9Sopenharmony_ci if (SECUREC_IS_REST_BUF_ENOUGH(textLen)) { 1368d96309c9Sopenharmony_ci SECUREC_SAFE_WRITE_STR(formatBuf.wStr, textLen, stream, 1369d96309c9Sopenharmony_ci &charsOut); /* char * cast to wchar * */ 1370d96309c9Sopenharmony_ci } else { 1371d96309c9Sopenharmony_ci SECUREC_WRITE_STRING(formatBuf.wStr, textLen, stream, &charsOut); 1372d96309c9Sopenharmony_ci } 1373d96309c9Sopenharmony_ci } 1374d96309c9Sopenharmony_ci#endif /* SECUREC_FOR_WCHAR */ 1375d96309c9Sopenharmony_ci 1376d96309c9Sopenharmony_ci if (charsOut >= 0 && (formatAttr.flags & SECUREC_FLAG_LEFT) && padding > 0) { 1377d96309c9Sopenharmony_ci /* pad on right with blanks */ 1378d96309c9Sopenharmony_ci if (SECUREC_IS_REST_BUF_ENOUGH(padding)) { 1379d96309c9Sopenharmony_ci /* char * cast to wchar * */ 1380d96309c9Sopenharmony_ci SECUREC_SAFE_PADDING(SECUREC_CHAR(' '), padding, stream, &charsOut); 1381d96309c9Sopenharmony_ci } else { 1382d96309c9Sopenharmony_ci SECUREC_WRITE_MULTI_CHAR(SECUREC_CHAR(' '), padding, stream, &charsOut); 1383d96309c9Sopenharmony_ci } 1384d96309c9Sopenharmony_ci } 1385d96309c9Sopenharmony_ci 1386d96309c9Sopenharmony_ci /* we're done! */ 1387d96309c9Sopenharmony_ci } 1388d96309c9Sopenharmony_ci if (floatBuf != NULL) { 1389d96309c9Sopenharmony_ci SECUREC_FREE(floatBuf); 1390d96309c9Sopenharmony_ci floatBuf = NULL; 1391d96309c9Sopenharmony_ci } 1392d96309c9Sopenharmony_ci break; 1393d96309c9Sopenharmony_ci case STAT_INVALID: 1394d96309c9Sopenharmony_ci return -1; 1395d96309c9Sopenharmony_ci default: 1396d96309c9Sopenharmony_ci return -1; /* input format is wrong, directly return */ 1397d96309c9Sopenharmony_ci } 1398d96309c9Sopenharmony_ci } 1399d96309c9Sopenharmony_ci 1400d96309c9Sopenharmony_ci if (state != STAT_NORMAL && state != STAT_TYPE) { 1401d96309c9Sopenharmony_ci return -1; 1402d96309c9Sopenharmony_ci } 1403d96309c9Sopenharmony_ci 1404d96309c9Sopenharmony_ci return charsOut; /* the number of characters written */ 1405d96309c9Sopenharmony_ci} /* arglist must not be declare as const */ 1406d96309c9Sopenharmony_ci 1407d96309c9Sopenharmony_ci 1408d96309c9Sopenharmony_ciint HiLogSecVsnprintfImpl(char *string, size_t count, bool isDebugMode, const char *format, va_list arglist) 1409d96309c9Sopenharmony_ci{ 1410d96309c9Sopenharmony_ci SecPrintfStream str; 1411d96309c9Sopenharmony_ci int retVal; 1412d96309c9Sopenharmony_ci 1413d96309c9Sopenharmony_ci str.count = (int)count; /* this count include \0 character */ 1414d96309c9Sopenharmony_ci str.cur = string; 1415d96309c9Sopenharmony_ci 1416d96309c9Sopenharmony_ci retVal = HiLogSecOutputS(&str, isDebugMode, format, arglist); 1417d96309c9Sopenharmony_ci if ((retVal >= 0) && (SECUREC_PUTC_ZERO(&str) != EOF)) { 1418d96309c9Sopenharmony_ci return (retVal); 1419d96309c9Sopenharmony_ci } else if (str.count < 0) { 1420d96309c9Sopenharmony_ci /* the buffer was too small; we return truncation */ 1421d96309c9Sopenharmony_ci string[count - 1] = 0; 1422d96309c9Sopenharmony_ci return SECUREC_PRINTF_TRUNCATE; 1423d96309c9Sopenharmony_ci } 1424d96309c9Sopenharmony_ci 1425d96309c9Sopenharmony_ci return -1; 1426d96309c9Sopenharmony_ci} 1427d96309c9Sopenharmony_ci 1428d96309c9Sopenharmony_ci 1429d96309c9Sopenharmony_ciint HiLog_Printf(char *strDest, size_t destMax, size_t count, bool isDebugMode, const char *format, va_list arglist) 1430d96309c9Sopenharmony_ci{ 1431d96309c9Sopenharmony_ci int retVal; 1432d96309c9Sopenharmony_ci 1433d96309c9Sopenharmony_ci if (format == NULL || strDest == NULL || destMax == 0 || destMax > SECUREC_STRING_MAX_LEN || 1434d96309c9Sopenharmony_ci (count > (SECUREC_STRING_MAX_LEN - 1) && count != (size_t)-1)) { 1435d96309c9Sopenharmony_ci if (strDest != NULL && destMax > 0) { 1436d96309c9Sopenharmony_ci strDest[0] = '\0'; 1437d96309c9Sopenharmony_ci } 1438d96309c9Sopenharmony_ci SECUREC_ERROR_INVALID_PARAMTER("hilog_printf"); 1439d96309c9Sopenharmony_ci return -1; 1440d96309c9Sopenharmony_ci } 1441d96309c9Sopenharmony_ci 1442d96309c9Sopenharmony_ci if (destMax > count) { 1443d96309c9Sopenharmony_ci retVal = HiLogSecVsnprintfImpl(strDest, count + 1, isDebugMode, format, arglist); 1444d96309c9Sopenharmony_ci if (retVal == SECUREC_PRINTF_TRUNCATE) { /* lsd add to keep dest buffer not destroyed 2014.2.18 */ 1445d96309c9Sopenharmony_ci /* the string has been truncated, return -1 */ 1446d96309c9Sopenharmony_ci return -1; /* to skip error handler, return strlen(strDest) or -1 */ 1447d96309c9Sopenharmony_ci } 1448d96309c9Sopenharmony_ci } else { /* destMax <= count */ 1449d96309c9Sopenharmony_ci retVal = HiLogSecVsnprintfImpl(strDest, destMax, isDebugMode, format, arglist); 1450d96309c9Sopenharmony_ci#ifdef SECUREC_COMPATIBLE_WIN_FORMAT 1451d96309c9Sopenharmony_ci if (retVal == SECUREC_PRINTF_TRUNCATE && count == (size_t)-1) { 1452d96309c9Sopenharmony_ci return -1; 1453d96309c9Sopenharmony_ci } 1454d96309c9Sopenharmony_ci#endif 1455d96309c9Sopenharmony_ci } 1456d96309c9Sopenharmony_ci 1457d96309c9Sopenharmony_ci if (retVal < 0) { 1458d96309c9Sopenharmony_ci strDest[0] = '\0'; /* empty the dest strDest */ 1459d96309c9Sopenharmony_ci 1460d96309c9Sopenharmony_ci if (retVal == SECUREC_PRINTF_TRUNCATE) { 1461d96309c9Sopenharmony_ci /* Buffer too small */ 1462d96309c9Sopenharmony_ci SECUREC_ERROR_INVALID_RANGE("hilog_printf"); 1463d96309c9Sopenharmony_ci } 1464d96309c9Sopenharmony_ci 1465d96309c9Sopenharmony_ci SECUREC_ERROR_INVALID_PARAMTER("hilog_printf"); 1466d96309c9Sopenharmony_ci return -1; 1467d96309c9Sopenharmony_ci } 1468d96309c9Sopenharmony_ci 1469d96309c9Sopenharmony_ci return retVal; 1470d96309c9Sopenharmony_ci} 1471d96309c9Sopenharmony_ci 1472d96309c9Sopenharmony_ci#define HILOG_DRIVER "/dev/hilog" 1473d96309c9Sopenharmony_cistatic char g_logLevelInfo[HILOG_LEVEL_MAX] = { 1474d96309c9Sopenharmony_ci 'N', // "NONE" 1475d96309c9Sopenharmony_ci 'N', // "NONE" 1476d96309c9Sopenharmony_ci 'N', // "NONE" 1477d96309c9Sopenharmony_ci 'D', // "DEBUG" 1478d96309c9Sopenharmony_ci 'I', // "INFO" 1479d96309c9Sopenharmony_ci 'W', // "WARN" 1480d96309c9Sopenharmony_ci 'E', // "ERROR" 1481d96309c9Sopenharmony_ci 'F' // "FATAL" 1482d96309c9Sopenharmony_ci}; 1483d96309c9Sopenharmony_ci 1484d96309c9Sopenharmony_ci#ifdef LOSCFG_BASE_CORE_HILOG 1485d96309c9Sopenharmony_ciextern int HiLogWriteInternal(const char *buffer, size_t bufLen); 1486d96309c9Sopenharmony_ci#else 1487d96309c9Sopenharmony_cistatic int g_hilogFd = -1; 1488d96309c9Sopenharmony_ci#endif 1489d96309c9Sopenharmony_ci 1490d96309c9Sopenharmony_ciint HiLogPrintArgs(LogType bufID, LogLevel prio, unsigned int domain, const char *tag, const char *fmt, va_list ap) 1491d96309c9Sopenharmony_ci{ 1492d96309c9Sopenharmony_ci int ret; 1493d96309c9Sopenharmony_ci char buf[LOG_BUF_SIZE] = {0}; 1494d96309c9Sopenharmony_ci bool isDebugMode = 1; 1495d96309c9Sopenharmony_ci unsigned int bufLen; 1496d96309c9Sopenharmony_ci (void)bufID; 1497d96309c9Sopenharmony_ci 1498d96309c9Sopenharmony_ci#ifdef OHOS_RELEASE 1499d96309c9Sopenharmony_ci isDebugMode = 0; 1500d96309c9Sopenharmony_ci#endif 1501d96309c9Sopenharmony_ci#ifdef DISABLE_HILOG_PRIVACY 1502d96309c9Sopenharmony_ci isDebugMode = 1; 1503d96309c9Sopenharmony_ci#endif 1504d96309c9Sopenharmony_ci if (snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, "%c %05X/%s: ", g_logLevelInfo[prio], (domain & DOMAIN_FILTER), 1505d96309c9Sopenharmony_ci tag) == -1) { 1506d96309c9Sopenharmony_ci return 0; 1507d96309c9Sopenharmony_ci } 1508d96309c9Sopenharmony_ci 1509d96309c9Sopenharmony_ci bufLen = strlen(buf); 1510d96309c9Sopenharmony_ci if (bufLen >= MAX_DOMAIN_TAG_SIZE) { 1511d96309c9Sopenharmony_ci return 0; 1512d96309c9Sopenharmony_ci } 1513d96309c9Sopenharmony_ci 1514d96309c9Sopenharmony_ci#ifndef LOSCFG_BASE_CORE_HILOG 1515d96309c9Sopenharmony_ci /* print traceid */ 1516d96309c9Sopenharmony_ci if (g_registerFunc != NULL) { 1517d96309c9Sopenharmony_ci char *logBuf = buf; 1518d96309c9Sopenharmony_ci int traceBufLen = 0; 1519d96309c9Sopenharmony_ci uint64_t chainId = 0; 1520d96309c9Sopenharmony_ci uint32_t flag = 0; 1521d96309c9Sopenharmony_ci uint64_t spanId = 0; 1522d96309c9Sopenharmony_ci uint64_t parentSpanId = 0; 1523d96309c9Sopenharmony_ci ret = -1; /* default value -1: invalid trace id */ 1524d96309c9Sopenharmony_ci atomic_fetch_add_explicit(&g_hiLogGetIdCallCount, 1, memory_order_relaxed); 1525d96309c9Sopenharmony_ci RegisterFunc func = g_registerFunc; 1526d96309c9Sopenharmony_ci if (g_registerFunc != NULL) { 1527d96309c9Sopenharmony_ci ret = func(&chainId, &flag, &spanId, &parentSpanId); 1528d96309c9Sopenharmony_ci } 1529d96309c9Sopenharmony_ci atomic_fetch_sub_explicit(&g_hiLogGetIdCallCount, 1, memory_order_relaxed); 1530d96309c9Sopenharmony_ci if (ret == 0) { /* 0: trace id with span id */ 1531d96309c9Sopenharmony_ci traceBufLen = snprintf_s(logBuf, LOG_BUF_SIZE, LOG_BUF_SIZE - 1, "[%llx, %llx, %llx] ", 1532d96309c9Sopenharmony_ci (unsigned long long)chainId, (unsigned long long)spanId, (unsigned long long)parentSpanId); 1533d96309c9Sopenharmony_ci } else if (ret != -1) { /* trace id without span id, -1: invalid trace id */ 1534d96309c9Sopenharmony_ci traceBufLen = snprintf_s(logBuf, LOG_BUF_SIZE, LOG_BUF_SIZE - 1, "[%llx] ", 1535d96309c9Sopenharmony_ci (unsigned long long)chainId); 1536d96309c9Sopenharmony_ci } 1537d96309c9Sopenharmony_ci if (traceBufLen > 0) { 1538d96309c9Sopenharmony_ci logBuf += traceBufLen; 1539d96309c9Sopenharmony_ci } else { 1540d96309c9Sopenharmony_ci traceBufLen = 0; 1541d96309c9Sopenharmony_ci } 1542d96309c9Sopenharmony_ci } 1543d96309c9Sopenharmony_ci#endif 1544d96309c9Sopenharmony_ci 1545d96309c9Sopenharmony_ci HiLog_Printf(buf + bufLen, LOG_BUF_SIZE - bufLen, LOG_BUF_SIZE - bufLen - 1, isDebugMode, fmt, ap); 1546d96309c9Sopenharmony_ci 1547d96309c9Sopenharmony_ci#ifdef LOSCFG_BASE_CORE_HILOG 1548d96309c9Sopenharmony_ci ret = HiLogWriteInternal(buf, strlen(buf) + 1); 1549d96309c9Sopenharmony_ci#else 1550d96309c9Sopenharmony_ci if (g_hilogFd == -1) { 1551d96309c9Sopenharmony_ci g_hilogFd = open(HILOG_DRIVER, O_WRONLY | O_CLOEXEC); 1552d96309c9Sopenharmony_ci } 1553d96309c9Sopenharmony_ci if (g_hilogFd == -1) { 1554d96309c9Sopenharmony_ci return 0; 1555d96309c9Sopenharmony_ci } 1556d96309c9Sopenharmony_ci ret = write(g_hilogFd, buf, strlen(buf) + 1); 1557d96309c9Sopenharmony_ci#endif 1558d96309c9Sopenharmony_ci return ret; 1559d96309c9Sopenharmony_ci} 1560d96309c9Sopenharmony_ci 1561d96309c9Sopenharmony_ciint HiLogPrint(LogType bufID, LogLevel prio, unsigned int domain, const char *tag, const char *fmt, ...) 1562d96309c9Sopenharmony_ci{ 1563d96309c9Sopenharmony_ci int ret; 1564d96309c9Sopenharmony_ci va_list ap; 1565d96309c9Sopenharmony_ci va_start(ap, fmt); 1566d96309c9Sopenharmony_ci ret = HiLogPrintArgs(bufID, prio, domain, tag, fmt, ap); 1567d96309c9Sopenharmony_ci va_end(ap); 1568d96309c9Sopenharmony_ci return ret; 1569d96309c9Sopenharmony_ci} 1570d96309c9Sopenharmony_ci 1571d96309c9Sopenharmony_ciint FlushHilog(void) 1572d96309c9Sopenharmony_ci{ 1573d96309c9Sopenharmony_ci int ret; 1574d96309c9Sopenharmony_ci#define FLUSH_LOG_WRITE_LEN 3 1575d96309c9Sopenharmony_ci char buf[FLUSH_LOG_WRITE_LEN] = {0x07, 0x07, '\0'}; 1576d96309c9Sopenharmony_ci#ifdef LOSCFG_BASE_CORE_HILOG 1577d96309c9Sopenharmony_ci ret = HiLogWriteInternal(buf, strlen(buf) + 1); 1578d96309c9Sopenharmony_ci#else 1579d96309c9Sopenharmony_ci if (g_hilogFd == -1) { 1580d96309c9Sopenharmony_ci g_hilogFd = open(HILOG_DRIVER, O_WRONLY); 1581d96309c9Sopenharmony_ci } 1582d96309c9Sopenharmony_ci if (g_hilogFd == -1) { 1583d96309c9Sopenharmony_ci return 0; 1584d96309c9Sopenharmony_ci } 1585d96309c9Sopenharmony_ci ret = write(g_hilogFd, buf, strlen(buf) + 1); 1586d96309c9Sopenharmony_ci#endif 1587d96309c9Sopenharmony_ci return ret; 1588d96309c9Sopenharmony_ci} 1589