1 | /*␊ |
2 | Copyright 2021 Grégory Soutadé␊ |
3 | ␊ |
4 | This file is part of uPDFParser.␊ |
5 | ␊ |
6 | uPDFParser is free software: you can redistribute it and/or modify␊ |
7 | it under the terms of the GNU Lesser General Public License as published by␊ |
8 | the Free Software Foundation, either version 3 of the License, or␊ |
9 | (at your option) any later version.␊ |
10 | ␊ |
11 | uPDFParser is distributed in the hope that it will be useful,␊ |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of␊ |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the␊ |
14 | GNU Lesser General Public License for more details.␊ |
15 | ␊ |
16 | You should have received a copy of the GNU Lesser General Public License␊ |
17 | along with uPDFParser. If not, see <http://www.gnu.org/licenses/>.␊ |
18 | */␊ |
19 | ␊ |
20 | #include <unistd.h>␊ |
21 | #include <algorithm>␊ |
22 | ␊ |
23 | #include "uPDFTypes.h"␊ |
24 | #include "uPDFParser_common.h"␊ |
25 | ␊ |
26 | namespace uPDFParser␊ |
27 | {␊ |
28 | Name::Name(const std::string& name):␊ |
29 | ␉DataType(DataType::TYPE::NAME)␊ |
30 | {␊ |
31 | ␉_value = name;␊ |
32 | }␊ |
33 | ␊ |
34 | String::String(const std::string& value):␊ |
35 | ␉DataType(DataType::TYPE::STRING)␊ |
36 | {␊ |
37 | ␉_value = value;␊ |
38 | }␊ |
39 | ␊ |
40 | HexaString::HexaString(const std::string& value):␊ |
41 | ␉DataType(DataType::TYPE::HEXASTRING)␊ |
42 | {␊ |
43 | ␉_value = value;␊ |
44 | }␊ |
45 | ␊ |
46 | std::string Integer::str()␊ |
47 | {␊ |
48 | ␉std::string sign("");␊ |
49 | ␉// Sign automatically added for negative numbers␊ |
50 | ␉if (_signed && _value >= 0)␊ |
51 | ␉ sign = "+";␊ |
52 | ␊ |
53 | ␉return " " + sign + std::to_string(_value);␊ |
54 | }␊ |
55 | ␊ |
56 | std::string Real::str()␊ |
57 | {␊ |
58 | ␉std::string res;␊ |
59 | ␉std::string sign("");␊ |
60 | ␉if (_signed && _value >= 0)␊ |
61 | ␉ sign = "+";␊ |
62 | ␊ |
63 | ␉res = " " + sign + std::to_string(_value);␊ |
64 | ␉std::replace( res.begin(), res.end(), ',', '.');␊ |
65 | ␊ |
66 | ␉return res;␊ |
67 | }␊ |
68 | ␊ |
69 | std::string Array::str()␊ |
70 | {␊ |
71 | ␉std::string res("[");␊ |
72 | ␉std::vector<DataType*>::iterator it;␊ |
73 | ␊ |
74 | ␉for(it = _value.begin(); it!=_value.end(); it++)␊ |
75 | ␉{␊ |
76 | ␉ if (res.size() > 1 &&␊ |
77 | ␉␉(*it)->type() != DataType::TYPE::INTEGER &&␊ |
78 | ␉␉(*it)->type() != DataType::TYPE::REAL)␊ |
79 | ␉␉res += " ";␊ |
80 | ␉ res += (*it)->str();␊ |
81 | ␉}␊ |
82 | ␉ ␊ |
83 | ␉return res + "]";␊ |
84 | }␊ |
85 | ␊ |
86 | void Dictionary::addData(const std::string& key, DataType* value)␊ |
87 | {␊ |
88 | ␉_value[key] = value;␊ |
89 | }␊ |
90 | ␊ |
91 | std::string Dictionary::str()␊ |
92 | {␊ |
93 | ␉std::string res("<<");␊ |
94 | ␉std::map<std::string, DataType*>::iterator it;␊ |
95 | ␉␊ |
96 | ␉for(it = _value.begin(); it!=_value.end(); it++)␊ |
97 | ␉{␊ |
98 | ␉ res += std::string("/") + it->first;␊ |
99 | ␉ if (it->second)␊ |
100 | ␉␉res += it->second->str();␊ |
101 | ␉}␊ |
102 | ␉ ␊ |
103 | ␉return res + std::string(">>\n"); ␊ |
104 | }␊ |
105 | ␊ |
106 | std::string Stream::str()␊ |
107 | {␊ |
108 | ␉std::string res = "stream\n";␊ |
109 | ␉const char* streamData = (const char*)data(); // Force reading if not in memory␊ |
110 | ␉res += std::string(streamData, _dataLength);␊ |
111 | ␉res += "\nendstream\n";␊ |
112 | ␊ |
113 | ␉return res;␊ |
114 | }␊ |
115 | ␊ |
116 | unsigned char* Stream::data()␊ |
117 | {␊ |
118 | ␉if (!_data)␊ |
119 | ␉{␊ |
120 | ␉ if (!fd)␊ |
121 | ␉␉EXCEPTION(INVALID_STREAM, "Accessing data, but no file descriptor supplied");␊ |
122 | ␊ |
123 | ␉ _dataLength = endOffset - startOffset;␊ |
124 | ␉ _data = new unsigned char[_dataLength];␊ |
125 | ␉ freeData = true;␊ |
126 | ␊ |
127 | ␉ lseek(fd, startOffset, SEEK_SET);␊ |
128 | ␉ int ret = ::read(fd, _data, _dataLength);␊ |
129 | ␊ |
130 | ␉ if ((unsigned int)ret != _dataLength)␊ |
131 | ␉␉EXCEPTION(INVALID_STREAM, "Not enough data to read (" << ret << ")");␊ |
132 | ␉}␊ |
133 | ␉␊ |
134 | ␉return _data;␊ |
135 | }␊ |
136 | ␊ |
137 | void Stream::setData(unsigned char* data, unsigned int dataLength, bool freeData)␊ |
138 | {␊ |
139 | ␉if (_data && freeData)␊ |
140 | ␉ delete[] _data;␊ |
141 | ␊ |
142 | ␉dict.deleteKey("Length");␊ |
143 | ␉dict.addData("Length", new Integer(dataLength));␊ |
144 | ␊ |
145 | ␉this->_data = data;␊ |
146 | ␉this->_dataLength = dataLength;␊ |
147 | ␉this->freeData = freeData;␊ |
148 | }␊ |
149 | }␊ |