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