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
|
/*
* printf.c
*
* Part of the PSXSDK C library
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SPRINTF_ALT_FLAG (1<<0)
#define SPRINTF_ZERO_FLAG (1<<1)
#define SPRINTF_NEGFIELD_FLAG (1<<2)
#define SPRINTF_SPACE_FLAG (1<<3)
#define SPRINTF_SIGN_FLAG (1<<4)
// sprintf() macros to calculate the real padding and to write it
// these were made to not repeat the code in the function
// they can only be used in sprintf()
// sprintf macros START
#define calculate_real_padding() \
y = 1; \
\
for(x=0;x<=19;x++) \
{ \
if(x == 0) \
pad_quantity--; \
else \
{ \
if(arg / y) \
pad_quantity--; \
} \
\
y *= 10; \
} \
\
if(pad_quantity < 0) pad_quantity = 0;
/*#define calculate_real_padding_hex() \
for (x = 0; x < 8; x++) \
{ \
if(x == 0) \
pad_quantity--; \
else \
{ \
if((arg >> (x * 4)) & 0xf) \
pad_quantity--; \
} \
}*/
#define calculate_real_padding_hex() \
last = 0; \
for (x = 0; x < 16; x++) \
if((arg >> (x * 4)) & 0xf) \
last = x; \
\
pad_quantity = (pad_quantity - 1) - last; \
if(pad_quantity < 0) pad_quantity = 0;
#define write_padding() \
if(!(flags & SPRINTF_NEGFIELD_FLAG)) \
for(x = 0; x < pad_quantity; x++) \
{ \
if(flags & SPRINTF_ZERO_FLAG) \
put_in_string(string, ssz, '0', string_pos++); \
else \
put_in_string(string, ssz, ' ', string_pos++); \
}
#define write_neg_padding() \
if(flags & SPRINTF_NEGFIELD_FLAG) \
{ \
for(x = 0; x < pad_quantity; x++) \
put_in_string(string, ssz, ' ', string_pos++);\
}
// sprintf macros END
enum
{
SPRINTF_SIZE_CHAR,
SPRINTF_SIZE_SHORT,
SPRINTF_SIZE_INT,
SPRINTF_SIZE_LONG,
SPRINTF_SIZE_LONG_LONG,
};
static unsigned int get_arg_in_size(int size, unsigned long long *arg, unsigned int check_sign)
{
int s = 0;
switch(size)
{
case SPRINTF_SIZE_CHAR:
*arg &= 0xff;
if(check_sign)
{
if(*arg & (1<<7))
{
*arg |= 0xffffff00;
*arg = ~(*arg - 1);
s = 1;
}
}
break;
case SPRINTF_SIZE_SHORT:
*arg &= 0xffff;
if(check_sign)
{
if(*arg & (1<<15))
{
*arg |= 0xffff0000;
*arg = ~(*arg - 1);
s = 1;
}
}
break;
// sizeof(long) == sizeof(int) on 32bit, so this will suffice for the psx
case SPRINTF_SIZE_INT:
case SPRINTF_SIZE_LONG:
*arg &= 0xffffffff;
if(check_sign)
{
if(*arg & (1<<31))
{
*arg |= (long long)0xffffffff00000000;
*arg = ~(*arg - 1);
s = 1;
}
}
break;
case SPRINTF_SIZE_LONG_LONG:
if(check_sign)
{
if(*arg & ((long long)1<<63))
{
*arg = ~(*arg - 1);
s = 1;
}
}
break;
}
return s;
}
static int libc_ulltoa(unsigned long long i, char *dst, int n, int nopad)
{
int x, y;
unsigned long long a, b;
int empty_digit = 1;
int sp=0;
int n2=0;
if(n<=0)
return 0;
for(x=18;x>=0;x--)
{
a = 1;
for(y = 0; y<x; y++)
a *= 10;
b = (i/a);
if(b>=1)
empty_digit = 0;
if(empty_digit == 0 || x == 0 || nopad == 1)
{
i -= b*a;
//put_in_string(string, ssz, b + '0', string_pos++);
if(n2!=(n-1))
{
//printf("n2=%d\n",n2);
dst[sp++] = b + '0';
n2++;
}
}
}
dst[sp] = 0;
return n2;
}
/*static void libc_float_to_string(float fl, char *dst, int n)
{
unsigned int *p = (unsigned int*)&fl;
unsigned long long i = 0;
unsigned long long f = 0;
int e, m, s;
int x, y;
unsigned long long z;
s = *p >> 31;
e = (*p >> 23) & 0xff;
m = *p & 0x7fffff;
if(e == 255 && m == 0) // Infinity
{
if(s) strncpy(dst, "-inf", n);
else strncpy(dst, "inf", n);
}else if(e == 255 && m != 0) // NaN
{
strncpy(dst, "nan", n);
}
else
{
e -= 127;
m |= 1<<23;
for(x = 23; x >= 0; x--)
{
if(m & (1<<x))
{
if(e >= 0)
{
z = 1;
for(y=0;y<e;y++)
z*=2;
i+=z;
}
else
{
z = 5000000000000000000;
for(y = 1; y < -e; y++)
z /= 2;
f+=z;
}
}
e--;
}
if(s && (n>0))
{
*(dst++) = '-';
n--;
}
x = libc_ulltoa(i, dst, n, 0);
n-=x+1;
dst+=x;
if(n>0)
{
*(dst++) = '.';
n--;
if(n>0)
{
x = libc_ulltoa(f, dst, n<6?n:6, 1);
n-=x;
dst+=x;
if(n>0)
*dst=0;
}
}
}
}*/
static void libc_double_to_string(double fl, char *dst, int n, int prec)
{
unsigned long long *p = (unsigned long long *)&fl;
unsigned long long i = 0;
unsigned long long f = 0;
unsigned long long m, s;
long long e;
int x;
unsigned long long z;
s = *p >> 63;
e = (*p >> 52) & 0x7ff;
//printf("%d\n", e);
m = *p & 0xfffffffffffff;
for(x=0;x<52;x++)
if(m&((unsigned long long)1<<(52-x))) putchar('1'); else putchar('0');
if(e == 255 && m == 0) // Infinity
{
if(s) strncpy(dst, "-inf", n);
else strncpy(dst, "inf", n);
}else if(e == 255 && m != 0) // NaN
{
strncpy(dst, "nan", n);
}
else
{
e -= 1023;
m |= (unsigned long long)1<<52;
for(x = 52; x >= 0; x--)
{
if(m & ((unsigned long long)1<<x))
{
if(e >= 0)
{
z = (long long)1<<e;
i+=z;
}
else
{
z = 5000000000000000000;
z >>= -(e + 1);
f+=z;
}
}
e--;
}
if(s && (n>0))
{
*(dst++) = '-';
n--;
}
x = libc_ulltoa(i, dst, n, 0);
n-=x+1;
dst+=x;
dprintf("N = %d\n", n);
if(n>0)
{
*(dst++) = '.';
if(n>0)
libc_ulltoa(f, dst, (n<(prec+1))?n:(prec+1), 1);
}
}
}
static char libc_sprintf_floatbuf[64];
static int __vsnprintf_internal(char *string, size_t size, const char *fmt, va_list ap, int (put_in_string(char *string, unsigned int sz, char c, int pos)))
{
int string_pos,fmt_pos;
int l;
unsigned long long arg;
char *argcp;
char *argcp_tmp;
int directive_coming = 0;
int flags = 0;
int argsize = 2; // int
int x, y;
unsigned long long a, b;
int empty_digit;
int ssz = size - 1;
int zero_flag_imp = 0;
int pad_quantity = 0;
int pad_quantity_f = -1;
int last;
if(size == 0)
ssz = 0;
l = strlen(fmt);
string_pos = 0;
for(fmt_pos=0;fmt_pos<l;fmt_pos++)
{
if(directive_coming)
{
switch(fmt[fmt_pos])
{
case '%':
put_in_string(string, ssz, '%', string_pos++);
directive_coming = 0;
break;
case ' ':
flags |= SPRINTF_SPACE_FLAG;
break;
case '#': // Specify alternate form
flags |= SPRINTF_ALT_FLAG;
break;
case '+': // Specify sign in signed conversions
flags |= SPRINTF_SIGN_FLAG;
break;
case '0': // Padding with zeros...
if(zero_flag_imp == 0)
{
flags |= SPRINTF_ZERO_FLAG;
zero_flag_imp = 1;
//printf("Zero padding enabled!\n");
}
else
{
pad_quantity *= 10;
//printf("pad_quantity = %d\n", pad_quantity);
}
break;
case '1' ... '9': // '...' cases are a GNU extension,
// but they simplify a lot
pad_quantity *= 10;
pad_quantity += fmt[fmt_pos] - '0';
zero_flag_imp = 1;
//printf("pad_quantity = %d\n", pad_quantity);
break;
case '-': // Negative field flag
if(flags & SPRINTF_ZERO_FLAG)
flags &= ~SPRINTF_ZERO_FLAG;
flags |= SPRINTF_NEGFIELD_FLAG;
break;
case '.': // Floating point precision
pad_quantity_f = pad_quantity;
pad_quantity = 0;
break;
case 'h': // Half argument size
if(argsize) argsize--;
break;
case 'l': // Double argument size
if(argsize < 2) argsize = 2;
else if(argsize < SPRINTF_SIZE_LONG_LONG) argsize++;
break;
// 'j', 't', 'z', 'q' added 2013-10-26 by nextvolume
case 'j': // Maximum integer size
argsize = SPRINTF_SIZE_LONG_LONG;
break;
case 't': // Size of ptrdiff_t (i.e. long on 32-bit, long long on 64-bit)
argsize = (sizeof(void*)==8)?
SPRINTF_SIZE_LONG_LONG:SPRINTF_SIZE_LONG;
break;
case 'z': // Size of size_t (int)
argsize = SPRINTF_SIZE_INT;
break;
case 'q': // Size of quad_t
argsize = SPRINTF_SIZE_LONG_LONG;
break;
case 'd': // signed decimal
case 'i':
empty_digit = 1;
//printf("argsize = %d\n", argsize);
if(argsize < SPRINTF_SIZE_LONG_LONG)
arg = (unsigned long long)va_arg(ap, unsigned int);
else
arg = va_arg(ap, unsigned long long);
if(flags & SPRINTF_SPACE_FLAG)
put_in_string(string, ssz, ' ', string_pos++);
if(get_arg_in_size(argsize, &arg, 1))
{
put_in_string(string, ssz, '-', string_pos++);
pad_quantity--;
}
else
{
if(flags & SPRINTF_SIGN_FLAG)
{
put_in_string(string, ssz, '+', string_pos++);
pad_quantity--;
}
}
/* Calculate how much padding we have to write */
/*y = 1;
for(x=0;x<=9;x++)
{
if(x == 0)
pad_quantity--;
else
{
if(arg / y)
pad_quantity--;
}
y *= 10;
}
if(pad_quantity < 0) pad_quantity = 0;*/
calculate_real_padding();
//printf("Actual pad quantity = %d\n", pad_quantity);
/*if(!(flags & SPRINTF_NEGFIELD_FLAG))
{
for(x = 0; x < pad_quantity; x++)
{
if(flags & SPRINTF_ZERO_FLAG)
put_in_string(string, ssz, '0', string_pos++);
else
put_in_string(string, ssz, ' ', string_pos++);
}
}*/
write_padding();
for(x=19;x>=0;x--)
{
a = 1;
for(y = 0; y<x; y++)
a *= 10;
b = (arg/a);
if(b>=1)
empty_digit = 0;
if(empty_digit == 0 || x == 0)
{
arg -= b*a;
put_in_string(string, ssz, b + '0', string_pos++);
}
}
/*if(flags & SPRINTF_NEGFIELD_FLAG)
{
for(x = 0; x < pad_quantity; x++)
put_in_string(string, ssz, ' ', string_pos++);
}*/
write_neg_padding();
directive_coming = 0;
break;
case 'u': // unsigned decimal
empty_digit = 1;
if(argsize < SPRINTF_SIZE_LONG_LONG)
arg = (unsigned long long)va_arg(ap, unsigned int);
else
arg = va_arg(ap, unsigned long long);
get_arg_in_size(argsize, &arg, 0);
calculate_real_padding();
write_padding();
for(x=19;x>=0;x--)
{
a = 1;
for(y = 0; y<x; y++)
a *= 10;
b = (arg/a);
if(b>=1)
empty_digit = 0;
if(empty_digit == 0 || x == 0)
{
arg -= b*a;
put_in_string(string, ssz, b + '0', string_pos++);
}
}
write_neg_padding();
directive_coming = 0;
break;
case 'x': // Hexadecimal
case 'X': // Hexadecimal with big letters
case 'p': // Hexadecimal with small letters with '0x' prefix
empty_digit = 1;
if(argsize < SPRINTF_SIZE_LONG_LONG)
arg = (unsigned long long)va_arg(ap, unsigned int);
else
arg = va_arg(ap, unsigned long long int);
get_arg_in_size(argsize, &arg, 0);
if(fmt_pos == 'p')
flags |= SPRINTF_ALT_FLAG;
if(flags & SPRINTF_ALT_FLAG)
{
put_in_string(string, ssz, '0', string_pos++);
if(fmt[fmt_pos] == 'X')
put_in_string(string, ssz, 'X', string_pos++);
else
put_in_string(string, ssz, 'x', string_pos++);
}
calculate_real_padding_hex();
write_padding();
for(x=15;x>=0;x--)
{
y = arg >> (x << 2);
y &= 0xf;
if(y>=1)
empty_digit = 0;
if(empty_digit == 0 || x == 0)
{
if(y>=0 && y<=9)
put_in_string(string, ssz, y + '0', string_pos++);
else if(y>=0xA && y<=0xF)
{
if(fmt[fmt_pos] == 'X')
put_in_string(string, ssz, (y - 0xa) + 'A', string_pos++);
else
put_in_string(string, ssz, (y - 0xa) + 'a', string_pos++);
}
}
}
write_neg_padding();
directive_coming = 0;
break;
case 'c': // character
arg = va_arg(ap, int);
put_in_string(string, ssz, arg & 0xff, string_pos++);
directive_coming = 0;
break;
case 's': // string
argcp = va_arg(ap, char *);
argcp_tmp = argcp;
if(argcp == NULL)
{
// Non standard extension, but supported by Linux and the BSDs.
put_in_string(string, ssz, '(', string_pos++);
put_in_string(string, ssz, 'n', string_pos++);
put_in_string(string, ssz, 'u', string_pos++);
put_in_string(string, ssz, 'l', string_pos++);
put_in_string(string, ssz, 'l', string_pos++);
put_in_string(string, ssz, ')', string_pos++);
directive_coming = 0;
break;
}
while(*argcp_tmp)
{
if(pad_quantity > 0) pad_quantity--;
argcp_tmp++;
}
if(!(flags & SPRINTF_NEGFIELD_FLAG))
{
while(pad_quantity > 0)
{
put_in_string(string,ssz, ' ', string_pos++);
pad_quantity--;
}
}
while(*argcp)
{
put_in_string(string, ssz, *argcp, string_pos++);
argcp++;
}
if(flags & SPRINTF_NEGFIELD_FLAG)
{
while(pad_quantity > 0)
{
put_in_string(string,ssz, ' ', string_pos++);
pad_quantity--;
}
}
directive_coming = 0;
break;
case 'o': // Octal
empty_digit = 1;
if(argsize < SPRINTF_SIZE_LONG_LONG)
arg = (unsigned long long)va_arg(ap, unsigned int);
else
arg = va_arg(ap, unsigned long long);
for(x=21;x>=0;x--)
{
y = arg >> (x * 3);
y &= 0x7;
if(y>=1)
empty_digit = 0;
if(empty_digit == 0 || x == 0)
put_in_string(string, ssz, y + '0', string_pos++);
}
directive_coming = 0;
break;
case '@': // Binary
empty_digit = 1;
if(argsize < SPRINTF_SIZE_LONG_LONG)
arg = (unsigned long long)va_arg(ap, unsigned int);
else
arg = va_arg(ap, unsigned long long);
for(x=63;x>=0;x--)
{
y = (arg >> x);
y &= 1;
if(y>=1)
empty_digit = 0;
if(empty_digit == 0 || x == 0)
put_in_string(string, ssz, y + '0', string_pos++);
}
directive_coming = 0;
break;
case 'f':
if(pad_quantity_f == -1)
pad_quantity_f = 6;
else
{
x = pad_quantity_f;
pad_quantity_f = pad_quantity;
pad_quantity = x;
}
dprintf("PRECISION = %d\n", pad_quantity_f);
libc_double_to_string(va_arg(ap, double), libc_sprintf_floatbuf, 64, pad_quantity_f);
// calculate padding
pad_quantity -= strlen(libc_sprintf_floatbuf);
write_padding();
for(x=0;libc_sprintf_floatbuf[x]!=0;x++)
put_in_string(string, ssz, libc_sprintf_floatbuf[x], string_pos++);
write_neg_padding();
directive_coming = 0;
break;
case 'n': // Number of characters written
*(va_arg(ap,unsigned int*)) = string_pos;
directive_coming = 0;
break;
default:
put_in_string(string, ssz, fmt[fmt_pos], string_pos++);
directive_coming = 0;
}
}
else
{
if(fmt[fmt_pos] == '%')
{
directive_coming = 1;
flags = 0;
argsize = 2;
pad_quantity = 0;
pad_quantity_f = -1;
zero_flag_imp = 0;
}
else
{
put_in_string(string, ssz, fmt[fmt_pos], string_pos++);
}
}
}
/*if(((size-1) < string_pos) && (size>0))
string[size - 1] = 0;
else
string[string_pos] = 0;*/
put_in_string(string, ssz, '\0', string_pos);
return string_pos;
}
static int vsnprintf_put_in_string(char *string, unsigned int sz, char c, int pos)
{
if(pos >= sz && c)
return 0;
else if (c || pos <= sz)
string[pos] = c;
return 1;
}
int vsnprintf(char *string, size_t size, const char *fmt, va_list ap)
{
return __vsnprintf_internal(string, size, fmt, ap, vsnprintf_put_in_string);
}
static int sio_put_in_string(char *string, unsigned int sz, char c, int pos)
{
sio_putchar(c);
return 1;
}
int sio_vprintf(const char *fmt, va_list ap)
{
return __vsnprintf_internal(NULL, -1, fmt, ap, sio_put_in_string);
}
static int out_put_in_string(char *string, unsigned int sz, char c, int pos)
{
putchar(c);
return 1;
}
int vprintf(const char *fmt, va_list ap)
{
return __vsnprintf_internal(NULL, -1, fmt, ap, out_put_in_string);
}
int vsprintf(char *string, const char *fmt, va_list ap)
{
return vsnprintf(string, 0xffffffff, fmt, ap);
}
int fprintf(FILE *const fd, const char *fmt, ...)
{
if (fd == stdout || fd == stderr)
{
int r;
va_list ap;
va_start(ap, fmt);
r = vprintf(fmt, ap);
va_end(ap);
return r;
}
return -1;
}
int sprintf(char *string, const char *fmt, ...)
{
int r;
va_list ap;
va_start(ap, fmt);
r = vsprintf(string, fmt, ap);
va_end(ap);
return r;
}
int snprintf(char *string, size_t size, const char *fmt, ...)
{
int r;
va_list ap;
va_start(ap, fmt);
r = vsnprintf(string, size, fmt, ap);
va_end(ap);
return r;
}
int sio_printf(const char *fmt, ...)
{
int r;
va_list ap;
va_start(ap, fmt);
r = sio_vprintf(fmt, ap);
va_end(ap);
return r;
}
|