archive_string_sprintf.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD: head/lib/libarchive/archive_string_sprintf.c 189435 2009-03-06 05:14:55Z kientzle $");
  27. /*
  28. * The use of printf()-family functions can be troublesome
  29. * for space-constrained applications. In addition, correctly
  30. * implementing this function in terms of vsnprintf() requires
  31. * two calls (one to determine the size, another to format the
  32. * result), which in turn requires duplicating the argument list
  33. * using va_copy, which isn't yet universally available. <sigh>
  34. *
  35. * So, I've implemented a bare minimum of printf()-like capability
  36. * here. This is only used to format error messages, so doesn't
  37. * require any floating-point support or field-width handling.
  38. */
  39. #ifdef HAVE_ERRNO_H
  40. #include <errno.h>
  41. #endif
  42. #include <stdio.h>
  43. #include "archive_string.h"
  44. #include "archive_private.h"
  45. /*
  46. * Utility functions to format signed/unsigned integers and append
  47. * them to an archive_string.
  48. */
  49. static void
  50. append_uint(struct archive_string *as, uintmax_t d, unsigned base)
  51. {
  52. static const char digits[] = "0123456789abcdef";
  53. if (d >= base)
  54. append_uint(as, d/base, base);
  55. archive_strappend_char(as, digits[d % base]);
  56. }
  57. static void
  58. append_int(struct archive_string *as, intmax_t d, unsigned base)
  59. {
  60. uintmax_t ud;
  61. if (d < 0) {
  62. archive_strappend_char(as, '-');
  63. ud = (d == INTMAX_MIN) ? (uintmax_t)(INTMAX_MAX) + 1 : (uintmax_t)(-d);
  64. } else
  65. ud = d;
  66. append_uint(as, ud, base);
  67. }
  68. void
  69. archive_string_sprintf(struct archive_string *as, const char *fmt, ...)
  70. {
  71. va_list ap;
  72. va_start(ap, fmt);
  73. archive_string_vsprintf(as, fmt, ap);
  74. va_end(ap);
  75. }
  76. /*
  77. * Like 'vsprintf', but ensures the target is big enough, resizing if
  78. * necessary.
  79. */
  80. void
  81. archive_string_vsprintf(struct archive_string *as, const char *fmt,
  82. va_list ap)
  83. {
  84. char long_flag;
  85. intmax_t s; /* Signed integer temp. */
  86. uintmax_t u; /* Unsigned integer temp. */
  87. const char *p, *p2;
  88. const wchar_t *pw;
  89. if (archive_string_ensure(as, 64) == NULL)
  90. __archive_errx(1, "Out of memory");
  91. if (fmt == NULL) {
  92. as->s[0] = 0;
  93. return;
  94. }
  95. for (p = fmt; *p != '\0'; p++) {
  96. const char *saved_p = p;
  97. if (*p != '%') {
  98. archive_strappend_char(as, *p);
  99. continue;
  100. }
  101. p++;
  102. long_flag = '\0';
  103. switch(*p) {
  104. case 'j':
  105. case 'l':
  106. case 'z':
  107. long_flag = *p;
  108. p++;
  109. break;
  110. }
  111. switch (*p) {
  112. case '%':
  113. archive_strappend_char(as, '%');
  114. break;
  115. case 'c':
  116. s = va_arg(ap, int);
  117. archive_strappend_char(as, (char)s);
  118. break;
  119. case 'd':
  120. switch(long_flag) {
  121. case 'j': s = va_arg(ap, intmax_t); break;
  122. case 'l': s = va_arg(ap, long); break;
  123. case 'z': s = va_arg(ap, ssize_t); break;
  124. default: s = va_arg(ap, int); break;
  125. }
  126. append_int(as, s, 10);
  127. break;
  128. case 's':
  129. switch(long_flag) {
  130. case 'l':
  131. pw = va_arg(ap, wchar_t *);
  132. if (pw == NULL)
  133. pw = L"(null)";
  134. if (archive_string_append_from_wcs(as, pw,
  135. wcslen(pw)) != 0 && errno == ENOMEM)
  136. __archive_errx(1, "Out of memory");
  137. break;
  138. default:
  139. p2 = va_arg(ap, char *);
  140. if (p2 == NULL)
  141. p2 = "(null)";
  142. archive_strcat(as, p2);
  143. break;
  144. }
  145. break;
  146. case 'S':
  147. pw = va_arg(ap, wchar_t *);
  148. if (pw == NULL)
  149. pw = L"(null)";
  150. if (archive_string_append_from_wcs(as, pw,
  151. wcslen(pw)) != 0 && errno == ENOMEM)
  152. __archive_errx(1, "Out of memory");
  153. break;
  154. case 'o': case 'u': case 'x': case 'X':
  155. /* Common handling for unsigned integer formats. */
  156. switch(long_flag) {
  157. case 'j': u = va_arg(ap, uintmax_t); break;
  158. case 'l': u = va_arg(ap, unsigned long); break;
  159. case 'z': u = va_arg(ap, size_t); break;
  160. default: u = va_arg(ap, unsigned int); break;
  161. }
  162. /* Format it in the correct base. */
  163. switch (*p) {
  164. case 'o': append_uint(as, u, 8); break;
  165. case 'u': append_uint(as, u, 10); break;
  166. default: append_uint(as, u, 16); break;
  167. }
  168. break;
  169. default:
  170. /* Rewind and print the initial '%' literally. */
  171. p = saved_p;
  172. archive_strappend_char(as, *p);
  173. }
  174. }
  175. }