merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / source / xmlsec / biginteger.cxx
blobe3947671ae38186863320d1f6a204921ba7f466f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
31 #include <xmlsecurity/biginteger.hxx>
33 #include <sal/types.h>
34 //For reasons that escape me, this is what xmlsec does when size_t is not 4
35 #if SAL_TYPES_SIZEOFPOINTER != 4
36 # define XMLSEC_NO_SIZE_T
37 #endif
38 #include <xmlsec/xmlsec.h>
39 #include <xmlsec/bn.h>
40 #include <com/sun/star/uno/Sequence.hxx>
42 using namespace ::com::sun::star::uno ;
43 using ::rtl::OUString ;
45 Sequence< sal_Int8 > numericStringToBigInteger ( OUString numeral )
47 if( numeral.getStr() != NULL )
49 xmlChar* chNumeral ;
50 const xmlSecByte* bnInteger ;
51 xmlSecSize length ;
52 xmlSecBn bn ;
54 rtl::OString onumeral = rtl::OUStringToOString( numeral , RTL_TEXTENCODING_ASCII_US ) ;
56 chNumeral = xmlStrndup( ( const xmlChar* )onumeral.getStr(), ( int )onumeral.getLength() ) ;
58 if( xmlSecBnInitialize( &bn, 0 ) < 0 ) {
59 xmlFree( chNumeral ) ;
60 return Sequence< sal_Int8 >();
63 if( xmlSecBnFromDecString( &bn, chNumeral ) < 0 ) {
64 xmlFree( chNumeral ) ;
65 xmlSecBnFinalize( &bn ) ;
66 return Sequence< sal_Int8 >();
69 xmlFree( chNumeral ) ;
71 length = xmlSecBnGetSize( &bn ) ;
72 if( length <= 0 ) {
73 xmlSecBnFinalize( &bn ) ;
74 return Sequence< sal_Int8 >();
77 bnInteger = xmlSecBnGetData( &bn ) ;
78 if( bnInteger == NULL ) {
79 xmlSecBnFinalize( &bn ) ;
80 return Sequence< sal_Int8 >();
83 Sequence< sal_Int8 > integer( length ) ;
84 for( unsigned int i = 0 ; i < length ; i ++ )
86 integer[i] = *( bnInteger + i ) ;
89 xmlSecBnFinalize( &bn ) ;
90 return integer ;
93 return Sequence< sal_Int8 >();
96 OUString bigIntegerToNumericString ( Sequence< sal_Int8 > integer )
98 OUString aRet ;
100 if( integer.getLength() ) {
101 xmlSecBn bn ;
102 xmlChar* chNumeral ;
104 if( xmlSecBnInitialize( &bn, 0 ) < 0 )
105 return aRet ;
107 if( xmlSecBnSetData( &bn, ( const unsigned char* )&integer[0], integer.getLength() ) < 0 ) {
108 xmlSecBnFinalize( &bn ) ;
109 return aRet ;
112 chNumeral = xmlSecBnToDecString( &bn ) ;
113 if( chNumeral == NULL ) {
114 xmlSecBnFinalize( &bn ) ;
115 return aRet ;
118 aRet = OUString::createFromAscii( ( const char* )chNumeral ) ;
120 xmlSecBnFinalize( &bn ) ;
121 xmlFree( chNumeral ) ;
124 return aRet ;