mirror of
https://github.com/tesseract-ocr/tesseract.git
synced 2025-01-22 09:53:03 +08:00
Fix serialization for vector of RecodedCharID
Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
parent
33868a52ae
commit
6cfe604d58
@ -28,8 +28,6 @@
|
||||
|
||||
namespace tesseract {
|
||||
|
||||
class TBOX;
|
||||
|
||||
/***********************************************************************
|
||||
QUOTE_IT MACRO DEFINITION
|
||||
===========================
|
||||
@ -90,7 +88,6 @@ class TESS_API TFile {
|
||||
bool DeSerializeSize(int32_t* data);
|
||||
bool DeSerialize(std::string& data);
|
||||
bool DeSerialize(std::vector<char>& data);
|
||||
bool DeSerialize(TBOX& data);
|
||||
template <typename T> bool DeSerialize(std::vector<T>& data);
|
||||
template <typename T>
|
||||
bool DeSerialize(T *data, size_t count = 1) {
|
||||
@ -106,29 +103,6 @@ class TESS_API TFile {
|
||||
bool Serialize(const T *data, size_t count = 1) {
|
||||
return FWrite(data, sizeof(T), count) == static_cast<int>(count);
|
||||
}
|
||||
template <typename T>
|
||||
bool SerializeClasses(const std::vector<T> &data) {
|
||||
int32_t sz = data.size();
|
||||
if (FWrite(&sz, sizeof(sz), 1) != 1)
|
||||
return false;
|
||||
for (auto &d : data) {
|
||||
if (!d.Serialize(this))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
template <typename T>
|
||||
bool DeSerializeClasses(std::vector<T> &data) {
|
||||
int32_t sz = data.size();
|
||||
if (FRead(&sz, sizeof(sz), 1) != 1)
|
||||
return false;
|
||||
data.resize(sz);
|
||||
for (auto &d : data) {
|
||||
if (!d.DeSerialize(this))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Skip data.
|
||||
bool Skip(size_t count);
|
||||
|
@ -83,6 +83,46 @@ static bool DecodeRadicalTable(STRING* radical_data, RSMap* radical_map) {
|
||||
return true;
|
||||
}
|
||||
|
||||
template <>
|
||||
bool TFile::DeSerialize(std::vector<RecodedCharID>& data) {
|
||||
uint32_t size;
|
||||
if (!DeSerialize(&size)) {
|
||||
return false;
|
||||
}
|
||||
// Arbitrarily limit the number of elements to protect against bad data.
|
||||
const uint32_t limit = 50000000;
|
||||
assert(size <= limit);
|
||||
if (size > limit) {
|
||||
return false;
|
||||
}
|
||||
if (size == 0) {
|
||||
data.clear();
|
||||
} else {
|
||||
// TODO: optimize.
|
||||
data.resize(size);
|
||||
for (auto& item : data) {
|
||||
if (!item.DeSerialize(this)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <>
|
||||
bool TFile::Serialize(const std::vector<RecodedCharID>& data) {
|
||||
uint32_t size = data.size();
|
||||
if (!Serialize(&size)) {
|
||||
return false;
|
||||
}
|
||||
for (auto& item : data) {
|
||||
if (!item.Serialize(this)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
UnicharCompress::UnicharCompress() : code_range_(0) {}
|
||||
UnicharCompress::UnicharCompress(const UnicharCompress& src) { *this = src; }
|
||||
UnicharCompress::~UnicharCompress() { Cleanup(); }
|
||||
@ -298,13 +338,12 @@ int UnicharCompress::DecodeUnichar(const RecodedCharID& code) const {
|
||||
|
||||
// Writes to the given file. Returns false in case of error.
|
||||
bool UnicharCompress::Serialize(TFile* fp) const {
|
||||
return fp->SerializeClasses(encoder_);
|
||||
return fp->Serialize(encoder_);
|
||||
}
|
||||
|
||||
// Reads from the given file. Returns false in case of error.
|
||||
bool UnicharCompress::DeSerialize(TFile* fp) {
|
||||
if (!fp->DeSerializeClasses(encoder_))
|
||||
return false;
|
||||
if (!fp->DeSerialize(encoder_)) return false;
|
||||
ComputeCodeRange();
|
||||
SetupDecoder();
|
||||
return true;
|
||||
|
Loading…
Reference in New Issue
Block a user