1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | // C99 required |
5 | // C99 required! |
|
|
6 | // this is not just for comments, but also for |
|
|
7 | // integer constant semantics, |
|
|
8 | // sscanf format modifiers and more. |
6 | |
9 | |
7 | enum { |
10 | enum { |
8 | // ASN_TAG |
11 | // ASN_TAG |
9 | ASN_BOOLEAN = 0x01, |
12 | ASN_BOOLEAN = 0x01, |
10 | ASN_INTEGER32 = 0x02, |
13 | ASN_INTEGER32 = 0x02, |
… | |
… | |
159 | return BER_TYPE_BYTES; |
162 | return BER_TYPE_BYTES; |
160 | |
163 | |
161 | return SvPVX (sv)[idx]; |
164 | return SvPVX (sv)[idx]; |
162 | } |
165 | } |
163 | |
166 | |
164 | static int |
167 | static void |
165 | profile_set (profile_type *profile, int klass, int tag, int type) |
168 | profile_set (profile_type *profile, int klass, int tag, int type) |
166 | { |
169 | { |
167 | SV *sv = (SV *)profile; |
170 | SV *sv = (SV *)profile; |
168 | U32 idx = (tag << 2) + klass; |
171 | U32 idx = (tag << 2) + klass; |
169 | STRLEN oldlen = SvCUR (sv); |
172 | STRLEN oldlen = SvCUR (sv); |
… | |
… | |
199 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
202 | { ASN_UNIVERSAL, ASN_UTF8_STRING , BER_TYPE_UTF8 }, |
200 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
203 | { ASN_UNIVERSAL, ASN_BMP_STRING , BER_TYPE_UCS2 }, |
201 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
204 | { ASN_UNIVERSAL, ASN_UNIVERSAL_STRING , BER_TYPE_UCS4 }, |
202 | }; |
205 | }; |
203 | |
206 | |
204 | for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem > default_map; celem--) |
207 | for (celem = default_map + sizeof (default_map) / sizeof (default_map [0]); celem-- > default_map; ) |
205 | profile_set ((void *)sv, celem->klass, celem->tag, celem->type); |
208 | profile_set ((profile_type *)sv, celem->klass, celem->tag, celem->type); |
206 | |
209 | |
207 | return sv_bless (newRV_noinc (sv), profile_stash); |
210 | return sv_bless (newRV_noinc (sv), profile_stash); |
208 | } |
211 | } |
209 | |
212 | |
210 | ///////////////////////////////////////////////////////////////////////////// |
213 | ///////////////////////////////////////////////////////////////////////////// |
… | |
… | |
343 | u /= 10; |
346 | u /= 10; |
344 | } |
347 | } |
345 | while (u); |
348 | while (u); |
346 | |
349 | |
347 | // reverse digits |
350 | // reverse digits |
348 | for (char *ptr = buf; --ptr != beg; ++beg) |
351 | char *ptr = buf; |
|
|
352 | while (--ptr != beg) |
349 | { |
353 | { |
350 | char c = *ptr; |
354 | char c = *ptr; |
351 | *ptr = *beg; |
355 | *ptr = *beg; |
352 | *beg = c; |
356 | *beg = c; |
|
|
357 | ++beg; |
353 | } |
358 | } |
354 | } |
359 | } |
355 | |
360 | |
356 | return buf; |
361 | return buf; |
357 | } |
362 | } |
… | |
… | |
557 | static void |
562 | static void |
558 | set_buf (SV *sv) |
563 | set_buf (SV *sv) |
559 | { |
564 | { |
560 | STRLEN len; |
565 | STRLEN len; |
561 | buf_sv = sv; |
566 | buf_sv = sv; |
562 | buf = SvPVbyte (buf_sv, len); |
567 | buf = (U8 *)SvPVbyte (buf_sv, len); |
563 | cur = buf; |
568 | cur = buf; |
564 | end = buf + len; |
569 | end = buf + len; |
565 | } |
570 | } |
566 | |
571 | |
567 | /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ |
572 | /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ |
… | |
… | |
581 | need (STRLEN len) |
586 | need (STRLEN len) |
582 | { |
587 | { |
583 | if (expect_false ((uintptr_t)(end - cur) < len)) |
588 | if (expect_false ((uintptr_t)(end - cur) < len)) |
584 | { |
589 | { |
585 | STRLEN pos = cur - buf; |
590 | STRLEN pos = cur - buf; |
586 | buf = my_sv_grow (buf_sv, pos, len); |
591 | buf = (U8 *)my_sv_grow (buf_sv, pos, len); |
587 | cur = buf + pos; |
592 | cur = buf + pos; |
588 | end = buf + SvLEN (buf_sv) - 1; |
593 | end = buf + SvLEN (buf_sv) - 1; |
589 | } |
594 | } |
590 | } |
595 | } |
591 | |
596 | |
… | |
… | |
817 | put_length (uchars * chrsize); |
822 | put_length (uchars * chrsize); |
818 | |
823 | |
819 | while (uchars--) |
824 | while (uchars--) |
820 | { |
825 | { |
821 | STRLEN uclen; |
826 | STRLEN uclen; |
822 | UV uchr = utf8_to_uvchr_buf (ptr, ptr + len, &uclen); |
827 | UV uchr = utf8_to_uvchr_buf ((U8 *)ptr, (U8 *)ptr + len, &uclen); |
823 | |
828 | |
824 | ptr += uclen; |
829 | ptr += uclen; |
825 | len -= uclen; |
830 | len -= uclen; |
826 | |
831 | |
827 | if (chrsize == 4) |
832 | if (chrsize == 4) |
… | |
… | |
868 | int fill = AvFILL (av); |
873 | int fill = AvFILL (av); |
869 | |
874 | |
870 | if (expect_false (SvRMAGICAL (av))) |
875 | if (expect_false (SvRMAGICAL (av))) |
871 | croak ("BER constructed data must not be tied"); |
876 | croak ("BER constructed data must not be tied"); |
872 | |
877 | |
|
|
878 | int i; |
873 | for (int i = 0; i <= fill; ++i) |
879 | for (i = 0; i <= fill; ++i) |
874 | encode_ber (AvARRAY (av)[i]); |
880 | encode_ber (AvARRAY (av)[i]); |
875 | |
881 | |
876 | len_fixup (mark); |
882 | len_fixup (mark); |
877 | } |
883 | } |
878 | else |
884 | else |
… | |
… | |
1028 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1034 | ber_decode (SV *ber, SV *profile = &PL_sv_undef) |
1029 | CODE: |
1035 | CODE: |
1030 | { |
1036 | { |
1031 | cur_profile = SvPROFILE (profile); |
1037 | cur_profile = SvPROFILE (profile); |
1032 | STRLEN len; |
1038 | STRLEN len; |
1033 | buf = SvPVbyte (ber, len); |
1039 | buf = (U8 *)SvPVbyte (ber, len); |
1034 | cur = buf; |
1040 | cur = buf; |
1035 | end = buf + len; |
1041 | end = buf + len; |
1036 | |
1042 | |
1037 | RETVAL = decode_ber (); |
1043 | RETVAL = decode_ber (); |
1038 | } |
1044 | } |