| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2020-2026 Carbo Kuo and contributors |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include <cassert> |
| 20 | #include <cstdint> |
| 21 | #include <cstring> |
| 22 | |
| 23 | #include "Lexicon.hpp" |
| 24 | #include "SerializedValues.hpp" |
| 25 | |
| 26 | using namespace opencc; |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | template <typename INT_TYPE> INT_TYPE ReadInteger(FILE* fp) { |
| 31 | INT_TYPE num; |
| 32 | size_t unitsRead = fread(&num, sizeof(INT_TYPE), 1, fp); |
| 33 | if (unitsRead != 1) { |
| 34 | throw InvalidFormat("Invalid OpenCC binary dictionary."); |
| 35 | } |
| 36 | return num; |
| 37 | } |
| 38 | |
| 39 | template <typename INT_TYPE> |
| 40 | INT_TYPE ReadIntegerFromBuffer(const char* data, size_t size, size_t* offset) { |
| 41 | if (size - *offset < sizeof(INT_TYPE)) { |
| 42 | throw InvalidFormat("Invalid OpenCC binary dictionary."); |
| 43 | } |
| 44 | INT_TYPE num; |
| 45 | memcpy(&num, data + *offset, sizeof(INT_TYPE)); |
| 46 | *offset += sizeof(INT_TYPE); |
| 47 | return num; |
| 48 | } |
| 49 | |
| 50 | template <typename INT_TYPE> void WriteInteger(FILE* fp, INT_TYPE num) { |
| 51 | size_t unitsWritten = fwrite(&num, sizeof(INT_TYPE), 1, fp); |
| 52 | if (unitsWritten != 1) { |
| 53 | throw InvalidFormat("Cannot write binary dictionary."); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | } // namespace |
| 58 | |
| 59 | size_t SerializedValues::KeyMaxLength() const { return 0; } |
| 60 | |
| 61 | void SerializedValues::SerializeToFile(FILE* fp) const { |
| 62 | std::string valueBuf; |
| 63 | std::vector<uint16_t> valueBytes; |
| 64 | uint32_t valueTotalLength = 0; |
| 65 | ConstructBuffer(&valueBuf, &valueBytes, &valueTotalLength); |
| 66 | // Number of items |
| 67 | uint32_t numItems = static_cast<uint32_t>(lexicon->Length()); |
| 68 | WriteInteger(fp, numItems); |
| 69 | |
| 70 | // Data |
| 71 | WriteInteger(fp, valueTotalLength); |
| 72 | fwrite(valueBuf.c_str(), sizeof(char), valueTotalLength, fp); |
| 73 | |
| 74 | size_t valueCursor = 0; |
| 75 | for (const std::unique_ptr<DictEntry>& entry : *lexicon) { |
| 76 | // Number of values |
| 77 | uint16_t numValues = static_cast<uint16_t>(entry->NumValues()); |
| 78 | WriteInteger(fp, numValues); |
| 79 | // Values offset |
| 80 | for (uint16_t i = 0; i < numValues; i++) { |
| 81 | uint16_t numValueBytes = valueBytes[valueCursor++]; |
| 82 | WriteInteger(fp, numValueBytes); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | std::shared_ptr<SerializedValues> SerializedValues::NewFromFile(FILE* fp) { |
| 88 | long savedOffset = ftell(fp); |
| 89 | fseek(fp, 0L, SEEK_END); |
| 90 | long fileEnd = ftell(fp); |
| 91 | fseek(fp, savedOffset, SEEK_SET); |
| 92 | size_t remainingSize = |
| 93 | (fileEnd > savedOffset) ? static_cast<size_t>(fileEnd - savedOffset) : 0; |
| 94 | |
| 95 | std::string buffer; |
| 96 | buffer.resize(remainingSize); |
| 97 | size_t unitsRead = fread(const_cast<char*>(buffer.c_str()), sizeof(char), |
| 98 | remainingSize, fp); |
| 99 | if (unitsRead != remainingSize) { |
| 100 | throw InvalidFormat("Invalid OpenCC binary dictionary."); |
| 101 | } |
| 102 | |
| 103 | size_t bytesRead = 0; |
| 104 | return NewFromBuffer(buffer.data(), buffer.size(), &bytesRead); |
| 105 | } |
| 106 | |
| 107 | std::shared_ptr<SerializedValues> SerializedValues::NewFromBuffer( |
| 108 | const char* data, size_t size, size_t* bytesRead) { |
| 109 | std::shared_ptr<SerializedValues> dict( |
| 110 | new SerializedValues(LexiconPtr(new Lexicon))); |
| 111 | size_t offset = 0; |
| 112 | |
| 113 | // Number of items |
| 114 | uint32_t numItems = ReadIntegerFromBuffer<uint32_t>(data, size, &offset); |
| 115 | |
| 116 | // Values |
| 117 | uint32_t valueTotalLength = |
| 118 | ReadIntegerFromBuffer<uint32_t>(data, size, &offset); |
| 119 | if (valueTotalLength > size - offset) { |
| 120 | throw InvalidFormat( |
| 121 | "Invalid OpenCC binary dictionary (valueTotalLength exceeds file size)"); |
| 122 | } |
| 123 | std::string valueBuffer; |
| 124 | valueBuffer.resize(valueTotalLength); |
| 125 | memcpy(valueBuffer.data(), data + offset, valueTotalLength); |
| 126 | offset += valueTotalLength; |
| 127 | |
| 128 | // Offsets |
| 129 | const char* pValueBuffer = valueBuffer.c_str(); |
| 130 | const char* pValueBufferEnd = pValueBuffer + valueTotalLength; |
| 131 | for (uint32_t i = 0; i < numItems; i++) { |
| 132 | // Number of values |
| 133 | uint16_t numValues = ReadIntegerFromBuffer<uint16_t>(data, size, &offset); |
| 134 | // Value offset |
| 135 | std::vector<std::string> values; |
| 136 | for (uint16_t j = 0; j < numValues; j++) { |
| 137 | uint16_t numValueBytes = |
| 138 | ReadIntegerFromBuffer<uint16_t>(data, size, &offset); |
| 139 | if (numValueBytes == 0 || pValueBuffer + numValueBytes > pValueBufferEnd) { |
| 140 | throw InvalidFormat( |
| 141 | "Invalid OpenCC binary dictionary (value offset out of bounds)"); |
| 142 | } |
| 143 | if (pValueBuffer[numValueBytes - 1] != '\0') { |
| 144 | throw InvalidFormat( |
| 145 | "Invalid OpenCC binary dictionary (value not null-terminated)"); |
| 146 | } |
| 147 | const char* value = pValueBuffer; |
| 148 | pValueBuffer += numValueBytes; |
| 149 | values.push_back(value); |
| 150 | } |
| 151 | DictEntry* entry = DictEntryFactory::New("", values); |
| 152 | dict->lexicon->Add(entry); |
| 153 | } |
| 154 | |
| 155 | if (bytesRead != nullptr) { |
| 156 | *bytesRead = offset; |
| 157 | } |
| 158 | return dict; |
| 159 | } |
| 160 | |
| 161 | void SerializedValues::ConstructBuffer(std::string* valueBuffer, |
| 162 | std::vector<uint16_t>* valueBytes, |
| 163 | uint32_t* valueTotalLength) const { |
| 164 | *valueTotalLength = 0; |
| 165 | // Calculate total length. |
| 166 | for (const std::unique_ptr<DictEntry>& entry : *lexicon) { |
| 167 | assert(entry->NumValues() != 0); |
| 168 | for (const auto& value : entry->Values()) { |
| 169 | *valueTotalLength += static_cast<uint32_t>(value.length()) + 1; |
| 170 | } |
| 171 | } |
| 172 | // Write values to the buffer. |
| 173 | valueBuffer->resize(*valueTotalLength, '\0'); |
| 174 | char* pValueBuffer = valueBuffer->data(); |
| 175 | for (const std::unique_ptr<DictEntry>& entry : *lexicon) { |
| 176 | for (const auto& value : entry->Values()) { |
| 177 | strcpy(pValueBuffer, value.c_str()); |
| 178 | valueBytes->push_back(static_cast<uint16_t>(value.length() + 1)); |
| 179 | pValueBuffer += value.length() + 1; |
| 180 | } |
| 181 | } |
| 182 | assert(valueBuffer->data() + *valueTotalLength == pValueBuffer); |
| 183 | } |