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 */
52static void
53append_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
61static void
62append_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
75void
76archive_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 */
89void
90archive_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 'j':
120		case 'l':
121		case 'z':
122			long_flag = *p;
123			p++;
124			break;
125		}
126
127		switch (*p) {
128		case '%':
129			archive_strappend_char(as, '%');
130			break;
131		case 'c':
132			s = va_arg(ap, int);
133			archive_strappend_char(as, (char)s);
134			break;
135		case 'd':
136			switch(long_flag) {
137			case 'j': s = va_arg(ap, intmax_t); break;
138			case 'l': s = va_arg(ap, long); break;
139			case 'z': s = va_arg(ap, ssize_t); break;
140			default:  s = va_arg(ap, int); break;
141			}
142		        append_int(as, s, 10);
143			break;
144		case 's':
145			switch(long_flag) {
146			case 'l':
147				pw = va_arg(ap, wchar_t *);
148				if (pw == NULL)
149					pw = L"(null)";
150				if (archive_string_append_from_wcs(as, pw,
151				    wcslen(pw)) != 0 && errno == ENOMEM)
152					__archive_errx(1, "Out of memory");
153				break;
154			default:
155				p2 = va_arg(ap, char *);
156				if (p2 == NULL)
157					p2 = "(null)";
158				archive_strcat(as, p2);
159				break;
160			}
161			break;
162		case 'S':
163			pw = va_arg(ap, wchar_t *);
164			if (pw == NULL)
165				pw = L"(null)";
166			if (archive_string_append_from_wcs(as, pw,
167			    wcslen(pw)) != 0 && errno == ENOMEM)
168				__archive_errx(1, "Out of memory");
169			break;
170		case 'o': case 'u': case 'x': case 'X':
171			/* Common handling for unsigned integer formats. */
172			switch(long_flag) {
173			case 'j': u = va_arg(ap, uintmax_t); break;
174			case 'l': u = va_arg(ap, unsigned long); break;
175			case 'z': u = va_arg(ap, size_t); break;
176			default:  u = va_arg(ap, unsigned int); break;
177			}
178			/* Format it in the correct base. */
179			switch (*p) {
180			case 'o': append_uint(as, u, 8); break;
181			case 'u': append_uint(as, u, 10); break;
182			default: append_uint(as, u, 16); break;
183			}
184			break;
185		default:
186			/* Rewind and print the initial '%' literally. */
187			p = saved_p;
188			archive_strappend_char(as, *p);
189		}
190	}
191}
192