base32.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 <stdbool.h>
  19. #include "base32.h"
  20. int base32_decode(const uint8_t* encoded, uint8_t* result, int bufSize) {
  21. int buffer = 0;
  22. int bitsLeft = 0;
  23. int count = 0;
  24. for(const uint8_t* ptr = encoded; count < bufSize && *ptr; ++ptr) {
  25. uint8_t ch = *ptr;
  26. bool chIsValid = (ch >= '0' && ch <= '8') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
  27. if(!chIsValid) {
  28. continue;
  29. }
  30. buffer <<= 5;
  31. // Deal with commonly mistyped characters
  32. if(ch == '0') {
  33. ch = 'O';
  34. } else if(ch == '1') {
  35. ch = 'L';
  36. } else if(ch == '8') {
  37. ch = 'B';
  38. }
  39. // Look up one base32 digit
  40. if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
  41. ch = (ch & 0x1F) - 1;
  42. } else if(ch >= '2' && ch <= '7') {
  43. ch -= '2' - 26;
  44. } else {
  45. return -1;
  46. }
  47. buffer |= ch;
  48. bitsLeft += 5;
  49. if(bitsLeft >= 8) {
  50. result[count++] = buffer >> (bitsLeft - 8);
  51. bitsLeft -= 8;
  52. }
  53. }
  54. if(count < bufSize) {
  55. result[count] = '\000';
  56. }
  57. return count;
  58. }