| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/* Copyright 2014 Emil Renner Berthing <[email protected]> */ |
| 3 |
|
| 4 |
#include <limits.h> |
| 5 |
#include <printf.h> |
| 6 |
#include <stdint.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
static const char* consume_nonarg(const char *fmt) { |
| 10 |
do { |
| 11 |
if (*fmt == '\0') |
| 12 |
return fmt; |
| 13 |
} while (*fmt++ != '%'); |
| 14 |
return fmt; |
| 15 |
} |
| 16 |
|
| 17 |
static const char* consume_num(const char *fmt) { |
| 18 |
for (;*fmt >= '0' && *fmt <= '9'; fmt++) |
| 19 |
/* do nothing */; |
| 20 |
return fmt; |
| 21 |
} |
| 22 |
|
| 23 |
static const char* consume_argn(const char *fmt, size_t *arg) { |
| 24 |
const char *p = fmt; |
| 25 |
size_t val = 0; |
| 26 |
|
| 27 |
if (*p < '1' || *p > '9') |
| 28 |
return fmt; |
| 29 |
do { |
| 30 |
val = 10*val + (*p++ - '0'); |
| 31 |
} while (*p >= '0' && *p <= '9'); |
| 32 |
|
| 33 |
if (*p != '$') |
| 34 |
return fmt; |
| 35 |
*arg = val; |
| 36 |
return p+1; |
| 37 |
} |
| 38 |
|
| 39 |
static const char* consume_flags(const char *fmt) { |
| 40 |
for (;;) |
| 41 |
switch (*fmt) { |
| 42 |
case '#': |
| 43 |
case '0': |
| 44 |
case '-': |
| 45 |
case ' ': |
| 46 |
case '+': |
| 47 |
case '\'': |
| 48 |
case 'I': |
| 49 |
fmt++; |
| 50 |
continue; |
| 51 |
default: |
| 52 |
return fmt; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
enum state { |
| 57 |
BARE, |
| 58 |
LPRE, |
| 59 |
LLPRE, |
| 60 |
HPRE, |
| 61 |
HHPRE, |
| 62 |
BIGLPRE, |
| 63 |
ZTPRE, |
| 64 |
JPRE, |
| 65 |
STOP, |
| 66 |
}; |
| 67 |
|
| 68 |
enum type { |
| 69 |
NONE, |
| 70 |
PTR, |
| 71 |
STR, |
| 72 |
WSTR, |
| 73 |
INT, |
| 74 |
SHORT, |
| 75 |
LONG, |
| 76 |
LLONG, |
| 77 |
IMAX, |
| 78 |
SIZET, |
| 79 |
CHAR, |
| 80 |
WCHAR, |
| 81 |
DBL, |
| 82 |
LDBL, |
| 83 |
NPTR, |
| 84 |
_TYPE_MAX, |
| 85 |
}; |
| 86 |
|
| 87 |
static const short pa_types[_TYPE_MAX] = { |
| 88 |
[NONE] = PA_INT, |
| 89 |
[PTR] = PA_POINTER, |
| 90 |
[STR] = PA_STRING, |
| 91 |
[WSTR] = PA_WSTRING, |
| 92 |
[INT] = PA_INT, |
| 93 |
[SHORT] = PA_INT | PA_FLAG_SHORT, |
| 94 |
[LONG] = PA_INT | PA_FLAG_LONG, |
| 95 |
#if ULLONG_MAX > ULONG_MAX |
| 96 |
[LLONG] = PA_INT | PA_FLAG_LONG_LONG, |
| 97 |
#else |
| 98 |
[LLONG] = PA_INT | PA_FLAG_LONG, |
| 99 |
#endif |
| 100 |
#if UINTMAX_MAX > ULONG_MAX |
| 101 |
[IMAX] = PA_INT | PA_FLAG_LONG_LONG, |
| 102 |
#elif UINTMAX_MAX > UINT_MAX |
| 103 |
[IMAX] = PA_INT | PA_FLAG_LONG, |
| 104 |
#else |
| 105 |
[IMAX] = PA_INT, |
| 106 |
#endif |
| 107 |
#if SIZE_MAX > ULONG_MAX |
| 108 |
[SIZET] = PA_INT | PA_FLAG_LONG_LONG, |
| 109 |
#elif SIZE_MAX > UINT_MAX |
| 110 |
[SIZET] = PA_INT | PA_FLAG_LONG, |
| 111 |
#else |
| 112 |
[SIZET] = PA_INT, |
| 113 |
#endif |
| 114 |
[CHAR] = PA_CHAR, |
| 115 |
[WCHAR] = PA_WCHAR, |
| 116 |
[DBL] = PA_DOUBLE, |
| 117 |
[LDBL] = PA_DOUBLE | PA_FLAG_LONG_DOUBLE, |
| 118 |
[NPTR] = PA_FLAG_PTR, |
| 119 |
}; |
| 120 |
|
| 121 |
#define S(x) [(x)-'A'] |
| 122 |
#define E(x) (STOP + (x)) |
| 123 |
|
| 124 |
static const unsigned char states[]['z'-'A'+1] = { |
| 125 |
{ /* 0: bare types */ |
| 126 |
S('d') = E(INT), S('i') = E(INT), |
| 127 |
S('o') = E(INT), S('u') = E(INT), S('x') = E(INT), S('X') = E(INT), |
| 128 |
S('e') = E(DBL), S('f') = E(DBL), S('g') = E(DBL), S('a') = E(DBL), |
| 129 |
S('E') = E(DBL), S('F') = E(DBL), S('G') = E(DBL), S('A') = E(DBL), |
| 130 |
S('c') = E(CHAR), S('C') = E(WCHAR), |
| 131 |
S('s') = E(STR), S('S') = E(WSTR), S('p') = E(PTR), |
| 132 |
S('n') = E(NPTR), |
| 133 |
S('m') = E(NONE), |
| 134 |
S('l') = LPRE, S('q') = LLPRE, S('h') = HPRE, S('L') = BIGLPRE, |
| 135 |
S('z') = ZTPRE, S('Z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE, |
| 136 |
}, |
| 137 |
{ /* 1: l-prefixed */ |
| 138 |
S('d') = E(LONG), S('i') = E(LONG), |
| 139 |
S('o') = E(LONG), S('u') = E(LONG), S('x') = E(LONG), S('X') = E(LONG), |
| 140 |
S('e') = E(DBL), S('f') = E(DBL), S('g') = E(DBL), S('a') = E(DBL), |
| 141 |
S('E') = E(DBL), S('F') = E(DBL), S('G') = E(DBL), S('A') = E(DBL), |
| 142 |
S('c') = E(CHAR), S('s') = E(STR), |
| 143 |
S('n') = E(NPTR), |
| 144 |
S('l') = LLPRE, |
| 145 |
}, |
| 146 |
{ /* 2: ll-prefixed */ |
| 147 |
S('d') = E(LLONG), S('i') = E(LLONG), |
| 148 |
S('o') = E(LLONG), S('u') = E(LLONG), S('x') = E(LLONG), S('X') = E(LLONG), |
| 149 |
S('n') = E(NPTR), |
| 150 |
}, |
| 151 |
{ /* 3: h-prefixed */ |
| 152 |
S('d') = E(SHORT), S('i') = E(SHORT), |
| 153 |
S('o') = E(SHORT), S('u') = E(SHORT), S('x') = E(SHORT), S('X') = E(SHORT), |
| 154 |
S('n') = E(NPTR), |
| 155 |
S('h') = HHPRE, |
| 156 |
}, |
| 157 |
{ /* 4: hh-prefixed */ |
| 158 |
S('d') = E(CHAR), S('i') = E(CHAR), |
| 159 |
S('o') = E(CHAR), S('u') = E(CHAR), S('x') = E(CHAR), S('X') = E(CHAR), |
| 160 |
S('n') = E(NPTR), |
| 161 |
}, |
| 162 |
{ /* 5: L-prefixed */ |
| 163 |
S('e') = E(LDBL), S('f') = E(LDBL), S('g') = E(LDBL), S('a') = E(LDBL), |
| 164 |
S('E') = E(LDBL), S('F') = E(LDBL), S('G') = E(LDBL), S('A') = E(LDBL), |
| 165 |
}, |
| 166 |
{ /* 6: z- or t-prefixed (assumed to be same size) */ |
| 167 |
S('d') = E(SIZET), S('i') = E(SIZET), |
| 168 |
S('o') = E(SIZET), S('u') = E(SIZET), S('x') = E(SIZET), S('X') = E(SIZET), |
| 169 |
S('n') = E(NPTR), |
| 170 |
}, |
| 171 |
{ /* 7: j-prefixed */ |
| 172 |
S('d') = E(IMAX), S('i') = E(IMAX), |
| 173 |
S('o') = E(IMAX), S('u') = E(IMAX), S('x') = E(IMAX), S('X') = E(IMAX), |
| 174 |
S('n') = E(NPTR), |
| 175 |
}, |
| 176 |
}; |
| 177 |
|
| 178 |
size_t parse_printf_format(const char *fmt, size_t n, int *types) { |
| 179 |
size_t i = 0; |
| 180 |
size_t last = 0; |
| 181 |
|
| 182 |
memset(types, 0, n); |
| 183 |
|
| 184 |
for (;;) { |
| 185 |
size_t arg; |
| 186 |
|
| 187 |
fmt = consume_nonarg(fmt); |
| 188 |
if (*fmt == '\0') |
| 189 |
break; |
| 190 |
if (*fmt == '%') { |
| 191 |
fmt++; |
| 192 |
continue; |
| 193 |
} |
| 194 |
arg = 0; |
| 195 |
fmt = consume_argn(fmt, &arg); |
| 196 |
/* flags */ |
| 197 |
fmt = consume_flags(fmt); |
| 198 |
/* width */ |
| 199 |
if (*fmt == '*') { |
| 200 |
size_t warg = 0; |
| 201 |
fmt = consume_argn(fmt+1, &warg); |
| 202 |
if (warg == 0) |
| 203 |
warg = ++i; |
| 204 |
if (warg > last) |
| 205 |
last = warg; |
| 206 |
if (warg <= n && types[warg-1] == NONE) |
| 207 |
types[warg-1] = INT; |
| 208 |
} else |
| 209 |
fmt = consume_num(fmt); |
| 210 |
/* precision */ |
| 211 |
if (*fmt == '.') { |
| 212 |
fmt++; |
| 213 |
if (*fmt == '*') { |
| 214 |
size_t parg = 0; |
| 215 |
fmt = consume_argn(fmt+1, &parg); |
| 216 |
if (parg == 0) |
| 217 |
parg = ++i; |
| 218 |
if (parg > last) |
| 219 |
last = parg; |
| 220 |
if (parg <= n && types[parg-1] == NONE) |
| 221 |
types[parg-1] = INT; |
| 222 |
} else { |
| 223 |
if (*fmt == '-') |
| 224 |
fmt++; |
| 225 |
fmt = consume_num(fmt); |
| 226 |
} |
| 227 |
} |
| 228 |
/* length modifier and conversion specifier */ |
| 229 |
unsigned state = BARE; |
| 230 |
for (;;) { |
| 231 |
unsigned char c = *fmt; |
| 232 |
|
| 233 |
if (c == '\0') |
| 234 |
break; |
| 235 |
|
| 236 |
fmt++; |
| 237 |
|
| 238 |
if (c < 'A' || c > 'z') |
| 239 |
break; |
| 240 |
|
| 241 |
state = states[state]S(c); |
| 242 |
if (state == 0 || state >= STOP) |
| 243 |
break; |
| 244 |
} |
| 245 |
|
| 246 |
if (state <= STOP) /* %m or invalid format */ |
| 247 |
continue; |
| 248 |
|
| 249 |
if (arg == 0) |
| 250 |
arg = ++i; |
| 251 |
if (arg > last) |
| 252 |
last = arg; |
| 253 |
if (arg <= n) |
| 254 |
types[arg-1] = state - STOP; |
| 255 |
} |
| 256 |
|
| 257 |
if (last > n) |
| 258 |
last = n; |
| 259 |
for (i = 0; i < last; i++) |
| 260 |
types[i] = pa_types[types[i]]; |
| 261 |
|
| 262 |
return last; |
| 263 |
} |