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.
 
 
 
 
 
 

131 lines
2.5 KiB

  1. /*
  2. * Copyright: (c) 2008 Artyom Beilise
  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 "base64.h"
  10. namespace {
  11. const unsigned char encode_6_to_8[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  12. inline unsigned char encode_8_to_6(unsigned char c)
  13. {
  14. if('A'<=c && c<='Z')
  15. return c - 'A';
  16. if('a'<=c && c<='z')
  17. return ('Z'-'A' + 1) + c - 'a';
  18. if('0'<=c && c<='9')
  19. return 2*('Z'-'A' + 1) + c - '0';
  20. if(c=='-')
  21. return 62;
  22. if(c=='_')
  23. return 63;
  24. return 0;
  25. }
  26. size_t inline bencode(unsigned const char in[3],unsigned char out[4],size_t len)
  27. {
  28. out[0] = encode_6_to_8[in[0] >> 2];
  29. if(len<=1) {
  30. out[1] = encode_6_to_8[(in[0] & 0x03) << 4];
  31. return 2;
  32. }
  33. else {
  34. out[1] = encode_6_to_8[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)];
  35. if(len<=2) {
  36. out[2] = encode_6_to_8[(in[1] & 0x0f) << 2];
  37. return 3;
  38. }
  39. else {
  40. out[2] = encode_6_to_8[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)];
  41. out[3] = encode_6_to_8[in[2] & 0x3f];
  42. return 4;
  43. }
  44. }
  45. }
  46. inline size_t bdecode(unsigned const char in8[4],unsigned char out[3],size_t len)
  47. {
  48. unsigned char in[4] = { 0 };
  49. for(unsigned i=0;i<len;i++)
  50. in[i]=encode_8_to_6(in8[i]);
  51. out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
  52. if(len==2) {
  53. return 1;
  54. }
  55. else {
  56. out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
  57. if(len==3) {
  58. return 2;
  59. }
  60. else {
  61. out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
  62. return 3;
  63. }
  64. }
  65. }
  66. } // anon namespace
  67. namespace cppcms {
  68. namespace b64url {
  69. int encoded_size(size_t s)
  70. {
  71. switch(s % 3) {
  72. case 1: return s/3*4+2;
  73. case 2: return s/3*4+3;
  74. default:
  75. return s/3*4;
  76. }
  77. }
  78. int decoded_size(size_t s)
  79. {
  80. switch(s % 4) {
  81. case 1: return -1; // invalid
  82. case 2: return s/4*3+1;
  83. case 3: return s/4*3+2;
  84. default:
  85. return s/4*3;
  86. }
  87. }
  88. unsigned char *encode(unsigned char const *begin,unsigned char const *end,unsigned char *target)
  89. {
  90. while(end - begin >=3) {
  91. bencode(begin,target,3);
  92. begin += 3;
  93. target += 4;
  94. }
  95. if(end!=begin)
  96. target+=bencode(begin,target,end-begin);
  97. return target;
  98. }
  99. unsigned char *decode(unsigned char const *begin,unsigned char const *end,unsigned char *target)
  100. {
  101. while(end - begin >=4) {
  102. bdecode(begin,target,4);
  103. begin+=4;
  104. target+=3;
  105. }
  106. if(end!=begin)
  107. target+=bdecode(begin,target,end-begin);
  108. return target;
  109. }
  110. } // b64url
  111. } // cppcms