File: multi.c

package info (click to toggle)
bglibs 2.04%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,368 kB
  • sloc: ansic: 15,820; perl: 674; sh: 64; makefile: 26
file content (326 lines) | stat: -rw-r--r-- 9,832 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* fmt/multi.c - Format multiple items.
 * Copyright (C) 2005  Bruce Guenter <bruce@untroubled.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */
#include <errno.h>
#include <stdarg.h>
#include <string.h>
#include "fmt.h"
#include "fmt.h"
#include "fmt.h"
#include "str.h"

/** Format multiple items.

The \c format string used to describe the multiple items is related to
what is used with printf and related functions, but has one critical
difference: instead of formatted items being escaped, literal text must
be escaped.  This solves the largest security issue with using
printf-style format strings, which is the possibility of accidentally
treating untrusted text as the format string.

The format string is composed of zero or more format items.  Each item
is composed of the following parts:

<b>Zero or more flag characters:</b>

<dl>

<dt>\c # <dd>Use an "alternate form" to convert the value.  For octal
conversion, the result is prefixed with a \c 0 .  For hexadecimal
conversion, the result is prefixed with either \c 0x or \c 0X depending
on the conversion type.

<dt>\c - <dd>(not implemented) Pad on the right (left justified) instead
of on the left (default right justified).

<dt>\c 0 <dd>Pad the result with zeros instead of spaces.

</dl>

<b>Field width.</b>

The option field width is a decimal digit string specifying the minimum
field width.  If the converted value has fewer characters than the field
width, it will be padded out to the field width.

<b>Length modifier:</b>

<dl>

<dt>\c l <dd>The following integer conversion uses a \c long type.

<dt>\c ll <dd>The following integer conversion uses a \c long \c long
type.

</dl>

<b>Conversion specifier.</b>

<dl>

<dt>\c d <dt>\c i <dd>The \c int argument is converted to a signed
decimal string.

<dt>\c o <dt>\c u <dt>\c x <dt>\c X <dd>The \c unsigned \c int argument
is converted to a unsigned octal, unsigned decimal, lowercase unsigned
hexadecimal, or uppercase unsigned hexadecimal string respectively.

<dt>\c c <dd>The \c int argument is converted to an unsigned char.

<dt>\c s <dd>The \c const \c char* argument is converted.

<dt>\c S <dd>The \c const \c str* argument is converted.

<dt>\c p <dd>The \c void* argument is converted to a hexadecimal string.

<dt>\c m <dd>The result of \c strerror(errno) is formatted.

<dt>\c \\ <dd>The next character literal from the format string is
converted as with \c c conversion.

<dt>\c {string} <dd>The literal string enclosed by the parenthesis is
converted as with \c s conversion.

<dt>\c @ <dd>Formats an arbitrary object using two arguments: The first
::fmt_function argument is used to format the following \c void*
argument.  The ::fmt_function is passed the output buffer, the data
pointer, width, and pad character, and is expected to return the number
of bytes formatted.

</dl>
*/
unsigned fmt_multi(char* buffer, const char* format, ...)
{
  va_list ap;
  unsigned i;
  va_start(ap, format);
  i = fmt_multiv(buffer, format, ap);
  va_end(ap);
  return i;
}

static unsigned fmt_ullnumwa(char* buffer, unsigned long long u,
			     unsigned width, char pad,
			     unsigned base, const char* digits,
			     const char* prefix)
{
  unsigned prefixlen = 0;
  if (prefix != 0) {
    prefixlen = strlen(prefix);
    width = (width > prefixlen) ? width - prefixlen : 0;
    if (buffer != 0)
      for (; *prefix != 0; ++prefix, ++buffer)
	*buffer = *prefix;
  }
  return prefixlen + fmt_ullnumw(buffer, u, width, pad, base, digits);
}

static unsigned fmt_unumwa(char* buffer, unsigned long u,
			   unsigned width, char pad,
			   unsigned base, const char* digits,
			   const char* prefix)
{
  unsigned prefixlen = 0;
  if (prefix != 0) {
    prefixlen = strlen(prefix);
    width = (width > prefixlen) ? width - prefixlen : 0;
    if (buffer != 0)
      for (; *prefix != 0; ++prefix, ++buffer)
	*buffer = *prefix;
  }
  return prefixlen + fmt_unumw(buffer, u, width, pad, base, digits);
}

/** Format multiple items, using a va_list.

This is the core function used to format multiple items.
*/
unsigned fmt_multiv(char* buffer, const char* format, va_list ap)
{
  unsigned length;

  for (length = 0; *format != 0; ++format) {
    int pad = ' ';
    int islong = 0;
    int islonglong = 0;
    int altfmt = 0;
    int leftadj = 0;
    unsigned width;
    unsigned ilength;
    long long value = 0;
    const char* altstr;
    char conv;
    const str* strp;
    const void* voidp;
    fmt_function fn;

    for (; *format != 0; ++format) {
      switch (*format) {
      case '#': altfmt = 1; continue;
      case '-': leftadj = 1; continue;
      case '0': pad = '0'; continue;
      }
      break;
    }
    for (width = 0; *format >= '0' && *format <= '9'; ++format)
      width = (width * 10) + (*format - '0');
    while (*format == 'l') {
      ++format;
      ++islong;
    }
    islonglong = islong >= 2;
    conv = *format;
    switch (conv) {
    case 'i': case 'd': case 'o': case 'u': case 'x': case 'X':
      value = islonglong
	? va_arg(ap, long long)
	: islong
	? va_arg(ap, long)
	: va_arg(ap, int);
    }
    switch (conv) {
    case 'i':
    case 'd':
      ilength = islonglong
	? fmt_sllnumw(buffer, value, width, pad, 10, fmt_lcase_digits)
	: fmt_snumw(buffer, value, width, pad, 10, fmt_lcase_digits);
      break;
    case 'o':
      altstr = altfmt ? "0" : 0;
      ilength = islonglong
	? fmt_ullnumwa(buffer, value, width, pad, 8, fmt_lcase_digits, altstr)
	: fmt_unumwa(buffer, value, width, pad, 8, fmt_lcase_digits, altstr);
      break;
    case 'u':
      ilength = islonglong
	? fmt_ullnumw(buffer, value, width, pad, 10, fmt_lcase_digits)
	: fmt_unumw(buffer, value, width, pad, 10, fmt_lcase_digits);
      break;
    case 'x':
      altstr = altfmt ? "0x" : 0;
      ilength = islonglong
	? fmt_ullnumwa(buffer, value, width, pad, 16, fmt_lcase_digits, altstr)
	: fmt_unumwa(buffer, value, width, pad, 16, fmt_lcase_digits, altstr);
      break;
    case 'X':
      altstr = altfmt ? "0X" : 0;
      ilength = islonglong
	? fmt_ullnumwa(buffer, value, width, pad, 16, fmt_ucase_digits, altstr)
	: fmt_unumwa(buffer, value, width, pad, 16, fmt_ucase_digits, altstr);
      break;
    case 'c':
      ilength = fmt_char(buffer, va_arg(ap, int), width, pad);
      break;
    case 's':
      ilength = fmt_chars(buffer, va_arg(ap, const char*), width, pad);
      break;
    case 'S':
      strp = va_arg(ap, const str*);
      ilength = fmt_mem(buffer, strp->s, strp->len, width, pad);
      break;
    case 'p':
      ilength = fmt_unumwa(buffer, (unsigned long)va_arg(ap, void*),
			   width, pad, 16, fmt_lcase_digits, "0x");
      break;
    case 'm':
      ilength = fmt_chars(buffer, strerror(errno), width, pad);
      break;
    case '@':
      fn = va_arg(ap, fmt_function);
      voidp = va_arg(ap, const void*);
      ilength = fn(buffer, voidp, width, pad);
      break;
    case '\\':
      ilength = fmt_char(buffer, *++format, width, pad);
      break;
    case '{': 
      {
	const char* start = ++format;
	for (; *format != 0 && *format != '}'; ++format)
	  ;
	ilength = fmt_mem(buffer, start, format-start, width, pad);
      }
      break;
    default:
      ilength = 0;
    }
    if (buffer != 0)
      buffer += ilength;
    length += ilength;
  }
  return length;
}

#ifdef SELFTEST_MAIN
unsigned fmt_bool(char* buffer, const void* data, unsigned width, char pad)
{
  return fmt_chars(buffer, data ? "true" : "false", width, pad);
}

void testit(const char* format, ...)
{
  char buffer[100];
  unsigned length;
  va_list ap;

  va_start(ap, format);
  length = fmt_multiv(0, format, ap);
  va_end(ap);
  obuf_putu(&outbuf, length);
  obuf_putc(&outbuf, ':');

  va_start(ap, format);
  length = fmt_multiv(buffer, format, ap);
  va_end(ap);
  obuf_putu(&outbuf, length);
  obuf_putc(&outbuf, ':');
  obuf_write(&outbuf, buffer, length);
  obuf_endl(&outbuf);
}

MAIN
{
  testit("s{|}1s{|zzz|}5s\\|05s", "one", "two", "nine", "ten");
  testit("c\\|1c\\|5c\\|05c", 'a', 'b', 'c', 'd');
  testit("09{foo}");
  testit("o\\|x\\|X\\|i\\|d", 95, 95, 95, 95, 95);
  testit("#o\\|#x\\|#X\\|#i\\|#d", 95, 95, 95, 95, 95);
  testit("p", (void*)12345678);
  testit("o\\|lo\\|llo", 505050505U, 505050505UL, 5050505050505050505ULL);
  testit("u\\|lu\\|llu", 505050505U, 505050505UL, 5050505050505050505ULL);
  testit("x\\|lx\\|llx", 505050505U, 505050505UL, 5050505050505050505ULL);
  testit("X\\|lX\\|llX", 505050505U, 505050505UL, 5050505050505050505ULL);
  testit("i\\|li\\|lli", 505050505U, 505050505UL, 5050505050505050505ULL);
  testit("i\\|li\\|lli", -505050505U, -505050505UL, -5050505050505050505ULL);
  testit("10@\\|010@", fmt_bool, (void*)1, fmt_bool, (void*)0);
}
#endif
#ifdef SELFTEST_EXP
23:23:one|two|zzz| nine|00ten
15:15:a|b|    c|0000d
9:9:000000foo
15:15:137|5f|5F|95|95
20:20:0137|0x5f|0X5F|95|95
8:8:0xbc614e
43:43:3606472611|3606472611|430267771265752674611
39:39:505050505|505050505|5050505050505050505
34:34:1e1a7589|1e1a7589|4616ff95afab7989
34:34:1E1A7589|1E1A7589|4616FF95AFAB7989
39:39:505050505|505050505|5050505050505050505
42:42:-505050505|-505050505|-5050505050505050505
21:21:      true|00000false
#endif