summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/luatex/lualibs/lualibs-md5.lua
blob: 9e3f7672d6bc79c93aa669351f75f493f6b9b8f4 (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
if not modules then modules = { } end modules ['l-md5'] = {
    version   = 1.001,
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- This also provides file checksums and checkers.

if not md5 then
    md5 = optionalrequire("md5")
end

if not md5 then
    md5 = {
        sum     = function(str) print("error: md5 is not loaded (sum     ignored)") return str end,
        sumhexa = function(str) print("error: md5 is not loaded (sumhexa ignored)") return str end,
    }
end

local md5, file = md5, file
local gsub = string.gsub
local modification, isfile, touch = lfs.modification, lfs.isfile, lfs.touch
local loaddata, savedata = io.loaddata, io.savedata

-- local gsub, format, byte = string.gsub, string.format, string.byte
--
-- local function convert(str,fmt)
--     return (gsub(md5sum(str),".",function(chr) return format(fmt,byte(chr)) end))
-- end
--
-- if not md5.HEX then function md5.HEX(str) return convert(str,"%02X") end end
-- if not md5.hex then function md5.hex(str) return convert(str,"%02x") end end
-- if not md5.dec then function md5.dec(str) return convert(str,"%03i") end end

do

    local patterns = lpeg and lpeg.patterns

    if patterns then

        local bytestoHEX = patterns.bytestoHEX
        local bytestohex = patterns.bytestohex
        local bytestodec = patterns.bytestodec

        local lpegmatch = lpeg.match
        local md5sum    = md5.sum

        if not md5.HEX then function md5.HEX(str) if str then return lpegmatch(bytestoHEX,md5sum(str)) end end end
        if not md5.hex then function md5.hex(str) if str then return lpegmatch(bytestohex,md5sum(str)) end end end
        if not md5.dec then function md5.dec(str) if str then return lpegmatch(bytestodec,md5sum(str)) end end end

        md5.sumhexa = md5.hex
        md5.sumHEXA = md5.HEX

    end

end

local md5HEX = md5.HEX

function file.needsupdating(oldname,newname,threshold) -- size modification access change
    local oldtime = modification(oldname)
    if oldtime then
        local newtime = modification(newname)
        if not newtime then
            return true -- no new file, so no updating needed
        elseif newtime >= oldtime then
            return false -- new file definitely needs updating
        elseif oldtime - newtime < (threshold or 1) then
            return false -- new file is probably still okay
        else
            return true -- new file has to be updated
        end
    else
        return false -- no old file, so no updating needed
    end
end

file.needs_updating = file.needsupdating

function file.syncmtimes(oldname,newname)
    local oldtime = modification(oldname)
    if oldtime and isfile(newname) then
        touch(newname,oldtime,oldtime)
    end
end

local function checksum(name)
    if md5 then
        local data = loaddata(name)
        if data then
            return md5HEX(data)
        end
    end
    return nil
end

file.checksum = checksum

function file.loadchecksum(name)
    if md5 then
        local data = loaddata(name .. ".md5")
        return data and (gsub(data,"%s",""))
    end
    return nil
end

function file.savechecksum(name,checksum)
    if not checksum then checksum = checksum(name) end
    if checksum then
        savedata(name .. ".md5",checksum)
        return checksum
    end
    return nil
end