1
0

Base32.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Base32_H
  16. #define Base32_H
  17. #include <stdint.h>
  18. /*
  19. * Base32 encode or decode a byte array using the format defined in:
  20. * http://dnscurve.org/in-implement.html
  21. */
  22. /** Returned by Base32_decode() if the input is not valid base32. */
  23. #define Base32_BAD_INPUT -1
  24. /** Returned by Base32_decode() or Base32_encode() if the output buffer is too small. */
  25. #define Base32_TOO_BIG -2
  26. /**
  27. * Decode a base32 encoded number.
  28. *
  29. * @param output the place to put the decoded bytes.
  30. * This may be modified even if there is a decoding error.
  31. * @param outLength the length of the output array, if the decoded output is longer,
  32. * Base32_TOO_BIG will be returned.
  33. * @param in the buffer holding the base32 encoded number.
  34. * @param inputLength the length of the input buffer.
  35. * @return the length of the output if all goes well, Base32_BAD_INPUT if the input
  36. * is not valid base32, or Base32_TOO_BIG if the output buffer is not large
  37. * enough to handle the output.
  38. */
  39. static inline int Base32_decode(uint8_t* output,
  40. const uint32_t outLength,
  41. const uint8_t* in,
  42. const uint32_t inputLength)
  43. {
  44. // Maps ascii character inputs to the numbers
  45. // Invalid characters are represented by 99
  46. static const uint8_t numForAscii[] =
  47. {
  48. 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
  49. 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
  50. 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
  51. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,99,99,99,99,99,99,
  52. 99,99,10,11,12,99,13,14,15,99,16,17,18,19,20,99,
  53. 21,22,23,24,25,26,27,28,29,30,31,99,99,99,99,99,
  54. 99,99,10,11,12,99,13,14,15,99,16,17,18,19,20,99,
  55. 21,22,23,24,25,26,27,28,29,30,31,99,99,99,99,99
  56. };
  57. uint32_t outIndex = 0;
  58. uint32_t inputIndex = 0;
  59. uint32_t nextByte = 0;
  60. uint32_t bits = 0;
  61. while (inputIndex < inputLength) {
  62. if (in[inputIndex] & 0x80) {
  63. return Base32_BAD_INPUT;
  64. }
  65. const uint8_t b = numForAscii[in[inputIndex++]];
  66. if (b > 31) {
  67. return Base32_BAD_INPUT;
  68. }
  69. nextByte |= ((unsigned) b) << bits;
  70. bits += 5;
  71. if (bits >= 8) {
  72. if (outIndex >= outLength) {
  73. return Base32_TOO_BIG;
  74. }
  75. output[outIndex++] = nextByte;
  76. bits -= 8;
  77. nextByte >>= 8;
  78. }
  79. }
  80. if (bits >= 5 || nextByte) {
  81. return Base32_BAD_INPUT;
  82. }
  83. return outIndex;
  84. }
  85. /**
  86. * Base32 encode a number.
  87. *
  88. * @param output the place to put the base32 encoded output.
  89. * This may be modified even if there is a encoding error.
  90. * @param outLength the length of the output array, if the encoded output is longer,
  91. * Base32_TOO_BIG will be returned.
  92. * @param in the buffer holding the bytes to encode.
  93. * @param inputLength the length of the input buffer.
  94. * @return the length of the output if all goes well,
  95. * or Base32_TOO_BIG if the output buffer is not large enough to handle the output.
  96. */
  97. static inline int Base32_encode(uint8_t* output,
  98. const uint32_t outputLength,
  99. const uint8_t* in,
  100. const uint32_t inputLength)
  101. {
  102. uint32_t outIndex = 0;
  103. uint32_t inIndex = 0;
  104. uint32_t work = 0;
  105. uint32_t bits = 0;
  106. static const uint8_t* kChars = (uint8_t*) "0123456789bcdfghjklmnpqrstuvwxyz";
  107. while (inIndex < inputLength) {
  108. work |= ((unsigned) in[inIndex++]) << bits;
  109. bits += 8;
  110. while (bits >= 5) {
  111. if (outIndex >= outputLength) {
  112. return Base32_TOO_BIG;
  113. }
  114. output[outIndex++] = kChars[work & 31];
  115. bits -= 5;
  116. work >>= 5;
  117. }
  118. }
  119. if (bits) {
  120. if (outIndex >= outputLength) {
  121. return Base32_TOO_BIG;
  122. }
  123. output[outIndex++] = kChars[work & 31];
  124. bits -= 5;
  125. work >>= 5;
  126. }
  127. if (outIndex < outputLength) {
  128. output[outIndex] = '\0';
  129. }
  130. return outIndex;
  131. }
  132. #endif