Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*************************************************************************
3 : *
4 : * The Contents of this file are made available subject to the terms of
5 : * either of the following licenses
6 : *
7 : * - GNU Lesser General Public License Version 2.1
8 : * - Sun Industry Standards Source License Version 1.1
9 : *
10 : * Sun Microsystems Inc., October, 2000
11 : *
12 : * GNU Lesser General Public License Version 2.1
13 : * =============================================
14 : * Copyright 2000 by Sun Microsystems, Inc.
15 : * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 : *
17 : * This library is free software; you can redistribute it and/or
18 : * modify it under the terms of the GNU Lesser General Public
19 : * License version 2.1, as published by the Free Software Foundation.
20 : *
21 : * This library is distributed in the hope that it will be useful,
22 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 : * Lesser General Public License for more details.
25 : *
26 : * You should have received a copy of the GNU Lesser General Public
27 : * License along with this library; if not, write to the Free Software
28 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 : * MA 02111-1307 USA
30 : *
31 : *
32 : * Sun Industry Standards Source License Version 1.1
33 : * =================================================
34 : * The contents of this file are subject to the Sun Industry Standards
35 : * Source License Version 1.1 (the "License"); You may not use this file
36 : * except in compliance with the License. You may obtain a copy of the
37 : * License at http://www.openoffice.org/license.html.
38 : *
39 : * Software provided under this License is provided on an "AS IS" basis,
40 : * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 : * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 : * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 : * See the License for the specific provisions governing your rights and
44 : * obligations concerning the Software.
45 : *
46 : * The Initial Developer of the Original Code is: IBM Corporation
47 : *
48 : * Copyright: 2008 by IBM Corporation
49 : *
50 : * All Rights Reserved.
51 : *
52 : * Contributor(s): _______________________________________
53 : *
54 : *
55 : ************************************************************************/
56 : /*************************************************************************
57 : * @file
58 : * Base64 tool.
59 : ************************************************************************/
60 : #include <string.h>
61 : #include "xfbase64.hxx"
62 :
63 : const sal_Char aBase64EncodeTable[] =
64 : { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
65 : 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
66 : 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
67 : 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
68 : '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
69 :
70 : /**
71 : * @descr Encode 3 byte to 4 byte.
72 : * Please refer to RFC to get the base64 algorithm.
73 : */
74 0 : inline void Encode_(sal_uInt8 *src, sal_Char* dest)
75 : {
76 0 : sal_Int32 nBinaer = (((sal_uInt8)src[ 0]) << 16) +
77 0 : (((sal_uInt8)src[1]) << 8) +
78 0 : ((sal_uInt8)src[2]);
79 :
80 0 : sal_uInt8 nIndex = ((nBinaer & 0xFC0000) >> 18);
81 0 : dest[0] = aBase64EncodeTable [nIndex];
82 :
83 0 : nIndex = (nBinaer & 0x3F000) >> 12;
84 0 : dest[1] =aBase64EncodeTable [nIndex];
85 :
86 0 : nIndex = (nBinaer & 0xFC0) >> 6;
87 0 : dest[2] = aBase64EncodeTable [nIndex];
88 :
89 0 : nIndex = (nBinaer & 0x3F);
90 0 : dest[3] = aBase64EncodeTable [nIndex];
91 0 : }
92 :
93 : /**
94 : * @descr Base64 encode.
95 : */
96 0 : rtl::OUString XFBase64::Encode(sal_uInt8 *buf, sal_Int32 len)
97 : {
98 : sal_Char *buffer;
99 : sal_Int32 nNeeded;
100 0 : sal_Int32 cycles = len/3;
101 0 : sal_Int32 remain = len%3;
102 :
103 0 : if( remain == 0 )
104 0 : nNeeded = cycles*4;
105 : else
106 0 : nNeeded = (cycles+1)*4;
107 0 : buffer = new sal_Char[nNeeded+1];
108 :
109 0 : memset(buffer, 0, nNeeded+1);
110 :
111 0 : for( sal_Int32 i=0; i<cycles; i++ )
112 0 : Encode_(buf+i*3,buffer+i*4);
113 :
114 : sal_uInt8 last[3];
115 0 : if( remain == 1 )
116 : {
117 0 : last[0] = buf[len-1];
118 0 : last[1] = last[2] = 0;
119 0 : Encode_(last,buffer+nNeeded+1-5);
120 : }
121 0 : else if( remain == 2 )
122 : {
123 0 : last[0] = buf[len-2];
124 0 : last[1] = buf[len-1];
125 0 : last[2] = 0;
126 0 : Encode_(last,buffer+nNeeded+1-5);
127 : }
128 :
129 0 : rtl::OUString str = rtl::OUString::createFromAscii(buffer);
130 0 : delete[] buffer;
131 :
132 0 : return str;
133 : }
134 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|