Blame


1 b2cfc4e2 2003-09-30 devnull #include <lib9.h>
2 b2cfc4e2 2003-09-30 devnull
3 b2cfc4e2 2003-09-30 devnull int
4 b2cfc4e2 2003-09-30 devnull dec32(uchar *dest, int ndest, char *src, int nsrc)
5 b2cfc4e2 2003-09-30 devnull {
6 b2cfc4e2 2003-09-30 devnull char *s, *tab;
7 b2cfc4e2 2003-09-30 devnull uchar *start;
8 b2cfc4e2 2003-09-30 devnull int i, u[8];
9 b2cfc4e2 2003-09-30 devnull
10 b2cfc4e2 2003-09-30 devnull if(ndest+1 < (5*nsrc+7)/8)
11 b2cfc4e2 2003-09-30 devnull return -1;
12 b2cfc4e2 2003-09-30 devnull start = dest;
13 b2cfc4e2 2003-09-30 devnull tab = "23456789abcdefghijkmnpqrstuvwxyz";
14 b2cfc4e2 2003-09-30 devnull while(nsrc>=8){
15 b2cfc4e2 2003-09-30 devnull for(i=0; i<8; i++){
16 b2cfc4e2 2003-09-30 devnull s = strchr(tab,(int)src[i]);
17 b2cfc4e2 2003-09-30 devnull u[i] = s ? s-tab : 0;
18 b2cfc4e2 2003-09-30 devnull }
19 b2cfc4e2 2003-09-30 devnull *dest++ = (u[0]<<3) | (0x7 & (u[1]>>2));
20 b2cfc4e2 2003-09-30 devnull *dest++ = ((0x3 & u[1])<<6) | (u[2]<<1) | (0x1 & (u[3]>>4));
21 b2cfc4e2 2003-09-30 devnull *dest++ = ((0xf & u[3])<<4) | (0xf & (u[4]>>1));
22 b2cfc4e2 2003-09-30 devnull *dest++ = ((0x1 & u[4])<<7) | (u[5]<<2) | (0x3 & (u[6]>>3));
23 b2cfc4e2 2003-09-30 devnull *dest++ = ((0x7 & u[6])<<5) | u[7];
24 b2cfc4e2 2003-09-30 devnull src += 8;
25 b2cfc4e2 2003-09-30 devnull nsrc -= 8;
26 b2cfc4e2 2003-09-30 devnull }
27 b2cfc4e2 2003-09-30 devnull if(nsrc > 0){
28 b2cfc4e2 2003-09-30 devnull if(nsrc == 1 || nsrc == 3 || nsrc == 6)
29 b2cfc4e2 2003-09-30 devnull return -1;
30 b2cfc4e2 2003-09-30 devnull for(i=0; i<nsrc; i++){
31 b2cfc4e2 2003-09-30 devnull s = strchr(tab,(int)src[i]);
32 b2cfc4e2 2003-09-30 devnull u[i] = s ? s-tab : 0;
33 b2cfc4e2 2003-09-30 devnull }
34 b2cfc4e2 2003-09-30 devnull *dest++ = (u[0]<<3) | (0x7 & (u[1]>>2));
35 b2cfc4e2 2003-09-30 devnull if(nsrc == 2)
36 b2cfc4e2 2003-09-30 devnull goto out;
37 b2cfc4e2 2003-09-30 devnull *dest++ = ((0x3 & u[1])<<6) | (u[2]<<1) | (0x1 & (u[3]>>4));
38 b2cfc4e2 2003-09-30 devnull if(nsrc == 4)
39 b2cfc4e2 2003-09-30 devnull goto out;
40 b2cfc4e2 2003-09-30 devnull *dest++ = ((0xf & u[3])<<4) | (0xf & (u[4]>>1));
41 b2cfc4e2 2003-09-30 devnull if(nsrc == 5)
42 b2cfc4e2 2003-09-30 devnull goto out;
43 b2cfc4e2 2003-09-30 devnull *dest++ = ((0x1 & u[4])<<7) | (u[5]<<2) | (0x3 & (u[6]>>3));
44 b2cfc4e2 2003-09-30 devnull }
45 b2cfc4e2 2003-09-30 devnull out:
46 b2cfc4e2 2003-09-30 devnull return dest-start;
47 b2cfc4e2 2003-09-30 devnull }
48 b2cfc4e2 2003-09-30 devnull
49 b2cfc4e2 2003-09-30 devnull int
50 b2cfc4e2 2003-09-30 devnull enc32(char *dest, int ndest, uchar *src, int nsrc)
51 b2cfc4e2 2003-09-30 devnull {
52 b2cfc4e2 2003-09-30 devnull char *tab, *start;
53 b2cfc4e2 2003-09-30 devnull int j;
54 b2cfc4e2 2003-09-30 devnull
55 b2cfc4e2 2003-09-30 devnull if(ndest <= (8*nsrc+4)/5 )
56 b2cfc4e2 2003-09-30 devnull return -1;
57 b2cfc4e2 2003-09-30 devnull start = dest;
58 b2cfc4e2 2003-09-30 devnull tab = "23456789abcdefghijkmnpqrstuvwxyz";
59 b2cfc4e2 2003-09-30 devnull while(nsrc>=5){
60 b2cfc4e2 2003-09-30 devnull j = (0x1f & (src[0]>>3));
61 b2cfc4e2 2003-09-30 devnull *dest++ = tab[j];
62 b2cfc4e2 2003-09-30 devnull j = (0x1c & (src[0]<<2)) | (0x03 & (src[1]>>6));
63 b2cfc4e2 2003-09-30 devnull *dest++ = tab[j];
64 b2cfc4e2 2003-09-30 devnull j = (0x1f & (src[1]>>1));
65 b2cfc4e2 2003-09-30 devnull *dest++ = tab[j];
66 b2cfc4e2 2003-09-30 devnull j = (0x10 & (src[1]<<4)) | (0x0f & (src[2]>>4));
67 b2cfc4e2 2003-09-30 devnull *dest++ = tab[j];
68 b2cfc4e2 2003-09-30 devnull j = (0x1e & (src[2]<<1)) | (0x01 & (src[3]>>7));
69 b2cfc4e2 2003-09-30 devnull *dest++ = tab[j];
70 b2cfc4e2 2003-09-30 devnull j = (0x1f & (src[3]>>2));
71 b2cfc4e2 2003-09-30 devnull *dest++ = tab[j];
72 b2cfc4e2 2003-09-30 devnull j = (0x18 & (src[3]<<3)) | (0x07 & (src[4]>>5));
73 b2cfc4e2 2003-09-30 devnull *dest++ = tab[j];
74 b2cfc4e2 2003-09-30 devnull j = (0x1f & (src[4]));
75 b2cfc4e2 2003-09-30 devnull *dest++ = tab[j];
76 b2cfc4e2 2003-09-30 devnull src += 5;
77 b2cfc4e2 2003-09-30 devnull nsrc -= 5;
78 b2cfc4e2 2003-09-30 devnull }
79 b2cfc4e2 2003-09-30 devnull if(nsrc){
80 b2cfc4e2 2003-09-30 devnull j = (0x1f & (src[0]>>3));
81 b2cfc4e2 2003-09-30 devnull *dest++ = tab[j];
82 b2cfc4e2 2003-09-30 devnull j = (0x1c & (src[0]<<2));
83 b2cfc4e2 2003-09-30 devnull if(nsrc == 1)
84 b2cfc4e2 2003-09-30 devnull goto out;
85 b2cfc4e2 2003-09-30 devnull j |= (0x03 & (src[1]>>6));
86 b2cfc4e2 2003-09-30 devnull *dest++ = tab[j];
87 b2cfc4e2 2003-09-30 devnull j = (0x1f & (src[1]>>1));
88 b2cfc4e2 2003-09-30 devnull if(nsrc == 2)
89 b2cfc4e2 2003-09-30 devnull goto out;
90 b2cfc4e2 2003-09-30 devnull *dest++ = tab[j];
91 b2cfc4e2 2003-09-30 devnull j = (0x10 & (src[1]<<4));
92 b2cfc4e2 2003-09-30 devnull if(nsrc == 3)
93 b2cfc4e2 2003-09-30 devnull goto out;
94 b2cfc4e2 2003-09-30 devnull j |= (0x0f & (src[2]>>4));
95 b2cfc4e2 2003-09-30 devnull *dest++ = tab[j];
96 b2cfc4e2 2003-09-30 devnull j = (0x1e & (src[2]<<1));
97 b2cfc4e2 2003-09-30 devnull if(nsrc == 4)
98 b2cfc4e2 2003-09-30 devnull goto out;
99 b2cfc4e2 2003-09-30 devnull j |= (0x01 & (src[3]>>7));
100 b2cfc4e2 2003-09-30 devnull *dest++ = tab[j];
101 b2cfc4e2 2003-09-30 devnull j = (0x1f & (src[3]>>2));
102 b2cfc4e2 2003-09-30 devnull *dest++ = tab[j];
103 b2cfc4e2 2003-09-30 devnull j = (0x18 & (src[3]<<3));
104 b2cfc4e2 2003-09-30 devnull out:
105 b2cfc4e2 2003-09-30 devnull *dest++ = tab[j];
106 b2cfc4e2 2003-09-30 devnull }
107 b2cfc4e2 2003-09-30 devnull *dest = 0;
108 b2cfc4e2 2003-09-30 devnull return dest-start;
109 b2cfc4e2 2003-09-30 devnull }