Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdint.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include "../common.h" |
| 6 | #include "C/LzmaDec.h" |
| 7 | #include "C/LzmaEnc.h" |
| 8 | |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 9 | #define L (uint64_t) |
| 10 | |
| 11 | static inline uint64_t get_64(const void *p) |
| 12 | { |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 13 | const unsigned char *data = (const unsigned char *)p; |
| 14 | return (L data[0]) | (L data[1] << 8) | (L data[2] << 16) | |
| 15 | (L data[3] << 24) | (L data [4] << 32) | (L data[5] << 40) | |
| 16 | (L data[6] << 48) | (L data[7] << 56); |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | static void put_64(void *p, uint64_t value) |
| 20 | { |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 21 | unsigned char *data = (unsigned char *)p; |
| 22 | data[0] = value & 0xff; |
| 23 | data[1] = (value >> 8) & 0xff; |
| 24 | data[2] = (value >> 16) & 0xff; |
| 25 | data[3] = (value >> 24) & 0xff; |
| 26 | data[4] = (value >> 32) & 0xff; |
| 27 | data[5] = (value >> 40) & 0xff; |
| 28 | data[6] = (value >> 48) & 0xff; |
| 29 | data[7] = (value >> 56) & 0xff; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | /* Memory Allocation API */ |
| 33 | |
| 34 | static void *SzAlloc(void *unused, size_t size) |
| 35 | { |
| 36 | return malloc(size); |
| 37 | } |
| 38 | |
| 39 | static void SzFree(void *unused, void *address) |
| 40 | { |
| 41 | free(address); |
| 42 | } |
| 43 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 44 | static struct ISzAlloc LZMAalloc = { SzAlloc, SzFree }; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 45 | |
| 46 | /* Streaming API */ |
| 47 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 48 | struct vector_t { |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 49 | char *p; |
| 50 | size_t pos; |
| 51 | size_t size; |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 52 | }; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 53 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 54 | static struct vector_t instream, outstream; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 55 | |
| 56 | static SRes Read(void *unused, void *buf, size_t *size) |
| 57 | { |
| 58 | if ((instream.size - instream.pos) < *size) |
| 59 | *size = instream.size - instream.pos; |
| 60 | memcpy(buf, instream.p + instream.pos, *size); |
| 61 | instream.pos += *size; |
| 62 | return SZ_OK; |
| 63 | } |
| 64 | |
| 65 | static size_t Write(void *unused, const void *buf, size_t size) |
| 66 | { |
| 67 | if(outstream.size - outstream.pos < size) |
| 68 | size = outstream.size - outstream.pos; |
| 69 | memcpy(outstream.p + outstream.pos, buf, size); |
| 70 | outstream.pos += size; |
| 71 | return size; |
| 72 | } |
| 73 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 74 | static struct ISeqInStream is = { Read }; |
| 75 | static struct ISeqOutStream os = { Write }; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Compress a buffer with lzma |
| 79 | * Don't copy the result back if it is too large. |
| 80 | * @param in a pointer to the buffer |
| 81 | * @param in_len the length in bytes |
| 82 | * @param out a pointer to a buffer of at least size in_len |
| 83 | * @param out_len a pointer to the compressed length of in |
| 84 | */ |
| 85 | |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame^] | 86 | int do_lzma_compress(char *in, int in_len, char *out, int *out_len) |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 87 | { |
| 88 | if (in_len == 0) { |
| 89 | ERROR("LZMA: Input length is zero.\n"); |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame^] | 90 | return -1; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 93 | struct CLzmaEncProps props; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 94 | LzmaEncProps_Init(&props); |
| 95 | props.dictSize = in_len; |
| 96 | props.pb = 0; /* PosStateBits, default: 2, range: 0..4 */ |
| 97 | props.lp = 0; /* LiteralPosStateBits, default: 0, range: 0..4 */ |
| 98 | props.lc = 1; /* LiteralContextBits, default: 3, range: 0..8 */ |
| 99 | props.fb = 273; /* NumFastBytes */ |
| 100 | props.mc = 0; /* MatchFinderCycles, default: 0 */ |
| 101 | props.algo = 1; /* AlgorithmNo, apparently, 0 and 1 are valid values. 0 = fast mode */ |
| 102 | props.numThreads = 1; |
| 103 | |
| 104 | switch (props.algo) { |
| 105 | case 0: // quick: HC4 |
| 106 | props.btMode = 0; |
| 107 | props.level = 1; |
| 108 | break; |
| 109 | case 1: // full: BT4 |
| 110 | default: |
| 111 | props.level = 9; |
| 112 | props.btMode = 1; |
| 113 | props.numHashBytes = 4; |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | CLzmaEncHandle p = LzmaEnc_Create(&LZMAalloc); |
| 118 | |
| 119 | int res = LzmaEnc_SetProps(p, &props); |
| 120 | if (res != SZ_OK) { |
| 121 | ERROR("LZMA: LzmaEnc_SetProps failed.\n"); |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame^] | 122 | return -1; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | unsigned char propsEncoded[LZMA_PROPS_SIZE + 8]; |
| 126 | size_t propsSize = sizeof propsEncoded; |
| 127 | res = LzmaEnc_WriteProperties(p, propsEncoded, &propsSize); |
| 128 | if (res != SZ_OK) { |
| 129 | ERROR("LZMA: LzmaEnc_WriteProperties failed.\n"); |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame^] | 130 | return -1; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | instream.p = in; |
Stefan Reinauer | 33e83ca | 2013-04-08 11:20:55 -0700 | [diff] [blame] | 134 | instream.pos = 0; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 135 | instream.size = in_len; |
| 136 | |
| 137 | outstream.p = out; |
Stefan Reinauer | 33e83ca | 2013-04-08 11:20:55 -0700 | [diff] [blame] | 138 | outstream.pos = 0; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 139 | outstream.size = in_len; |
| 140 | |
| 141 | put_64(propsEncoded + LZMA_PROPS_SIZE, in_len); |
| 142 | Write(&os, propsEncoded, LZMA_PROPS_SIZE+8); |
| 143 | |
| 144 | res = LzmaEnc_Encode(p, &os, &is, 0, &LZMAalloc, &LZMAalloc); |
| 145 | if (res != SZ_OK) { |
| 146 | ERROR("LZMA: LzmaEnc_Encode failed %d.\n", res); |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame^] | 147 | return -1; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | *out_len = outstream.pos; |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame^] | 151 | return 0; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame^] | 154 | int do_lzma_uncompress(char *dst, int dst_len, char *src, int src_len) |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 155 | { |
| 156 | if (src_len <= LZMA_PROPS_SIZE + 8) { |
| 157 | ERROR("LZMA: Input length is too small.\n"); |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame^] | 158 | return -1; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | uint64_t out_sizemax = get_64(&src[LZMA_PROPS_SIZE]); |
| 162 | |
| 163 | if (out_sizemax > (size_t) dst_len) { |
| 164 | ERROR("Not copying %d bytes to %d-byte buffer!\n", |
| 165 | (unsigned int)out_sizemax, dst_len); |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame^] | 166 | return -1; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Alexandru Gagniuc | 9ad52fe | 2014-01-27 20:57:54 -0600 | [diff] [blame] | 169 | enum ELzmaStatus status; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 170 | |
| 171 | size_t destlen = out_sizemax; |
| 172 | size_t srclen = src_len - (LZMA_PROPS_SIZE + 8); |
| 173 | |
Alexandru Gagniuc | 91e9f27 | 2014-01-26 22:55:01 -0600 | [diff] [blame] | 174 | int res = LzmaDecode((uint8_t *) dst, &destlen, |
| 175 | (uint8_t *) &src[LZMA_PROPS_SIZE + 8], &srclen, |
| 176 | (uint8_t *) &src[0], LZMA_PROPS_SIZE, |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 177 | LZMA_FINISH_END, |
| 178 | &status, |
| 179 | &LZMAalloc); |
| 180 | |
| 181 | if (res != SZ_OK) { |
| 182 | ERROR("Error while decompressing.\n"); |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame^] | 183 | return -1; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 184 | } |
Gabe Black | dbd006b | 2014-02-20 23:38:49 -0800 | [diff] [blame^] | 185 | |
| 186 | return 0; |
Stefan Reinauer | aa3f7ba | 2013-03-28 16:51:45 -0700 | [diff] [blame] | 187 | } |