2021-08-21 18:22:58 +02:00
|
|
|
/*
|
|
|
|
Copyright 2021 Grégory Soutadé
|
|
|
|
|
|
|
|
This file is part of uPDFParser.
|
|
|
|
|
|
|
|
uPDFParser is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
uPDFParser 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 Lesser General Public License
|
|
|
|
along with uPDFParser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-12-18 17:27:35 +01:00
|
|
|
#include <unistd.h>
|
2022-03-12 21:12:07 +01:00
|
|
|
#include <algorithm>
|
2021-12-18 17:27:35 +01:00
|
|
|
|
2021-08-21 18:22:58 +02:00
|
|
|
#include "uPDFTypes.h"
|
|
|
|
#include "uPDFParser_common.h"
|
|
|
|
|
|
|
|
namespace uPDFParser
|
|
|
|
{
|
|
|
|
Name::Name(const std::string& name):
|
|
|
|
DataType(DataType::TYPE::NAME)
|
|
|
|
{
|
|
|
|
_value = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
String::String(const std::string& value):
|
|
|
|
DataType(DataType::TYPE::STRING)
|
|
|
|
{
|
|
|
|
_value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
HexaString::HexaString(const std::string& value):
|
|
|
|
DataType(DataType::TYPE::HEXASTRING)
|
|
|
|
{
|
|
|
|
_value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Integer::str()
|
|
|
|
{
|
|
|
|
std::string sign("");
|
2021-12-18 17:21:48 +01:00
|
|
|
// Sign automatically added for negative numbers
|
|
|
|
if (_signed && _value >= 0)
|
|
|
|
sign = "+";
|
2021-08-21 18:22:58 +02:00
|
|
|
|
|
|
|
return " " + sign + std::to_string(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Real::str()
|
|
|
|
{
|
2022-03-12 21:12:07 +01:00
|
|
|
std::string res;
|
2021-08-21 18:22:58 +02:00
|
|
|
std::string sign("");
|
2021-12-18 17:21:48 +01:00
|
|
|
if (_signed && _value >= 0)
|
|
|
|
sign = "+";
|
2021-08-21 18:22:58 +02:00
|
|
|
|
2022-03-12 21:12:07 +01:00
|
|
|
res = " " + sign + std::to_string(_value);
|
|
|
|
std::replace( res.begin(), res.end(), ',', '.');
|
|
|
|
|
|
|
|
return res;
|
2021-08-21 18:22:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Array::str()
|
|
|
|
{
|
|
|
|
std::string res("[");
|
|
|
|
std::vector<DataType*>::iterator it;
|
|
|
|
|
|
|
|
for(it = _value.begin(); it!=_value.end(); it++)
|
|
|
|
{
|
2023-08-08 20:06:44 +02:00
|
|
|
/* These types has already a space in front */
|
|
|
|
if ((*it)->type() != DataType::TYPE::INTEGER &&
|
|
|
|
(*it)->type() != DataType::TYPE::REAL &&
|
|
|
|
(*it)->type() != DataType::TYPE::REFERENCE)
|
|
|
|
{
|
|
|
|
if (res.size() > 1)
|
|
|
|
res += " ";
|
|
|
|
res += (*it)->str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (res.size() > 1)
|
|
|
|
res += (*it)->str();
|
|
|
|
/* First time, remove front space*/
|
|
|
|
else
|
|
|
|
res += (*it)->str().substr(1);
|
|
|
|
}
|
2021-08-21 18:22:58 +02:00
|
|
|
}
|
2023-08-08 20:06:44 +02:00
|
|
|
|
|
|
|
if (res.size() == 1)
|
|
|
|
res += " ";
|
|
|
|
|
2022-03-14 19:56:50 +01:00
|
|
|
return res + "]";
|
2021-08-21 18:22:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Dictionary::addData(const std::string& key, DataType* value)
|
|
|
|
{
|
|
|
|
_value[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Dictionary::str()
|
|
|
|
{
|
|
|
|
std::string res("<<");
|
|
|
|
std::map<std::string, DataType*>::iterator it;
|
2022-03-12 21:12:07 +01:00
|
|
|
|
2021-08-21 18:22:58 +02:00
|
|
|
for(it = _value.begin(); it!=_value.end(); it++)
|
|
|
|
{
|
|
|
|
res += std::string("/") + it->first;
|
|
|
|
if (it->second)
|
|
|
|
res += it->second->str();
|
|
|
|
}
|
|
|
|
|
|
|
|
return res + std::string(">>\n");
|
|
|
|
}
|
2021-12-18 17:27:35 +01:00
|
|
|
|
|
|
|
std::string Stream::str()
|
|
|
|
{
|
2023-01-07 15:43:39 +01:00
|
|
|
std::string res = "stream\n";
|
2021-12-18 17:27:35 +01:00
|
|
|
const char* streamData = (const char*)data(); // Force reading if not in memory
|
|
|
|
res += std::string(streamData, _dataLength);
|
2023-01-07 15:43:39 +01:00
|
|
|
res += "\nendstream\n";
|
2021-12-18 17:27:35 +01:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char* Stream::data()
|
|
|
|
{
|
|
|
|
if (!_data)
|
|
|
|
{
|
|
|
|
if (!fd)
|
|
|
|
EXCEPTION(INVALID_STREAM, "Accessing data, but no file descriptor supplied");
|
|
|
|
|
|
|
|
_dataLength = endOffset - startOffset;
|
|
|
|
_data = new unsigned char[_dataLength];
|
|
|
|
freeData = true;
|
|
|
|
|
|
|
|
lseek(fd, startOffset, SEEK_SET);
|
|
|
|
int ret = ::read(fd, _data, _dataLength);
|
|
|
|
|
|
|
|
if ((unsigned int)ret != _dataLength)
|
|
|
|
EXCEPTION(INVALID_STREAM, "Not enough data to read (" << ret << ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
return _data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stream::setData(unsigned char* data, unsigned int dataLength, bool freeData)
|
|
|
|
{
|
|
|
|
if (_data && freeData)
|
|
|
|
delete[] _data;
|
2022-12-23 16:55:47 +01:00
|
|
|
|
|
|
|
dict.deleteKey("Length");
|
|
|
|
dict.addData("Length", new Integer(dataLength));
|
|
|
|
|
2021-12-18 17:27:35 +01:00
|
|
|
this->_data = data;
|
|
|
|
this->_dataLength = dataLength;
|
|
|
|
this->freeData = freeData;
|
|
|
|
}
|
2021-08-21 18:22:58 +02:00
|
|
|
}
|