source: trunk/third/libghttp/http_base64.c @ 15592

Revision 15592, 2.7 KB checked in by ghudson, 24 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r15591, which included commits to RCS files with non-trunk default branches.
RevLine 
[15591]1/*
2 * http_base64.c -- This file contains code for encoding strings with base64.
3 * Created: Christopher Blizzard <blizzard@appliedtheory.com>, 20-Nov-1998
4 * Author: Joe Orton <jeo101@york.ac.uk>
5 *
6 * Copyright (C) 1998 Free Software Foundation
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <stdlib.h>
24#include <stdio.h>
25#include <string.h>
26
27const char b64_alphabet[65] = {
28    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
29    "abcdefghijklmnopqrstuvwxyz"
30    "0123456789+/=" };
31   
32char *
33http_base64_encode(const char *text) {
34  /* The tricky thing about this is doing the padding at the end,
35   * doing the bit manipulation requires a bit of concentration only */
36  char *buffer = NULL;
37  char *point = NULL;
38  int inlen = 0;
39  int outlen = 0;
40
41  /* check our args */
42  if (text == NULL)
43    return NULL;
44 
45  /* Use 'buffer' to store the output. Work out how big it should be...
46   * This must be a multiple of 4 bytes */
47 
48  inlen = strlen( text );
49  /* check our arg...avoid a pesky FPE */
50  if (inlen == 0)
51    {
52      buffer = malloc(sizeof(char));
53      buffer[0] = '\0';
54      return buffer;
55    }
56  outlen = (inlen*4)/3;
57  if( (inlen % 3) > 0 ) /* got to pad */
58    outlen += 4 - (inlen % 3);
59 
60  buffer = malloc( outlen + 1 ); /* +1 for the \0 */
61  memset(buffer, 0, outlen + 1); /* initialize to zero */
62 
63  /* now do the main stage of conversion, 3 bytes at a time,
64   * leave the trailing bytes (if there are any) for later */
65 
66  for( point=buffer; inlen>=3; inlen-=3, text+=3 ) {
67    *(point++) = b64_alphabet[ *text>>2 ];
68    *(point++) = b64_alphabet[ (*text<<4 & 0x30) | *(text+1)>>4 ];
69    *(point++) = b64_alphabet[ (*(text+1)<<2 & 0x3c) | *(text+2)>>6 ];
70    *(point++) = b64_alphabet[ *(text+2) & 0x3f ];
71  }
72 
73  /* Now deal with the trailing bytes */
74  if( inlen ) {
75    /* We always have one trailing byte */
76    *(point++) = b64_alphabet[ *text>>2 ];
77    *(point++) = b64_alphabet[ (*text<<4 & 0x30) |
78                             (inlen==2?*(text+1)>>4:0) ];
79    *(point++) = (inlen==1?'=':b64_alphabet[ *(text+1)<<2 & 0x3c ] );
80    *(point++) = '=';
81  }
82 
83  *point = '\0';
84 
85  return buffer;
86}
Note: See TracBrowser for help on using the repository browser.