summaryrefslogtreecommitdiff
path: root/cd-tool.lua
blob: d3e592e8051f5172d7b816234db579390c5447a4 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
 ------------------
-- misc functions --
 ------------------

function extract_file(source, dest, mode)
    if (mode == nil) then
	source = cdfile(source)
    else
	source = cdfile(source, mode)
    end
    dest = Output(dest)
    source:copyto(dest)
end

function insert_file(source, dest, mode)
    if (type(source) == "string") then
        source = Input(source)
    end
    if (type(dest) == "string") then
	dest = findpath(dest)
    end
    if (mode == nil) then
	writefile(source, -1, dest.Sector)
    else
	writefile(source, -1, dest.Sector, mode)
    end
end

function display(inp, n)
    local i
    if (type(inp) == "string") then
        inp = Input(inp)
    elseif (type(inp) ~= "table") then
        error("Display needs a string or an Input object")
    end
    
    i = 0
    
    while(not inp:isclosed()) do
	i = i + 1
        print(inp:read())
	if ((n ~= nil) and (i >= n)) then
	    return
	end
    end
end

function pchar(n)
    if (not ((n >= 32) and (n <= 127))) then
        n = 46 -- aka '.' or 0x2e
    end
    return hex(n, "%c")
end

function hexdump(inp, from, to, width)
    local size, nlines, remaining, data_array, line, byte, outstring
    
    if (type(inp) == "string") then
        inp = Input(inp)
    elseif (type(inp) ~= "table") then
        error("Hexdump needs a string or an Input object")
    end
    
    size = inp:getsize()

    if (from == nil) then
        from = 0
    end
    
    if (to == nil) then
        to = size
    end
    
    if (to > size) then
        to = size
    end
    
    size = to - from
    
    if (width == nil) then
        width = 16
    end
    
    nlines = math.floor(size / width)
    remaining = math.mod(size, width)
    inp:seek(from)
    data_array = inp:read(size)
    
    for line = 0, nlines - 1, 1 do
        outstring = hex(line * width + from, "%08x   ")
        for byte = 0, width - 1, 1 do
            outstring = outstring .. hex(data_array[line * 16 + byte]) .. " "
        end
        outstring = outstring .. "  "
        for byte = 0, width - 1, 1 do
            outstring = outstring .. pchar(data_array[line * 16 + byte])
        end
        print(outstring)
    end
    
    if (remaining == 0) then
        return
    end
    
    outstring = hex(nlines * width + from, "%08x   ");
    for byte = 0, remaining - 1, 1 do
        outstring = outstring .. hex(data_array[nlines * 16 + byte]) .. " "
    end
    for byte = remaining + 1, width - 1, 1 do
        outstring = outstring .. "   "
    end
    outstring = outstring .. "  "
    for byte = 0, remaining - 1, 1 do
        outstring = outstring .. pchar(data_array[nlines * 16 + byte])
    end

    print(outstring)
end


 --------------------------
-- cdutil object wrappers --
 --------------------------

function check_cdutil()
    if (cdutil == nil) then error "cdutil object non existant" end
end

function cdfile(arg1, arg2, arg3, arg4)    
    local cdutil_implied = false
    
    if ((type(arg1) ~= "table") or (arg1.cdfile == nil)) then
	check_cdutil()
        cdutil_implied = true
	if (type(arg1) == "string") then
	    arg1 = findpath(arg1)
	end
    else
	if (type(arg2) == "string") then
	    arg2 = findpath(arg2)
	end
    end
    
    if ((arg2 == nil) and (arg3 == nil) and (arg4 == nil)) then
        return cdutil:cdfile(arg1)
    elseif ((arg3 == nil) and (arg4 == nil)) then
        if (cdutil_implied) then
            return cdutil:cdfile(arg1, arg2)
        else
            return arg1:cdfile(arg2)
        end
    elseif (arg4 == nil) then
        if (cdutil_implied) then
            return cdutil:cdfile(arg1, arg2, arg3)
        else
            return arg1:cdfile(arg2, arg3)
        end
    else
        return arg1:cdfile(arg2, arg3, arg4)
    end
end

function setisow(iso_w)
    check_cdutil()
    return cdutil:setisow(iso_w)
end

function guessmode(sect)
    check_cdutil()
    if (sect == nil) then
        return cdutil:guessmode()
    else
        return cdutil:guessmode(sect)
    end
end

function sectorseek(sect)
    check_cdutil()
    return cdutil:sectorseek(sect)
end

function readsector(sect, mode)
    check_cdutil()
    if (sect == nil) then
        return cdutil:readsector()
    elseif (mode == nil) then
        return cdutil:readsector(sect)
    else
        return cdutil:readsector(sect, mode)
    end
end

function readdatas(size, sector, mode)
    check_cdutil()
    if (sect == nil) then
        return cdutil:readdatas(size)
    elseif (mode == nil) then
        return cdutil:readdatas(size, sect)
    else
        return cdutil:readdatas(size, sect, mode)
    end
end

function readfile(handle, size, sector, mode)
    check_cdutil()
    if (sect == nil) then
        return cdutil:readfile(handle, size)
    elseif (mode == nil) then
        return cdutil:readfile(handle, size, sect)
    else
        return cdutil:readfile(handle, size, sect, mode)
    end
end

function writesector(array, sector, mode)
    check_cdutil()
    if (sector == nil) then
        return cdutil:writesector(array, sector)
    elseif (mode == nil) then
        return cdutil:writesector(array, sector, mode)
    end
end

function writedatas(array, size, sector, mode)
    check_cdutil()
    if (sector == nil) then
        return cdutil:writedatas(array, size)
    elseif (mode == nil) then
        return cdutil:writedatas(array, size, sector)
    else
        return cdutil:writedatas(array, size, sector, mode)
    end
end

function writefile(handle, size, sector, mode)
    check_cdutil()
    if (size == nil) then
        return cdutil:writefile(handle)
    elseif (sector == nil) then
        return cdutil:writefile(handle, size)
    elseif (mode == nil) then
        return cdutil:writefile(handle, size, sector)
    else
        return cdutil:writefile(handle, size, sector, mode)
    end
end

function findpath(path)
    check_cdutil()
    if (findpath == nil) then
	return cdutil:findpath "/"
    else
	return cdutil:findpath(path)
    end
end

function findparent(path)
    check_cdutil()
    return cdutil:findparent(path)
end

function finddirectory(dir, path)
    check_cdutil()
    return cdutil:finddirectory(dir, path)
end


 -----------------------
-- iso object wrappers --
 -----------------------

function check_iso()
    if (iso == nil) then error "iso object non existant" end
end

function foreword(lcdutil)
    check_iso()
    if ((lcdutil == nil) and (cdutil == nil)) then error "cdutil object non existant" end
    if (lcdutil == nil) then
        return iso:foreword(cdutil)
    else
        return iso:foreword(lcdutil)
    end
end

function foreword_handle(handle, mode)
    check_iso()
    if (mode == nil) then
        return iso:foreword_handle(handle)
    else
        return iso:foreword_handle(handle, mode)
    end
end

function foreword_array(array, mode)
    check_iso()
    if (mode == nil) then
        return iso:foreword_array(array)
    else
        return iso:foreword_array(array, mode)
    end
end

function getdispsect()
    check_iso()
    return iso:getdispsect()
end

function putfile(handle, mode, sector)
    check_iso()
    if (mode == nil) then
        iso:putfile(handle)
    elseif (sector == nil) then
        iso:putfile(handle, mode)
    else
        iso:putfile(handle, mode, sector)
    end
end

function putdatas(array, size, mode, sector)
    check_iso()
    if (mode == nil) then
        iso:putdatas(array, size)
    elseif (sector == nil) then
        iso:putdatas(array, size, mode)
    else
        iso:putdatas(array, size, mode, sector)
    end
end

function createsector(array, mode, sector)
    check_iso()
    if (mode == nil) then
        iso:createsector(array)
    elseif (sector == nil) then
        iso:createsector(array, mode)
    else
        iso:createsector(array, mode, sector)
    end
end

function setEOF()
    check_iso()
    iso:setEOF()
end

function clearEOF()
    check_iso()
    iso:clearEOF()
end

function setbasics(pvd, rootsize, ptsize, nvd, rootsect)
    check_iso()
    if (rootsize == nil) then
        iso:setbasics(pvd)
    elseif (ptsize == nil) then
        iso:setbasics(pvd, rootsize)
    elseif (nvd == nil) then
        iso:setbasics(pvd, rootsize, ptsize)
    elseif (rootsect) then
        iso:setbasics(pvd, rootsize, ptsize, nvd)
    else
        iso:setbasics(pvd, rootsize, ptsize, nvd, rootsect)
    end
end

function createdir(dirtree, name, size, direntry, mode)
    check_iso()
    if (size == nil) then
        iso:createdir(dirtree, name)
    elseif (mode == nil) then
        iso:createdir(dirtree, name, size, direntry)
    else
        iso:createdir(dirtree, name, size, direntry, mode)
    end
end

function createfile(dirtree, name, size, direntry, mode)
    check_iso()
    if (mode == nil) then
        iso:createfile(dirtree, name, size, direntry)
    else
        iso:createfile(dirtree, name, size, direntry, mode)
    end
end

function copydir(dirtree, cdutils, direntry, mode)
    check_iso()
    if (mode == nil) then
        iso:copydir(dirtree, cdutils, direntry)
    else
        iso:copydir(dirtree, cdutils, direntry, mode)
    end
end

function close(cuefile, mode, nsectors)
    check_iso()
    if (cuefile == nil) then
        iso:close()
    elseif (mode == nil) then
        iso:close(cuefile)
    elseif (nsectors == nil) then
        iso:close(cuefile, mode)
    else
        iso:close(cuefile, mode, nsectors)
    end
end