summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/l-gzip.lua
blob: f141b5ebbbe9ba52201bced44ada4692c78a932e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
if not modules then modules = { } end modules ['l-gzip'] = {
    version   = 1.001,
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- We only have a few official methods here:
--
--   local decompressed = gzip.load       (filename)
--   local resultsize   = gzip.save       (filename,compresslevel)
--   local compressed   = gzip.compress   (str,compresslevel)
--   local decompressed = gzip.decompress (str)
--   local iscompressed = gzip.compressed (str)
--   local suffix, okay = gzip.suffix     (filename)
--
-- In LuaMetaTeX we have only xzip which implements a very few methods:
--
--   compress   (str,level,method,window,memory,strategy)
--   decompress (str,window)
--   adler32    (str,checksum)
--   crc32      (str,checksum)
--
-- Special window values are:
--
-- flate : - 15
-- zlib  :   15
-- gzip  :   15 | 16
-- auto  :   15 | 32

gzip = gzip or { } -- so in luatex we keep the old ones too

if not zlib then
    zlib = xzip    -- in luametatex we shadow the old one
elseif not xzip then
    xzip = zlib
end

if zlib then

    local suffix     = file.suffix
    local suffixes   = file.suffixes
    local find       = string.find
    local openfile   = io.open

    local gzipwindow = 15 + 16 -- +16: gzip, +32: gzip|zlib
    local gziplevel  = 3
    local identifier = "^\x1F\x8B\x08"

    local compress   = zlib.compress
    local decompress = zlib.decompress

    function gzip.load(filename)
        local f = openfile(filename,"rb")
        if not f then
            -- invalid file
        else
            local data = f:read("*all")
            f:close()
            if data and data ~= "" then
                if suffix(filename) == "gz" then
                    data = decompress(data,gzipwindow)
                end
                return data
            end
        end
    end

    function gzip.save(filename,data,level)
        if suffix(filename) ~= "gz" then
            filename = filename .. ".gz"
        end
        local f = openfile(filename,"wb")
        if f then
            data = compress(data or "",level or gziplevel,nil,gzipwindow)
            f:write(data)
            f:close()
            return #data
        end
    end

    function gzip.suffix(filename)
        local suffix, extra = suffixes(filename)
        local gzipped = extra == "gz"
        return suffix, gzipped
    end

    function gzip.compressed(s)
        return s and find(s,identifier)
    end

    function gzip.compress(s,level)
        if s and not find(s,identifier) then -- the find check might go away
            if not level then
                level = gziplevel
            elseif level <= 0 then
                return s
            elseif level > 9 then
                level = 9
            end
            return compress(s,level or gziplevel,nil,gzipwindow) or s
        end
    end

    function gzip.decompress(s)
        if s and find(s,identifier) then
            return decompress(s,gzipwindow)
        else
            return s
        end
    end

end

-- In luametatex we can use this one but it doesn't look like there wil be stream
-- support so for now we still use zlib (the performance difference is not that
-- spectacular in our usage.

-- if flate then
--
--     local type = type
--     local find = string.find
--
--     local compress   = flate.gz_compress
--     local decompress = flate.gz_decompress
--
--     local absmax     = 128*1024*1024
--     local initial    =       64*1024
--     local identifier = "^\x1F\x8B\x08"
--
--     function gzip.compressed(s)
--         return s and find(s,identifier)
--     end
--
--     function gzip.compress(s,level)
--         if s and not find(s,identifier) then -- the find check might go away
--             if not level then
--                 level = 3
--             elseif level <= 0 then
--                 return s
--             elseif level > 9 then
--                 level = 9
--             end
--             return compress(s,level) or s
--         end
--     end
--
--     function gzip.decompress(s,size,iterate)
--         if s and find(s,identifier) then
--             if type(size) ~= "number" then
--                 size = initial
--             end
--             if size > absmax then
--                 size = absmax
--             end
--             if type(iterate) == "number" then
--                 max = size * iterate
--             elseif iterate == nil or iterate == true then
--                 iterate = true
--                 max     = absmax
--             end
--             if max > absmax then
--                 max = absmax
--             end
--             while true do
--                 local d = decompress(s,size)
--                 if d then
--                     return d
--                 end
--                 size = 2 * size
--                 if not iterate or size > max then
--                     return false
--                 end
--             end
--         else
--             return s
--         end
--     end
--
-- end