aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/Pl_ASCII85Decoder.cc
blob: 3a3b57b10d106f51a0ee4b858b29c449561525ff (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
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
#include <qpdf/Pl_ASCII85Decoder.hh>
#include <qpdf/QTC.hh>
#include <stdexcept>
#include <string.h>

Pl_ASCII85Decoder::Pl_ASCII85Decoder(char const* identifier, Pipeline* next) :
    Pipeline(identifier, next),
    pos(0),
    eod(0)
{
    memset(this->inbuf, 117, 5);
}

Pl_ASCII85Decoder::~Pl_ASCII85Decoder()
{
}

void
Pl_ASCII85Decoder::write(unsigned char* buf, size_t len)
{
    if (eod > 1)
    {
	return;
    }
    for (size_t i = 0; i < len; ++i)
    {
	if (eod > 1)
	{
	    break;
	}
	else if (eod == 1)
	{
	    if (buf[i] == '>')
	    {
		flush();
		eod = 2;
	    }
	    else
	    {
		throw std::runtime_error(
		    "broken end-of-data sequence in base 85 data");
	    }
	}
	else
	{
	    switch (buf[i])
	    {
	      case ' ':
	      case '\f':
	      case '\v':
	      case '\t':
	      case '\r':
	      case '\n':
		QTC::TC("libtests", "Pl_ASCII85Decoder ignore space");
		// ignore whitespace
		break;

	      case '~':
		eod = 1;
		break;

	      case 'z':
		if (pos != 0)
		{
		    throw std::runtime_error(
			"unexpected z during base 85 decode");
		}
		else
		{
		    QTC::TC("libtests", "Pl_ASCII85Decoder read z");
                    unsigned char zeroes[4];
                    memset(zeroes, '\0', 4);
		    getNext()->write(zeroes, 4);
		}
		break;

	      default:
		if ((buf[i] < 33) || (buf[i] > 117))
		{
		    throw std::runtime_error(
			"character out of range during base 85 decode");
		}
		else
		{
		    this->inbuf[this->pos++] = buf[i];
		    if (pos == 5)
		    {
			flush();
		    }
		}
		break;
	    }
	}
    }
}

void
Pl_ASCII85Decoder::flush()
{
    if (this->pos == 0)
    {
	QTC::TC("libtests", "Pl_ASCII85Decoder no-op flush");
	return;
    }
    unsigned long lval = 0;
    for (int i = 0; i < 5; ++i)
    {
	lval *= 85;
	lval += (this->inbuf[i] - 33);
    }

    unsigned char outbuf[4];
    memset(outbuf, 0, 4);
    for (int i = 3; i >= 0; --i)
    {
	outbuf[i] = lval & 0xff;
	lval >>= 8;
    }

    QTC::TC("libtests", "Pl_ASCII85Decoder partial flush",
	    (this->pos == 5) ? 0 : 1);
    getNext()->write(outbuf, this->pos - 1);

    this->pos = 0;
    memset(this->inbuf, 117, 5);
}

void
Pl_ASCII85Decoder::finish()
{
    flush();
    getNext()->finish();
}