llbase64.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * @file llbase64.h
  3. *
  4. * $LicenseInfo:firstyear=2007&license=viewergpl$
  5. * See llbase64.cpp for the Apache license applicable to part of the code.
  6. *
  7. * Copyright (c) 2004 Apache Foundation (for parts borrowed from APR-util),
  8. * (c) 2007-2009, Linden Research, Inc.
  9. * (c) 2009-2023, Henri Beauchamp.
  10. *
  11. * Second Life Viewer Source Code
  12. * The source code in this file ("Source Code") is provided by Linden Lab
  13. * to you under the terms of the GNU General Public License, version 2.0
  14. * ("GPL"), unless you have obtained a separate licensing agreement
  15. * ("Other License"), formally executed by you and Linden Lab. Terms of
  16. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  17. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  18. *
  19. * There are special exceptions to the terms and conditions of the GPL as
  20. * it is applied to this Source Code. View the full text of the exception
  21. * in the file doc/FLOSS-exception.txt in this software distribution, or
  22. * online at
  23. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  24. *
  25. * By copying, modifying or distributing this software, you acknowledge
  26. * that you have read and understood your obligations described above,
  27. * and agree to abide by those obligations.
  28. *
  29. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  30. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  31. * COMPLETENESS OR PERFORMANCE.
  32. * $/LicenseInfo$
  33. */
  34. #ifndef LL_LLBASE64_H
  35. #define LL_LLBASE64_H
  36. #include <string>
  37. #include "llpreprocessor.h"
  38. // Purely static class
  39. class LLBase64
  40. {
  41. LLBase64() = delete;
  42. ~LLBase64() = delete;
  43. public:
  44. static std::string encode(const char* input, size_t input_size);
  45. LL_INLINE static std::string encode(const std::string& input)
  46. {
  47. return encode(input.c_str(), input.size());
  48. }
  49. static std::string decode(const char* input);
  50. LL_INLINE static std::string decode(const std::string& input)
  51. {
  52. return decode(input.c_str());
  53. }
  54. // Low-level API, derived from APR-util's code (see llbase64.cpp for the
  55. // Apache license applicable to this code). Only used in lldserialize.cpp
  56. // (keep it that way, please and use instead the above methods for any new
  57. // code needing base64 coding, since the underlying code may change in the
  58. // future for a better/faster implementation). HB
  59. // Returns an estimation of the required maximum buffer size for encoding.
  60. LL_INLINE static size_t encodeLen(size_t len)
  61. {
  62. return ((len + 2) / 3 * 4) + 1;
  63. }
  64. // Returns an estimation of the required maximum buffer size for decoding.
  65. static size_t decodeLen(const char* input);
  66. static size_t decode(unsigned char* output, const char* input);
  67. static size_t encode(char* output, const unsigned char* input, size_t len);
  68. };
  69. #endif // LL_LLBASE64_H