vswprint.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // Copyright (c) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  4. //
  5. /***
  6. *vswprint.c - print formatted data into a string from var arg list
  7. *
  8. *Purpose:
  9. * defines vswprintf(), _vswprintf_c and _vsnwprintf() - print formatted output to
  10. * a string, get the data from an argument ptr instead of explicit
  11. * arguments.
  12. *
  13. *******************************************************************************/
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <limits.h>
  17. #include "internal_securecrt.h"
  18. #include "mbusafecrt_internal.h"
  19. typedef int (*WOUTPUTFN)(miniFILE *, const char16_t *, va_list);
  20. static int _vswprintf_helper( WOUTPUTFN outfn, char16_t *string, size_t count, const char16_t *format, va_list ap );
  21. static int _vscwprintf_helper (WOUTPUTFN outfn, const char16_t *format, va_list ap );
  22. /***
  23. *ifndef _COUNT_
  24. *int _vswprintf(string, format, ap) - print formatted data to string from arg ptr
  25. *else
  26. *ifndef _SWPRINTFS_ERROR_RETURN_FIX
  27. *int _vsnwprintf(string, cnt, format, ap) - print formatted data to string from arg ptr
  28. *else
  29. *int _vswprintf_c(string, cnt, format, ...) - print formatted data to string
  30. *endif
  31. *endif
  32. *
  33. *Purpose:
  34. * Prints formatted data, but to a string and gets data from an argument
  35. * pointer.
  36. * Sets up a FILE so file i/o operations can be used, make string look
  37. * like a huge buffer to it, but _flsbuf will refuse to flush it if it
  38. * fills up. Appends '\0' to make it a true string.
  39. *
  40. * Allocate the 'fake' _iob[] entryit statically instead of on
  41. * the stack so that other routines can assume that _iob[] entries are in
  42. * are in DGROUP and, thus, are near.
  43. *
  44. *ifdef _COUNT_
  45. *ifndef _SWPRINTFS_ERROR_RETURN_FIX
  46. * The _vsnwprintf() flavor takes a count argument that is
  47. * the max number of bytes that should be written to the
  48. * user's buffer.
  49. * We don't expose this function directly in the headers.
  50. *else
  51. * The _vswprintf_c() flavor does the same thing as the _snwprintf
  52. * above, but, it also fixes an issue in the return value in the case
  53. * when there isn't enough space to write the null terminator
  54. * We don't fix this issue in _vsnwprintf because of backward
  55. * compatibility. In new code, however, _vsnwprintf is #defined to
  56. * _vswprintf_c so users get the fix.
  57. *
  58. *endif
  59. *
  60. * Multi-thread: (1) Since there is no stream, this routine must never try
  61. * to get the stream lock (i.e., there is no stream lock either). (2)
  62. * Also, since there is only one statically allocated 'fake' iob, we must
  63. * lock/unlock to prevent collisions.
  64. *
  65. *Entry:
  66. * char16_t *string - place to put destination string
  67. *ifdef _COUNT_
  68. * size_t count - max number of bytes to put in buffer
  69. *endif
  70. * char16_t *format - format string, describes format of data
  71. * va_list ap - varargs argument pointer
  72. *
  73. *Exit:
  74. * returns number of wide characters in string
  75. * returns -2 if the string has been truncated (only in _vsnprintf_helper)
  76. * returns -1 in other error cases
  77. *
  78. *Exceptions:
  79. *
  80. *******************************************************************************/
  81. int __cdecl _vswprintf_helper (
  82. WOUTPUTFN woutfn,
  83. char16_t *string,
  84. size_t count,
  85. const char16_t *format,
  86. va_list ap
  87. )
  88. {
  89. miniFILE str;
  90. miniFILE *outfile = &str;
  91. int retval;
  92. _VALIDATE_RETURN( (format != NULL), EINVAL, -1);
  93. _VALIDATE_RETURN( (count == 0) || (string != NULL), EINVAL, -1 );
  94. outfile->_flag = _IOWRT|_IOSTRG;
  95. outfile->_ptr = outfile->_base = (char *) string;
  96. if(count>(INT_MAX/sizeof(char16_t)))
  97. {
  98. /* old-style functions allow any large value to mean unbounded */
  99. outfile->_cnt = INT_MAX;
  100. }
  101. else
  102. {
  103. outfile->_cnt = (int)(count*sizeof(char16_t));
  104. }
  105. retval = woutfn(outfile, format, ap );
  106. if(string==NULL)
  107. {
  108. return retval;
  109. }
  110. if((retval >= 0) && (_putc_nolock('\0',outfile) != EOF) && (_putc_nolock('\0',outfile) != EOF))
  111. return(retval);
  112. string[count - 1] = 0;
  113. if (outfile->_cnt < 0)
  114. {
  115. /* the buffer was too small; we return -2 to indicate truncation */
  116. return -2;
  117. }
  118. return -1;
  119. }
  120. int __cdecl _vswprintf_s (
  121. char16_t *string,
  122. size_t sizeInWords,
  123. const char16_t *format,
  124. va_list ap
  125. )
  126. {
  127. int retvalue = -1;
  128. /* validation section */
  129. _VALIDATE_RETURN(format != NULL, EINVAL, -1);
  130. _VALIDATE_RETURN(string != NULL && sizeInWords > 0, EINVAL, -1);
  131. retvalue = _vswprintf_helper(_woutput_s, string, sizeInWords, format, ap);
  132. if (retvalue < 0)
  133. {
  134. string[0] = 0;
  135. _SECURECRT__FILL_STRING(string, sizeInWords, 1);
  136. }
  137. if (retvalue == -2)
  138. {
  139. _VALIDATE_RETURN(("Buffer too small" && 0), ERANGE, -1);
  140. }
  141. if (retvalue >= 0)
  142. {
  143. _SECURECRT__FILL_STRING(string, sizeInWords, retvalue + 1);
  144. }
  145. return retvalue;
  146. }
  147. int __cdecl vswprintf_s (
  148. char16_t *string,
  149. size_t sizeInWords,
  150. const char16_t *format,
  151. va_list ap
  152. )
  153. {
  154. return _vswprintf_s(string, sizeInWords, format, ap);
  155. }
  156. int __cdecl _vsnwprintf_s (
  157. char16_t *string,
  158. size_t sizeInWords,
  159. size_t count,
  160. const char16_t *format,
  161. va_list ap
  162. )
  163. {
  164. int retvalue = -1;
  165. errno_t save_errno = 0;
  166. /* validation section */
  167. _VALIDATE_RETURN(format != NULL, EINVAL, -1);
  168. if (count == 0 && string == NULL && sizeInWords == 0)
  169. {
  170. /* this case is allowed; nothing to do */
  171. return 0;
  172. }
  173. _VALIDATE_RETURN(string != NULL && sizeInWords > 0, EINVAL, -1);
  174. if (sizeInWords > count)
  175. {
  176. save_errno = errno;
  177. retvalue = _vswprintf_helper(_woutput_s, string, count + 1, format, ap);
  178. if (retvalue == -2)
  179. {
  180. /* the string has been truncated, return -1 */
  181. _SECURECRT__FILL_STRING(string, sizeInWords, count + 1);
  182. if (errno == ERANGE)
  183. {
  184. errno = save_errno;
  185. }
  186. return -1;
  187. }
  188. }
  189. else /* sizeInWords <= count */
  190. {
  191. save_errno = errno;
  192. retvalue = _vswprintf_helper(_woutput_s, string, sizeInWords, format, ap);
  193. string[sizeInWords - 1] = 0;
  194. /* we allow truncation if count == _TRUNCATE */
  195. if (retvalue == -2 && count == _TRUNCATE)
  196. {
  197. if (errno == ERANGE)
  198. {
  199. errno = save_errno;
  200. }
  201. return -1;
  202. }
  203. }
  204. if (retvalue < 0)
  205. {
  206. string[0] = 0;
  207. _SECURECRT__FILL_STRING(string, sizeInWords, 1);
  208. if (retvalue == -2)
  209. {
  210. _VALIDATE_RETURN(("Buffer too small" && 0), ERANGE, -1);
  211. }
  212. return -1;
  213. }
  214. _SECURECRT__FILL_STRING(string, sizeInWords, retvalue + 1);
  215. return (retvalue < 0 ? -1 : retvalue);
  216. }
  217. /***
  218. * _vscwprintf() - counts the number of character needed to print the formatted
  219. * data
  220. *
  221. *Purpose:
  222. * Counts the number of characters in the fotmatted data.
  223. *
  224. *Entry:
  225. * char16_t *format - format string, describes format of data
  226. * va_list ap - varargs argument pointer
  227. *
  228. *Exit:
  229. * returns number of characters needed to print formatted data.
  230. *
  231. *Exceptions:
  232. *
  233. *******************************************************************************/
  234. #ifndef _COUNT_
  235. int __cdecl _vscwprintf_helper (
  236. WOUTPUTFN woutfn,
  237. const char16_t *format,
  238. va_list ap
  239. )
  240. {
  241. miniFILE str;
  242. miniFILE *outfile = &str;
  243. int retval;
  244. _VALIDATE_RETURN( (format != NULL), EINVAL, -1);
  245. outfile->_cnt = INT_MAX; //MAXSTR;
  246. outfile->_flag = _IOWRT|_IOSTRG;
  247. outfile->_ptr = outfile->_base = NULL;
  248. retval = woutfn(outfile, format, ap);
  249. return(retval);
  250. }
  251. int __cdecl _vscwprintf (
  252. const char16_t *format,
  253. va_list ap
  254. )
  255. {
  256. return _vscwprintf_helper(_woutput_s, format, ap);
  257. }
  258. #endif /* _COUNT_ */