1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
30 
31 #include <xmlsecurity/biginteger.hxx>
32 
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>
41 
42 using namespace ::com::sun::star::uno ;
43 using ::rtl::OUString ;
44 
45 Sequence< sal_Int8 > numericStringToBigInteger ( OUString numeral )
46 {
47 	if( numeral.getStr() != NULL )
48 	{
49 		xmlChar* chNumeral ;
50 		const xmlSecByte* bnInteger ;
51 		xmlSecSize length ;
52 		xmlSecBn bn ;
53 
54 		rtl::OString onumeral = rtl::OUStringToOString( numeral , RTL_TEXTENCODING_ASCII_US ) ;
55 
56 		chNumeral = xmlStrndup( ( const xmlChar* )onumeral.getStr(), ( int )onumeral.getLength() ) ;
57 
58 		if( xmlSecBnInitialize( &bn, 0 ) < 0 ) {
59 			xmlFree( chNumeral ) ;
60 			return Sequence< sal_Int8 >();
61 		}
62 
63 		if( xmlSecBnFromDecString( &bn, chNumeral ) < 0 ) {
64 			xmlFree( chNumeral ) ;
65 			xmlSecBnFinalize( &bn ) ;
66 			return Sequence< sal_Int8 >();
67 		}
68 
69 		xmlFree( chNumeral ) ;
70 
71 		length = xmlSecBnGetSize( &bn ) ;
72 		if( length <= 0 ) {
73 			xmlSecBnFinalize( &bn ) ;
74 			return Sequence< sal_Int8 >();
75 		}
76 
77 		bnInteger = xmlSecBnGetData( &bn ) ;
78 		if( bnInteger == NULL ) {
79 			xmlSecBnFinalize( &bn ) ;
80 			return Sequence< sal_Int8 >();
81 		}
82 
83 		Sequence< sal_Int8 > integer( length ) ;
84 		for( unsigned int i = 0 ; i < length ; i ++ )
85 		{
86 			integer[i] = *( bnInteger + i ) ;
87 		}
88 
89 		xmlSecBnFinalize( &bn ) ;
90 		return integer ;
91 	}
92 
93 	return Sequence< sal_Int8 >();
94 }
95 
96 OUString bigIntegerToNumericString ( Sequence< sal_Int8 > integer )
97 {
98 	OUString aRet ;
99 
100 	if( integer.getLength() ) {
101 		xmlSecBn bn ;
102 		xmlChar* chNumeral ;
103 
104 		if( xmlSecBnInitialize( &bn, 0 ) < 0 )
105 			return aRet ;
106 
107 		if( xmlSecBnSetData( &bn, ( const unsigned char* )&integer[0], integer.getLength() ) < 0 ) {
108 			xmlSecBnFinalize( &bn ) ;
109 			return aRet ;
110 		}
111 
112 		chNumeral = xmlSecBnToDecString( &bn ) ;
113 		if( chNumeral == NULL ) {
114 			xmlSecBnFinalize( &bn ) ;
115 			return aRet ;
116 		}
117 
118 		aRet = OUString::createFromAscii( ( const char* )chNumeral ) ;
119 
120 		xmlSecBnFinalize( &bn ) ;
121 		xmlFree( chNumeral ) ;
122 	}
123 
124 	return aRet ;
125 }
126 
127