File: base64.c

package info (click to toggle)
ppxp 0.99120923-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,812 kB
  • ctags: 3,704
  • sloc: ansic: 24,532; tcl: 3,992; makefile: 517; sh: 80
file content (126 lines) | stat: -rw-r--r-- 2,353 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
  This code is based on:

	   Encode or decode file as MIME base64 (RFC 1341)

			    by John Walker
		       http://www.fourmilab.ch/

		This program is in the public domain.

*/

#define REVDATE "11th August 1997"

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

typedef unsigned char byte;	      /* Byte type */

/*  ENCODE  --	Encode binary file into base64.  */

void
Base64Encode(char *dst, char *src, size_t max)
{
    int i, n=3;
    static byte dtable[64];

    if (!dtable[63]) {
	/*	Fill dtable with character encodings.  */

	for (i = 0; i < 26; i++) {
	    dtable[i] = 'A' + i;
	    dtable[26 + i] = 'a' + i;
	}
	for (i = 0; i < 10; i++) {
	    dtable[52 + i] = '0' + i;
	}
	dtable[62] = '+';
	dtable[63] = '/';
    }

    while (max > 0 && n > 2 && *src) {
	byte igroup[4]={0};

	strncpy(igroup, src, 3);
	n = strlen(igroup);
	dst[0] = dtable[igroup[0] >> 2];
	dst[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
	dst[2] = dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
	dst[3] = dtable[igroup[2] & 0x3F];

	if (n < 3) {
	    dst[3] = '=';
	    if (n < 2) {
		dst[2] = '=';
		dst --;
		max ++;
	    }
	}
	src += 3;
	dst += 4;
	max -= 4;
    }
    *dst = '\0';
}

/*  DECODE  --	Decode base64.	*/

void
Base64Decode(char *dst, char *src, size_t max)
{
    int i;
    static byte dtable[256];

    if (!dtable['/']) {
	for (i = 0; i < 255; i++) {
	    dtable[i] = 0x80;
	}
	for (i = 'A'; i <= 'Z'; i++) {
	    dtable[i] = 0 + (i - 'A');
	}
	for (i = 'a'; i <= 'z'; i++) {
	    dtable[i] = 26 + (i - 'a');
	}
	for (i = '0'; i <= '9'; i++) {
	    dtable[i] = 52 + (i - '0');
	}
	dtable['+'] = 62;
	dtable['/'] = 63;
	dtable['='] = 0;
    }

    /*CONSTANTCONDITION*/
    while (max > 0 && *src > ' ') {
	*dst ++ = (dtable[src[0]] << 2) | (dtable[src[1]] >> 4);
	if (src[2] != '=') {
	    *dst ++ = (dtable[src[1]] << 4) | (dtable[src[2]] >> 2);
	    if (src[3] != '=')
		*dst ++ = (dtable[src[2]] << 6) | dtable[src[3]];
	    else break;
	} else break;
	src += 4;
	max -= 4;
    }
    *dst = '\0';
}

#ifdef	DEBUG_BASE64

/*  Main program  */

int main(int argc, char *argv[])
{
    char en[256], de[256];

    printf("<%s>", argv[1]);
    Base64Encode(en, argv[1], sizeof(en));
    printf("-><%s>", en);
    Base64Decode(de, en, sizeof(de));
    printf("-><%s>\n", de);
    return 0;
}

#endif