Base64.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /************************************************
  2. * *
  3. * CBase64.h *
  4. * Base 64 de- and encoding class *
  5. * *
  6. * ============================================ *
  7. * *
  8. * This class was written on 28.05.2003 *
  9. * by Jan Raddatz [jan-raddatz@web.de] *
  10. * *
  11. * ============================================ *
  12. * *
  13. * Copyright (c) by Jan Raddatz *
  14. * This class was published @ codeguru.com *
  15. * 28.05.2003 *
  16. * *
  17. ************************************************/
  18. #pragma once
  19. #include <afx.h>
  20. #include <stdlib.h>
  21. #include <math.h>
  22. #include <memory.h>
  23. const static unsigned int MAX_LINE_LENGTH = 76;
  24. const static char BASE64_ALPHABET [64] =
  25. {
  26. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', // 0 - 9
  27. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', // 10 - 19
  28. 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', // 20 - 29
  29. 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 30 - 39
  30. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', // 40 - 49
  31. 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', // 50 - 59
  32. '8', '9', '+', '/' // 60 - 63
  33. };
  34. const static char BASE64_DEALPHABET [128] =
  35. {
  36. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 9
  37. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10 - 19
  38. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20 - 29
  39. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30 - 39
  40. 0, 0, 0, 62, 0, 0, 0, 63, 52, 53, // 40 - 49
  41. 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, // 50 - 59
  42. 0, 61, 0, 0, 0, 0, 1, 2, 3, 4, // 60 - 69
  43. 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 70 - 79
  44. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 80 - 89
  45. 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, // 90 - 99
  46. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // 100 - 109
  47. 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, // 110 - 119
  48. 49, 50, 51, 0, 0, 0, 0, 0 // 120 - 127
  49. };
  50. enum
  51. {
  52. UNABLE_TO_OPEN_INPUT_FILE,
  53. UNABLE_TO_OPEN_OUTPUT_FILE,
  54. UNABLE_TO_CREATE_OUTPUTBUFFER
  55. };
  56. class CBase64
  57. {
  58. public:
  59. CBase64 ();
  60. unsigned int CalculateRecquiredEncodeOutputBufferSize (unsigned int p_InputByteCount);
  61. unsigned int CalculateRecquiredDecodeOutputBufferSize (char* p_pInputBufferString);
  62. void EncodeByteTriple (char* p_pInputBuffer, unsigned int InputCharacters, char* p_pOutputBuffer);
  63. unsigned int DecodeByteQuartet (char* p_pInputBuffer, char* p_pOutputBuffer);
  64. void EncodeBuffer (char* p_pInputBuffer, unsigned int p_InputBufferLength, char*p_pOutputBufferString);
  65. unsigned int DecodeBuffer (char* p_pInputBufferString, char* p_pOutputBuffer);
  66. unsigned int CreateMatchingEncodingBuffer (unsigned int p_InputByteCount, char** p_ppEncodingBuffer);
  67. unsigned int CreateMatchingDecodingBuffer (char* p_pInputBufferString, char** p_ppDecodingBuffer);
  68. unsigned int EncodeFile (CString p_pSourceFileName, CString p_pEncodedFileName);
  69. unsigned int DecodeFile (CString p_pSourceFileName, CString p_pDecodedFileName);
  70. };