| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* libwps |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * Major Contributor(s): |
| 10 | * Copyright (C) 2009, 2011 Alonso Laurent ([email protected]) |
| 11 | * Copyright (C) 2006, 2007 Andrew Ziem |
| 12 | * Copyright (C) 2004-2006 Fridrich Strba ([email protected]) |
| 13 | * Copyright (C) 2004 Marc Maurer ([email protected]) |
| 14 | * Copyright (C) 2003-2005 William Lachance ([email protected]) |
| 15 | * |
| 16 | * For minor contributions see the git repository. |
| 17 | * |
| 18 | * Alternatively, the contents of this file may be used under the terms |
| 19 | * of the GNU Lesser General Public License Version 2.1 or later |
| 20 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 21 | * applicable instead of those above. |
| 22 | * |
| 23 | * For further information visit http://libwps.sourceforge.net |
| 24 | */ |
| 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include "libwps_internal.h" |
| 30 | |
| 31 | #include "WPSParser.h" |
| 32 | |
| 33 | #include "WPSTextParser.h" |
| 34 | |
| 35 | //////////////////////////////////////////////////////////// |
| 36 | // Constructor |
| 37 | //////////////////////////////////////////////////////////// |
| 38 | WPSTextParser::WPSTextParser(WPSParser &parser, RVNGInputStreamPtr &input) |
| 39 | : m_version(0) |
| 40 | , m_input(input) |
| 41 | , m_mainParser(parser) |
| 42 | , m_textPositions(), |
| 43 | m_FODList() |
| 44 | , m_asciiFile(parser.ascii()) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | WPSTextParser::~WPSTextParser() {} |
| 49 | |
| 50 | |
| 51 | int WPSTextParser::version() const |
| 52 | { |
| 53 | if (m_version <= 0) |
| 54 | m_version=m_mainParser.version(); |
| 55 | return m_version; |
| 56 | } |
| 57 | |
| 58 | WPSParser::NameMultiMap &WPSTextParser::getNameEntryMap() |
| 59 | { |
| 60 | return m_mainParser.getNameEntryMap(); |
| 61 | } |
| 62 | |
| 63 | WPSParser::NameMultiMap const &WPSTextParser::getNameEntryMap() const |
| 64 | { |
| 65 | return m_mainParser.getNameEntryMap(); |
| 66 | } |
| 67 | |
| 68 | //////////////////////////////////////////////////////////// |
| 69 | // read data |
| 70 | //////////////////////////////////////////////////////////// |
| 71 | bool WPSTextParser::readFDP(WPSEntry const &entry, |
| 72 | std::vector<DataFOD> &fods, |
| 73 | WPSTextParser::FDPParser parser) |
| 74 | { |
| 75 | RVNGInputStreamPtr input = getInput(); |
| 76 | if (entry.length() <= 0 || entry.begin() <= 0) |
| 77 | { |
| 78 | WPS_DEBUG_MSG(("WPSTextParser::readFDP: warning: FDP entry unintialized\n")); |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | entry.setParsed(); |
| 83 | long page_offset = entry.begin(); |
| 84 | long length = entry.length(); |
| 85 | long endPage = entry.end(); |
| 86 | |
| 87 | bool smallFDP = version() < 5; |
| 88 | int deplSize = smallFDP ? 1 : 2; |
| 89 | int headerSize = smallFDP ? 4 : 8; |
| 90 | |
| 91 | if (length < headerSize) |
| 92 | { |
| 93 | WPS_DEBUG_MSG(("WPSTextParser::readFDP: warning: FDP offset=0x%lx, length=0x%lx\n", |
| 94 | static_cast<unsigned long>(page_offset), static_cast<unsigned long>(length))); |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | libwps::DebugStream f, f2; |
| 99 | if (smallFDP) |
| 100 | { |
| 101 | endPage--; |
| 102 | input->seek(endPage, librevenge::RVNG_SEEK_SET); |
| 103 | } |
| 104 | else |
| 105 | input->seek(page_offset, librevenge::RVNG_SEEK_SET); |
| 106 | uint16_t cfod = deplSize == 1 ? uint16_t(libwps::readU8(m_input)) : libwps::readU16(m_input); |
| 107 | |
| 108 | f << "FDP: N="<<int(cfod); |
| 109 | if (smallFDP) input->seek(page_offset, librevenge::RVNG_SEEK_SET); |
| 110 | else f << ", unk=" << libwps::read16(m_input); |
| 111 | |
| 112 | if (headerSize+(4+deplSize)*static_cast<long>(cfod) > length) |
| 113 | { |
| 114 | WPS_DEBUG_MSG(("WPSTextParser::readFDP: error: cfod = %i (0x%X)\n", cfod, unsigned(cfod))); |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | auto firstFod = int(fods.size()); |
| 119 | long lastLimit = firstFod ? fods.back().m_pos : 0; |
| 120 | |
| 121 | long lastReadPos = 0L; |
| 122 | |
| 123 | DataFOD::Type type = DataFOD::ATTR_UNKN; |
| 124 | if (entry.hasType("FDPC")) type = DataFOD::ATTR_TEXT; |
| 125 | else if (entry.hasType("FDPP")) type = DataFOD::ATTR_PARAG; |
| 126 | else |
| 127 | { |
| 128 | WPS_DEBUG_MSG(("WPSTextParser::readFDP: FDP error: unknown type = '%s'\n", entry.type().c_str())); |
| 129 | } |
| 130 | |
| 131 | /* Read array of fcLim of FODs. The fcLim refers to the offset of the |
| 132 | last character covered by the formatting. */ |
| 133 | for (int i = 0; i <= cfod; ++i) |
| 134 | { |
| 135 | DataFOD fod; |
| 136 | fod.m_type = type; |
| 137 | fod.m_pos = long(libwps::readU32(m_input)); |
| 138 | if (fod.m_pos == 0) fod.m_pos=m_textPositions.begin(); |
| 139 | |
| 140 | /* check that fcLim is not too large */ |
| 141 | if (fod.m_pos > m_textPositions.end()) |
| 142 | { |
| 143 | WPS_DEBUG_MSG(("WPSTextParser::readFDP: error: length of 'text selection' %ld > " |
| 144 | "total text length %ld\n", fod.m_pos, m_textPositions.end())); |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | /* check that pos is monotonic */ |
| 149 | if (lastLimit > fod.m_pos) |
| 150 | { |
| 151 | WPS_DEBUG_MSG(("WPSTextParser::readFDP: error: character position list must " |
| 152 | "be monotonic, but found %ld, %ld\n", lastLimit, fod.m_pos)); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | lastLimit = fod.m_pos; |
| 157 | |
| 158 | if (i != cfod) |
| 159 | fods.push_back(fod); |
| 160 | else // ignore the last text position |
| 161 | lastReadPos = fod.m_pos; |
| 162 | } |
| 163 | |
| 164 | std::vector<DataFOD>::iterator fods_iter; |
| 165 | /* Read array of bfprop of FODs. The bfprop is the offset where |
| 166 | the FPROP is located. */ |
| 167 | f << ", Tpos:defP=("; |
| 168 | for (fods_iter = fods.begin() + firstFod; fods_iter!= fods.end(); ++fods_iter) |
| 169 | { |
| 170 | unsigned depl = deplSize == 1 ? libwps::readU8(m_input) : libwps::readU16(m_input); |
| 171 | /* check size of bfprop */ |
| 172 | if ((depl < unsigned(headerSize+(4+deplSize)*cfod) && depl > 0) || |
| 173 | page_offset+long(depl) > endPage) |
| 174 | { |
| 175 | WPS_DEBUG_MSG(("WPSTextParser::readFDP: error: pos of bfprop is bad " |
| 176 | "%u (0x%X)\n", depl, depl)); |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | if (depl) |
| 181 | (*fods_iter).m_defPos = long(depl) + page_offset; |
| 182 | } |
| 183 | ascii().addPos(input->tell()); |
| 184 | |
| 185 | std::map<long,int> mapPtr; |
| 186 | bool smallSzInProp = smallFDP; |
| 187 | for (fods_iter = fods.begin() + firstFod; fods_iter!= fods.end(); ++fods_iter) |
| 188 | { |
| 189 | long pos = (*fods_iter).m_defPos; |
| 190 | f << std::hex << (*fods_iter).m_pos << std::dec << ":"; |
| 191 | if (pos == 0) |
| 192 | { |
| 193 | f << "_, "; |
| 194 | continue; |
| 195 | } |
| 196 | |
| 197 | auto it= mapPtr.find(pos); |
| 198 | if (it != mapPtr.end()) |
| 199 | { |
| 200 | (*fods_iter).m_id = mapPtr[pos]; |
| 201 | f << entry.type() << (*fods_iter).m_id << ", "; |
| 202 | continue; |
| 203 | } |
| 204 | |
| 205 | input->seek(pos, librevenge::RVNG_SEEK_SET); |
| 206 | int szProp = smallSzInProp ? libwps::readU8(m_input) : libwps::readU16(m_input); |
| 207 | if (smallSzInProp) szProp++; |
| 208 | if (szProp == 0) |
| 209 | { |
| 210 | WPS_DEBUG_MSG(("WPSTextParser::readFDP: error: 0 == szProp at file offset 0x%lx\n", static_cast<unsigned long>(input->tell()-1))); |
| 211 | return false; |
| 212 | } |
| 213 | long endPos = pos+szProp; |
| 214 | if (endPos > endPage) |
| 215 | { |
| 216 | WPS_DEBUG_MSG(("WPSTextParser::readFDP: error: cch = %d, too large\n", szProp)); |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | int id=0; |
| 221 | std::string mess; |
| 222 | if (parser &&(this->*parser)(endPos, id, mess)) |
| 223 | { |
| 224 | (*fods_iter).m_id = mapPtr[pos] = id; |
| 225 | |
| 226 | f2.str(""); |
| 227 | f2 << entry.type() << id <<":" << mess; |
| 228 | ascii().addPos(pos); |
| 229 | ascii().addNote(f2.str().c_str()); |
| 230 | pos = input->tell(); |
| 231 | } |
| 232 | f << entry.type() << (*fods_iter).m_id << ", "; |
| 233 | if (pos != endPos) |
| 234 | { |
| 235 | f2.str(""); |
| 236 | f2 << entry.type() << "###"; |
| 237 | ascii().addPos(pos); |
| 238 | ascii().addNote(f2.str().c_str()); |
| 239 | } |
| 240 | } |
| 241 | f << "), lstPos=" << std::hex << lastReadPos << std::dec << ", "; |
| 242 | |
| 243 | ascii().addPos(page_offset); |
| 244 | ascii().addNote(f.str().c_str()); |
| 245 | |
| 246 | /* go to end of page */ |
| 247 | input->seek(endPage, librevenge::RVNG_SEEK_SET); |
| 248 | |
| 249 | return m_textPositions.end() > lastReadPos; |
| 250 | } |
| 251 | |
| 252 | std::vector<WPSTextParser::DataFOD> WPSTextParser::mergeSortedFODLists |
| 253 | (std::vector<WPSTextParser::DataFOD> const &lst1, |
| 254 | std::vector<WPSTextParser::DataFOD> const &lst2) const |
| 255 | { |
| 256 | std::vector<WPSTextParser::DataFOD> res; |
| 257 | // we regroup these two lists in one list |
| 258 | size_t num1 = lst1.size(), i1 = 0; |
| 259 | size_t num2 = lst2.size(), i2 = 0; |
| 260 | |
| 261 | while (i1 < num1 || i2 < num2) |
| 262 | { |
| 263 | DataFOD val; |
| 264 | if (i2 == num2) val = lst1[i1++]; |
| 265 | else if (i1 == num1 || lst2[i2].m_pos < lst1[i1].m_pos) |
| 266 | val = lst2[i2++]; |
| 267 | else val = lst1[i1++]; |
| 268 | |
| 269 | if (val.m_pos < m_textPositions.begin() || val.m_pos > m_textPositions.end()) |
| 270 | continue; |
| 271 | |
| 272 | res.push_back(val); |
| 273 | } |
| 274 | return res; |
| 275 | } |
| 276 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |