You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ref.c 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*-
  2. Copyright (c) 2013-2015, Alfred Klomp
  3. Copyright (c) 2016, Vsevolod Stakhov
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are
  7. met:
  8. - Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. - Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  14. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  15. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  16. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  17. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  18. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  19. TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  21. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "libutil/util.h"
  27. extern const uint8_t base64_table_dec[256];
  28. #define INNER_LOOP_64 \
  29. do { \
  30. uint64_t str, res, dec; \
  31. bool aligned = rspamd_is_aligned_as(c, str); \
  32. while (inlen >= 13) { \
  33. if (aligned) { str = *(uint64_t *) c; } \
  34. else { \
  35. memcpy(&str, c, sizeof(str)); \
  36. } \
  37. str = GUINT64_TO_BE(str); \
  38. if ((dec = base64_table_dec[str >> 56]) > 63) { \
  39. break; \
  40. } \
  41. res = dec << 58; \
  42. if ((dec = base64_table_dec[(str >> 48) & 0xFF]) > 63) { \
  43. break; \
  44. } \
  45. res |= dec << 52; \
  46. if ((dec = base64_table_dec[(str >> 40) & 0xFF]) > 63) { \
  47. break; \
  48. } \
  49. res |= dec << 46; \
  50. if ((dec = base64_table_dec[(str >> 32) & 0xFF]) > 63) { \
  51. break; \
  52. } \
  53. res |= dec << 40; \
  54. if ((dec = base64_table_dec[(str >> 24) & 0xFF]) > 63) { \
  55. break; \
  56. } \
  57. res |= dec << 34; \
  58. if ((dec = base64_table_dec[(str >> 16) & 0xFF]) > 63) { \
  59. break; \
  60. } \
  61. res |= dec << 28; \
  62. if ((dec = base64_table_dec[(str >> 8) & 0xFF]) > 63) { \
  63. break; \
  64. } \
  65. res |= dec << 22; \
  66. if ((dec = base64_table_dec[str & 0xFF]) > 63) { \
  67. break; \
  68. } \
  69. res |= dec << 16; \
  70. res = GUINT64_FROM_BE(res); \
  71. memcpy(o, &res, sizeof(res)); \
  72. c += 8; \
  73. o += 6; \
  74. outl += 6; \
  75. inlen -= 8; \
  76. } \
  77. } while (0)
  78. #define INNER_LOOP_32 \
  79. do { \
  80. uint32_t str, res, dec; \
  81. bool aligned = rspamd_is_aligned_as(c, str); \
  82. while (inlen >= 8) { \
  83. if (aligned) { str = *(uint32_t *) c; } \
  84. else { \
  85. memcpy(&str, c, sizeof(str)); \
  86. } \
  87. str = GUINT32_TO_BE(str); \
  88. if ((dec = base64_table_dec[str >> 24]) > 63) { \
  89. break; \
  90. } \
  91. res = dec << 26; \
  92. if ((dec = base64_table_dec[(str >> 16) & 0xFF]) > 63) { \
  93. break; \
  94. } \
  95. res |= dec << 20; \
  96. if ((dec = base64_table_dec[(str >> 8) & 0xFF]) > 63) { \
  97. break; \
  98. } \
  99. res |= dec << 14; \
  100. if ((dec = base64_table_dec[str & 0xFF]) > 63) { \
  101. break; \
  102. } \
  103. res |= dec << 8; \
  104. res = GUINT32_FROM_BE(res); \
  105. memcpy(o, &res, sizeof(res)); \
  106. c += 4; \
  107. o += 3; \
  108. outl += 3; \
  109. inlen -= 4; \
  110. } \
  111. } while (0)
  112. int base64_decode_ref(const char *in, size_t inlen,
  113. unsigned char *out, size_t *outlen)
  114. {
  115. ssize_t ret = 0;
  116. const uint8_t *c = (const uint8_t *) in;
  117. uint8_t *o = (uint8_t *) out;
  118. uint8_t q, carry;
  119. size_t outl = 0;
  120. size_t leftover = 0;
  121. repeat:
  122. switch (leftover) {
  123. for (;;) {
  124. case 0:
  125. #if defined(__LP64__)
  126. INNER_LOOP_64;
  127. #else
  128. INNER_LOOP_32;
  129. #endif
  130. if (inlen-- == 0) {
  131. ret = 1;
  132. break;
  133. }
  134. if ((q = base64_table_dec[*c++]) >= 254) {
  135. ret = 0;
  136. break;
  137. }
  138. carry = (uint8_t) (q << 2);
  139. leftover++;
  140. case 1:
  141. if (inlen-- == 0) {
  142. ret = 1;
  143. break;
  144. }
  145. if ((q = base64_table_dec[*c++]) >= 254) {
  146. ret = 0;
  147. break;
  148. }
  149. *o++ = carry | (q >> 4);
  150. carry = (uint8_t) (q << 4);
  151. leftover++;
  152. outl++;
  153. case 2:
  154. if (inlen-- == 0) {
  155. ret = 1;
  156. break;
  157. }
  158. if ((q = base64_table_dec[*c++]) >= 254) {
  159. leftover++;
  160. if (q == 254) {
  161. if (inlen-- != 0) {
  162. leftover = 0;
  163. q = base64_table_dec[*c++];
  164. ret = ((q == 254) && (inlen == 0)) ? 1 : 0;
  165. break;
  166. }
  167. else {
  168. ret = 1;
  169. break;
  170. }
  171. }
  172. else {
  173. leftover--;
  174. }
  175. /* If we get here, there was an error: */
  176. break;
  177. }
  178. *o++ = carry | (q >> 2);
  179. carry = (uint8_t) (q << 6);
  180. leftover++;
  181. outl++;
  182. case 3:
  183. if (inlen-- == 0) {
  184. ret = 1;
  185. break;
  186. }
  187. if ((q = base64_table_dec[*c++]) >= 254) {
  188. /*
  189. * When q == 254, the input char is '='. Return 1 and EOF.
  190. * When q == 255, the input char is invalid. Return 0 and EOF.
  191. */
  192. if (q == 254 && inlen == 0) {
  193. ret = 1;
  194. leftover = 0;
  195. }
  196. else {
  197. ret = 0;
  198. }
  199. break;
  200. }
  201. *o++ = carry | q;
  202. carry = 0;
  203. leftover = 0;
  204. outl++;
  205. }
  206. }
  207. if (!ret && inlen > 0) {
  208. /* Skip to the next valid character in input */
  209. while (inlen > 0 && base64_table_dec[*c] >= 254) {
  210. c++;
  211. inlen--;
  212. }
  213. if (inlen > 0) {
  214. goto repeat;
  215. }
  216. }
  217. *outlen = outl;
  218. return ret;
  219. }