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 26 #include "archive_platform.h" 27 28 /* 29 * The use of printf()-family functions can be troublesome 30 * for space-constrained applications. In addition, correctly 31 * implementing this function in terms of vsnprintf() requires 32 * two calls (one to determine the size, another to format the 33 * result), which in turn requires duplicating the argument list 34 * using va_copy, which isn't yet universally available. <sigh> 35 * 36 * So, I've implemented a bare minimum of printf()-like capability 37 * here. This is only used to format error messages, so doesn't 38 * require any floating-point support or field-width handling. 39 */ 40 #ifdef HAVE_ERRNO_H 41 #include <errno.h> 42 #endif 43 #include <stdio.h> 44 45 #include "archive_string.h" 46 #include "archive_private.h" 47 48 /* 49 * Utility functions to format signed/unsigned integers and append 50 * them to an archive_string. 51 */ 52 static void 53 append_uint(struct archive_string *as, uintmax_t d, unsigned base) 54 { 55 static const char digits[] = "0123456789abcdef"; 56 if (d >= base) 57 append_uint(as, d/base, base); 58 archive_strappend_char(as, digits[d % base]); 59 } 60 61 static void 62 append_int(struct archive_string *as, intmax_t d, unsigned base) 63 { 64 uintmax_t ud; 65 66 if (d < 0) { 67 archive_strappend_char(as, '-'); 68 ud = (d == INTMAX_MIN) ? (uintmax_t)(INTMAX_MAX) + 1 : (uintmax_t)(-d); 69 } else 70 ud = d; 71 append_uint(as, ud, base); 72 } 73 74 75 void 76 archive_string_sprintf(struct archive_string *as, const char *fmt, ...) 77 { 78 va_list ap; 79 80 va_start(ap, fmt); 81 archive_string_vsprintf(as, fmt, ap); 82 va_end(ap); 83 } 84 85 /* 86 * Like 'vsprintf', but ensures the target is big enough, resizing if 87 * necessary. 88 */ 89 void 90 archive_string_vsprintf(struct archive_string *as, const char *fmt, 91 va_list ap) 92 { 93 char long_flag; 94 intmax_t s; /* Signed integer temp. */ 95 uintmax_t u; /* Unsigned integer temp. */ 96 const char *p, *p2; 97 const wchar_t *pw; 98 99 if (archive_string_ensure(as, 64) == NULL) 100 __archive_errx(1, "Out of memory"); 101 102 if (fmt == NULL) { 103 as->s[0] = 0; 104 return; 105 } 106 107 for (p = fmt; *p != '\0'; p++) { 108 const char *saved_p = p; 109 110 if (*p != '%') { 111 archive_strappend_char(as, *p); 112 continue; 113 } 114 115 p++; 116 117 long_flag = '\0'; 118 switch(*p) { 119 case 'l': 120 if (p[1] == 'l') { 121 long_flag = 'L'; 122 p += 2; 123 break; 124 } 125 __LA_FALLTHROUGH; 126 case 'j': 127 case 'z': 128 long_flag = *p; 129 p++; 130 break; 131 } 132 133 switch (*p) { 134 case '%': 135 archive_strappend_char(as, '%'); 136 break; 137 case 'c': 138 s = va_arg(ap, int); 139 archive_strappend_char(as, (char)s); 140 break; 141 case 'd': 142 switch(long_flag) { 143 case 'j': s = va_arg(ap, intmax_t); break; 144 case 'l': s = va_arg(ap, long); break; 145 case 'L': s = va_arg(ap, long long); break; 146 case 'z': s = va_arg(ap, ssize_t); break; 147 default: s = va_arg(ap, int); break; 148 } 149 append_int(as, s, 10); 150 break; 151 case 's': 152 switch(long_flag) { 153 case 'l': 154 case 'L': 155 pw = va_arg(ap, wchar_t *); 156 if (pw == NULL) 157 pw = L"(null)"; 158 if (archive_string_append_from_wcs(as, pw, 159 wcslen(pw)) != 0 && errno == ENOMEM) 160 __archive_errx(1, "Out of memory"); 161 break; 162 default: 163 p2 = va_arg(ap, char *); 164 if (p2 == NULL) 165 p2 = "(null)"; 166 archive_strcat(as, p2); 167 break; 168 } 169 break; 170 case 'S': 171 pw = va_arg(ap, wchar_t *); 172 if (pw == NULL) 173 pw = L"(null)"; 174 if (archive_string_append_from_wcs(as, pw, 175 wcslen(pw)) != 0 && errno == ENOMEM) 176 __archive_errx(1, "Out of memory"); 177 break; 178 case 'o': case 'u': case 'x': case 'X': 179 /* Common handling for unsigned integer formats. */ 180 switch(long_flag) { 181 case 'j': u = va_arg(ap, uintmax_t); break; 182 case 'l': u = va_arg(ap, unsigned long); break; 183 case 'L': u = va_arg(ap, unsigned long long); break; 184 case 'z': u = va_arg(ap, size_t); break; 185 default: u = va_arg(ap, unsigned int); break; 186 } 187 /* Format it in the correct base. */ 188 switch (*p) { 189 case 'o': append_uint(as, u, 8); break; 190 case 'u': append_uint(as, u, 10); break; 191 default: append_uint(as, u, 16); break; 192 } 193 break; 194 default: 195 /* Rewind and print the initial '%' literally. */ 196 p = saved_p; 197 archive_strappend_char(as, *p); 198 } 199 } 200 } 201