Skip to content

Commit 096f254

Browse files
committed
FEAT: simple GZIP codec
1 parent 849b0aa commit 096f254

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

src/mezz/boot-files.r

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ REBOL [
5151
%codec-wav.r
5252
%codec-der.r
5353
%codec-crt.r
54+
%codec-gzip.r
5455
]
5556

5657
;-- protocols:

src/mezz/codec-gzip.r

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
REBOL [
2+
title: "REBOL 3 coded lossless compressed data format compatible with GZIP utility."
3+
name: 'codec-gzip
4+
author: rights: "Oldes"
5+
version: 0.0.1
6+
specification: https://tools.ietf.org/html/rfc1952
7+
]
8+
9+
register-codec [
10+
name: 'GZIP
11+
title: "Lossless compressed data format compatible with GZIP utility."
12+
suffixes: [%.gz]
13+
decode: function[data [binary!]] [
14+
if verbose > 0 [ identify data ]
15+
decompress/gzip data
16+
]
17+
18+
encode: function [data [binary!]][
19+
compress/gzip/level data level
20+
]
21+
22+
identify: function [data [binary!]][
23+
if verbose > 0 [
24+
print ["^[[1;32mDecode GZIP data^[[m (^[[1m" length? data "^[[mbytes )"]
25+
]
26+
bin: binary data
27+
binary/read bin [
28+
id: UI16 ; Magick: #{1F8B}
29+
cm: UI8 ; Compression Method
30+
flg: UI8 ; FLaGs
31+
mtime: UI32LE ; Modification TIME
32+
xfl: UI8 ; eXtra FLags
33+
os: UI8 ; Operating System
34+
]
35+
if any [
36+
id <> 8075 ;- not GZIP magick number
37+
cm <> 8 ;- unsupported compression
38+
][ return none ]
39+
40+
mtime: either mtime > 0 [ 1-1-1970 + to time! mtime ][ none ]
41+
42+
if 4 = (4 and flg) [ ;FEXTRA
43+
extra: binary/read bin 'UI16LEBYTES
44+
]
45+
if 8 = (8 and flg) [ ;FNAME
46+
name: to file! binary/read bin 'STRING ; zero terminated
47+
]
48+
if 16 = (16 and flg) [ ;FCOMMENT
49+
comm: to file! binary/read bin 'STRING ; zero terminated
50+
]
51+
if 2 = (2 and flg) [ ;FHCRC
52+
;the two least significant bytes of the CRC32 for all bytes of the gzip header up to and not including the CRC16
53+
;checksum/method/part bin/buffer-write 'CRC32 index? bin/buffer
54+
crc16: binary/read bin 'UI16LE
55+
]
56+
if verbose > 0 [
57+
print ["^[[32mModified: ^[[0;1m" mtime "^[[m"]
58+
print ["^[[32mExtra flags: ^[[0;1m" xfl "^[[m"]
59+
print ["^[[32mOperating system: ^[[0;1m" os "^[[m"]
60+
print ["^[[32mExtra field: ^[[0;1m" extra "^[[m"]
61+
print ["^[[32mFile name: ^[[0;1m" name "^[[m"]
62+
print ["^[[32mHeader CRC: ^[[0;1m" crc16 "^[[m"]
63+
]
64+
compose [
65+
file: (all [name to file! name])
66+
modified: (mtime)
67+
os: (os)
68+
extra-flags: (xfl)
69+
extra-data: (extra)
70+
]
71+
]
72+
73+
verbose: 1
74+
level: 9
75+
]

src/tests/units/compress-test.r3

+8
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ data: "test test test"
7070
--assert #{74657374} = decompress/gzip compress/gzip/part data 4
7171
--assert #{74657374} = decompress/gzip compress/gzip/part skip data 5 4
7272
--assert #{74657374} = decompress/gzip compress/gzip/part tail data -4
73+
74+
--test-- "GZIP codec"
75+
--assert binary? alice: load %units/files/alice29.txt.gz
76+
--assert #{37A087D23C8709E97AA45ECE662FAF3D07006A58} = checksum/method alice 'sha1
77+
--assert port? save %units/files/tmp.gz alice
78+
--assert binary? alice: load %units/files/tmp.gz
79+
--assert #{37A087D23C8709E97AA45ECE662FAF3D07006A58} = checksum/method alice 'sha1
80+
delete %units/files/tmp.gz
7381

7482
===end-group===
7583

src/tests/units/files/alice29.txt.gz

50.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)