| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include "internal.h" |
|---|
| 21 | #ifdef ENABLE_ZLIB |
|---|
| 22 | #include "compression.h" |
|---|
| 23 | |
|---|
| 24 | #include <zlib.h> |
|---|
| 25 | #include <string.h> |
|---|
| 26 | #include <stdlib.h> |
|---|
| 27 | #include "errors.h" |
|---|
| 28 | |
|---|
| 29 | static int zerr_to_opensc(int err) { |
|---|
| 30 | switch(err) { |
|---|
| 31 | case Z_OK: |
|---|
| 32 | case Z_STREAM_END: |
|---|
| 33 | return SC_SUCCESS; |
|---|
| 34 | case Z_UNKNOWN: |
|---|
| 35 | return SC_ERROR_UNKNOWN; |
|---|
| 36 | case Z_BUF_ERROR: |
|---|
| 37 | case Z_MEM_ERROR: |
|---|
| 38 | return SC_ERROR_MEMORY_FAILURE; |
|---|
| 39 | case Z_VERSION_ERROR: |
|---|
| 40 | case Z_DATA_ERROR: |
|---|
| 41 | case Z_STREAM_ERROR: |
|---|
| 42 | |
|---|
| 43 | default: |
|---|
| 44 | return SC_ERROR_INTERNAL; |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | static int detect_method(const u8* in, size_t inLen) { |
|---|
| 48 | if(inLen > 2 && in[0] == 0x1f && in[1] == 0x8b) { |
|---|
| 49 | return COMPRESSION_GZIP; |
|---|
| 50 | } else if(inLen > 1 ) { |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | return COMPRESSION_ZLIB; |
|---|
| 56 | } else { |
|---|
| 57 | return COMPRESSION_UNKNOWN; |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | static int sc_decompress_gzip(u8* out, size_t* outLen, const u8* in, size_t inLen) { |
|---|
| 62 | |
|---|
| 63 | z_stream gz; |
|---|
| 64 | int err; |
|---|
| 65 | int window_size = 15 + 0x20; |
|---|
| 66 | memset(&gz, 0, sizeof(gz)); |
|---|
| 67 | |
|---|
| 68 | gz.next_in = (u8*)in; |
|---|
| 69 | gz.avail_in = inLen; |
|---|
| 70 | gz.next_out = out; |
|---|
| 71 | gz.avail_out = *outLen; |
|---|
| 72 | |
|---|
| 73 | err = inflateInit2(&gz, window_size); |
|---|
| 74 | if(err != Z_OK) return zerr_to_opensc(err); |
|---|
| 75 | err = inflate(&gz, Z_FINISH); |
|---|
| 76 | if(err != Z_STREAM_END) { |
|---|
| 77 | inflateEnd(&gz); |
|---|
| 78 | return zerr_to_opensc(err); |
|---|
| 79 | } |
|---|
| 80 | *outLen = gz.total_out; |
|---|
| 81 | |
|---|
| 82 | err = inflateEnd(&gz); |
|---|
| 83 | return zerr_to_opensc(err); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | int sc_decompress(u8* out, size_t* outLen, const u8* in, size_t inLen, int method) { |
|---|
| 87 | unsigned long zlib_outlen; |
|---|
| 88 | int rc; |
|---|
| 89 | |
|---|
| 90 | if(method == COMPRESSION_AUTO) { |
|---|
| 91 | method = detect_method(in, inLen); |
|---|
| 92 | if(method == COMPRESSION_UNKNOWN) { |
|---|
| 93 | return SC_ERROR_UNKNOWN_DATA_RECEIVED; |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | switch(method) { |
|---|
| 97 | case COMPRESSION_ZLIB: |
|---|
| 98 | zlib_outlen = *outLen; |
|---|
| 99 | rc = zerr_to_opensc(uncompress(out, &zlib_outlen, in, inLen)); |
|---|
| 100 | *outLen = zlib_outlen; |
|---|
| 101 | return rc; |
|---|
| 102 | case COMPRESSION_GZIP: |
|---|
| 103 | return sc_decompress_gzip(out, outLen, in, inLen); |
|---|
| 104 | default: |
|---|
| 105 | return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | static int sc_decompress_zlib_alloc(u8** out, size_t* outLen, const u8* in, size_t inLen, int gzip) { |
|---|
| 110 | |
|---|
| 111 | z_stream gz; |
|---|
| 112 | int err; |
|---|
| 113 | int window_size = 15; |
|---|
| 114 | const int startSize = inLen < 1024 ? 2048 : inLen * 2; |
|---|
| 115 | const int blockSize = inLen < 1024 ? 512 : inLen / 2; |
|---|
| 116 | int bufferSize = startSize; |
|---|
| 117 | if(gzip) |
|---|
| 118 | window_size += 0x20; |
|---|
| 119 | memset(&gz, 0, sizeof(gz)); |
|---|
| 120 | |
|---|
| 121 | gz.next_in = (u8*)in; |
|---|
| 122 | gz.avail_in = inLen; |
|---|
| 123 | |
|---|
| 124 | err = inflateInit2(&gz, window_size); |
|---|
| 125 | if(err != Z_OK) return zerr_to_opensc(err); |
|---|
| 126 | |
|---|
| 127 | *outLen = 0; |
|---|
| 128 | |
|---|
| 129 | while(1) { |
|---|
| 130 | |
|---|
| 131 | int num; |
|---|
| 132 | u8* buf = realloc(*out, bufferSize); |
|---|
| 133 | if(!buf) { |
|---|
| 134 | if(*out) |
|---|
| 135 | free(*out); |
|---|
| 136 | *out = NULL; |
|---|
| 137 | return Z_MEM_ERROR; |
|---|
| 138 | } |
|---|
| 139 | *out = buf; |
|---|
| 140 | gz.next_out = buf + *outLen; |
|---|
| 141 | gz.avail_out = bufferSize - *outLen; |
|---|
| 142 | |
|---|
| 143 | err = inflate(&gz, Z_FULL_FLUSH); |
|---|
| 144 | if(err != Z_STREAM_END && err != Z_OK) { |
|---|
| 145 | if(*out) |
|---|
| 146 | free(*out); |
|---|
| 147 | *out = NULL; |
|---|
| 148 | break; |
|---|
| 149 | } |
|---|
| 150 | num = bufferSize - *outLen - gz.avail_out; |
|---|
| 151 | if(num > 0) { |
|---|
| 152 | *outLen += num; |
|---|
| 153 | bufferSize += num + blockSize; |
|---|
| 154 | } |
|---|
| 155 | if(err == Z_STREAM_END) { |
|---|
| 156 | buf = realloc(buf, *outLen); |
|---|
| 157 | if(buf) { |
|---|
| 158 | *out = buf; |
|---|
| 159 | } |
|---|
| 160 | break; |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | inflateEnd(&gz); |
|---|
| 164 | return zerr_to_opensc(err); |
|---|
| 165 | } |
|---|
| 166 | int sc_decompress_alloc(u8** out, size_t* outLen, const u8* in, size_t inLen, int method) { |
|---|
| 167 | if(method == COMPRESSION_AUTO) { |
|---|
| 168 | method = detect_method(in, inLen); |
|---|
| 169 | if(method == COMPRESSION_UNKNOWN) { |
|---|
| 170 | return SC_ERROR_UNKNOWN_DATA_RECEIVED; |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | switch(method) { |
|---|
| 174 | case COMPRESSION_ZLIB: |
|---|
| 175 | return sc_decompress_zlib_alloc(out, outLen, in, inLen, 0); |
|---|
| 176 | case COMPRESSION_GZIP: |
|---|
| 177 | return sc_decompress_zlib_alloc(out, outLen, in, inLen, 1); |
|---|
| 178 | default: |
|---|
| 179 | return SC_ERROR_INVALID_ARGUMENTS; |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | #endif |
|---|