Debian Codegraph @

xmlsecurity/source/xmlsec/biginteger.cxx

raw
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 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20
21 #include <biginteger.hxx>
22
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <xmlsec/xmlsec.h>
25 #include <xmlsec/bn.h>
26
27 #include <comphelper/sequence.hxx>
28
29 using namespace ::com::sun::star::uno ;
30
31 namespace xmlsecurity
32 {
33 Sequence< sal_Int8 > numericStringToBigInteger ( std::u16string_view numeral )
34 {
35 xmlChar* chNumeral ;
36 const xmlSecByte* bnInteger ;
37 xmlSecSize length ;
38 xmlSecBn bn ;
39
40 OString onumeral = OUStringToOString( numeral , RTL_TEXTENCODING_ASCII_US ) ;
41
42 chNumeral = xmlStrndup( reinterpret_cast<const xmlChar*>(onumeral.getStr()), static_cast<int>(onumeral.getLength()) ) ;
43
44 if( xmlSecBnInitialize( &bn, 0 ) < 0 ) {
45 xmlFree( chNumeral ) ;
46 return Sequence< sal_Int8 >();
47 }
48
49 if( xmlSecBnFromDecString( &bn, chNumeral ) < 0 ) {
50 xmlFree( chNumeral ) ;
51 xmlSecBnFinalize( &bn ) ;
52 return Sequence< sal_Int8 >();
53 }
54
55 xmlFree( chNumeral ) ;
56
57 length = xmlSecBnGetSize( &bn ) ;
58 if( length <= 0 ) {
59 xmlSecBnFinalize( &bn ) ;
60 return Sequence< sal_Int8 >();
61 }
62
63 bnInteger = xmlSecBnGetData( &bn ) ;
64 if( bnInteger == nullptr ) {
65 xmlSecBnFinalize( &bn ) ;
66 return Sequence< sal_Int8 >();
67 }
68
69 Sequence< sal_Int8 > integer = comphelper::arrayToSequence<sal_Int8>(bnInteger, length);
70
71 xmlSecBnFinalize( &bn ) ;
72 return integer ;
73 }
74
75 OUString bigIntegerToNumericString ( const Sequence< sal_Int8 >& integer )
76 {
77 OUString aRet ;
78
79 if( integer.hasElements() ) {
80 xmlSecBn bn ;
81 xmlChar* chNumeral ;
82
83 if( xmlSecBnInitialize( &bn, 0 ) < 0 )
84 return aRet ;
85
86 if( xmlSecBnSetData( &bn, reinterpret_cast<const unsigned char*>(integer.getConstArray()), integer.getLength() ) < 0 ) {
87 xmlSecBnFinalize( &bn ) ;
88 return aRet ;
89 }
90
91 chNumeral = xmlSecBnToDecString( &bn ) ;
92 if( chNumeral == nullptr ) {
93 xmlSecBnFinalize( &bn ) ;
94 return aRet ;
95 }
96
97 aRet = OUString::createFromAscii( reinterpret_cast<char*>(chNumeral) ) ;
98
99 xmlSecBnFinalize( &bn ) ;
100 xmlFree( chNumeral ) ;
101 }
102
103 return aRet ;
104 }
105 }
106
107 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */