]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libc/port/u32.c
9ad9eb781ccdea952e9bce678579a92ccc0b7dbd
[plan9front.git] / sys / src / libc / port / u32.c
1 #include <u.h>
2 #include <libc.h>
3
4 #define between(x,min,max)      (((min-1-x) & (x-max-1))>>8)
5
6 int
7 enc32chr(int o)
8 {
9         int c;
10
11         c  = between(o,  0, 25) & ('A'+o);
12         c |= between(o, 26, 31) & ('2'+(o-26));
13         return c;
14 }
15
16 int
17 dec32chr(int c)
18 {
19         int o;
20
21         o  = between(c, 'A', 'Z') & (1+(c-'A'));
22         o |= between(c, 'a', 'z') & (1+(c-'a'));
23         o |= between(c, '2', '7') & (1+26+(c-'2'));
24         return o-1;
25 }
26
27 int
28 dec32(uchar *dest, int ndest, char *src, int nsrc)
29 {
30         uchar *start;
31         int i, j, u[8];
32
33         if(ndest+1 < (5*nsrc+7)/8)
34                 return -1;
35         start = dest;
36         while(nsrc>=8){
37                 for(i=0; i<8; i++){
38                         j = dec32chr(src[i]);
39                         if(j < 0)
40                                 j = 0;
41                         u[i] = j;
42                 }
43                 *dest++ = (u[0]<<3) | (0x7 & (u[1]>>2));
44                 *dest++ = ((0x3 & u[1])<<6) | (u[2]<<1) | (0x1 & (u[3]>>4));
45                 *dest++ = ((0xf & u[3])<<4) | (0xf & (u[4]>>1));
46                 *dest++ = ((0x1 & u[4])<<7) | (u[5]<<2) | (0x3 & (u[6]>>3));
47                 *dest++ = ((0x7 & u[6])<<5) | u[7];
48                 src  += 8;
49                 nsrc -= 8;
50         }
51         if(nsrc > 0){
52                 if(nsrc == 1 || nsrc == 3 || nsrc == 6)
53                         return -1;
54                 for(i=0; i<nsrc; i++){
55                         j = dec32chr(src[i]);
56                         if(j < 0)
57                                 j = 0;
58                         u[i] = j;
59                 }
60                 *dest++ = (u[0]<<3) | (0x7 & (u[1]>>2));
61                 if(nsrc == 2)
62                         goto out;
63                 *dest++ = ((0x3 & u[1])<<6) | (u[2]<<1) | (0x1 & (u[3]>>4));
64                 if(nsrc == 4)
65                         goto out;
66                 *dest++ = ((0xf & u[3])<<4) | (0xf & (u[4]>>1));
67                 if(nsrc == 5)
68                         goto out;
69                 *dest++ = ((0x1 & u[4])<<7) | (u[5]<<2) | (0x3 & (u[6]>>3));
70         }
71 out:
72         return dest-start;
73 }
74
75 int
76 enc32(char *dest, int ndest, uchar *src, int nsrc)
77 {
78         char *start;
79         int j;
80
81         if(ndest <= (8*nsrc+4)/5)
82                 return -1;
83         start = dest;
84         while(nsrc>=5){
85                 j = (0x1f & (src[0]>>3));
86                 *dest++ = enc32chr(j);
87                 j = (0x1c & (src[0]<<2)) | (0x03 & (src[1]>>6));
88                 *dest++ = enc32chr(j);
89                 j = (0x1f & (src[1]>>1));
90                 *dest++ = enc32chr(j);
91                 j = (0x10 & (src[1]<<4)) | (0x0f & (src[2]>>4));
92                 *dest++ = enc32chr(j);
93                 j = (0x1e & (src[2]<<1)) | (0x01 & (src[3]>>7));
94                 *dest++ = enc32chr(j);
95                 j = (0x1f & (src[3]>>2));
96                 *dest++ = enc32chr(j);
97                 j = (0x18 & (src[3]<<3)) | (0x07 & (src[4]>>5));
98                 *dest++ = enc32chr(j);
99                 j = (0x1f & (src[4]));
100                 *dest++ = enc32chr(j);
101                 src  += 5;
102                 nsrc -= 5;
103         }
104         if(nsrc){
105                 j = (0x1f & (src[0]>>3));
106                 *dest++ = enc32chr(j);
107                 j = (0x1c & (src[0]<<2));
108                 if(nsrc == 1)
109                         goto out;
110                 j |= (0x03 & (src[1]>>6));
111                 *dest++ = enc32chr(j);
112                 j = (0x1f & (src[1]>>1));
113                 *dest++ = enc32chr(j);
114                 j = (0x10 & (src[1]<<4));
115                 if(nsrc == 2)
116                         goto out;
117                 j |= (0x0f & (src[2]>>4));
118                 *dest++ = enc32chr(j);
119                 j = (0x1e & (src[2]<<1));
120                 if(nsrc == 3)
121                         goto out;
122                 j |= (0x01 & (src[3]>>7));
123                 *dest++ = enc32chr(j);
124                 j = (0x1f & (src[3]>>2));
125                 *dest++ = enc32chr(j);
126                 j = (0x18 & (src[3]<<3));
127 out:
128                 *dest++ = enc32chr(j);
129         }
130         *dest = 0;
131         return dest-start;
132 }