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/lib/libarchive/archive_string_sprintf.c 189435 2009-03-06 05:14:55Z kientzle $");
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 #ifdef HAVE_ERRNO_H
42 #include <errno.h>
43 #endif
44 #include <stdio.h>
45 
46 #include "archive_string.h"
47 #include "archive_private.h"
48 
49 /*
50  * Utility functions to format signed/unsigned integers and append
51  * them to an archive_string.
52  */
53 static void
54 append_uint(struct archive_string *as, uintmax_t d, unsigned base)
55 {
56 	static const char digits[] = "0123456789abcdef";
57 	if (d >= base)
58 		append_uint(as, d/base, base);
59 	archive_strappend_char(as, digits[d % base]);
60 }
61 
62 static void
63 append_int(struct archive_string *as, intmax_t d, unsigned base)
64 {
65 	uintmax_t ud;
66 
67 	if (d < 0) {
68 		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 
76 void
77 archive_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  */
90 void
91 archive_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