aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/Pl_QPDFTokenizer.cc
blob: 9a93f8f3123e0ae0efc8ae555842814b0ab1c4d9 (plain) (blame)
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
#include <qpdf/Pl_QPDFTokenizer.hh>
#include <qpdf/QTC.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/BufferInputSource.hh>
#include <stdexcept>
#include <string.h>

Pl_QPDFTokenizer::Members::Members() :
    filter(0),
    buf("tokenizer buffer")
{
}

Pl_QPDFTokenizer::Members::~Members()
{
}

Pl_QPDFTokenizer::Pl_QPDFTokenizer(char const* identifier,
                                   QPDFObjectHandle::TokenFilter* filter,
                                   Pipeline* next) :
    Pipeline(identifier, next),
    m(new Members)
{
    m->filter = filter;
    QPDFObjectHandle::TokenFilter::PipelineAccessor::setPipeline(
        m->filter, next);
    m->tokenizer.allowEOF();
    m->tokenizer.includeIgnorable();
}

Pl_QPDFTokenizer::~Pl_QPDFTokenizer()
{
}

void
Pl_QPDFTokenizer::write(unsigned char* data, size_t len)
{
    this->m->buf.write(data, len);
}

void
Pl_QPDFTokenizer::finish()
{
    this->m->buf.finish();
    PointerHolder<InputSource> input =
        new BufferInputSource("tokenizer data",
                              this->m->buf.getBuffer(), true);

    while (true)
    {
        QPDFTokenizer::Token token = this->m->tokenizer.readToken(
            input, "offset " + QUtil::int_to_string(input->tell()),
            true);
	this->m->filter->handleToken(token);
        if (token.getType() == QPDFTokenizer::tt_eof)
        {
            break;
        }
        else if ((token.getType() == QPDFTokenizer::tt_word) &&
                 (token.getValue() == "ID"))
        {
            // Read the space after the ID.
            char ch = ' ';
            input->read(&ch, 1);
            this->m->filter->handleToken(
                QPDFTokenizer::Token(
                    QPDFTokenizer::tt_space, std::string(1, ch)));
            QTC::TC("qpdf", "Pl_QPDFTokenizer found ID");
            this->m->tokenizer.expectInlineImage(input);
        }
    }
    this->m->filter->handleEOF();
    QPDFObjectHandle::TokenFilter::PipelineAccessor::setPipeline(
        m->filter, 0);
    Pipeline* next = this->getNext(true);
    if (next)
    {
        next->finish();
    }
}