诸暨麻将添加redis
您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
 
 
 
 
 
 

126 行
3.5 KiB

  1. /*
  2. base64.cpp and base64.h
  3. Copyright (C) 2004-2008 Ren¨¦ Nyffenegger
  4. This source code is provided 'as-is', without any express or implied
  5. warranty. In no event will the author be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this source code must not be misrepresented; you must not
  11. claim that you wrote the original source code. If you use this source code
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original source code.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. Ren¨¦ Nyffenegger rene.nyffenegger@adp-gmbh.ch
  18. */
  19. #include "stdafx.h"
  20. #include "ZBase64.h"
  21. #include <iostream>
  22. static const std::string base64_chars =
  23. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  24. "abcdefghijklmnopqrstuvwxyz"
  25. "0123456789+/";
  26. static inline bool is_base64(unsigned char c) {
  27. return (isalnum(c) || (c == '+') || (c == '/'));
  28. }
  29. std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
  30. std::string ret;
  31. int i = 0;
  32. int j = 0;
  33. unsigned char char_array_3[3];
  34. unsigned char char_array_4[4];
  35. while (in_len--) {
  36. char_array_3[i++] = *(bytes_to_encode++);
  37. if (i == 3) {
  38. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  39. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  40. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  41. char_array_4[3] = char_array_3[2] & 0x3f;
  42. for (i = 0; (i <4); i++)
  43. ret += base64_chars[char_array_4[i]];
  44. i = 0;
  45. }
  46. }
  47. if (i)
  48. {
  49. for (j = i; j < 3; j++)
  50. char_array_3[j] = '\0';
  51. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  52. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  53. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  54. char_array_4[3] = char_array_3[2] & 0x3f;
  55. for (j = 0; (j < i + 1); j++)
  56. ret += base64_chars[char_array_4[j]];
  57. while ((i++ < 3))
  58. ret += '=';
  59. }
  60. return ret;
  61. }
  62. std::string base64_decode(std::string const& encoded_string) {
  63. size_t in_len = encoded_string.size();
  64. int i = 0;
  65. int j = 0;
  66. int in_ = 0;
  67. unsigned char char_array_4[4], char_array_3[3];
  68. std::string ret;
  69. while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  70. char_array_4[i++] = encoded_string[in_]; in_++;
  71. if (i == 4) {
  72. for (i = 0; i <4; i++)
  73. char_array_4[i] = base64_chars.find(char_array_4[i]);
  74. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  75. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  76. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  77. for (i = 0; (i < 3); i++)
  78. ret += char_array_3[i];
  79. i = 0;
  80. }
  81. }
  82. if (i) {
  83. for (j = i; j <4; j++)
  84. char_array_4[j] = 0;
  85. for (j = 0; j <4; j++)
  86. char_array_4[j] = base64_chars.find(char_array_4[j]);
  87. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  88. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  89. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  90. for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
  91. }
  92. return ret;
  93. }