Debian Codegraph @

texk/web2c/luatexdir/lua/liolibext.c

raw
1 /* liolibext.c
2
3 Copyright 2014 Taco Hoekwater <[email protected]>
4
5 This file is part of LuaTeX.
6
7 LuaTeX is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
11
12 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
19
20 */
21
22 #include "ptexlib.h"
23 #include "lua/luatex-api.h"
24
25 #ifdef LuajitTeX
26 #include "lua/lauxlib_bridge.h"
27 #else
28 #include "lauxlib.h"
29 #endif
30 #include "lualib.h"
31
32 #ifdef LuajitTeX
33 /* luajit has its own way for io, which is a mix of */
34 /* lua 5.1 and lua 5.2 . We use the stock luajit. */
35 #else
36 /*
37 ** {======================================================
38 ** lua_popen spawns a new process connected to the current
39 ** one through the file streams.
40 ** =======================================================
41 */
42
43 #if defined(_WIN32)
44
45 #ifdef _MSC_VER
46 #define lua_popen(L,c,m) ((void)L, win32_popen(c,m))
47 #define lua_pclose(L,file) ((void)L, win32_pclose(file))
48 #else
49 #define lua_popen(L,c,m) ((void)L, _popen(c,m))
50 #define lua_pclose(L,file) ((void)L, _pclose(file))
51 #endif
52
53 #else
54
55 #define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m))
56 #define lua_pclose(L,file) ((void)L, pclose(file))
57
58 #endif
59
60 /* }====================================================== */
61
62 #if defined(LUA_USE_POSIX)
63
64 #define l_fseek(f,o,w) fseeko(f,o,w)
65 #define l_ftell(f) ftello(f)
66 #define l_seeknum off_t
67
68 #elif defined(LUA_WIN) && !defined(_CRTIMP_TYPEINFO) \
69 && defined(_MSC_VER) && (_MSC_VER >= 1400)
70 /* Windows (but not DDK) and Visual C++ 2005 or higher */
71
72 #define l_fseek(f,o,w) _fseeki64(f,o,w)
73 #define l_ftell(f) _ftelli64(f)
74 #define l_seeknum __int64
75
76 #elif defined(__MINGW32__)
77
78 #define l_fseek(f,o,w) fseeko64(f,o,w)
79 #define l_ftell(f) ftello64(f)
80 #define l_seeknum int64_t
81
82 #else
83
84 #define l_fseek(f,o,w) fseek(f,o,w)
85 #define l_ftell(f) ftell(f)
86 #define l_seeknum long
87
88 #endif
89
90 #endif/* #ifdef LuajitTeX */
91
92 static FILE *tofile (lua_State *L) {
93 #ifdef LuajitTeX
94 FILE **f = luaL_checkudata(L,1,LUA_FILEHANDLE);
95 if (*f == NULL)
96 luaL_error(L,"attempt to use a closed file");
97 return *f;
98 #else
99 luaL_Stream *p = ((luaL_Stream *)luaL_checkudata(L, 1, LUA_FILEHANDLE));
100 if ((p)->closef == NULL)
101 luaL_error(L, "attempt to use a closed file");
102 lua_assert(p->f);
103 return p->f;
104 #endif
105 }
106
107 #define uchar(c) ((unsigned char)(c))
108
109 /*
110 HH: A few helpers to avoid reading numbers as strings. For now we put them in their
111 own namespace. We also have a few helpers that can make io functions tex friendly.
112 */
113
114 static int readcardinal1(lua_State *L) {
115 FILE *f = tofile(L);
116 int a = getc(f);
117 if (a == EOF)
118 lua_pushnil(L);
119 else
120 lua_pushinteger(L, a);
121 return 1;
122 }
123
124 static int readcardinal1_s(lua_State *L) {
125 size_t l;
126 const char *s = luaL_checklstring(L, 1, &l);
127 size_t p = luaL_checkinteger(L, 2) - 1;
128 if (p >= l) {
129 lua_pushnil(L);
130 } else {
131 int a = uchar(s[p]);
132 lua_pushinteger(L, a);
133 }
134 return 1;
135 }
136
137 static int readcardinal2(lua_State *L) {
138 FILE *f = tofile(L);
139 int a = getc(f);
140 int b = getc(f);
141 if (b == EOF)
142 lua_pushnil(L);
143 else
144 /* (a<<8) | b */
145 lua_pushinteger(L, 0x100 * a + b);
146 return 1;
147 }
148 static int readcardinal2_le(lua_State *L) {
149 FILE *f = tofile(L);
150 int b = getc(f);
151 int a = getc(f);
152 if (a == EOF)
153 lua_pushnil(L);
154 else
155 /* (a<<8) | b */
156 lua_pushinteger(L, 0x100 * a + b);
157 return 1;
158 }
159
160 static int readcardinal2_s(lua_State *L) {
161 size_t l;
162 const char *s = luaL_checklstring(L, 1, &l);
163 size_t p = luaL_checkinteger(L, 2) - 1;
164 if (p+1 >= l) {
165 lua_pushnil(L);
166 } else {
167 int a = uchar(s[p++]);
168 int b = uchar(s[p]);
169 lua_pushinteger(L, 0x100 * a + b);
170 }
171 return 1;
172 }
173 static int readcardinal2_s_le(lua_State *L) {
174 size_t l;
175 const char *s = luaL_checklstring(L, 1, &l);
176 size_t p = luaL_checkinteger(L, 2) - 1;
177 if (p+1 >= l) {
178 lua_pushnil(L);
179 } else {
180 int b = uchar(s[p++]);
181 int a = uchar(s[p]);
182 lua_pushinteger(L, 0x100 * a + b);
183 }
184 return 1;
185 }
186
187 static int readcardinal3(lua_State *L) {
188 FILE *f = tofile(L);
189 int a = getc(f);
190 int b = getc(f);
191 int c = getc(f);
192 if (c == EOF)
193 lua_pushnil(L);
194 else
195 /* (a<<16) | (b<<8) | c */
196 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c);
197 return 1;
198 }
199 static int readcardinal3_le(lua_State *L) {
200 FILE *f = tofile(L);
201 int c = getc(f);
202 int b = getc(f);
203 int a = getc(f);
204 if (a == EOF)
205 lua_pushnil(L);
206 else
207 /* (a<<16) | (b<<8) | c */
208 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c);
209 return 1;
210 }
211
212 static int readcardinal3_s(lua_State *L) {
213 size_t l;
214 const char *s = luaL_checklstring(L, 1, &l);
215 size_t p = luaL_checkinteger(L, 2) - 1;
216 if (p+2 >= l) {
217 lua_pushnil(L);
218 } else {
219 int a = uchar(s[p++]);
220 int b = uchar(s[p++]);
221 int c = uchar(s[p]);
222 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c);
223 }
224 return 1;
225 }
226 static int readcardinal3_s_le(lua_State *L) {
227 size_t l;
228 const char *s = luaL_checklstring(L, 1, &l);
229 size_t p = luaL_checkinteger(L, 2) - 1;
230 if (p+2 >= l) {
231 lua_pushnil(L);
232 } else {
233 int c = uchar(s[p++]);
234 int b = uchar(s[p++]);
235 int a = uchar(s[p]);
236 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c);
237 }
238 return 1;
239 }
240
241 static int readcardinal4(lua_State *L) {
242 FILE *f = tofile(L);
243 int a = getc(f);
244 int b = getc(f);
245 int c = getc(f);
246 int d = getc(f);
247 if (d == EOF)
248 lua_pushnil(L);
249 else
250 /* (a<<24) | (b<<16) | (c<<8) | d */
251 lua_pushinteger(L,0x1000000 * a + 0x10000 * b + 0x100 * c + d);
252 return 1;
253 }
254 static int readcardinal4_le(lua_State *L) {
255 FILE *f = tofile(L);
256 int d = getc(f);
257 int c = getc(f);
258 int b = getc(f);
259 int a = getc(f);
260 if (a == EOF)
261 lua_pushnil(L);
262 else
263 /* (a<<24) | (b<<16) | (c<<8) | d */
264 lua_pushinteger(L,0x1000000 * a + 0x10000 * b + 0x100 * c + d);
265 return 1;
266 }
267
268 static int readcardinal4_s(lua_State *L) {
269 size_t l;
270 const char *s = luaL_checklstring(L, 1, &l);
271 size_t p = luaL_checkinteger(L, 2) - 1;
272 if (p+3 >= l) {
273 lua_pushnil(L);
274 } else {
275 int a = uchar(s[p++]);
276 int b = uchar(s[p++]);
277 int c = uchar(s[p++]);
278 int d = uchar(s[p]);
279 lua_pushinteger(L,0x1000000 * a + 0x10000 * b + 0x100 * c + d);
280 }
281 return 1;
282 }
283 static int readcardinal4_s_le(lua_State *L) {
284 size_t l;
285 const char *s = luaL_checklstring(L, 1, &l);
286 size_t p = luaL_checkinteger(L, 2) - 1;
287 if (p+3 >= l) {
288 lua_pushnil(L);
289 } else {
290 int d = uchar(s[p++]);
291 int c = uchar(s[p++]);
292 int b = uchar(s[p++]);
293 int a = uchar(s[p]);
294 lua_pushinteger(L,0x1000000 * a + 0x10000 * b + 0x100 * c + d);
295 }
296 return 1;
297 }
298
299 static int readcardinaltable(lua_State *L) {
300 FILE *f = tofile(L);
301 int n = lua_tointeger(L,2);
302 int b = lua_tointeger(L,3);
303 int i;
304 lua_createtable(L,n,0);
305 switch (b) {
306 case 1:
307 for (i=1;i<=n;i++) {
308 int a = getc(f);
309 if (a == EOF) {
310 break;
311 } else {
312 lua_pushinteger(L, a);
313 lua_rawseti(L, -2, i);
314 }
315 }
316 break;
317 case 2:
318 for (i=1;i<=n;i++) {
319 int a = getc(f);
320 int b = getc(f);
321 if (b == EOF) {
322 break;
323 } else {
324 /* (a<<8) | b */
325 lua_pushinteger(L, 0x100 * a + b);
326 lua_rawseti(L, -2, i);
327 }
328 }
329 break;
330 case 3:
331 for (i=1;i<=n;i++) {
332 int a = getc(f);
333 int b = getc(f);
334 int c = getc(f);
335 if (c == EOF) {
336 break;
337 } else {
338 /* (a<<16) | (b<<8) | c */
339 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c);
340 lua_rawseti(L, -2, i);
341 }
342 }
343 break;
344 case 4:
345 for (i=1;i<=n;i++) {
346 int a = getc(f);
347 int b = getc(f);
348 int c = getc(f);
349 int d = getc(f);
350 if (d == EOF) {
351 break;
352 } else {
353 /* (a<<24) | (b<<16) | (c<<8) | d */
354 lua_pushinteger(L,0x1000000 * a + 0x10000 * b + 0x100 * c + d);
355 lua_rawseti(L, -2, i);
356 }
357 }
358 break;
359 default:
360 break;
361 }
362 return 1;
363 }
364
365 static int readcardinaltable_s(lua_State *L) {
366 size_t l;
367 const char *s = luaL_checklstring(L, 1, &l);
368 size_t p = luaL_checkinteger(L, 2) - 1;
369 int n = lua_tointeger(L,3);
370 int b = lua_tointeger(L,4);
371 int i;
372 lua_createtable(L,n,0);
373 /*if (p >= 0) {*/
374 switch (b) {
375 case 1:
376 for (i=1;i<=n;i++) {
377 if (p >= l) {
378 break;
379 } else {
380 int a = uchar(s[p++]);
381 lua_pushinteger(L, a);
382 lua_rawseti(L, -2, i);
383 }
384 }
385 break;
386 case 2:
387 for (i=1;i<=n;i++) {
388 if (p+1 >= l) {
389 break;
390 } else {
391 int a = uchar(s[p++]);
392 int b = uchar(s[p++]);
393 lua_pushinteger(L, 0x100 * a + b);
394 lua_rawseti(L, -2, i);
395 }
396 }
397 break;
398 case 3:
399 for (i=1;i<=n;i++) {
400 if (p+2 >= l) {
401 break;
402 } else {
403 int a = uchar(s[p++]);
404 int b = uchar(s[p++]);
405 int c = uchar(s[p++]);
406 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c);
407 lua_rawseti(L, -2, i);
408 }
409 }
410 break;
411 case 4:
412 for (i=1;i<=n;i++) {
413 if (p+3 >= l) {
414 break;
415 } else {
416 int a = uchar(s[p++]);
417 int b = uchar(s[p++]);
418 int c = uchar(s[p++]);
419 int d = uchar(s[p++]);
420 lua_pushinteger(L,0x1000000 * a + 0x10000 * b + 0x100 * c + d);
421 lua_rawseti(L, -2, i);
422 }
423 }
424 break;
425 default:
426 break;
427 }
428 /*}*/
429 return 1;
430 }
431
432 static int readinteger1(lua_State *L) {
433 FILE *f = tofile(L);
434 int a = getc(f);
435 if (a == EOF)
436 lua_pushnil(L);
437 else if (a >= 0x80)
438 lua_pushinteger(L, a - 0x100);
439 else
440 lua_pushinteger(L, a);
441 return 1;
442 }
443
444 static int readinteger1_s(lua_State *L) {
445 size_t l;
446 const char *s = luaL_checklstring(L, 1, &l);
447 size_t p = luaL_checkinteger(L, 2) - 1;
448 if (p >= l) {
449 lua_pushnil(L);
450 } else {
451 int a = uchar(s[p]);
452 if (a >= 0x80)
453 lua_pushinteger(L, a - 0x100);
454 else
455 lua_pushinteger(L, a);
456 }
457 return 1;
458 }
459
460 static int readinteger2(lua_State *L) {
461 FILE *f = tofile(L);
462 int a = getc(f);
463 int b = getc(f);
464 if (b == EOF)
465 lua_pushnil(L);
466 else if (a >= 0x80)
467 lua_pushinteger(L, 0x100 * a + b - 0x10000);
468 else
469 lua_pushinteger(L, 0x100 * a + b);
470 return 1;
471 }
472 static int readinteger2_le(lua_State *L) {
473 FILE *f = tofile(L);
474 int b = getc(f);
475 int a = getc(f);
476 if (a == EOF)
477 lua_pushnil(L);
478 else if (a >= 0x80)
479 lua_pushinteger(L, 0x100 * a + b - 0x10000);
480 else
481 lua_pushinteger(L, 0x100 * a + b);
482 return 1;
483 }
484
485 static int readinteger2_s(lua_State *L) {
486 size_t l;
487 const char *s = luaL_checklstring(L, 1, &l);
488 size_t p = luaL_checkinteger(L, 2) - 1;
489 if (p+1 >= l) {
490 lua_pushnil(L);
491 } else {
492 int a = uchar(s[p++]);
493 int b = uchar(s[p]);
494 if (a >= 0x80)
495 lua_pushinteger(L, 0x100 * a + b - 0x10000);
496 else
497 lua_pushinteger(L, 0x100 * a + b);
498 }
499 return 1;
500 }
501 static int readinteger2_s_le(lua_State *L) {
502 size_t l;
503 const char *s = luaL_checklstring(L, 1, &l);
504 size_t p = luaL_checkinteger(L, 2) - 1;
505 if (p+1 >= l) {
506 lua_pushnil(L);
507 } else {
508 int b = uchar(s[p++]);
509 int a = uchar(s[p]);
510 if (a >= 0x80)
511 lua_pushinteger(L, 0x100 * a + b - 0x10000);
512 else
513 lua_pushinteger(L, 0x100 * a + b);
514 }
515 return 1;
516 }
517
518 static int readinteger3(lua_State *L) {
519 FILE *f = tofile(L);
520 int a = getc(f);
521 int b = getc(f);
522 int c = getc(f);
523 if (c == EOF)
524 lua_pushnil(L);
525 else if (a >= 0x80)
526 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c - 0x1000000);
527 else
528 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c);
529 return 1;
530 }
531 static int readinteger3_le(lua_State *L) {
532 FILE *f = tofile(L);
533 int c = getc(f);
534 int b = getc(f);
535 int a = getc(f);
536 if (a == EOF)
537 lua_pushnil(L);
538 else if (a >= 0x80)
539 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c - 0x1000000);
540 else
541 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c);
542 return 1;
543 }
544
545 static int readinteger3_s(lua_State *L) {
546 size_t l;
547 const char *s = luaL_checklstring(L, 1, &l);
548 size_t p = luaL_checkinteger(L, 2) - 1;
549 if (p+2 >= l) {
550 lua_pushnil(L);
551 } else {
552 int a = uchar(s[p++]);
553 int b = uchar(s[p++]);
554 int c = uchar(s[p]);
555 if (a >= 0x80)
556 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c - 0x1000000);
557 else
558 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c);
559 }
560 return 1;
561 }
562 static int readinteger3_s_le(lua_State *L) {
563 size_t l;
564 const char *s = luaL_checklstring(L, 1, &l);
565 size_t p = luaL_checkinteger(L, 2) - 1;
566 if (p+2 >= l) {
567 lua_pushnil(L);
568 } else {
569 int c = uchar(s[p++]);
570 int b = uchar(s[p++]);
571 int a = uchar(s[p]);
572 if (a >= 0x80)
573 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c - 0x1000000);
574 else
575 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c);
576 }
577 return 1;
578 }
579
580 static int readinteger4(lua_State *L) {
581 FILE *f = tofile(L);
582 lua_Integer a = getc(f);
583 int b = getc(f);
584 int c = getc(f);
585 int d = getc(f);
586 if (d == EOF)
587 lua_pushnil(L);
588 else if (a >= 0x80)
589 lua_pushinteger(L, 0x1000000 * a + 0x10000 * b + 0x100 * c + d - 0x100000000);
590 else
591 lua_pushinteger(L, 0x1000000 * a + 0x10000 * b + 0x100 * c + d);
592 return 1;
593 }
594 static int readinteger4_le(lua_State *L) {
595 FILE *f = tofile(L);
596 int d = getc(f);
597 int c = getc(f);
598 int b = getc(f);
599 lua_Integer a = getc(f);
600 if (a == EOF)
601 lua_pushnil(L);
602 else if (a >= 0x80)
603 lua_pushinteger(L, 0x1000000 * a + 0x10000 * b + 0x100 * c + d - 0x100000000);
604 else
605 lua_pushinteger(L, 0x1000000 * a + 0x10000 * b + 0x100 * c + d);
606 return 1;
607 }
608
609 static int readinteger4_s(lua_State *L) {
610 size_t l;
611 const char *s = luaL_checklstring(L, 1, &l);
612 size_t p = luaL_checkinteger(L, 2) - 1;
613 if (p+3 >= l) {
614 lua_pushnil(L);
615 } else {
616 lua_Integer a = uchar(s[p++]);
617 int b = uchar(s[p++]);
618 int c = uchar(s[p++]);
619 int d = uchar(s[p]);
620 if (a >= 0x80)
621 lua_pushinteger(L, 0x1000000 * a + 0x10000 * b + 0x100 * c + d - 0x100000000);
622 else
623 lua_pushinteger(L, 0x1000000 * a + 0x10000 * b + 0x100 * c + d);
624 }
625 return 1;
626 }
627 static int readinteger4_s_le(lua_State *L) {
628 size_t l;
629 const char *s = luaL_checklstring(L, 1, &l);
630 size_t p = luaL_checkinteger(L, 2) - 1;
631 if (p+3 >= l) {
632 lua_pushnil(L);
633 } else {
634 int d = uchar(s[p++]);
635 int c = uchar(s[p++]);
636 int b = uchar(s[p++]);
637 lua_Integer a = uchar(s[p]);
638 if (a >= 0x80)
639 lua_pushinteger(L, 0x1000000 * a + 0x10000 * b + 0x100 * c + d - 0x100000000);
640 else
641 lua_pushinteger(L, 0x1000000 * a + 0x10000 * b + 0x100 * c + d);
642 }
643 return 1;
644 }
645
646 static int readintegertable(lua_State *L) {
647 FILE *f = tofile(L);
648 int n = lua_tointeger(L,2);
649 int b = lua_tointeger(L,3);
650 int i;
651 lua_createtable(L,n,0);
652 switch (b) {
653 case 1:
654 for (i=1;i<=n;i++) {
655 int a = getc(f);
656 if (a == EOF) {
657 break;
658 } else if (a >= 0x80) {
659 lua_pushinteger(L, a - 0x100);
660 } else {
661 lua_pushinteger(L, a);
662 }
663 lua_rawseti(L, -2, i);
664 }
665 break;
666 case 2:
667 for (i=1;i<=n;i++) {
668 int a = getc(f);
669 int b = getc(f);
670 if (b == EOF) {
671 break;
672 } else if (a >= 0x80) {
673 lua_pushinteger(L, 0x100 * a + b - 0x10000);
674 } else {
675 lua_pushinteger(L, 0x100 * a + b);
676 }
677 lua_rawseti(L, -2, i);
678 }
679 break;
680 case 3:
681 for (i=1;i<=n;i++) {
682 int a = getc(f);
683 int b = getc(f);
684 int c = getc(f);
685 if (c == EOF) {
686 break;
687 } else if (a >= 0x80) {
688 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c - 0x1000000);
689 } else {
690 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c);
691 }
692 lua_rawseti(L, -2, i);
693 }
694 break;
695 case 4:
696 for (i=1;i<=n;i++) {
697 lua_Integer a = getc(f);
698 int b = getc(f);
699 int c = getc(f);
700 int d = getc(f);
701 if (d == EOF) {
702 break;
703 } else if (a >= 0x80) {
704 lua_pushinteger(L, 0x1000000 * a + 0x10000 * b + 0x100 * c + d - 0x100000000);
705 } else {
706 lua_pushinteger(L, 0x1000000 * a + 0x10000 * b + 0x100 * c + d);
707 }
708 lua_rawseti(L, -2, i);
709 }
710 break;
711 default:
712 break;
713 }
714 return 1;
715 }
716
717 static int readintegertable_s(lua_State *L) {
718 size_t l;
719 const char *s = luaL_checklstring(L, 1, &l);
720 size_t p = luaL_checkinteger(L, 2) - 1;
721 int n = lua_tointeger(L,3);
722 int b = lua_tointeger(L,4);
723 int i;
724 lua_createtable(L,n,0);
725 /*if (p >= 0) {*/
726 switch (b) {
727 case 1:
728 for (i=1;i<=n;i++) {
729 if (p >= l) {
730 break;
731 } else {
732 int a = uchar(s[p++]);
733 if (a >= 0x80) {
734 lua_pushinteger(L, a - 0x100);
735 } else {
736 lua_pushinteger(L, a);
737 }
738 lua_rawseti(L, -2, i);
739 }
740 }
741 break;
742 case 2:
743 for (i=1;i<=n;i++) {
744 if (p+1 >= l) {
745 break;
746 } else {
747 int a = uchar(s[p++]);
748 int b = uchar(s[p++]);
749 if (a >= 0x80) {
750 lua_pushinteger(L, 0x100 * a + b - 0x10000);
751 } else {
752 lua_pushinteger(L, 0x100 * a + b);
753 }
754 lua_rawseti(L, -2, i);
755 }
756 }
757 break;
758 case 3:
759 for (i=1;i<=n;i++) {
760 if (p+2 >= l) {
761 break;
762 } else {
763 int a = uchar(s[p++]);
764 int b = uchar(s[p++]);
765 int c = uchar(s[p++]);
766 if (a >= 0x80) {
767 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c - 0x1000000);
768 } else {
769 lua_pushinteger(L, 0x10000 * a + 0x100 * b + c);
770 }
771 lua_rawseti(L, -2, i);
772 }
773 }
774 break;
775 case 4:
776 for (i=1;i<=n;i++) {
777 if (p+3 >= l) {
778 break;
779 } else {
780 lua_Integer a = uchar(s[p++]);
781 int b = uchar(s[p++]);
782 int c = uchar(s[p++]);
783 int d = uchar(s[p++]);
784 if (a >= 0x80) {
785 lua_pushinteger(L, 0x1000000 * a + 0x10000 * b + 0x100 * c + d - 0x100000000);
786 } else {
787 lua_pushinteger(L, 0x1000000 * a + 0x10000 * b + 0x100 * c + d);
788 }
789 lua_rawseti(L, -2, i);
790 }
791 }
792 break;
793 default:
794 break;
795 }
796 /*}*/
797 return 1;
798 }
799
800 /* from ff */
801
802 static int readfixed2(lua_State *L) {
803 FILE *f = tofile(L);
804 int a = getc(f);
805 int b = getc(f);
806 if (b == EOF) {
807 lua_pushnil(L);
808 } else {
809 int n = 0x100 * a + b;
810 lua_pushnumber(L,(double) ((n>>8) + ((n&0xff)/256.0)));
811 }
812 return 1;
813 }
814
815 static int readfixed2_s(lua_State *L) {
816 size_t l;
817 const char *s = luaL_checklstring(L, 1, &l);
818 size_t p = luaL_checkinteger(L, 2) - 1;
819 if (p+3 >= l) {
820 lua_pushnil(L);
821 } else {
822 int a = uchar(s[p++]);
823 int b = uchar(s[p]);
824 int n = 0x100 * a + b;
825 lua_pushnumber(L,(double) ((n>>8) + ((n&0xff)/256.0)));
826 }
827 return 1;
828 }
829
830 static int readfixed4(lua_State *L) {
831 FILE *f = tofile(L);
832 int a = getc(f);
833 int b = getc(f);
834 int c = getc(f);
835 int d = getc(f);
836 if (d == EOF) {
837 lua_pushnil(L);
838 } else {
839 int n = 0x1000000 * a + 0x10000 * b + 0x100 * c + d;
840 lua_pushnumber(L,(double) ((n>>16) + ((n&0xffff)/65536.0)));
841 }
842 return 1;
843 }
844
845 static int readfixed4_s(lua_State *L) {
846 size_t l;
847 const char *s = luaL_checklstring(L, 1, &l);
848 size_t p = luaL_checkinteger(L, 2) - 1;
849 if (p+3 >= l) {
850 lua_pushnil(L);
851 } else {
852 int a = uchar(s[p++]);
853 int b = uchar(s[p++]);
854 int c = uchar(s[p++]);
855 int d = uchar(s[p]);
856 int n = 0x1000000 * a + 0x10000 * b + 0x100 * c + d;
857 lua_pushnumber(L,(double) ((n>>16) + ((n&0xffff)/65536.0)));
858 }
859 return 1;
860 }
861
862 static int read2dot14(lua_State *L) {
863 FILE *f = tofile(L);
864 int a = getc(f);
865 int b = getc(f);
866 if (b == EOF) {
867 lua_pushnil(L);
868 } else {
869 int n = 0x100 * a + b;
870 /* from ff */
871 lua_pushnumber(L,(double) (((n<<16)>>(16+14)) + ((n&0x3fff)/16384.0)));
872 }
873 return 1;
874 }
875
876 static int read2dot14_s(lua_State *L) {
877 size_t l;
878 const char *s = luaL_checklstring(L, 1, &l);
879 size_t p = luaL_checkinteger(L, 2) - 1;
880 if (p+1 >= l) {
881 lua_pushnil(L);
882 } else {
883 int a = uchar(s[p++]);
884 int b = uchar(s[p]);
885 int n = 0x100 * a + b;
886 lua_pushnumber(L,(double) (((n<<16)>>(16+14)) + ((n&0x3fff)/16384.0)));
887 }
888 return 1;
889 }
890
891 static int getposition(lua_State *L) {
892 FILE *f = tofile(L);
893 long p = ftell(f);
894 if (p<0)
895 lua_pushnil(L);
896 else
897 lua_pushinteger(L, p);
898 return 1;
899 }
900
901 static int setposition(lua_State *L) {
902 FILE *f = tofile(L);
903 long p = lua_tointeger(L,2);
904 p = fseek(f,p,SEEK_SET);
905 if (p<0)
906 lua_pushnil(L);
907 else
908 lua_pushinteger(L, p);
909 return 1;
910 }
911
912 static int skipposition(lua_State *L) {
913 FILE *f = tofile(L);
914 long p = lua_tointeger(L,2);
915 p = fseek(f,ftell(f)+p,SEEK_SET);
916 if (p<0)
917 lua_pushnil(L);
918 else
919 lua_pushinteger(L, p);
920 return 1;
921 }
922
923 static int readbytetable(lua_State *L) {
924 FILE *f = tofile(L);
925 int n = lua_tointeger(L,2);
926 int i ;
927 lua_createtable(L, n, 0);
928 for (i=1;i<=n;i++) {
929 int a = getc(f);
930 if (a == EOF) {
931 break;
932 } else {
933 /*
934 lua_pushinteger(L, i);
935 lua_pushinteger(L, a);
936 lua_rawset(L, -3);
937 */
938 lua_pushinteger(L, a);
939 lua_rawseti(L,-2,i);
940 }
941 }
942 return 1;
943 }
944
945 static int readbytetable_s(lua_State *L) {
946 size_t l;
947 const char *s = luaL_checklstring(L, 1, &l);
948 size_t p = luaL_checkinteger(L, 2) - 1;
949 int n = lua_tointeger(L,3);
950 if (p >= l) {
951 lua_pushnil(L);
952 } else {
953 int i ;
954 if (p + n >= l) {
955 n = l - p ;
956 }
957 lua_createtable(L, n, 0);
958 for (i=1;i<=n;i++) {
959 int a = uchar(s[p++]);
960 lua_pushinteger(L, a);
961 lua_rawseti(L,-2,i);
962 }
963 }
964 return 1;
965 }
966
967 static int readbytes(lua_State *L) {
968 FILE *f = tofile(L);
969 int n = lua_tointeger(L,2);
970 int i = 0;
971 for (i=1;i<=n;i++) {
972 int a = getc(f);
973 if (a == EOF) {
974 return i-1;
975 } else {
976 lua_pushinteger(L, a);
977 }
978 }
979 return n;
980 }
981
982 static int readbytes_s(lua_State *L) {
983 size_t l;
984 const char *s = luaL_checklstring(L, 1, &l);
985 size_t p = luaL_checkinteger(L, 2) - 1;
986 int n = lua_tointeger(L,3);
987 if (p >= l) {
988 return 0;
989 } else {
990 int i ;
991 if (p + n >= l) {
992 n = l - p ;
993 }
994 lua_createtable(L, n, 0);
995 for (i=1;i<=n;i++) {
996 int a = uchar(s[p++]);
997 lua_pushinteger(L, a);
998 }
999 return n;
1000 }
1001 }
1002
1003 static int readline(lua_State *L)
1004 {
1005 luaL_Buffer buf;
1006 int c, d;
1007 FILE *f = tofile(L);
1008 luaL_buffinit(L, &buf);
1009 while (1) {
1010 c = fgetc(f);
1011 if (c == EOF) {
1012 luaL_pushresult(&buf);
1013 if (lua_rawlen(L, -1) == 0) {
1014 lua_pop(L, 1);
1015 lua_pushnil(L);
1016 }
1017 return 1;
1018 } else if (c == '\n') {
1019 luaL_pushresult(&buf);
1020 return 1;
1021 } else if (c == '\r') {
1022 d = fgetc(f);
1023 if (d != EOF && d != '\n') {
1024 ungetc(d, f);
1025 }
1026 luaL_pushresult(&buf);
1027 return 1;
1028 } else {
1029 luaL_addchar(&buf, c);
1030 }
1031 }
1032 }
1033
1034 static const luaL_Reg fiolib[] = {
1035 /* helpers */
1036 { "readcardinal1", readcardinal1 },
1037 { "readcardinal2", readcardinal2 },
1038 { "readcardinal3", readcardinal3 },
1039 { "readcardinal4", readcardinal4 },
1040 { "readcardinal1le", readcardinal1 },
1041 { "readcardinal2le", readcardinal2_le },
1042 { "readcardinal3le", readcardinal3_le },
1043 { "readcardinal4le", readcardinal4_le },
1044 { "readcardinaltable", readcardinaltable },
1045 { "readinteger1", readinteger1 },
1046 { "readinteger2", readinteger2 },
1047 { "readinteger3", readinteger3 },
1048 { "readinteger4", readinteger4 },
1049 { "readinteger1le", readinteger1 },
1050 { "readinteger2le", readinteger2_le },
1051 { "readinteger3le", readinteger3_le },
1052 { "readinteger4le", readinteger4_le },
1053 { "readintegertable", readintegertable },
1054 { "readfixed2", readfixed2 },
1055 { "readfixed4", readfixed4 },
1056 { "read2dot14", read2dot14 },
1057 { "setposition", setposition },
1058 { "getposition", getposition },
1059 { "skipposition", skipposition },
1060 { "readbytes", readbytes },
1061 { "readbytetable", readbytetable },
1062 { "readline", readline },
1063 /* done */
1064 { NULL, NULL }
1065 };
1066
1067 static const luaL_Reg siolib[] = {
1068 { "readcardinal1", readcardinal1_s },
1069 { "readcardinal2", readcardinal2_s },
1070 { "readcardinal3", readcardinal3_s },
1071 { "readcardinal4", readcardinal4_s },
1072 { "readcardinal1le", readcardinal1_s },
1073 { "readcardinal2le", readcardinal2_s_le },
1074 { "readcardinal3le", readcardinal3_s_le },
1075 { "readcardinal4le", readcardinal4_s_le },
1076 { "readcardinaltable", readcardinaltable_s },
1077 { "readinteger1", readinteger1_s },
1078 { "readinteger2", readinteger2_s },
1079 { "readinteger3", readinteger3_s },
1080 { "readinteger4", readinteger4_s },
1081 { "readinteger1le", readinteger1_s },
1082 { "readinteger2le", readinteger2_s_le },
1083 { "readinteger3le", readinteger3_s_le },
1084 { "readinteger4le", readinteger4_s_le },
1085 { "readintegertable", readintegertable_s },
1086 { "readfixed2", readfixed2_s },
1087 { "readfixed4", readfixed4_s },
1088 { "read2dot14", read2dot14_s },
1089 { "readbytes", readbytes_s },
1090 { "readbytetable", readbytetable_s },
1091 /* done */
1092 { NULL, NULL }
1093 };
1094
1095 /*
1096 The sio helpers are experimental and might be handy at some point. Speed-wise
1097 there is no gain over file access because with ssd and caching we basically
1098 operate in memory too. We keep them as complement to the file ones. I did
1099 consider using an userdata object for the position etc but some simple tests
1100 demonstrated that there is no real gain and the current ones permits to wrap
1101 up whatever interface one likes.
1102 */
1103
1104 int luaopen_fio(lua_State *L) {
1105 luaL_openlib(L, "fio", fiolib, 0);
1106 luaL_openlib(L, "sio", siolib, 0);
1107 return 1;
1108 }