base64_decode.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2009 by Thermotemp GmbH. All rights reserved.
  3. *
  4. * These routines where mainly taken from pro/dencode.c
  5. * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*!
  36. * \file gorp/base64/base64_encode.c
  37. * \brief Base64 encoder.
  38. *
  39. * \verbatim
  40. *
  41. * $Log$
  42. * Revision 1.3 2009/03/08 20:18:37 haraldkipp
  43. * Replaced inttypes.h by stdint.h.
  44. *
  45. * Revision 1.2 2009/03/06 23:51:37 olereinhardt
  46. * Fixed minor compile bugs
  47. *
  48. * Revision 1.1 2009/03/06 17:46:21 olereinhardt
  49. * Initial checkin, base64 encoding and decoding routines
  50. *
  51. *
  52. * \endverbatim
  53. */
  54. #include <sys/types.h>
  55. #include <stdint.h>
  56. /*!
  57. * \addtogroup xgBase64
  58. */
  59. /*@{*/
  60. /* Base-64 decoding. This represents binary data as printable ASCII
  61. ** characters. Three 8-bit binary bytes are turned into four 6-bit
  62. ** values, like so:
  63. **
  64. ** [11111111] [22222222] [33333333]
  65. **
  66. ** [111111] [112222] [222233] [333333]
  67. **
  68. ** Then the 6-bit values are represented using the characters "A-Za-z0-9+/".
  69. */
  70. /* Since base-64 encodes strings do not have any character above 127,
  71. * we need just the first 128 bytes. Furthermore there is no char
  72. * below 32, so we can save 32 additional bytes of flash.
  73. */
  74. static const char base64dtab[96] PROGMEM = {
  75. /*
  76. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  77. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  78. */
  79. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  80. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
  81. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  82. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  83. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  84. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
  85. /*
  86. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  87. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  88. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  89. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  90. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  91. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  92. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  93. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 */
  94. };
  95. /*!
  96. * \brief Do base-64 decoding on a string.
  97. *
  98. * Ignore any non-base64 bytes.
  99. * Return the actual number of bytes generated. The decoded size will
  100. * be at most 3/4 the size of the encoded, and may be smaller if there
  101. * are padding characters (blanks, newlines).
  102. *
  103. * \param str Points to the base64 encoded string to be decoded
  104. * \return Return the actual number of bytes generated.
  105. */
  106. /*
  107. * Do base-64 decoding on a string.
  108. */
  109. char *NutDecodeBase64(char * str)
  110. {
  111. /* bug fix from Damian Slee. */
  112. char code;
  113. char *sp;
  114. char *tp;
  115. char last = -1;
  116. char step = 0;
  117. for (tp = sp = str; *sp; ++sp) {
  118. if (*sp < 32)
  119. continue;
  120. if ((code = PRG_RDB(&base64dtab[(int) *sp - 32])) == (char)-1)
  121. continue;
  122. switch (step++) {
  123. case 1:
  124. *tp++ = ((last << 2) | ((code & 0x30) >> 4));
  125. break;
  126. case 2:
  127. *tp++ = (((last & 0xf) << 4) | ((code & 0x3c) >> 2));
  128. break;
  129. case 3:
  130. *tp++ = (((last & 0x03) << 6) | code);
  131. step = 0;
  132. break;
  133. }
  134. last = code;
  135. }
  136. *tp = 0;
  137. return str;
  138. }
  139. /*@}*/