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.

73 lines
2.4 KiB

  1. /*--------------------------------------------------------------------------*
  2. *
  3. * Microsoft Windows
  4. * Copyright (C) Microsoft Corporation, 1999 - 1999
  5. *
  6. * File: base64.h
  7. *
  8. * Contents: Implements encoding / decoding table for base64 format
  9. *
  10. * History: 17-Dec-99 audriusz Created
  11. *
  12. *--------------------------------------------------------------------------*/
  13. /*+-------------------------------------------------------------------------*
  14. * class base64_table
  15. *
  16. * PURPOSE: Class maintains Base64 conversion templates. It exposes static methods
  17. * to both encode and decode Base64 data
  18. *
  19. * USAGE: call static methods to encode/decode one piece of data (24 bites or less)
  20. *
  21. *+-------------------------------------------------------------------------*/
  22. class base64_table
  23. {
  24. protected:
  25. static BYTE _six2pr64[64];
  26. static BYTE _pr2six[256];
  27. public:
  28. base64_table();
  29. int decode4(BYTE * src, int nChars, BYTE * dest);
  30. BYTE map2six(BYTE bt);
  31. // static functions used for zero terminated LPOLESTR format data only
  32. static void encode(const BYTE * &src, DWORD &cbInput, LPOLESTR &dest);
  33. static bool decode(LPCOLESTR &src, BYTE * &dest);
  34. };
  35. /*+-------------------------------------------------------------------------*
  36. *
  37. * METHOD: base64_table::map2six
  38. *
  39. * PURPOSE: maps symbols to 6bit value if smb is valid, 0xff else
  40. *
  41. *+-------------------------------------------------------------------------*/
  42. inline BYTE base64_table::map2six(BYTE bt)
  43. {
  44. return (bt > 255 ? 0xff : _pr2six[bt]);
  45. }
  46. /*+-------------------------------------------------------------------------*
  47. *
  48. * METHOD: base64_table::decode4
  49. *
  50. * PURPOSE: glues 3 bytes from stored 6bit values
  51. *
  52. * NOTE: gluing is done in place - data pointed by src is destroyed
  53. *
  54. *+-------------------------------------------------------------------------*/
  55. inline int base64_table::decode4(BYTE * src, int nChars, BYTE * dest)
  56. {
  57. // glue to form 3 full bytes
  58. src[0] <<= 2; src[0] |= (src[1] >> 4);
  59. src[1] <<= 4; src[1] |= (src[2] >> 2);
  60. src[2] <<= 6; src[2] |= (src[3] );
  61. // now store as many bytes as have complete set of bites;
  62. // int nFull = nChars*6/8; // it actually boils to 0 or nChars - 1, whichever is bigger
  63. for (int i=0; i< nChars-1; i++)
  64. *dest++ = src[i];
  65. return nChars ? nChars - 1 : 0;
  66. }