Debian Codegraph @

src/fuzz/commonfuzzer.cpp

raw symbols SCIP
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 {
30 const long pos = input.tell();
31 if (0 > pos)
32 return 0;
33
34 long end = 0;
35
36 if (0 == input.seek(0, librevenge::RVNG_SEEK_END))
37 end = input.tell();
38 if ((0 != input.seek(pos, librevenge::RVNG_SEEK_SET)) || (0 > end))
39 return 0;
40
41 return static_cast<unsigned long>(end);
42 }
43
44 void testRead(librevenge::RVNGInputStream &input, std::vector<unsigned char> &buf)
45 {
46 const unsigned long size = getLength(input);
47 unsigned long readBytes = 0;
48 const unsigned char *data = input.read(size, readBytes);
49 buf.resize(size_t(size));
50 std::copy_n(data, size, std::back_inserter(buf));
51 }
52
53 }
54
55 void testStructuredStream(librevenge::RVNGInputStream &input)
56 {
57 std::vector<unsigned char> buf;
58
59 for (unsigned i = 0, ssc = input.subStreamCount(); i < ssc; ++i)
60 {
61 std::unique_ptr<librevenge::RVNGInputStream> subStream(input.getSubStreamById(i));
62 if (bool(subStream))
63 testRead(*subStream, buf);
64
65 const char *const name = input.subStreamName(i);
66 std::unique_ptr<librevenge::RVNGInputStream> namedSubStream(input.getSubStreamByName(name));
67 if (bool(namedSubStream))
68 testRead(*namedSubStream, buf);
69 }
70 }
71
72 }
73
74 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */