zlib.odin 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. package zlib
  2. import "core:compress"
  3. import "core:mem"
  4. import "core:io"
  5. import "core:bytes"
  6. import "core:hash"
  7. /*
  8. zlib.inflate decompresses a ZLIB stream passed in as a []u8 or io.Stream.
  9. Returns: Error.
  10. */
  11. Context :: compress.Context;
  12. Compression_Method :: enum u8 {
  13. DEFLATE = 8,
  14. Reserved = 15,
  15. }
  16. Compression_Level :: enum u8 {
  17. Fastest = 0,
  18. Fast = 1,
  19. Default = 2,
  20. Maximum = 3,
  21. }
  22. Options :: struct {
  23. window_size: u16,
  24. level: u8,
  25. }
  26. Error :: compress.Error;
  27. E_General :: compress.General_Error;
  28. E_ZLIB :: compress.ZLIB_Error;
  29. E_Deflate :: compress.Deflate_Error;
  30. DEFLATE_MAX_CHUNK_SIZE :: 65535;
  31. DEFLATE_MAX_LITERAL_SIZE :: 65535;
  32. DEFLATE_MAX_DISTANCE :: 32768;
  33. DEFLATE_MAX_LENGTH :: 258;
  34. HUFFMAN_MAX_BITS :: 16;
  35. HUFFMAN_FAST_BITS :: 9;
  36. HUFFMAN_FAST_MASK :: ((1 << HUFFMAN_FAST_BITS) - 1);
  37. Z_LENGTH_BASE := [31]u16{
  38. 3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,
  39. 67,83,99,115,131,163,195,227,258,0,0,
  40. };
  41. Z_LENGTH_EXTRA := [31]u8{
  42. 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0,
  43. };
  44. Z_DIST_BASE := [32]u16{
  45. 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,
  46. 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0,
  47. };
  48. Z_DIST_EXTRA := [32]u8{
  49. 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,0,0,
  50. };
  51. Z_LENGTH_DEZIGZAG := []u8{
  52. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15,
  53. };
  54. Z_FIXED_LENGTH := [288]u8{
  55. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  56. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  57. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  58. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  59. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  60. 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  61. 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  62. 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  63. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,
  64. };
  65. Z_FIXED_DIST := [32]u8{
  66. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  67. };
  68. /*
  69. Accelerate all cases in default tables.
  70. */
  71. ZFAST_BITS :: 9;
  72. ZFAST_MASK :: ((1 << ZFAST_BITS) - 1);
  73. /*
  74. ZLIB-style Huffman encoding.
  75. JPEG packs from left, ZLIB from right. We can't share code.
  76. */
  77. Huffman_Table :: struct {
  78. fast: [1 << ZFAST_BITS]u16,
  79. firstcode: [16]u16,
  80. maxcode: [17]int,
  81. firstsymbol: [16]u16,
  82. size: [288]u8,
  83. value: [288]u16,
  84. };
  85. // Implementation starts here
  86. z_bit_reverse :: #force_inline proc(n: u16, bits: u8) -> (r: u16) {
  87. assert(bits <= 16);
  88. // NOTE: Can optimize with llvm.bitreverse.i64 or some bit twiddling
  89. // by reversing all of the bits and masking out the unneeded ones.
  90. r = n;
  91. r = ((r & 0xAAAA) >> 1) | ((r & 0x5555) << 1);
  92. r = ((r & 0xCCCC) >> 2) | ((r & 0x3333) << 2);
  93. r = ((r & 0xF0F0) >> 4) | ((r & 0x0F0F) << 4);
  94. r = ((r & 0xFF00) >> 8) | ((r & 0x00FF) << 8);
  95. r >>= (16 - bits);
  96. return;
  97. }
  98. write_byte :: #force_inline proc(z: ^Context, c: u8) -> (err: io.Error) #no_bounds_check {
  99. c := c;
  100. buf := transmute([]u8)mem.Raw_Slice{data=&c, len=1};
  101. z.rolling_hash = hash.adler32(buf, z.rolling_hash);
  102. _, e := z.output->impl_write(buf);
  103. if e != .None {
  104. return e;
  105. }
  106. z.last[z.bytes_written % z.window_size] = c;
  107. z.bytes_written += 1;
  108. return .None;
  109. }
  110. allocate_huffman_table :: proc(allocator := context.allocator) -> (z: ^Huffman_Table, err: Error) {
  111. z = new(Huffman_Table, allocator);
  112. return z, nil;
  113. }
  114. build_huffman :: proc(z: ^Huffman_Table, code_lengths: []u8) -> (err: Error) {
  115. sizes: [HUFFMAN_MAX_BITS+1]int;
  116. next_code: [HUFFMAN_MAX_BITS]int;
  117. k := int(0);
  118. mem.zero_slice(sizes[:]);
  119. mem.zero_slice(z.fast[:]);
  120. for v, _ in code_lengths {
  121. sizes[v] += 1;
  122. }
  123. sizes[0] = 0;
  124. for i in 1..16 {
  125. if sizes[i] > (1 << uint(i)) {
  126. return E_Deflate.Huffman_Bad_Sizes;
  127. }
  128. }
  129. code := int(0);
  130. for i in 1..<16 {
  131. next_code[i] = code;
  132. z.firstcode[i] = u16(code);
  133. z.firstsymbol[i] = u16(k);
  134. code = code + sizes[i];
  135. if sizes[i] != 0 {
  136. if (code - 1 >= (1 << u16(i))) {
  137. return E_Deflate.Huffman_Bad_Code_Lengths;
  138. }
  139. }
  140. z.maxcode[i] = code << (16 - uint(i));
  141. code <<= 1;
  142. k += int(sizes[i]);
  143. }
  144. z.maxcode[16] = 0x10000; // Sentinel
  145. c: int;
  146. for v, ci in code_lengths {
  147. if v != 0 {
  148. c = next_code[v] - int(z.firstcode[v]) + int(z.firstsymbol[v]);
  149. fastv := u16((u16(v) << 9) | u16(ci));
  150. z.size[c] = u8(v);
  151. z.value[c] = u16(ci);
  152. if (v <= ZFAST_BITS) {
  153. j := z_bit_reverse(u16(next_code[v]), v);
  154. for j < (1 << ZFAST_BITS) {
  155. z.fast[j] = fastv;
  156. j += (1 << v);
  157. }
  158. }
  159. next_code[v] += 1;
  160. }
  161. }
  162. return nil;
  163. }
  164. decode_huffman_slowpath :: proc(z: ^Context, t: ^Huffman_Table) -> (r: u16, err: Error) #no_bounds_check {
  165. r = 0;
  166. err = nil;
  167. k: int;
  168. s: u8;
  169. code := u16(compress.peek_bits_lsb(z, 16));
  170. k = int(z_bit_reverse(code, 16));
  171. #no_bounds_check for s = HUFFMAN_FAST_BITS+1; ; {
  172. if k < t.maxcode[s] {
  173. break;
  174. }
  175. s += 1;
  176. }
  177. if (s >= 16) {
  178. return 0, E_Deflate.Bad_Huffman_Code;
  179. }
  180. // code size is s, so:
  181. b := (k >> (16-s)) - int(t.firstcode[s]) + int(t.firstsymbol[s]);
  182. if b >= size_of(t.size) {
  183. return 0, E_Deflate.Bad_Huffman_Code;
  184. }
  185. if t.size[b] != s {
  186. return 0, E_Deflate.Bad_Huffman_Code;
  187. }
  188. compress.consume_bits_lsb(z, s);
  189. r = t.value[b];
  190. return r, nil;
  191. }
  192. decode_huffman :: proc(z: ^Context, t: ^Huffman_Table) -> (r: u16, err: Error) #no_bounds_check {
  193. if z.num_bits < 16 {
  194. if z.num_bits == -100 {
  195. return 0, E_ZLIB.Code_Buffer_Malformed;
  196. }
  197. compress.refill_lsb(z);
  198. if z.eof {
  199. return 0, E_General.Stream_Too_Short;
  200. }
  201. }
  202. #no_bounds_check b := t.fast[z.code_buffer & ZFAST_MASK];
  203. if b != 0 {
  204. s := u8(b >> ZFAST_BITS);
  205. compress.consume_bits_lsb(z, s);
  206. return b & 511, nil;
  207. }
  208. return decode_huffman_slowpath(z, t);
  209. }
  210. parse_huffman_block :: proc(z: ^Context, z_repeat, z_offset: ^Huffman_Table) -> (err: Error) #no_bounds_check {
  211. #no_bounds_check for {
  212. value, e := decode_huffman(z, z_repeat);
  213. if e != nil {
  214. return err;
  215. }
  216. if value < 256 {
  217. e := write_byte(z, u8(value));
  218. if e != .None {
  219. return E_General.Output_Too_Short;
  220. }
  221. } else {
  222. if value == 256 {
  223. // End of block
  224. return nil;
  225. }
  226. value -= 257;
  227. length := Z_LENGTH_BASE[value];
  228. if Z_LENGTH_EXTRA[value] > 0 {
  229. length += u16(compress.read_bits_lsb(z, Z_LENGTH_EXTRA[value]));
  230. }
  231. value, e = decode_huffman(z, z_offset);
  232. if e != nil {
  233. return E_Deflate.Bad_Huffman_Code;
  234. }
  235. distance := Z_DIST_BASE[value];
  236. if Z_DIST_EXTRA[value] > 0 {
  237. distance += u16(compress.read_bits_lsb(z, Z_DIST_EXTRA[value]));
  238. }
  239. if z.bytes_written < i64(distance) {
  240. // Distance is longer than we've decoded so far.
  241. return E_Deflate.Bad_Distance;
  242. }
  243. offset := i64(z.bytes_written - i64(distance));
  244. /*
  245. These might be sped up with a repl_byte call that copies
  246. from the already written output more directly, and that
  247. update the Adler checksum once after.
  248. That way we'd suffer less Stream vtable overhead.
  249. */
  250. if distance == 1 {
  251. /*
  252. Replicate the last outputted byte, length times.
  253. */
  254. if length > 0 {
  255. b, e := compress.peek_back_byte(z, offset);
  256. if e != .None {
  257. return E_General.Output_Too_Short;
  258. }
  259. #no_bounds_check for _ in 0..<length {
  260. write_byte(z, b);
  261. }
  262. }
  263. } else {
  264. if length > 0 {
  265. #no_bounds_check for _ in 0..<length {
  266. b, e := compress.peek_back_byte(z, offset);
  267. if e != .None {
  268. return E_General.Output_Too_Short;
  269. }
  270. write_byte(z, b);
  271. offset += 1;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. inflate_from_stream :: proc(using ctx: ^Context, raw := false, allocator := context.allocator) -> (err: Error) #no_bounds_check {
  279. /*
  280. ctx.input must be an io.Stream backed by an implementation that supports:
  281. - read
  282. - size
  283. ctx.output must be an io.Stream backed by an implementation that supports:
  284. - write
  285. raw determines whether the ZLIB header is processed, or we're inflating a raw
  286. DEFLATE stream.
  287. */
  288. if !raw {
  289. data_size := io.size(ctx.input);
  290. if data_size < 6 {
  291. return E_General.Stream_Too_Short;
  292. }
  293. cmf, _ := compress.read_u8(ctx);
  294. method := Compression_Method(cmf & 0xf);
  295. if method != .DEFLATE {
  296. return E_General.Unknown_Compression_Method;
  297. }
  298. cinfo := (cmf >> 4) & 0xf;
  299. if cinfo > 7 {
  300. return E_ZLIB.Unsupported_Window_Size;
  301. }
  302. ctx.window_size = 1 << (cinfo + 8);
  303. flg, _ := compress.read_u8(ctx);
  304. fcheck := flg & 0x1f;
  305. fcheck_computed := (cmf << 8 | flg) & 0x1f;
  306. if fcheck != fcheck_computed {
  307. return E_General.Checksum_Failed;
  308. }
  309. fdict := (flg >> 5) & 1;
  310. /*
  311. We don't handle built-in dictionaries for now.
  312. They're application specific and PNG doesn't use them.
  313. */
  314. if fdict != 0 {
  315. return E_ZLIB.FDICT_Unsupported;
  316. }
  317. // flevel := Compression_Level((flg >> 6) & 3);
  318. /*
  319. Inflate can consume bits belonging to the Adler checksum.
  320. We pass the entire stream to Inflate and will unget bytes if we need to
  321. at the end to compare checksums.
  322. */
  323. // Seed the Adler32 rolling checksum.
  324. ctx.rolling_hash = 1;
  325. }
  326. // Parse ZLIB stream without header.
  327. err = inflate_raw(ctx);
  328. if err != nil {
  329. return err;
  330. }
  331. if !raw {
  332. compress.discard_to_next_byte_lsb(ctx);
  333. adler32 := compress.read_bits_lsb(ctx, 8) << 24 | compress.read_bits_lsb(ctx, 8) << 16 | compress.read_bits_lsb(ctx, 8) << 8 | compress.read_bits_lsb(ctx, 8);
  334. if ctx.rolling_hash != u32(adler32) {
  335. return E_General.Checksum_Failed;
  336. }
  337. }
  338. return nil;
  339. }
  340. // @(optimization_mode="speed")
  341. inflate_from_stream_raw :: proc(z: ^Context, allocator := context.allocator) -> (err: Error) #no_bounds_check {
  342. final := u32(0);
  343. type := u32(0);
  344. z.num_bits = 0;
  345. z.code_buffer = 0;
  346. z_repeat: ^Huffman_Table;
  347. z_offset: ^Huffman_Table;
  348. codelength_ht: ^Huffman_Table;
  349. z_repeat, err = allocate_huffman_table(allocator=context.allocator);
  350. if err != nil {
  351. return err;
  352. }
  353. z_offset, err = allocate_huffman_table(allocator=context.allocator);
  354. if err != nil {
  355. return err;
  356. }
  357. codelength_ht, err = allocate_huffman_table(allocator=context.allocator);
  358. if err != nil {
  359. return err;
  360. }
  361. defer free(z_repeat);
  362. defer free(z_offset);
  363. defer free(codelength_ht);
  364. if z.window_size == 0 {
  365. z.window_size = DEFLATE_MAX_DISTANCE;
  366. }
  367. // Allocate rolling window buffer.
  368. last_b := mem.make_dynamic_array_len_cap([dynamic]u8, z.window_size, z.window_size, allocator);
  369. z.last = &last_b;
  370. defer delete(last_b);
  371. for {
  372. final = compress.read_bits_lsb(z, 1);
  373. type = compress.read_bits_lsb(z, 2);
  374. // fmt.printf("Final: %v | Type: %v\n", final, type);
  375. switch type {
  376. case 0:
  377. // Uncompressed block
  378. // Discard bits until next byte boundary
  379. compress.discard_to_next_byte_lsb(z);
  380. uncompressed_len := i16(compress.read_bits_lsb(z, 16));
  381. length_check := i16(compress.read_bits_lsb(z, 16));
  382. // fmt.printf("LEN: %v, ~LEN: %v, NLEN: %v, ~NLEN: %v\n", uncompressed_len, ~uncompressed_len, length_check, ~length_check);
  383. if ~uncompressed_len != length_check {
  384. return E_Deflate.Len_Nlen_Mismatch;
  385. }
  386. /*
  387. TODO: Maybe speed this up with a stream-to-stream copy (read_from)
  388. and a single Adler32 update after.
  389. */
  390. #no_bounds_check for uncompressed_len > 0 {
  391. compress.refill_lsb(z);
  392. lit := compress.read_bits_lsb(z, 8);
  393. write_byte(z, u8(lit));
  394. uncompressed_len -= 1;
  395. }
  396. case 3:
  397. return E_Deflate.BType_3;
  398. case:
  399. // log.debugf("Err: %v | Final: %v | Type: %v\n", err, final, type);
  400. if type == 1 {
  401. // Use fixed code lengths.
  402. err = build_huffman(z_repeat, Z_FIXED_LENGTH[:]);
  403. if err != nil {
  404. return err;
  405. }
  406. err = build_huffman(z_offset, Z_FIXED_DIST[:]);
  407. if err != nil {
  408. return err;
  409. }
  410. } else {
  411. lencodes: [286+32+137]u8;
  412. codelength_sizes: [19]u8;
  413. //i: u32;
  414. n: u32;
  415. compress.refill_lsb(z, 14);
  416. hlit := compress.read_bits_no_refill_lsb(z, 5) + 257;
  417. hdist := compress.read_bits_no_refill_lsb(z, 5) + 1;
  418. hclen := compress.read_bits_no_refill_lsb(z, 4) + 4;
  419. ntot := hlit + hdist;
  420. #no_bounds_check for i in 0..<hclen {
  421. s := compress.read_bits_lsb(z, 3);
  422. codelength_sizes[Z_LENGTH_DEZIGZAG[i]] = u8(s);
  423. }
  424. err = build_huffman(codelength_ht, codelength_sizes[:]);
  425. if err != nil {
  426. return err;
  427. }
  428. n = 0;
  429. c: u16;
  430. for n < ntot {
  431. c, err = decode_huffman(z, codelength_ht);
  432. if err != nil {
  433. return err;
  434. }
  435. if c < 0 || c >= 19 {
  436. return E_Deflate.Huffman_Bad_Code_Lengths;
  437. }
  438. if c < 16 {
  439. lencodes[n] = u8(c);
  440. n += 1;
  441. } else {
  442. fill := u8(0);
  443. compress.refill_lsb(z, 7);
  444. switch c {
  445. case 16:
  446. c = u16(compress.read_bits_no_refill_lsb(z, 2) + 3);
  447. if n == 0 {
  448. return E_Deflate.Huffman_Bad_Code_Lengths;
  449. }
  450. fill = lencodes[n - 1];
  451. case 17:
  452. c = u16(compress.read_bits_no_refill_lsb(z, 3) + 3);
  453. case 18:
  454. c = u16(compress.read_bits_no_refill_lsb(z, 7) + 11);
  455. case:
  456. return E_Deflate.Huffman_Bad_Code_Lengths;
  457. }
  458. if ntot - n < u32(c) {
  459. return E_Deflate.Huffman_Bad_Code_Lengths;
  460. }
  461. nc := n + u32(c);
  462. #no_bounds_check for ; n < nc; n += 1 {
  463. lencodes[n] = fill;
  464. }
  465. }
  466. }
  467. if n != ntot {
  468. return E_Deflate.Huffman_Bad_Code_Lengths;
  469. }
  470. err = build_huffman(z_repeat, lencodes[:hlit]);
  471. if err != nil {
  472. return err;
  473. }
  474. err = build_huffman(z_offset, lencodes[hlit:ntot]);
  475. if err != nil {
  476. return err;
  477. }
  478. }
  479. err = parse_huffman_block(z, z_repeat, z_offset);
  480. // log.debugf("Err: %v | Final: %v | Type: %v\n", err, final, type);
  481. if err != nil {
  482. return err;
  483. }
  484. }
  485. if final == 1 {
  486. break;
  487. }
  488. }
  489. return nil;
  490. }
  491. inflate_from_byte_array :: proc(input: []u8, buf: ^bytes.Buffer, raw := false) -> (err: Error) {
  492. ctx := Context{};
  493. r := bytes.Reader{};
  494. bytes.reader_init(&r, input);
  495. rs := bytes.reader_to_stream(&r);
  496. ctx.input = rs;
  497. buf := buf;
  498. ws := bytes.buffer_to_stream(buf);
  499. ctx.output = ws;
  500. err = inflate_from_stream(&ctx, raw);
  501. return err;
  502. }
  503. inflate_from_byte_array_raw :: proc(input: []u8, buf: ^bytes.Buffer, raw := false) -> (err: Error) {
  504. return inflate_from_byte_array(input, buf, true);
  505. }
  506. inflate :: proc{inflate_from_stream, inflate_from_byte_array};
  507. inflate_raw :: proc{inflate_from_stream_raw, inflate_from_byte_array_raw};