vx32

Local 9vx git repository for patches.
git clone git://r-36.net/vx32
Log | Files | Refs

u32.c (2413B)


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