Repository navigation
Expand file tree
/
Copy pathFloatLib.cpp
More file actions
1008 lines (880 loc) · 22.3 KB
/
Copy pathFloatLib.cpp
File metadata and controls
1008 lines (880 loc) · 22.3 KB
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
//
// FloatLib.c
//
//
// Created by Slice on 20.06.2018.
//
//#include <Platform.h>
#include "FloatLib.h"
#include "EfiCommon.h"
#include <stdlib.h> // Добавьте эту строку в начало файла для определения malloc
#include <string.h> // Для определения memcpy
//#define fabsf(x) ((x >= 0.0f)?x:(-x))
//#define fabsf(x) FabsF(x)
//we will assume sqrt(abs(x))
float SqrtF(float X)
{
/*
struct FloatInt {
union {
INT32 i;
float f;
} fi;
};
*/
if (X == 0.0f) {
return 0.0f;
} else if (X < 0.0f) {
X = -X;
}
// struct FloatInt Y;
float Yf;
Yf = X * 0.3f;
// Y.i = Y.i >> 1; // dirty hack - first iteration
//do six iterations
Yf = Yf * 0.5f + X / (Yf * 2.0f);
Yf = Yf * 0.5f + X / (Yf * 2.0f);
Yf = Yf * 0.5f + X / (Yf * 2.0f);
Yf = Yf * 0.5f + X / (Yf * 2.0f);
Yf = Yf * 0.5f + X / (Yf * 2.0f);
Yf = Yf * 0.5f + X / (Yf * 2.0f);
return Yf;
}
float CosF(float X);
//we know sin is odd
float SinF(float X)
{
INTN Period;
float X2;
float Sign = 1.0f;
if (X < 0.0f) {
X = -X;
Sign = -1.0f;
}
Period = (INTN)(X / PI2);
X = X - Period * PI2;
if (X > PI) {
X = X - PI;
Sign *= -1.0f;
}
if (X > PI5) {
X = PI - X;
}
if (X > PI * 0.25f) {
return (Sign*CosF(PI5 - X));
}
X2 = X * X;
return (Sign*(X - X2 * X / 6.0f + X2 * X2 * X / 120.0f));
}
//we know cos is even
float CosF(float X)
{
INTN Period;
float Sign = 1.0f;
float X2;
if (X < 0.0f) {
X = -X;
}
Period = (INTN)(X / PI2);
X = X - Period * PI2;
if (X > PI) {
X = PI - X;
Sign = -1.0f;
}
if (X > PI5) {
X = PI - X;
Sign *= -1.0f;
}
if (X > PI * 0.25f) {
return (Sign*SinF(PI5 - X));
}
X2 = X * X;
return (Sign * (1.0f - X2 * 0.5f + X2 * X2 / 24.0f));
}
float TanF(float X)
{
float Y = CosF(X);
if (Y == 0.0f) {
Y = 1.0e-37f;
}
return SinF(X)/Y;
}
float PowF(float x, INTN n)
{
float Data = x;
if (n > 0) {
while (n > 0) {
Data *= 10.0f;
n--;
}
} else {
while (n < 0) {
Data *= 0.1f;
n++;
}
}
return Data;
}
float CeilF(float X)
{
INT32 I = (INT32)X;
return (float)(++I);
}
float FloorF(float X)
{
INT32 I = (INT32)X;
return (float)I;
}
float ModF(float X, float Y)
{
if (Y == 0.0f) return 0.0f;
INT32 I = (INT32)(X / Y);
return (X - (float)I * Y);
}
float AcosF(float X)
{
float X2 = X * X;
float res = 0.f, Y = 0.f;
INTN Sign = 0;
if (X2 < 0.3f) {
Y = X * (1.0f + X2 / 6.0f + X2 * X2 * (3.0f / 40.0f));
return (PI5 - Y);
} else if (X2 >= 1.0f) {
return 0.0f;
} else {
if (X < 0) {
X = -X;
Sign = 1;
}
Y = 1.0f - X; //for X ~ 1
X2 = Y * (2.0f + Y * (1.0f / 3.0f + Y * (4.0f / 45.0f + Y / 35.0f))); //Dwight, form.508
res = SqrtF(X2);
if (Sign) {
res = PI - res;
}
}
return res;
}
float AtanF(float X) //assume 0.0 < X < 1.0
{
float Eps = 1.0e-8f;
int i = 1;
float X2 = X * X;
float D = X;
float Y = 0.f;
float sign = 1.0f;
if (X > 0.5f) {
//make here arctg(1-x)
X = 1.0f - D;
X2 = X * X;
Y = PI4 - X * 0.5f - X2 * 0.25f - X * X2 * 0.25f * ( 1.f / 3.f - X2 * (0.1f + X / 12.f + X2 / 28.f));
} else {
// Y = X * (1 - X2 * ( 1.0f / 3.0f - X2 * (1.0f / 5.0f - X2 * ( 1.0f / 7.0f))));
for (i = 1; i < 50; i += 2) {
Y += (D * sign / i);
D *= X2;
if (D < Eps) {
break;
}
sign = - sign;
}
}
return Y;
}
float Atan2F(float Y, float X) //result -pi..+pi
{
float sign = (((X >= 0.0f) && (Y < 0.0f)) ||
((X < 0.0f) && (Y >= 0.0f)))?-1.0f:1.0f;
float PP = 0.f;
float res = 0.f;
//1,1 = pi4 1,-1=pi34 -1,-1=-pi34 -1,1=-pi4
if (X < 0.f) {
PP = PI;
}
X = (X >= 0.0f)?X:(-X);
Y = (Y >= 0.0f)?Y:(-Y);
if (Y < X) {
res = AtanF(Y / X);
} else if (X == 0.0f) {
res = PI5;
} else {
res = (PI5 - AtanF(X / Y));
}
return sign * (res - PP);
}
RETURN_STATUS
AsciiStrToFloat(IN CONST CHAR8 *String,
OUT CHAR8 **EndPointer, OPTIONAL
OUT float *Data)
{
UINTN Temp = 0;
INTN Sign = 1;
float Mantissa, Ftemp;
CHAR8* TmpStr = NULL;
RETURN_STATUS Status = RETURN_SUCCESS;
if (EndPointer != NULL) {
*EndPointer = (CHAR8 *) String;
}
//
// Ignore the pad spaces (space or tab)
//
while ((*String == ' ') || (*String == '\t')) {
String++;
}
if (*String == '-') {
Sign = -1;
String++;
} else if (*String == '+') {
String++;
}
Status = AsciiStrDecimalToUintnS(String, &TmpStr, &Temp);
Mantissa = (float)Temp;
String = TmpStr;
if (*String == '.') {
String++;
Temp = 0;
Status = AsciiStrDecimalToUintnS(String, &TmpStr, &Temp);
Ftemp = (float)Temp;
while (String != TmpStr) {
if (*String == '\0') {
break;
}
Ftemp *= 0.1f;
String++;
}
Mantissa += Ftemp;
}
if ((*String == 'E') || (*String == 'e')){
INTN ExpSign = 1;
String++;
if (*String == '-') {
ExpSign = -1;
String++;
} else if (*String == '+') {
String++;
}
Temp = 0;
Status = AsciiStrDecimalToUintnS(String, &TmpStr, &Temp);
if ( Temp > MAX_INTN ) return RETURN_UNSUPPORTED;
if (Status == RETURN_SUCCESS) {
Ftemp = PowF(10.0f, ExpSign * (INTN)Temp); // cast to avoid warning
Mantissa *= Ftemp;
}
}
*Data = (Sign > 0)?Mantissa:-Mantissa;
if (EndPointer != NULL) {
*EndPointer = (CHAR8 *) TmpStr;
}
return RETURN_SUCCESS;
}
//Slice my qsort implementation.
void QuickSortWorker(UINT8* Array, INTN Low, INTN High, INTN Size,
int(*compare)(CONST void* a, CONST void* b), void* Temp)
{
INTN i = Low, j = High;
// СОЗДАЕМ ОТДЕЛЬНЫЙ БУФЕР ДЛЯ МЕДИАНЫ
UINT8* medBuffer = (UINT8*)AllocatePool(Size);
if (!medBuffer) return;
UINT8* MedPtr = Array + ((Low + High) / 2) * Size; // Central element, just pointer
// КОПИРУЕМ ЗНАЧЕНИЕ центрального элемента
CopyMem(medBuffer, MedPtr, Size);
// Sort around center
while (i <= j)
{
while (compare((const void*)(Array + i * Size), medBuffer) == -1) ++i;
while (compare((const void*)(Array + j * Size), medBuffer) == 1) --j;
// Change
if (i <= j) {
CopyMem(Temp, (Array + i * Size), Size);
CopyMem((Array + i * Size), (Array + j * Size), Size);
CopyMem((Array + j * Size), Temp, Size);
i++;
j--;
}
}
FreePool(medBuffer);
if (j > Low) QuickSortWorker(Array, Low, j, Size, compare, Temp);
if (High > i) QuickSortWorker(Array, i, High, Size, compare, Temp);
}
void QuickSort(void* Array, INTN Number, INTN Size, int(*compare)(CONST void* a, CONST void* b))
{
void* Buffer = AllocatePool(Size);
QuickSortWorker((UINT8*)Array, 0, Number - 1, Size, compare, Buffer);
FreePool(Buffer);
}
//float hypot(float x, float y) { return SqrtF(x*x + y*y); }
static UINT32 seed = 12345;
float rndf() //expected 0..1
{
// UINT16 Rand = 0;
// AsmRdRand16(&Rand); //it's a pity panic
// return (float)Rand / 65536.f;
seed = seed * 214013 + 2531011;
float x = (float)seed / 4294967296.0f;
return x;
}
int dither(float x, int level)
{
if (!level) {
return (int)x;
}
int i = (int)(x) * level; //5.1 * 4 = 20.4, 5.8 * 4 = 23.2|i=20
float dx = x * level - (float)(i); //0.4, 3.2
i /= level;
if (dx > rndf() * level) {
i += (int)((0.9999f+rndf())*level); //because rndf has mean value 0.5, but (int)rnd=0
}
return i;
}
//there is
#if 0
XBool
EFIAPI
AsmRdRand16 (
OUT UINT16 *Rand
);
#endif
UINTN
EFIAPI
AsciiStrHexToUintn(
IN CONST CHAR8 *String)
{
UINTN Result = 0;
RETURN_STATUS Status;
Status = AsciiStrHexToUintnS(String, (CHAR8 **)NULL, &Result);
if (Status != RETURN_SUCCESS)
{
return 0;
}
return Result;
}
RETURN_STATUS
EFIAPI
AsciiStrHexToUintnS(
IN CONST CHAR8 *String,
OUT CHAR8 **EndPointer, OPTIONAL OUT UINTN *Data)
{
//
// 1. Neither String nor Data shall be a null pointer.
//
SAFE_STRING_CONSTRAINT_CHECK((String != NULL), RETURN_INVALID_PARAMETER);
SAFE_STRING_CONSTRAINT_CHECK((Data != NULL), RETURN_INVALID_PARAMETER);
//
// 2. The length of String shall not be greater than ASCII_RSIZE_MAX.
//
// if (ASCII_RSIZE_MAX != 0) {
// SAFE_STRING_CONSTRAINT_CHECK ((AsciiStrnLenS (String, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
// }
if (EndPointer != NULL)
{
*EndPointer = (CHAR8 *)String;
}
//
// Ignore the pad spaces (space or tab)
//
while ((*String == ' ') || (*String == '\t'))
{
String++;
}
//
// Ignore leading Zeros after the spaces
//
while (*String == '0')
{
String++;
}
if (AsciiCharToUpper(*String) == 'X')
{
if (*(String - 1) != '0')
{
*Data = 0;
return RETURN_SUCCESS;
}
//
// Skip the 'X'
//
String++;
}
*Data = 0;
while (InternalAsciiIsHexaDecimalDigitCharacter(*String))
{
*Data = (*Data << 4) + InternalAsciiHexCharToUintn(*String);
String++;
}
if (EndPointer != NULL)
{
*EndPointer = (CHAR8 *)String;
}
return RETURN_SUCCESS;
}
BOOLEAN
EFIAPI
InternalIsDecimalDigitCharacter(
IN CHAR16 Char)
{
return (BOOLEAN)((Char >= L'0') && (Char <= L'9'));
}
BOOLEAN
EFIAPI
InternalAsciiIsHexaDecimalDigitCharacter(
IN CHAR8 Char
)
{
return (BOOLEAN)(InternalAsciiIsDecimalDigitCharacter(Char) ||
((Char >= 'A') && (Char <= 'F')) ||
((Char >= 'a') && (Char <= 'f')));
}
BOOLEAN
EFIAPI
InternalAsciiIsDecimalDigitCharacter(
IN CHAR8 Char
)
{
return (BOOLEAN)((Char >= '0') && (Char <= '9'));
}
CHAR8
EFIAPI
AsciiCharToUpper(
IN CHAR8 Chr)
{
return (UINT8)(((Chr >= 'a') && (Chr <= 'z')) ? Chr - ('a' - 'A') : Chr);
}
/**
Convert a ASCII character to numerical value.
This internal function only deal with Unicode character
which maps to a valid hexadecimal ASII character, i.e.
'0' to '9', 'a' to 'f' or 'A' to 'F'. For other
ASCII character, the value returned does not make sense.
@param Char The character to convert.
@return The numerical value converted.
**/
UINTN
EFIAPI
InternalAsciiHexCharToUintn(
IN CHAR8 Char)
{
if (InternalIsDecimalDigitCharacter(Char))
{
return Char - '0';
}
return (10 + AsciiCharToUpper(Char) - 'A');
}
XBool IsHexDigit(char c)
{
return (IS_DIGIT(c) || (IS_HEX(c))) ? true : false;
}
UINTN
EFIAPI
AsciiStrDecimalToUintn(
IN CONST CHAR8 *String)
{
UINTN Result = 0;
RETURN_STATUS Status;
Status = AsciiStrDecimalToUintnS(String, (CHAR8 **)NULL, &Result);
if (Status != RETURN_SUCCESS)
{
return 0;
}
return Result;
}
RETURN_STATUS
EFIAPI
AsciiStrDecimalToUintnS(
IN CONST CHAR8 *String,
OUT CHAR8 **EndPointer, OPTIONAL OUT UINTN *Data)
{
//
// 1. Neither String nor Data shall be a null pointer.
//
SAFE_STRING_CONSTRAINT_CHECK((String != NULL), RETURN_INVALID_PARAMETER);
SAFE_STRING_CONSTRAINT_CHECK((Data != NULL), RETURN_INVALID_PARAMETER);
//
// 2. The length of String shall not be greater than ASCII_RSIZE_MAX.
//
// if (ASCII_RSIZE_MAX != 0) {
// SAFE_STRING_CONSTRAINT_CHECK ((AsciiStrnLenS (String, ASCII_RSIZE_MAX + 1) <= ASCII_RSIZE_MAX), RETURN_INVALID_PARAMETER);
// }
if (EndPointer != NULL)
{
*EndPointer = (CHAR8 *)String;
}
//
// Ignore the pad spaces (space or tab)
//
while ((*String == ' ') || (*String == '\t'))
{
String++;
}
//
// Ignore leading Zeros after the spaces
//
while (*String == '0')
{
String++;
}
*Data = 0;
while (InternalAsciiIsDecimalDigitCharacter(*String))
{
*Data = *Data * 10 + (*String - '0');
String++;
}
if (EndPointer != NULL)
{
*EndPointer = (CHAR8 *)String;
}
return RETURN_SUCCESS;
}
/*
Translate VT-UTF8 characters into one Unicode character.
UTF8 Encoding Table
Bits per Character | Unicode Character Range | Unicode Binary Encoding | UTF8 Binary Encoding
0-7 | 0x0000 - 0x007F | 00000000 0xxxxxxx | 0xxxxxxx
8-11 | 0x0080 - 0x07FF | 00000xxx xxxxxxxx | 110xxxxx 10xxxxxx
12-16 | 0x0800 - 0xFFFF | xxxxxxxx xxxxxxxx | 1110xxxx 10xxxxxx 10xxxxxx
$ U+0024 10 0100 00100100 24
¢ U+00A2 1010 0010 11000010 10100010 C2 A2
€ U+20AC 0010 0000 1010 1100 11100010 10000010 10101100 E2 82 AC
𐍈 U+10348 1 0000 0011 0100 1000 11110000 10010000 10001101 10001000 F0 90 8D 88
*/
CHAR8 *GetUnicodeChar(CHAR8 *s, CHAR16 *UnicodeChar)
{
char ValidBytes = 0;
UINT8 Byte0, Byte1, Byte2;
UINT8 UnicodeByte0, UnicodeByte1;
CHAR16 A = L'\0';
if (*s == '&')
{
if (AsciiStrStr(s, "&#x") != 0)
{
s += 3;
while (IS_HEX(*s) || IS_DIGIT(*s))
{
A <<= 4;
if (IS_DIGIT(*s))
{
A += *s - 0x30;
}
else if (IS_UPPER(*s))
{
A += *s - 0x41 + 10;
}
else
{
A += *s - 0x61 + 10;
}
s++;
}
}
else if (AsciiStrStr(s, "&") != 0)
{
A = 0x26; //&
s += 5;
}
else if (AsciiStrStr(s, """) != 0)
{
A = 0x22; //"
s += 6;
}
else if (AsciiStrStr(s, "<") != 0)
{
A = 0x3C; //<
s += 4;
}
else if (AsciiStrStr(s, ">") != 0)
{
A = 0x3E; //>
s += 4;
}
else if (AsciiStrStr(s, " ") != 0)
{
A = 0xA0; //>
s += 6;
}
*UnicodeChar = A;
}
else
{
if ((*s & 0x80) == 0)
{
ValidBytes = 1;
}
else if ((*s & 0xe0) == 0xc0)
{
ValidBytes = 2;
}
else if ((*s & 0xf0) == 0xe0)
{
ValidBytes = 3;
}
switch (ValidBytes)
{
case 1:
//
// one-byte utf8 code
//
*UnicodeChar = (UINT16)(*s++);
break;
case 2:
//
// two-byte utf8 code
//
Byte1 = *s++; // c2
Byte0 = *s++; // a2
UnicodeByte0 = (UINT8)((Byte1 << 6) | (Byte0 & 0x3f));
UnicodeByte1 = (UINT8)((Byte1 >> 2) & 0x07);
*UnicodeChar = (UINT16)(UnicodeByte0 | (UnicodeByte1 << 8));
break;
case 3:
//
// three-byte utf8 code
// sample E3 90 A1 = 0x3421
//
Byte2 = *s++;
Byte1 = *s++;
Byte0 = *s++;
UnicodeByte0 = (UINT8)((Byte1 << 6) | (Byte0 & 0x3f));
UnicodeByte1 = (UINT8)((Byte2 << 4) | ((Byte1 >> 2) & 0x0f));
*UnicodeChar = (UINT16)(UnicodeByte0 | (UnicodeByte1 << 8));
default:
break;
}
}
return s;
}
/**
Copies the string pointed to by Source (including the terminating null char)
to the array pointed to by Destination.
This function is similar as strcpy_s defined in C11.
If an error would be returned, then the function will also ASSERT().
If an error is returned, then the Destination is unmodified.
@param Destination A pointer to a Null-terminated Ascii string.
@param DestMax The maximum number of Destination Ascii
char, including terminating null char.
@param Source A pointer to a Null-terminated Ascii string.
@retval RETURN_SUCCESS String is copied.
@retval RETURN_BUFFER_TOO_SMALL If DestMax is NOT greater than StrLen(Source).
@retval RETURN_INVALID_PARAMETER If Destination is NULL.
If Source is NULL.
If PcdMaximumAsciiStringLength is not zero,
and DestMax is greater than
PcdMaximumAsciiStringLength.
If DestMax is 0.
@retval RETURN_ACCESS_DENIED If Source and Destination overlap.
**/
RETURN_STATUS
EFIAPI
AsciiStrCpyS(
OUT CHAR8 *Destination,
IN UINTN DestMax,
IN CONST CHAR8 *Source)
{
//
// 1. Neither Destination nor Source shall be a null pointer.
//
SAFE_STRING_CONSTRAINT_CHECK((Destination != NULL), RETURN_INVALID_PARAMETER);
SAFE_STRING_CONSTRAINT_CHECK((Source != NULL), RETURN_INVALID_PARAMETER);
SAFE_STRING_CONSTRAINT_CHECK((DestMax != 0), RETURN_INVALID_PARAMETER);
while ((*Source != 0) && (--DestMax > 0))
{
*(Destination++) = *(Source++);
}
*Destination = 0;
return RETURN_SUCCESS;
}
/**
Appends a copy of the string pointed to by Source (including the terminating
null char) to the end of the string pointed to by Destination.
This function is similar as strcat_s defined in C11.
If an error would be returned, then the function will also ASSERT().
If an error is returned, then the Destination is unmodified.
@param Destination A pointer to a Null-terminated Ascii string.
@param DestMax The maximum number of Destination Ascii
char, including terminating null char.
@param Source A pointer to a Null-terminated Ascii string.
@retval RETURN_SUCCESS String is appended.
@retval RETURN_BAD_BUFFER_SIZE If DestMax is NOT greater than
StrLen(Destination).
@retval RETURN_BUFFER_TOO_SMALL If (DestMax - StrLen(Destination)) is NOT
greater than StrLen(Source).
@retval RETURN_INVALID_PARAMETER If Destination is NULL.
If Source is NULL.
If PcdMaximumAsciiStringLength is not zero,
and DestMax is greater than
PcdMaximumAsciiStringLength.
If DestMax is 0.
@retval RETURN_ACCESS_DENIED If Source and Destination overlap.
**/
RETURN_STATUS
EFIAPI
AsciiStrCatS(
IN OUT CHAR8 *Destination,
IN UINTN DestMax,
IN CONST CHAR8 *Source)
{
UINTN DestLen;
UINTN CopyLen;
DestLen = AsciiStrnLenS(Destination, DestMax);
CopyLen = DestMax - DestLen;
//
// 1. Neither Destination nor Source shall be a null pointer.
//
SAFE_STRING_CONSTRAINT_CHECK((Destination != NULL), RETURN_INVALID_PARAMETER);
SAFE_STRING_CONSTRAINT_CHECK((Source != NULL), RETURN_INVALID_PARAMETER);
SAFE_STRING_CONSTRAINT_CHECK((DestMax != 0), RETURN_INVALID_PARAMETER);
SAFE_STRING_CONSTRAINT_CHECK((CopyLen != 0), RETURN_BAD_BUFFER_SIZE);
Destination = Destination + DestLen;
while ((*Source != 0) && (--CopyLen > 0))
{
*(Destination++) = *(Source++);
}
*Destination = 0;
return RETURN_SUCCESS;
}
UINTN
EFIAPI
AsciiStrnLenS(
IN CONST CHAR8 *String,
IN UINTN MaxSize)
{
UINTN Length;
//
// If String is a null pointer, then the AsciiStrnLenS function returns zero.
//
if (String == NULL)
{
return 0;
}
//
// Otherwise, the AsciiStrnLenS function returns the number of characters that precede the
// terminating null character. If there is no null character in the first MaxSize characters of
// String then AsciiStrnLenS returns MaxSize. At most the first MaxSize characters of String shall
// be accessed by AsciiStrnLenS.
//
Length = 0;
while (String[Length] != 0)
{
if (Length >= MaxSize - 1)
{
return MaxSize;
}
Length++;
}
return Length;
}
// assume len = number of UINT8 values while hex is string of hex values, e.g. "0A 1B 2C 3D" or "0A,1B,2C,3D" or "0A1B2C3D"
UINT32 hex2bin(IN const CHAR8 *hex, OUT UINT8 *bin, UINT32 len)
{
CHAR8 *p;
UINT32 i, outlen = 0;
CHAR8 buf[3];
if (hex == NULL || bin == NULL || len <= 0 || AsciiStrLen(hex) < len * 2) {
// DBG("[ERROR] bin2hex input error\n"); //this is not error, this is empty value
return false;
}
buf[2] = '\0';
p = (CHAR8 *) hex;
for (i = 0; i < len; i++)
{
while ( *p == 0x20 || *p == ',' || *p == '\n' || *p == '\r' ) {
p++; //skip spaces and commas
}
if (*p == 0) {
break;
}
if (!IsHexDigit(p[0]) || !IsHexDigit(p[1])) {
//MsgLog("[ERROR] bin2hex '%s' syntax error\n", hex);
return 0;
}
buf[0] = *p++;
buf[1] = *p++;
bin[i] = hexstrtouint8(buf);
outlen++;
}
//bin[outlen] = 0;
return outlen;
}
/**
Returns the first occurrence of a Null-terminated ASCII sub-string
in a Null-terminated ASCII string.
This function scans the contents of the ASCII string specified by String
and returns the first occurrence of SearchString. If SearchString is not
found in String, then NULL is returned. If the length of SearchString is zero,
then String is returned.
If String is NULL, then ASSERT().
If SearchString is NULL, then ASSERT().
If PcdMaximumAsciiStringLength is not zero, and SearchString or
String contains more than PcdMaximumAsciiStringLength Unicode characters
not including the Null-terminator, then ASSERT().
@param String A pointer to a Null-terminated ASCII string.
@param SearchString A pointer to a Null-terminated ASCII string to search for.
@retval NULL If the SearchString does not appear in String.
@retval others If there is a match return the first occurrence of SearchingString.
If the length of SearchString is zero,return String.
**/
CHAR8*
EFIAPI
AsciiStrStr(
IN CONST CHAR8* String,
IN CONST CHAR8* SearchString
)
{
CONST CHAR8* FirstMatch;
CONST CHAR8* SearchStringTmp;
if (!String || !SearchString) {
return NULL;
}
if (*SearchString == '\0') {
return (CHAR8*)String;
}
while (*String != '\0') {
SearchStringTmp = SearchString;
FirstMatch = String;
while ((*String == *SearchStringTmp)
&& (*String != '\0')) {
String++;
SearchStringTmp++;
}
if (*SearchStringTmp == '\0') {
return (CHAR8*)FirstMatch;
}
if (*String == '\0') {
return NULL;
}
String = FirstMatch + 1;
}
return NULL;
}
UINTN
EFIAPI
AsciiStrLen(
IN CONST CHAR8* String
)
{
UINTN Length;
if (!String) {
return 0;
}
for (Length = 0; *String != '\0'; String++, Length++) {
if (Length == 100000000ull) {
break;
}
}
return Length;
}
UINT8 hexstrtouint8(const CHAR8* buf)
{
char i = 0;
if (IS_DIGIT(buf[0]))
i = buf[0] - '0';
else if (IS_HEX(buf[0]))
i = (buf[0] | 0x20) - 'a' + 10;
if (strlen(buf) == 1) {
return i;
}