KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pads_binary_utils.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <cstring>
27
28namespace PADS_IO
29{
30
31std::optional<SDB_FOOTER_ERROR> CheckSdbFooter( const std::vector<uint8_t>& aData, size_t aFooterStart,
32 const char* aGuid, size_t aGuidLen )
33{
34 // Subtract rather than add so a near-SIZE_MAX offset cannot wrap past the check
35 if( aData.size() < 4 || aGuidLen > aData.size() - 4
36 || aFooterStart > aData.size() - aGuidLen - 4 )
37 {
38 return SDB_FOOTER_ERROR{ aData.size(), "PADS binary file too small for the footer" };
39 }
40
41 if( std::memcmp( &aData[aFooterStart], aGuid, aGuidLen ) != 0 )
42 return SDB_FOOTER_ERROR{ aFooterStart, "Invalid PADS binary footer GUID" };
43
44 size_t backPointerOffset = aFooterStart + aGuidLen;
45 uint32_t containerItemsOffset = getU32LE( aData, backPointerOffset );
46
47 if( containerItemsOffset > aFooterStart || aFooterStart - containerItemsOffset < 4 )
48 return SDB_FOOTER_ERROR{ backPointerOffset, "Invalid PADS binary container-item back-pointer" };
49
50 return std::nullopt;
51}
52
53
54void ValidateSdbFooter( const std::vector<uint8_t>& aData, size_t aFooterStart, const char* aGuid,
55 size_t aGuidLen )
56{
57 if( std::optional<SDB_FOOTER_ERROR> error = CheckSdbFooter( aData, aFooterStart, aGuid, aGuidLen ) )
58 THROW_IO_ERROR( error->detail );
59}
60
61} // namespace PADS_IO
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
std::optional< SDB_FOOTER_ERROR > CheckSdbFooter(const std::vector< uint8_t > &aData, size_t aFooterStart, const char *aGuid, size_t aGuidLen)
Check a PADS SDB footer at aFooterStart: the ASCII GUID followed by an absolute back-pointer to the s...
uint32_t getU32LE(const std::vector< uint8_t > &aData, size_t aOffset)
void ValidateSdbFooter(const std::vector< uint8_t > &aData, size_t aFooterStart, const char *aGuid, size_t aGuidLen)
As CheckSdbFooter, throwing a bare IO_ERROR on failure.