forked from RoelKroes/TBTracker-ESP32
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasn1crt_encoding.c
More file actions
1464 lines (1176 loc) · 33.9 KB
/
asn1crt_encoding.c
File metadata and controls
1464 lines (1176 loc) · 33.9 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
#include <string.h>
#include "assert_override.h"
#include <math.h>
#include <float.h>
#include "asn1crt_encoding.h"
const byte masks[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
const byte masksb[] = { 0x0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF };
const asn1SccUint32 masks2[] = { 0x0,
0xFF,
0xFF00,
0xFF0000,
0xFF000000 };
flag OctetString_equal(int len1, int len2, const byte arr1[], const byte arr2[])
{
return (len1 == len2) && (memcmp(arr1, arr2, len1) == 0);
}
/***********************************************************************************************/
/* Byte Stream Functions */
/***********************************************************************************************/
asn1SccSint ByteStream_GetLength(ByteStream* pStrm)
{
return pStrm->currentByte;
}
/***********************************************************************************************/
/* Bit Stream Functions */
/***********************************************************************************************/
void BitStream_Init(BitStream* pBitStrm, unsigned char* buf, long count)
{
pBitStrm->count = count;
pBitStrm->buf = buf;
memset(pBitStrm->buf, 0x0, (size_t)count);
pBitStrm->currentByte = 0;
pBitStrm->currentBit = 0;
pBitStrm->pushDataPrm = NULL;
pBitStrm->fetchDataPrm = NULL;
}
void BitStream_AttachBuffer(BitStream* pBitStrm, unsigned char* buf, long count)
{
pBitStrm->count = count;
pBitStrm->buf = buf;
pBitStrm->currentByte = 0;
pBitStrm->currentBit = 0;
pBitStrm->pushDataPrm = NULL;
pBitStrm->fetchDataPrm = NULL;
}
void BitStream_AttachBuffer2(BitStream* pBitStrm, unsigned char* buf, long count, void* pushDataPrm, void* fetchDataPrm)
{
BitStream_AttachBuffer(pBitStrm, buf, count);
pBitStrm->pushDataPrm = pushDataPrm;
pBitStrm->fetchDataPrm = fetchDataPrm;
}
asn1SccSint BitStream_GetLength(BitStream* pBitStrm)
{
int ret = pBitStrm->currentByte;
if (pBitStrm->currentBit)
ret++;
return ret;
}
/*
Append bit one.
Example
cur bit = 3
x x x |
|_|_|_|_|_|_|_|_|
0 1 2 3 4 5 6 7
xxxy????
or 00010000
------------
xxx1????
*/
void BitStream_AppendBitOne(BitStream* pBitStrm)
{
pBitStrm->buf[pBitStrm->currentByte] |= masks[pBitStrm->currentBit];
if (pBitStrm->currentBit<7)
pBitStrm->currentBit++;
else {
pBitStrm->currentBit = 0;
pBitStrm->currentByte++;
bitstream_push_data_if_required(pBitStrm);
}
assert(pBitStrm->currentByte * 8 + pBitStrm->currentBit <= pBitStrm->count * 8);
}
/*
Append bit zero.
Example
cur bit = 3
x x x |
|_|_|_|_|_|_|_|_|
0 1 2 3 4 5 6 7
xxxy????
and 11101111
------------
xxx0????
*/
void BitStream_AppendNBitZero(BitStream* pBitStrm, int nbits)
{
int totalBits = pBitStrm->currentBit + nbits;
int totalBytes = totalBits / 8;
pBitStrm->currentBit = totalBits % 8;
//pBitStrm->currentByte += totalBits / 8;
if (pBitStrm->currentByte + totalBytes <= pBitStrm->count) {
pBitStrm->currentByte += totalBytes;
bitstream_push_data_if_required(pBitStrm);
} else {
int extraBytes = pBitStrm->currentByte + totalBytes - pBitStrm->count;
pBitStrm->currentByte = pBitStrm->count;
bitstream_push_data_if_required(pBitStrm);
pBitStrm->currentByte = extraBytes;
}
}
void BitStream_AppendNBitOne(BitStream* pBitStrm, int nbits)
{
int i;
while (nbits >= 8) {
BitStream_AppendByte(pBitStrm, 0xFF, FALSE);
nbits -= 8;
}
for (i = 0; i<nbits; i++)
BitStream_AppendBitOne(pBitStrm);
}
void BitStream_AppendBit(BitStream* pBitStrm, flag v)
{
if (v) {
pBitStrm->buf[pBitStrm->currentByte] |= masks[pBitStrm->currentBit];
}
else {
byte nmask = (byte)~masks[pBitStrm->currentBit];
pBitStrm->buf[pBitStrm->currentByte] &= nmask;
}
if (pBitStrm->currentBit<7)
pBitStrm->currentBit++;
else {
pBitStrm->currentBit = 0;
pBitStrm->currentByte++;
bitstream_push_data_if_required(pBitStrm);
}
assert(pBitStrm->currentByte * 8 + pBitStrm->currentBit <= pBitStrm->count * 8);
}
flag BitStream_ReadBit(BitStream* pBitStrm, flag* v)
{
*v = pBitStrm->buf[pBitStrm->currentByte] & masks[pBitStrm->currentBit];
if (pBitStrm->currentBit<7)
pBitStrm->currentBit++;
else {
pBitStrm->currentBit = 0;
pBitStrm->currentByte++;
bitstream_fetch_data_if_required(pBitStrm);
}
return pBitStrm->currentByte * 8 + pBitStrm->currentBit <= pBitStrm->count * 8;
}
flag BitStream_PeekBit(BitStream* pBitStrm) {
return pBitStrm->buf[pBitStrm->currentByte] & masks[pBitStrm->currentBit];
}
/*
Append byte.
Example
cur bit = 3
|
x x x b b b b b b b b
|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
first byte
xxx?????
and 11100000 (mask)
------------
xxx00000
or 000bbbbb
------------
xxxbbbbb
*/
void BitStream_AppendByte(BitStream* pBitStrm, byte v, flag negate)
{
//static byte masksb[] = { 0x0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF };
int cb = pBitStrm->currentBit;
int ncb = 8 - cb;
if (negate)
v = (byte)~v;
byte mask = (byte)~masksb[ncb];
pBitStrm->buf[pBitStrm->currentByte] &= mask;
pBitStrm->buf[pBitStrm->currentByte++] |= (byte)(v >> cb);
bitstream_push_data_if_required(pBitStrm);
assert(pBitStrm->currentByte * 8 + pBitStrm->currentBit <= pBitStrm->count * 8);
if (cb) {
mask = (byte)~mask;
pBitStrm->buf[pBitStrm->currentByte] &= mask;
pBitStrm->buf[pBitStrm->currentByte] |= (byte)(v << ncb);
}
}
flag BitStream_AppendByte0(BitStream* pBitStrm, byte v)
{
int cb = pBitStrm->currentBit;
int ncb = 8 - cb;
byte mask = (byte)~masksb[ncb];
pBitStrm->buf[pBitStrm->currentByte] &= mask;
pBitStrm->buf[pBitStrm->currentByte++] |= (byte)(v >> cb);
bitstream_push_data_if_required(pBitStrm);
if (cb) {
if (pBitStrm->currentByte >= pBitStrm->count)
return FALSE;
mask = (byte)~mask;
pBitStrm->buf[pBitStrm->currentByte] &= mask;
pBitStrm->buf[pBitStrm->currentByte] |= (byte)(v << ncb);
}
return TRUE;
}
flag BitStream_AppendByteArray(BitStream* pBitStrm, const byte arr[], const int arr_len)
{
//static byte masks[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
//static byte masksb[] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF };
int cb = pBitStrm->currentBit;
int ncb = 8 - cb;
byte mask = (byte)~masksb[ncb];
byte nmask = (byte)~mask;
//if (pBitStrm->currentByte + (int)arr_len + (cb > 0 ? 1 : 0) >= pBitStrm->count)
if ( (pBitStrm->currentByte + arr_len)*8 + cb > pBitStrm->count*8)
return FALSE;
if (arr_len> 0) {
byte v = arr[0];
pBitStrm->buf[pBitStrm->currentByte] &= mask; //make zero right bits (i.e. the ones that will get the new value)
pBitStrm->buf[pBitStrm->currentByte++] |= (byte)(v >> cb); //shift right and then populate current byte
bitstream_push_data_if_required(pBitStrm);
pBitStrm->buf[pBitStrm->currentByte] &= nmask;
pBitStrm->buf[pBitStrm->currentByte] |= (byte)(v << ncb);
}
for (int i = 1; i < arr_len - 1; i++) {
byte v = arr[i];
byte v1 = (byte)(v >> cb);
byte v2 = (byte)(v << ncb);
pBitStrm->buf[pBitStrm->currentByte++] |= v1; //shift right and then populate current byte
bitstream_push_data_if_required(pBitStrm);
pBitStrm->buf[pBitStrm->currentByte] |= v2;
}
if (arr_len - 1 > 0) {
byte v = arr[arr_len - 1];
pBitStrm->buf[pBitStrm->currentByte] &= mask; //make zero right bits (i.e. the ones that will get the new value)
pBitStrm->buf[pBitStrm->currentByte++] |= (byte)(v >> cb); //shift right and then populate current byte
bitstream_push_data_if_required(pBitStrm);
if (cb) {
pBitStrm->buf[pBitStrm->currentByte] &= nmask;
pBitStrm->buf[pBitStrm->currentByte] |= (byte)(v << ncb);
}
}
return TRUE;
}
flag BitStream_ReadByte(BitStream* pBitStrm, byte* v)
{
int cb = pBitStrm->currentBit; //bit position in the current byte
//check if the available bytes are enough
int requiredBytes = (cb > 0) ? 2 : 1;
int availableBytes = pBitStrm->count - pBitStrm->currentByte;
if (availableBytes < requiredBytes) {
return FALSE;
}
*v = (byte)(pBitStrm->buf[pBitStrm->currentByte++] << cb);
bitstream_fetch_data_if_required(pBitStrm);
if (cb > 0) {
// we need to read bits from the next byte
int ncb = 8 - cb;
*v |= (byte)(pBitStrm->buf[pBitStrm->currentByte] >> ncb);
}
return TRUE;
}
flag BitStream_ReadByteArray(BitStream* pBitStrm, byte* arr, int arr_len) {
int cb = pBitStrm->currentBit;
int ncb = 8 - cb;
byte* rb = &pBitStrm->buf[pBitStrm->currentByte];
byte* wb = arr;
#ifndef ASN1SCC_STREAMING
if ( (pBitStrm->currentByte + arr_len)*8 + cb > pBitStrm->count*8)
return FALSE;
#endif
for (int i = 0; i < arr_len; i++) {
*wb = (byte)((*rb) << cb);
rb++;
bitstream_fetch_data_if_required(pBitStrm);
*wb |= (byte)((*rb) >> ncb);
wb++;
}
pBitStrm->currentByte += arr_len;
return TRUE;
}
/*
flag BitStream_ReadByte2(BitStream2* pBitStrm, byte* v)
{
int cb = pBitStrm->currentBit;
int ncb = 8 - pBitStrm->currentBit;
*v = (byte)(pBitStrm->buf[pBitStrm->currentByte++] << cb);
if (pBitStrm->currentByte == pBitStrm->count && pBitStrm->fetchData != NULL) {
pBitStrm->fetchData(pBitStrm);
}
if (cb) {
*v |= (byte)(pBitStrm->buf[pBitStrm->currentByte] >> ncb);
}
return pBitStrm->currentByte * 8 + pBitStrm->currentBit <= pBitStrm->count * 8;
}
*/
/* nbits 1..7*/
void BitStream_AppendPartialByte(BitStream* pBitStrm, byte v, byte nbits, flag negate)
{
int cb = pBitStrm->currentBit;
int totalBits = cb + nbits;
int ncb = 8 - cb;
int totalBitsForNextByte;
if (negate)
v = masksb[nbits] & ((byte)~v);
byte mask1 = (byte)~masksb[ncb];
if (totalBits <= 8) {
//static byte masksb[] = { 0x0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF };
byte mask2 = masksb[8 - totalBits];
byte mask = mask1 | mask2;
//e.g. current bit = 3 --> mask = 1110 0000
//nbits = 3 --> totalBits = 6
// mask= 1110 0000
// and 0000 0011 <- masks[totalBits - 1]
// -----------
// final mask 1110 0011
pBitStrm->buf[pBitStrm->currentByte] &= mask;
pBitStrm->buf[pBitStrm->currentByte] |= (byte)(v << (8 - totalBits));
pBitStrm->currentBit += nbits;
if (pBitStrm->currentBit == 8) {
pBitStrm->currentBit = 0;
pBitStrm->currentByte++;
bitstream_push_data_if_required(pBitStrm);
}
}
else {
totalBitsForNextByte = totalBits - 8;
pBitStrm->buf[pBitStrm->currentByte] &= mask1;
pBitStrm->buf[pBitStrm->currentByte++] |= (byte)(v >> totalBitsForNextByte);
bitstream_push_data_if_required(pBitStrm);
byte mask = (byte)~masksb[8 - totalBitsForNextByte];
pBitStrm->buf[pBitStrm->currentByte] &= mask;
pBitStrm->buf[pBitStrm->currentByte] |= (byte)(v << (8 - totalBitsForNextByte));
pBitStrm->currentBit = totalBitsForNextByte;
}
assert(pBitStrm->currentByte * 8 + pBitStrm->currentBit <= pBitStrm->count * 8);
}
/* nbits 1..7*/
flag BitStream_ReadPartialByte(BitStream* pBitStrm, byte *v, byte nbits)
{
int cb = pBitStrm->currentBit;
int totalBits = cb + nbits;
int totalBitsForNextByte;
if (totalBits <= 8) {
*v = (byte)((pBitStrm->buf[pBitStrm->currentByte] >> (8 - totalBits)) & masksb[nbits]);
pBitStrm->currentBit += nbits;
if (pBitStrm->currentBit == 8) {
pBitStrm->currentBit = 0;
pBitStrm->currentByte++;
bitstream_fetch_data_if_required(pBitStrm);
}
}
else {
totalBitsForNextByte = totalBits - 8;
*v = (byte)(pBitStrm->buf[pBitStrm->currentByte++] << totalBitsForNextByte);
bitstream_fetch_data_if_required(pBitStrm);
*v |= (byte)(pBitStrm->buf[pBitStrm->currentByte] >> (8 - totalBitsForNextByte));
*v &= masksb[nbits];
pBitStrm->currentBit = totalBitsForNextByte;
}
return pBitStrm->currentByte * 8 + pBitStrm->currentBit <= pBitStrm->count * 8;
}
/***********************************************************************************************/
/***********************************************************************************************/
/***********************************************************************************************/
/***********************************************************************************************/
/* Integer Functions */
/***********************************************************************************************/
/***********************************************************************************************/
/***********************************************************************************************/
/***********************************************************************************************/
static void BitStream_EncodeNonNegativeInteger32Neg(BitStream* pBitStrm,
asn1SccUint32 v,
flag negate)
{
int cc;
asn1SccUint32 curMask;
int pbits;
if (v == 0)
return;
if (v<0x100) {
cc = 8;
curMask = 0x80;
}
else if (v<0x10000) {
cc = 16;
curMask = 0x8000;
}
else if (v<0x1000000) {
cc = 24;
curMask = 0x800000;
}
else {
cc = 32;
curMask = 0x80000000;
}
while ((v & curMask) == 0) {
curMask >>= 1;
cc--;
}
pbits = cc % 8;
if (pbits) {
cc -= pbits;
BitStream_AppendPartialByte(pBitStrm, (byte)(v >> cc), (byte)pbits, negate);
}
while (cc) {
asn1SccUint32 t1 = v & masks2[cc >> 3];
cc -= 8;
BitStream_AppendByte(pBitStrm, (byte)(t1 >> cc), negate);
}
}
static flag BitStream_DecodeNonNegativeInteger32Neg(BitStream* pBitStrm,
asn1SccUint32* v,
int nBits)
{
byte b;
*v = 0;
while (nBits >= 8) {
*v <<= 8;
if (!BitStream_ReadByte(pBitStrm, &b))
return FALSE;
*v |= b;
nBits -= 8;
}
if (nBits)
{
*v <<= nBits;
if (!BitStream_ReadPartialByte(pBitStrm, &b, (byte)nBits))
return FALSE;
*v |= b;
}
return TRUE;
}
void BitStream_EncodeNonNegativeInteger(BitStream* pBitStrm, asn1SccUint v)
{
#if WORD_SIZE==8
if (v<0x100000000LL)
BitStream_EncodeNonNegativeInteger32Neg(pBitStrm, (asn1SccUint32)v, 0);
else {
asn1SccUint32 hi = (asn1SccUint32)(v >> 32);
asn1SccUint32 lo = (asn1SccUint32)v;
int nBits;
BitStream_EncodeNonNegativeInteger32Neg(pBitStrm, hi, 0);
nBits = GetNumberOfBitsForNonNegativeInteger(lo);
BitStream_AppendNBitZero(pBitStrm, 32 - nBits);
BitStream_EncodeNonNegativeInteger32Neg(pBitStrm, lo, 0);
}
#else
BitStream_EncodeNonNegativeInteger32Neg(pBitStrm, v, 0);
#endif
}
flag BitStream_DecodeNonNegativeInteger(BitStream* pBitStrm, asn1SccUint* v, int nBits)
{
#if WORD_SIZE==8
asn1SccUint32 hi = 0;
asn1SccUint32 lo = 0;
flag ret;
if (nBits <= 32)
{
ret = BitStream_DecodeNonNegativeInteger32Neg(pBitStrm, &lo, nBits);
*v = lo;
return ret;
}
ret = BitStream_DecodeNonNegativeInteger32Neg(pBitStrm, &hi, 32) && BitStream_DecodeNonNegativeInteger32Neg(pBitStrm, &lo, nBits - 32);
*v = hi;
*v <<= nBits - 32;
*v |= lo;
return ret;
#else
return BitStream_DecodeNonNegativeInteger32Neg(pBitStrm, v, nBits);
#endif
}
void BitStream_EncodeNonNegativeIntegerNeg(BitStream* pBitStrm, asn1SccUint v, flag negate)
{
#if WORD_SIZE==8
if (v<0x100000000LL)
BitStream_EncodeNonNegativeInteger32Neg(pBitStrm, (asn1SccUint32)v, negate);
else {
int nBits;
asn1SccUint32 hi = (asn1SccUint32)(v >> 32);
asn1SccUint32 lo = (asn1SccUint32)v;
BitStream_EncodeNonNegativeInteger32Neg(pBitStrm, hi, negate);
/*bug !!!!*/
if (negate)
lo = ~lo;
nBits = GetNumberOfBitsForNonNegativeInteger(lo);
BitStream_AppendNBitZero(pBitStrm, 32 - nBits);
BitStream_EncodeNonNegativeInteger32Neg(pBitStrm, lo, 0);
}
#else
BitStream_EncodeNonNegativeInteger32Neg(pBitStrm, v, negate);
#endif
}
int GetNumberOfBitsForNonNegativeInteger32(asn1SccUint32 v)
{
int ret = 0;
if (v<0x100) {
ret = 0;
}
else if (v<0x10000) {
ret = 8;
v >>= 8;
}
else if (v<0x1000000) {
ret = 16;
v >>= 16;
}
else {
ret = 24;
v >>= 24;
}
while (v>0) {
v >>= 1;
ret++;
}
return ret;
}
int GetNumberOfBitsForNonNegativeInteger(asn1SccUint v)
{
#if WORD_SIZE==8
if (v<0x100000000LL)
return GetNumberOfBitsForNonNegativeInteger32((asn1SccUint32)v);
else {
asn1SccUint32 hi = (asn1SccUint32)(v >> 32);
return 32 + GetNumberOfBitsForNonNegativeInteger32(hi);
}
#else
return GetNumberOfBitsForNonNegativeInteger32(v);
#endif
}
int GetLengthInBytesOfUInt(asn1SccUint64 v)
{
int ret = 0;
asn1SccUint32 v32 = (asn1SccUint32)v;
#if WORD_SIZE==8
if (v>0xFFFFFFFF) {
ret = 4;
v32 = (asn1SccUint32)(v >> 32);
}
#endif
if (v32<0x100)
return ret + 1;
if (v32<0x10000)
return ret + 2;
if (v32<0x1000000)
return ret + 3;
return ret + 4;
}
static int GetLengthSIntHelper(asn1SccUint v)
{
int ret = 0;
asn1SccUint32 v32 = (asn1SccUint32)v;
#if WORD_SIZE==8
if (v>0x7FFFFFFF) {
ret = 4;
v32 = (asn1SccUint32)(v >> 32);
}
#endif
if (v32 <= 0x7F)
return ret + 1;
if (v32 <= 0x7FFF)
return ret + 2;
if (v32 <= 0x7FFFFF)
return ret + 3;
return ret + 4;
}
int GetLengthInBytesOfSInt(asn1SccSint v)
{
if (v >= 0)
return GetLengthSIntHelper((asn1SccUint)v);
return GetLengthSIntHelper((asn1SccUint)(-v - 1));
}
void BitStream_EncodeConstraintWholeNumber(BitStream* pBitStrm, asn1SccSint v, asn1SccSint min, asn1SccSint max)
{
int nRangeBits;
int nBits;
asn1SccUint range;
assert(min <= max);
range = (asn1SccUint)(max - min);
if (!range)
return;
nRangeBits = GetNumberOfBitsForNonNegativeInteger(range);
nBits = GetNumberOfBitsForNonNegativeInteger((asn1SccUint)(v - min));
BitStream_AppendNBitZero(pBitStrm, nRangeBits - nBits);
BitStream_EncodeNonNegativeInteger(pBitStrm, (asn1SccUint)(v - min));
}
void BitStream_EncodeConstraintPosWholeNumber(BitStream* pBitStrm, asn1SccUint v, asn1SccUint min, asn1SccUint max)
{
int nRangeBits;
int nBits;
asn1SccUint range;
assert(min <= v);
assert(v <= max);
range = (asn1SccUint)(max - min);
if (!range)
return;
nRangeBits = GetNumberOfBitsForNonNegativeInteger(range);
nBits = GetNumberOfBitsForNonNegativeInteger(v - min);
BitStream_AppendNBitZero(pBitStrm, nRangeBits - nBits);
BitStream_EncodeNonNegativeInteger(pBitStrm, v - min);
}
flag BitStream_DecodeConstraintWholeNumber(BitStream* pBitStrm, asn1SccSint* v, asn1SccSint min, asn1SccSint max)
{
asn1SccUint uv;
int nRangeBits;
asn1SccUint range = (asn1SccUint)(max - min);
ASSERT_OR_RETURN_FALSE(min <= max);
*v = 0;
if (!range) {
*v = min;
return TRUE;
}
nRangeBits = GetNumberOfBitsForNonNegativeInteger(range);
if (BitStream_DecodeNonNegativeInteger(pBitStrm, &uv, nRangeBits))
{
*v = ((asn1SccSint)uv) + min;
return TRUE;
}
return FALSE;
}
flag BitStream_DecodeConstraintWholeNumberInt16(BitStream* pBitStrm, int16_t* v, int16_t min, int16_t max)
{
asn1SccSint bv;
flag ret;
ret = BitStream_DecodeConstraintWholeNumber(pBitStrm, &bv, min, max);
*v = (int16_t)bv;
return ret;
}
flag BitStream_DecodeConstraintWholeNumberInt32(BitStream* pBitStrm, int32_t* v, int32_t min, int32_t max)
{
asn1SccSint bv;
flag ret;
ret = BitStream_DecodeConstraintWholeNumber(pBitStrm, &bv, min, max);
*v = (int32_t)bv;
return ret;
}
flag BitStream_DecodeConstraintPosWholeNumberUInt8(BitStream* pBitStrm, uint8_t* v, uint8_t min, uint8_t max)
{
asn1SccUint bv;
flag ret;
ret = BitStream_DecodeConstraintPosWholeNumber(pBitStrm, &bv, min, max);
*v = (uint8_t)bv;
return ret;
}
flag BitStream_DecodeConstraintPosWholeNumberUInt16(BitStream* pBitStrm, uint16_t* v, uint16_t min, uint16_t max)
{
asn1SccUint bv;
flag ret;
ret = BitStream_DecodeConstraintPosWholeNumber(pBitStrm, &bv, min, max);
*v = (uint16_t)bv;
return ret;
}
flag BitStream_DecodeConstraintPosWholeNumber(BitStream* pBitStrm, asn1SccUint* v, asn1SccUint min, asn1SccUint max)
{
asn1SccUint uv;
int nRangeBits;
asn1SccUint range = max - min;
ASSERT_OR_RETURN_FALSE(min <= max);
*v = 0;
if (!range) {
*v = min;
return TRUE;
}
nRangeBits = GetNumberOfBitsForNonNegativeInteger(range);
if (BitStream_DecodeNonNegativeInteger(pBitStrm, &uv, nRangeBits))
{
*v = uv + min;
return TRUE;
}
return FALSE;
}
void BitStream_EncodeUnConstraintWholeNumber(BitStream* pBitStrm, asn1SccSint v)
{
int nBytes = GetLengthInBytesOfSInt(v);
/* encode length */
BitStream_EncodeConstraintWholeNumber(pBitStrm, nBytes, 0, 255); /*8 bits, first bit is always 0*/
if (v >= 0) {
BitStream_AppendNBitZero(pBitStrm, nBytes * 8 - GetNumberOfBitsForNonNegativeInteger((asn1SccUint)v));
BitStream_EncodeNonNegativeInteger(pBitStrm, (asn1SccUint)(v));
}
else {
BitStream_AppendNBitOne(pBitStrm, nBytes * 8 - GetNumberOfBitsForNonNegativeInteger((asn1SccUint)(-v - 1)));
BitStream_EncodeNonNegativeIntegerNeg(pBitStrm, (asn1SccUint)(-v - 1), 1);
}
}
flag BitStream_DecodeUnConstraintWholeNumber(BitStream* pBitStrm, asn1SccSint* v)
{
asn1SccSint nBytes;
int i;
flag valIsNegative;
if (!BitStream_DecodeConstraintWholeNumber(pBitStrm, &nBytes, 0, 255))
return FALSE;
valIsNegative = BitStream_PeekBit(pBitStrm);
*v = valIsNegative ? MAX_INT : 0;
for (i = 0; i<nBytes; i++) {
byte b = 0;
if (!BitStream_ReadByte(pBitStrm, &b))
return FALSE;
*v = (*v << 8) | b;
}
return TRUE;
}
#ifndef INFINITY
#ifdef __GNUC__
#define INFINITY (__builtin_inf())
#endif
#endif
/*
Bynary encoding will be used
REAL = M*B^E
where
M = S*N*2^F
ENCODING is done within three parts
part 1 is 1 byte header
part 2 is 1 or more byte for exponent
part 3 is 3 or more byte for mantissa (N)
First byte
S :0-->+, S:1-->-1
Base will be always be 2 (implied by 6th and 5th bit which are zero)
ab: F (0..3)
cd:00 --> 1 byte for exponent as 2's complement
cd:01 --> 2 byte for exponent as 2's complement
cd:10 --> 3 byte for exponent as 2's complement
cd:11 --> 1 byte for encoding the length of the exponent, then the expoent
8 7 6 5 4 3 2 1
+-+-+-+-+-+-+-+-+
|1|S|0|0|a|b|c|d|
+-+-+-+-+-+-+-+-+
*/
#if FP_WORD_SIZE==8
#define ExpoBitMask 0x7FF0000000000000ULL
#define MantBitMask 0x000FFFFFFFFFFFFFULL
#define MantBitMask2 0xFFE0000000000000ULL
#define MantisaExtraBit 0x0010000000000000ULL
#else
#define ExpoBitMask 0x7F800000U
#define MantBitMask 0x007FFFFFU
#define MantBitMask2 0xF0000000U
#define MantisaExtraBit 0x00800000U
#endif
void CalculateMantissaAndExponent(asn1Real d, int* exponent, asn1SccUint64* mantissa)
{
#if FP_WORD_SIZE==8
union {
asn1Real in;
asn1SccUint64 out;
} double2uint;
asn1SccUint64 ll = 0;
#else
union {
asn1Real in;
asn1SccUint32 out;
} double2uint;
asn1SccUint32 ll = 0;
#endif
double2uint.in = d;
ll = double2uint.out;
*exponent = 0;
*mantissa = 0;
#if FP_WORD_SIZE==8
* exponent = (int)(((ll & ExpoBitMask) >> 52) - 1023 - 52);
*mantissa = ll & MantBitMask;
(*mantissa) |= MantisaExtraBit;
#else
*exponent = (int)(((ll & ExpoBitMask) >> 23) - 127 - 23);
*mantissa = ll & MantBitMask;
(*mantissa) |= MantisaExtraBit;
#endif
}
asn1Real GetDoubleByMantissaAndExp(asn1SccUint mantissa, int exponent)
{
#ifdef USE_LDEXP
return (asn1Real)ldexp((double)mantissa, exponent);
#else
asn1Real ret = 1.0;
if (mantissa == 0)
return 0.0;
if (exponent >= 0) {
while (exponent) {
ret = ret * 2.0;
exponent--;
}
return (asn1Real)mantissa*ret;
}
else {
exponent = -exponent;
while (exponent) {
ret = ret * 2.0;
exponent--;
}
return ((asn1Real)mantissa) / ret;
}
#endif
}
void BitStream_EncodeReal(BitStream* pBitStrm, asn1Real v)