You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

secureprintoutput_w.c 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* if some platforms don't have wchar.h, dont't include it */
  17. #if !(defined(SECUREC_VXWORKS_PLATFORM))
  18. /* This header file is placed below secinput.h, which will cause tool alarm,
  19. * but if there is no macro above, it will cause compiling alarm
  20. */
  21. #if defined(_MSC_VER) && (_MSC_VER >= 1400)
  22. #ifndef _CRTIMP_ALTERNATIVE
  23. #define _CRTIMP_ALTERNATIVE /* comment microsoft *_s function */
  24. #endif
  25. #ifndef __STDC_WANT_SECURE_LIB__
  26. #define __STDC_WANT_SECURE_LIB__ 0
  27. #endif
  28. #endif
  29. #include <wchar.h>
  30. #endif
  31. #define SECUREC_ENABLE_WCHAR_FUNC 0
  32. #define SECUREC_INLINE_DO_MEMCPY 1
  33. #define SECUREC_FORMAT_OUTPUT_INPUT 1
  34. #ifndef SECUREC_FOR_WCHAR
  35. #define SECUREC_FOR_WCHAR
  36. #endif
  37. #include "secureprintoutput.h"
  38. #ifndef WEOF
  39. #define WEOF ((wchar_t)(-1))
  40. #endif
  41. #define SECUREC_CHAR(x) L ## x
  42. #define SECUREC_WRITE_MULTI_CHAR SecWriteMultiCharW
  43. #define SECUREC_WRITE_STRING SecWriteStringW
  44. static void SecWriteCharW(wchar_t ch, SecPrintfStream *f, int *pnumwritten);
  45. static void SecWriteMultiCharW(wchar_t ch, int num, SecPrintfStream *f, int *pnumwritten);
  46. static void SecWriteStringW(const wchar_t *string, int len, SecPrintfStream *f, int *pnumwritten);
  47. static int SecPutWcharStrEndingZero(SecPrintfStream *str, int zeroCount);
  48. #include "output.inl"
  49. /*
  50. * Wide character formatted output implementation
  51. */
  52. int SecVswprintfImpl(wchar_t *string, size_t sizeInWchar, const wchar_t *format, va_list argList)
  53. {
  54. SecPrintfStream str;
  55. int retVal; /* If initialization causes e838 */
  56. str.cur = (char *)string;
  57. /* this count include \0 character, Must be greater than zero */
  58. str.count = (int)(sizeInWchar * sizeof(wchar_t));
  59. retVal = SecOutputSW(&str, format, argList);
  60. if ((retVal >= 0) && SecPutWcharStrEndingZero(&str, (int)sizeof(wchar_t))) {
  61. return (retVal);
  62. } else if (str.count < 0) {
  63. /* the buffer was too small; we return truncation */
  64. string[sizeInWchar - 1] = L'\0';
  65. return SECUREC_PRINTF_TRUNCATE;
  66. }
  67. string[0] = L'\0';
  68. return -1;
  69. }
  70. /*
  71. * Output one zero character zero into the SecPrintfStream structure
  72. */
  73. static int SecPutZeroChar(SecPrintfStream *str)
  74. {
  75. if (str->count > 0) {
  76. *(str->cur) = (char)('\0');
  77. str->count = str->count - 1;
  78. str->cur = str->cur + 1;
  79. return 0;
  80. }
  81. return -1;
  82. }
  83. /*
  84. * Output a wide character zero end into the SecPrintfStream structure
  85. */
  86. static int SecPutWcharStrEndingZero(SecPrintfStream *str, int zeroCount)
  87. {
  88. int succeed = 0;
  89. int i = 0;
  90. while (i < zeroCount && (SecPutZeroChar(str) == 0)) {
  91. ++i;
  92. }
  93. if (i == zeroCount) {
  94. succeed = 1;
  95. }
  96. return succeed;
  97. }
  98. /*
  99. * Output a wide character into the SecPrintfStream structure
  100. */
  101. static wchar_t SecPutCharW(wchar_t ch, SecPrintfStream *f)
  102. {
  103. wchar_t wcRet = 0;
  104. if (((f)->count -= (int)sizeof(wchar_t)) >= 0) {
  105. *(wchar_t *)(void *)(f->cur) = ch;
  106. f->cur += sizeof(wchar_t);
  107. wcRet = ch;
  108. } else {
  109. wcRet = (wchar_t)WEOF;
  110. }
  111. return wcRet;
  112. }
  113. /*
  114. * Output a wide character into the SecPrintfStream structure, returns the number of characters written
  115. */
  116. static void SecWriteCharW(wchar_t ch, SecPrintfStream *f, int *pnumwritten)
  117. {
  118. if (SecPutCharW(ch, f) == (wchar_t)WEOF) {
  119. *pnumwritten = -1;
  120. } else {
  121. *pnumwritten = *pnumwritten + 1;
  122. }
  123. }
  124. /*
  125. * Output multiple wide character into the SecPrintfStream structure, returns the number of characters written
  126. */
  127. static void SecWriteMultiCharW(wchar_t ch, int num, SecPrintfStream *f, int *pnumwritten)
  128. {
  129. int count = num;
  130. while (count-- > 0) {
  131. SecWriteCharW(ch, f, pnumwritten);
  132. if (*pnumwritten == -1) {
  133. break;
  134. }
  135. }
  136. }
  137. /*
  138. * Output a wide string into the SecPrintfStream structure, returns the number of characters written
  139. */
  140. static void SecWriteStringW(const wchar_t *string, int len, SecPrintfStream *f, int *pnumwritten)
  141. {
  142. const wchar_t *str = string;
  143. int count = len;
  144. while (count-- > 0) {
  145. SecWriteCharW(*str++, f, pnumwritten);
  146. if (*pnumwritten == -1) {
  147. break;
  148. }
  149. }
  150. }