Main Page   Modules   Compound List   File List   Compound Members   File Members   Related Pages  

wbxml_base64.c

Go to the documentation of this file.
00001 /*
00002  * WBXML Lib, the WBXML Library.
00003  * Copyright (C) 2002-2003  Aymerick Jéhanne
00004  * 
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License (version 2.1) as published by the Free Software Foundation.
00008  * 
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  * LGPL v2.1: http://www.gnu.org/licenses/lgpl.txt
00019  *
00020  * Author Contact: libwbxml@jehanne.org
00021  * WBXML Lib home: http://libwbxml.jehanne.org
00022  */
00023  
00036 #include <string.h> /* For strlen() */
00037 
00038 #include "wbxml.h"
00039 
00040 
00041 /* aaaack but it's fast and const should make it shared text page. */
00042 static const unsigned char pr2six[256] =
00043 {
00044     /* ASCII table */
00045     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00046     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00047     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
00048     52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
00049     64,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
00050     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
00051     64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
00052     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
00053     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00054     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00055     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00056     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00057     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00058     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00059     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
00060     64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
00061 };
00062 
00064 static const char basis_64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00065 
00066 
00067 /**********************************
00068  *    Public functions
00069  */
00070 
00071 /* Function adapted from APR library (http://apr.apache.org/) */
00072 WBXML_DECLARE(WB_UTINY *) wbxml_base64_encode(const WB_UTINY *buffer, WB_LONG len)
00073 {
00074     WB_LONG i = 0;
00075     WB_UTINY *p = NULL, *result = NULL;
00076 
00077     if ((buffer == NULL) || (len <= 0))
00078         return NULL;
00079 
00080     /* Malloc result buffer */
00081     if ((result = (WB_UTINY *) wbxml_malloc(((len + 2) / 3 * 4) + 1 + 1)) == NULL)
00082         return NULL;
00083 
00084     p = result;
00085     for (i = 0; i < len - 2; i += 3) {
00086             *p++ = basis_64[(buffer[i] >> 2) & 0x3F];
00087             *p++ = basis_64[((buffer[i] & 0x3) << 4) |
00088                             ((int) (buffer[i + 1] & 0xF0) >> 4)];
00089             *p++ = basis_64[((buffer[i + 1] & 0xF) << 2) |
00090                             ((int) (buffer[i + 2] & 0xC0) >> 6)];
00091             *p++ = basis_64[buffer[i + 2] & 0x3F];
00092     }
00093     if (i < len) {
00094             *p++ = basis_64[(buffer[i] >> 2) & 0x3F];
00095             if (i == (len - 1)) {
00096                 *p++ = basis_64[((buffer[i] & 0x3) << 4)];
00097                 *p++ = '=';
00098             }
00099             else {
00100                 *p++ = basis_64[((buffer[i] & 0x3) << 4) |
00101                                 ((int) (buffer[i + 1] & 0xF0) >> 4)];
00102                 *p++ = basis_64[((buffer[i + 1] & 0xF) << 2)];
00103             }
00104             *p++ = '=';
00105     }
00106 
00107     *p++ = '\0';
00108 
00109     return result;
00110 }
00111 
00112 
00113 /* Function adapted from APR library (http://apr.apache.org/) */
00114 WBXML_DECLARE(WB_LONG) wbxml_base64_decode(const WB_UTINY *buffer, WB_UTINY **result)
00115 {
00116     WB_LONG nbytesdecoded = 0, nprbytes = 0;
00117     const WB_UTINY *bufin = NULL;
00118     WB_UTINY *bufout = NULL;
00119 
00120     if ((buffer == NULL) || (result == NULL))
00121         return 0;
00122 
00123     /* Initialize output buffer */
00124     *result = NULL;
00125 
00126     bufin = buffer;   
00127     while (pr2six[*(bufin++)] <= 63);
00128     
00129     nprbytes = (bufin - buffer) - 1;
00130     nbytesdecoded = ((nprbytes + 3) / 4) * 3;
00131     
00132     /* Malloc result buffer */
00133     if ((*result = (WB_UTINY*) wbxml_malloc(nbytesdecoded + 1)) == NULL)
00134         return 0;
00135 
00136     bufout = *result;
00137     bufin = buffer;
00138 
00139     while (nprbytes > 4) 
00140     {
00141             *(bufout++) = (WB_UTINY) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
00142         *(bufout++) = (WB_UTINY) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
00143         *(bufout++) = (WB_UTINY) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
00144         bufin += 4;
00145         nprbytes -= 4;
00146     }
00147 
00148     /* Note: (nprbytes == 1) would be an error, so just ingore that case */
00149     if (nprbytes > 1) {
00150             *(bufout++) = (WB_UTINY) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
00151     }
00152     if (nprbytes > 2) {
00153             *(bufout++) = (WB_UTINY) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
00154     }
00155     if (nprbytes > 3) {
00156             *(bufout++) = (WB_UTINY) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
00157     }
00158 
00159     nbytesdecoded -= (4 - nprbytes) & 3;
00160     
00161     return nbytesdecoded;
00162 }

Generated on Mon Nov 24 20:09:42 2003 for WBXML Library by doxygen1.3-rc1