| 1 |
/* |
| 2 |
Copyright (C) 2005 Silicon Graphics, Inc. All Rights Reserved. |
| 3 |
Portions Copyright (C) 2013-2019 David Anderson. All Rights Reserved. |
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of version 2 of the GNU General |
| 6 |
Public License as published by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it would be |
| 9 |
useful, but WITHOUT ANY WARRANTY; without even the implied |
| 10 |
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. |
| 12 |
|
| 13 |
Further, this software is distributed without any warranty |
| 14 |
that it is free of the rightful claim of any third person |
| 15 |
regarding infringement or the like. Any license provided |
| 16 |
herein, whether implied or otherwise, applies only to this |
| 17 |
software file. Patent licenses, if any, provided herein |
| 18 |
do not apply to combinations of this program with other |
| 19 |
software, or any other product whatsoever. |
| 20 |
|
| 21 |
You should have received a copy of the GNU General Public |
| 22 |
License along with this program; if not, write the Free |
| 23 |
Software Foundation, Inc., 51 Franklin Street - Fifth Floor, |
| 24 |
Boston MA 02110-1301, USA. |
| 25 |
*/ |
| 26 |
|
| 27 |
/* esb.c |
| 28 |
extensible string buffer. |
| 29 |
|
| 30 |
A simple means (vaguely like a C++ class) that |
| 31 |
enables safely saving strings of arbitrary length built up |
| 32 |
in small pieces. |
| 33 |
|
| 34 |
We really do allow only C strings here. NUL bytes |
| 35 |
in a string result in adding only up to the NUL. |
| 36 |
Errors get an ERROR line printed. |
| 37 |
|
| 38 |
The functions assume that |
| 39 |
pointer arguments of all kinds are not NULL. |
| 40 |
*/ |
| 41 |
|
| 42 |
#include <config.h> |
| 43 |
#include <stdarg.h> /* va_end() va_start() */ |
| 44 |
#include <stdlib.h> /* exit() free() malloc() realloc() */ |
| 45 |
#include <string.h> /* memcpy() memset() strlen() */ |
| 46 |
#include <stdio.h> /* FILE decl for dd_esb.h */ |
| 47 |
#include "dd_esb.h" |
| 48 |
#include "libdwarf_private.h" /* For malloc/calloc debug */ |
| 49 |
#include "dd_minimal.h" /* For dwarfdump error count */ |
| 50 |
|
| 51 |
/* INITIAL_ALLOC value takes no account of space for a trailing NUL, |
| 52 |
the NUL is accounted for in init_esb_string |
| 53 |
and in later tests against esb_allocated_size. */ |
| 54 |
#ifdef TESTING |
| 55 |
#define INITIAL_ALLOC 1 /* TESTING */ |
| 56 |
#define MALLOC_COUNT 1 |
| 57 |
#else |
| 58 |
/* There is nothing magic about this size. |
| 59 |
It is just big enough to avoid most resizing. */ |
| 60 |
#define INITIAL_ALLOC 100 |
| 61 |
#endif |
| 62 |
/* Allow for final NUL */ |
| 63 |
static size_t alloc_size = INITIAL_ALLOC; |
| 64 |
|
| 65 |
/* NULL device used when printing formatted strings */ |
| 66 |
static FILE *null_device_handle = 0; |
| 67 |
#ifdef _WIN32 |
| 68 |
#define NULL_DEVICE_NAME "NUL" |
| 69 |
#else |
| 70 |
#define NULL_DEVICE_NAME "/dev/null" |
| 71 |
#endif /* _WIN32 */ |
| 72 |
|
| 73 |
#ifdef MALLOC_COUNT |
| 74 |
size_t malloc_count = 0; |
| 75 |
size_t malloc_size = 0; |
| 76 |
#endif |
| 77 |
|
| 78 |
/* m must be a string, like "ESBERR..." for this to work */ |
| 79 |
#define ESBERR(m) esb_appendn_internal(data,m,sizeof(m)-1) |
| 80 |
|
| 81 |
#define INCRVALIDATENEXT(n) do { \ |
| 82 |
++n ; \ |
| 83 |
if (!format[n]) { \ |
| 84 |
ESBERR("ESBERR_next_followedby_end"); \ |
| 85 |
return; \ |
| 86 |
} \ |
| 87 |
} while (0) |
| 88 |
FILE *esb_open_null_device(void) |
| 89 |
{ |
| 90 |
if (!null_device_handle) { |
| 91 |
null_device_handle = fopen(NULL_DEVICE_NAME,"w"); |
| 92 |
} |
| 93 |
return null_device_handle; |
| 94 |
} |
| 95 |
|
| 96 |
/* Close the null device used during formatting printing */ |
| 97 |
void esb_close_null_device(void) |
| 98 |
{ |
| 99 |
if (null_device_handle) { |
| 100 |
fclose(null_device_handle); |
| 101 |
null_device_handle = 0; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/* min_len is overall space wanted for initial alloc. |
| 106 |
ASSERT: esb_allocated_size == 0 |
| 107 |
Return FALSE if unable to init. Else return TRUE */ |
| 108 |
static int |
| 109 |
init_esb_string(struct esb_s *data, size_t min_len) |
| 110 |
{ |
| 111 |
char* d; |
| 112 |
static int notederror = FALSE; |
| 113 |
|
| 114 |
if (data->esb_allocated_size > 0) { |
| 115 |
return TRUE; |
| 116 |
} |
| 117 |
/* Only esb_constructor applied so far. |
| 118 |
Now Allow for string space. */ |
| 119 |
if (min_len < alloc_size) { |
| 120 |
min_len = alloc_size; |
| 121 |
} else { |
| 122 |
min_len++ ; /* Allow for NUL at end */ |
| 123 |
} |
| 124 |
d = (char *)malloc(min_len); |
| 125 |
#ifdef MALLOC_COUNT |
| 126 |
++malloc_count; |
| 127 |
malloc_size += min_len; |
| 128 |
#endif |
| 129 |
data->esb_used_bytes = 0; |
| 130 |
if (!d) { |
| 131 |
if (!notederror) { |
| 132 |
printf("ERROR: dwarfdump is out of memory" |
| 133 |
" allocating" |
| 134 |
" %lu esb string bytes. Unable to set up esb_string" |
| 135 |
"strings will be incomplete.\n", |
| 136 |
(unsigned long) min_len); |
| 137 |
notederror = TRUE; |
| 138 |
dd_minimal_count_global_error(); |
| 139 |
} |
| 140 |
data->esb_allocated_size = 0; |
| 141 |
data->esb_string = 0; |
| 142 |
return FALSE; |
| 143 |
} |
| 144 |
data->esb_string = d; |
| 145 |
data->esb_string[0] = 0; |
| 146 |
data->esb_allocated_size = min_len; |
| 147 |
return TRUE; |
| 148 |
} |
| 149 |
|
| 150 |
/* Make more room. Leaving contents unchanged, effectively. |
| 151 |
The NUL byte at end has room and this preserves that room. |
| 152 |
Return TRUE if allocated, else return FALSE |
| 153 |
*/ |
| 154 |
static int |
| 155 |
esb_allocate_more(struct esb_s *data, size_t len) |
| 156 |
{ |
| 157 |
size_t new_size = 0; |
| 158 |
char* newd = 0; |
| 159 |
int ires = FALSE; |
| 160 |
int notederror = FALSE; |
| 161 |
|
| 162 |
if (data->esb_rigid) { |
| 163 |
return TRUE; |
| 164 |
} |
| 165 |
if (data->esb_allocated_size == 0) { |
| 166 |
ires = init_esb_string(data, alloc_size); |
| 167 |
if (ires == FALSE) { |
| 168 |
return FALSE; |
| 169 |
} |
| 170 |
} |
| 171 |
new_size = data->esb_allocated_size + len; |
| 172 |
if (new_size < alloc_size) { |
| 173 |
new_size = alloc_size; |
| 174 |
} |
| 175 |
if (data->esb_fixed) { |
| 176 |
newd = malloc(new_size); |
| 177 |
#ifdef MALLOC_COUNT |
| 178 |
++malloc_count; |
| 179 |
malloc_size += len; |
| 180 |
#endif |
| 181 |
if (newd) { |
| 182 |
memcpy(newd,data->esb_string,data->esb_used_bytes+1); |
| 183 |
} else { |
| 184 |
return FALSE; |
| 185 |
} |
| 186 |
} else { |
| 187 |
newd = realloc(data->esb_string, new_size); |
| 188 |
#ifdef MALLOC_COUNT |
| 189 |
++malloc_count; |
| 190 |
malloc_size += len; |
| 191 |
#endif |
| 192 |
} |
| 193 |
if (!newd) { |
| 194 |
if ( !notederror) { |
| 195 |
printf("ERROR: dwarfdump is out of memory " |
| 196 |
"allocating " |
| 197 |
"%lu esb string bytes, " |
| 198 |
"strings will print incorrectly\n", |
| 199 |
(unsigned long)new_size); |
| 200 |
notederror=TRUE; |
| 201 |
dd_minimal_count_global_error(); |
| 202 |
} |
| 203 |
return FALSE; |
| 204 |
} |
| 205 |
/* If the area was reallocated by realloc() the earlier |
| 206 |
space was free()d by realloc(). */ |
| 207 |
data->esb_string = newd; |
| 208 |
data->esb_allocated_size = new_size; |
| 209 |
data->esb_fixed = 0; |
| 210 |
return TRUE; |
| 211 |
} |
| 212 |
|
| 213 |
/* Ensure that the total buffer length is large enough that |
| 214 |
at least minlen bytes are available, unused, |
| 215 |
in the allocation. */ |
| 216 |
void |
| 217 |
esb_force_allocation(struct esb_s *data, size_t minlen) |
| 218 |
{ |
| 219 |
size_t target_len = 0; |
| 220 |
|
| 221 |
if (data->esb_rigid) { |
| 222 |
return; |
| 223 |
} |
| 224 |
if (data->esb_allocated_size == 0) { |
| 225 |
int ires = init_esb_string(data, alloc_size); |
| 226 |
if (ires == FALSE) { |
| 227 |
return; |
| 228 |
} |
| 229 |
} |
| 230 |
target_len = data->esb_used_bytes + minlen; |
| 231 |
if (data->esb_allocated_size < target_len) { |
| 232 |
size_t needed = target_len - data->esb_allocated_size; |
| 233 |
esb_allocate_more(data,needed); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/* The 'len' is believed. Do not pass in strings < len bytes long. |
| 238 |
For strlen(in_string) > len bytes we take the initial len bytes. |
| 239 |
len does not include the trailing NUL. */ |
| 240 |
static void |
| 241 |
esb_appendn_internal(struct esb_s *data, |
| 242 |
const char * in_string, size_t len) |
| 243 |
{ |
| 244 |
size_t remaining = 0; |
| 245 |
size_t needed = len; |
| 246 |
|
| 247 |
if (data->esb_allocated_size == 0) { |
| 248 |
size_t maxlen = (len >= alloc_size)? len:alloc_size; |
| 249 |
int ires = init_esb_string(data, maxlen); |
| 250 |
|
| 251 |
if (ires == FALSE) { |
| 252 |
return; |
| 253 |
} |
| 254 |
} |
| 255 |
/* ASSERT: data->esb_allocated_size > data->esb_used_bytes */ |
| 256 |
remaining = data->esb_allocated_size - data->esb_used_bytes - 1; |
| 257 |
if (remaining <= needed) { |
| 258 |
if (data->esb_rigid && len > remaining) { |
| 259 |
len = remaining; |
| 260 |
} else { |
| 261 |
size_t alloc_amt = needed - remaining; |
| 262 |
int ires = esb_allocate_more(data,alloc_amt); |
| 263 |
|
| 264 |
if (ires == FALSE) { |
| 265 |
return; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
if (len == 0) { |
| 270 |
/* No room for anything more, or no more requested. */ |
| 271 |
return; |
| 272 |
} |
| 273 |
/* Might be that len < string len, so do not assume len+1 byte |
| 274 |
is a NUL byte. */ |
| 275 |
memcpy(&data->esb_string[data->esb_used_bytes],in_string,len); |
| 276 |
data->esb_used_bytes += len; |
| 277 |
/* Insist on explicit NUL terminator */ |
| 278 |
data->esb_string[data->esb_used_bytes] = 0; |
| 279 |
} |
| 280 |
|
| 281 |
/* len >= strlen(in_string) */ |
| 282 |
void |
| 283 |
esb_appendn(struct esb_s *data, |
| 284 |
const char * in_string, size_t len) |
| 285 |
{ |
| 286 |
size_t full_len = strlen(in_string); |
| 287 |
|
| 288 |
if (full_len < len) { |
| 289 |
ESBERR("ESBERR_appendn bad call"); |
| 290 |
return; |
| 291 |
} |
| 292 |
esb_appendn_internal(data, in_string, len); |
| 293 |
} |
| 294 |
|
| 295 |
/* The length is gotten from the in_string itself, this |
| 296 |
is the usual way to add string data.. */ |
| 297 |
void |
| 298 |
esb_append(struct esb_s *data, const char * in_string) |
| 299 |
{ |
| 300 |
size_t lenszt = 0; |
| 301 |
if (in_string) { |
| 302 |
lenszt = strlen(in_string); |
| 303 |
if (lenszt) { |
| 304 |
esb_appendn_internal(data, in_string, lenszt); |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
/* Always returns an empty string or a non-empty string. Never 0. */ |
| 310 |
|
| 311 |
char* |
| 312 |
esb_get_string(struct esb_s *data) |
| 313 |
{ |
| 314 |
if (data->esb_allocated_size == 0) { |
| 315 |
int ires = init_esb_string(data, alloc_size); |
| 316 |
if (ires == FALSE) { |
| 317 |
dd_minimal_count_global_error(); |
| 318 |
return "<ERROR ESBERROR out of memory >"; |
| 319 |
} |
| 320 |
} |
| 321 |
return data->esb_string; |
| 322 |
} |
| 323 |
|
| 324 |
/* Sets esb_used_bytes to zero. The string is not freed and |
| 325 |
esb_allocated_size is unchanged. */ |
| 326 |
void |
| 327 |
esb_empty_string(struct esb_s *data) |
| 328 |
{ |
| 329 |
if (data->esb_allocated_size == 0) { |
| 330 |
int ires = init_esb_string(data, alloc_size); |
| 331 |
if (ires == FALSE) { |
| 332 |
return; |
| 333 |
} |
| 334 |
} |
| 335 |
data->esb_used_bytes = 0; |
| 336 |
data->esb_string[0] = 0; |
| 337 |
} |
| 338 |
|
| 339 |
/* Return esb_used_bytes. */ |
| 340 |
size_t |
| 341 |
esb_string_len(struct esb_s *data) |
| 342 |
{ |
| 343 |
return data->esb_used_bytes; |
| 344 |
} |
| 345 |
|
| 346 |
/* *data is presumed to contain garbage, not values, and |
| 347 |
is properly initialized here. */ |
| 348 |
void |
| 349 |
esb_constructor(struct esb_s *data) |
| 350 |
{ |
| 351 |
memset(data, 0, sizeof(*data)); |
| 352 |
} |
| 353 |
|
| 354 |
/* ASSERT: buflen > 0 */ |
| 355 |
void |
| 356 |
esb_constructor_fixed(struct esb_s *data,char *buf,size_t buflen) |
| 357 |
{ |
| 358 |
memset(data, 0, sizeof(*data)); |
| 359 |
if (buflen < 1) { |
| 360 |
return; |
| 361 |
} |
| 362 |
data->esb_string = buf; |
| 363 |
data->esb_string[0] = 0; |
| 364 |
data->esb_allocated_size = buflen; |
| 365 |
data->esb_used_bytes = 0; |
| 366 |
data->esb_rigid = 0; |
| 367 |
data->esb_fixed = 1; |
| 368 |
} |
| 369 |
|
| 370 |
/* The string is freed, contents of *data set to zeros. */ |
| 371 |
void |
| 372 |
esb_destructor(struct esb_s *data) |
| 373 |
{ |
| 374 |
if (data->esb_fixed) { |
| 375 |
data->esb_allocated_size = 0; |
| 376 |
data->esb_used_bytes = 0; |
| 377 |
data->esb_string = 0; |
| 378 |
data->esb_rigid = 0; |
| 379 |
data->esb_fixed = 0; |
| 380 |
return; |
| 381 |
} |
| 382 |
if (data->esb_string) { |
| 383 |
free(data->esb_string); |
| 384 |
data->esb_string = 0; |
| 385 |
} |
| 386 |
esb_constructor(data); |
| 387 |
} |
| 388 |
|
| 389 |
/* To get all paths in the code tested, this sets the |
| 390 |
initial allocation/reallocation file-static |
| 391 |
which can be quite small but must not be zero |
| 392 |
The alloc_size variable is used for initializations. */ |
| 393 |
void |
| 394 |
esb_alloc_size(size_t size) |
| 395 |
{ |
| 396 |
if (size < 1) { |
| 397 |
size = 1; |
| 398 |
} |
| 399 |
alloc_size = size; |
| 400 |
} |
| 401 |
|
| 402 |
size_t |
| 403 |
esb_get_allocated_size(struct esb_s *data) |
| 404 |
{ |
| 405 |
return data->esb_allocated_size; |
| 406 |
} |
| 407 |
|
| 408 |
static void |
| 409 |
esb_appendn_internal_spaces(struct esb_s *data,size_t l) |
| 410 |
{ |
| 411 |
static char spacebuf[] = |
| 412 |
{" "}; |
| 413 |
size_t charct = sizeof(spacebuf)-1; |
| 414 |
while (l > charct) { |
| 415 |
esb_appendn_internal(data,spacebuf,charct); |
| 416 |
l -= charct; |
| 417 |
} |
| 418 |
/* ASSERT: l > 0 */ |
| 419 |
esb_appendn_internal(data,spacebuf,l); |
| 420 |
} |
| 421 |
|
| 422 |
static void |
| 423 |
esb_appendn_internal_zeros(struct esb_s *data,size_t l) |
| 424 |
{ |
| 425 |
static char zeros[] = |
| 426 |
{"0000000000000000000000000000000000000000"}; |
| 427 |
size_t charct = sizeof(zeros)-1; |
| 428 |
while (l > charct) { |
| 429 |
esb_appendn_internal(data,zeros,charct); |
| 430 |
l -= charct; |
| 431 |
} |
| 432 |
/* ASSERT: l > 0 */ |
| 433 |
esb_appendn_internal(data,zeros,l); |
| 434 |
} |
| 435 |
|
| 436 |
void |
| 437 |
esb_append_printf_s(struct esb_s *data, |
| 438 |
const char *format,const char *s) |
| 439 |
{ |
| 440 |
size_t stringlenszt = strlen(s); |
| 441 |
size_t next = 0; |
| 442 |
long val = 0; |
| 443 |
char *endptr = 0; |
| 444 |
const char *numptr = 0; |
| 445 |
/* was %[-]fixedlen. Zero means no len provided. */ |
| 446 |
size_t fixedlen = 0; |
| 447 |
/* was %-, nonzero means left-justify */ |
| 448 |
long leftjustify = 0; |
| 449 |
size_t prefixlen = 0; |
| 450 |
|
| 451 |
while (format[next] && format[next] != '%') { |
| 452 |
++next; |
| 453 |
++prefixlen; |
| 454 |
} |
| 455 |
if (prefixlen) { |
| 456 |
esb_appendn_internal(data,format,prefixlen); |
| 457 |
} |
| 458 |
if (format[next] != '%') { |
| 459 |
/* No % operator found */ |
| 460 |
ESBERR("ESBERR..esb_append_printf_s has no percent operator"); |
| 461 |
return; |
| 462 |
} |
| 463 |
next++; |
| 464 |
if (format[next] == ' ') { |
| 465 |
ESBERR("ESBERR_pct_followedby_space_in_s"); |
| 466 |
return; |
| 467 |
} |
| 468 |
if (!format[next]) { |
| 469 |
ESBERR("ESBERR_pct_followedby_wrong_in_s"); |
| 470 |
return; |
| 471 |
} |
| 472 |
if (format[next] == '-') { |
| 473 |
leftjustify++; |
| 474 |
next++; |
| 475 |
} |
| 476 |
numptr = format+next; |
| 477 |
val = strtol(numptr,&endptr,10); |
| 478 |
if ( endptr != numptr) { |
| 479 |
fixedlen = val; |
| 480 |
} |
| 481 |
next = (endptr - format); |
| 482 |
if (format[next] != 's') { |
| 483 |
ESBERR("ESBERR_pct_s_missing_in_s"); |
| 484 |
return; |
| 485 |
} |
| 486 |
next++; |
| 487 |
if (fixedlen && (fixedlen <= stringlenszt)) { |
| 488 |
leftjustify = 0; |
| 489 |
} |
| 490 |
if (leftjustify) { |
| 491 |
esb_appendn_internal(data,s,stringlenszt); |
| 492 |
if (fixedlen) { |
| 493 |
size_t trailingspaces = fixedlen - stringlenszt; |
| 494 |
|
| 495 |
esb_appendn_internal_spaces(data,trailingspaces); |
| 496 |
} |
| 497 |
} else { |
| 498 |
if (fixedlen && (fixedlen < stringlenszt)) { |
| 499 |
/* This lets us have fixedlen < stringlenszt by |
| 500 |
taking all the chars from s ignoring |
| 501 |
the fixedlen*/ |
| 502 |
esb_appendn_internal(data,s,stringlenszt); |
| 503 |
} else { |
| 504 |
if (fixedlen) { |
| 505 |
size_t leadingspaces = fixedlen - stringlenszt; |
| 506 |
size_t k = 0; |
| 507 |
|
| 508 |
for ( ; k < leadingspaces; ++k) { |
| 509 |
esb_appendn_internal(data," ",1); |
| 510 |
} |
| 511 |
} |
| 512 |
esb_appendn_internal(data,s,stringlenszt); |
| 513 |
} |
| 514 |
} |
| 515 |
if (!format[next]) { |
| 516 |
return; |
| 517 |
} |
| 518 |
{ |
| 519 |
const char * startpt = format+next; |
| 520 |
size_t suffixlen = strlen(startpt); |
| 521 |
|
| 522 |
esb_appendn_internal(data,startpt,suffixlen); |
| 523 |
} |
| 524 |
return; |
| 525 |
} |
| 526 |
|
| 527 |
static char dtable[10] = { |
| 528 |
'0','1','2','3','4','5','6','7','8','9' |
| 529 |
}; |
| 530 |
static char xtable[16] = { |
| 531 |
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' |
| 532 |
}; |
| 533 |
static char Xtable[16] = { |
| 534 |
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' |
| 535 |
}; |
| 536 |
|
| 537 |
/* With gcc version 5.4.0 20160609 a version using |
| 538 |
const char *formatp instead of format[next] |
| 539 |
and deleting the 'next' variable |
| 540 |
is a few hundredths of a second slower, repeatably. |
| 541 |
|
| 542 |
We deal with formats like: |
| 543 |
%u %5u %05u (and ld and lld too). |
| 544 |
%x %5x %05x (and ld and lld too). */ |
| 545 |
void |
| 546 |
esb_append_printf_u(struct esb_s *data, |
| 547 |
const char *format,esb_unsigned v) |
| 548 |
{ |
| 549 |
size_t next = 0; |
| 550 |
long val = 0; |
| 551 |
char *endptr = 0; |
| 552 |
const char *numptr = 0; |
| 553 |
size_t fixedlen = 0; |
| 554 |
int leadingzero = 0; |
| 555 |
int lcount = 0; |
| 556 |
int ucount = 0; |
| 557 |
int dcount = 0; |
| 558 |
int xcount = 0; |
| 559 |
int Xcount = 0; |
| 560 |
char *ctable = 0; |
| 561 |
size_t divisor = 0; |
| 562 |
size_t prefixlen = 0; |
| 563 |
|
| 564 |
while (format[next] && format[next] != '%') { |
| 565 |
++next; |
| 566 |
++prefixlen; |
| 567 |
} |
| 568 |
esb_appendn_internal(data,format,prefixlen); |
| 569 |
if (format[next] != '%') { |
| 570 |
/* No % operator found */ |
| 571 |
ESBERR("ESBERR..esb_append_printf_u has no percent operator"); |
| 572 |
return; |
| 573 |
} |
| 574 |
next++; |
| 575 |
if (format[next] == ' ') { |
| 576 |
ESBERR("ESBERR_pct_followedby_space_in_s"); |
| 577 |
return; |
| 578 |
} |
| 579 |
if (!format[next]) { |
| 580 |
ESBERR("ESBERR_pct_followedby_wrong_in_s"); |
| 581 |
return; |
| 582 |
} |
| 583 |
if (format[next] == '-') { |
| 584 |
ESBERR("ESBERR_printf_u - format not supported"); |
| 585 |
INCRVALIDATENEXT(next); |
| 586 |
} |
| 587 |
if (format[next] == '0') { |
| 588 |
leadingzero = 1; |
| 589 |
INCRVALIDATENEXT(next); |
| 590 |
} |
| 591 |
numptr = format+next; |
| 592 |
val = strtol(numptr,&endptr,10); |
| 593 |
if ( endptr != numptr) { |
| 594 |
fixedlen = val; |
| 595 |
} |
| 596 |
next = (endptr - format); |
| 597 |
/* Following is lx lu or u or llx llu , we take |
| 598 |
all this to mean 64 bits, */ |
| 599 |
#ifdef _WIN32 |
| 600 |
if (format[next] == 'I') { |
| 601 |
/*lcount++;*/ |
| 602 |
next++; |
| 603 |
} |
| 604 |
if (format[next] == '6') { |
| 605 |
/*lcount++;*/ |
| 606 |
next++; |
| 607 |
} |
| 608 |
if (format[next] == '4') { |
| 609 |
/*lcount++;*/ |
| 610 |
next++; |
| 611 |
} |
| 612 |
#endif /* _WIN32 */ |
| 613 |
if (format[next] == 'l') { |
| 614 |
lcount++; |
| 615 |
next++; |
| 616 |
} |
| 617 |
if (format[next] == 'l') { |
| 618 |
lcount++; |
| 619 |
next++; |
| 620 |
} |
| 621 |
if (format[next] == 'u') { |
| 622 |
ucount++; |
| 623 |
next++; |
| 624 |
} |
| 625 |
if (format[next] == 'd') { |
| 626 |
dcount++; |
| 627 |
next++; |
| 628 |
} |
| 629 |
if (format[next] == 'x') { |
| 630 |
xcount++; |
| 631 |
next++; |
| 632 |
} |
| 633 |
if (format[next] == 'X') { |
| 634 |
Xcount++; |
| 635 |
next++; |
| 636 |
} |
| 637 |
if (format[next] == 's') { |
| 638 |
ESBERR("ESBERR_pct_scount_in_u"); |
| 639 |
return; |
| 640 |
} |
| 641 |
|
| 642 |
if ( (Xcount +xcount+dcount+ucount) > 1) { |
| 643 |
ESBERR("ESBERR_pct_xcount_etc_u"); |
| 644 |
/* error */ |
| 645 |
return; |
| 646 |
} |
| 647 |
if (lcount > 2) { |
| 648 |
ESBERR("ESBERR_pct_lcount_error_u"); |
| 649 |
/* error */ |
| 650 |
return; |
| 651 |
} |
| 652 |
if (dcount > 0) { |
| 653 |
ESBERR("ESBERR_pct_dcount_error_u"); |
| 654 |
/* error */ |
| 655 |
return; |
| 656 |
} |
| 657 |
if (ucount) { |
| 658 |
divisor = 10; |
| 659 |
ctable = dtable; |
| 660 |
} else { |
| 661 |
divisor = 16; |
| 662 |
if (xcount) { |
| 663 |
ctable = xtable; |
| 664 |
} else { |
| 665 |
ctable = Xtable; |
| 666 |
} |
| 667 |
} |
| 668 |
{ |
| 669 |
char digbuf[36]; |
| 670 |
char *digptr = 0; |
| 671 |
size_t digcharlen = 0; |
| 672 |
esb_unsigned remaining = v; |
| 673 |
|
| 674 |
if (divisor == 16) { |
| 675 |
digptr = digbuf+sizeof(digbuf) -1; |
| 676 |
for ( ;; ) { |
| 677 |
esb_unsigned dig; |
| 678 |
dig = remaining & 0xf; |
| 679 |
remaining = remaining >> 4; |
| 680 |
*digptr = ctable[dig]; |
| 681 |
++digcharlen; |
| 682 |
if (!remaining) { |
| 683 |
break; |
| 684 |
} |
| 685 |
--digptr; |
| 686 |
} |
| 687 |
} else { |
| 688 |
digptr = digbuf+sizeof(digbuf) -1; |
| 689 |
*digptr = 0; |
| 690 |
--digptr; |
| 691 |
for ( ;; ) { |
| 692 |
esb_unsigned dig; |
| 693 |
dig = remaining % divisor; |
| 694 |
remaining /= divisor; |
| 695 |
*digptr = ctable[dig]; |
| 696 |
++digcharlen; |
| 697 |
if (!remaining) { |
| 698 |
break; |
| 699 |
} |
| 700 |
--digptr; |
| 701 |
} |
| 702 |
} |
| 703 |
if (fixedlen <= digcharlen) { |
| 704 |
esb_appendn_internal(data,digptr,digcharlen); |
| 705 |
} else { |
| 706 |
if (!leadingzero) { |
| 707 |
size_t justcount = fixedlen - digcharlen; |
| 708 |
esb_appendn_internal_spaces(data,justcount); |
| 709 |
esb_appendn_internal(data,digptr,digcharlen); |
| 710 |
} else { |
| 711 |
size_t prefixcount = fixedlen - digcharlen; |
| 712 |
esb_appendn_internal_zeros(data,prefixcount); |
| 713 |
esb_appendn_internal(data,digptr,digcharlen); |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
if (format[next]) { |
| 718 |
size_t trailinglen = strlen(format+next); |
| 719 |
esb_appendn_internal(data,format+next,trailinglen); |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
static char v32m[] = {"-2147483648"}; |
| 724 |
static char v64m[] = {"-9223372036854775808"}; |
| 725 |
|
| 726 |
/* We deal with formats like: |
| 727 |
%d %5d %05d %+d %+5d (and ld and lld too). */ |
| 728 |
void |
| 729 |
esb_append_printf_i(struct esb_s *data, |
| 730 |
const char *format,esb_int v) |
| 731 |
{ |
| 732 |
size_t next = 0; |
| 733 |
long val = 0; |
| 734 |
char *endptr = 0; |
| 735 |
const char *numptr = 0; |
| 736 |
size_t fixedlen = 0; |
| 737 |
int leadingzero = 0; |
| 738 |
int pluscount = 0; |
| 739 |
int lcount = 0; |
| 740 |
int ucount = 0; |
| 741 |
int dcount = 0; |
| 742 |
int xcount = 0; |
| 743 |
int Xcount = 0; |
| 744 |
char *ctable = dtable; |
| 745 |
size_t prefixlen = 0; |
| 746 |
int done = 0; |
| 747 |
|
| 748 |
while (format[next] && format[next] != '%') { |
| 749 |
++next; |
| 750 |
++prefixlen; |
| 751 |
} |
| 752 |
esb_appendn_internal(data,format,prefixlen); |
| 753 |
if (format[next] != '%') { |
| 754 |
/* No % operator found */ |
| 755 |
ESBERR("ESBERR..esb_append_printf_i has no percent operator"); |
| 756 |
return; |
| 757 |
} |
| 758 |
next++; |
| 759 |
if (format[next] == ' ') { |
| 760 |
ESBERR("ESBERR_pct_followedby_space_in_s"); |
| 761 |
return; |
| 762 |
} |
| 763 |
if (!format[next]) { |
| 764 |
ESBERR("ESBERR_pct_followedby_wrong_in_s"); |
| 765 |
return; |
| 766 |
} |
| 767 |
if (format[next] == '-') { |
| 768 |
ESBERR("ESBERR_printf_i - format not supported"); |
| 769 |
INCRVALIDATENEXT(next); |
| 770 |
} |
| 771 |
if (format[next] == '+') { |
| 772 |
pluscount++; |
| 773 |
INCRVALIDATENEXT(next); |
| 774 |
} |
| 775 |
if (format[next] == '0') { |
| 776 |
leadingzero = 1; |
| 777 |
INCRVALIDATENEXT(next); |
| 778 |
} |
| 779 |
numptr = format+next; |
| 780 |
val = strtol(numptr,&endptr,10); |
| 781 |
if ( endptr != numptr) { |
| 782 |
fixedlen = val; |
| 783 |
} |
| 784 |
next = (endptr - format); |
| 785 |
/* Following is lx lu or u or llx llu , we take |
| 786 |
all this to mean 64 bits, */ |
| 787 |
#ifdef _WIN32 |
| 788 |
if (format[next] == 'I') { |
| 789 |
/*lcount++;*/ |
| 790 |
next++; |
| 791 |
} |
| 792 |
if (format[next] == '6') { |
| 793 |
/*lcount++;*/ |
| 794 |
next++; |
| 795 |
} |
| 796 |
if (format[next] == '4') { |
| 797 |
/*lcount++;*/ |
| 798 |
next++; |
| 799 |
} |
| 800 |
#endif /* _WIN32 */ |
| 801 |
if (format[next] == 'l') { |
| 802 |
lcount++; |
| 803 |
next++; |
| 804 |
} |
| 805 |
if (format[next] == 'l') { |
| 806 |
lcount++; |
| 807 |
next++; |
| 808 |
} |
| 809 |
if (format[next] == 'u') { |
| 810 |
ucount++; |
| 811 |
next++; |
| 812 |
} |
| 813 |
if (format[next] == 'd') { |
| 814 |
dcount++; |
| 815 |
next++; |
| 816 |
} |
| 817 |
if (format[next] == 'x') { |
| 818 |
xcount++; |
| 819 |
next++; |
| 820 |
} |
| 821 |
if (format[next] == 'X') { |
| 822 |
Xcount++; |
| 823 |
next++; |
| 824 |
} |
| 825 |
if (format[next] == 's') { |
| 826 |
ESBERR("ESBERR_pct_scount_in_i"); |
| 827 |
return; |
| 828 |
} |
| 829 |
if (!dcount || (lcount >2) || |
| 830 |
(Xcount +xcount+dcount+ucount) > 1) { |
| 831 |
/* error */ |
| 832 |
ESBERR("ESBERR_xcount_etc_i"); |
| 833 |
return; |
| 834 |
} |
| 835 |
{ |
| 836 |
char digbuf[36]; |
| 837 |
char *digptr = digbuf+sizeof(digbuf) -1; |
| 838 |
size_t digcharlen = 0; |
| 839 |
esb_int remaining = v; |
| 840 |
int vissigned = 0; |
| 841 |
esb_int divisor = 10; |
| 842 |
|
| 843 |
*digptr = 0; |
| 844 |
--digptr; |
| 845 |
if (v < 0) { |
| 846 |
vissigned = 1; |
| 847 |
/* This test is for twos-complement |
| 848 |
machines and would be better done via |
| 849 |
configure with a compile-time check |
| 850 |
so we do not need a size test at runtime. */ |
| 851 |
if (sizeof(v) == 8) { |
| 852 |
esb_unsigned vm = 0x7fffffffffffffffULL; |
| 853 |
if (vm == (esb_unsigned)~v) { |
| 854 |
memcpy(digbuf,v64m,sizeof(v64m)); |
| 855 |
digcharlen = sizeof(v64m)-1; |
| 856 |
digptr = digbuf; |
| 857 |
done = 1; |
| 858 |
} else { |
| 859 |
remaining = -v; |
| 860 |
} |
| 861 |
} else if (sizeof(v) == 4) { |
| 862 |
esb_unsigned vm = 0x7fffffffL; |
| 863 |
if (vm == (esb_unsigned)~v) { |
| 864 |
memcpy(digbuf,v32m,sizeof(v32m)); |
| 865 |
digcharlen = sizeof(v32m)-1; |
| 866 |
digptr = digbuf; |
| 867 |
done = 1; |
| 868 |
} else { |
| 869 |
remaining = -v; |
| 870 |
} |
| 871 |
}else { |
| 872 |
ESBERR("ESBERR_sizeof_v_i"); |
| 873 |
/* error */ |
| 874 |
return; |
| 875 |
} |
| 876 |
} |
| 877 |
if (!done) { |
| 878 |
for ( ;; ) { |
| 879 |
esb_unsigned dig = 0; |
| 880 |
|
| 881 |
dig = remaining % divisor; |
| 882 |
remaining /= divisor; |
| 883 |
*digptr = ctable[dig]; |
| 884 |
digcharlen++; |
| 885 |
if (!remaining) { |
| 886 |
break; |
| 887 |
} |
| 888 |
--digptr; |
| 889 |
} |
| 890 |
if (vissigned) { |
| 891 |
--digptr; |
| 892 |
digcharlen++; |
| 893 |
*digptr = '-'; |
| 894 |
} else if (pluscount) { |
| 895 |
--digptr; |
| 896 |
digcharlen++; |
| 897 |
*digptr = '+'; |
| 898 |
} |
| 899 |
} |
| 900 |
if (fixedlen > 0) { |
| 901 |
if (fixedlen <= digcharlen) { |
| 902 |
esb_appendn_internal(data,digptr,digcharlen); |
| 903 |
} else { |
| 904 |
size_t prefixcount = fixedlen - digcharlen; |
| 905 |
if (!leadingzero) { |
| 906 |
esb_appendn_internal_spaces(data,prefixcount); |
| 907 |
esb_appendn_internal(data,digptr,digcharlen); |
| 908 |
} else { |
| 909 |
if (*digptr == '-') { |
| 910 |
esb_appendn_internal(data,"-",1); |
| 911 |
esb_appendn_internal_zeros(data,prefixcount); |
| 912 |
digptr++; |
| 913 |
esb_appendn_internal(data, |
| 914 |
digptr,digcharlen-1); |
| 915 |
} else if (*digptr == '+') { |
| 916 |
esb_appendn_internal(data,"+",1); |
| 917 |
esb_appendn_internal_zeros(data,prefixcount); |
| 918 |
digptr++; |
| 919 |
esb_appendn_internal(data, |
| 920 |
digptr,digcharlen-1); |
| 921 |
} else { |
| 922 |
esb_appendn_internal_zeros(data,prefixcount); |
| 923 |
esb_appendn_internal(data,digptr,digcharlen); |
| 924 |
} |
| 925 |
} |
| 926 |
} |
| 927 |
} else { |
| 928 |
esb_appendn_internal(data,digptr,digcharlen); |
| 929 |
} |
| 930 |
} |
| 931 |
if (format[next]) { |
| 932 |
size_t trailinglen = strlen(format+next); |
| 933 |
esb_appendn_internal(data,format+next,trailinglen); |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
/* Get a copy of the internal data buffer. |
| 938 |
It is up to the code calling this |
| 939 |
to free() the string using the |
| 940 |
pointer returned here. |
| 941 |
Returns NULL if copy not possible. */ |
| 942 |
char* |
| 943 |
esb_get_copy(struct esb_s *data) |
| 944 |
{ |
| 945 |
char* copy = NULL; |
| 946 |
|
| 947 |
/* is ok as is if esb_allocated_size is 0 */ |
| 948 |
size_t len = data->esb_used_bytes+1; |
| 949 |
if (len) { |
| 950 |
copy = (char*)malloc(len); |
| 951 |
if (!copy) { |
| 952 |
return NULL; |
| 953 |
} |
| 954 |
#ifdef MALLOC_COUNT |
| 955 |
++malloc_count; |
| 956 |
malloc_size += len+1; |
| 957 |
#endif |
| 958 |
memcpy(copy,data->esb_string,len); |
| 959 |
} |
| 960 |
return copy; |
| 961 |
} |