Debian Codegraph @

src/lib/libdwarf/dwarf_string.c

raw
1 /*
2 Copyright (c) 2019-2019, David Anderson
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with
6 or without modification, are permitted provided that the
7 following conditions are met:
8
9 Redistributions of source code must retain the above
10 copyright notice, this list of conditions and the following
11 disclaimer.
12
13 Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following
15 disclaimer in the documentation and/or other materials
16 provided with the distribution.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /* A lighly generalized data buffer.
34 Works for more than just strings,
35 but has features (such as ensuring
36 data always has a NUL byte following
37 the data area used) most useful for C strings.
38
39 All these return either TRUE (the values altered)
40 or FALSE (something went wrong, quite likely
41 the caller presented a bad format string for the
42 value). Normally a string like
43 DWARFSTRINGERR is stuck in the output in case of error.
44
45 */
46
47 #include <config.h>
48
49 #include <stdlib.h> /* free() malloc() strtol() */
50 #include <string.h> /* memcpy() strlen() */
51
52 #include "libdwarf_private.h"
53 #include "dwarf_string.h"
54
55 /* m must be a string, like "DWARFSTRINGERR..." for this to work */
56 #define DWSERR(m) dwarfstring_append_length(data,(m),sizeof(m)-1)
57
58 static unsigned long minimumnewlen = 30;
59
60 /* Here we set s_data to a valid pointer to a null byte, though
61 the s_size and s_avail are set to zero. */
62 int
63 dwarfstring_constructor(struct dwarfstring_s *g)
64 {
65 g->s_data = "";
66 g->s_size = 0;
67 g->s_avail = 0;
68 g->s_malloc = FALSE;
69 return TRUE;
70 }
71
72 /* We only increase the s_data space.
73 Only calling dwarfstring_destructor
74 eliminates space. */
75 static int
76 dwarfstring_add_to(struct dwarfstring_s *g,size_t newlen)
77 {
78 char *b = 0;
79 /* s_size - s_avail is the string without counting
80 the null following the string. So, is strlen() */
81 size_t lastpos = g->s_size - g->s_avail;
82 size_t malloclen = newlen+1;
83
84 /* ASSERT: newlen as well as malloclen are
85 greater than g->s_size at both call points */
86 if (malloclen < minimumnewlen) {
87 malloclen = minimumnewlen;
88 }
89 /* Not zeroing the new buffer block. */
90 b = malloc(malloclen);
91 if (!b) {
92 return FALSE;
93 }
94 if (lastpos > 0) {
95 /* Copying the non-null bytes in s_data. */
96 memcpy(b,g->s_data,lastpos);
97 }
98 if (g->s_malloc) {
99 free(g->s_data);
100 g->s_data = 0;
101 }
102 g->s_data = b;
103 /* s_data[lastpos] is one past the end of anything
104 counted as string
105 in s_data at the point of call, and is guaranteed
106 to be safe as we increased the size of s_data, we did not
107 shrink. And, too, we add 1 to newlen, always,
108 so space for a terminating null byte is guaranteed
109 available. */
110 g->s_data[lastpos] = 0;
111 g->s_size = newlen;
112 g->s_avail = newlen - lastpos;
113 g->s_malloc = TRUE;
114 return TRUE;
115 }
116
117 int
118 dwarfstring_reset(struct dwarfstring_s *g)
119 {
120 if (!g->s_size) {
121 /* In initial condition, nothing to do. */
122 return TRUE;
123 }
124 g->s_avail = g->s_size;
125 g->s_data[0] = 0;
126 return TRUE;
127 }
128
129 int
130 dwarfstring_constructor_fixed(struct dwarfstring_s *g,
131 size_t len)
132 {
133 int r = FALSE;
134
135 dwarfstring_constructor(g);
136 if (len == 0) {
137 return TRUE;
138 }
139 r = dwarfstring_add_to(g,len);
140 if (!r) {
141 return FALSE;
142 }
143 return TRUE;
144 }
145
146 int
147 dwarfstring_constructor_static(struct dwarfstring_s *g,
148 char * space,
149 size_t len)
150 {
151 dwarfstring_constructor(g);
152 g->s_data = space;
153 g->s_data[0] = 0;
154 g->s_size = len;
155 g->s_avail = len;
156 g->s_malloc = FALSE;
157 return TRUE;
158 }
159
160 void
161 dwarfstring_destructor(struct dwarfstring_s *g)
162 {
163 if (g->s_malloc) {
164 free(g->s_data);
165 g->s_data = 0;
166 g->s_malloc = 0;
167 }
168 /* The constructor sets all the fields, most to zero.
169 s_data is set to point to static string ""
170 s_malloc set to FALSE. */
171 dwarfstring_constructor(g);
172 }
173
174 /* For the case where one wants just the first 'len'
175 characters of 'str'. NUL terminator provided
176 for you in s_data.
177 */
178 int
179 dwarfstring_append_length(struct dwarfstring_s *g,char *str,
180 size_t slen)
181 {
182 /* lastpos is the length of characters
183 without the null-terminator we call it strlen */
184 size_t lastpos = g->s_size - g->s_avail;
185 int r = 0;
186
187 if (!str || slen ==0) {
188 return TRUE;
189 }
190 if (slen >= g->s_avail) {
191 size_t newlen = 0;
192
193 newlen = g->s_size + slen+2;
194 r = dwarfstring_add_to(g,newlen);
195 if (!r) {
196 /* Unable to resize, dare not do anything. */
197 return FALSE;
198 }
199 }
200 memcpy(g->s_data + lastpos,str,slen);
201 g->s_avail -= slen;
202 /* Adding string terminating null byte.
203 Space is guaranteed available to do this.*/
204 g->s_data[g->s_size - g->s_avail] = 0;
205 return TRUE;
206 }
207
208 int
209 dwarfstring_append(struct dwarfstring_s *g,char *str)
210 {
211 size_t dlenszt = 0;
212
213 if (!str) {
214 return TRUE;
215 }
216 dlenszt = strlen(str);
217 return dwarfstring_append_length(g,str,dlenszt);
218 }
219
220 char *
221 dwarfstring_string(struct dwarfstring_s *g)
222 {
223 return g->s_data;
224 }
225
226 size_t
227 dwarfstring_strlen(struct dwarfstring_s *g)
228 {
229 return g->s_size - g->s_avail;
230 }
231
232 static int
233 _dwarfstring_append_spaces(dwarfstring *data,
234 size_t count)
235 {
236 int res = 0;
237 char spacebuf[] = {" "};
238 size_t charct = sizeof(spacebuf)-1;
239 size_t l = count;
240
241 while (l > charct) {
242 res = dwarfstring_append_length(data,spacebuf,charct);
243 l -= charct;
244 if (res != TRUE) {
245 return res;
246 }
247 }
248 /* ASSERT: l > 0 */
249 res = dwarfstring_append_length(data,spacebuf,l);
250 return res;
251 }
252 static int
253 _dwarfstring_append_zeros(dwarfstring *data, size_t l)
254 {
255 int res = 0;
256 static char zeros[] = {
257 "0000000000000000000000000000000000000000"};
258 size_t charct = sizeof(zeros)-1;
259
260 while (l > charct) {
261 res = dwarfstring_append_length(data,zeros,charct);
262 l -= charct;
263 if (res != TRUE) {
264 return res;
265 }
266 }
267 /* ASSERT: l > 0 */
268 dwarfstring_append_length(data,zeros,l);
269 return res;
270 }
271
272 int dwarfstring_append_printf_s(dwarfstring *data,
273 char *format,char *s)
274 {
275 size_t stringlenszt = 0;
276 size_t next = 0;
277 long val = 0;
278 char *endptr = 0;
279 const char *numptr = 0;
280 /* was %[-]fixedlen. Zero means no len provided. */
281 size_t fixedlen = 0;
282 /* was %-, nonzero means left-justify */
283 long leftjustify = 0;
284 size_t prefixlen = 0;
285 int res = 0;
286
287 if (!s) {
288 DWSERR("<DWARFSTRINGERR: null string pointer to "
289 "dwarfstring_append_printf_s>");
290 return FALSE;
291 }
292 stringlenszt = strlen(s);
293 if (!format) {
294 DWSERR("<DWARFSTRINGERR: null format pointer to "
295 "dwarfstring_append_printf_s>");
296 return FALSE;
297 }
298 while (format[next] && format[next] != '%') {
299 ++next;
300 ++prefixlen;
301 }
302 if (prefixlen) {
303 dwarfstring_append_length(data,format,prefixlen);
304 /* Fall through whether return value TRUE or FALSE */
305 }
306 if (format[next] != '%') {
307 /* No % operator found, we are done */
308 DWSERR("<DWARFSTRINGERR: no percent passed to "
309 "dwarfstring_append_printf_s>");
310 return FALSE;
311 }
312 next++;
313 if (!format[next]) {
314 DWSERR("<DWARFSTRINGERR: empty percent to "
315 "dwarfstring_append_printf_s>");
316 return FALSE;
317 }
318 if (format[next] == ' ') {
319 DWSERR("<DWARFSTRINGERR: empty percent to "
320 "dwarfstring_append_printf_s>");
321 return FALSE;
322 }
323
324 if (format[next] == '-') {
325 leftjustify++;
326 next++;
327 }
328 numptr = format+next;
329 val = strtol(numptr,&endptr,10);
330 if ( endptr != numptr) {
331 fixedlen = val;
332 }
333 next = (endptr - format);
334 if (format[next] != 's') {
335 DWSERR("<DWARFSTRINGERR: no percent-s to "
336 "dwarfstring_append_printf_s>");
337 return FALSE;
338 }
339 next++;
340
341 if (fixedlen && (stringlenszt >= fixedlen)) {
342 /* Ignore leftjustify (if any) and the stringlenszt
343 as the actual string overrides those. */
344 leftjustify = 0;
345 }
346 if (leftjustify) {
347
348 dwarfstring_append_length(data,s,stringlenszt);
349 /* Ignore return value */
350 if (fixedlen) {
351 size_t trailingspaces = fixedlen - stringlenszt;
352
353 _dwarfstring_append_spaces(data,trailingspaces);
354 }
355 } else {
356 if (fixedlen && fixedlen < stringlenszt) {
357 /* This lets us have fixedlen < stringlenszt by
358 taking all the chars from s*/
359 dwarfstring_append_length(data,s,stringlenszt);
360 /* Ignore return value, just keep going */
361 } else {
362 if (fixedlen) {
363 size_t leadingspaces = fixedlen - stringlenszt;
364 size_t k = 0;
365
366 for ( ; k < leadingspaces; ++k) {
367 dwarfstring_append_length(data," ",1);
368 }
369 }
370 res = dwarfstring_append_length(data,s,stringlenszt);
371 if (res == FALSE) {
372 return res;
373 }
374 }
375 }
376 if (!format[next]) {
377 return TRUE;
378 }
379 {
380 char * startpt = format+next;
381 size_t suffixlen = strlen(startpt);
382
383 res = dwarfstring_append_length(data,startpt,suffixlen);
384 }
385 return res;
386 }
387
388 static char v32m[] = {"-2147483648"};
389 static char v64m[] = {"-9223372036854775808"};
390 static char dtable[10] = {
391 '0','1','2','3','4','5','6','7','8','9'
392 };
393 static char xtable[16] = {
394 '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
395 };
396 static char Xtable[16] = {
397 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
398 };
399
400 /* We deal with formats like:
401 %d %5d %05d %+d %+5d %-5d (and ld and lld too). */
402 int dwarfstring_append_printf_i(dwarfstring *data,
403 char *format,
404 dwarfstring_i v)
405 {
406 int res = TRUE;
407 size_t next = 0;
408 long val = 0;
409 char *endptr = 0;
410 const char *numptr = 0;
411 size_t fixedlen = 0;
412 int leadingzero = 0;
413 int minuscount = 0; /*left justify */
414 int pluscount = 0;
415 int lcount = 0;
416 int ucount = 0;
417 int dcount = 0;
418 int xcount = 0;
419 int Xcount = 0;
420 char *ctable = dtable;
421 size_t prefixlen = 0;
422 int done = 0;
423
424 if (!format) {
425 DWSERR("<DWARFSTRINGERR: null format pointer to "
426 "dwarfstring_append_printf_i>");
427 return FALSE;
428 }
429 while (format[next] && format[next] != '%') {
430 ++next;
431 ++prefixlen;
432 }
433 dwarfstring_append_length(data,format,prefixlen);
434 if (format[next] != '%') {
435 /* No % operator found, we are done */
436 DWSERR("<DWARFSTRINGERR: no percent passed to "
437 "dwarfstring_append_printf_i>");
438 return FALSE;
439 }
440 next++;
441 if (!format[next]) {
442 DWSERR("<DWARFSTRINGERR: empty percent to "
443 "dwarfstring_append_printf_i>");
444 return FALSE;
445 }
446 if (format[next] == ' ') {
447 DWSERR("<DWARFSTRINGERR: empty percent to "
448 "dwarfstring_append_printf_i>");
449 return FALSE;
450 }
451 if (format[next] == '-') {
452 minuscount++;
453 next++;
454 }
455 if (format[next] == '+') {
456 pluscount++;
457 next++;
458 }
459 if (format[next] == '-') {
460 minuscount++;
461 next++;
462 }
463 if (format[next] == '0') {
464 leadingzero = 1;
465 next++;
466 }
467 numptr = format+next;
468 val = strtol(numptr,&endptr,10);
469 if ( endptr != numptr) {
470 fixedlen = val;
471 }
472 next = (endptr - format);
473 /* Following is lx lu or u or llx llu , we take
474 all this to mean 64 bits, */
475 #ifdef _WIN32
476 if (format[next] == 'I') {
477 /*lcount++;*/
478 next++;
479 }
480 if (format[next] == '6') {
481 /*lcount++;*/
482 next++;
483 }
484 if (format[next] == '4') {
485 /*lcount++;*/
486 next++;
487 }
488 #endif /* _WIN32 */
489 if (format[next] == 'l') {
490 lcount++;
491 next++;
492 }
493 if (format[next] == 'l') {
494 lcount++;
495 next++;
496 }
497 if (format[next] == 'l') {
498 lcount++;
499 next++;
500 }
501 if (format[next] == 'u') {
502 ucount++;
503 next++;
504 }
505 if (format[next] == 'd') {
506 dcount++;
507 next++;
508 }
509 if (format[next] == 'x') {
510 xcount++;
511 next++;
512 }
513 if (format[next] == 'X') {
514 Xcount++;
515 next++;
516 }
517 if (format[next] == 's') {
518 DWSERR( "<DWARFSTRINGERR: format percent s passed to "
519 "dwarfstring_append_printf_i>");
520 return FALSE;
521 }
522 if (xcount || Xcount) {
523 /* Use the printf_u for %x and the like
524 just copying the entire format makes
525 it easier for coders to understand
526 nothing much was done */
527 DWSERR("<DWARFSTRINGERR: format %x or %X passed to "
528 "dwarfstring_append_printf_i>");
529 return FALSE;
530 }
531 if (!dcount || (lcount >2) ||
532 (Xcount+xcount+dcount+ucount) > 1) {
533 /* error */
534 DWSERR( "<DWARFSTRINGERR: format has too many "
535 "percent x/d/u/l passed to "
536 "dwarfstring_append_printf_i>");
537 return FALSE;
538 }
539 if (pluscount && minuscount) {
540 /* We don't allow format +- */
541 DWSERR("<DWARFSTRINGERR: format disallowed. +- passed to "
542 "dwarfstring_append_printf_i>");
543 return FALSE;
544 }
545 {
546 char digbuf[36];
547 char *digptr = digbuf+sizeof(digbuf) -1;
548 size_t digcharlen = 0;
549 dwarfstring_i remaining = v;
550 int vissigned = 0;
551 dwarfstring_i divisor = 10;
552
553 *digptr = 0;
554 --digptr;
555 if (v < 0) {
556 vissigned = 1;
557 /* This test is for twos-complement
558 machines and would be better done via
559 configure with a compile-time check
560 so we do not need a size test at runtime. */
561 if (sizeof(v) == 8) {
562 dwarfstring_u vm = 0x7fffffffffffffffULL;
563 if (vm == (dwarfstring_u)~v) {
564 memcpy(digbuf,v64m,sizeof(v64m));
565 digcharlen = sizeof(v64m)-1;
566 digptr = digbuf;
567 done = 1;
568 } else {
569 remaining = -v;
570 }
571 } else if (sizeof(v) == 4) {
572 dwarfstring_u vm = 0x7fffffffL;
573 if (vm == (dwarfstring_u)~v) {
574 memcpy(digbuf,v32m,sizeof(v32m));
575 digcharlen = sizeof(v32m)-1;
576 digptr = digbuf;
577 done = 1;
578 } else {
579 remaining = -v;
580 }
581 }else {
582 DWSERR("<DWARFSTRINGERR: v passed to "
583 "dwarfstring_append_printf_i "
584 "cannot be handled:integer size>");
585 return FALSE;
586 }
587 }
588 if (!done) {
589 for ( ;; ) {
590 dwarfstring_u dig = 0;
591
592 dig = remaining % divisor;
593 remaining /= divisor;
594 *digptr = ctable[dig];
595 digcharlen++;
596 if (!remaining) {
597 break;
598 }
599 --digptr;
600 }
601 if (vissigned) { /* could check minuscount instead */
602 --digptr;
603 digcharlen++;
604 *digptr = '-';
605 } else if (pluscount) {
606 --digptr;
607 digcharlen++;
608 *digptr = '+';
609 } else { /* Fall through */ }
610 }
611 if (fixedlen > 0) {
612 if (fixedlen <= digcharlen) {
613 dwarfstring_append_length(data,digptr,digcharlen);
614 } else {
615 size_t prefixcount = fixedlen - digcharlen;
616 if (!leadingzero) {
617 _dwarfstring_append_spaces(data,prefixcount);
618 dwarfstring_append_length(data,digptr,digcharlen);
619 } else {
620 if (*digptr == '-') {
621 dwarfstring_append_length(data,"-",1);
622 _dwarfstring_append_zeros(data,prefixcount);
623 digptr++;
624 dwarfstring_append_length(data,digptr,
625 digcharlen-1);
626 } else if (*digptr == '+') {
627 dwarfstring_append_length(data,"+",1);
628 _dwarfstring_append_zeros(data,prefixcount);
629 digptr++;
630 dwarfstring_append_length(data,digptr,
631 digcharlen-1);
632 } else {
633 _dwarfstring_append_zeros(data,prefixcount);
634 dwarfstring_append_length(data,digptr,
635 digcharlen);
636 }
637 }
638 }
639 } else {
640 res = dwarfstring_append_length(data,digptr,digcharlen);
641 }
642 }
643 if (format[next]) {
644 size_t trailinglen = strlen(format+next);
645 res = dwarfstring_append_length(data,format+next,trailinglen);
646 }
647 return res;
648 }
649
650 #if 0 /* Unused trimleadingzeros */
651 /* Counts hex chars. divide by two to get bytes from input
652 integer. */
653 static unsigned
654 trimleadingzeros(char *ptr,size_t digits,unsigned keepcount)
655 {
656 char *cp = ptr;
657 size_t leadzeroscount = 0;
658 size_t trimoff = 0;
659
660 for (; *cp; ++cp) {
661 if (*cp == '0') {
662 leadzeroscount++;
663 continue;
664 }
665 }
666 trimoff = keepcount - digits;
667 if (trimoff&1) {
668 trimoff--;
669 }
670 return trimoff;
671 }
672 #endif /*0*/
673
674 /* With gcc version 5.4.0 20160609 a version using
675 const char *formatp instead of format[next]
676 and deleting the 'next' variable
677 is a few hundredths of a second slower, repeatably.
678
679 We deal with formats like:
680 %u %5u %05u (and ld and lld too).
681 %x %5x %05x (and ld and lld too). */
682
683 int dwarfstring_append_printf_u(dwarfstring *data,
684 char *format,
685 dwarfstring_u v)
686 {
687
688 size_t next = 0;
689 long val = 0;
690 char *endptr = 0;
691 const char *numptr = 0;
692 size_t fixedlen = 0;
693 int leadingzero = 0;
694 int lcount = 0;
695 int ucount = 0;
696 int dcount = 0;
697 int xcount = 0;
698 int Xcount = 0;
699 char *ctable = 0;
700 size_t divisor = 0;
701 size_t prefixlen = 0;
702
703 if (!format) {
704 DWSERR("<DWARFSTRINGERR: null format pointer to "
705 "dwarfstring_append_printf_u>");
706 return FALSE;
707 }
708 while (format[next] && format[next] != '%') {
709 ++next;
710 ++prefixlen;
711 }
712 dwarfstring_append_length(data,format,prefixlen);
713 if (format[next] != '%') {
714 /* No % operator found, we are done */
715 DWSERR("<DWARFSTRINGERR: no percent passed to "
716 "dwarfstring_append_printf_u>");
717 return FALSE;
718 }
719 next++;
720 if (!format[next]) {
721 DWSERR("<DWARFSTRINGERR: empty percent to "
722 "dwarfstring_append_printf_u>");
723 return FALSE;
724 }
725 if (format[next] == ' ') {
726 DWSERR("<DWARFSTRINGERR: empty percent to "
727 "dwarfstring_append_printf_u>");
728 return FALSE;
729 }
730 if (format[next] == '-') {
731 DWSERR("<DWARFSTRINGERR: format - passed to "
732 "dwarfstring_append_printf_u "
733 "cannot be handled>");
734 return FALSE;
735 }
736 if (format[next] == '0') {
737 leadingzero = 1;
738 next++;
739 }
740 numptr = format+next;
741 val = strtol(numptr,&endptr,10);
742 if ( endptr != numptr) {
743 fixedlen = val;
744 }
745 next = (endptr - format);
746 /* Following is lx lu or u or llx llu , we take
747 all this to mean 64 bits, */
748 #ifdef _WIN32
749 if (format[next] == 'I') {
750 /*lcount++;*/
751 next++;
752 }
753 if (format[next] == '6') {
754 /*lcount++;*/
755 next++;
756 }
757 if (format[next] == '4') {
758 /*lcount++;*/
759 next++;
760 }
761 #endif /* _WIN32 */
762 if (format[next] == 'l') {
763 lcount++;
764 next++;
765 }
766 if (format[next] == 'l') {
767 lcount++;
768 next++;
769 }
770 if (format[next] == 'l') {
771 lcount++;
772 next++;
773 }
774 if (format[next] == 'u') {
775 ucount++;
776 next++;
777 }
778 if (format[next] == 'd') {
779 dcount++;
780 next++;
781 }
782 if (format[next] == 'x') {
783 xcount++;
784 next++;
785 }
786 if (format[next] == 'X') {
787 Xcount++;
788 next++;
789 }
790 if (format[next] == 's') {
791 DWSERR("<DWARFSTRINGERR: format percent-s passed to "
792 "dwarfstring_append_printf_u "
793 "cannot be handled>");
794 return FALSE;
795 }
796 if ( (Xcount +xcount+dcount+ucount) > 1) {
797 DWSERR("<DWARFSTRINGERR: format percent -x X d u repeats to "
798 "dwarfstring_append_printf_u "
799 "cannot be handled>");
800 return FALSE;
801 }
802 if ( (Xcount +xcount+dcount+ucount) == 0) {
803 DWSERR("<DWARFSTRINGERR: format percent x X d u missing to "
804 "dwarfstring_append_printf_u "
805 "cannot be handled>");
806 return FALSE;
807 }
808 if (lcount > 2) {
809 DWSERR("<DWARFSTRINGERR: format percent lll to "
810 "dwarfstring_append_printf_u "
811 "cannot be handled>");
812 return FALSE;
813 }
814 if (dcount > 0) {
815 DWSERR("<DWARFSTRINGERR: format percent-d to "
816 "dwarfstring_append_printf_u "
817 "cannot be handled>");
818 return FALSE;
819 }
820 if (ucount) {
821 divisor = 10;
822 ctable = dtable;
823 } else {
824 divisor = 16;
825 if (xcount) {
826 ctable = xtable;
827 } else {
828 ctable = Xtable;
829 }
830 }
831 {
832 char digbuf[36];
833 char *digptr = 0;
834 unsigned digcharlen = 0;
835 dwarfstring_u remaining = v;
836
837 if (divisor == 16) {
838 digptr = digbuf+sizeof(digbuf) -1;
839 for ( ;; ) {
840 dwarfstring_u dig;
841 dig = remaining & 0xf;
842 remaining = remaining >> 4;
843 *digptr = ctable[dig];
844 ++digcharlen;
845 if (!remaining) {
846 break;
847 }
848 --digptr;
849 }
850 } else {
851 digptr = digbuf+sizeof(digbuf) -1;
852 *digptr = 0;
853 --digptr;
854 for ( ;; ) {
855 dwarfstring_u dig;
856 dig = remaining % divisor;
857 remaining /= divisor;
858 *digptr = ctable[dig];
859 ++digcharlen;
860 if (!remaining) {
861 break;
862 }
863 --digptr;
864 }
865 }
866 if (fixedlen <= digcharlen) {
867 dwarfstring_append_length(data,digptr,digcharlen);
868 } else {
869 if (!leadingzero) {
870 size_t justcount = fixedlen - digcharlen;
871 _dwarfstring_append_spaces(data,justcount);
872 dwarfstring_append_length(data,digptr,digcharlen);
873 } else {
874 size_t prefixcount = fixedlen - digcharlen;
875 _dwarfstring_append_zeros(data,prefixcount);
876 dwarfstring_append_length(data,digptr,digcharlen);
877 }
878 }
879 }
880 if (format[next]) {
881 size_t trailinglen = strlen(format+next);
882 dwarfstring_append_length(data,format+next,trailinglen);
883 }
884 return FALSE;
885 }