LibreOffice Module comphelper (master)  1
xmltools.cxx
Go to the documentation of this file.
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 <rtl/uuid.h>
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  {
63  static_assert(sizeof(aChaffEncoder) == 256, "this has to cover all chars");
64 
65  for (auto & elem : rChaff)
66  {
67  elem = aChaffEncoder[elem];
68  }
69  }
70 }
71 
72 namespace comphelper::xml
73 {
74  OString makeXMLChaff()
75  {
76  rtlRandomPool pool = rtl_random_createPool();
77 
78  sal_Int8 n;
79  rtl_random_getBytes(pool, &n, 1);
80 
81  sal_Int32 nLength = 1024+n;
82  // coverity[tainted_data] - 1024 deliberate random minus max -127/plus max 128
83  std::vector<sal_uInt8> aChaff(nLength);
84  rtl_random_getBytes(pool, aChaff.data(), nLength);
85 
86  rtl_random_destroyPool(pool);
87 
88  encodeChaff(aChaff);
89 
90  return OString(reinterpret_cast<const char*>(aChaff.data()), nLength);
91  }
92 
94  {
95  sal_uInt8 aSeq[16];
96  rtl_createUuid(aSeq, nullptr, true);
97 
98  char str[39];
99  sprintf(str, "{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
100  aSeq[0], aSeq[1], aSeq[2], aSeq[3], aSeq[4], aSeq[5], aSeq[6], aSeq[7], aSeq[8],
101  aSeq[9], aSeq[10], aSeq[11], aSeq[12], aSeq[13], aSeq[14], aSeq[15]);
102 
103  return str;
104  }
105 }
106 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
signed char sal_Int8
sal_Int64 n
OString makeXMLChaff()
Definition: xmltools.cxx:74
OString generateGUIDString()
Definition: xmltools.cxx:93
void * rtlRandomPool
unsigned char sal_uInt8
Sequence< sal_Int8 > aSeq
sal_Int32 nLength