Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. static const char MimeBase64[] = {
  2. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  3. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  4. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  5. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  6. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  7. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  8. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  9. '4', '5', '6', '7', '8', '9', '+', '/'
  10. };
  11.  
  12. static int DecodeMimeBase64[256] = {
  13. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
  14. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
  15. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
  16. 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
  17. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
  18. 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
  19. -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
  20. 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
  21. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
  22. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
  23. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
  24. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
  25. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
  26. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
  27. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
  28. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
  29. };
  30.  
  31. typedef union {
  32. struct {
  33. unsigned char c1, c2, c3;
  34. };
  35. struct {
  36. unsigned int e1 : 6, e2 : 6, e3 : 6, e4 : 6;
  37. };
  38. } BF;
  39.  
  40. int endian = 0; // little : 0, big : 1
  41.  
  42. void base64e(char *src, char *result, int length) {
  43. int i, j = 0;
  44. BF temp;
  45.  
  46. if (endian == 0) { // little endian(intel)
  47. for (i = 0; i < length; i = i + 3, j = j + 4) {
  48. temp.c3 = src[i];
  49. if ((i + 1) >= length) temp.c2 = 0x00;
  50. else temp.c2 = src[i + 1];
  51. if ((i + 2) >= length) temp.c1 = 0x00;
  52. else temp.c1 = src[i + 2];
  53.  
  54. result[j] = MimeBase64[temp.e4];
  55. result[j + 1] = MimeBase64[temp.e3];
  56. result[j + 2] = MimeBase64[temp.e2];
  57. result[j + 3] = MimeBase64[temp.e1];
  58.  
  59. if ((i + 1) >= length) result[j + 2] = '=';
  60. if ((i + 2) >= length) result[j + 3] = '=';
  61. }
  62. }
  63. else { // big endian(sun)
  64. for (i = 0; i < length; i = i + 3, j = j + 4) {
  65. temp.c1 = src[i];
  66. if ((i + 1) >= length) temp.c2 = 0x00;
  67. else temp.c2 = src[i + 1];
  68. if ((i + 2) >= length) temp.c3 = 0x00;
  69. else temp.c3 = src[i + 2];
  70.  
  71. result[j] = MimeBase64[temp.e4];
  72. result[j + 1] = MimeBase64[temp.e3];
  73. result[j + 2] = MimeBase64[temp.e2];
  74. result[j + 3] = MimeBase64[temp.e1];
  75.  
  76. if ((i + 1) >= length) result[j + 2] = '=';
  77. if ((i + 2) >= length) result[j + 3] = '=';
  78. }
  79. }
  80. }
  81.  
  82. void base64d(char *src, char *result, int *length) {
  83. int i, j = 0, src_length, blank = 0;
  84. BF temp;
  85.  
  86. src_length = strlen(src);
  87.  
  88. if (endian == 0) { // little endian(intel)
  89. for (i = 0; i < src_length; i = i + 4, j = j + 3) {
  90. temp.e4 = DecodeMimeBase64[src[i]];
  91. temp.e3 = DecodeMimeBase64[src[i + 1]];
  92. if (src[i + 2] == '=') {
  93. temp.e2 = 0x00;
  94. blank++;
  95. }
  96. else temp.e2 = DecodeMimeBase64[src[i + 2]];
  97. if (src[i + 3] == '=') {
  98. temp.e1 = 0x00;
  99. blank++;
  100. }
  101. else temp.e1 = DecodeMimeBase64[src[i + 3]];
  102.  
  103. result[j] = temp.c3;
  104. result[j + 1] = temp.c2;
  105. result[j + 2] = temp.c1;
  106. }
  107. }
  108. else { // big endian(sun)
  109. for (i = 0; i < src_length; i = i + 4, j = j + 3) {
  110. temp.e4 = DecodeMimeBase64[src[i]];
  111. temp.e3 = DecodeMimeBase64[src[i + 1]];
  112. if (src[i + 2] == '=') {
  113. temp.e2 = 0x00;
  114. blank++;
  115. }
  116. else temp.e2 = DecodeMimeBase64[src[i + 2]];
  117. if (src[i + 3] == '=') {
  118. temp.e1 = 0x00;
  119. blank++;
  120. }
  121. else temp.e1 = DecodeMimeBase64[src[i + 3]];
  122.  
  123. result[j] = temp.c1;
  124. result[j + 1] = temp.c2;
  125. result[j + 2] = temp.c3;
  126. }
  127. }
  128. *length = j - blank;
  129. }
  130.  
  131.  
  132. void CMFCApplication1Dlg::OnBnClickedButton1()
  133. {
  134. // TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다.
  135.  
  136. char str1[] = "테스트문자열입니다.ABCabc123,./";
  137. char str2[] = "7YWM7Iqk7Yq466y47J6Q7Je07J6F64uI64ukLkFCQ2FiYzEyMywuLw==";
  138. char *result;
  139. int src_size;
  140. struct timespec start, end;
  141.  
  142. TCHAR filePath[] = L"D:\\project\\WindowsFormsApp1\\WindowsFormsApp1\\bin\\Debug\\test.png";
  143. TCHAR filePath2[] = L"D:\\project\\WindowsFormsApp1\\WindowsFormsApp1\\bin\\Debug\\test2.png";
  144.  
  145. CFile oFile;
  146. if (oFile.Open(filePath, CFile::modeRead) == FALSE)
  147. return;
  148.  
  149. long iFileSiz = oFile.GetLength(); // Getting is the content length
  150. BYTE* pData = new BYTE[iFileSiz];
  151.  
  152. oFile.Read(pData, iFileSiz); // Reading file content
  153.  
  154. pData[iFileSiz] = '\0'; // Add last character as NULL
  155.  
  156. oFile.Close();
  157.  
  158. src_size = iFileSiz;
  159. size_t size = (4 * (src_size / 3)) + (src_size % 3 ? 4 : 0);
  160. result = (char *)malloc(size);
  161.  
  162. base64e((char*)pData, result, src_size);
  163.  
  164. //File Save
  165. CFile cfile;
  166. cfile.Open(_T("d:\\Write_File.dat"), CFile::modeCreate |
  167. CFile::modeReadWrite);
  168.  
  169. cfile.Write(result, size);
  170. cfile.Flush();
  171. cfile.Close();
  172.  
  173. free(result);
  174.  
  175.  
  176. //src_size = strlen(str2);
  177. //result = (char *)malloc(3 * (src_size / 4));
  178. //base64d(str2, result, &src_size);
  179. //free(result);
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement