Debian Codegraph @

src/tools/file/rsrc.cpp

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw: tools
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
18 *
19 *
20 * All Rights Reserved.
21 *
22 * For minor contributions see the git repository.
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
26 * in which case the provisions of the LGPLv2+ are applicable
27 * instead of those above.
28 */
29
30 #include <iostream>
31 #include <sstream>
32 #include <string>
33
34 #include "file_internal.h"
35 #include "input.h"
36 #include "rsrc.h"
37
38 namespace libmwaw_tools
39 {
40 std::string RSRC::Version::makePretty(std::string const &orig)
41 {
42 size_t j;
43 std::string res(orig);
44 for (; (j = res.find(0xd)) != std::string::npos;)
45 res.replace(j, 1, " -- ");
46 return res;
47 }
48
49 std::ostream &operator<< (std::ostream &o, RSRC::Version const &vers)
50 {
51 if (vers.m_id >= 0) o << "[" << vers.m_id << "]";
52 o << ":\t";
53 o << RSRC::Version::makePretty(vers.m_string);
54 if (vers.m_versionString.length())
55 o << "(" << vers.m_versionString << ")";
56 o << ",";
57 o << "version=" << vers.m_majorVersion;
58 if (vers.m_minorVersion)
59 o << "(" << vers.m_minorVersion << ")";
60 o << ",";
61 #ifdef DEBUG
62 if (vers.m_countryCode)
63 o << "country=" << std::hex << vers.m_countryCode << std::dec << ",";
64 o << vers.m_extra;
65 #endif
66 return o;
67 }
68
69 bool RSRC::createMapEntries()
70 {
71 if (!m_typeMapEntryMap.empty()) return true;
72
73 long eof = m_input.length();
74 if (eof < 16) return false;
75 m_input.seek(0, InputStream::SK_SET);
76 m_dataOffset = long(m_input.readU32());
77 auto mapBegin = long(m_input.readU32());
78 auto dataLength = long(m_input.readU32());
79 auto mapLength = long(m_input.readU32());
80 long mapEnd = mapBegin+mapLength;
81 if (mapLength < 28 || dataLength < 0 || m_dataOffset+dataLength < 0 || m_dataOffset+dataLength > eof ||
82 mapBegin+mapLength <0 || mapBegin+mapLength > eof) {
83 MWAW_DEBUG_MSG(("RSRC::createMapEntries: can not read the structure size\n"));
84 return false;
85 }
86
87 m_input.seek(mapBegin+24, InputStream::SK_SET);
88 auto offsetTypes=long(m_input.readU16());
89 auto offsetNameLists=long(m_input.readU16());
90 auto numTypes = static_cast<int>(m_input.readU16());
91 if (offsetTypes+2 > mapLength || offsetNameLists > mapLength) {
92 MWAW_DEBUG_MSG(("RSRC::createMapEntries: the offsets( limits seem bad\n"));
93 return false;
94 }
95 // this case can appear if no data
96 if (numTypes == 0xFFFF)
97 numTypes = -1;
98
99 long pos = mapBegin+offsetTypes+2;
100 m_input.seek(pos, InputStream::SK_SET);
101 if (pos+8*(numTypes+1) > mapEnd) {
102 MWAW_DEBUG_MSG(("RSRC::createMapEntries: the type zones seems too short\n"));
103 return false;
104 }
105 for (int i = 0; i <= numTypes; i++) {
106 MapEntry entry;
107 std::string type("");
108 for (int c = 0; c < 4; c++)
109 type+=char(m_input.readU8());
110 entry.m_type = type;
111 entry.m_numEntry = static_cast<int>(m_input.readU16())+1; // the number of entry
112 entry.m_pos = mapBegin+offsetTypes+long(m_input.readU16());
113
114 if (entry.m_pos < 0 || entry.m_pos+12*entry.m_numEntry > mapEnd) {
115 MWAW_DEBUG_MSG(("RSRC::parseMap: can not read an entry: %s\n", type.c_str()));
116 continue;
117 }
118 m_typeMapEntryMap[type] = entry;
119 }
120
121 return true;
122 }
123
124 std::vector<RSRC::MapEntry> RSRC::getMapEntries(std::string const &type)
125 {
126 std::vector<MapEntry> res;
127 if (!createMapEntries() ||
128 m_typeMapEntryMap.find(type) == m_typeMapEntryMap.end())
129 return res;
130
131 long eof = m_input.length();
132 MapEntry const &entry = m_typeMapEntryMap.find(type)->second;
133 m_input.seek(entry.m_pos, InputStream::SK_SET);
134 for (int n = 0; n < entry.m_numEntry; n++) {
135 long pos = m_input.tell();
136 MapEntry lEntry;
137 lEntry.m_type = type;
138 lEntry.m_id = static_cast<int>(m_input.read16());
139 auto offset = long(m_input.readU16());
140 if (offset != 0xFFFF) {
141 // fixme: possible to read the entry name here ( see MWAWRSRCParser )
142 }
143 unsigned long dOffset = m_input.readU32();
144 if (dOffset & 0xFF000000)
145 dOffset &= 0xFFFFFF;
146 lEntry.m_pos = m_dataOffset+long(dOffset);
147 if (lEntry.m_pos >= 0 && lEntry.m_pos+4 < eof)
148 res.push_back(lEntry);
149 else {
150 MWAW_DEBUG_MSG(("RSRC::getMapEntries: find bad pos %lx\n", static_cast<long unsigned int>(lEntry.m_pos)));
151 }
152 m_input.seek(pos+12, InputStream::SK_SET);
153 }
154 return res;
155 }
156
157 bool RSRC::hasEntry(std::string type, int id)
158 {
159 auto lEntries = RSRC::getMapEntries(type);
160 for (auto const &entry : lEntries) {
161 if (entry.m_id == id) return true;
162 }
163 return false;
164 }
165
166 std::string RSRC::getString(int id)
167 {
168 std::string res("");
169 auto lEntries = RSRC::getMapEntries("STR ");
170 for (auto const &entry : lEntries) {
171 if (entry.m_id != id) continue;
172 m_input.seek(entry.m_pos, InputStream::SK_SET);
173
174 auto sz = long(m_input.readU32());
175 if (entry.m_pos+4+sz > m_input.length()) {
176 MWAW_DEBUG_MSG(("RSRC::getString: entry is invalid\n"));
177 return res;
178 }
179 auto fSz = static_cast<int>(m_input.readU8());
180 if (fSz+1 > sz) {
181 MWAW_DEBUG_MSG(("RSRC::getString: pascal size seems bad\n"));
182 return res;
183 }
184 for (int i = 0; i < fSz; i++)
185 res += char(m_input.readU8());
186 break;
187 }
188
189 return res;
190 }
191
192
193 std::vector<RSRC::Version> RSRC::getVersionList()
194 {
195 std::vector<Version> res;
196 auto lEntries = RSRC::getMapEntries("vers");
197 for (auto const &entry : lEntries) {
198 Version vers;
199 vers.m_id = entry.m_id;
200 vers.m_begin = entry.m_pos;
201 parseVers(vers);
202 res.push_back(vers);
203 }
204
205 return res;
206 }
207
208 bool RSRC::parseVers(RSRC::Version &vers)
209 {
210 m_input.seek(vers.m_begin, InputStream::SK_SET);
211 auto sz = long(m_input.readU32());
212 if (vers.m_begin+4+sz > m_input.length() || vers.m_begin <= 0) {
213 MWAW_DEBUG_MSG(("RSRC::parseVers: entry is invalid\n"));
214 return false;
215 }
216 long end = vers.m_begin+4+sz;
217
218 vers.m_majorVersion = static_cast<int>(m_input.readU8());
219 vers.m_minorVersion = static_cast<int>(m_input.readU8());
220 std::stringstream f;
221 auto val = long(m_input.readU8());
222 if (val) f << "devStage=" << val << ",";
223 val = long(m_input.readU8());
224 if (val) f << "preReleaseLevel=" << std::hex << val << std::dec << ",";
225 vers.m_countryCode = static_cast<int>(m_input.readU16());
226 for (int i = 0; i < 2; i++) {
227 sz = long(m_input.readU8());
228 long pos = m_input.tell();
229 if (pos+sz > end) {
230 MWAW_DEBUG_MSG(("RSRC::parseVers: can not read strings %d\n", i));
231 return false;
232 }
233 std::string str("");
234 for (long c = 0; c < sz; c++)
235 str+=char(m_input.readU8());
236 if (i==0)
237 vers.m_versionString = str;
238 else
239 vers.m_string = str;
240 }
241 vers.m_extra = f.str();
242 return true;
243 }
244
245 }
246
247 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: