base32.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Base32 implementation
  2. //
  3. // Copyright 2010 Google Inc.
  4. // Author: Markus Gutschke
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. #include <string.h>
  18. #include "base32.h"
  19. int base32_decode(const uint8_t *encoded, uint8_t *result, int bufSize) {
  20. int buffer = 0;
  21. int bitsLeft = 0;
  22. int count = 0;
  23. for (const uint8_t *ptr = encoded; count < bufSize && *ptr; ++ptr) {
  24. uint8_t ch = *ptr;
  25. if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '-') {
  26. continue;
  27. }
  28. buffer <<= 5;
  29. // Deal with commonly mistyped characters
  30. if (ch == '0') {
  31. ch = 'O';
  32. } else if (ch == '1') {
  33. ch = 'L';
  34. } else if (ch == '8') {
  35. ch = 'B';
  36. }
  37. // Look up one base32 digit
  38. if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
  39. ch = (ch & 0x1F) - 1;
  40. } else if (ch >= '2' && ch <= '7') {
  41. ch -= '2' - 26;
  42. } else {
  43. return -1;
  44. }
  45. buffer |= ch;
  46. bitsLeft += 5;
  47. if (bitsLeft >= 8) {
  48. result[count++] = buffer >> (bitsLeft - 8);
  49. bitsLeft -= 8;
  50. }
  51. }
  52. if (count < bufSize) {
  53. result[count] = '\000';
  54. }
  55. return count;
  56. }
  57. int base32_encode(const uint8_t *data, int length, uint8_t *result,
  58. int bufSize) {
  59. if (length < 0 || length > (1 << 28)) {
  60. return -1;
  61. }
  62. int count = 0;
  63. if (length > 0) {
  64. int buffer = data[0];
  65. int next = 1;
  66. int bitsLeft = 8;
  67. while (count < bufSize && (bitsLeft > 0 || next < length)) {
  68. if (bitsLeft < 5) {
  69. if (next < length) {
  70. buffer <<= 8;
  71. buffer |= data[next++] & 0xFF;
  72. bitsLeft += 8;
  73. } else {
  74. int pad = 5 - bitsLeft;
  75. buffer <<= pad;
  76. bitsLeft += pad;
  77. }
  78. }
  79. int index = 0x1F & (buffer >> (bitsLeft - 5));
  80. bitsLeft -= 5;
  81. result[count++] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"[index];
  82. }
  83. }
  84. if (count < bufSize) {
  85. result[count] = '\000';
  86. }
  87. return count;
  88. }