-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback_printf.c
executable file
·3451 lines (2999 loc) · 121 KB
/
callback_printf.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************\
* *
* FILE NAME: callback_printf.c *
* *
* --------------------------------------------------------------------------- *
* *
* DESCRIPTION: system independent (v)s(n)printf wrapper functions *
* *
* --------------------------------------------------------------------------- *
* *
* COPYRIGHT: (c) 2024 Dipl.-Ing. Klaus Lux (Aachen, Germany) *
* *
* --------------------------------------------------------------------------- *
* *
* ORIGIN: https://github/klux21/callback_printf *
* *
* --------------------------------------------------------------------------- *
* *
* Civil Usage Public License, Version 1.1, January 2024 *
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions are met: *
* *
* 1. Redistributions of source code must retain the above copyright *
* notice, this list of conditions, the explanation of terms *
* and the following disclaimer. *
* *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation or other materials provided with the distribution. *
* *
* 3. All modified files must carry prominent notices stating that the *
* files have been changed. *
* *
* 4. The source code and binary forms and any derivative works are not *
* stored or executed in systems or devices which are designed or *
* intended to harm, to kill or to forcibly immobilize people. *
* *
* 5. The source code and binary forms and any derivative works are not *
* stored or executed in systems or devices which are intended to *
* monitor, to track, to change or to control the behavior, the *
* constitution, the location or the communication of any people or *
* their property without the explicit and prior agreement of those *
* people except those devices and systems are solely designed for *
* saving or protecting peoples life or health. *
* *
* 6. The source code and binary forms and any derivative works are not *
* stored or executed in any systems or devices that are intended *
* for the production of any of the systems or devices that *
* have been stated before except the ones for saving or protecting *
* peoples life or health only. *
* *
* The term 'systems' in all clauses shall include all types and combinations *
* of physical, virtualized or simulated hardware and software and any kind *
* of data storage. *
* *
* The term 'devices' shall include any kind of local or non-local control *
* system of the stated devices as part of that device als well. Any assembly *
* of more than one device is one and the same device regarding this license. *
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" *
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE *
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE *
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF *
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN *
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) *
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
* --------------------------------------------------------------------------- *
* *
* What are the advantages of using this implementation instead of using the *
* build-in functions of your compiler? *
* *
* Security: You do not share your possibly private internal string data *
* with 3rd party compiler libraries which are easy to modify. *
* Portability: The implementation behaves the same on every plattform as *
* long as the data types have the same sizes. You do not have *
* to worry about different format strings on every platform. *
* Features: This implementation is pretty fast and provides some special *
* features as the common length modiers for %n, output of *
* integers in binary format and a fully featured floating point *
* output in tridynal, quadral, hexal, heptal, octal and nonal *
* system. For selecting a special numeric base just add a *
* r2 .. r9 prefix to common integer or floation point format *
* specifiers. It's possible to speficy the numeric base using r* *
* prefix and and additional integer argument that contains the *
* base, e.g for printing a long int using numeric base 4 *
* ssprintf(buffer, "%r*ld", (int) 4, LONG_MIN); *
* callback_printf supports the Windows I64 and I32 integer *
* length modifiers as well as the additional modifiers l1, l2, *
* l4 and l8 for 8, 16, 32 and 64 bit wide integer arguments. *
* Wide characters and wide character string are always converted *
* to UTF-8 output. You can use l1, l2 and l4 length modiefiers *
* for converting string of 8, 16 or 32 bit wide character size *
* on every platform. *
* And last but not least is it pretty ease to create your own *
* fully featured and fast sprintf compatible string generator *
* function by using callback_printf whithout worrying about *
* portability. This software is more than a toy. *
* Limits: The function rebase uses some multiplications for calculating *
* the decimal mantissa and is able to convert values for more *
* base systems than just the decimal system. The conversion is *
* very fast but the math of the convertion costs a little bit of *
* precision because of some additionally required floating point *
* operations. This should be rarely a big problem of course. *
* *
* The output of very big values with %f happens in %e format. *
* The output length of the mantissa of floating points is *
* limited in size. *
* callback_printf does not care about any differences of the *
* users locale. *
* *
\*****************************************************************************/
#undef _CRT_ERRNO_DEFINED
#include <errno.h>
#include <limits.h>
#include <float.h> /* LDBL_MAX */
#include <callback_printf.h>
#ifdef _WIN32
#pragma warning(disable : 4100 4127 4706 4710)
#endif
/* ========================================================================= *\
\* ========================================================================= */
const char * LowerDigit = "0123456789abcdefghijklmnopqrstuvwxyz"; /* array for encoding numbers */
const char * UpperDigit = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; /* array for encoding numbers */
/* Array of character type flags:
0x01 : Characters: A-Z a-z _
0x02 : Digits: 0 - 9
0x04 : blanks and control characters
0x08 : operators: \n \r ! " # $ % & ( ) * + , - . / : ; < = > ? [ \ ] ^ @ { | } ~
0x10 : printf format termination characters bBdiouxXaAeEfFgGsScCpn% '\0'
0x20 : printf format flag characters -+# 0
0x40 : printf integer format characters bBdiouxX */
const uint8_t CharType[256] = { 0x10,0x04,0x04,0x04, 0x04,0x04,0x04,0x04, 0x04,0x04,0x08,0x04, 0x04,0x08,0x04,0x04,
0x04,0x04,0x04,0x04, 0x04,0x04,0x04,0x04, 0x04,0x04,0x04,0x04, 0x04,0x04,0x04,0x04,
/* ! " # $ % & ' ( ) * + , - . / */
0x24,0x08,0x08,0x28, 0x08,0x18,0x08,0x08, 0x08,0x08,0x08,0x28, 0x08,0x28,0x08,0x08,
/* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
0x22,0x02,0x02,0x02, 0x02,0x02,0x02,0x02, 0x02,0x02,0x08,0x08, 0x08,0x08,0x08,0x08,
/* @ A B C D E F G H I J K L M N O */
0x08,0x11,0x51,0x11, 0x01,0x91,0x91,0x91, 0x01,0x01,0x01,0x01, 0x01,0x01,0x01,0x01,
/* P Q R S T U V W X Y Z [ \ ] ^ _ */
0x01,0x01,0x01,0x11, 0x01,0x01,0x01,0x01, 0x51,0x01,0x01,0x08, 0x08,0x08,0x08,0x01,
/* ` a b c d e f g h i j k l m n o */
0x08,0x11,0x51,0x11, 0x51,0x91,0x91,0x91, 0x01,0x51,0x01,0x01, 0x01,0x01,0x11,0x51,
/* p q r s t u v w x y z { | } ~ */
0x11,0x01,0x01,0x11, 0x01,0x51,0x01,0x01, 0x51,0x01,0x01,0x08, 0x08,0x08,0x08,0x04,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x04};
/* ========================================================================= *\
UTF8 encoding and decoding
\* ========================================================================= */
/* ------------------------------------------------------------------------- *\
iUtf8Decode decodes an UTF8 data buffer. It stops at end of decodable
source or if end of destination buffer gets reached. Source and
destination buffer must not overlap. Source and destination pointer get
moved behind last decoded character of converted input and output.
The function returns -1 in case of wrong arguments and -2 in case of
(currently ignored!) errors within source data. In success case 0 gets
returned.
If pDst is NULL then no output happens and pDstSize will be set to the
required size of the destination buffer to hold the whole converted input
and the source size and source pointer are unchanged.
\* ------------------------------------------------------------------------- */
int iUtf8Decode (void ** pDst, /* destination buffer */
size_t * pDstSize, /* remaining destination buffer size */
void ** pSrc, /* source buffer */
size_t * pSrcSize, /* remaining source data size */
size_t DstCharWidth) /* width of destination characters */
{
static uint8_t Utf8length[]={ 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* 80 - 9f */
0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, /* a0 - bf */
2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, /* c0 - df */
3,3,3,3,3,3,3,3, 3,3,3,3,3,3,3,3, 4,4,4,4,4,4,4,4, 5,5,5,5,6,6,7,2 }; /* e0 - ff */
int err = 0; /* return value */
size_t dz = 0;
size_t sz = 0;
size_t val = 0x87654321;
size_t max_val = 0;
unsigned char * pv = (unsigned char *) &val;
unsigned char * ps;
size_t l;
int little_endian = 0x21 == *pv; /* nonzero in case of little endian */
if((pDst && !*pDst) || !pDstSize || (pDst && !*pDstSize) ||
!pSrc || !*pSrc || !pSrcSize || !*pSrcSize ||
!DstCharWidth || (DstCharWidth > sizeof(val)))
{
err = -1;
goto Exit;
}
if(sizeof(val) != DstCharWidth)
max_val = ~max_val >> ((sizeof(val) - DstCharWidth) << 3);
else
max_val = ~max_val;
if(!little_endian)
pv += sizeof(val);
sz = *pSrcSize;
ps = (unsigned char *) *pSrc;
if(!pDst)
{
while(sz)
{
l = Utf8length[*ps];
if(1 == l)
{
val = *ps;
ps++;
sz--;
}
else
{
if(!l)
{/* Invalid character, skip this possibly a broken sequence. */
sz--;
ps++;
err = -2;
continue;
}
if(sz < l)
break; /* buffer size smaller than character size */
if(0xff == *ps)
{/* For now we'll ignore 0xff and any immediately following character */
ps += 2;
sz -= 2;
continue;
}
val = *ps++ & ~(0xff << (7-l));
sz--;
while(--l)
{
if(Utf8length[*ps])
break; /* ignore invalid encoded characters */
val <<= 6;
val += *ps++ & 0x3f;
sz--;
}
if(l)
{ /* Let's skip invalid encoded characters and set error flag */
err = -2;
continue;
}
}
if(val > max_val)
{/* The decoded value exceeds character width. Let's replace it by the max. possible value. */
err = -2;
val = max_val;
}
dz += DstCharWidth;
}
}
else
{
unsigned char * pd = (unsigned char *) *pDst;
dz = *pDstSize;
while(sz && (dz >= DstCharWidth))
{
l = Utf8length[*ps];
if(1 == l)
{
val = *ps;
ps++;
sz--;
}
else
{
if(!l)
{/* Invalid character, skip this possibly a broken sequence. */
sz--;
ps++;
err = -2;
continue;
}
if(sz < l)
break; /* buffer size smaller than character size */
if(0xff == *ps)
{/* For now we'll ignore 0xff and any immediately following character */
ps += 2;
sz -= 2;
continue;
}
val = *ps++ & ~(0xff << (7-l));
sz--;
while(--l)
{
if(Utf8length[*ps])
break; /* ignore invalid encoded characters */
val <<= 6;
val += *ps++ & 0x3f;
sz--;
}
if(l)
{ /* Let's skip invalid encoded characters and set error flag */
err = -2;
continue;
}
}
if(val > max_val)
{/* The decoded value exceeds character width. Let's replace it by the max. possible value. */
err = -2;
val = max_val;
}
if(!pDst)
{
dz += DstCharWidth;
}
else
{
if(little_endian)
{
l = 0;
while(l < DstCharWidth)
{
pd[l] = pv[l];
l++;
}
}
else
{
l = DstCharWidth;
while(l)
{
*(pd + DstCharWidth - l) = *(pv - l);
l--;
}
}
pd += DstCharWidth;
dz -= DstCharWidth;
}
}
*pDst = pd;
*pSrc = ps;
*pSrcSize = sz;
}
*pDstSize = dz;
Exit:;
return (err);
}/* int iUtf8Decode (...) */
/* ------------------------------------------------------------------------- *\
iUtf8Encode encodes unicode characters in UTF8 format. It stops at end of
decodable source or if end of destination buffer gets reached. Source and
destination buffer must not overlap. Source and destination pointer get
moved behind last decoded character of converted input and output.
The function returns -1 in case of wrong arguments and -2 in case of
(currently without any output ignored!) errors within source data
(e.g. if a source value exceeds 32 bit). In success case 0 gets returned.
If pDst is NULL then no output happens and pDstSize will be set to the
required size of the destination buffer to hold the whole converted input
and the source size and source pointer are unchanged.
\* ------------------------------------------------------------------------- */
int iUtf8Encode (void ** pDst, /* destination buffer */
size_t * pDstSize, /* remaining destination buffer size */
void ** pSrc, /* source buffer */
size_t * pSrcSize, /* remaining source data size */
size_t SrcCharWidth) /* width of destination characters */
{
int err = 0; /* return value */
size_t dz = 0;
size_t sz = 0;
size_t val = 0x87654321;
unsigned char * pv = (unsigned char *) &val;
unsigned char * ps;
size_t cw = 0;
int little_endian = 0x21 == *pv;
if((pDst && !*pDst) || !pDstSize || (pDst && !*pDstSize) ||
!pSrc || !*pSrc || !pSrcSize || !*pSrcSize ||
(SrcCharWidth > sizeof(val)))
{
err = -1;
goto Exit;
}
if(!little_endian)
pv += sizeof(val);
sz = *pSrcSize;
ps = (unsigned char *) *pSrc;
if (!pDst)
{
while(sz >= SrcCharWidth)
{
val = 0;
if(little_endian)
{
cw = 0;
while(cw < SrcCharWidth)
{
pv[cw] = ps[cw];
cw++;
}
}
else
{
cw = SrcCharWidth;
while(cw)
{
*(pv-cw) = *(ps + SrcCharWidth - cw);
cw--;
}
}
if(val <= 0x7f)
dz++;
else if(val <= 0x7ff)
dz += 2;
else if(val <= 0xffff)
dz += 3;
else if(val <= 0x1fffff)
dz += 4;
else if(val <= 0x3ffffff)
dz += 5;
else if(val <= 0x7fffffff)
dz += 6;
else if(val <= 0xffffffff)
dz += 7;
else
{ /* We don't handle values that exceed 32 bits */
err = -2;
}
ps += SrcCharWidth;
sz -= SrcCharWidth;
}
}
else
{
unsigned char * pd = (unsigned char *) *pDst;
dz = *pDstSize;
while(sz >= SrcCharWidth)
{
val = 0;
if(little_endian)
{
cw = 0;
while(cw < SrcCharWidth)
{
pv[cw] = ps[cw];
cw++;
}
}
else
{
cw = SrcCharWidth;
while(cw)
{
*(pv-cw) = *(ps + SrcCharWidth - cw);
cw--;
}
}
if(val <= 0x7f)
{
if(dz < 1)
break;
dz--;
*pd++ = (unsigned char) val;
}
else if(val <= 0x7ff)
{
if (dz < 2)
break;
dz -= 2;
pd[0] = (unsigned char) (((val >> 6) & 0x1f) + 0xc0);
pd[1] = (unsigned char) ((val & 0x3f) + 0x80);
pd += 2;
}
else if(val <= 0xffff)
{
if (dz < 3)
break;
dz -= 3;
pd[0] = (unsigned char) (((val >> 12) & 0xf) + 0xe0);
pd[1] = (unsigned char) (((val >> 6) & 0x3f) + 0x80);
pd[2] = (unsigned char) ((val & 0x3f) + 0x80);
pd += 3;
}
else if(val <= 0x1fffff)
{
if (dz < 4)
break;
dz -= 4;
pd[0] = (unsigned char) (((val >> 18) & 0x7) + 0xf0);
pd[1] = (unsigned char) (((val >> 12) & 0x3f) + 0x80);
pd[2] = (unsigned char) (((val >> 6) & 0x3f) + 0x80);
pd[3] = (unsigned char) ((val & 0x3f) + 0x80);
pd += 4;
}
else if(val <= 0x3ffffff)
{
if (dz < 5)
break;
dz -= 5;
pd[0] = (unsigned char) (((val >> 24) & 0x3) + 0xf8);
pd[1] = (unsigned char) (((val >> 18) & 0x3f) + 0x80);
pd[2] = (unsigned char) (((val >> 12) & 0x3f) + 0x80);
pd[3] = (unsigned char) (((val >> 6) & 0x3f) + 0x80);
pd[4] = (unsigned char) ((val & 0x3f) + 0x80);
pd += 5;
}
else if(val <= 0x7fffffff)
{
if(dz < 6)
break;
dz -= 6;
pd[0] = (unsigned char) (((val >> 30) & 0x1) + 0xfc);
pd[1] = (unsigned char) (((val >> 24) & 0x3f) + 0x80);
pd[2] = (unsigned char) (((val >> 18) & 0x3f) + 0x80);
pd[3] = (unsigned char) (((val >> 12) & 0x3f) + 0x80);
pd[4] = (unsigned char) (((val >> 6) & 0x3f) + 0x80);
pd[5] = (unsigned char) ((val & 0x3f) + 0x80);
pd += 6;
}
else if(val <= 0xffffffff)
{
if(dz < 7)
break;
dz -= 7;
pd[0] = 0xfe;
pd[1] = (unsigned char) (((val >> 30) & 0x3) + 0x80);
pd[2] = (unsigned char) (((val >> 24) & 0x3f) + 0x80);
pd[3] = (unsigned char) (((val >> 18) & 0x3f) + 0x80);
pd[4] = (unsigned char) (((val >> 12) & 0x3f) + 0x80);
pd[5] = (unsigned char) (((val >> 6) & 0x3f) + 0x80);
pd[6] = (unsigned char) ((val & 0x3f) + 0x80);
pd += 7;
}
else
{ /* We don't handle values that exceed 32 bits */
err = -2;
}
ps += SrcCharWidth;
sz -= SrcCharWidth;
}
*pDst = pd; /* store the end position of the scan */
*pSrc = ps;
*pSrcSize = sz;
}
*pDstSize = dz;
Exit:;
return (err);
} /* int iUtf8Encode (...) */
/* ========================================================================= *\
callback_printf related stuff ...
\* ========================================================================= */
static const char * pzeros = "00000000000000000000000000000000"; /* blanks to copy */
static const char * pblanks = " "; /* blanks to copy */
static const char * lower_digits = "0123456789abcdefghijklmnopqrstuvwxyz";
static const char * upper_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static size_t cbk_print_number(void * pUserData, /* user specific context for the callback */
PRINTF_CALLBACK * pCB, /* data write callback */
const char * pvalue, /* number string to to write out */
size_t length, /* length of number string */
const char * padding, /* pointer to padding bytes to fill in after prefix before the numeric value */
char sign_char, /* optional sign character to fill in */
uint8_t prefix_base, /* if nonzero base for C style prefixing of octals and hexadecimals if prefixeng is required required */
uint8_t uppercase, /* whether to use uppercase letters for prefix */
uint8_t left_justified, /* left justified value witin the output data field */
size_t minwidth, /* minimum width of value to print */
size_t fieldwidth) /* minimum field width */
{
size_t zRet = 0;
char prefix[8];
char * pf = prefix;
size_t prelen; /* length of sign and prefix */
/* ------------------------------------------------------------------------- *\
prepare prefix value
\* ------------------------------------------------------------------------- */
if (sign_char)
*pf++ = sign_char;
if(prefix_base)
{
if(prefix_base == 16)
{
*pf++ = '0';
*pf++ = uppercase ? 'X' : 'x';
}
else if (prefix_base == 8)
{
if(length >= minwidth)
*pf++ = '0';
}
else if (prefix_base == 2)
{
*pf++ = '0';
*pf++ = uppercase ? 'B' : 'b';
}
}
prelen = pf - prefix;
/* ------------------------------------------------------------------------- *\
generate the output
\* ------------------------------------------------------------------------- */
if(minwidth > length)
minwidth -= length;
else
minwidth = 0;
if(fieldwidth <= (minwidth + length + prelen))
{
zRet = minwidth + length + prelen;
if(prelen)
pCB(pUserData, prefix, prelen);
if(minwidth)
{
while (minwidth > 32) /* adjust the byte length 'x' to be swapped within the output string */
{
pCB(pUserData, pzeros, 32);
minwidth -= 32;
}
pCB(pUserData, pzeros, minwidth);
}
if(length)
pCB(pUserData, pvalue, length); /* output of value at begin */
}
else
{ /* we have to add filler characters for ensuring the minimum field width */
zRet = fieldwidth;
fieldwidth -= length + minwidth + prelen; /* calculate number of required fill characters */
if(prelen && (padding == pzeros))
{
pCB(pUserData, prefix, prelen);
prelen = 0;
}
if (!left_justified)
{ /* fill in leading blanks */
while (fieldwidth > 32)
{
pCB(pUserData, padding, 32);
fieldwidth -= 32;
}
pCB(pUserData, padding, fieldwidth);
}
if(prelen)
pCB(pUserData, prefix, prelen);
if(minwidth)
{ /* fill in leading zeros */
while (minwidth > 32)
{
pCB(pUserData, pzeros, 32);
minwidth -= 32;
}
pCB(pUserData, pzeros, minwidth);
}
if(length)
pCB(pUserData, pvalue, length); /* output of value at begin */
if (left_justified)
{ /* fill in trailing blanks */
while (fieldwidth > 32)
{
pCB(pUserData, pblanks, 32);
fieldwidth -= 32;
}
pCB(pUserData, pblanks, fieldwidth);
}
}
return (zRet);
} /* size_t cbk_print_number(...) */
/* Array of the numeric bases that are related to the printf integer format characters */
static const uint8_t PrintfIntBase[256] = {10,16, 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,
/* ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
32,33,34,35, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10,16, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
/* @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ */
0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0,
/* ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ */
0, 0, 2, 0, 10, 0, 0, 0, 0,10, 0, 0, 0, 0, 0, 8, 16, 0, 0, 0, 0,10, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
/* d00 contains the decimal numbers from 00 to 99 */
static const char * d00 = "00010203040506070809" "10111213141516171819" "20212223242526272829" "30313233343536373839" "40414243444546474849"
"50515253545556575859" "60616263646566676869" "70717273747576777879" "80818283848586878889" "90919293949596979899";
/* ------------------------------------------------------------------------- *\
cbk_print_u64 prints an uint64_t using a printf callback function
and returns the written string data length.
\* ------------------------------------------------------------------------- */
static size_t cbk_print_u64(void * pUserData, /* user specific context for the callback */
PRINTF_CALLBACK * pCB, /* data write callback */
uint64_t value, /* value to write to the string */
char format, /* format character in the format string */
char sign_char, /* expected sign character */
uint8_t prefixing, /* C style prefixing of octals and hexadecimals required */
uint8_t left_justified, /* left justified value witin the output data field */
uint8_t blank_padding, /* whether to pad insuffizient left fieldwidth with blanks or zeros */
size_t precision, /* specified precision or (~(size_t) 0) if unspecified */
size_t fieldwidth) /* minimum field width */
{
size_t zRet = 0;
const char * padding = (blank_padding || (precision != ~(size_t) 0)) ? pblanks : pzeros; /* padding characters in front of the output value */
const char * digit = (format & 0x20) ? lower_digits : upper_digits; /* digits to use */
uint8_t base = PrintfIntBase[(unsigned char) format]; /* numeric base system for the output data */
size_t minwidth = (precision == ~(size_t) 0) ? 1 : precision; /* minimum width of value to print */
char buf[80]; /* buffer for temporary output of value */
char * pe = buf + sizeof(buf);
char * ps = pe;
size_t length = 0;
uint64_t x = value;
if(base < 2)
goto Exit;
if (base == 10)
{
const char * pd;
while (x >= 100)
{
uint64_t tmp = x;
x = tmp / 100;
pd = d00 + ((tmp - x * 100) * 2);
ps -= 2;
ps[0] = pd[0];
ps[1] = pd[1];
}
if(x >= 10)
{
pd = d00 + (x * 2);
ps -= 2;
ps[0] = pd[0];
ps[1] = pd[1];
}
else
*--ps = digit[x];
}
else if (base == 16)
{
if(!x)
prefixing = 0; /* no prefixing of 0x according to the C standard */
while (x >= 16)
{
*--ps = digit[x & 0xf];
x >>= 4;
}
*--ps = digit[x];
}
else if (base == 8)
{
if(!x)
prefixing = 0; /* because of 0 no prefixing required */
while (x >= 8)
{
*--ps = digit[x & 0x7];
x >>= 3;
}
*--ps = digit[x];
}
else if (base == 2)
{
if(!x)
prefixing = 0; /* no prefixing of B according to the C standard */
while (x >= 2)
{
*--ps = digit[x & 0x1];
x >>= 1;
}
*--ps = digit[x];
}
else
{
while (x >= base)
{
uint64_t tmp = x;
x /= base;
*--ps = digit[tmp - (x * base)];
}
*--ps = digit[x];
}
length = (size_t) (pe - ps);
if(!precision && !value)
length = 0; /* strange thing but required by C and Posix standards :o( */
zRet = cbk_print_number(pUserData, pCB, ps, length, padding, sign_char, prefixing ? base : 0, digit == upper_digits, left_justified, minwidth, fieldwidth);
Exit:;
return (zRet);
} /* size_t cbk_print_u64(...) */
/* ------------------------------------------------------------------------- *\
cbk_print_u32 prints an uint32_t using a printf callback function
and returns the written string data length.
\* ------------------------------------------------------------------------- */
static size_t cbk_print_u32(void * pUserData, /* user specific context for the callback */
PRINTF_CALLBACK * pCB, /* data write callback */
uint32_t value, /* value to write to the string */
char format, /* format character in the format string */
char sign_char, /* expected sign character */
uint8_t prefixing, /* C style prefixing of octals and hexadecimals required */
uint8_t left_justified, /* left justified value witin the output data field */
uint8_t blank_padding, /* whether to pad insuffizient left fieldwidth with blanks or zeros */
size_t precision, /* specified precision or (~(size_t) 0) if unspecified */
size_t fieldwidth) /* minimum field width */
{
size_t zRet = 0;
const char * padding = (blank_padding || (precision != ~(size_t) 0)) ? pblanks : pzeros; /* padding characters in front of the output value */
const char * digit = (format & 0x20) ? lower_digits : upper_digits; /* digits to use */
uint8_t base = PrintfIntBase[(unsigned char) format]; /* numeric base system of for the output data */
size_t minwidth = (precision == ~(size_t) 0) ? 1 : precision; /* minimum width of value to print */
char buf[80]; /* buffer for temporary output of value */
char * pe = buf + sizeof(buf);
char * ps = pe;
size_t length = 0;
uint32_t x = value;
if(base < 2)
goto Exit;
if (base == 10)
{
const char * pd;
while (x >= 100)
{
uint32_t tmp = x;
x = tmp / 100;
pd = d00 + ((tmp - x * 100) * 2);
ps -= 2;
ps[0] = pd[0];
ps[1] = pd[1];
}
if(x >= 10)
{
pd = d00 + (x * 2);
ps -= 2;
ps[0] = pd[0];
ps[1] = pd[1];
}
else
*--ps = digit[x];
}
else if (base == 16)
{
if(!x)
prefixing = 0; /* no prefixing of 0x according to the C standard */
while (x >= 16)
{
*--ps = digit[x & 0xf];
x >>= 4;
}
*--ps = digit[x];
}
else if (base == 8)
{
if(!x)
prefixing = 0; /* because of 0 no prefixing required */
while (x >= 8)
{
*--ps = digit[x & 0x7];
x >>= 3;
}
*--ps = digit[x];
}
else if (base == 2)
{
if(!x)
prefixing = 0; /* no prefixing of B according to the C standard */
while (x >= 2)
{
*--ps = digit[x & 0x1];
x >>= 1;
}
*--ps = digit[x];
}
else
{
while (x >= base)
{
uint32_t tmp = x;
x /= base;
*--ps = digit[tmp - (x * base)];
}
*--ps = digit[x];
}