Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / xmlsecurity / source / xmlsec / biginteger.cxx
blob1a4ab6fd9d132336362710f635766f4c24b9f299
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <biginteger.hxx>
23 #include <xmlsec-wrapper.h>
24 #include <com/sun/star/uno/Sequence.hxx>
26 #include <comphelper/sequence.hxx>
28 using namespace ::com::sun::star::uno ;
30 namespace xmlsecurity
32 Sequence< sal_Int8 > numericStringToBigInteger ( std::u16string_view numeral )
34 xmlChar* chNumeral ;
35 const xmlSecByte* bnInteger ;
36 xmlSecSize length ;
37 xmlSecBn bn ;
39 OString onumeral = OUStringToOString( numeral , RTL_TEXTENCODING_ASCII_US ) ;
41 chNumeral = xmlStrndup( reinterpret_cast<const xmlChar*>(onumeral.getStr()), static_cast<int>(onumeral.getLength()) ) ;
43 if( xmlSecBnInitialize( &bn, 0 ) < 0 ) {
44 xmlFree( chNumeral ) ;
45 return Sequence< sal_Int8 >();
48 if( xmlSecBnFromDecString( &bn, chNumeral ) < 0 ) {
49 xmlFree( chNumeral ) ;
50 xmlSecBnFinalize( &bn ) ;
51 return Sequence< sal_Int8 >();
54 xmlFree( chNumeral ) ;
56 length = xmlSecBnGetSize( &bn ) ;
57 if( length <= 0 ) {
58 xmlSecBnFinalize( &bn ) ;
59 return Sequence< sal_Int8 >();
62 bnInteger = xmlSecBnGetData( &bn ) ;
63 if( bnInteger == nullptr ) {
64 xmlSecBnFinalize( &bn ) ;
65 return Sequence< sal_Int8 >();
68 Sequence< sal_Int8 > integer = comphelper::arrayToSequence<sal_Int8>(bnInteger, length);
70 xmlSecBnFinalize( &bn ) ;
71 return integer ;
74 OUString bigIntegerToNumericString ( const Sequence< sal_Int8 >& integer )
76 OUString aRet ;
78 if( integer.hasElements() ) {
79 xmlSecBn bn ;
80 xmlChar* chNumeral ;
82 if( xmlSecBnInitialize( &bn, 0 ) < 0 )
83 return aRet ;
85 if( xmlSecBnSetData( &bn, reinterpret_cast<const unsigned char*>(integer.getConstArray()), integer.getLength() ) < 0 ) {
86 xmlSecBnFinalize( &bn ) ;
87 return aRet ;
90 chNumeral = xmlSecBnToDecString( &bn ) ;
91 if( chNumeral == nullptr ) {
92 xmlSecBnFinalize( &bn ) ;
93 return aRet ;
96 aRet = OUString::createFromAscii( reinterpret_cast<char*>(chNumeral) ) ;
98 xmlSecBnFinalize( &bn ) ;
99 xmlFree( chNumeral ) ;
102 return aRet ;
106 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */