Line data Source code
1 : #include "ToolsFile.h"
2 : #include <fstream>
3 : #include <sstream>
4 : #include <charconv>
5 : #include <stdexcept>
6 : #include <cerrno>
7 : #include <cmath>
8 : #include <cstdlib>
9 : #include "../parse_error.h"
10 : #include "../invalid_odb_error.h"
11 : #include "Logger.h"
12 : #include "str_utils.h"
13 : #include "../../Constants.h"
14 : #include <ArchiveExtractor.h>
15 : #include "../../Text/Utf8Sanitizer.h"
16 :
17 : namespace
18 : {
19 : // shortest round-trippable decimal form (avoids both precision loss and
20 : // fixed/scientific surprises in Save output)
21 12 : std::string format_double(double value)
22 : {
23 : // non-finite values have no ODB++ representation; emitting inf/nan
24 : // would produce a file our own parser (isfinite check) rejects
25 12 : if (!std::isfinite(value))
26 : {
27 1 : throw std::domain_error("non-finite numeric value cannot be serialized to the tools file grammar");
28 : }
29 : char buffer[64];
30 11 : const auto result = std::to_chars(buffer, buffer + sizeof(buffer), value);
31 11 : if (result.ec != std::errc())
32 : {
33 0 : return "0";
34 : }
35 22 : return std::string(buffer, result.ptr);
36 : }
37 :
38 : // numeric fields parse through validated helpers: a malformed or
39 : // hostile tools file surfaces as parse_error with file/line context.
40 : // from_chars (not stoi/stoul/stod) so that validation is total rather
41 : // than exception-only: trailing garbage, sign abuse on unsigned fields,
42 : // and out-of-target-range values are all rejected — sto* would accept
43 : // "5abc", wrap "-5", or truncate >UINT_MAX on LP64
44 : using Odb::Lib::FileModel::parse_error;
45 :
46 1393 : unsigned int parseUnsigned(const std::filesystem::path& path, const std::string& line,
47 : int lineNumber, const std::string& token, const std::string& value)
48 : {
49 1393 : unsigned int result = 0;
50 1393 : const auto* last = value.data() + value.size();
51 1393 : const auto [ptr, ec] = std::from_chars(value.data(), last, result);
52 1393 : if (ec != std::errc() || ptr != last)
53 : {
54 4 : throw_parse_error(path, line, token, lineNumber);
55 : }
56 1389 : return result;
57 : }
58 :
59 5955 : double parseDouble(const std::filesystem::path& path, const std::string& line,
60 : int lineNumber, const std::string& token, const std::string& value)
61 : {
62 : // std::from_chars for floating point is unavailable on libc++
63 : // (Xcode), so the double path uses strtod. Strictness matches
64 : // from_chars for the grammar we care about: full consumption,
65 : // range, finite result. Known deviation: C99 strtod also accepts
66 : // hex-float literals ("0x10") which from_chars would reject; a
67 : // hex value is still a valid double and re-serializes as decimal,
68 : // so this is a documented difference, not a defect
69 5955 : if (value.empty())
70 : {
71 0 : throw_parse_error(path, line, token, lineNumber);
72 : }
73 5955 : errno = 0;
74 5955 : char* end = nullptr;
75 5955 : const double result = std::strtod(value.c_str(), &end);
76 5955 : if (end != value.c_str() + value.size() || errno == ERANGE || !std::isfinite(result))
77 : {
78 4 : throw_parse_error(path, line, token, lineNumber);
79 : }
80 5951 : return result;
81 : }
82 : }
83 :
84 : namespace Odb::Lib::FileModel::Design
85 : {
86 3248 : ToolsFile::ToolsFile()
87 3248 : : m_path("")
88 3248 : , m_directory("")
89 12992 : , m_units("")
90 : {
91 3248 : }
92 :
93 1416 : bool ToolsFile::ToolRecord::subTypeIsLegal() const
94 : {
95 1416 : switch (type)
96 : {
97 746 : case Type::Plated:
98 746 : return subType == SubType::Standard || subType == SubType::PressFit;
99 363 : case Type::NonPlated:
100 363 : return subType == SubType::Standard;
101 307 : case Type::Via:
102 307 : return subType == SubType::Standard || subType == SubType::Photo || subType == SubType::Laser;
103 0 : default:
104 0 : return false;
105 : }
106 : }
107 :
108 2 : const char* ToolsFile::ToolRecord::typeName(Type type)
109 : {
110 2 : switch (type)
111 : {
112 0 : case Type::Plated: return TYPE_PLATED;
113 0 : case Type::NonPlated: return TYPE_NON_PLATED;
114 2 : case Type::Via: return TYPE_VIA;
115 0 : default: return "UNKNOWN";
116 : }
117 : }
118 :
119 2 : const char* ToolsFile::ToolRecord::subTypeName(SubType subType)
120 : {
121 2 : switch (subType)
122 : {
123 2 : case SubType::Standard: return SUB_TYPE_STANDARD;
124 0 : case SubType::PressFit: return SUB_TYPE_PRESS_FIT;
125 0 : case SubType::Photo: return SUB_TYPE_PHOTO;
126 0 : case SubType::Laser: return SUB_TYPE_LASER;
127 0 : default: return "UNKNOWN";
128 : }
129 : }
130 :
131 2 : std::string ToolsFile::GetUnits() const
132 : {
133 2 : return m_units;
134 : }
135 :
136 1 : double ToolsFile::GetThickness() const
137 : {
138 1 : return m_thickness;
139 : }
140 :
141 2 : const std::vector<std::string>& ToolsFile::GetUserParams() const
142 : {
143 2 : return m_userParams;
144 : }
145 :
146 2482 : const ToolsFile::ToolRecord::Vector& ToolsFile::GetTools() const
147 : {
148 2482 : return m_tools;
149 : }
150 :
151 0 : std::filesystem::path ToolsFile::GetPath() const
152 : {
153 0 : return m_path;
154 : }
155 :
156 3075 : bool ToolsFile::Parse(std::filesystem::path directory)
157 : {
158 3075 : std::ifstream toolsFile;
159 3075 : int lineNumber = 0;
160 3075 : std::string line;
161 :
162 : try
163 : {
164 3075 : m_directory = directory;
165 :
166 15375 : loginfo("checking for extraction...");
167 :
168 3075 : auto toolsFilePath = Utils::ArchiveExtractor::getUncompressedFilePath(m_directory, TOOLS_FILENAME);
169 3075 : if (std::filesystem::exists(toolsFilePath) && std::filesystem::is_regular_file(toolsFilePath))
170 : {
171 1266 : loginfo("found tools file: [" + toolsFilePath.string() + "]");
172 : }
173 :
174 3075 : m_path = toolsFilePath;
175 :
176 3075 : if (!std::filesystem::exists(m_path))
177 : {
178 : // not every layer carries a drill tools table (only drill/rout
179 : // layers do); absence is not an error
180 5306 : logwarn("tools file does not exist: [" + m_directory.string() + "]");
181 2653 : return true;
182 : }
183 422 : else if (!std::filesystem::is_regular_file(m_path))
184 : {
185 0 : auto message = "tools is not a file: [" + m_path.string() + "]";
186 0 : throw invalid_odb_error(message.c_str());
187 0 : }
188 :
189 422 : toolsFile.open(m_path.string(), std::ios::in);
190 422 : if (!toolsFile.is_open())
191 : {
192 0 : auto message = "unable to open tools file: [" + m_path.string() + "]";
193 0 : throw invalid_odb_error(message.c_str());
194 0 : }
195 :
196 422 : auto parsingRecord = false;
197 422 : std::shared_ptr<ToolRecord> pCurrentRecord;
198 :
199 16918 : while (std::getline(toolsFile, line))
200 : {
201 16509 : lineNumber++;
202 :
203 16509 : Utils::str_trim(line);
204 19293 : if (line.empty()) continue;
205 14731 : if (line.find(Constants::COMMENT_TOKEN) == 0) continue;
206 :
207 14730 : std::stringstream lineStream(line);
208 :
209 14730 : if (!parsingRecord)
210 : {
211 : // a TOOLS array record opens with "TOOLS {" — no '=' on the
212 : // line, so check the array token before key/value parsing.
213 : // Require the brace (optionally spaced) so a TOOLS-prefixed
214 : // parameter line is never mistaken for a record open.
215 2636 : if (line.rfind(TOOLS_ARRAY_TOKEN, 0) == 0)
216 : {
217 : // char_traits::length over strlen: constexpr and provably bounded
218 : // for this class constant (Flawfinder CWE-126 flags strlen on
219 : // non-literal pointers as a potential over-read)
220 1397 : auto afterToken = line.substr(std::char_traits<char>::length(TOOLS_ARRAY_TOKEN));
221 1397 : Utils::str_trim(afterToken);
222 1397 : if (afterToken != Constants::ARRAY_RECORD_OPEN_TOKEN)
223 : {
224 3 : throw_parse_error(m_path, line, TOOLS_ARRAY_TOKEN, lineNumber);
225 : }
226 1396 : pCurrentRecord = std::make_shared<ToolRecord>();
227 1396 : parsingRecord = true;
228 1396 : continue;
229 1397 : }
230 :
231 : // global parameter
232 1239 : std::string key;
233 1239 : if (!std::getline(lineStream, key, '='))
234 : {
235 0 : throw_parse_error(m_path, line, "", lineNumber);
236 : }
237 :
238 1239 : Utils::str_trim(key);
239 :
240 1239 : if (key == Constants::UNITS_TOKEN)
241 : {
242 422 : std::string value;
243 422 : std::getline(lineStream, value);
244 422 : Utils::str_trim(value);
245 422 : m_units = value;
246 422 : }
247 817 : else if (key == THICKNESS_KEY)
248 : {
249 : // obsolete per spec; tolerated and preserved for round-tripping
250 409 : std::string value;
251 409 : std::getline(lineStream, value);
252 409 : Utils::str_trim(value);
253 409 : m_thickness = value.empty() ? 0.0 : parseDouble(m_path, line, lineNumber, key, value);
254 409 : }
255 408 : else if (key == USER_PARAMS_KEY)
256 : {
257 407 : std::string value;
258 407 : std::getline(lineStream, value);
259 407 : Utils::str_trim(value);
260 407 : if (!value.empty())
261 : {
262 1 : m_userParams.push_back(value);
263 : }
264 407 : }
265 : else
266 : {
267 1 : throw_parse_error(m_path, line, key, lineNumber);
268 : }
269 1239 : }
270 : else
271 : {
272 : // inside a TOOLS record
273 12094 : if (line == Constants::ARRAY_RECORD_CLOSE_TOKEN)
274 : {
275 1388 : if (pCurrentRecord->type == ToolRecord::Type::Unknown)
276 : {
277 0 : throw_parse_error(m_path, line, TYPE_KEY, lineNumber);
278 : }
279 1388 : else if (!pCurrentRecord->hasNum)
280 : {
281 : // spec requires a tool number; without this a
282 : // NUM-less record silently commits with num=0
283 0 : throw_parse_error(m_path, line, NUM_KEY, lineNumber);
284 : }
285 1388 : else if (!pCurrentRecord->subTypeIsLegal())
286 : {
287 3 : throw_parse_error(m_path, line, TYPE2_KEY, lineNumber);
288 : }
289 :
290 1387 : m_tools.push_back(pCurrentRecord);
291 1387 : pCurrentRecord = nullptr;
292 1387 : parsingRecord = false;
293 1387 : continue;
294 : }
295 :
296 10706 : std::string key;
297 10706 : if (!std::getline(lineStream, key, '='))
298 : {
299 0 : throw_parse_error(m_path, line, "", lineNumber);
300 : }
301 :
302 10706 : Utils::str_trim(key);
303 :
304 10706 : if (line.find('=') == std::string::npos)
305 : {
306 : // no '=' on the line at all — not a key/value field
307 : // (trailing '=' with empty value, e.g. BIT=, is legal)
308 1 : throw_parse_error(m_path, line, key, lineNumber);
309 : }
310 10705 : std::string value;
311 10705 : std::getline(lineStream, value);
312 10705 : Utils::str_trim(value);
313 :
314 10705 : if (key == NUM_KEY)
315 : {
316 1393 : pCurrentRecord->num = parseUnsigned(m_path, line, lineNumber, key, value);
317 1389 : pCurrentRecord->hasNum = true;
318 : }
319 9312 : else if (key == TYPE_KEY)
320 : {
321 1390 : if (value == ToolRecord::TYPE_PLATED) pCurrentRecord->type = ToolRecord::Type::Plated;
322 657 : else if (value == ToolRecord::TYPE_NON_PLATED) pCurrentRecord->type = ToolRecord::Type::NonPlated;
323 302 : else if (value == ToolRecord::TYPE_VIA) pCurrentRecord->type = ToolRecord::Type::Via;
324 1 : else throw_parse_error(m_path, line, value, lineNumber);
325 : }
326 7922 : else if (key == TYPE2_KEY)
327 : {
328 989 : if (value == ToolRecord::SUB_TYPE_STANDARD) pCurrentRecord->subType = ToolRecord::SubType::Standard;
329 2 : else if (value == ToolRecord::SUB_TYPE_PRESS_FIT) pCurrentRecord->subType = ToolRecord::SubType::PressFit;
330 2 : else if (value == ToolRecord::SUB_TYPE_PHOTO) pCurrentRecord->subType = ToolRecord::SubType::Photo;
331 1 : else if (value == ToolRecord::SUB_TYPE_LASER) pCurrentRecord->subType = ToolRecord::SubType::Laser;
332 0 : else throw_parse_error(m_path, line, value, lineNumber);
333 : }
334 6933 : else if (key == MIN_TOL_KEY)
335 : {
336 1386 : pCurrentRecord->minTol = value.empty() ? 0.0 : parseDouble(m_path, line, lineNumber, key, value);
337 : }
338 5547 : else if (key == MAX_TOL_KEY)
339 : {
340 1386 : pCurrentRecord->maxTol = value.empty() ? 0.0 : parseDouble(m_path, line, lineNumber, key, value);
341 : }
342 4161 : else if (key == BIT_KEY)
343 : {
344 1386 : pCurrentRecord->bit = value;
345 : }
346 2775 : else if (key == FINISH_SIZE_KEY)
347 : {
348 : // spec: -1 when not set
349 1389 : pCurrentRecord->finishSize = value.empty() ? -1.0 : parseDouble(m_path, line, lineNumber, key, value);
350 : }
351 1386 : else if (key == DRILL_SIZE_KEY)
352 : {
353 1386 : pCurrentRecord->drillSize = value.empty() ? -1.0 : parseDouble(m_path, line, lineNumber, key, value);
354 : }
355 : else
356 : {
357 0 : throw_parse_error(m_path, line, key, lineNumber);
358 : }
359 10713 : }
360 14730 : }
361 :
362 409 : if (parsingRecord)
363 : {
364 0 : throw_parse_error(m_path, "EOF", Constants::ARRAY_RECORD_CLOSE_TOKEN, lineNumber);
365 : }
366 :
367 409 : toolsFile.close();
368 3088 : }
369 13 : catch (parse_error& pe)
370 : {
371 13 : auto m = pe.toString("Parse Error:");
372 26 : logerror(m);
373 13 : toolsFile.close();
374 13 : throw;
375 26 : }
376 0 : catch (std::exception& e)
377 : {
378 0 : parse_info pi(m_path, line, lineNumber);
379 0 : const auto m = pi.toString();
380 0 : logexception_msg(e, m);
381 0 : toolsFile.close();
382 : // bare rethrow preserves the original exception type; rethrowing
383 : // the caught variable would slice invalid_odb_error/parse_error to
384 : // std::exception
385 0 : throw;
386 0 : }
387 :
388 409 : return true;
389 3088 : }
390 :
391 5 : bool ToolsFile::Save(std::ostream& os)
392 : {
393 5 : os << Constants::UNITS_TOKEN << "=" << m_units << std::endl;
394 5 : os << THICKNESS_KEY << "=" << format_double(m_thickness) << std::endl;
395 5 : for (const auto& userParam : m_userParams)
396 : {
397 0 : os << USER_PARAMS_KEY << "=" << userParam << std::endl;
398 : }
399 :
400 6 : for (const auto& pTool : m_tools)
401 : {
402 5 : if (pTool->type == ToolRecord::Type::Unknown)
403 : {
404 : // TYPE=UNKNOWN is not part of the file grammar; a record with
405 : // no type cannot round-trip and must fail loudly, not corrupt
406 10 : throw_parse_error(m_path, "", TYPE_KEY, 0);
407 : }
408 3 : if (pTool->subType == ToolRecord::SubType::Unknown)
409 : {
410 5 : throw_parse_error(m_path, "", TYPE2_KEY, 0);
411 : }
412 2 : os << TOOLS_ARRAY_TOKEN << " " << Constants::ARRAY_RECORD_OPEN_TOKEN << std::endl;
413 2 : os << '\t' << NUM_KEY << "=" << pTool->num << std::endl;
414 2 : os << '\t' << TYPE_KEY << "=" << ToolRecord::typeName(pTool->type) << std::endl;
415 2 : os << '\t' << TYPE2_KEY << "=" << ToolRecord::subTypeName(pTool->subType) << std::endl;
416 2 : os << '\t' << MIN_TOL_KEY << "=" << format_double(pTool->minTol) << std::endl;
417 2 : os << '\t' << MAX_TOL_KEY << "=" << format_double(pTool->maxTol) << std::endl;
418 2 : os << '\t' << BIT_KEY << "=" << pTool->bit << std::endl;
419 2 : os << '\t' << FINISH_SIZE_KEY << "=" << format_double(pTool->finishSize) << std::endl;
420 1 : os << '\t' << DRILL_SIZE_KEY << "=" << format_double(pTool->drillSize) << std::endl;
421 1 : os << Constants::ARRAY_RECORD_CLOSE_TOKEN << std::endl;
422 : }
423 :
424 1 : return true;
425 : }
426 :
427 2682 : std::unique_ptr<Odb::Lib::Protobuf::ToolsFile> ToolsFile::to_protobuf() const
428 : {
429 2682 : auto message = std::make_unique<Odb::Lib::Protobuf::ToolsFile>();
430 :
431 5364 : message->set_directory(Odb::Lib::Text::ToUtf8(m_directory.string()));
432 5364 : message->set_path(Odb::Lib::Text::ToUtf8(m_path.string()));
433 5364 : message->set_units(Odb::Lib::Text::ToUtf8(m_units));
434 2682 : message->set_thickness(m_thickness);
435 2682 : for (const auto& userParam : m_userParams)
436 : {
437 0 : message->add_userparams(Odb::Lib::Text::ToUtf8(userParam));
438 : }
439 4123 : for (const auto& pTool : m_tools)
440 : {
441 1441 : message->add_tools()->CopyFrom(*pTool->to_protobuf());
442 : }
443 :
444 2682 : return message;
445 0 : }
446 :
447 173 : void ToolsFile::from_protobuf(const Odb::Lib::Protobuf::ToolsFile& message)
448 : {
449 173 : m_directory = message.directory();
450 173 : m_path = message.path();
451 173 : m_units = message.units();
452 173 : m_thickness = message.thickness();
453 173 : for (const auto& userParam : message.userparams())
454 : {
455 0 : m_userParams.push_back(userParam);
456 : }
457 252 : for (const auto& tool : message.tools())
458 : {
459 79 : auto pTool = std::make_shared<ToolRecord>();
460 79 : pTool->from_protobuf(tool);
461 79 : m_tools.push_back(pTool);
462 79 : }
463 173 : }
464 :
465 1441 : std::unique_ptr<Odb::Lib::Protobuf::ToolsFile::Tool> ToolsFile::ToolRecord::to_protobuf() const
466 : {
467 1441 : auto message = std::make_unique<Odb::Lib::Protobuf::ToolsFile::Tool>();
468 :
469 1441 : message->set_num(num);
470 1441 : message->set_type(static_cast<Odb::Lib::Protobuf::ToolsFile::ToolType>(type));
471 1441 : message->set_subtype(static_cast<Odb::Lib::Protobuf::ToolsFile::ToolSubType>(subType));
472 1441 : message->set_mintol(minTol);
473 1441 : message->set_maxtol(maxTol);
474 2882 : message->set_bit(Odb::Lib::Text::ToUtf8(bit));
475 1441 : message->set_finishsize(finishSize);
476 1441 : message->set_drillsize(drillSize);
477 :
478 1441 : return message;
479 0 : }
480 :
481 80 : void ToolsFile::ToolRecord::from_protobuf(const Odb::Lib::Protobuf::ToolsFile::Tool& message)
482 : {
483 80 : num = message.num();
484 : // keep the NUM-present invariant consistent with the parse path
485 : // (hasNum gates the close-brace NUM-required check); proto presence
486 : // is the wire equivalent of "NUM= appeared in the file"
487 80 : hasNum = message.has_num();
488 : // proto3 delivers out-of-range enum values from hostile/foreign wire
489 : // messages verbatim; coerce them to Unknown so Save()'s existing
490 : // Unknown refusal catches them instead of writing TYPE2=<integer
491 : // garbage> that cannot re-parse
492 80 : const auto typeValue = message.type();
493 80 : type = Protobuf::ToolsFile::ToolType_IsValid(typeValue)
494 80 : ? static_cast<Type>(typeValue)
495 : : Type::Unknown;
496 : // absent sub-type deserializes to the parse-time default (STANDARD),
497 : // not Unknown — matches a record parsed from a file without TYPE2
498 80 : if (!message.has_subtype())
499 : {
500 5 : subType = SubType::Standard;
501 : }
502 : else
503 : {
504 75 : const auto subTypeValue = message.subtype();
505 150 : subType = Protobuf::ToolsFile::ToolSubType_IsValid(subTypeValue)
506 75 : ? static_cast<SubType>(subTypeValue)
507 : : SubType::Unknown;
508 : }
509 80 : minTol = message.mintol();
510 80 : maxTol = message.maxtol();
511 80 : bit = message.bit();
512 : // absent sizes keep the spec's -1 "unset" sentinel rather than 0.0
513 80 : finishSize = message.has_finishsize() ? message.finishsize() : -1.0;
514 80 : drillSize = message.has_drillsize() ? message.drillsize() : -1.0;
515 80 : }
516 : }
|