summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/data-dec.lua
blob: 2a62b7dd91a01f015d746bde50ee8d4c48b0880f (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
if not modules then modules = { } end modules ['data-dec'] = {
    version   = 1.001,
    comment   = "companion to luat-lib.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local loaddata = io.loaddata
local suffix   = file.suffix
local resultof = os.resultof

local decompressors     = { }
resolvers.decompressors = decompressors

local decompresslzma = nil
local decompressgzip = gzip.decompress

local function decompressed(k)
    local s = suffix(k)
    if s == "xz" then
        if decompresslzma == nil then
            local lzma = require(resolvers.findfile("libs-imp-lzma.lmt"))
            if lzma then
                local decompress = lzma.decompress
                decompresslzma = function(name)
                    return decompress(loaddata(k))
                end
            else
                decompresslzma = function(name)
                    -- todo: use a proper runner
                    return resultof("xz -d -c -q -q " .. name)
                end
            end
        end
        return decompresslzma(k)
    elseif s == "gz" then
        return decompressgzip(loaddata(k))
    end
end

local cache = table.setmetatableindex(function(t,k)
    local v = decompressed(k) or false
    t[k] = v
    return v
end)

decompressors.decompress = decompress

function decompressors.register(filename)
    return cache[filename]
end

function decompressors.unregister(filename)
    cache[filename] = nil
end