archive_string_sprintf.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if (d < 0) {
  58. archive_strappend_char(as, '-');
  59. d = -d;
  60. }
  61. append_uint(as, d, base);
  62. }
  63. void
  64. __archive_string_sprintf(struct archive_string *as, const char *fmt, ...)
  65. {
  66. va_list ap;
  67. va_start(ap, fmt);
  68. archive_string_vsprintf(as, fmt, ap);
  69. va_end(ap);
  70. }
  71. /*
  72. * Like 'vsprintf', but ensures the target is big enough, resizing if
  73. * necessary.
  74. */
  75. void
  76. __archive_string_vsprintf(struct archive_string *as, const char *fmt,
  77. va_list ap)
  78. {
  79. char long_flag;
  80. intmax_t s; /* Signed integer temp. */
  81. uintmax_t u; /* Unsigned integer temp. */
  82. const char *p, *p2;
  83. if (__archive_string_ensure(as, 64) == NULL)
  84. __archive_errx(1, "Out of memory");
  85. if (fmt == NULL) {
  86. as->s[0] = 0;
  87. return;
  88. }
  89. for (p = fmt; *p != '\0'; p++) {
  90. const char *saved_p = p;
  91. if (*p != '%') {
  92. archive_strappend_char(as, *p);
  93. continue;
  94. }
  95. p++;
  96. long_flag = '\0';
  97. switch(*p) {
  98. case 'j':
  99. long_flag = 'j';
  100. p++;
  101. break;
  102. case 'l':
  103. long_flag = 'l';
  104. p++;
  105. break;
  106. }
  107. switch (*p) {
  108. case '%':
  109. __archive_strappend_char(as, '%');
  110. break;
  111. case 'c':
  112. s = va_arg(ap, int);
  113. __archive_strappend_char(as, s);
  114. break;
  115. case 'd':
  116. switch(long_flag) {
  117. case 'j': s = va_arg(ap, intmax_t); break;
  118. case 'l': s = va_arg(ap, long); break;
  119. default: s = va_arg(ap, int); break;
  120. }
  121. append_int(as, s, 10);
  122. break;
  123. case 's':
  124. p2 = va_arg(ap, char *);
  125. archive_strcat(as, p2);
  126. break;
  127. case 'o': case 'u': case 'x': case 'X':
  128. /* Common handling for unsigned integer formats. */
  129. switch(long_flag) {
  130. case 'j': u = va_arg(ap, uintmax_t); break;
  131. case 'l': u = va_arg(ap, unsigned long); break;
  132. default: u = va_arg(ap, unsigned int); break;
  133. }
  134. /* Format it in the correct base. */
  135. switch (*p) {
  136. case 'o': append_uint(as, u, 8); break;
  137. case 'u': append_uint(as, u, 10); break;
  138. default: append_uint(as, u, 16); break;
  139. }
  140. break;
  141. default:
  142. /* Rewind and print the initial '%' literally. */
  143. p = saved_p;
  144. archive_strappend_char(as, *p);
  145. }
  146. }
  147. }