| 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"cxx . . $ `<file>/src/common/setup_before.h`/ |
| 28 | #include "asnprintf.h"cxx . . $ `<file>/src/common/asnprintf.h`/ |
| 29 | |
| 30 | #include <cctype> |
| 31 | #include <cstdint> |
| 32 | #include <cstring> |
| 33 | |
| 34 | #include "common/setup_after.h"cxx . . $ `<file>/src/common/setup_after.h`/ |
| 35 | |
| 36 | namespace pvpgncxx . . $ pvpgn/ |
| 37 | { |
| 38 | |
| 39 | static int skip_atoicxx . . $ pvpgn/skip_atoi(15355eb6db2855df).(const char **s) |
| 40 | { |
| 41 | int i = 0; |
| 42 | |
| 43 | while (std::isdigitcxx . . $ std/isdigit(a1851578aea3f541).(**s)) |
| 44 | i = i * 10 + *((*s)++) - '0'; |
| 45 | return i; |
| 46 | } |
| 47 | |
| 48 | std::uint32_tcxx . . $ std/uint32_t# do_divcxx . . $ pvpgn/do_div(3dc79194c10de1e8).(std::uint64_tcxx . . $ std/uint64_t# *n, std::uint32_tcxx . . $ std/uint32_t# base) |
| 49 | { |
| 50 | std::uint32_tcxx . . $ std/uint32_t# remainder = *n % base; |
| 51 | *n = *n / base; |
| 52 | return remainder; |
| 53 | } |
| 54 | |
| 55 | static char * numbercxx . . $ pvpgn/number(2db111e567b7c443).(char * buf, char * end, std::uint64_tcxx . . $ 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 & LARGEcxx . . $ `src/common/asnprintf.h:31:9`!) ? large_digits : small_digits; |
| 64 | if (type & LEFTcxx . . $ `src/common/asnprintf.h:29:9`!) |
| 65 | type &= ~ZEROPADcxx . . $ `src/common/asnprintf.h:25:9`!; |
| 66 | if (base < 2 || base > 36) |
| 67 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 68 | c = (type & ZEROPADcxx . . $ `src/common/asnprintf.h:25:9`!) ? '0' : ' '; |
| 69 | sign = 0; |
| 70 | if (type & SIGNcxx . . $ `src/common/asnprintf.h:26:9`!) { |
| 71 | if ((std::int64_tcxx . . $ std/int64_t#)num < 0) { |
| 72 | sign = '-'; |
| 73 | num = -(std::int64_tcxx . . $ std/int64_t#)num; |
| 74 | size--; |
| 75 | } |
| 76 | else if (type & PLUScxx . . $ `src/common/asnprintf.h:27:9`!) { |
| 77 | sign = '+'; |
| 78 | size--; |
| 79 | } |
| 80 | else if (type & SPACEcxx . . $ `src/common/asnprintf.h:28:9`!) { |
| 81 | sign = ' '; |
| 82 | size--; |
| 83 | } |
| 84 | } |
| 85 | if (type & SPECIALcxx . . $ `src/common/asnprintf.h:30:9`!) { |
| 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_divcxx . . $ pvpgn/do_div(3dc79194c10de1e8).(&num, base)]; |
| 96 | if (i > precision) |
| 97 | precision = i; |
| 98 | size -= precision; |
| 99 | if (!(type&(ZEROPADcxx . . $ `src/common/asnprintf.h:25:9`! + LEFTcxx . . $ `src/common/asnprintf.h:29:9`!))) { |
| 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 & SPECIALcxx . . $ `src/common/asnprintf.h:30:9`!) { |
| 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 & LEFTcxx . . $ `src/common/asnprintf.h:29:9`!)) { |
| 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 vasnprintfcxx . . $ pvpgn/vasnprintf(317e05b89f3a90fd).(char *buf, std::size_tcxx . . $ std/size_t# size, t_fmtentrycxx . . $ pvpgn/t_fmtentry# *entries, unsigned entlen, const char *fmt, std::va_listcxx . . $ std/va_list# args) |
| 152 | { |
| 153 | int len; |
| 154 | std::uint64_tcxx . . $ 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_fmtentrycxx . . $ pvpgn/t_fmtentry#* const fmtent = entries + fmtno; |
| 184 | |
| 185 | if (*fmt != '%') |
| 186 | continue; |
| 187 | |
| 188 | if (fmtno >= entlen) return -1; |
| 189 | fmtent->transcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#trans. = str; |
| 190 | fmtent->fmtcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#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 |= LEFTcxx . . $ `src/common/asnprintf.h:29:9`!; goto repeat; |
| 199 | case '+': flags |= PLUScxx . . $ `src/common/asnprintf.h:27:9`!; goto repeat; |
| 200 | case ' ': flags |= SPACEcxx . . $ `src/common/asnprintf.h:28:9`!; goto repeat; |
| 201 | case '#': flags |= SPECIALcxx . . $ `src/common/asnprintf.h:30:9`!; goto repeat; |
| 202 | case '0': flags |= ZEROPADcxx . . $ `src/common/asnprintf.h:25:9`!; goto repeat; |
| 203 | } |
| 204 | |
| 205 | /* get field width */ |
| 206 | field_width = -1; |
| 207 | if (std::isdigitcxx . . $ std/isdigit(a1851578aea3f541).(*fmt)) |
| 208 | field_width = skip_atoicxx . . $ pvpgn/skip_atoi(15355eb6db2855df).(&fmt); |
| 209 | else if (*fmt == '*') { |
| 210 | ++fmt; |
| 211 | /* it's the next argument */ |
| 212 | field_width = va_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(args, int); |
| 213 | if (field_width < 0) { |
| 214 | field_width = -field_width; |
| 215 | flags |= LEFTcxx . . $ `src/common/asnprintf.h:29:9`!; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* get the precision */ |
| 220 | precision = -1; |
| 221 | if (*fmt == '.') { |
| 222 | ++fmt; |
| 223 | if (std::isdigitcxx . . $ std/isdigit(a1851578aea3f541).(*fmt)) |
| 224 | precision = skip_atoicxx . . $ pvpgn/skip_atoi(15355eb6db2855df).(&fmt); |
| 225 | else if (*fmt == '*') { |
| 226 | ++fmt; |
| 227 | /* it's the next argument */ |
| 228 | precision = va_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(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 & LEFTcxx . . $ `src/common/asnprintf.h:29:9`!)) { |
| 252 | while (--field_width > 0) { |
| 253 | if (str > end) |
| 254 | return -1; |
| 255 | *str = ' '; |
| 256 | ++str; |
| 257 | } |
| 258 | } |
| 259 | c = (unsigned char)va_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(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->fmtlencxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#fmtlen. = fmt - fmtent->fmtcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#fmt. + 1; |
| 271 | fmtent->translencxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#translen. = str - fmtent->transcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#trans.; |
| 272 | continue; |
| 273 | |
| 274 | case 's': |
| 275 | s = va_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(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 & LEFTcxx . . $ `src/common/asnprintf.h:29:9`!)) { |
| 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->fmtlencxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#fmtlen. = fmt - fmtent->fmtcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#fmt. + 1; |
| 304 | fmtent->translencxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#translen. = str - fmtent->transcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#trans.; |
| 305 | continue; |
| 306 | |
| 307 | case 'p': |
| 308 | if (field_width == -1) { |
| 309 | field_width = 2 * sizeof(void *); |
| 310 | flags |= ZEROPADcxx . . $ `src/common/asnprintf.h:25:9`!; |
| 311 | } |
| 312 | str = numbercxx . . $ pvpgn/number(2db111e567b7c443).(str, end, |
| 313 | (unsigned long)va_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(args, void *), |
| 314 | 16, field_width, precision, flags); |
| 315 | if (str > end) return -1; |
| 316 | fmtent->fmtlencxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#fmtlen. = fmt - fmtent->fmtcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#fmt. + 1; |
| 317 | fmtent->translencxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#translen. = str - fmtent->transcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#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_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(args, long *); |
| 326 | *ip = (str - buf); |
| 327 | } |
| 328 | else if (qualifier == 'Z' || qualifier == 'z') { |
| 329 | std::size_tcxx . . $ std/size_t# * ip = va_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(args, std::size_t *); |
| 330 | *ip = (str - buf); |
| 331 | } |
| 332 | else { |
| 333 | int * ip = va_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(args, int *); |
| 334 | *ip = (str - buf); |
| 335 | } |
| 336 | fmtent->fmtlencxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#fmtlen. = fmt - fmtent->fmtcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#fmt. + 1; |
| 337 | fmtent->translencxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#translen. = str - fmtent->transcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#trans.; |
| 338 | continue; |
| 339 | |
| 340 | case '%': |
| 341 | if (str > end) |
| 342 | return -1; |
| 343 | *str = '%'; |
| 344 | ++str; |
| 345 | fmtent->fmtlencxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#fmtlen. = fmt - fmtent->fmtcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#fmt. + 1; |
| 346 | fmtent->translencxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#translen. = str - fmtent->transcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#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 |= LARGEcxx . . $ `src/common/asnprintf.h:31:9`!; |
| 356 | case 'x': |
| 357 | base = 16; |
| 358 | break; |
| 359 | |
| 360 | case 'd': |
| 361 | case 'i': |
| 362 | flags |= SIGNcxx . . $ `src/common/asnprintf.h:26:9`!; |
| 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->fmtlencxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#fmtlen. = fmt - fmtent->fmtcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#fmt. + 1; |
| 381 | fmtent->translencxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#translen. = str - fmtent->transcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#trans.; |
| 382 | continue; |
| 383 | } |
| 384 | /* |
| 385 | if (qualifier == 'L') |
| 386 | num = va_arg(args, long long); |
| 387 | else*/ if (qualifier == 'l') { |
| 388 | num = va_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(args, unsigned long); |
| 389 | if (flags & SIGNcxx . . $ `src/common/asnprintf.h:26:9`!) |
| 390 | num = (signed long)num; |
| 391 | } |
| 392 | else if (qualifier == 'Z' || qualifier == 'z') { |
| 393 | num = va_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(args, std::size_t); |
| 394 | } |
| 395 | else if (qualifier == 'h') { |
| 396 | num = (unsigned short)va_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(args, int); |
| 397 | if (flags & SIGNcxx . . $ `src/common/asnprintf.h:26:9`!) |
| 398 | num = (signed short)num; |
| 399 | } |
| 400 | else { |
| 401 | num = va_argcxx . . $ `<external>/8105065126723003039/stdarg.h:53:9`!(args, unsigned int); |
| 402 | if (flags & SIGNcxx . . $ `src/common/asnprintf.h:26:9`!) |
| 403 | num = (signed int)num; |
| 404 | } |
| 405 | str = numbercxx . . $ pvpgn/number(2db111e567b7c443).(str, end, num, base, |
| 406 | field_width, precision, flags); |
| 407 | if (str > end) return -1; |
| 408 | fmtent->fmtlencxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#fmtlen. = fmt - fmtent->fmtcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#fmt. + 1; |
| 409 | fmtent->translencxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#translen. = str - fmtent->transcxx . . $ pvpgn/$anonymous_type_bd85e8b1635b9208_0#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 asnprintfcxx . . $ pvpgn/asnprintf(e811632e67542722).(char * buf, std::size_tcxx . . $ std/size_t# size, t_fmtentrycxx . . $ pvpgn/t_fmtentry# *entries, unsigned entlen, const char *fmt, ...) |
| 420 | { |
| 421 | std::va_listcxx . . $ std/va_list# args; |
| 422 | int i; |
| 423 | |
| 424 | va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(args, fmt); |
| 425 | i = vasnprintfcxx . . $ pvpgn/vasnprintf(317e05b89f3a90fd).(buf, size, entries, entlen, fmt, args); |
| 426 | va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(args); |
| 427 | return i; |
| 428 | } |
| 429 | |
| 430 | } |