nss: upgrade to release 3.73
[LibreOffice.git] / xmlsecurity / source / xmlsec / biginteger.cxx
blob0bfcbd326de1fa3a5a8961a240418b9866826691
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 using namespace ::com::sun::star::uno ;
28 namespace xmlsecurity
30 Sequence< sal_Int8 > numericStringToBigInteger ( const OUString& numeral )
32 xmlChar* chNumeral ;
33 const xmlSecByte* bnInteger ;
34 xmlSecSize length ;
35 xmlSecBn bn ;
37 OString onumeral = OUStringToOString( numeral , RTL_TEXTENCODING_ASCII_US ) ;
39 chNumeral = xmlStrndup( reinterpret_cast<const xmlChar*>(onumeral.getStr()), static_cast<int>(onumeral.getLength()) ) ;
41 if( xmlSecBnInitialize( &bn, 0 ) < 0 ) {
42 xmlFree( chNumeral ) ;
43 return Sequence< sal_Int8 >();
46 if( xmlSecBnFromDecString( &bn, chNumeral ) < 0 ) {
47 xmlFree( chNumeral ) ;
48 xmlSecBnFinalize( &bn ) ;
49 return Sequence< sal_Int8 >();
52 xmlFree( chNumeral ) ;
54 length = xmlSecBnGetSize( &bn ) ;
55 if( length <= 0 ) {
56 xmlSecBnFinalize( &bn ) ;
57 return Sequence< sal_Int8 >();
60 bnInteger = xmlSecBnGetData( &bn ) ;
61 if( bnInteger == nullptr ) {
62 xmlSecBnFinalize( &bn ) ;
63 return Sequence< sal_Int8 >();
66 Sequence< sal_Int8 > integer( length ) ;
67 for( xmlSecSize i = 0 ; i < length ; i ++ )
69 integer[i] = *( bnInteger + i ) ;
72 xmlSecBnFinalize( &bn ) ;
73 return integer ;
76 OUString bigIntegerToNumericString ( const Sequence< sal_Int8 >& integer )
78 OUString aRet ;
80 if( integer.hasElements() ) {
81 xmlSecBn bn ;
82 xmlChar* chNumeral ;
84 if( xmlSecBnInitialize( &bn, 0 ) < 0 )
85 return aRet ;
87 if( xmlSecBnSetData( &bn, reinterpret_cast<const unsigned char*>(integer.getConstArray()), integer.getLength() ) < 0 ) {
88 xmlSecBnFinalize( &bn ) ;
89 return aRet ;
92 chNumeral = xmlSecBnToDecString( &bn ) ;
93 if( chNumeral == nullptr ) {
94 xmlSecBnFinalize( &bn ) ;
95 return aRet ;
98 aRet = OUString::createFromAscii( reinterpret_cast<char*>(chNumeral) ) ;
100 xmlSecBnFinalize( &bn ) ;
101 xmlFree( chNumeral ) ;
104 return aRet ;
108 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */