-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
append.c
584 lines (536 loc) · 14.7 KB
/
append.c
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
* String appending functions that take into account buffer limit.
*
* Copyright 2020 by Gray Watson
*
* This file is part of the dmalloc package.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies, and that the name of Gray Watson not be used in advertising
* or publicity pertaining to distribution of the document or software
* without specific, written prior permission.
*
* Gray Watson makes no representations about the suitability of the
* software described herein for any purpose. It is provided "as is"
* without express or implied warranty.
*
* The author may be contacted via https://dmalloc.com/
*/
/*
* This file holds the compatibility routines necessary for the library to
* function just in case your system does not have them.
*/
#if HAVE_STDARG_H
# include <stdarg.h> /* for ... */
#endif
#if HAVE_STDIO_H
# include <stdio.h> /* for FILE */
#endif
#if HAVE_STRING_H
# include <string.h> /* for strlen */
#endif
#if HAVE_UNISTD_H
# include <unistd.h> /* for write */
#endif
#define DMALLOC_DISABLE
#include "conf.h"
#include "dmalloc.h"
#include "append.h"
#include "compat.h"
#include "dmalloc_loc.h"
/*
* If we did not specify a precision then we need to stop somewhere
* otherwise we get something like 99.0000100000000031741365091872284.
* This is somewhat arbitrary. We could try to autoconf a value based
* on float.h and FLT_MAX or something but for internal purposes, this
* seems fine.
*/
#define DEFAULT_DECIMAL_PRECISION 10
/*
* Internal method to process a long or int number and write into a buffer.
*/
static char *handle_decimal(char *buf, char *limit, const long num, const int base)
{
char *buf_p = buf;
buf_p = append_long(buf_p, limit, num, base);
append_null(buf_p, limit);
return buf_p;
}
/*
* Internal method to process a long or int number and write into a buffer.
*/
static char *handle_pointer(char *buf, char *limit, const PNT_ARITH_TYPE num, const int base)
{
char *buf_p = buf;
buf_p = append_pointer(buf_p, limit, num, base);
append_null(buf_p, limit);
return buf_p;
}
/*
* Internal method to handle floating point numbers.
*/
static char *handle_float(char *buf, char *limit, double num, int decimal_precision,
const int no_precision)
{
long long_num;
char *buf_p = buf;
/*
* Doing a float and a double if long_arg hrows a super bad
* exception and causes the program to abort. Not sure if this is
* something that we need to autoconf around.
*/
long_num = (long)num;
buf_p = append_long(buf_p, limit, long_num, 10);
/* remove the int part */
num -= (double)long_num;
if (num == 0.0 || decimal_precision == 0) {
append_null(buf_p, limit);
return buf_p;
}
/* TODO: need to handle different number formats */
if (buf_p < limit) {
*buf_p++ = '.';
}
char *non_zero_limit_p = buf_p;
while (num > 0.0 && --decimal_precision >= 0) {
num *= 10.0;
long_num = (long)num;
if (long_num >= 10) {
long_num = 9;
}
if (buf_p < limit) {
*buf_p++ = '0' + long_num;
/* track the last non-0 digit in case we need to truncate ending 000 chars */
if (long_num != 0) {
non_zero_limit_p = buf_p;
}
}
num -= (double)long_num;
}
/* if we did not specify precision then don't end with a bunch of 0s */
if (no_precision) {
buf_p = non_zero_limit_p;
}
append_null(buf_p, limit);
return buf_p;
}
/*
* Append string argument to destination up to limit pointer. Pointer
* to the end of the characters added will be returned. No \0
* character will be added.
*/
char *append_string(char *dest, const char *limit, const char *value)
{
while (dest < limit && *value != '\0') {
*dest++ = *value++;
}
return dest;
}
/*
* Append long value argument to destination up to limit pointer.
* Pointer to the end of the added characters will be returned. No \0
* character will be added. Variant of itoa() written by Lukas
* Chmela which is released under GPLv3.
*/
char *append_long(char *dest, char *limit, long value, int base)
{
char buf[30];
char *ptr = buf;
char *ptr1 = buf;
char tmp_char;
long tmp_value;
/* letters that handle both negative and positive values */
char *letters =
"zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz";
int mid = 35;
/* build the string with low order digits first: 100 => "001" */
do {
tmp_value = value;
value /= base;
*ptr++ = letters[mid + tmp_value - (value * base)];
} while (value != 0);
/* apply negative sign */
if (tmp_value < 0) {
*ptr++ = '-';
}
*ptr-- = '\0';
/* now we swap the characters to move high order digits first */
while (ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
return append_string(dest, limit, buf);
}
/*
* Append unsigned long value argument to destination up to limit
* pointer. Pointer to the end of the added characters will be
* returned. No \0 character will be added. Variant of itoa()
* written by Lukas Chmela. Released under GPLv3.
*/
char *append_ulong(char *dest, char *limit, unsigned long value, int base)
{
char buf[30];
char *ptr = buf;
char *ptr1 = buf;
char tmp_char;
unsigned long tmp_value;
/* letters that handle both negative and positive values */
char *letters =
"zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz";
int mid = 35;
/* build the string with low order digits first: 100 => "001" */
do {
tmp_value = value;
value /= base;
*ptr++ = letters[mid + tmp_value - (value * base)];
} while (value != 0);
*ptr-- = '\0';
/* now we swap the characters to move high order digits first */
while (ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
return append_string(dest, limit, buf);
}
/*
* Append pointer value argument to destination up to limit pointer.
* Pointer to the end of the added characters will be returned. No \0
* character will be added. Variant of itoa() written by Lukas
* Chmela which is released under GPLv3.
*/
char *append_pointer(char *dest, char *limit, PNT_ARITH_TYPE value, int base)
{
char buf[30];
char *ptr = buf;
char *ptr1 = buf;
char tmp_char;
PNT_ARITH_TYPE tmp_value;
/* letters that handle both negative and positive values */
char *letters =
"zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz";
int mid = 35;
/* build the string with low order digits first: 100 => "001" */
do {
tmp_value = value;
value /= base;
*ptr++ = letters[mid + tmp_value - (value * base)];
} while (value != 0);
/* apply negative sign */
if (tmp_value < 0) {
*ptr++ = '-';
}
*ptr-- = '\0';
/* now we swap the characters to move high order digits first */
while (ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
return append_string(dest, limit, buf);
}
/*
* Append a varargs format to destination. Pointer to the end of the
* characters added will be returned. No \0 character will be added.
*/
char *append_vformat(char *dest, char *limit, const char *format,
va_list args)
{
const char *format_p = format;
char value_buf[64];
char *value_limit = value_buf + sizeof(value_buf);
char *dest_p = dest;
int format_prefix, neg_pad, trunc, long_arg;
char pad_char;
char *prefix, *prefix_p;
int width_len, trunc_len, prefix_len;
while (*format_p != '\0' && dest_p < limit) {
if (*format_p != '%') {
*dest_p++ = *format_p++;
continue;
}
format_p++;
format_prefix = 0;
neg_pad = 0;
trunc = 0;
long_arg = 0;
pad_char = ' ';
prefix = "";
width_len = -1;
trunc_len = -1;
prefix_len = 0;
while (1) {
char ch = *format_p++;
if (ch == '\0') {
break;
}
if (ch == '%') {
if (dest_p < limit) {
*dest_p++ = '%';
}
break;
}
if (ch == '#') {
format_prefix = 1;
} else if (ch == '0' && width_len < 0 && trunc_len < 0) {
pad_char = '0';
} else if (ch == '-') {
neg_pad = 1;
} else if (ch == '.') {
trunc = 1;
} else if (ch == '*') {
if (trunc) {
trunc_len = va_arg(args, int);
} else {
width_len = va_arg(args, int);
}
} else if (ch >= '0' && ch <= '9') {
if (trunc) {
if (trunc_len < 0) {
trunc_len = (ch - '0');
} else {
trunc_len = trunc_len * 10 + (ch - '0');
}
} else {
if (width_len < 0) {
width_len = (ch - '0');
} else {
width_len = width_len * 10 + (ch - '0');
}
}
} else if (ch == 'l') {
long_arg = 1;
} else if (ch != 'c' && ch != 'd' && ch != 'f' && ch != 'o' && ch != 'p'
&& ch != 's' && ch != 'u' && ch != 'x') {
continue;
}
/* process the % format character */
const char *value;
if (ch == 'c') {
value_buf[0] = (char)va_arg(args, int);
value_buf[1] = '\0';
value = value_buf;
} else if (ch == 'd') {
long num;
if (long_arg) {
num = va_arg(args, long);
} else {
num = va_arg(args, int);
}
handle_decimal(value_buf, value_limit, num, 10);
value = value_buf;
} else if (ch == 'f') {
double num = va_arg(args, double);
if (trunc_len < 0) {
handle_float(value_buf, value_limit, num, DEFAULT_DECIMAL_PRECISION, 1);
} else {
handle_float(value_buf, value_limit, num, trunc_len, 0);
}
value = value_buf;
/* special case here, the trunc length is really decimal precision */
trunc_len = -1;
} else if (ch == 'o') {
long num;
if (long_arg) {
num = va_arg(args, long);
} else {
num = va_arg(args, int);
}
handle_decimal(value_buf, value_limit, num, 8);
value = value_buf;
if (format_prefix) {
prefix = "0";
prefix_len = 1;
}
} else if (ch == 'p') {
DMALLOC_PNT pnt = va_arg(args, DMALLOC_PNT);
PNT_ARITH_TYPE num = (PNT_ARITH_TYPE)pnt;
handle_pointer(value_buf, value_limit, num, 16);
value = value_buf;
// because %#p throws a gcc warning, I've decreed that %p has a 0x hex prefix
prefix = "0x";
prefix_len = 2;
} else if (ch == 's') {
value = va_arg(args, char *);
} else if (ch == 'u') {
unsigned long num;
if (long_arg) {
num = va_arg(args, unsigned long);
} else {
num = va_arg(args, unsigned int);
}
char *value_buf_p = value_buf;
value_buf_p = append_ulong(value_buf_p, value_limit, num, 10);
append_null(value_buf_p, value_limit);
value = value_buf;
} else if (ch == 'x') {
long num;
if (long_arg) {
num = va_arg(args, long);
} else {
num = va_arg(args, int);
}
handle_decimal(value_buf, value_limit, num, 16);
if (format_prefix) {
prefix = "0x";
prefix_len = 2;
}
value = value_buf;
} else {
continue;
}
/* prepend optional numeric prefix which has to come before padding */
if (prefix_len > 0) {
prefix_p = prefix;
while (*prefix_p != '\0' && dest_p < limit) {
*dest_p++ = *prefix_p++;
}
}
/* handle our pre-padding with spaces or 0s */
int pad = 0;
if (width_len >= 0) {
if (trunc_len >= 0) {
pad = width_len - strnlen(value, trunc_len) - prefix_len;
} else {
pad = width_len - strlen(value) - prefix_len;
}
if (!neg_pad) {
while (pad-- > 0 && dest_p < limit) {
*dest_p++ = pad_char;
}
}
}
/* change the limit if we are truncating the string */
char *str_limit = limit;
if (trunc_len >= 0) {
str_limit = dest_p + trunc_len;
if (str_limit > limit) {
str_limit = limit;
}
}
/* copy the value watching our limit */
while (*value != '\0' && dest_p < str_limit) {
*dest_p++ = *value++;
}
/* maybe handle left over padding which would be negative padding */
while (pad-- > 0 && dest_p < limit) {
/* always pad at the end with a space */
*dest_p++ = ' ';
}
break;
}
}
return dest_p;
}
/*
* Append a varargs format to destination. Pointer to the end of the
* added characters will be returned. No \0 character will be added.
*/
char *append_format(char *dest, char *limit, const char *format, ...)
{
va_list args;
char *dest_p;
va_start(args, format);
dest_p = append_vformat(dest, limit, format, args);
va_end(args);
return dest_p;
}
/*
* Append \0 character to destination. If dest is => limit then \0
* will be written one character before the limit. Pointer past the
* end of the \0 character will be returned.
*/
char *append_null(char *dest, char *limit)
{
if (dest < limit) {
*dest++ = '\0';
} else {
*(limit - 1) = '\0';
}
return dest;
}
/*
* Local implementation of snprintf. We are doing this trying to not
* use the system version which often can allocate memory itself
* causing the library to go recursive.
*/
int loc_vsnprintf(char *buf, const int size, const char *format,
va_list args)
{
char *limit, *buf_p;
limit = buf + size;
buf_p = append_vformat(buf, limit, format, args);
append_null(buf_p, limit);
return (int)(buf_p - buf);
}
/*
* Local implementation of snprintf. We are doing this trying to not
* use the system version which often can allocate memory itself
* causing the library to go recursive.
*/
int loc_snprintf(char *buf, const int size, const char *format, ...)
{
va_list args;
int len;
va_start(args, format);
len = loc_vsnprintf(buf, size, format, args);
va_end(args);
return len;
}
/*
* Local implementation of printf so we can use %p and other non-standard formats.
*/
void loc_printf(const char *format, ...)
{
va_list args;
va_start(args, format);
loc_vfprintf(stdout, format, args);
va_end(args);
}
/*
* Local implementation of fprintf so we can use %p and other non-standard formats.
*/
void loc_fprintf(FILE *file, const char *format, ...)
{
va_list args;
va_start(args, format);
loc_vfprintf(file, format, args);
va_end(args);
}
/*
* Local implementation of vfprintf so we can use %p and other non-standard formats.
*/
void loc_vfprintf(FILE *file, const char *format, va_list args)
{
// these are simple messages so this limit is ok
char buf[256];
char *buf_p, *limit;
limit = buf + sizeof(buf);
buf_p = append_vformat(buf, limit, format, args);
fwrite(buf, 1, (buf_p - buf), file);
}
/*
* Local implementation of dprintf so we can use %p and other non-standard formats.
*/
void loc_dprintf(int fd, const char *format, ...)
{
va_list args;
va_start(args, format);
loc_vdprintf(fd, format, args);
va_end(args);
}
/*
* Local implementation of vdprintf so we can use %p and other non-standard formats.
*/
void loc_vdprintf(int fd, const char *format, va_list args)
{
// these are simple messages so this limit is ok
char buf[256];
char *buf_p, *limit;
limit = buf + sizeof(buf);
buf_p = append_vformat(buf, limit, format, args);
(void)!write(fd, buf, buf_p - buf);
}