ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
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.
 
 
 
 
 
 

182 lines
3.9 KiB

  1. /*
  2. * Copyright: (c) 2008-2012 Artyom Beilis
  3. * License: MIT
  4. * Other Copyrights:
  5. * 7 lines of code had taken from: http://base64.sourceforge.net/b64.c
  6. * and they are subject to MIT license by (c) Trantor Standard Systems Inc., 2001
  7. */
  8. #define CPPCMS_SOURCE
  9. #include <cppcms/base64.h>
  10. #include <vector>
  11. #include <ostream>
  12. namespace {
  13. const unsigned char encode_6_to_8[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  14. inline unsigned char encode_8_to_6(unsigned char c)
  15. {
  16. if('A'<=c && c<='Z')
  17. return c - 'A';
  18. if('a'<=c && c<='z')
  19. return ('Z'-'A' + 1) + c - 'a';
  20. if('0'<=c && c<='9')
  21. return 2*('Z'-'A' + 1) + c - '0';
  22. if(c=='-')
  23. return 62;
  24. if(c=='_')
  25. return 63;
  26. return 0;
  27. }
  28. size_t inline bencode(unsigned const char in[3],unsigned char out[4],size_t len)
  29. {
  30. out[0] = encode_6_to_8[in[0] >> 2];
  31. if(len<=1) {
  32. out[1] = encode_6_to_8[(in[0] & 0x03) << 4];
  33. return 2;
  34. }
  35. else {
  36. out[1] = encode_6_to_8[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)];
  37. if(len<=2) {
  38. out[2] = encode_6_to_8[(in[1] & 0x0f) << 2];
  39. return 3;
  40. }
  41. else {
  42. out[2] = encode_6_to_8[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)];
  43. out[3] = encode_6_to_8[in[2] & 0x3f];
  44. return 4;
  45. }
  46. }
  47. }
  48. inline size_t bdecode(unsigned const char in8[4],unsigned char out[3],size_t len)
  49. {
  50. unsigned char in[4] = { 0 };
  51. for(unsigned i=0;i<len;i++)
  52. in[i]=encode_8_to_6(in8[i]);
  53. out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
  54. if(len==2) {
  55. return 1;
  56. }
  57. else {
  58. out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
  59. if(len==3) {
  60. return 2;
  61. }
  62. else {
  63. out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
  64. return 3;
  65. }
  66. }
  67. }
  68. } // anon namespace
  69. namespace cppcms {
  70. namespace b64url {
  71. int encoded_size(size_t s)
  72. {
  73. switch(s % 3) {
  74. case 1: return s/3*4+2;
  75. case 2: return s/3*4+3;
  76. default:
  77. return s/3*4;
  78. }
  79. }
  80. int decoded_size(size_t s)
  81. {
  82. switch(s % 4) {
  83. case 1: return -1; // invalid
  84. case 2: return s/4*3+1;
  85. case 3: return s/4*3+2;
  86. default:
  87. return s/4*3;
  88. }
  89. }
  90. unsigned char *encode(unsigned char const *begin,unsigned char const *end,unsigned char *target)
  91. {
  92. while(end - begin >=3) {
  93. bencode(begin,target,3);
  94. begin += 3;
  95. target += 4;
  96. }
  97. if(end!=begin)
  98. target+=bencode(begin,target,end-begin);
  99. return target;
  100. }
  101. void encode(unsigned char const *begin,unsigned char const *end,std::ostream &out)
  102. {
  103. unsigned char target[4];
  104. while(end - begin >=3) {
  105. bencode(begin,target,3);
  106. begin += 3;
  107. out.write(reinterpret_cast<char *>(target),4);
  108. }
  109. if(end!=begin) {
  110. int n = bencode(begin,target,end-begin);
  111. out.write(reinterpret_cast<char *>(target),n);
  112. }
  113. }
  114. unsigned char *decode(unsigned char const *begin,unsigned char const *end,unsigned char *target)
  115. {
  116. while(end - begin >=4) {
  117. bdecode(begin,target,4);
  118. begin+=4;
  119. target+=3;
  120. }
  121. if(end!=begin)
  122. target+=bdecode(begin,target,end-begin);
  123. return target;
  124. }
  125. bool CPPCMS_API decode(std::string const &input,std::string &output)
  126. {
  127. int ds = decoded_size(input.size());
  128. if(ds < 0)
  129. return false;
  130. if(ds == 0)
  131. return true;
  132. unsigned char const *begin = reinterpret_cast<unsigned char const *>(input.c_str());
  133. unsigned char const *end = begin + input.size();
  134. std::vector<char> buf(ds,0);
  135. unsigned char *outbuf = reinterpret_cast<unsigned char *>(&buf[0]);
  136. decode(begin,end,outbuf);
  137. output.assign(&buf[0],ds);
  138. return true;
  139. }
  140. ///
  141. /// Perform base64 URL encoding of the binary data \a input, and return it
  142. ///
  143. ///
  144. std::string CPPCMS_API encode(std::string const &input)
  145. {
  146. std::string result;
  147. size_t enc_size = encoded_size(input.size());
  148. if(enc_size == 0)
  149. return result;
  150. unsigned char const *begin = reinterpret_cast<unsigned char const *>(input.c_str());
  151. unsigned char const *end = begin + input.size();
  152. std::vector<char> buf(enc_size,0);
  153. unsigned char *outbuf = reinterpret_cast<unsigned char *>(&buf[0]);
  154. encode(begin,end,outbuf);
  155. result.assign(&buf[0],enc_size);
  156. return result;
  157. }
  158. } // b64url
  159. } // cppcms