PageRenderTime 41ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/llbase32.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 238 lines | 57 code | 22 blank | 159 comment | 7 complexity | 3b8009b81986f59622acc8ee0c1e58a9 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llbase32.cpp
  3. * @brief base32 encoding that returns a std::string
  4. * @author James Cook
  5. *
  6. * Based on code from bitter
  7. * http://ghostwhitecrab.com/bitter/
  8. *
  9. * Some parts of this file are:
  10. * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  11. * Second Life Viewer Source Code
  12. * Copyright (C) 2010, Linden Research, Inc.
  13. *
  14. * This library is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Lesser General Public
  16. * License as published by the Free Software Foundation;
  17. * version 2.1 of the License only.
  18. *
  19. * This library is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with this library; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. *
  28. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  29. * $/LicenseInfo$
  30. */
  31. /**
  32. * Copyright (c) 2006 Christian Biere <christianbiere@gmx.de>
  33. * All rights reserved.
  34. *
  35. * Redistribution and use in source and binary forms, with or without
  36. * modification, are permitted provided that the following conditions
  37. * are met:
  38. *
  39. * 1. Redistributions of source code must retain the above copyright
  40. * notice, this list of conditions and the following disclaimer.
  41. * 2. Redistributions in binary form must reproduce the above copyright
  42. * notice, this list of conditions and the following disclaimer in the
  43. * documentation and/or other materials provided with the distribution.
  44. * 3. Neither the name of the authors nor the names of its contributors
  45. * may be used to endorse or promote products derived from this software
  46. * without specific prior written permission.
  47. *
  48. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
  49. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  52. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  53. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  54. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  56. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  58. * SUCH DAMAGE.
  59. */
  60. #include "linden_common.h"
  61. #include "llbase32.h"
  62. #include <string>
  63. // bitter - base32.c starts here
  64. /*
  65. * See RFC 3548 for details about Base 32 encoding:
  66. * http://www.faqs.org/rfcs/rfc3548.html
  67. */
  68. static const char base32_alphabet[32] = {
  69. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  70. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  71. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  72. 'Y', 'Z', '2', '3', '4', '5', '6', '7'
  73. };
  74. size_t
  75. base32_encode(char *dst, size_t size, const void *data, size_t len)
  76. {
  77. size_t i = 0;
  78. const U8 *p = (const U8*)data;
  79. const char *end = &dst[size];
  80. char *q = dst;
  81. do {
  82. size_t j, k;
  83. U8 x[5];
  84. char s[8];
  85. switch (len - i) {
  86. case 4: k = 7; break;
  87. case 3: k = 5; break;
  88. case 2: k = 3; break;
  89. case 1: k = 2; break;
  90. default:
  91. k = 8;
  92. }
  93. for (j = 0; j < 5; j++)
  94. x[j] = i < len ? p[i++] : 0;
  95. /*
  96. +-------+-----------+--------+
  97. | target| source | source |
  98. | byte | bits | byte |
  99. +-------+-----------+--------+
  100. | 0 | 7 6 5 4 3 | 0 |
  101. | 1 | 2 1 0 7 6 | 0-1 |
  102. | 2 | 5 4 3 2 1 | 1 |
  103. | 3 | 0 7 6 5 4 | 1-2 |
  104. | 4 | 3 2 1 0 7 | 2-3 |
  105. | 5 | 6 5 4 3 2 | 3 |
  106. | 6 | 1 0 7 6 5 | 3-4 |
  107. | 7 | 4 3 2 1 0 | 4 |
  108. +-------+-----------+--------+
  109. */
  110. s[0] = (x[0] >> 3);
  111. s[1] = ((x[0] & 0x07) << 2) | (x[1] >> 6);
  112. s[2] = (x[1] >> 1) & 0x1f;
  113. s[3] = ((x[1] & 0x01) << 4) | (x[2] >> 4);
  114. s[4] = ((x[2] & 0x0f) << 1) | (x[3] >> 7);
  115. s[5] = (x[3] >> 2) & 0x1f;
  116. s[6] = ((x[3] & 0x03) << 3) | (x[4] >> 5);
  117. s[7] = x[4] & 0x1f;
  118. for (j = 0; j < k && q != end; j++)
  119. *q++ = base32_alphabet[(U8) s[j]];
  120. } while (i < len);
  121. return q - dst;
  122. }
  123. /* *TODO: Implement base32 encode.
  124. static inline int
  125. ascii_toupper(int c)
  126. {
  127. return c >= 97 && c <= 122 ? c - 32 : c;
  128. }
  129. static inline int
  130. ascii_tolower(int c)
  131. {
  132. return c >= 65 && c <= 90 ? c + 32 : c;
  133. }
  134. static char base32_map[(unsigned char) -1];
  135. size_t
  136. base32_decode(char *dst, size_t size, const void *data, size_t len)
  137. {
  138. const char *end = &dst[size];
  139. const unsigned char *p = data;
  140. char *q = dst;
  141. size_t i;
  142. unsigned max_pad = 3;
  143. if (0 == base32_map[0]) {
  144. for (i = 0; i < LL_ARRAY_SIZE(base32_map); i++) {
  145. const char *x;
  146. x = memchr(base32_alphabet, ascii_toupper(i), sizeof base32_alphabet);
  147. base32_map[i] = x ? (x - base32_alphabet) : (unsigned char) -1;
  148. }
  149. }
  150. for (i = 0; i < len && max_pad > 0; i++) {
  151. unsigned char c;
  152. char s[8];
  153. size_t j;
  154. c = p[i];
  155. if ('=' == c) {
  156. max_pad--;
  157. c = 0;
  158. } else {
  159. c = base32_map[c];
  160. if ((unsigned char) -1 == c) {
  161. return -1;
  162. }
  163. }
  164. j = i % LL_ARRAY_SIZE(s);
  165. s[j] = c;
  166. if (7 == j) {
  167. char b[5];
  168. b[0] = ((s[0] << 3) & 0xf8) | ((s[1] >> 2) & 0x07);
  169. b[1] = ((s[1] & 0x03) << 6) | ((s[2] & 0x1f) << 1) | ((s[3] >> 4) & 1);
  170. b[2] = ((s[3] & 0x0f) << 4) | ((s[4] >> 1) & 0x0f);
  171. b[3] = ((s[4] & 1) << 7) | ((s[5] & 0x1f) << 2) | ((s[6] >> 3) & 0x03);
  172. b[4] = ((s[6] & 0x07) << 5) | (s[7] & 0x1f);
  173. for (j = 0; j < LL_ARRAY_SIZE(b); j++) {
  174. if (q != end)
  175. *q = b[j];
  176. q++;
  177. }
  178. }
  179. }
  180. return q - dst;
  181. }
  182. */
  183. // The following is
  184. // Copyright (c) 2007-$CurrentYear$, Linden Research, Inc.
  185. // static
  186. std::string LLBase32::encode(const U8* input, size_t input_size)
  187. {
  188. std::string output;
  189. if (input)
  190. {
  191. // Each 5 byte chunk of input is represented by an
  192. // 8 byte chunk of output.
  193. size_t input_chunks = (input_size + 4) / 5;
  194. size_t output_size = input_chunks * 8;
  195. output.resize(output_size);
  196. size_t encoded = base32_encode(&output[0], output_size, input, input_size);
  197. llinfos << "encoded " << encoded << " into buffer of size "
  198. << output_size << llendl;
  199. }
  200. return output;
  201. }