/src/libreoffice/comphelper/source/xml/xmltools.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
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 | | |
10 | | #include <comphelper/xmltools.hxx> |
11 | | #include <rtl/random.h> |
12 | | #include <tools/Guid.hxx> |
13 | | #include <vector> |
14 | | |
15 | | using namespace com::sun::star; |
16 | | |
17 | | namespace |
18 | | { |
19 | | //Will be inside an xml comment, so can't use '-' in case '--' appears in |
20 | | //output, etc. Despite what *is* legal in an xml comment, just using the |
21 | | //base-64 subset to avoid pain with simplistic third-party parsers |
22 | | const sal_uInt8 aChaffEncoder[] = |
23 | | { |
24 | | 'A', 'Q', 'g', 'w', 'B', 'R', 'h', 'x', |
25 | | 'C', 'S', 'i', 'y', 'D', 'T', 'j', 'z', |
26 | | 'E', 'U', 'k', '0', 'F', 'V', 'l', '1', |
27 | | 'G', 'W', 'm', '2', 'H', 'X', 'n', '3', |
28 | | 'I', 'Y', 'o', '4', 'J', 'Z', 'p', '5', |
29 | | 'K', 'a', 'q', '6', 'L', 'b', 'r', '7', |
30 | | 'M', 'c', 's', '8', 'N', 'd', 't', '9', |
31 | | 'O', 'e', 'u', '+', 'P', 'f', 'v', '/', |
32 | | |
33 | | 'A', 'Q', 'g', 'w', 'B', 'R', 'h', 'x', |
34 | | 'C', 'S', 'i', 'y', 'D', 'T', 'j', 'z', |
35 | | 'E', 'U', 'k', '0', 'F', 'V', 'l', '1', |
36 | | 'G', 'W', 'm', '2', 'H', 'X', 'n', '3', |
37 | | 'I', 'Y', 'o', '4', 'J', 'Z', 'p', '5', |
38 | | 'K', 'a', 'q', '6', 'L', 'b', 'r', '7', |
39 | | 'M', 'c', 's', '8', 'N', 'd', 't', '9', |
40 | | 'O', 'e', 'u', '+', 'P', 'f', 'v', '/', |
41 | | |
42 | | 'A', 'Q', 'g', 'w', 'B', 'R', 'h', 'x', |
43 | | 'C', 'S', 'i', 'y', 'D', 'T', 'j', 'z', |
44 | | 'E', 'U', 'k', '0', 'F', 'V', 'l', '1', |
45 | | 'G', 'W', 'm', '2', 'H', 'X', 'n', '3', |
46 | | 'I', 'Y', 'o', '4', 'J', 'Z', 'p', '5', |
47 | | 'K', 'a', 'q', '6', 'L', 'b', 'r', '7', |
48 | | 'M', 'c', 's', '8', 'N', 'd', 't', '9', |
49 | | 'O', 'e', 'u', '+', 'P', 'f', 'v', '/', |
50 | | |
51 | | 'A', 'Q', 'g', 'w', 'B', 'R', 'h', 'x', |
52 | | 'C', 'S', 'i', 'y', 'D', 'T', 'j', 'z', |
53 | | 'E', 'U', 'k', '0', 'F', 'V', 'l', '1', |
54 | | 'G', 'W', 'm', '2', 'H', 'X', 'n', '3', |
55 | | 'I', 'Y', 'o', '4', 'J', 'Z', 'p', '5', |
56 | | 'K', 'a', 'q', '6', 'L', 'b', 'r', '7', |
57 | | 'M', 'c', 's', '8', 'N', 'd', 't', '9', |
58 | | 'O', 'e', 'u', '+', 'P', 'f', 'v', '/' |
59 | | }; |
60 | | |
61 | | void encodeChaff(std::vector<sal_uInt8> &rChaff) |
62 | 0 | { |
63 | 0 | static_assert(sizeof(aChaffEncoder) == 256, "this has to cover all chars"); |
64 | |
|
65 | 0 | for (auto & elem : rChaff) |
66 | 0 | { |
67 | 0 | elem = aChaffEncoder[elem]; |
68 | 0 | } |
69 | 0 | } |
70 | | } |
71 | | |
72 | | namespace comphelper::xml |
73 | | { |
74 | | // Generate some 'chaff' of varying length to be the body of an |
75 | | // XML comment to put at the start of encrypted content to make |
76 | | // document content a little less predictable. |
77 | | // See SvXMLExport::addChaffWhenEncryptedStorage |
78 | | OString makeXMLChaff() |
79 | 0 | { |
80 | 0 | sal_Int8 n; |
81 | 0 | (void)rtl_random_getBytes(nullptr, &n, 1); |
82 | |
|
83 | 0 | sal_Int32 nLength = 1024+n; |
84 | | // coverity[tainted_data] - 1024 deliberate random minus max -127/plus max 128 |
85 | 0 | std::vector<sal_uInt8> aChaff(nLength); |
86 | 0 | (void)rtl_random_getBytes(nullptr, aChaff.data(), nLength); |
87 | |
|
88 | 0 | encodeChaff(aChaff); |
89 | |
|
90 | 0 | return OString(reinterpret_cast<const char*>(aChaff.data()), nLength); |
91 | 0 | } |
92 | | |
93 | | OString generateGUIDString() |
94 | 120 | { |
95 | 120 | tools::Guid aGuid(tools::Guid::Generate); |
96 | 120 | return aGuid.getString(); |
97 | 120 | } |
98 | | } |
99 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |