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
+ ]
0 commit comments