/src/librevenge/src/fuzz/commonfuzzer.cpp
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
2 | | /* librevenge |
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 | | * Alternatively, the contents of this file may be used under the terms |
10 | | * of the GNU Lesser General Public License Version 2.1 or later |
11 | | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
12 | | * applicable instead of those above. |
13 | | */ |
14 | | |
15 | | #include "commonfuzzer.h" |
16 | | |
17 | | #include <algorithm> |
18 | | #include <iterator> |
19 | | #include <memory> |
20 | | #include <vector> |
21 | | |
22 | | namespace fuzz |
23 | | { |
24 | | |
25 | | namespace |
26 | | { |
27 | | |
28 | | unsigned long getLength(librevenge::RVNGInputStream &input) |
29 | 83.4k | { |
30 | 83.4k | const long pos = input.tell(); |
31 | 83.4k | if (0 > pos) |
32 | 0 | return 0; |
33 | | |
34 | 83.4k | long end = 0; |
35 | | |
36 | 83.4k | if (0 == input.seek(0, librevenge::RVNG_SEEK_END)) |
37 | 83.4k | end = input.tell(); |
38 | 83.4k | if ((0 != input.seek(pos, librevenge::RVNG_SEEK_SET)) || (0 > end)) |
39 | 0 | return 0; |
40 | | |
41 | 83.4k | return static_cast<unsigned long>(end); |
42 | 83.4k | } |
43 | | |
44 | | void testRead(librevenge::RVNGInputStream &input, std::vector<unsigned char> &buf) |
45 | 83.4k | { |
46 | 83.4k | const unsigned long size = getLength(input); |
47 | 83.4k | unsigned long readBytes = 0; |
48 | 83.4k | const unsigned char *data = input.read(size, readBytes); |
49 | 83.4k | buf.resize(size_t(size)); |
50 | 83.4k | std::copy_n(data, size, std::back_inserter(buf)); |
51 | 83.4k | } |
52 | | |
53 | | } |
54 | | |
55 | | void testStructuredStream(librevenge::RVNGInputStream &input) |
56 | 4.17k | { |
57 | 4.17k | std::vector<unsigned char> buf; |
58 | | |
59 | 182k | for (unsigned i = 0, ssc = input.subStreamCount(); i < ssc; ++i) |
60 | 178k | { |
61 | 178k | std::unique_ptr<librevenge::RVNGInputStream> subStream(input.getSubStreamById(i)); |
62 | 178k | if (bool(subStream)) |
63 | 41.7k | testRead(*subStream, buf); |
64 | | |
65 | 178k | const char *const name = input.subStreamName(i); |
66 | 178k | std::unique_ptr<librevenge::RVNGInputStream> namedSubStream(input.getSubStreamByName(name)); |
67 | 178k | if (bool(namedSubStream)) |
68 | 41.7k | testRead(*namedSubStream, buf); |
69 | 178k | } |
70 | 4.17k | } |
71 | | |
72 | | } |
73 | | |
74 | | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |