1
0

u32.c 2.4 KB

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