source: rtems/cpukit/httpd/wbase64.c @ cf1f72e

4.104.114.84.95
Last change on this file since cf1f72e was c1cdaa0, checked in by Joel Sherrill <joel.sherrill@…>, on 10/27/99 at 12:50:33

Patch from Emmanuel Raguet <raguet@…> and Eric Valette
<valette@…> to add a port of the GoAhead? web server
(httpd) to the RTEMS build tree. They have successfully used
this BSP on i386/pc386 and PowerPC/mcp750.

Mark and Joel spoke with Nick Berliner <nickb@…> on
26 Oct 1999 about this port and got verbal approval to include
it in RTEMS distributions.

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2 * base64.c -- Base64 Mime encoding
3 *
4 * Copyright (c) Go Ahead Software Inc., 1995-1999. All Rights Reserved.
5 *
6 * See the file "license.txt" for usage and redistribution license requirements
7 */
8
9/******************************** Description *********************************/
10
11/*
12 *      The base64 command encodes and decodes a string in mime base64 format
13 */
14
15/********************************* Includes ***********************************/
16
17#include        "wsIntrn.h"
18
19/******************************** Local Data **********************************/
20/*
21 *      Mapping of ANSI chars to base64 Mime encoding alphabet (see below)
22 */
23
24static char_t   map64[] = {
25        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
26        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
27        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
28        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
29        -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
30    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
31        -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
32    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
33        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
39        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45};
46
47static char_t   alphabet64[] = {
48        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
49        'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
50        'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
51        'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
52        'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
53        'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
54        'w', 'x', 'y', 'z', '0', '1', '2', '3',
55        '4', '5', '6', '7', '8', '9', '+', '/',
56};
57
58/*********************************** Code *************************************/
59/*
60 *      Decode a buffer from "string" and into "outbuf"
61 */
62
63int websDecode64(char_t* outbuf, char_t* string, int outlen)
64{
65        unsigned long   shiftbuf;
66        char_t*                 cp;
67        char_t*                 op;
68        int                             c, i, j, shift;
69
70        op = outbuf;
71        *op = '\0';
72        cp = string;
73        while (*cp && *cp != '=') {
74/*
75 *              Map 4 (6bit) input bytes and store in a single long (shiftbuf)
76 */
77                shiftbuf = 0;
78                shift = 18;
79                for (i = 0; i < 4 && *cp && *cp != '='; i++, cp++) {
80                        c = map64[*cp & 0xff];
81                        if (c == -1) {
82                                error(E_L, E_LOG, T("Bad string: %s at %c index %d"), string,
83                                        c, i);
84                                return -1;
85                        }
86                        shiftbuf = shiftbuf | (c << shift);
87                        shift -= 6;
88                }
89/*
90 *              Interpret as 3 normal 8 bit bytes (fill in reverse order).
91 *              Check for potential buffer overflow before filling.
92 */
93                --i;
94                if ((op + i) >= &outbuf[outlen]) {
95                        gstrcpy(outbuf, T("String too big"));
96                        return -1;
97                }
98                for (j = 0; j < i; j++) {
99                        *op++ = (char_t) ((shiftbuf >> (8 * (2 - j))) & 0xff);
100                }
101                *op = '\0';
102        }
103        return 0;
104}
105
106
107/******************************************************************************/
108/*
109 *      Encode a buffer from "string" into "outbuf"
110 */
111
112void websEncode64(char_t* outbuf, char_t* string, int outlen)
113{
114        unsigned long   shiftbuf;
115        char_t*                 cp;
116        char_t*                 op;
117        int                             x, i, j, shift;
118
119        op = outbuf;
120        *op = '\0';
121        cp = string;
122        while (*cp) {
123/*
124 *              Take three characters and create a 24 bit number in shiftbuf
125 */
126                shiftbuf = 0;
127                for (j = 2; j >= 0 && *cp; j--, cp++) {
128                        shiftbuf |= ((*cp & 0xff) << (j * 8));
129                }
130/*
131 *              Now convert shiftbuf to 4 base64 letters.  The i,j magic calculates
132 *              how many letters need to be output.
133 */
134                shift = 18;
135                for (i = ++j; i < 4 && op < &outbuf[outlen] ; i++) {
136                        x = (shiftbuf >> shift) & 0x3f;
137                        *op++ = alphabet64[(shiftbuf >> shift) & 0x3f];
138                        shift -= 6;
139                }
140/*
141 *              Pad at the end with '='
142 */
143                while (j-- > 0) {
144                        *op++ = '=';
145                }
146                *op = '\0';
147        }
148}
149/******************************************************************************/
Note: See TracBrowser for help on using the repository browser.