tc_base64.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef __TC_BASE64_H
  17. #define __TC_BASE64_H
  18. #include <string>
  19. using namespace std;
  20. namespace tars
  21. {
  22. /////////////////////////////////////////////////
  23. /**
  24. * @file tc_base64.h
  25. * @brief base64编解码类.
  26. */
  27. /////////////////////////////////////////////////
  28. /**
  29. * @brief 该类提供标准的Base64的编码解码
  30. */
  31. class TC_Base64
  32. {
  33. public:
  34. /**
  35. * @brief 对字符串进行base64编码.
  36. *
  37. * @param data 需要编码的数据
  38. * @param bChangeLine 是否需要在最终编码数据加入换行符 ,
  39. * (RFC中建议每76个字符后加入回车换行,默认为不添加换行
  40. * @return string 编码后的数据
  41. */
  42. static string encode(const string &data, bool bChangeLine = false);
  43. /**
  44. * @brief 对字符串进行base64解码.
  45. *
  46. * @param data 需要解码的数据
  47. * @return string 解码后的数据
  48. */
  49. static string decode(const string &data);
  50. /**
  51. * @brief 对字符串进行base64编码.
  52. *
  53. * @param buffer buffer指针
  54. * @param length 长度
  55. * @param bChangeLine 是否需要在最终编码数据加入换行符 ,
  56. * (RFC中建议每76个字符后加入回车换行,默认为不添加换行
  57. * @return string 编码后的数据
  58. */
  59. static string encode(const char *buffer, size_t length, bool bChangeLine = false);
  60. /**
  61. * @brief 对字符串进行base64编码 .
  62. *
  63. * @param pSrc 需要编码的数据
  64. * @param nSrcLen 需要编码的数据长度
  65. * @param pDst 编码后的数据
  66. * @param bChangeLine 是否需要在最终编码数据加入换行符,
  67. * RFC中建议每76个字符后加入回车换行,默认为不添加换行
  68. * @return 编码后的字符串的长度
  69. */
  70. static int encode(const unsigned char* pSrc, size_t nSrcLen, char* pDst, bool bChangeLine = false);
  71. /**
  72. * @brief 对字符串进行base64解码.
  73. *
  74. * @param pSrc 需要解码的数据
  75. * @param nSrcLe 需要解码的数据长度
  76. * @param pDst 解码后的数据
  77. * @return 解码后的字符串的长度
  78. */
  79. static int decode(const char* pSrc, size_t nSrcLen, unsigned char* pDst);
  80. protected:
  81. /**
  82. * base64编码表
  83. */
  84. static const char EnBase64Tab[];
  85. /**
  86. * base64解码表
  87. */
  88. static const char DeBase64Tab[];
  89. };
  90. }
  91. #endif