« blib/lib/EBook/Ishmael/CharDet/ISO88595.pm
↑ Summary
blib/lib/EBook/Ishmael/CharDet/ISO2022JP.pm »
| 1 | #define PERL_NO_GET_CONTEXT | ||||||||||
| 2 | #include "EXTERN.h" | ||||||||||
| 3 | #include "perl.h" | ||||||||||
| 4 | #include "XSUB.h" | ||||||||||
| 5 | |||||||||||
| 6 | #include "ppport.h" | ||||||||||
| 7 | |||||||||||
| 8 | #define DECODE_BUF_SIZE 8192 | ||||||||||
| 9 | |||||||||||
| 10 | /* https://wiki.mobileread.com/wiki/PalmDOC#PalmDoc_byte_pair_compression */ | ||||||||||
| 11 | int | ||||||||||
| 12 | 201 | c_palmdoc_decode( | |||||||||
| 13 | const unsigned char* input, STRLEN inlen, | ||||||||||
| 14 | unsigned char* output, STRLEN outlen, | ||||||||||
| 15 | STRLEN* decoded | ||||||||||
| 16 | ) { | ||||||||||
| 17 | |||||||||||
| 18 | 201 | unsigned int b = 0; | |||||||||
| 19 | 201 | unsigned int ld = 0; | |||||||||
| 20 | 201 | unsigned int l, d = 0; | |||||||||
| 21 | 201 | STRLEN outp = 0; | |||||||||
| 22 | |||||||||||
| 23 | 201 | *decoded = 0; | |||||||||
| 24 | |||||||||||
| 25 | 263154 | ▶ | for (STRLEN i = 0; i < inlen;) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 262953 | b = input[i++]; | |||||||||
| 27 | /* space + xor byte with 0x80 */ | ||||||||||
| 28 | 262953 | ▶ | if (b >= 0xc0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 11467 | ▶ | if (outp >= outlen) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 0 | return -1; | |||||||||
| 31 | } | ||||||||||
| 32 | 11467 | output[outp++] = ' '; | |||||||||
| 33 | 11467 | ▶ | if (outp >= outlen) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 0 | return -1; | |||||||||
| 35 | } | ||||||||||
| 36 | 11467 | output[outp++] = b ^ 0x80; | |||||||||
| 37 | /* length-distance pair: get next byte, strip 2 leading bits, split | ||||||||||
| 38 | byte into 11 bits of distance and 3 bits of length + 3 */ | ||||||||||
| 39 | 251486 | ▶ | } else if (b >= 0x80) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 113855 | ▶ | if (i + 1 >= inlen) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | return -1; | |||||||||
| 42 | } | ||||||||||
| 43 | 113855 | ld = (b << 8) + input[i++]; | |||||||||
| 44 | 113855 | d = (ld >> 3) & 0x7ff; | |||||||||
| 45 | 113855 | l = (ld & 0x0007) + 3; | |||||||||
| 46 | 113855 | ▶ | if (d > outp) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 0 | return -1; | |||||||||
| 48 | } | ||||||||||
| 49 | 113855 | ▶ | if (l > outlen - outp - 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 0 | return -1; | |||||||||
| 51 | } | ||||||||||
| 52 | 736503 | ▶ | while (l != 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 622648 | output[outp] = output[outp - d]; | |||||||||
| 54 | 622648 | outp++; | |||||||||
| 55 | 622648 | l--; | |||||||||
| 56 | } | ||||||||||
| 57 | /* literal copy */ | ||||||||||
| 58 | 137631 | ▶ | } else if (b >= 0x09) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 136922 | ▶ | if (outp >= outlen) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 0 | return -1; | |||||||||
| 61 | } | ||||||||||
| 62 | 136922 | output[outp++] = b; | |||||||||
| 63 | /* copy next 1-8 bytes */ | ||||||||||
| 64 | 709 | ▶ | } else if (b >= 0x01) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 709 | ▶ | if (i + b > inlen) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 0 | return -1; | |||||||||
| 67 | } | ||||||||||
| 68 | 709 | ▶ | if (outp + b > outlen) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | return -1; | |||||||||
| 70 | } | ||||||||||
| 71 | 2681 | ▶ | while (b != 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 1972 | output[outp++] = input[i++]; | |||||||||
| 73 | 1972 | b--; | |||||||||
| 74 | } | ||||||||||
| 75 | /* copy null byte */ | ||||||||||
| 76 | } else { | ||||||||||
| 77 | 0 | ▶ | if (outp >= outlen) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 0 | return -1; | |||||||||
| 79 | } | ||||||||||
| 80 | 0 | output[outp++] = '\0'; | |||||||||
| 81 | } | ||||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | assert(outp <= outlen); | ||||||||||
| 85 | |||||||||||
| 86 | 201 | *decoded = outp; | |||||||||
| 87 | 201 | return 0; | |||||||||
| 88 | |||||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | MODULE = EBook::Ishmael PACKAGE = EBook::Ishmael::Decode | ||||||||||
| 92 | |||||||||||
| 93 | PROTOTYPES: DISABLE | ||||||||||
| 94 | |||||||||||
| 95 | SV* | ||||||||||
| 96 | xs_palmdoc_decode(encode) | ||||||||||
| 97 | SV * encode | ||||||||||
| 98 | INIT: | ||||||||||
| 99 | STRLEN enclen; | ||||||||||
| 100 | SV * outsv; | ||||||||||
| 101 | unsigned char output[DECODE_BUF_SIZE]; | ||||||||||
| 102 | STRLEN declen; | ||||||||||
| 103 | int s; | ||||||||||
| 104 | CODE: | ||||||||||
| 105 | 201 | enclen = sv_len_utf8(encode); | |||||||||
| 106 | 201 | s = c_palmdoc_decode( | |||||||||
| 107 | 201 | (unsigned char*) SvPVbyte_nolen(encode), enclen, | |||||||||
| 108 | output, DECODE_BUF_SIZE, | ||||||||||
| 109 | &declen | ||||||||||
| 110 | ); | ||||||||||
| 111 | assert(declen <= DECODE_BUF_SIZE); | ||||||||||
| 112 | 201 | ▶ | if (s != 0) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 0 | croak("Invalid LZ77-encoded PalmDOC data stream\n"); | |||||||||
| 114 | } | ||||||||||
| 115 | 201 | outsv = newSVpvn_flags(output, declen, 0); | |||||||||
| 116 | 201 | RETVAL = outsv; | |||||||||
| 117 | OUTPUT: | ||||||||||
| 118 | RETVAL | ||||||||||