TUM-Logo

libRASCH

 

Home
 

General

About libRASCH/News
Design
Screen shots
Sample programs (with source code)
License
 

Download

libRASCH
Tools
 

Documentation

User
Developer
 

Resources

Mailing list
Supported Formats
Plugins
Status
Links
 
Contact
About this site
 
Last updated
Tue Mar 27 23:03:52 2007
libRASCH: mime64.c Source File

mime64.c

Go to the documentation of this file.
00001 
00009 /*----------------------------------------------------------------------------
00010  *
00011  * Copyright (C) 2002, Raphael Schneider
00012  * See the file COPYING for information on usage and redistribution.
00013  *
00014  * $Header: /home/cvs_repos/librasch/src/eval/mime64.c,v 1.5 2004/07/13 20:16:26 rasch Exp $
00015  *
00016  *--------------------------------------------------------------------------*/
00017 
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 
00021 
00022 unsigned char base64_alph[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
00023 
00024 
00025 /* to be sure of hex-values
00026 
00027 0     1     2     3     4     5     6     7     8     9     A     B     C     D     E     F
00028 0000  0001  0010  0011  0100  0101  0110  0111  1000  1001  1010  1011  1100  1101  1110  1111
00029 
00030 */
00031 
00041 int
00042 encode_base64(unsigned char *in, long isize, unsigned char **out, long *osize)
00043 {
00044         long l, o_pos = 0, len;
00045         unsigned char t;
00046         long line_size = 0;
00047 
00048         len = (long) ((double) isize * 1.33) + 4;
00049         len += ((len / 76) * 2) + 2;
00050         *out = malloc(len);
00051         if (*out == NULL)
00052                 return -1;
00053 
00054         for (l = 0; l < isize; l += 3)
00055         {
00056                 if (l < (isize - 2))
00057                 {
00058                         t = (in[l] & 0xfc) >> 2;
00059                         (*out)[o_pos++] = base64_alph[t];
00060                         t = ((in[l] & 0x03) << 4) | ((in[l + 1] & 0xf0) >> 4);
00061                         (*out)[o_pos++] = base64_alph[t];
00062                         t = ((in[l + 1] & 0x0f) << 2) | ((in[l + 2] & 0xc0) >> 6);
00063                         (*out)[o_pos++] = base64_alph[t];
00064                         t = in[l + 2] & 0x3f;
00065                         (*out)[o_pos++] = base64_alph[t];
00066                 }
00067                 else if (l < (isize - 1))
00068                 {
00069                         t = (in[l] & 0xfc) >> 2;
00070                         (*out)[o_pos++] = base64_alph[t];
00071                         t = ((in[l] & 0x03) << 4) | ((in[l + 1] & 0xf0) >> 4);
00072                         (*out)[o_pos++] = base64_alph[t];
00073                         t = (in[l + 1] & 0x0f) << 2;
00074                         (*out)[o_pos++] = base64_alph[t];
00075                         (*out)[o_pos++] = '=';
00076                 }
00077                 else
00078                 {
00079                         t = (in[l] & 0xfc) >> 2;
00080                         (*out)[o_pos++] = base64_alph[t];
00081                         t = (in[l] & 0x03) << 4;
00082                         (*out)[o_pos++] = base64_alph[t];
00083                         (*out)[o_pos++] = '=';
00084                         (*out)[o_pos++] = '=';
00085                 }
00086                 line_size += 4;
00087 
00088                 if (line_size >= 76)
00089                 {
00090                         (*out)[o_pos++] = '\n'; /* libxml2 has problems with CRLF */
00091                         line_size = 0;
00092                 }
00093         }
00094         (*out)[o_pos] = '\0';   /* being save if osize will be ignored */
00095         *osize = o_pos;
00096         *out = realloc(*out, *osize + 1);
00097 
00098         return 0;
00099 } /* encode_base64() */
00100 
00101 
00113 int
00114 decode_base64(unsigned char *in, long isize, unsigned char **out, long *osize, long max_size)
00115 {
00116         long l;
00117         long o_pos = 0;
00118 
00119         unsigned char decode_alph[255];
00120 
00121         for (l = 0; l < 64; l++)
00122                 decode_alph[base64_alph[l]] = (unsigned char)l;
00123 
00124         *out = malloc(isize);
00125 
00126         l = 0;
00127         for (;;)
00128         {
00129                 if ((max_size != -1) && (o_pos >= max_size))
00130                         break;  /* we have all needed data */
00131 
00132                 if ((l + 3) >= isize)
00133                         break;
00134 
00135                 if ((in[l] == 0x0A) || (in[l] == 0x0D) || (in[l] == ' '))
00136                 {
00137                         l++;
00138                         continue;
00139                 }
00140 
00141                 (*out)[o_pos++] =
00142                         (decode_alph[in[l]] << 2) | ((decode_alph[in[l + 1]] & 0xf0) >> 4);
00143                 if (in[l + 2] != '=')
00144                         (*out)[o_pos++] =
00145                                 ((decode_alph[in[l + 1]] & 0x0f) << 4) |
00146                                 ((decode_alph[in[l + 2]] & 0xfc) >> 2);
00147 
00148                 if (in[l + 3] != '=')
00149                         (*out)[o_pos++] =
00150                                 ((decode_alph[in[l + 2]] & 0x03) << 6) | decode_alph[in[l + 3]];
00151 
00152                 l += 4;
00153         }
00154 
00155         *osize = o_pos;
00156         *out = realloc(*out, *osize);
00157 
00158         return 0;
00159 } /* decode_base64() */

Generated on Fri May 27 11:32:38 2005 for libRASCH by  doxygen 1.4.2