117680Spst/*- 217680Spst * Copyright (c) 2003-2007 Tim Kientzle 317680Spst * All rights reserved. 417680Spst * 517680Spst * Redistribution and use in source and binary forms, with or without 617680Spst * modification, are permitted provided that the following conditions 717680Spst * are met: 817680Spst * 1. Redistributions of source code must retain the above copyright 917680Spst * notice, this list of conditions and the following disclaimer. 1017680Spst * 2. Redistributions in binary form must reproduce the above copyright 1117680Spst * notice, this list of conditions and the following disclaimer in the 1217680Spst * documentation and/or other materials provided with the distribution. 1317680Spst * 1417680Spst * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 1517680Spst * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1617680Spst * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1717680Spst * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 1817680Spst * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1917680Spst * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2017680Spst * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2117680Spst * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2217680Spst * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2317680Spst * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2417680Spst */ 2517680Spst 2617680Spst#include "archive_platform.h" 2717680Spst__FBSDID("$FreeBSD$"); 2817680Spst 2917680Spst/* 3017680Spst * The use of printf()-family functions can be troublesome 3117680Spst * for space-constrained applications. In addition, correctly 3217680Spst * implementing this function in terms of vsnprintf() requires 3317680Spst * two calls (one to determine the size, another to format the 3417680Spst * result), which in turn requires duplicating the argument list 3517680Spst * using va_copy, which isn't yet universally available. <sigh> 3617680Spst * 3717680Spst * So, I've implemented a bare minimum of printf()-like capability 3817680Spst * here. This is only used to format error messages, so doesn't 3917680Spst * require any floating-point support or field-width handling. 4017680Spst */ 4117680Spst#ifdef HAVE_ERRNO_H 4217680Spst#include <errno.h> 4317680Spst#endif 4417680Spst#include <stdio.h> 4517680Spst 4617680Spst#include "archive_string.h" 4717680Spst#include "archive_private.h" 4817680Spst 4917680Spst/* 5017680Spst * Utility functions to format signed/unsigned integers and append 5117680Spst * them to an archive_string. 5217680Spst */ 5317680Spststatic void 5417680Spstappend_uint(struct archive_string *as, uintmax_t d, unsigned base) 5517680Spst{ 5617680Spst static const char *digits = "0123456789abcdef"; 5717680Spst if (d >= base) 5817680Spst append_uint(as, d/base, base); 5917680Spst archive_strappend_char(as, digits[d % base]); 6017680Spst} 6117680Spst 6217680Spststatic void 6317680Spstappend_int(struct archive_string *as, intmax_t d, unsigned base) 6417680Spst{ 6517680Spst uintmax_t ud; 6617680Spst 6717680Spst if (d < 0) { 6817680Spst archive_strappend_char(as, '-'); 69 ud = (d == INTMAX_MIN) ? (uintmax_t)(INTMAX_MAX) + 1 : (uintmax_t)(-d); 70 } else 71 ud = d; 72 append_uint(as, ud, base); 73} 74 75 76void 77archive_string_sprintf(struct archive_string *as, const char *fmt, ...) 78{ 79 va_list ap; 80 81 va_start(ap, fmt); 82 archive_string_vsprintf(as, fmt, ap); 83 va_end(ap); 84} 85 86/* 87 * Like 'vsprintf', but ensures the target is big enough, resizing if 88 * necessary. 89 */ 90void 91archive_string_vsprintf(struct archive_string *as, const char *fmt, 92 va_list ap) 93{ 94 char long_flag; 95 intmax_t s; /* Signed integer temp. */ 96 uintmax_t u; /* Unsigned integer temp. */ 97 const char *p, *p2; 98 const wchar_t *pw; 99 100 if (archive_string_ensure(as, 64) == NULL) 101 __archive_errx(1, "Out of memory"); 102 103 if (fmt == NULL) { 104 as->s[0] = 0; 105 return; 106 } 107 108 for (p = fmt; *p != '\0'; p++) { 109 const char *saved_p = p; 110 111 if (*p != '%') { 112 archive_strappend_char(as, *p); 113 continue; 114 } 115 116 p++; 117 118 long_flag = '\0'; 119 switch(*p) { 120 case 'j': 121 case 'l': 122 case 'z': 123 long_flag = *p; 124 p++; 125 break; 126 } 127 128 switch (*p) { 129 case '%': 130 archive_strappend_char(as, '%'); 131 break; 132 case 'c': 133 s = va_arg(ap, int); 134 archive_strappend_char(as, (char)s); 135 break; 136 case 'd': 137 switch(long_flag) { 138 case 'j': s = va_arg(ap, intmax_t); break; 139 case 'l': s = va_arg(ap, long); break; 140 case 'z': s = va_arg(ap, ssize_t); break; 141 default: s = va_arg(ap, int); break; 142 } 143 append_int(as, s, 10); 144 break; 145 case 's': 146 switch(long_flag) { 147 case 'l': 148 pw = va_arg(ap, wchar_t *); 149 if (pw == NULL) 150 pw = L"(null)"; 151 if (archive_string_append_from_wcs(as, pw, 152 wcslen(pw)) != 0 && errno == ENOMEM) 153 __archive_errx(1, "Out of memory"); 154 break; 155 default: 156 p2 = va_arg(ap, char *); 157 if (p2 == NULL) 158 p2 = "(null)"; 159 archive_strcat(as, p2); 160 break; 161 } 162 break; 163 case 'S': 164 pw = va_arg(ap, wchar_t *); 165 if (pw == NULL) 166 pw = L"(null)"; 167 if (archive_string_append_from_wcs(as, pw, 168 wcslen(pw)) != 0 && errno == ENOMEM) 169 __archive_errx(1, "Out of memory"); 170 break; 171 case 'o': case 'u': case 'x': case 'X': 172 /* Common handling for unsigned integer formats. */ 173 switch(long_flag) { 174 case 'j': u = va_arg(ap, uintmax_t); break; 175 case 'l': u = va_arg(ap, unsigned long); break; 176 case 'z': u = va_arg(ap, size_t); break; 177 default: u = va_arg(ap, unsigned int); break; 178 } 179 /* Format it in the correct base. */ 180 switch (*p) { 181 case 'o': append_uint(as, u, 8); break; 182 case 'u': append_uint(as, u, 10); break; 183 default: append_uint(as, u, 16); break; 184 } 185 break; 186 default: 187 /* Rewind and print the initial '%' literally. */ 188 p = saved_p; 189 archive_strappend_char(as, *p); 190 } 191 } 192} 193