| 1 | /* |
| 2 | * Copyright (C) 2005 Olaf Freyer ([email protected]) |
| 3 | * |
| 4 | * Based upon lib/vsnprintf.c from the linux kernel sources |
| 5 | * |
| 6 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 7 | * vsprintf.c -- Lars Wirzenius & Linus Torvalds. * |
| 8 | * |
| 9 | * Wirzenius wrote this portably, Torvalds fucked it up :-) |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License |
| 13 | * as published by the Free Software Foundation; either version 2 |
| 14 | * of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | #include "common/setup_before.h" |
| 28 | #include "asnprintf.h" |
| 29 | |
| 30 | #include <cctype> |
| 31 | #include <cstdint> |
| 32 | #include <cstring> |
| 33 | |
| 34 | #include "common/setup_after.h" |
| 35 | |
| 36 | namespace pvpgn |
| 37 | { |
| 38 | |
| 39 | static int skip_atoi(const char **s) |
| 40 | { |
| 41 | int i = 0; |
| 42 | |
| 43 | while (std::isdigit(**s)) |
| 44 | i = i * 10 + *((*s)++) - '0'; |
| 45 | return i; |
| 46 | } |
| 47 | |
| 48 | std::uint32_t do_div(std::uint64_t *n, std::uint32_t base) |
| 49 | { |
| 50 | std::uint32_t remainder = *n % base; |
| 51 | *n = *n / base; |
| 52 | return remainder; |
| 53 | } |
| 54 | |
| 55 | static char * number(char * buf, char * end, std::uint64_t num, int base, int size, int precision, int type) |
| 56 | { |
| 57 | char c, sign, tmp[66]; |
| 58 | const char *digits; |
| 59 | static const char small_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
| 60 | static const char large_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 61 | int i; |
| 62 | |
| 63 | digits = (type & LARGE) ? large_digits : small_digits; |
| 64 | if (type & LEFT) |
| 65 | type &= ~ZEROPAD; |
| 66 | if (base < 2 || base > 36) |
| 67 | return NULL; |
| 68 | c = (type & ZEROPAD) ? '0' : ' '; |
| 69 | sign = 0; |
| 70 | if (type & SIGN) { |
| 71 | if ((std::int64_t)num < 0) { |
| 72 | sign = '-'; |
| 73 | num = -(std::int64_t)num; |
| 74 | size--; |
| 75 | } |
| 76 | else if (type & PLUS) { |
| 77 | sign = '+'; |
| 78 | size--; |
| 79 | } |
| 80 | else if (type & SPACE) { |
| 81 | sign = ' '; |
| 82 | size--; |
| 83 | } |
| 84 | } |
| 85 | if (type & SPECIAL) { |
| 86 | if (base == 16) |
| 87 | size -= 2; |
| 88 | else if (base == 8) |
| 89 | size--; |
| 90 | } |
| 91 | i = 0; |
| 92 | if (num == 0) |
| 93 | tmp[i++] = '0'; |
| 94 | else while (num != 0) |
| 95 | tmp[i++] = digits[do_div(&num, base)]; |
| 96 | if (i > precision) |
| 97 | precision = i; |
| 98 | size -= precision; |
| 99 | if (!(type&(ZEROPAD + LEFT))) { |
| 100 | while (size-- > 0) { |
| 101 | if (buf <= end) |
| 102 | *buf = ' '; |
| 103 | ++buf; |
| 104 | } |
| 105 | } |
| 106 | if (sign) { |
| 107 | if (buf <= end) |
| 108 | *buf = sign; |
| 109 | ++buf; |
| 110 | } |
| 111 | if (type & SPECIAL) { |
| 112 | if (base == 8) { |
| 113 | if (buf <= end) |
| 114 | *buf = '0'; |
| 115 | ++buf; |
| 116 | } |
| 117 | else if (base == 16) { |
| 118 | if (buf <= end) |
| 119 | *buf = '0'; |
| 120 | ++buf; |
| 121 | if (buf <= end) |
| 122 | *buf = digits[33]; |
| 123 | ++buf; |
| 124 | } |
| 125 | } |
| 126 | if (!(type & LEFT)) { |
| 127 | while (size-- > 0) { |
| 128 | if (buf <= end) |
| 129 | *buf = c; |
| 130 | ++buf; |
| 131 | } |
| 132 | } |
| 133 | while (i < precision--) { |
| 134 | if (buf <= end) |
| 135 | *buf = '0'; |
| 136 | ++buf; |
| 137 | } |
| 138 | while (i-- > 0) { |
| 139 | if (buf <= end) |
| 140 | *buf = tmp[i]; |
| 141 | ++buf; |
| 142 | } |
| 143 | while (size-- > 0) { |
| 144 | if (buf <= end) |
| 145 | *buf = ' '; |
| 146 | ++buf; |
| 147 | } |
| 148 | return buf; |
| 149 | } |
| 150 | |
| 151 | int vasnprintf(char *buf, std::size_t size, t_fmtentry *entries, unsigned entlen, const char *fmt, std::va_list args) |
| 152 | { |
| 153 | int len; |
| 154 | std::uint64_t num; |
| 155 | int i, base; |
| 156 | char *str, *end, c; |
| 157 | const char *s; |
| 158 | |
| 159 | int flags; /* flags to number() */ |
| 160 | |
| 161 | int field_width; /* width of output field */ |
| 162 | int precision; /* min. # of digits for integers; max |
| 163 | number of chars for from string */ |
| 164 | int qualifier; /* 'h', 'l', or 'L' for integer fields */ |
| 165 | /* 'z' support added 23/7/1999 S.H. */ |
| 166 | /* 'z' changed to 'Z' --davidm 1/25/99 */ |
| 167 | unsigned fmtno = 0; |
| 168 | |
| 169 | |
| 170 | /* Reject out-of-range values early */ |
| 171 | if ((int)size < 0) |
| 172 | return 0; |
| 173 | |
| 174 | str = buf; |
| 175 | end = buf + size - 1; |
| 176 | /* |
| 177 | if (end < buf - 1) { |
| 178 | end = ((void *) -1); |
| 179 | size = end - buf + 1; |
| 180 | } |
| 181 | */ |
| 182 | for (; *fmt; ++fmt) { |
| 183 | t_fmtentry* const fmtent = entries + fmtno; |
| 184 | |
| 185 | if (*fmt != '%') |
| 186 | continue; |
| 187 | |
| 188 | if (fmtno >= entlen) return -1; |
| 189 | fmtent->trans = str; |
| 190 | fmtent->fmt = fmt; |
| 191 | fmtno++; |
| 192 | |
| 193 | /* process flags */ |
| 194 | flags = 0; |
| 195 | repeat: |
| 196 | ++fmt; /* this also skips first '%' */ |
| 197 | switch (*fmt) { |
| 198 | case '-': flags |= LEFT; goto repeat; |
| 199 | case '+': flags |= PLUS; goto repeat; |
| 200 | case ' ': flags |= SPACE; goto repeat; |
| 201 | case '#': flags |= SPECIAL; goto repeat; |
| 202 | case '0': flags |= ZEROPAD; goto repeat; |
| 203 | } |
| 204 | |
| 205 | /* get field width */ |
| 206 | field_width = -1; |
| 207 | if (std::isdigit(*fmt)) |
| 208 | field_width = skip_atoi(&fmt); |
| 209 | else if (*fmt == '*') { |
| 210 | ++fmt; |
| 211 | /* it's the next argument */ |
| 212 | field_width = va_arg(args, int); |
| 213 | if (field_width < 0) { |
| 214 | field_width = -field_width; |
| 215 | flags |= LEFT; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* get the precision */ |
| 220 | precision = -1; |
| 221 | if (*fmt == '.') { |
| 222 | ++fmt; |
| 223 | if (std::isdigit(*fmt)) |
| 224 | precision = skip_atoi(&fmt); |
| 225 | else if (*fmt == '*') { |
| 226 | ++fmt; |
| 227 | /* it's the next argument */ |
| 228 | precision = va_arg(args, int); |
| 229 | } |
| 230 | if (precision < 0) |
| 231 | precision = 0; |
| 232 | } |
| 233 | |
| 234 | /* get the conversion qualifier */ |
| 235 | qualifier = -1; |
| 236 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || |
| 237 | *fmt == 'Z' || *fmt == 'z') { |
| 238 | qualifier = *fmt; |
| 239 | ++fmt; |
| 240 | if (qualifier == 'l' && *fmt == 'l') { |
| 241 | qualifier = 'L'; |
| 242 | ++fmt; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* default base */ |
| 247 | base = 10; |
| 248 | |
| 249 | switch (*fmt) { |
| 250 | case 'c': |
| 251 | if (!(flags & LEFT)) { |
| 252 | while (--field_width > 0) { |
| 253 | if (str > end) |
| 254 | return -1; |
| 255 | *str = ' '; |
| 256 | ++str; |
| 257 | } |
| 258 | } |
| 259 | c = (unsigned char)va_arg(args, int); |
| 260 | if (str > end) |
| 261 | return -1; |
| 262 | *str = c; |
| 263 | ++str; |
| 264 | while (--field_width > 0) { |
| 265 | if (str > end) |
| 266 | return -1; |
| 267 | *str = ' '; |
| 268 | ++str; |
| 269 | } |
| 270 | fmtent->fmtlen = fmt - fmtent->fmt + 1; |
| 271 | fmtent->translen = str - fmtent->trans; |
| 272 | continue; |
| 273 | |
| 274 | case 's': |
| 275 | s = va_arg(args, char *); |
| 276 | if (!s) |
| 277 | s = "<NULL>"; |
| 278 | |
| 279 | len = std::strlen(s); |
| 280 | if (len > precision) |
| 281 | len = precision; |
| 282 | |
| 283 | if (!(flags & LEFT)) { |
| 284 | while (len < field_width--) { |
| 285 | if (str > end) |
| 286 | return -1; |
| 287 | *str = ' '; |
| 288 | ++str; |
| 289 | } |
| 290 | } |
| 291 | for (i = 0; i < len; ++i) { |
| 292 | if (str > end) |
| 293 | return -1; |
| 294 | *str = *s; |
| 295 | ++str; ++s; |
| 296 | } |
| 297 | while (len < field_width--) { |
| 298 | if (str > end) |
| 299 | return -1; |
| 300 | *str = ' '; |
| 301 | ++str; |
| 302 | } |
| 303 | fmtent->fmtlen = fmt - fmtent->fmt + 1; |
| 304 | fmtent->translen = str - fmtent->trans; |
| 305 | continue; |
| 306 | |
| 307 | case 'p': |
| 308 | if (field_width == -1) { |
| 309 | field_width = 2 * sizeof(void *); |
| 310 | flags |= ZEROPAD; |
| 311 | } |
| 312 | str = number(str, end, |
| 313 | (unsigned long)va_arg(args, void *), |
| 314 | 16, field_width, precision, flags); |
| 315 | if (str > end) return -1; |
| 316 | fmtent->fmtlen = fmt - fmtent->fmt + 1; |
| 317 | fmtent->translen = str - fmtent->trans; |
| 318 | continue; |
| 319 | |
| 320 | |
| 321 | case 'n': |
| 322 | /* FIXME: |
| 323 | * What does C99 say about the overflow case here? */ |
| 324 | if (qualifier == 'l') { |
| 325 | long * ip = va_arg(args, long *); |
| 326 | *ip = (str - buf); |
| 327 | } |
| 328 | else if (qualifier == 'Z' || qualifier == 'z') { |
| 329 | std::size_t * ip = va_arg(args, std::size_t *); |
| 330 | *ip = (str - buf); |
| 331 | } |
| 332 | else { |
| 333 | int * ip = va_arg(args, int *); |
| 334 | *ip = (str - buf); |
| 335 | } |
| 336 | fmtent->fmtlen = fmt - fmtent->fmt + 1; |
| 337 | fmtent->translen = str - fmtent->trans; |
| 338 | continue; |
| 339 | |
| 340 | case '%': |
| 341 | if (str > end) |
| 342 | return -1; |
| 343 | *str = '%'; |
| 344 | ++str; |
| 345 | fmtent->fmtlen = fmt - fmtent->fmt + 1; |
| 346 | fmtent->translen = str - fmtent->trans; |
| 347 | continue; |
| 348 | |
| 349 | /* integer number formats - set up the flags and "break" */ |
| 350 | case 'o': |
| 351 | base = 8; |
| 352 | break; |
| 353 | |
| 354 | case 'X': |
| 355 | flags |= LARGE; |
| 356 | case 'x': |
| 357 | base = 16; |
| 358 | break; |
| 359 | |
| 360 | case 'd': |
| 361 | case 'i': |
| 362 | flags |= SIGN; |
| 363 | case 'u': |
| 364 | break; |
| 365 | |
| 366 | default: |
| 367 | if (str > end) |
| 368 | return -1; |
| 369 | *str = '%'; |
| 370 | ++str; |
| 371 | if (*fmt) { |
| 372 | if (str > end) |
| 373 | return -1; |
| 374 | *str = *fmt; |
| 375 | ++str; |
| 376 | } |
| 377 | else { |
| 378 | --fmt; |
| 379 | } |
| 380 | fmtent->fmtlen = fmt - fmtent->fmt + 1; |
| 381 | fmtent->translen = str - fmtent->trans; |
| 382 | continue; |
| 383 | } |
| 384 | /* |
| 385 | if (qualifier == 'L') |
| 386 | num = va_arg(args, long long); |
| 387 | else*/ if (qualifier == 'l') { |
| 388 | num = va_arg(args, unsigned long); |
| 389 | if (flags & SIGN) |
| 390 | num = (signed long)num; |
| 391 | } |
| 392 | else if (qualifier == 'Z' || qualifier == 'z') { |
| 393 | num = va_arg(args, std::size_t); |
| 394 | } |
| 395 | else if (qualifier == 'h') { |
| 396 | num = (unsigned short)va_arg(args, int); |
| 397 | if (flags & SIGN) |
| 398 | num = (signed short)num; |
| 399 | } |
| 400 | else { |
| 401 | num = va_arg(args, unsigned int); |
| 402 | if (flags & SIGN) |
| 403 | num = (signed int)num; |
| 404 | } |
| 405 | str = number(str, end, num, base, |
| 406 | field_width, precision, flags); |
| 407 | if (str > end) return -1; |
| 408 | fmtent->fmtlen = fmt - fmtent->fmt + 1; |
| 409 | fmtent->translen = str - fmtent->trans; |
| 410 | } |
| 411 | if (str <= end) |
| 412 | *str = '\0'; |
| 413 | /* the trailing null byte doesn't count towards the total |
| 414 | * ++str; |
| 415 | */ |
| 416 | return str - buf; |
| 417 | } |
| 418 | |
| 419 | int asnprintf(char * buf, std::size_t size, t_fmtentry *entries, unsigned entlen, const char *fmt, ...) |
| 420 | { |
| 421 | std::va_list args; |
| 422 | int i; |
| 423 | |
| 424 | va_start(args, fmt); |
| 425 | i = vasnprintf(buf, size, entries, entlen, fmt, args); |
| 426 | va_end(args); |
| 427 | return i; |
| 428 | } |
| 429 | |
| 430 | } |