Initial revision
[libcurl.git] / lib / base64.c
blobd0e2dd55d949a6b0b58eb6ac29c30bd0a4f2f251
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * The contents of this file are subject to the Mozilla Public License
9 * Version 1.0 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS IS"
14 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15 * License for the specific language governing rights and limitations
16 * under the License.
18 * The Original Code is Curl.
20 * The Initial Developer of the Original Code is Daniel Stenberg.
22 * Portions created by the Initial Developer are Copyright (C) 1998.
23 * All Rights Reserved.
25 * ------------------------------------------------------------
26 * Main author:
27 * - Daniel Stenberg <Daniel.Stenberg@haxx.nu>
29 * http://curl.haxx.nu
31 * $Source: /cvsroot/curl/curl/lib/base64.c,v $
32 * $Revision: 1.1 $
33 * $Date: 1999-12-29 14:21:17 $
34 * $Author: bagder $
35 * $State: Exp $
36 * $Locker: $
38 * ------------------------------------------------------------
39 ****************************************************************************/
41 #include <stdio.h>
43 /* ---- Base64 Encoding --- */
44 static char table64[]=
45 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
47 void base64Encode(char *intext, char *output)
49 unsigned char ibuf[3];
50 unsigned char obuf[4];
51 int i;
52 int inputparts;
54 while(*intext) {
55 for (i = inputparts = 0; i < 3; i++) {
56 if(*intext) {
57 inputparts++;
58 ibuf[i] = *intext;
59 intext++;
61 else
62 ibuf[i] = 0;
65 obuf [0] = (ibuf [0] & 0xFC) >> 2;
66 obuf [1] = ((ibuf [0] & 0x03) << 4) | ((ibuf [1] & 0xF0) >> 4);
67 obuf [2] = ((ibuf [1] & 0x0F) << 2) | ((ibuf [2] & 0xC0) >> 6);
68 obuf [3] = ibuf [2] & 0x3F;
70 switch(inputparts) {
71 case 1: /* only one byte read */
72 sprintf(output, "%c%c==",
73 table64[obuf[0]],
74 table64[obuf[1]]);
75 break;
76 case 2: /* two bytes read */
77 sprintf(output, "%c%c%c=",
78 table64[obuf[0]],
79 table64[obuf[1]],
80 table64[obuf[2]]);
81 break;
82 default:
83 sprintf(output, "%c%c%c%c",
84 table64[obuf[0]],
85 table64[obuf[1]],
86 table64[obuf[2]],
87 table64[obuf[3]] );
88 break;
90 output += 4;
92 *output=0;
94 /* ---- End of Base64 Encoding ---- */