LibreOffice Module xmlsecurity (master) 1
biginteger.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 * 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 <xmlsec-wrapper.h>
24#include <com/sun/star/uno/Sequence.hxx>
25
27
28using namespace ::com::sun::star::uno ;
29
30namespace xmlsecurity
31{
32Sequence< sal_Int8 > numericStringToBigInteger ( std::u16string_view numeral )
33{
34 xmlChar* chNumeral ;
35 const xmlSecByte* bnInteger ;
36 xmlSecSize length ;
37 xmlSecBn bn ;
38
39 OString onumeral = OUStringToOString( numeral , RTL_TEXTENCODING_ASCII_US ) ;
40
41 chNumeral = xmlStrndup( reinterpret_cast<const xmlChar*>(onumeral.getStr()), static_cast<int>(onumeral.getLength()) ) ;
42
43 if( xmlSecBnInitialize( &bn, 0 ) < 0 ) {
44 xmlFree( chNumeral ) ;
45 return Sequence< sal_Int8 >();
46 }
47
48 if( xmlSecBnFromDecString( &bn, chNumeral ) < 0 ) {
49 xmlFree( chNumeral ) ;
50 xmlSecBnFinalize( &bn ) ;
51 return Sequence< sal_Int8 >();
52 }
53
54 xmlFree( chNumeral ) ;
55
56 length = xmlSecBnGetSize( &bn ) ;
57 if( length <= 0 ) {
58 xmlSecBnFinalize( &bn ) ;
59 return Sequence< sal_Int8 >();
60 }
61
62 bnInteger = xmlSecBnGetData( &bn ) ;
63 if( bnInteger == nullptr ) {
64 xmlSecBnFinalize( &bn ) ;
65 return Sequence< sal_Int8 >();
66 }
67
68 Sequence< sal_Int8 > integer = comphelper::arrayToSequence<sal_Int8>(bnInteger, length);
69
70 xmlSecBnFinalize( &bn ) ;
71 return integer ;
72}
73
75{
76 OUString aRet ;
77
78 if( integer.hasElements() ) {
79 xmlSecBn bn ;
80 xmlChar* chNumeral ;
81
82 if( xmlSecBnInitialize( &bn, 0 ) < 0 )
83 return aRet ;
84
85 if( xmlSecBnSetData( &bn, reinterpret_cast<const unsigned char*>(integer.getConstArray()), integer.getLength() ) < 0 ) {
86 xmlSecBnFinalize( &bn ) ;
87 return aRet ;
88 }
89
90 chNumeral = xmlSecBnToDecString( &bn ) ;
91 if( chNumeral == nullptr ) {
92 xmlSecBnFinalize( &bn ) ;
93 return aRet ;
94 }
95
96 aRet = OUString::createFromAscii( reinterpret_cast<char*>(chNumeral) ) ;
97
98 xmlSecBnFinalize( &bn ) ;
99 xmlFree( chNumeral ) ;
100 }
101
102 return aRet ;
103}
104}
105
106/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::boost::spirit::classic::rule< ScannerT > integer
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
OUString bigIntegerToNumericString(const Sequence< sal_Int8 > &integer)
Definition: biginteger.cxx:74
Sequence< sal_Int8 > numericStringToBigInteger(std::u16string_view numeral)
Definition: biginteger.cxx:32