/Base64/Base64.cpp

https://gitlab.com/swinkelhofer/ownApps · C++ · 338 lines · 311 code · 16 blank · 11 comment · 95 complexity · 8cbba61054ad45a36b56e68738e0b9b3 MD5 · raw file

  1. #include "Base64.h"
  2. #include <QtGui>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string>
  8. #include <ctype.h>
  9. #include <windows.h>
  10. char code_table[64] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  11. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  12. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  13. 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
  14. Base64::Base64(QWidget *parent):QDialog(parent)
  15. {
  16. input2output = new QPushButton("Convert Input to Output");
  17. input2file = new QPushButton("Convert Input to File");
  18. file2output = new QPushButton("Convert File to Output");
  19. file2file = new QPushButton("Convert File to File");
  20. sourceformat = new QComboBox;
  21. sourceformat->addItem("Base64");
  22. sourceformat->addItem("Unicode");
  23. destinationformat = new QComboBox;
  24. destinationformat->addItem("Unicode");
  25. destinationformat->addItem("Base64");
  26. input = new QTextEdit;
  27. output = new QTextEdit;
  28. QVBoxLayout *leftlayout = new QVBoxLayout;
  29. leftlayout->addWidget(new QLabel("<b>Input:</b>"));
  30. leftlayout->addWidget(input);
  31. leftlayout->addWidget(new QLabel("Input Format:"));
  32. leftlayout->addWidget(sourceformat);
  33. leftlayout->addWidget(new QLabel("Output Format:"));
  34. leftlayout->addWidget(destinationformat);
  35. leftlayout->addWidget(new QLabel("<b>Output:</b>"));
  36. leftlayout->addWidget(output);
  37. QVBoxLayout *rightlayout = new QVBoxLayout;
  38. rightlayout->addStretch();
  39. rightlayout->addWidget(input2output);
  40. rightlayout->addWidget(input2file);
  41. rightlayout->addWidget(file2output);
  42. rightlayout->addWidget(file2file);
  43. rightlayout->addStretch();
  44. QHBoxLayout *mainlayout = new QHBoxLayout;
  45. mainlayout->addLayout(leftlayout);
  46. mainlayout->addLayout(rightlayout);
  47. setLayout(mainlayout);
  48. connect(input2output, SIGNAL(clicked()), this, SLOT(transform()));
  49. connect(sourceformat, SIGNAL(currentIndexChanged(int)), this, SLOT(showOrHide(int)));
  50. connect(destinationformat, SIGNAL(currentIndexChanged(int)), this, SLOT(showOrHide(int)));
  51. connect(input2file, SIGNAL(clicked()), this, SLOT(transform2file()));
  52. connect(file2output, SIGNAL(clicked()), this, SLOT(transform2output()));
  53. connect(file2file, SIGNAL(clicked()), this, SLOT(transformfile2file()));
  54. }
  55. void Base64::transform()
  56. {
  57. if(sourceformat->currentIndex() == 0 && destinationformat->currentIndex() == 0)
  58. {
  59. std::string intext = input->toPlainText().toStdString();
  60. output->setText(QString::fromStdString(base2unicode(intext)));
  61. }
  62. else if(sourceformat->currentIndex() == 1 && destinationformat->currentIndex() == 1)
  63. {
  64. std::string intext = input->toPlainText().toStdString();
  65. output->setText(QString::fromStdString(unicode2base(intext)));
  66. }
  67. }
  68. void Base64::transform2file()
  69. {
  70. if(sourceformat->currentIndex() == 0 && destinationformat->currentIndex() == 0)
  71. {
  72. base2file();
  73. }
  74. else if(sourceformat->currentIndex() == 1 && destinationformat->currentIndex() == 1)
  75. {
  76. unicode2file();
  77. }
  78. }
  79. void Base64::transform2output()
  80. {
  81. QMessageBox msgBox;
  82. msgBox.setText("File2Output");
  83. msgBox.exec();
  84. }
  85. void Base64::transformfile2file()
  86. {
  87. /*std::fstream in;
  88. in.open(QFileDialog::getSaveFileName(this, "Datei öffnen", QDir::currentPath(), "Alle Dateien (*.*)").toStdString().c_str(), std::ios_base::in | std::ios_base::binary);
  89. std::fstream out;
  90. out.open(QFileDialog::getSaveFileName(this, "Speichern unter", QDir::currentPath(), "Alle Dateien (*.*)").toStdString().c_str(), std::ios_base::out | std::ios_base::binary);
  91. */
  92. if(sourceformat->currentIndex() == 0 && destinationformat->currentIndex() == 0)
  93. {
  94. FILE *in = fopen(QFileDialog::getOpenFileName(this, "Datei öffnen", QDir::currentPath(), "Alle Dateien (*.*)").toStdString().c_str(), "rb");
  95. FILE *out = fopen(QFileDialog::getSaveFileName(this, "Speichern unter", QDir::currentPath(), "Alle Dateien (*.*)").toStdString().c_str(), "w+b");
  96. if(in == NULL || out == NULL)
  97. {
  98. QMessageBox msgBox;
  99. msgBox.setText("Error opening Files");
  100. msgBox.exec();
  101. }
  102. char quart[4];
  103. int sign[4] = {0,0,0,0};
  104. while(!feof(in))
  105. {
  106. fread(quart, 1, 4, in);
  107. for(int i = 0; i < 64; i++)
  108. {
  109. if(quart[0] == code_table[i])
  110. sign[0] = i;
  111. if(quart[1] == code_table[i])
  112. sign[1] = i;
  113. if(quart[2] == code_table[i])
  114. sign[2] = i;
  115. if(quart[3] == code_table[i])
  116. sign[3] = i;
  117. }
  118. int i = 3;
  119. for(; i >= 0; --i)
  120. {
  121. if(quart[i] != '=')
  122. {
  123. return;
  124. }
  125. }
  126. if(i == 3)
  127. {
  128. fwrite((char*)((sign[0] << 2) + (sign[1] >> 4)), 1, 1, out);
  129. fwrite((char*)(((sign[1] & 0xf) << 4) + (sign[2] >> 2)), 1, 1, out);
  130. fwrite((char*)(((sign[2] & 0x3) << 6) + sign[3]), 1, 1, out);
  131. }
  132. else if(i == 2)
  133. {
  134. fwrite((char*)((sign[0] << 2) + (sign[1] >> 4)), 1, 1, out);
  135. fwrite((char*)(((sign[1] & 0xf) << 4) + (sign[2] >> 2)), 1, 1, out);
  136. }
  137. else if(i == 1)
  138. {
  139. fwrite((char*)((sign[0] << 2) + (sign[1] >> 4)), 1, 1, out);
  140. }
  141. }
  142. fclose(in);
  143. fclose(out);
  144. }
  145. }
  146. std::string Base64::base2unicode(std::string base64)
  147. {
  148. for(int i = 0; i < (int)base64.length(); i++)
  149. {
  150. if(!isprint(base64[i]))
  151. base64.erase(i, 1);
  152. }
  153. int fill = base64.length()-1;
  154. while(base64[fill] == '=')
  155. fill--;
  156. fill = base64.length() - 1 - fill;
  157. std::string unicode = "";
  158. char quart[4];
  159. int sign[4] = {0,0,0,0};
  160. for(int i = 0; i < (int)base64.length(); i+=4)
  161. {
  162. quart[0] = base64[i];
  163. quart[1] = base64[i+1];
  164. quart[2] = base64[i+2];
  165. quart[3] = base64[i+3];
  166. for(int i = 0; i < 64; i++)
  167. {
  168. if(quart[0] == code_table[i])
  169. sign[0] = i;
  170. if(quart[1] == code_table[i])
  171. sign[1] = i;
  172. if(quart[2] == code_table[i])
  173. sign[2] = i;
  174. if(quart[3] == code_table[i])
  175. sign[3] = i;
  176. }
  177. unicode += (char)((sign[0] << 2) + (sign[1] >> 4));
  178. unicode += (char)(((sign[1] & 0xf) << 4) + (sign[2] >> 2));
  179. unicode += (char)(((sign[2] & 0x3) << 6) + sign[3]);
  180. }
  181. return unicode.substr(0, unicode.length() - fill);
  182. }
  183. void Base64::base2file()
  184. {
  185. std::string base64 = input->toPlainText().toStdString();
  186. for(int i = 0; i < (int)base64.length(); i++)
  187. {
  188. if(!isprint(base64[i]))
  189. base64.erase(i, 1);
  190. }
  191. int fill = base64.length()-1;
  192. while(base64[fill] == '=')
  193. fill--;
  194. fill = base64.length() - 1 - fill;
  195. std::string unicode = "";
  196. char quart[4];
  197. int sign[4] = {0,0,0,0};
  198. std::fstream out;
  199. out.open(QFileDialog::getSaveFileName(this, "Speichern unter", QDir::currentPath(), "Alle Dateien (*.*)").toStdString().c_str(), std::ios_base::out | std::ios_base::binary);
  200. for(int i = 0; i < (int)base64.length()-4; i+=4)
  201. {
  202. quart[0] = base64[i];
  203. quart[1] = base64[i+1];
  204. quart[2] = base64[i+2];
  205. quart[3] = base64[i+3];
  206. for(int i = 0; i < 64; i++)
  207. {
  208. if(quart[0] == code_table[i])
  209. sign[0] = i;
  210. if(quart[1] == code_table[i])
  211. sign[1] = i;
  212. if(quart[2] == code_table[i])
  213. sign[2] = i;
  214. if(quart[3] == code_table[i])
  215. sign[3] = i;
  216. }
  217. //unicode += (char)((sign[0] << 2) + (sign[1] >> 4));
  218. //unicode += (char)(((sign[1] & 0xf) << 4) + (sign[2] >> 2));
  219. //unicode += (char)(((sign[2] & 0x3) << 6) + sign[3]);
  220. out << (char)((sign[0] << 2) + (sign[1] >> 4));
  221. out << (char)(((sign[1] & 0xf) << 4) + (sign[2] >> 2));
  222. out << (char)(((sign[2] & 0x3) << 6) + sign[3]);
  223. }
  224. int l = base64.length()-4;
  225. quart[0] = base64[l];
  226. quart[1] = base64[l+1];
  227. quart[2] = base64[l+2];
  228. quart[3] = base64[l+3];
  229. for(int i = 0; i < 64; i++)
  230. {
  231. if(quart[0] == code_table[i])
  232. sign[0] = i;
  233. if(quart[1] == code_table[i])
  234. sign[1] = i;
  235. if(quart[2] == code_table[i])
  236. sign[2] = i;
  237. if(quart[3] == code_table[i])
  238. sign[3] = i;
  239. }
  240. //unicode += (char)((sign[0] << 2) + (sign[1] >> 4));
  241. //unicode += (char)(((sign[1] & 0xf) << 4) + (sign[2] >> 2));
  242. //unicode += (char)(((sign[2] & 0x3) << 6) + sign[3]);
  243. quart[0] = (char)((sign[0] << 2) + (sign[1] >> 4));
  244. quart[1] = (char)(((sign[1] & 0xf) << 4) + (sign[2] >> 2));
  245. quart[2] = (char)(((sign[2] & 0x3) << 6) + sign[3]);
  246. for(int i = 0; i < (3 - fill); i++)
  247. {
  248. out << quart[i];
  249. }
  250. }
  251. std::string Base64::unicode2base(std::string unicode)
  252. {
  253. std::string base64 = "";
  254. int length = 0;
  255. while(unicode.length() % 3 != 0)
  256. {
  257. unicode += (char) 0x0;
  258. length++;
  259. }
  260. char triple[3];
  261. for(int i = 0; i < (int) unicode.length(); i+=3)
  262. {
  263. triple[0] = unicode[i];
  264. triple[1] = unicode[i+1];
  265. triple[2] = unicode[i+2];
  266. base64 += code_table[((unsigned char)triple[0] >> 2)];
  267. base64 += code_table[(((unsigned char)triple[1] >> 4) + (((unsigned char)triple[0] & 0x3) << 4))];
  268. base64 += code_table[((((unsigned char)triple[1] & 0xf) << 2) + ((unsigned char)triple[2] >> 6))];
  269. base64 += code_table[(unsigned char)triple[2] & 0x3f];
  270. }
  271. for(int i = 0; i < length; i++)
  272. {
  273. base64[base64.length() - 1 - i] = '=';
  274. }
  275. return base64;
  276. }
  277. void Base64::unicode2file()
  278. {
  279. std::string unicode = input->toPlainText().toStdString();
  280. std::string base64 = "";
  281. int length = 0;
  282. while(unicode.length() % 3 != 0)
  283. {
  284. unicode += (char) 0x0;
  285. length++;
  286. }
  287. char triple[3];
  288. for(int i = 0; i < (int) unicode.length(); i+=3)
  289. {
  290. triple[0] = unicode[i];
  291. triple[1] = unicode[i+1];
  292. triple[2] = unicode[i+2];
  293. base64 += code_table[((unsigned char)triple[0] >> 2)];
  294. base64 += code_table[(((unsigned char)triple[1] >> 4) + (((unsigned char)triple[0] & 0x3) << 4))];
  295. base64 += code_table[((((unsigned char)triple[1] & 0xf) << 2) + ((unsigned char)triple[2] >> 6))];
  296. base64 += code_table[(unsigned char)triple[2] & 0x3f];
  297. }
  298. for(int i = 0; i < length; i++)
  299. {
  300. base64[base64.length() - 1 - i] = '=';
  301. }
  302. std::fstream out;
  303. out.open(QFileDialog::getSaveFileName(this, "Speichern unter", QDir::currentPath(), "Alle Dateien (*.*)").toStdString().c_str(), std::ios_base::out | std::ios_base::binary);
  304. out << base64;
  305. }
  306. void Base64::showOrHide(int i)
  307. {
  308. if(sourceformat->currentIndex() == destinationformat->currentIndex())
  309. {
  310. input2output->setEnabled(true);
  311. input2file->setEnabled(true);
  312. file2output->setEnabled(true);
  313. file2file->setEnabled(true);
  314. }
  315. else
  316. {
  317. input2file->setEnabled(false);
  318. input2output->setEnabled(false);
  319. file2output->setEnabled(false);
  320. file2file->setEnabled(false);
  321. }
  322. }