summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/lua/lpdfscannerlib.c
blob: 90e7da58bf07b02043f6b49dcc061fa43b5eef93 (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
/* lpdfscannerlib.c

   Copyright 2013 Taco Hoekwater <taco@luatex.org>

   This file is part of LuaTeX.

   LuaTeX is free software; you can redistribute it and/or modify it under
   the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 2 of the License, or (at your
   option) any later version.

   LuaTeX is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
   License for more details.

   You should have received a copy of the GNU General Public License along
   with LuaTeX; if not, see <http://www.gnu.org/licenses/>.

*/

/*tex

    The scanner can read from a string or stream. Streams can be given directly as
    |ppstream| object or as a |pparray| of streams. Here is an example of usage:

    \starttyping
    local operatortable = { }

    operatortable.Do = function(scanner,info)
        local resources = info.resources
        if resources then
            local val     = scanner:pop()
            local name    = val[2]
            local xobject = resources.XObject
            print(info.space .. "Uses XObject " .. name)
            local resources = xobject.Resources
            if resources then
                local newinfo =  {
                    space     = info.space .. "  ",
                    resources = resources,
                }
                pdfscanner.scan(entry, operatortable, newinfo)
            end
        end
    end

    local function Analyze(filename)
        local doc = pdfe.open(filename)
        if doc then
            local pages = doc.Pages
            for i=1,#pages do
                local page = pages[i]
                local info = {
                  space     = "  " ,
                  resources = page.Resources,
                }
                print("Page " .. i)
                pdfscanner.scan(page.Contents,operatortable,info)
                pdfscanner.scan(page.Contents(),operatortable,info)
            end
        end
    end

    Analyze("foo.pdf")
    \stoptyping

*/

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <math.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#include "pplib.h"

#include <lua/luatex-api.h>

#define SCANNER "pdfscanner"

#define MAXOPERANDS 1000

typedef enum {
    pdf_integer = 1,
    pdf_real,
    pdf_boolean,
    pdf_name,
    pdf_operator,
    pdf_string,
    pdf_startarray,
    pdf_stoparray,
    pdf_startdict,
    pdf_stopdict,
} pdf_token_type;

typedef struct Token {
    pdf_token_type type;
    double value;
    char *string;
} Token;

typedef struct ObjectList {
    struct ObjectList *next;
    ppstream *stream;
} ObjectList;

typedef struct scannerdata {
    int _ininlineimage;
    int _nextoperand;
    Token **_operandstack;
    ppstream *_stream;
    ObjectList *_streams;
    const char *buffer;
    size_t position;
    size_t size;
    int uses_stream;
} scannerdata;

#define PDFE_METATABLE_ARRAY  "luatex.pdfe.array"
#define PDFE_METATABLE_STREAM "luatex.pdfe.stream"

typedef struct {
    void *d;
    /*tex reference to |PdfDocument|, or |NULL| */
    void *pd;
    /*tex counter to detect |PDFDoc| change */
    unsigned long pc;
} udstruct;

static void clear_operand_stack(scannerdata * self, int from);
static Token *_parseToken(scannerdata * self, int c);
static void push_token(lua_State * L, scannerdata * self);

static void *priv_xmalloc(size_t size)
{
    void *new_mem = (void *) malloc(size);
    if (new_mem == NULL) {
        luaL_error(Luas, "no room for <pdfscanned> stream");
    }
    return new_mem;
}

static void *priv_xrealloc(void *old_ptr, size_t size)
{
    void *new_mem = (void *) realloc(old_ptr, size);
    if (new_mem == NULL) {
        luaL_error(Luas, "no room for <pdfscanned> stream");
    }
    return new_mem;
}

#define xreallocarray(ptr,type,size) ((type*)priv_xrealloc(ptr,(size+1)*sizeof(type)))

#define INITBUFSIZE 64

#define define_buffer(a) \
  char *a = (char *)priv_xmalloc (INITBUFSIZE); \
  int a##_size = INITBUFSIZE; \
  int a##index = 0; \
  memset (a,0,INITBUFSIZE)

#define check_overflow(a, wsize) do { \
    if (wsize >= a##_size) { \
        int nsize = a##_size + a##_size / 4; \
        a = (char *) xreallocarray(a, char, (unsigned) nsize); \
        memset (a+a##_size, 0, a##_size / 4); \
        a##_size = nsize; \
    } \
} while (0)


static scannerdata *scanner_push(lua_State * L)
{
    scannerdata *a = (scannerdata *) lua_newuserdata(L, sizeof(scannerdata));
    luaL_getmetatable(L, SCANNER);
    lua_setmetatable(L, -2);
    return a;
}

static scannerdata *scanner_check(lua_State * L, int index)
{
    scannerdata *bar;
    luaL_checktype(L, index, LUA_TUSERDATA);
    bar = (scannerdata *) luaL_checkudata(L, index, SCANNER);
    if (bar == NULL)
        luaL_argerror(L, index, SCANNER " expected");
    return bar;
}

static void free_token(Token * token)
{
    if (token->string) {
        free(token->string);
    }
    free(token);
}

static void clear_operand_stack(scannerdata * self, int from)
{
    int i = self->_nextoperand - 1;
    while (i >= from) {
        if (self->_operandstack[i]) {
            free_token(self->_operandstack[i]);
            self->_operandstack[i] = NULL;
        }
        i--;
    }
    self->_nextoperand = from;
}

static void push_operand(scannerdata * self, Token * token)
{
    if (self->_nextoperand + 1 > MAXOPERANDS) {
        fprintf(stderr, "out of operand stack space");
        exit(1);
    }
    self->_operandstack[self->_nextoperand++] = token;
}

static Token *new_operand(pdf_token_type c)
{
    Token *token = (Token *) priv_xmalloc(sizeof(Token));
    memset(token, 0, sizeof(Token));
    token->type = c;
    return token;
}

static void _nextStream(scannerdata * self)
{
    ObjectList *rover = NULL;
    if (self->uses_stream && self->buffer != NULL) {
        if (self->uses_stream) {
            ppstream_done(self->_stream);
        } else {
            free(self->_stream);
        }
    }
    rover = self->_streams;
    self->_stream = rover->stream;
    if (self->uses_stream) {
        self->buffer = (const char *) ppstream_all(self->_stream, &self->size, 1);
    }
    self->position = 0;
    self->_streams = rover->next;
    free(rover);
}

static int streamGetChar(scannerdata * self)
{
    int i = EOF;
    if (self->position < self->size) {
        const char c = self->buffer[self->position];
        ++self->position;
        i = (int) c;
    }
    if (i < 0 && self->_streams) {
        _nextStream(self);
        i = streamGetChar(self);
    }
    return i;
}

static int streamLookChar(scannerdata * self)
{
    int i = EOF;
    if (self->position < self->size) {
        const char c = self->buffer[self->position];
        /*not |++self->position;| */
        i = (int) c;
    }
    if (i < 0 && self->_streams) {
        _nextStream(self);
        i = streamGetChar(self);
    }
    return i;
}

static void streamReset(scannerdata * self)
{
    if (self->uses_stream) {
        self->buffer = (const char *) ppstream_all(self->_stream, &self->size, 1);
    }
    self->position = 0;
}

static void streamClose(scannerdata * self)
{
    if (self->uses_stream) {
        ppstream_done(self->_stream);
    } else {
        free(self->_stream);
    }
    self->buffer = NULL;
    self->_stream = NULL;
}

/*tex end of stream interface */

static Token *_parseSpace(scannerdata * self)
{
    return _parseToken(self, streamGetChar(self));
}

static Token *_parseString(scannerdata * self, int c)
{
    int level;
    Token *token = NULL;
    define_buffer(found);
    level = 1;
    while (1) {
        c = streamGetChar(self);
        if (c == '(') {
            level = level + 1;
        } else if (c == ')') {
            level = level - 1;
            if (level < 1)
            break;
        } else if (c == '\\') {
            int next = streamGetChar(self);
            if (next == '(' || next == ')' || next == '\\') {
                c = next;
            } else if (next == '\n' || next == '\r') {
                c = '\0';
            } else if (next == 'n') {
                c = '\n';
            } else if (next == 'r') {
                c = '\r';
            } else if (next == 't') {
                c = '\t';
            } else if (next == 'b') {
                c = '\b';
            } else if (next == 'f') {
                c = '\f';
            } else if (next >= '0' && next <= '7') {
                int next2;
                next = next - '0';
                next2 = streamLookChar(self);
                if (next2 >= '0' && next2 <= '7') {
                    int next3;
                    next2 = streamGetChar(self);
                    next2 = next2 - '0';
                    next3 = streamLookChar(self);
                    if (next3 >= '0' && next3 <= '7') {
                    next3 = streamGetChar(self);
                    next3 = next3 - '0';
                    c = (next * 64 + next2 * 8 + next3);
                    } else {
                    c = (next * 8 + next2);
                    }
                } else {
                    c = next;
                }
            } else {
                c = next;
            }
        }
        check_overflow(found, foundindex);
        if (c >= 0) {
            found[foundindex++] = c;
        }
    }
    token = new_operand(pdf_string);
    token->value = foundindex;
    token->string = found;
    return token;
}

static Token *_parseNumber(scannerdata * self, int c)
{
    double value = 0;
    pdf_token_type type = pdf_integer;
    int isfraction = 0;
    int isnegative = 0;
    int i = 0;
    Token *token = NULL;
    if (c == '-') {
        isnegative = 1;
        c = streamGetChar(self);
    }
    if (c == '.') {
        type = pdf_real;
        isfraction = 1;
    } else {
        value = c - '0';
    }
    c = streamLookChar(self);
    if ((c >= '0' && c <= '9') || c == '.') {
    c = streamGetChar(self);
    while (1) {
        if (c == '.') {
            type = pdf_real;
            isfraction = 1;
        } else {
            i = c - '0';
            if (isfraction > 0) {
                value = value + (i / (pow(10.0, isfraction)));
                isfraction = isfraction + 1;
            } else {
                value = (value * 10) + i;
            }
        }
        c = streamLookChar(self);
        if (!((c >= '0' && c <= '9') || c == '.'))
            break;
        c = streamGetChar(self);
    }
    }
    if (isnegative) {
        value = -value;
    }
    token = new_operand(type);
    token->value = value;
    return token;
}

static Token *_parseName(scannerdata * self, int c)
{
    Token *token = NULL;
    define_buffer(found);
    c = streamGetChar(self);
    while (1) {
        check_overflow(found, foundindex);
        found[foundindex++] = c;
        c = streamLookChar(self);
        if (c == ' ' || c == '\n' || c == '\r' || c == '\t' ||
            c == '/' || c == '[' || c == '(' || c == '<')
            break;
        c = streamGetChar(self);
    }
    token = new_operand(pdf_name);
    token->string = found;
    token->value = strlen(found);
    return token;
}

#define hexdigit(c)	\
  (c>= '0' && c<= '9') ? (c - '0') : ((c>= 'A' && c<= 'F') ? (c - 'A' + 10) : (c - 'a' + 10))

static Token *_parseHexstring(scannerdata * self, int c)
{
    int isodd = 1;
    int hexval = 0;
    Token *token = NULL;
    define_buffer(found);
    while (c != '>') {
    if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) {
        if (isodd == 1) {
            int v = hexdigit(c);
            hexval = 16 * v;
        } else {
            hexval += hexdigit(c);
            check_overflow(found, foundindex);
            found[foundindex++] = hexval;
        }
        isodd = (isodd == 1 ? 0 : 1);
    }
        c = streamGetChar(self);
    }
    token = new_operand(pdf_string);
    token->value = foundindex;
    token->string = found;
    return token;
}

#define pdf_isspace(a) (a == '\0' || a == ' ' || a == '\n' || a == '\r' || a == '\t' || a == '\v')

/*tex this is rather horrible */

static Token *_parseInlineImage(scannerdata * self, int c)
{
    Token *token = NULL;
    define_buffer(found);
    if (c == ' ') {
        /*tex first space can be ignored */
        c = streamGetChar(self);
    }
    check_overflow(found, foundindex);
    found[foundindex++] = c;
    while (1) {
        c = streamLookChar(self);
        if (c == 'E'
            && (found[foundindex - 1] == '\n'
            || found[foundindex - 1] == '\r')) {
            c = streamGetChar(self);
            check_overflow(found, foundindex);
            found[foundindex++] = c;
            c = streamLookChar(self);
            if (c == 'I') {
                c = streamGetChar(self);
                check_overflow(found, foundindex);
                found[foundindex++] = c;
                c = streamLookChar(self);
                if (pdf_isspace(c)) {
                    /*tex |I| */
                    found[--foundindex] = '\0';
                    /*tex |E| */
                    found[--foundindex] = '\0';
                    /*tex remove end-of-line before |EI| */
                    if (found[foundindex - 1] == '\n') {
                        found[--foundindex] = '\0';
                    }
                    if (found[foundindex - 1] == '\r') {
                        found[--foundindex] = '\0';
                    }
                    break;
                } else {
                    c = streamGetChar(self);
                    check_overflow(found, foundindex);
                    found[foundindex++] = c;
                }
            } else {
                c = streamGetChar(self);
                check_overflow(found, foundindex);
                found[foundindex++] = c;
            }
        } else {
            c = streamGetChar(self);
            check_overflow(found, foundindex);
            found[foundindex++] = c;
        }
    }
    token = new_operand(pdf_string);
    token->value = foundindex;
    token->string = found;
    return token;
}

static Token *_parseOperator(scannerdata * self, int c)
{
    define_buffer(found);
    while (1) {
        check_overflow(found, foundindex);
        found[foundindex++] = c;
        c = streamLookChar(self);
        if ((c < 0) || (c == ' ' || c == '\n' || c == '\r' || c == '\t' ||
                c == '/' || c == '[' || c == '(' || c == '<'))
            break;
        c = streamGetChar(self);
    }
    /*tex |print| (found) */
    if (strcmp(found, "ID") == 0) {
        self->_ininlineimage = 1;
    }
    if (strcmp(found, "false") == 0) {
        Token *token = new_operand(pdf_boolean);
        token->value = 0;
        free(found);
        return token;
    } else if (strcmp(found, "true") == 0) {
        Token *token = new_operand(pdf_boolean);
        token->value = 1.0;
        free(found);
        return token;
    } else {
        Token *token = new_operand(pdf_operator);
        token->string = found;
        return token;
    }
}

static Token *_parseComment(scannerdata * self, int c)
{
    do {
        c = streamGetChar(self);
    } while (c != '\n' && c != '\r' && c != -1);
    return _parseToken(self, streamGetChar(self));
}

static Token *_parseLt(scannerdata * self, int c)
{
    c = streamGetChar(self);
    if (c == '<') {
        return new_operand(pdf_startdict);
    } else {
        return _parseHexstring(self, c);
    }
}

static Token *_parseGt(scannerdata * self, int c)
{
    c = streamGetChar(self);
    if (c == '>') {
        return new_operand(pdf_stopdict);
    } else {
        fprintf(stderr, "stray > in stream");
        return NULL;
    }
}

static Token *_parseError(int c)
{
    fprintf(stderr, "stray %c [%d] in stream", c, c);
    return NULL;
}

static Token *_parseStartarray(void)
{
    return new_operand(pdf_startarray);
}

static Token *_parseStoparray(void)
{
    return new_operand(pdf_stoparray);
}

static Token *_parseToken(scannerdata * self, int c)
{
    if (self->_ininlineimage == 1) {
        self->_ininlineimage = 2;
        return _parseInlineImage(self, c);
    } else if (self->_ininlineimage == 2) {
        Token *token = NULL;
        self->_ininlineimage = 0;
        token = new_operand(pdf_operator);
        token->string = strdup("EI");
        return token;
    }
    if (c < 0)
        return NULL;
    switch (c) {
        case '(':
            return _parseString(self, c);
            break;
        case ')':
            return _parseError(c);
            break;
        case '[':
            return _parseStartarray();
            break;
        case ']':
            return _parseStoparray();
            break;
        case '/':
            return _parseName(self, c);
            break;
        case '<':
            return _parseLt(self, c);
            break;
        case '>':
            return _parseGt(self, c);
            break;
        case '%':
            return _parseComment(self, c);
            break;
        case ' ':
        case '\r':
        case '\n':
        case '\t':
            return _parseSpace(self);
            break;
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
        case '-':
        case '.':
            return _parseNumber(self, c);
            break;
        default:
            if (c <= 127) {
                return _parseOperator(self, c);
            } else {
                return _parseError(c);
            }
    }
}

static int scanner_scan(lua_State * L)
{
    Token *token;
    scannerdata *self;
    if (lua_gettop(L) != 3) {
        return 0;
    }
    luaL_checktype(L, 2, LUA_TTABLE);
    luaL_checktype(L, 3, LUA_TTABLE);
    self = scanner_push(L);
    memset(self, 0, sizeof(scannerdata));
    self->_operandstack = (Token **) priv_xmalloc(MAXOPERANDS * sizeof(Token));
    memset(self->_operandstack, 0, (MAXOPERANDS * sizeof(Token)));
    /*tex stack slot 4 = self */
    self->uses_stream = 1;
    if (lua_type(L, 1) == LUA_TSTRING) {
      /*tex
            We could make a temporary copy on the stack (or in the registry)
            which saves memory.
      */
      char *buf = NULL;
      const char *s = lua_tolstring(L, 1, &self->size);
      if (s==NULL){
	fprintf(stderr,"fatal: cannot convert the token to string.");
	exit(1);
      }
      buf = priv_xmalloc(self->size+1);
      buf[self->size]='\0';
      self->uses_stream = 0;
      memcpy(buf,s,self->size);
      self->buffer = buf;
    } else if (lua_type(L, 1) == LUA_TTABLE) {
        udstruct *uin;
        void *ud;
        int i = 1;
        while (1) {
            lua_rawgeti(L, 1, i);
            if (lua_type(L, -1) == LUA_TUSERDATA) {
                ud = luaL_checkudata(L, -1, PDFE_METATABLE_STREAM);
                if (ud != NULL) {
                    ObjectList *rover = NULL;
                    ObjectList *item = NULL;
                    uin = (udstruct *) ud;
                    rover = self->_streams;
                    item = (ObjectList *) priv_xmalloc(sizeof(ObjectList));
                    item->stream = ((ppstream *) uin->d);
                    item->next = NULL;
                    if (!rover) {
                        rover = item;
                        self->_streams = rover;
                    } else {
                        while (rover->next)
                            rover = rover->next;
                        rover->next = item;
                    }
                }
            } else {
                ObjectList *rover = self->_streams;
                self->_stream = rover->stream;
                self->_streams = rover->next;
                free(rover);
                lua_pop(L, 1);
                break;
            }
            lua_pop(L, 1);
            i++;
        }
    } else {
        udstruct *uin;
        void *ud;
        luaL_checktype(L, 1, LUA_TUSERDATA);
        ud = luaL_checkudata(L, 1, PDFE_METATABLE_STREAM);
        if (ud != NULL) {
            uin = (udstruct *) ud;
            self->_stream = ((ppstream *) uin->d);
        } else {
            ud = luaL_checkudata(L, 1, PDFE_METATABLE_ARRAY);
            if (ud != NULL) {
            ObjectList *rover = NULL;
            pparray *array = NULL;
            int count;
            int i;
            uin = (udstruct *) ud;
            array = (pparray *) uin->d;
            count = array->size;
            for (i = 0; i < count; i++) {
                ppobj *obj = pparray_at(array, i);
                if (obj->type == PPSTREAM) {
                    ObjectList *rover = self->_streams;
                    ObjectList *item =
                        (ObjectList *)
                        priv_xmalloc(sizeof(ObjectList));
                    item->stream = obj->stream;
                    item->next = NULL;
                    if (!rover) {
                        rover = item;
                        self->_streams = rover;
                    } else {
                        while (rover->next)
                        rover = rover->next;
                        rover->next = item;
                    }
                }
            }
            rover = self->_streams;
            self->_stream = rover->stream;
            self->_streams = rover->next;
            }
        }
    }
    streamReset(self);
    token = _parseToken(self, streamGetChar(self));
    while (token) {
        if (token->type == pdf_operator) {
            lua_pushstring(L, token->string);
            free_token(token);
            /*tex fetch operator table */
            lua_rawget(L, 2);
            if (lua_isfunction(L, -1)) {
                lua_pushvalue(L, 4);
                lua_pushvalue(L, 3);
                (void) lua_call(L, 2, 0);
            } else {
                /*tex nil */
                lua_pop(L, 1);
            }
            clear_operand_stack(self, 0);
        } else {
            push_operand(self, token);
        }
        if (self->uses_stream) {
            if (!self->_stream) {
                break;
            }
        } else {
            if (self->buffer == NULL) {
                break;
            }
        }
        token = _parseToken(self, streamGetChar(self));
    }
    /*tex wrap up */
    if (self->_stream) {
        streamClose(self);
    }
    clear_operand_stack(self, 0);
    free(self->_operandstack);
    return 0;
}

static int scanner_done(lua_State * L)
{
    int c;
    scannerdata *self = scanner_check(L, 1);
    while ((c = streamGetChar(self)) >= 0);
    return 0;
}

/*tex here are the stack popping functions, and their helpers */

static void operandstack_backup(scannerdata * self)
{
    int i = self->_nextoperand - 1;
    int balance = 0;
    int backupstart = 0;
    int backupstop = self->_operandstack[i]->type;
    if (backupstop == pdf_stopdict) {
        backupstart = pdf_startdict;
    } else if (backupstop == pdf_stoparray) {
        backupstart = pdf_startarray;
    } else {
        return;
    }
    for (; i >= 0; i--) {
        if (self->_operandstack[i]->type == backupstop) {
            balance++;
        } else if (self->_operandstack[i]->type == backupstart) {
            balance--;
        }
        if (balance == 0) {
            break;
        }
    }
    self->_nextoperand = i + 1;
}

static void push_array(lua_State * L, scannerdata * self)
{
    /*tex nesting tracking */
    int balance = 1;
    /*tex \LUA\ array index */
    int index = 1;
    Token *token = self->_operandstack[self->_nextoperand++];
    lua_newtable(L);
    while (token) {
    if (token->type == pdf_stoparray)
        balance--;
    if (token->type == pdf_startarray)
        balance++;
    if (!balance) {
        break;
    } else {
        push_token(L, self);
        lua_rawseti(L, -2, index++);
    }
    token = self->_operandstack[self->_nextoperand++];
    }
}


static void push_dict(lua_State * L, scannerdata * self)
{
    /*tex nesting tracking */
    int balance = 1;
    /*tex toggle between \LUA\ value and \LUA\ key */
    int needskey = 1;
    Token *token = self->_operandstack[self->_nextoperand++];
    lua_newtable(L);
    while (token) {
    if (token->type == pdf_stopdict)
        balance--;
    if (token->type == pdf_startdict)
        balance++;
    if (!balance) {
        break;
    } else if (needskey) {
        lua_pushlstring(L, token->string, token->value);
        needskey = 0;
    } else {
        push_token(L, self);
        needskey = 1;
        lua_rawset(L, -3);
    }
    token = self->_operandstack[self->_nextoperand++];
    }
}

const char *typenames[pdf_stopdict + 1] = {
    "unknown", "integer", "real", "boolean", "name", "operator",
    "string", "array", "array", "dict", "dict"
};

static void push_token(lua_State * L, scannerdata * self)
{
    Token *token = self->_operandstack[self->_nextoperand - 1];
    lua_createtable(L, 2, 0);
    lua_pushstring(L, typenames[token->type]);
    lua_rawseti(L, -2, 1);
    if (token->type == pdf_string || token->type == pdf_name) {
        lua_pushlstring(L, token->string, token->value);
    } else if (token->type == pdf_real || token->type == pdf_integer) {
        /*tex This is an integer or float. */
        lua_pushnumber(L, token->value);
    } else if (token->type == pdf_boolean) {
        lua_pushboolean(L, (int) token->value);
    } else if (token->type == pdf_startarray) {
        push_array(L, self);
    } else if (token->type == pdf_startdict) {
        push_dict(L, self);
    } else {
        lua_pushnil(L);
    }
    lua_rawseti(L, -2, 2);
}

static int scanner_popsingular(lua_State * L, int token_type)
{
    Token *token = NULL;
    /*tex this keeps track of how much of the operand stack needs deleting: */
    int clear = 0;
    scannerdata *self = scanner_check(L, 1);
    if (self->_nextoperand == 0) {
        return 0;
    }
    clear = self->_nextoperand - 1;
    token = self->_operandstack[self->_nextoperand - 1];
    if (token == NULL || (token->type != token_type)) {
        return 0;
    }
    /*tex
        The simple cases can be written out directly, but dicts and
        arrays are better done via the recursive function.
    */
    if (token_type == pdf_stoparray || token_type == pdf_stopdict) {
        operandstack_backup(self);
        clear = self->_nextoperand - 1;
        push_token(L, self);
        lua_rawgeti(L, -1, 2);
    } else if (token_type == pdf_real || token_type == pdf_integer) {
        /*tex the number can be an integer or float */
        lua_pushnumber(L, token->value);
    } else if (token_type == pdf_boolean) {
        lua_pushboolean(L, (int) token->value);
    } else if (token_type == pdf_name || token_type == pdf_string) {
        lua_pushlstring(L, token->string, token->value);
    } else {
        return 0;
    }
    clear_operand_stack(self, clear);
    return 1;
}

static int scanner_popanything(lua_State * L)
{
    Token *token = NULL;
    /*tex how much of the operand stack needs deleting: */
    int clear = 0;
    int token_type;
    scannerdata *self = scanner_check(L, 1);
    if (self->_nextoperand == 0) {
        return 0;
    }
    clear = self->_nextoperand - 1;
    token = self->_operandstack[self->_nextoperand - 1];
    if (token == NULL) {
        return 0;
    }
    token_type = token->type;
    /*tex
        The simple cases can be written out directly, but dicts and
        arrays are better done via the recursive function.
    */
    if (token_type == pdf_stoparray || token_type == pdf_stopdict) {
        operandstack_backup(self);
        clear = self->_nextoperand - 1;
        push_token(L, self);
    } else {
        push_token(L, self);
    }
    clear_operand_stack(self, clear);
    return 1;
}

static int scanner_popnumber(lua_State * L)
{
    if (scanner_popsingular(L, pdf_real))
        return 1;
    if (scanner_popsingular(L, pdf_integer))
        return 1;
    lua_pushnil(L);
    return 1;
}

static int scanner_popboolean(lua_State * L)
{
    if (scanner_popsingular(L, pdf_boolean))
        return 1;
    lua_pushnil(L);
    return 1;
}

static int scanner_popstring(lua_State * L)
{
    if (scanner_popsingular(L, pdf_string))
        return 1;
    lua_pushnil(L);
    return 1;
}

static int scanner_popname(lua_State * L)
{
    if (scanner_popsingular(L, pdf_name))
        return 1;
    lua_pushnil(L);
    return 1;
}

static int scanner_poparray(lua_State * L)
{
    if (scanner_popsingular(L, pdf_stoparray))
        return 1;
    lua_pushnil(L);
    return 1;
}

static int scanner_popdictionary(lua_State * L)
{
    if (scanner_popsingular(L, pdf_stopdict))
        return 1;
    lua_pushnil(L);
    return 1;
}

static int scanner_popany(lua_State * L)
{
    if (scanner_popanything(L))
        return 1;
    lua_pushnil(L);
    return 1;
}

static const luaL_Reg scannerlib_meta[] = {
    {0, 0}
};

static const struct luaL_Reg scannerlib_m[] = {
    { "done",          scanner_done },
    { "pop",           scanner_popany },
    { "popnumber",     scanner_popnumber },
    { "popname",       scanner_popname },
    { "popstring",     scanner_popstring },
    { "poparray",      scanner_poparray },
    { "popdictionary", scanner_popdictionary },
    { "popboolean",    scanner_popboolean },
    /*tex For old times sake: */
    { "popNumber",     scanner_popnumber },
    { "popName",       scanner_popname },
    { "popString",     scanner_popstring },
    { "popArray",      scanner_poparray },
    { "popDict",       scanner_popdictionary },
    { "popBool",       scanner_popboolean },
    /*tex Sentinel: */
    { NULL,            NULL }
};

static const luaL_Reg scannerlib[] = {
    { "scan", scanner_scan },
    /*tex Sentinel: */
    { NULL,   NULL }
};

LUALIB_API int luaopen_pdfscanner(lua_State * L)
{
    luaL_newmetatable(L, SCANNER);
    luaL_openlib(L, 0, scannerlib_meta, 0);
    lua_pushvalue(L, -1);
    lua_setfield(L, -2, "__index");
    luaL_openlib(L, NULL, scannerlib_m, 0);
    luaL_openlib(L, "pdfscanner", scannerlib, 0);
    return 1;
}