Debian Codegraph @

texk/web2c/luatexdir/lua/luatex-core.lua

raw symbols SCIP
1 -- luatex-core security and io overloads ..
2
3 -- if not modules then modules = { } end modules ['luatex-core'] = {
4 -- version = 1.112,
5 -- comment = 'companion to luatex',
6 -- author = 'Hans Hagen & Luigi Scarso',
7 -- copyright = 'LuaTeX Development Team',
8 -- }
9
10 LUATEXCOREVERSION = 1.180 -- we reflect the luatex version where changes happened
11
12 -- This file overloads some Lua functions. The readline variants provide the same
13 -- functionality as LuaTeX <= 1.04 and doing it this way permits us to keep the
14 -- original io libraries clean. Performance is probably even a bit better now.
15
16 -- We test for functions already being defined so that we don't overload ones that
17 -- are provided in the startup script.
18
19
20
21 local saferoption = status.safer_option
22 local luadebugoption = status.luadebug_option
23 local shellescape = status.shell_escape -- 0 (disabled) 1 (anything) 2 (restricted)
24 local kpseused = status.kpse_used -- 0 1
25 local gmatch = string.gmatch
26
27 --
28 -- Useful extension of lfs.mkdir,
29 -- lfs.mkdirp(path) make parent directories as needed
30 -- (from luatex-fonts-merged.lua)
31 --
32 function lfs.mkdirp(path)
33 local full=""
34 local r1,r2,r3
35 for sub in gmatch(path,"(/*[^\\/]+)") do
36 full=full..sub
37 r1,r2,r3 = lfs.mkdir(full)
38 end
39 return r1,r2,r3
40 end
41
42
43
44 if kpseused == 1 and (status.shell_escape ~=1) then
45
46 local type = type
47 local gsub = string.gsub
48 local find = string.find
49
50 local mt = getmetatable(io.stderr)
51 local mt_lines = mt.lines
52
53 local kpse_checkpermission = kpse.check_permission
54 local kpse_recordinputfile = kpse.record_input_file
55 local kpse_recordoutputfile = kpse.record_output_file
56
57 local kpse_in_name_ok_silent_extended = kpse.in_name_ok_silent_extended
58 local kpse_out_name_ok_silent_extended = kpse.out_name_ok_silent_extended
59
60 local io_open = io.open
61 -- local io_popen = io.popen -- not need, we use os.kpsepopen
62 local io_lines = io.lines
63
64 local fio_readline = fio.readline
65
66 local write_nl = texio.write_nl
67 local format = string.format
68
69 io.saved_lines = io_lines -- always readonly
70 mt.saved_lines = mt_lines -- always readonly
71
72 local os_rename = os.rename
73 local os_remove = os.remove
74
75 local lfs_attributes = lfs.attributes
76 local lfs_chdir = lfs.chdir
77 local lfs_lock_dir = lfs.lock_dir
78 local lfs_dir = lfs.dir
79 local lfs_link = lfs.link
80 local lfs_mkdir = lfs.mkdir
81 local lfs_mkdirp = lfs.mkdirp
82 local lfs_rmdir = lfs.rmdir
83 local lfs_symlinkattributes = lfs.symlinkattributes
84 local lfs_touch = lfs.touch
85
86
87
88 local LUATEX_EPERM = -1
89 local LUATEX_EPERM_MSG = "LuaTeX: operation not permitted"
90
91 local function luatex_io_open(name,how)
92 if not how then
93 how = 'r'
94 end
95 local check = true
96 if how == 'r' or how == 'rb' or how == '' then
97 check = kpse_in_name_ok_silent_extended(name)
98 else
99 check = kpse_out_name_ok_silent_extended(name)
100 end
101 local f = nil
102 if check then
103 f = io_open(name,how)
104 if f then
105 if type(how) == 'string' and find(how,'w') then
106 kpse_recordoutputfile(name,'w')
107 else
108 kpse_recordinputfile(name,'r')
109 end
110 end
111 end
112 return f
113 end
114
115 local function luatex_io_open_readonly(name,how)
116 if not how then
117 how = 'r'
118 else
119 how = gsub(how,'[^rb]','')
120 if how == '' then
121 how = 'r'
122 end
123 end
124 local check = false
125 if how == 'r' or how == 'rb' or how == '' then
126 check = kpse_in_name_ok_silent_extended(name)
127 end
128 local f = nil
129 if check then
130 f = io_open(name,how)
131 if f then
132 fio_recordfilename(name,'r')
133 end
134 end
135 return f
136 end
137
138 -- not need, we use os.kpsepopen
139 --local function luatex_io_popen(name,...)
140 -- local okay, found = kpse_checkpermission(name)
141 -- if okay and found then
142 -- return io_popen(found,...)
143 -- end
144 --end
145
146 -- local function luatex_io_lines(name,how)
147 -- if name then
148 -- local f = io_open(name,how or 'r')
149 -- if f then
150 -- return function()
151 -- return fio_readline(f)
152 -- end
153 -- end
154 -- else
155 -- return io_lines()
156 -- end
157 -- end
158
159 -- For some reason the gc doesn't kick in so we need to close explicitly
160 -- so that the handle is flushed.
161
162 local error, type = error, type
163
164 local function luatex_io_lines(name,how)
165 if type(name) == "string" then
166 local check = kpse_in_name_ok_silent_extended(name)
167 local f = check and io_open(name,how or 'r')
168 if f then
169 return function()
170 local l = fio_readline(f)
171 if not l then
172 f:close()
173 end
174 return l
175 end
176 else
177 -- for those who like it this way:
178 error("patched 'io.lines' can't open '" .. name .. "'")
179 end
180 else
181 return io_lines()
182 end
183 end
184
185 local function luatex_io_readline(f)
186 return function()
187 return fio_readline(f)
188 end
189 end
190
191 --
192 --
193 -- These functions must pass
194 -- kpse.in_name_ok_silent_extended
195 -- and kpse.out_name_ok_silent_extended
196 --
197
198 local function luatex_os_rename(oldname,newname)
199 local check1 = kpse_in_name_ok_silent_extended(oldname)
200 local check2 = kpse_out_name_ok_silent_extended(newname)
201 if check1 and check2 then
202 return os_rename(oldname,newname)
203 else
204 return nil, LUATEX_EPERM_MSG,LUATEX_EPERM
205 end
206 end
207
208 local function luatex_os_remove(filename)
209 local check1 = kpse_in_name_ok_silent_extended(filename)
210 local check2 = kpse_out_name_ok_silent_extended(filename)
211 if check1 and check2 then
212 return os_remove(filename)
213 else
214 return nil, LUATEX_EPERM_MSG,LUATEX_EPERM
215 end
216 end
217
218 local function luatex_lfs_attributes(filepath, opt)
219 local check1 = kpse_in_name_ok_silent_extended(filepath)
220 if check1 then
221 return lfs_attributes(filepath,opt)
222 else
223 return nil, LUATEX_EPERM_MSG,LUATEX_EPERM
224 end
225 end
226
227 local function luatex_lfs_chdir(name)
228 local check1 = kpse_in_name_ok_silent_extended(name) and kpse_out_name_ok_silent_extended(name)
229 if check1 then
230 return lfs_chdir(name)
231 else
232 return nil, LUATEX_EPERM_MSG
233 end
234 end
235
236 local function luatex_lfs_lock_dir(name,second_stale)
237 local check1 = kpse_in_name_ok_silent_extended(name) and kpse_out_name_ok_silent_extended(name)
238 if check1 then
239 return lfs_lock_dir(name,second_stale)
240 else
241 return nil, LUATEX_EPERM_MSG
242 end
243 end
244
245 local function luatex_lfs_dir(name)
246 local check1 = kpse_in_name_ok_silent_extended(name)
247 if check1 then
248 return lfs_dir(name)
249 else
250 error(LUATEX_EPERM_MSG)
251 end
252 end
253
254 local function luatex_lfs_link(oldf,newf,symlink)
255 local check1 = kpse_in_name_ok_silent_extended(newf) and kpse_out_name_ok_silent_extended(newf)
256 if check1 then
257 check1 = kpse_in_name_ok_silent_extended(oldf) and kpse_out_name_ok_silent_extended(oldf)
258 if check1 then
259 return lfs_link(oldf,newf,symlink)
260 else
261 return nil, LUATEX_EPERM_MSG,LUATEX_EPERM
262 end
263 else
264 return nil, LUATEX_EPERM_MSG,LUATEX_EPERM
265 end
266 end
267
268 local function luatex_lfs_mkdir(name)
269 local check1 = kpse_in_name_ok_silent_extended(name) and kpse_out_name_ok_silent_extended(name)
270 if check1 then
271 return lfs_mkdir(name)
272 else
273 return nil, LUATEX_EPERM_MSG,LUATEX_EPERM
274 end
275 end
276
277 local function luatex_lfs_mkdirp(name)
278 local check1 = kpse_in_name_ok_silent_extended(name) and kpse_out_name_ok_silent_extended(name)
279 if check1 then
280 local full=""
281 local r1,r2,r3
282 for sub in gmatch(name,"(/*[^\\/]+)") do
283 full=full..sub
284 r1,r2,r3 = lfs_mkdir(full)
285 end
286 return r1,r2,r3
287 else
288 return nil, LUATEX_EPERM_MSG,LUATEX_EPERM
289 end
290 end
291
292 local function luatex_lfs_rmdir(name)
293 local check1 = kpse_in_name_ok_silent_extended(name) and kpse_out_name_ok_silent_extended(name)
294 if check1 then
295 return lfs_rmdir(name)
296 else
297 return nil, LUATEX_EPERM_MSG,LUATEX_EPERM
298 end
299 end
300
301 local function luatex_lfs_symlinkattributes(filepath,aname)
302 local check1 = kpse_in_name_ok_silent_extended(filepath)
303 if check1 then
304 return lfs_symlinkattributes(filepath,aname)
305 else
306 return nil, LUATEX_EPERM_MSG,LUATEX_EPERM
307 end
308 end
309
310 local function luatex_lfs_touch(name,atime,mtime)
311 local check1 = kpse_in_name_ok_silent_extended(name) and kpse_out_name_ok_silent_extended(name)
312 if check1 then
313 return lfs_touch(name,atime,mtime)
314 else
315 return nil, LUATEX_EPERM_MSG
316 end
317 end
318
319
320
321
322 io.lines = luatex_io_lines
323 mt.lines = luatex_io_readline
324
325 io.open = luatex_io_open
326 --io.popen = luatex_io_popen -- not need, we use os.kpsepopen
327 io.popen = os.kpsepopen
328
329 os.rename = luatex_os_rename
330 os.remove = luatex_os_remove
331
332 lfs.attributes = luatex_lfs_attributes
333 lfs.chdir = luatex_lfs_chdir
334 lfs.lock_dir = luatex_lfs_lock_dir
335 lfs.dir = luatex_lfs_dir
336 lfs.link = luatex_lfs_link
337 lfs.mkdir = luatex_lfs_mkdir
338 lfs.mkdirp = luatex_lfs_mkdirp
339 lfs.rmdir = luatex_lfs_rmdir
340 lfs.symlinkattributes = luatex_lfs_symlinkattributes
341 lfs.touch = luatex_lfs_touch
342
343
344 else
345
346 -- we assume management elsewhere
347
348 end
349
350 if luadebugoption == 0 then
351
352 for k,_ in pairs(package.loaded.debug) do
353 if not(k=='traceback') then package.loaded.debug[k] = nil; end
354 end
355
356 --[==[ Not really necessary ]==]
357 for k,v in pairs(debug) do
358 if not(k=='traceback') then debug[k] = nil; end
359 end
360
361 end
362
363
364 -- maybe also only when in kpse mode
365
366 if saferoption == 1 then
367
368 local write_nl = texio.write_nl
369 local format = string.format
370
371 local function installdummy(str,f)
372 local reported = false
373 return function(...)
374 if not reported then
375 write_nl(format("safer option set, function %q is %s",
376 str,f and "limited" or "disabled"))
377 reported = true
378 end
379 if f then
380 return f(...)
381 end
382 end
383 end
384
385 local function installlimit(str,f)
386 local reported = false
387 end
388
389 os.execute = installdummy("os.execute")
390 os.spawn = installdummy("os.spawn")
391 os.exec = installdummy("os.exec")
392 os.setenv = installdummy("os.setenv")
393 os.tempdir = installdummy("os.tempdir")
394
395 os.kpsepopen = installdummy("os.kpsepopen")
396
397 io.popen = installdummy("io.popen")
398 io.open = installdummy("io.open",luatex_io_open_readonly)
399
400 os.rename = installdummy("os.rename")
401 os.remove = installdummy("os.remove")
402
403 io.tmpfile = installdummy("io.tmpfile")
404 io.output = installdummy("io.output")
405
406 lfs.chdir = installdummy("lfs.chdir")
407 lfs.lock = installdummy("lfs.lock")
408 lfs.touch = installdummy("lfs.touch")
409 lfs.rmdir = installdummy("lfs.rmdir")
410 lfs.mkdir = installdummy("lfs.mkdir")
411 lfs.mkdirp = installdummy("lfs.mkdirp")
412
413
414 package.loaded.debug = nil
415 debug = nil
416
417 -- os.[execute|os.spawn|os.exec] already are shellescape aware)
418
419 end
420
421 -- maybe also only when in kpse mode
422
423 if saferoption == 1 or shellescape ~= 1 then
424
425 package.loadlib = function() end
426 package.searchers[4] = nil
427 package.searchers[3] = nil
428
429 if os.setenv then
430 os.setenv = function(...) end
431 end
432 ffi = require('ffi')
433
434 if ffi then
435 for k, v in next, ffi do
436 if k ~= 'gc' then
437 ffi[k] = nil
438 end
439 end
440 end
441
442 ffi = nil
443 package.loaded.ffi = nil
444 package.preload.ffi = error
445
446 end
447
448 if md5 then
449
450 local sum = md5.sum
451 local gsub = string.gsub
452 local format = string.format
453 local byte = string.byte
454
455 if not md5.sumhexa then
456 function md5.sumhexa(k)
457 return (gsub(sum(k), ".", function(c)
458 return format("%02x",byte(c))
459 end))
460 end
461 end
462
463 if not md5.sumHEXA then
464 function md5.sumHEXA(k)
465 return (gsub(sum(k), ".", function(c)
466 return format("%02X",byte(c))
467 end))
468 end
469 end
470
471 end
472
473 -- compatibility: this might go away
474
475 if not unpack then
476 unpack = table.unpack
477 end
478
479 if not package.loaders then
480 package.loaders = package.searchers
481 end
482
483 if not loadstring then
484 loadstring = load
485 end
486
487 -- compatibility: this might stay
488
489 if bit32 then
490
491 -- lua 5.2: we're okay
492
493 elseif utf8 then
494
495 -- lua 5.3: bitwise.lua, v 1.24 2014/12/26 17:20:53 roberto
496
497 bit32 = load ( [[
498 local select = select -- instead of: arg = { ... }
499
500 bit32 = {
501 bnot = function (a)
502 return ~a & 0xFFFFFFFF
503 end,
504 band = function (x, y, z, ...)
505 if not z then
506 return ((x or -1) & (y or -1)) & 0xFFFFFFFF
507 else
508 local res = x & y & z
509 for i=1,select("#",...) do
510 res = res & select(i,...)
511 end
512 return res & 0xFFFFFFFF
513 end
514 end,
515 bor = function (x, y, z, ...)
516 if not z then
517 return ((x or 0) | (y or 0)) & 0xFFFFFFFF
518 else
519 local res = x | y | z
520 for i=1,select("#",...) do
521 res = res | select(i,...)
522 end
523 return res & 0xFFFFFFFF
524 end
525 end,
526 bxor = function (x, y, z, ...)
527 if not z then
528 return ((x or 0) ~ (y or 0)) & 0xFFFFFFFF
529 else
530 local res = x ~ y ~ z
531 for i=1,select("#",...) do
532 res = res ~ select(i,...)
533 end
534 return res & 0xFFFFFFFF
535 end
536 end,
537 btest = function (x, y, z, ...)
538 if not z then
539 return (((x or -1) & (y or -1)) & 0xFFFFFFFF) ~= 0
540 else
541 local res = x & y & z
542 for i=1,select("#",...) do
543 res = res & select(i,...)
544 end
545 return (res & 0xFFFFFFFF) ~= 0
546 end
547 end,
548 lshift = function (a, b)
549 return ((a & 0xFFFFFFFF) << b) & 0xFFFFFFFF
550 end,
551 rshift = function (a, b)
552 return ((a & 0xFFFFFFFF) >> b) & 0xFFFFFFFF
553 end,
554 arshift = function (a, b)
555 a = a & 0xFFFFFFFF
556 if b <= 0 or (a & 0x80000000) == 0 then
557 return (a >> b) & 0xFFFFFFFF
558 else
559 return ((a >> b) | ~(0xFFFFFFFF >> b)) & 0xFFFFFFFF
560 end
561 end,
562 lrotate = function (a ,b)
563 b = b & 31
564 a = a & 0xFFFFFFFF
565 a = (a << b) | (a >> (32 - b))
566 return a & 0xFFFFFFFF
567 end,
568 rrotate = function (a, b)
569 b = -b & 31
570 a = a & 0xFFFFFFFF
571 a = (a << b) | (a >> (32 - b))
572 return a & 0xFFFFFFFF
573 end,
574 extract = function (a, f, w)
575 return (a >> f) & ~(-1 << (w or 1))
576 end,
577 replace = function (a, v, f, w)
578 local mask = ~(-1 << (w or 1))
579 return ((a & ~(mask << f)) | ((v & mask) << f)) & 0xFFFFFFFF
580 end,
581 }
582 ]] )
583
584 elseif bit then
585
586 -- luajit (for now)
587
588 bit32 = load ( [[
589 local band, bnot, rshift, lshift = bit.band, bit.bnot, bit.rshift, bit.lshift
590
591 bit32 = {
592 arshift = bit.arshift,
593 band = band,
594 bnot = bnot,
595 bor = bit.bor,
596 bxor = bit.bxor,
597 btest = function(...)
598 return band(...) ~= 0
599 end,
600 extract = function(a,f,w)
601 return band(rshift(a,f),2^(w or 1)-1)
602 end,
603 lrotate = bit.rol,
604 lshift = lshift,
605 replace = function(a,v,f,w)
606 local mask = 2^(w or 1)-1
607 return band(a,bnot(lshift(mask,f)))+lshift(band(v,mask),f)
608 end,
609 rrotate = bit.ror,
610 rshift = rshift,
611 }
612 ]] )
613
614 else
615
616 -- hope for the best or fail
617
618 bit32 = require("bit32")
619
620 end
621
622 -- this is needed for getting require("socket") right
623
624 do
625
626 local loaded = package.loaded
627
628 if not loaded.socket then loaded.socket = loaded["socket.core"] end
629 if not loaded.mime then loaded.mime = loaded["mime.core"] end
630
631 if not loaded.lfs then loaded.lfs = lfs end
632
633 end
634
635 do
636
637 local lfsattributes = lfs.attributes
638 local symlinkattributes = lfs.symlinkattributes
639
640 -- these can now be done using lfs (was dead slow before)
641
642 if not lfs.isfile then
643 function lfs.isfile(name)
644 local m = lfsattributes(name,"mode")
645 return m == "file" or m == "link"
646 end
647 end
648
649 if not lfs.isdir then
650 function lfs.isdir(name)
651 local m = lfsattributes(name,"mode")
652 return m == "directory"
653 end
654 end
655
656 -- shortnames have also be sort of dropped from kpse
657
658 if not lfs.shortname then
659 function lfs.shortname(name)
660 return name
661 end
662 end
663
664 -- now there is a target field, so ...
665
666 if not lfs.readlink then
667 function lfs.readlink(name)
668 return symlinkattributes(name,"target") or nil
669 end
670 end
671
672 end
673
674 -- start omit
675
676 if utilities and utilities.merger and utilities.merger.compact then
677
678 local byte, format, gmatch, gsub = string.byte, string.format, string.gmatch, string.gsub
679 local concat = table.concat
680
681 local data = io.loaddata('luatex-core.lua')
682
683 data = gsub(data,'%-%-%s*start%s*omit.-%-%-%s*stop%s*omit%s*','')
684 data = gsub(data,'\r\n','\n')
685
686 local t = { }
687 local r = { }
688 local n = 0
689 local s = utilities.merger.compact(data) -- no comments and less spaces
690
691 t[#t+1] = '/* generated from and by luatex-core.lua */'
692 t[#t+1] = ''
693 t[#t+1] = '#include "lua.h"'
694 t[#t+1] = '#include "lauxlib.h"'
695 t[#t+1] = ''
696 t[#t+1] = 'int load_luatex_core_lua (lua_State * L);'
697 t[#t+1] = ''
698 t[#t+1] = 'int load_luatex_core_lua (lua_State * L)'
699 t[#t+1] = '{'
700 t[#t+1] = ' static unsigned char luatex_core_lua[] = {'
701 for c in gmatch(data,'.') do
702 if n == 16 then
703 n = 1
704 t[#t+1] = ' ' .. concat(r,', ') .. ','
705 else
706 n = n + 1
707 end
708 r[n] = format('0x%02x',byte(c))
709 end
710 n = n + 1
711 r[n] = '0x00'
712 t[#t+1] = ' ' .. concat(r,', ',1,n)
713 t[#t+1] = ' };'
714 -- t[#t+1] = format('unsigned int luatex_core_lua_len = 0x%x;',#d+1)
715 t[#t+1] = ' return luaL_dostring(L, (const char*) luatex_core_lua);'
716 t[#t+1] = '}'
717
718 io.savedata('luatex-core.c',concat(t,'\n'))
719 io.savedata('luatex-core-stripped.lua',s)
720
721 end
722
723 -- stop omit