source: rtems/cpukit/httpd/wbase64.c @ 4e97166

4.104.114.84.95
Last change on this file since 4e97166 was ee3afa2, checked in by Joel Sherrill <joel.sherrill@…>, on 04/11/03 at 14:46:55

2002-04-10 Mike Siers <mikes@…>

  • rtems_webserver/NOTES, rtems_webserver/asp.c, rtems_webserver/balloc.c, rtems_webserver/default.c, rtems_webserver/ej.h, rtems_webserver/ejIntrn.h, rtems_webserver/ejlex.c, rtems_webserver/ejparse.c, rtems_webserver/emfdb.c, rtems_webserver/emfdb.h, rtems_webserver/form.c, rtems_webserver/h.c, rtems_webserver/handler.c, rtems_webserver/license.txt, rtems_webserver/md5.h, rtems_webserver/md5c.c, rtems_webserver/mime.c, rtems_webserver/misc.c, rtems_webserver/ringq.c, rtems_webserver/rom.c, rtems_webserver/security.c, rtems_webserver/sock.c, rtems_webserver/sym.c, rtems_webserver/uemf.c, rtems_webserver/uemf.h, rtems_webserver/um.c, rtems_webserver/um.h, rtems_webserver/url.c, rtems_webserver/value.c, rtems_webserver/wbase64.c, rtems_webserver/webcomp.c, rtems_webserver/webpage.c, rtems_webserver/webrom.c, rtems_webserver/webs.c, rtems_webserver/webs.h, rtems_webserver/websuemf.c, rtems_webserver/wsIntrn.h: Update to GoAhead? Webserver 2.1.4. The following URL is the release notes from GoAhead?.

http://data.goahead.com/Software/Webserver/2.1.4/release.htm

I have only done a minimal amount of testing (i.e. the network
demo program works fine). Please try this out and let me know
if it works. The patch needs to be applied on the
c/src/libnetworking/rtems_webserver directory.

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