]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/printf.c
fixed condition
[proxmark3-svn] / armsrc / printf.c
CommitLineData
6f5cb60c 1/*-
2 * Copyright (c) 1986, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
35 */
36
f7e3ed82 37#include <stddef.h>
38#include <stdarg.h>
39#include "printf.h"
40#include "util.h"
9ab7a6c7 41#include "string.h"
f7e3ed82 42
6f5cb60c 43typedef unsigned char u_char;
44typedef unsigned int u_int;
45typedef unsigned long u_long;
46typedef unsigned short u_short;
47typedef unsigned long long u_quad_t;
48typedef long long quad_t;
f7e3ed82 49
50typedef int ssize_t;
51
6f5cb60c 52#define NBBY 8 /* number of bits in a byte */
f7e3ed82 53
6f5cb60c 54char const hex2ascii_data[] = "0123456789abcdefghijklmnopqrstuvwxyz";
55#define hex2ascii(hex) (hex2ascii_data[hex])
6f5cb60c 56#define toupper(c) ((c) - 0x20 * (((c) >= 'a') && ((c) <= 'z')))
6f5cb60c 57
58/* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
59#define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
60
61/*
62 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
63 * order; return an optional length and a pointer to the last character
64 * written in the buffer (i.e., the first character of the string).
65 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
66 */
67static char *
68ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
69{
70 char *p, c;
71
72 p = nbuf;
73 *p = '\0';
74 do {
75 c = hex2ascii(num % base);
76 *++p = upper ? toupper(c) : c;
77 } while (num /= base);
78 if (lenp)
79 *lenp = p - nbuf;
80 return (p);
81}
82
83/*
84 * Scaled down version of printf(3).
85 *
86 * Two additional formats:
87 *
88 * The format %b is supported to decode error registers.
89 * Its usage is:
90 *
91 * printf("reg=%b\n", regval, "*");
92 *
93 * where is the output base expressed as a control character, e.g.
94 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
95 * the first of which gives the bit number to be inspected (origin 1), and
96 * the next characters (up to a control character, i.e. a character <= 32),
97 * give the name of the register. Thus:
98 *
99 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
100 *
101 * would produce output:
102 *
103 * reg=3
104 *
105 * XXX: %D -- Hexdump, takes pointer and separator string:
106 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
107 * ("%*D", len, ptr, " " -> XX XX XX XX ...
108 */
109int
110kvsprintf(char const *fmt, void *arg, int radix, va_list ap)
111{
112#define PCHAR(c) {int cc=(c); *d++ = cc; retval++; }
113 char nbuf[MAXNBUF];
114 char *d;
115 const char *p, *percent, *q;
116 u_char *up;
117 int ch, n;
118 uintmax_t num;
119 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
120 int cflag, hflag, jflag, tflag, zflag;
121 int dwidth, upper;
122 char padc;
123 int stop = 0, retval = 0;
124
125 num = 0;
126 d = (char *) arg;
127
128 if (fmt == NULL)
129 fmt = "(fmt null)\n";
130
131 if (radix < 2 || radix > 36)
132 radix = 10;
133
134 for (;;) {
135 padc = ' ';
136 width = 0;
137 while ((ch = (u_char)*fmt++) != '%' || stop) {
138 PCHAR(ch);
139 if (ch == '\0')
140 return (retval);
141 }
142 percent = fmt - 1;
143 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
144 sign = 0; dot = 0; dwidth = 0; upper = 0;
145 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
146reswitch: switch (ch = (u_char)*fmt++) {
147 case '.':
148 dot = 1;
149 goto reswitch;
150 case '#':
151 sharpflag = 1;
152 goto reswitch;
153 case '+':
154 sign = 1;
155 goto reswitch;
156 case '-':
157 ladjust = 1;
158 goto reswitch;
159 case '%':
160 PCHAR(ch);
161 break;
162 case '*':
163 if (!dot) {
164 width = va_arg(ap, int);
165 if (width < 0) {
166 ladjust = !ladjust;
167 width = -width;
168 }
169 } else {
170 dwidth = va_arg(ap, int);
171 }
172 goto reswitch;
173 case '0':
174 if (!dot) {
175 padc = '0';
176 goto reswitch;
177 }
178 case '1': case '2': case '3': case '4':
179 case '5': case '6': case '7': case '8': case '9':
180 for (n = 0;; ++fmt) {
181 n = n * 10 + ch - '0';
182 ch = *fmt;
183 if (ch < '0' || ch > '9')
184 break;
185 }
186 if (dot)
187 dwidth = n;
188 else
189 width = n;
190 goto reswitch;
191 case 'b':
192 num = (u_int)va_arg(ap, int);
193 p = va_arg(ap, char *);
194 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
195 PCHAR(*q--);
196
197 if (num == 0)
198 break;
199
200 for (tmp = 0; *p;) {
201 n = *p++;
202 if (num & (1 << (n - 1))) {
203 PCHAR(tmp ? ',' : '<');
204 for (; (n = *p) > ' '; ++p)
205 PCHAR(n);
206 tmp = 1;
207 } else
208 for (; *p > ' '; ++p)
209 continue;
210 }
211 if (tmp)
212 PCHAR('>');
213 break;
214 case 'c':
215 PCHAR(va_arg(ap, int));
216 break;
217 case 'D':
218 up = va_arg(ap, u_char *);
219 p = va_arg(ap, char *);
220 if (!width)
221 width = 16;
222 while(width--) {
223 PCHAR(hex2ascii(*up >> 4));
224 PCHAR(hex2ascii(*up & 0x0f));
225 up++;
226 if (width)
227 for (q=p;*q;q++)
228 PCHAR(*q);
229 }
230 break;
231 case 'd':
232 case 'i':
233 base = 10;
234 sign = 1;
235 goto handle_sign;
236 case 'h':
237 if (hflag) {
238 hflag = 0;
239 cflag = 1;
240 } else
241 hflag = 1;
242 goto reswitch;
243 case 'j':
244 jflag = 1;
245 goto reswitch;
246 case 'l':
247 if (lflag) {
248 lflag = 0;
249 qflag = 1;
250 } else
251 lflag = 1;
252 goto reswitch;
253 case 'n':
254 if (jflag)
255 *(va_arg(ap, intmax_t *)) = retval;
256 else if (qflag)
257 *(va_arg(ap, quad_t *)) = retval;
258 else if (lflag)
259 *(va_arg(ap, long *)) = retval;
260 else if (zflag)
261 *(va_arg(ap, size_t *)) = retval;
262 else if (hflag)
263 *(va_arg(ap, short *)) = retval;
264 else if (cflag)
265 *(va_arg(ap, char *)) = retval;
266 else
267 *(va_arg(ap, int *)) = retval;
268 break;
269 case 'o':
270 base = 8;
271 goto handle_nosign;
272 case 'p':
273 base = 16;
274 sharpflag = (width == 0);
275 sign = 0;
276 num = (uintptr_t)va_arg(ap, void *);
277 goto number;
278 case 'q':
279 qflag = 1;
280 goto reswitch;
281 case 'r':
282 base = radix;
283 if (sign)
284 goto handle_sign;
285 goto handle_nosign;
286 case 's':
287 p = va_arg(ap, char *);
288 if (p == NULL)
289 p = "(null)";
290 if (!dot)
291 n = strlen (p);
292 else
293 for (n = 0; n < dwidth && p[n]; n++)
294 continue;
295
296 width -= n;
297
298 if (!ladjust && width > 0)
299 while (width--)
300 PCHAR(padc);
301 while (n--)
302 PCHAR(*p++);
303 if (ladjust && width > 0)
304 while (width--)
305 PCHAR(padc);
306 break;
307 case 't':
308 tflag = 1;
309 goto reswitch;
310 case 'u':
311 base = 10;
312 goto handle_nosign;
313 case 'X':
314 upper = 1;
315 case 'x':
316 base = 16;
317 goto handle_nosign;
318 case 'y':
319 base = 16;
320 sign = 1;
321 goto handle_sign;
322 case 'z':
323 zflag = 1;
324 goto reswitch;
325handle_nosign:
326 sign = 0;
327 if (jflag)
328 num = va_arg(ap, uintmax_t);
329 else if (qflag)
330 num = va_arg(ap, u_quad_t);
331 else if (tflag)
332 num = va_arg(ap, ptrdiff_t);
333 else if (lflag)
334 num = va_arg(ap, u_long);
335 else if (zflag)
336 num = va_arg(ap, size_t);
337 else if (hflag)
338 num = (u_short)va_arg(ap, int);
339 else if (cflag)
340 num = (u_char)va_arg(ap, int);
341 else
342 num = va_arg(ap, u_int);
343 goto number;
344handle_sign:
345 if (jflag)
346 num = va_arg(ap, intmax_t);
347 else if (qflag)
348 num = va_arg(ap, quad_t);
349 else if (tflag)
350 num = va_arg(ap, ptrdiff_t);
351 else if (lflag)
352 num = va_arg(ap, long);
353 else if (zflag)
354 num = va_arg(ap, ssize_t);
355 else if (hflag)
356 num = (short)va_arg(ap, int);
357 else if (cflag)
358 num = (char)va_arg(ap, int);
359 else
360 num = va_arg(ap, int);
361number:
362 if (sign && (intmax_t)num < 0) {
363 neg = 1;
364 num = -(intmax_t)num;
365 }
366 p = ksprintn(nbuf, num, base, &tmp, upper);
367 if (sharpflag && num != 0) {
368 if (base == 8)
369 tmp++;
370 else if (base == 16)
371 tmp += 2;
372 }
373 if (neg)
374 tmp++;
375
376 if (!ladjust && padc != '0' && width
377 && (width -= tmp) > 0)
378 while (width--)
379 PCHAR(padc);
380 if (neg)
381 PCHAR('-');
382 if (sharpflag && num != 0) {
383 if (base == 8) {
384 PCHAR('0');
385 } else if (base == 16) {
386 PCHAR('0');
387 PCHAR('x');
388 }
389 }
390 if (!ladjust && width && (width -= tmp) > 0)
391 while (width--)
392 PCHAR(padc);
393
394 while (*p)
395 PCHAR(*p--);
396
397 if (ladjust && width && (width -= tmp) > 0)
398 while (width--)
399 PCHAR(padc);
400
401 break;
402 default:
403 while (percent < fmt)
404 PCHAR(*percent++);
405 /*
406 * Since we ignore an formatting argument it is no
407 * longer safe to obey the remaining formatting
408 * arguments as the arguments will no longer match
409 * the format specs.
410 */
411 stop = 1;
412 break;
413 }
414 }
415 PCHAR(0);
416 return retval;
417#undef PCHAR
418}
419
f7e3ed82 420int vsprintf(char *dest, const char *fmt, va_list ap)
421{
422 return kvsprintf(fmt, dest, 10, ap);
423}
424
6f5cb60c 425int
426sprintf(char *dest, const char *fmt, ...)
427{
428 /* http://www.pagetable.com/?p=298 */
429 int retval;
430 va_list ap;
431
432 va_start(ap, fmt);
433 retval = kvsprintf(fmt, dest, 10, ap);
434 va_end(ap);
435 return retval;
436}
Impressum, Datenschutz