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