Leaked source code of windows server 2003
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.

122 lines
3.7 KiB

  1. /*--------------------------------------------------------------------------*
  2. *
  3. * Microsoft Windows
  4. * Copyright (C) Microsoft Corporation, 1999 - 1999
  5. *
  6. * File: base64.cpp
  7. *
  8. * Contents: Implements encoding / decoding table for base64 format
  9. *
  10. * History: 17-Dec-99 audriusz Created
  11. *
  12. *--------------------------------------------------------------------------*/
  13. #include <windows.h>
  14. #include <comdef.h>
  15. #include <memory.h>
  16. #include "base64.h"
  17. /*+-------------------------------------------------------------------------*
  18. *
  19. * TABLE base64_table::_six2pr64
  20. *
  21. * PURPOSE: for conversion from binary to base64
  22. *
  23. *+-------------------------------------------------------------------------*/
  24. BYTE base64_table::_six2pr64[64] =
  25. {
  26. 'A','B','C','D','E','F','G','H','I','J','K','L','M',
  27. 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  28. 'a','b','c','d','e','f','g','h','i','j','k','l','m',
  29. 'n','o','p','q','r','s','t','u','v','w','x','y','z',
  30. '0','1','2','3','4','5','6','7','8','9','+','/'
  31. };
  32. /*+-------------------------------------------------------------------------*
  33. *
  34. * TABLE base64_table::_six2pr64
  35. *
  36. * PURPOSE: for conversion from base64 to binary
  37. * [filled by base64_table::base64_table()]
  38. *
  39. *+-------------------------------------------------------------------------*/
  40. BYTE base64_table::_pr2six[256];
  41. /*+-------------------------------------------------------------------------*
  42. *
  43. * METHOD: base64_table::base64_table
  44. *
  45. * PURPOSE: c-tor. fills the table
  46. *
  47. *+-------------------------------------------------------------------------*/
  48. base64_table::base64_table()
  49. {
  50. memset(_pr2six,-1,sizeof(_pr2six));
  51. // Build up the reverse index from base64 characters to values
  52. for (int i = 0; i < sizeof(_six2pr64)/sizeof(_six2pr64[0]); i++)
  53. _pr2six[_six2pr64[i]] = (BYTE)i;
  54. }
  55. /*+-------------------------------------------------------------------------*
  56. *
  57. * METHOD: base64_table::decode
  58. *
  59. * PURPOSE: decodes 0-3 bytes of data ( as much as available )
  60. *
  61. *+-------------------------------------------------------------------------*/
  62. bool base64_table::decode(LPCOLESTR &src, BYTE * &dest)
  63. {
  64. BYTE Inputs[4] = { 0, 0, 0, 0 };
  65. int nChars = 0;
  66. // force table initialization on first call
  67. static base64_table table_init;
  68. // collect 4 characters if possible.
  69. while (*src && *src != '=' && nChars < 4)
  70. {
  71. BYTE bt = table_init.map2six(static_cast<BYTE>(*src++));
  72. if (bt != 0xff)
  73. Inputs[nChars++] = bt;
  74. }
  75. dest += table_init.decode4(Inputs, nChars, dest);
  76. return (nChars == 4);
  77. }
  78. /*+-------------------------------------------------------------------------*
  79. *
  80. * METHOD: base64_table::encode
  81. *
  82. * PURPOSE: encodes 1-3 bytes of data. pads if the last set
  83. *
  84. *+-------------------------------------------------------------------------*/
  85. void base64_table::encode(const BYTE * &src, DWORD &cbInput, LPOLESTR &dest)
  86. {
  87. BYTE chr0 = src[0];
  88. BYTE chr1 = cbInput > 1 ? src[1] : 0;
  89. BYTE chr2 = cbInput > 2 ? src[2] : 0;
  90. *(dest++) = _six2pr64[chr0 >> 2]; // c1
  91. *(dest++) = _six2pr64[((chr0 << 4) & 060) | ((chr1 >> 4) & 017)]; // c2
  92. *(dest++) = _six2pr64[((chr1 << 2) & 074) | ((chr2 >> 6) & 03) ]; // c3
  93. *(dest++) = _six2pr64[chr2 & 077]; // c4
  94. src += 3;
  95. if (cbInput == 1)
  96. {
  97. *(dest-1) = '=';
  98. *(dest-2) = '=';
  99. cbInput = 0;
  100. }
  101. else if (cbInput == 2)
  102. {
  103. *(dest-1) = '=';
  104. cbInput = 0;
  105. }
  106. else
  107. cbInput -= 3;
  108. if (!cbInput)
  109. *dest = 0;
  110. }