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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#include <u.h>
#include <libc.h>
#define between(x,min,max) (((min-1-x) & (x-max-1))>>8)
int
enc32chr(int o)
{
int c;
c = between(o, 0, 25) & ('A'+o);
c |= between(o, 26, 31) & ('2'+(o-26));
return c;
}
int
dec32chr(int c)
{
int o;
o = between(c, 'A', 'Z') & (1+(c-'A'));
o |= between(c, 'a', 'z') & (1+(c-'a'));
o |= between(c, '2', '7') & (1+26+(c-'2'));
return o-1;
}
int
dec32x(uchar *dest, int ndest, char *src, int nsrc, int (*chr)(int))
{
uchar *start;
int i, j, u[8];
if(ndest+1 < (5*nsrc+7)/8)
return -1;
start = dest;
while(nsrc>=8){
for(i=0; i<8; i++){
j = chr(src[i]);
if(j < 0)
j = 0;
u[i] = j;
}
*dest++ = (u[0]<<3) | (0x7 & (u[1]>>2));
*dest++ = ((0x3 & u[1])<<6) | (u[2]<<1) | (0x1 & (u[3]>>4));
*dest++ = ((0xf & u[3])<<4) | (0xf & (u[4]>>1));
*dest++ = ((0x1 & u[4])<<7) | (u[5]<<2) | (0x3 & (u[6]>>3));
*dest++ = ((0x7 & u[6])<<5) | u[7];
src += 8;
nsrc -= 8;
}
if(nsrc > 0){
if(nsrc == 1 || nsrc == 3 || nsrc == 6)
return -1;
for(i=0; i<nsrc; i++){
j = chr(src[i]);
if(j < 0)
j = 0;
u[i] = j;
}
*dest++ = (u[0]<<3) | (0x7 & (u[1]>>2));
if(nsrc == 2)
goto out;
*dest++ = ((0x3 & u[1])<<6) | (u[2]<<1) | (0x1 & (u[3]>>4));
if(nsrc == 4)
goto out;
*dest++ = ((0xf & u[3])<<4) | (0xf & (u[4]>>1));
if(nsrc == 5)
goto out;
*dest++ = ((0x1 & u[4])<<7) | (u[5]<<2) | (0x3 & (u[6]>>3));
}
out:
return dest-start;
}
int
enc32x(char *dest, int ndest, uchar *src, int nsrc, int (*chr)(int))
{
char *start;
int j;
if(ndest <= (8*nsrc+4)/5)
return -1;
start = dest;
while(nsrc>=5){
j = (0x1f & (src[0]>>3));
*dest++ = chr(j);
j = (0x1c & (src[0]<<2)) | (0x03 & (src[1]>>6));
*dest++ = chr(j);
j = (0x1f & (src[1]>>1));
*dest++ = chr(j);
j = (0x10 & (src[1]<<4)) | (0x0f & (src[2]>>4));
*dest++ = chr(j);
j = (0x1e & (src[2]<<1)) | (0x01 & (src[3]>>7));
*dest++ = chr(j);
j = (0x1f & (src[3]>>2));
*dest++ = chr(j);
j = (0x18 & (src[3]<<3)) | (0x07 & (src[4]>>5));
*dest++ = chr(j);
j = (0x1f & (src[4]));
*dest++ = chr(j);
src += 5;
nsrc -= 5;
}
if(nsrc){
j = (0x1f & (src[0]>>3));
*dest++ = chr(j);
j = (0x1c & (src[0]<<2));
if(nsrc == 1)
goto out;
j |= (0x03 & (src[1]>>6));
*dest++ = chr(j);
j = (0x1f & (src[1]>>1));
*dest++ = chr(j);
j = (0x10 & (src[1]<<4));
if(nsrc == 2)
goto out;
j |= (0x0f & (src[2]>>4));
*dest++ = chr(j);
j = (0x1e & (src[2]<<1));
if(nsrc == 3)
goto out;
j |= (0x01 & (src[3]>>7));
*dest++ = chr(j);
j = (0x1f & (src[3]>>2));
*dest++ = chr(j);
j = (0x18 & (src[3]<<3));
out:
*dest++ = chr(j);
}
*dest = 0;
return dest-start;
}
int
enc32(char *dest, int ndest, uchar *src, int nsrc)
{
return enc32x(dest, ndest, src, nsrc, enc32chr);
}
int
dec32(uchar *dest, int ndest, char *src, int nsrc)
{
return dec32x(dest, ndest, src, nsrc, dec32chr);
}
|