OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rtems-20020807/] [c/] [src/] [libnetworking/] [rtems_webserver/] [wbase64.c] - Blame information for rev 1779

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1026 ivang
/*
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
 
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
 
24
static 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
 
47
static 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
 
63
int websDecode64(char_t *outbuf, char_t *string, int outlen)
64
{
65
        unsigned long   shiftbuf;
66
        char_t                  *cp, *op;
67
        int                             c, i, j, shift;
68
 
69
        op = outbuf;
70
        *op = '\0';
71
        cp = string;
72
        while (*cp && *cp != '=') {
73
/*
74
 *              Map 4 (6bit) input bytes and store in a single long (shiftbuf)
75
 */
76
                shiftbuf = 0;
77
                shift = 18;
78
                for (i = 0; i < 4 && *cp && *cp != '='; i++, cp++) {
79
                        c = map64[*cp & 0xff];
80
                        if (c == -1) {
81
                                error(E_L, E_LOG, T("Bad string: %s at %c index %d"), string,
82
                                        c, i);
83
                                return -1;
84
                        }
85
                        shiftbuf = shiftbuf | (c << shift);
86
                        shift -= 6;
87
                }
88
/*
89
 *              Interpret as 3 normal 8 bit bytes (fill in reverse order).
90
 *              Check for potential buffer overflow before filling.
91
 */
92
                --i;
93
                if ((op + i) >= &outbuf[outlen]) {
94
                        gstrcpy(outbuf, T("String too big"));
95
                        return -1;
96
                }
97
                for (j = 0; j < i; j++) {
98
                        *op++ = (char_t) ((shiftbuf >> (8 * (2 - j))) & 0xff);
99
                }
100
                *op = '\0';
101
        }
102
        return 0;
103
}
104
 
105
 
106
/******************************************************************************/
107
/*
108
 *      Encode a buffer from "string" into "outbuf"
109
 */
110
 
111
void websEncode64(char_t *outbuf, char_t *string, int outlen)
112
{
113
        unsigned long   shiftbuf;
114
        char_t                  *cp, *op;
115
        int                             x, i, j, shift;
116
 
117
        op = outbuf;
118
        *op = '\0';
119
        cp = string;
120
        while (*cp) {
121
/*
122
 *              Take three characters and create a 24 bit number in shiftbuf
123
 */
124
                shiftbuf = 0;
125
                for (j = 2; j >= 0 && *cp; j--, cp++) {
126
                        shiftbuf |= ((*cp & 0xff) << (j * 8));
127
                }
128
/*
129
 *              Now convert shiftbuf to 4 base64 letters.  The i,j magic calculates
130
 *              how many letters need to be output.
131
 */
132
                shift = 18;
133
                for (i = ++j; i < 4 && op < &outbuf[outlen] ; i++) {
134
                        x = (shiftbuf >> shift) & 0x3f;
135
                        *op++ = alphabet64[(shiftbuf >> shift) & 0x3f];
136
                        shift -= 6;
137
                }
138
/*
139
 *              Pad at the end with '='
140
 */
141
                while (j-- > 0) {
142
                        *op++ = '=';
143
                }
144
                *op = '\0';
145
        }
146
}
147
/******************************************************************************/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.