Update ooo320-m1
[ooovba.git] / xmlsecurity / source / xmlsec / biginteger.cxx
blob8d3a72db40573c5e2c44101349df6f0a08607fb1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: biginteger.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include <xmlsecurity/biginteger.hxx>
36 #include <sal/types.h>
37 //For reasons that escape me, this is what xmlsec does when size_t is not 4
38 #if SAL_TYPES_SIZEOFPOINTER != 4
39 # define XMLSEC_NO_SIZE_T
40 #endif
41 #include <xmlsec/xmlsec.h>
42 #include <xmlsec/bn.h>
43 #include <com/sun/star/uno/Sequence.hxx>
45 using namespace ::com::sun::star::uno ;
46 using ::rtl::OUString ;
48 Sequence< sal_Int8 > numericStringToBigInteger ( OUString numeral )
50 if( numeral.getStr() != NULL )
52 xmlChar* chNumeral ;
53 const xmlSecByte* bnInteger ;
54 xmlSecSize length ;
55 xmlSecBn bn ;
57 rtl::OString onumeral = rtl::OUStringToOString( numeral , RTL_TEXTENCODING_ASCII_US ) ;
59 chNumeral = xmlStrndup( ( const xmlChar* )onumeral.getStr(), ( int )onumeral.getLength() ) ;
61 if( xmlSecBnInitialize( &bn, 0 ) < 0 ) {
62 xmlFree( chNumeral ) ;
63 return Sequence< sal_Int8 >();
66 if( xmlSecBnFromDecString( &bn, chNumeral ) < 0 ) {
67 xmlFree( chNumeral ) ;
68 xmlSecBnFinalize( &bn ) ;
69 return Sequence< sal_Int8 >();
72 xmlFree( chNumeral ) ;
74 length = xmlSecBnGetSize( &bn ) ;
75 if( length <= 0 ) {
76 xmlSecBnFinalize( &bn ) ;
77 return Sequence< sal_Int8 >();
80 bnInteger = xmlSecBnGetData( &bn ) ;
81 if( bnInteger == NULL ) {
82 xmlSecBnFinalize( &bn ) ;
83 return Sequence< sal_Int8 >();
86 Sequence< sal_Int8 > integer( length ) ;
87 for( unsigned int i = 0 ; i < length ; i ++ )
89 integer[i] = *( bnInteger + i ) ;
92 xmlSecBnFinalize( &bn ) ;
93 return integer ;
96 return Sequence< sal_Int8 >();
99 OUString bigIntegerToNumericString ( Sequence< sal_Int8 > integer )
101 OUString aRet ;
103 if( integer.getLength() ) {
104 xmlSecBn bn ;
105 xmlChar* chNumeral ;
107 if( xmlSecBnInitialize( &bn, 0 ) < 0 )
108 return aRet ;
110 if( xmlSecBnSetData( &bn, ( const unsigned char* )&integer[0], integer.getLength() ) < 0 ) {
111 xmlSecBnFinalize( &bn ) ;
112 return aRet ;
115 chNumeral = xmlSecBnToDecString( &bn ) ;
116 if( chNumeral == NULL ) {
117 xmlSecBnFinalize( &bn ) ;
118 return aRet ;
121 aRet = OUString::createFromAscii( ( const char* )chNumeral ) ;
123 xmlSecBnFinalize( &bn ) ;
124 xmlFree( chNumeral ) ;
127 return aRet ;