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_a.c 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #define SECUREC_INLINE_DO_MEMCPY 1
  17. #define SECUREC_FORMAT_OUTPUT_INPUT 1
  18. #ifdef SECUREC_FOR_WCHAR
  19. #undef SECUREC_FOR_WCHAR
  20. #endif
  21. #include "secureprintoutput.h"
  22. #define SECUREC_CHAR(x) x
  23. #define SECUREC_WRITE_MULTI_CHAR SecWriteMultiChar
  24. #define SECUREC_WRITE_STRING SecWriteString
  25. #ifndef EOF
  26. #define EOF (-1)
  27. #endif
  28. /* put a char to output */
  29. #define SECUREC_PUTC(c, outStream) ((--(outStream)->count >= 0) ? \
  30. (int)((unsigned int)(unsigned char)(*((outStream)->cur++) = (char)(c)) & 0xff) : EOF)
  31. /* to clear e835 */
  32. #define SECUREC_PUTC_ZERO(outStream) ((--(outStream)->count >= 0) ? \
  33. ((*((outStream)->cur++) = (char)('\0'))) : EOF)
  34. static void SecWriteMultiChar(char ch, int num, SecPrintfStream *f, int *pnumwritten);
  35. static void SecWriteString(const char *string, int len, SecPrintfStream *f, int *pnumwritten);
  36. #include "output.inl"
  37. /*
  38. * Wide character formatted output implementation
  39. */
  40. int SecVsnprintfImpl(char *string, size_t count, const char *format, va_list argList)
  41. {
  42. SecPrintfStream str;
  43. int retVal;
  44. str.count = (int)count; /* this count include \0 character, Must be greater than zero */
  45. str.cur = string;
  46. retVal = SecOutputS(&str, format, argList);
  47. if ((retVal >= 0) && (SECUREC_PUTC_ZERO(&str) != EOF)) {
  48. return retVal;
  49. } else if (str.count < 0) {
  50. /* the buffer was too small; we return truncation */
  51. string[count - 1] = '\0';
  52. return SECUREC_PRINTF_TRUNCATE;
  53. }
  54. string[0] = '\0'; /* empty the dest strDest */
  55. return -1;
  56. }
  57. /*
  58. * Sec write Wide character
  59. */
  60. static void SecWriteMultiChar(char ch, int num, SecPrintfStream *f, int *pnumwritten)
  61. {
  62. int count = num;
  63. while (count-- > 0) {
  64. if (SECUREC_PUTC(ch, f) == EOF) {
  65. *pnumwritten = -1;
  66. break;
  67. } else {
  68. *pnumwritten = *pnumwritten + 1;
  69. }
  70. }
  71. }
  72. /*
  73. * Sec write string function
  74. */
  75. static void SecWriteString(const char *string, int len, SecPrintfStream *f, int *pnumwritten)
  76. {
  77. const char *str = string;
  78. int count = len;
  79. while (count-- > 0) {
  80. if (SECUREC_PUTC(*str, f) == EOF) {
  81. *pnumwritten = -1;
  82. break;
  83. } else {
  84. *pnumwritten = *pnumwritten + 1;
  85. ++str;
  86. }
  87. }
  88. }