1
0

base32_from.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <stddef.h>
  2. #include <stdint.h>
  3. #include "types.h"
  4. #include "base32.h"
  5. static const u8 base32f[256] = {
  6. //00 01 02 03 04 05 06 07
  7. //08 09 0A 0B 0C 0D 0E 0F
  8. // 0x00..0x3F
  9. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x00
  10. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x08
  11. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x10
  12. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x18
  13. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x20
  14. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x28
  15. 0xFF, 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, // 0x30
  16. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x38
  17. // 0x40..0x7F
  18. 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // 0x40
  19. 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 0x48
  20. 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 0x50
  21. 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x58
  22. 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // 0x60
  23. 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 0x68
  24. 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 0x70
  25. 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x78
  26. // 0x80..0xBF
  27. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  28. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  29. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  30. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  31. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  32. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  33. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  34. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  35. // 0xC0..0xFF
  36. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  37. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  38. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  39. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  40. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  41. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  42. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  43. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  44. };
  45. /*
  46. +--first octet--+-second octet--+--third octet--+--forth octet--+--fifth octet--+
  47. |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|
  48. +---------+-----+---+---------+-+-------+-------+-+---------+---+-----+---------+
  49. |4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|4 3 2 1 0|
  50. +-1.index-+-2.index-+-3.index-+-4.index-+-5.index-+-6.index-+-7.index-+-8.index-+
  51. */
  52. size_t base32_from(u8 *dst,u8 *dmask,const char *src)
  53. {
  54. size_t i, j, k = (size_t)-1, l, sk = 0;
  55. u8 mask = 0, cmask = 0;
  56. for (i = 0;;i += 5) {
  57. j = i/5;
  58. if (!src[j]) {
  59. if (k != (size_t)-1)
  60. dst[k] &= cmask;
  61. if (dmask)
  62. *dmask = cmask;
  63. return k + 1;
  64. }
  65. l = i%8;
  66. k = i/8;
  67. if (k != sk)
  68. cmask = 0;
  69. sk = k;
  70. mask = (0x1F << 3) >> l;
  71. cmask |= mask;
  72. //printf("1:m[%02X] cm[%02X]\n",mask,cmask);
  73. dst[k] &= ~mask;
  74. dst[k] |= (base32f[(u8)src[j]] << 3) >> l;
  75. if (((0x1F << 8) >> (l+5-8)) & 0xFF) {
  76. ++k;
  77. sk = k;
  78. mask = ((0x1F << 8) >> (l+5-8)) & 0xFF;
  79. cmask = mask;
  80. //printf("2:m[%02X] cm[%02X]\n",mask,cmask);
  81. dst[k] &= ~mask;
  82. dst[k] |= ((base32f[(u8)src[j]] << 8) >> (l+5-8)) & 0xFF;
  83. }
  84. //printf("i = %02d, i/8 = %02d, i%8 = %02d, i%8+5-8 = %02d\n", i, i/8, i%8, i%8+5-8);
  85. //printf("mask0: %02x\n", (0x1F << 3) >> (i%8));
  86. //printf("mask1: %02x\n", ((0x1F << 8) >> (i%8+5-8)) & 0xFF);
  87. }
  88. }
  89. int base32_valid(const char *src,size_t *count)
  90. {
  91. const char *p;
  92. for (p = src;base32f[(u8)*p] != 0xFF;++p)
  93. ;
  94. if (count)
  95. *count = (size_t) (p - src);
  96. return !*p;
  97. }