archive_string_sprintf.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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: src/lib/libarchive/archive_string_sprintf.c,v 1.10 2008/03/14 22:00:09 kientzle Exp $");
  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. #include <stdio.h>
  40. #include "archive_string.h"
  41. #include "archive_private.h"
  42. /*
  43. * Utility functions to format signed/unsigned integers and append
  44. * them to an archive_string.
  45. */
  46. static void
  47. append_uint(struct archive_string *as, uintmax_t d, unsigned base)
  48. {
  49. static const char *digits = "0123456789abcdef";
  50. if (d >= base)
  51. append_uint(as, d/base, base);
  52. archive_strappend_char(as, digits[d % base]);
  53. }
  54. static void
  55. append_int(struct archive_string *as, intmax_t d, unsigned base)
  56. {
  57. uintmax_t ud;
  58. if (d < 0) {
  59. archive_strappend_char(as, '-');
  60. ud = (d == INTMAX_MIN) ? (uintmax_t)(INTMAX_MAX) + 1 : (uintmax_t)(-d);
  61. } else
  62. ud = d;
  63. append_uint(as, ud, base);
  64. }
  65. void
  66. __archive_string_sprintf(struct archive_string *as, const char *fmt, ...)
  67. {
  68. va_list ap;
  69. va_start(ap, fmt);
  70. archive_string_vsprintf(as, fmt, ap);
  71. va_end(ap);
  72. }
  73. /*
  74. * Like 'vsprintf', but ensures the target is big enough, resizing if
  75. * necessary.
  76. */
  77. void
  78. __archive_string_vsprintf(struct archive_string *as, const char *fmt,
  79. va_list ap)
  80. {
  81. char long_flag;
  82. intmax_t s; /* Signed integer temp. */
  83. uintmax_t u; /* Unsigned integer temp. */
  84. const char *p, *p2;
  85. if (__archive_string_ensure(as, 64) == NULL)
  86. __archive_errx(1, "Out of memory");
  87. if (fmt == NULL) {
  88. as->s[0] = 0;
  89. return;
  90. }
  91. for (p = fmt; *p != '\0'; p++) {
  92. const char *saved_p = p;
  93. if (*p != '%') {
  94. archive_strappend_char(as, *p);
  95. continue;
  96. }
  97. p++;
  98. long_flag = '\0';
  99. switch(*p) {
  100. case 'j':
  101. long_flag = 'j';
  102. p++;
  103. break;
  104. case 'l':
  105. long_flag = 'l';
  106. p++;
  107. break;
  108. }
  109. switch (*p) {
  110. case '%':
  111. __archive_strappend_char(as, '%');
  112. break;
  113. case 'c':
  114. s = va_arg(ap, int);
  115. __archive_strappend_char(as, s);
  116. break;
  117. case 'd':
  118. switch(long_flag) {
  119. case 'j': s = va_arg(ap, intmax_t); break;
  120. case 'l': s = va_arg(ap, long); break;
  121. default: s = va_arg(ap, int); break;
  122. }
  123. append_int(as, s, 10);
  124. break;
  125. case 's':
  126. p2 = va_arg(ap, char *);
  127. archive_strcat(as, p2);
  128. break;
  129. case 'o': case 'u': case 'x': case 'X':
  130. /* Common handling for unsigned integer formats. */
  131. switch(long_flag) {
  132. case 'j': u = va_arg(ap, uintmax_t); break;
  133. case 'l': u = va_arg(ap, unsigned long); break;
  134. default: u = va_arg(ap, unsigned int); break;
  135. }
  136. /* Format it in the correct base. */
  137. switch (*p) {
  138. case 'o': append_uint(as, u, 8); break;
  139. case 'u': append_uint(as, u, 10); break;
  140. default: append_uint(as, u, 16); break;
  141. }
  142. break;
  143. default:
  144. /* Rewind and print the initial '%' literally. */
  145. p = saved_p;
  146. archive_strappend_char(as, *p);
  147. }
  148. }
  149. }