(C) Relatively high density file backups on paper. Cross-platform CLI port of Ollydbg's Paperback from Windows and Borland C.
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.

main.cpp 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: main.cpp
  5. *
  6. * Description: Cross-platform command line version of Oleh Yuchuk's Paperbak, a
  7. * (relatively) high-density paper backup solution
  8. *
  9. * Version: 1.2
  10. * Created: 07/27/2017 03:04:03 PM
  11. * Revision: none
  12. * Compiler: gcc
  13. *
  14. * Author: scuti@teknik.io
  15. *
  16. * =====================================================================================
  17. */
  18. #include <stdlib.h>
  19. #include <getopt.h>
  20. #include <stdbool.h>
  21. #include "paperbak.h"
  22. #include "Resource.h"
  23. using namespace std;
  24. #define VERSIONHI 1
  25. #define VERSIONLO 2
  26. // Global forward declarations
  27. t_fproc pb_fproc[NFILE]; // Processed file
  28. int pb_resx, pb_resy; // Printer resolution, dpi (may be 0!)
  29. t_printdata pb_printdata; // Print control structure
  30. int pb_orientation; // Orientation of bitmap (-1: unknown)
  31. t_procdata pb_procdata; // Descriptor of processed data
  32. char pb_infile[MAXPATH]; // Last selected file to read
  33. char pb_outbmp[MAXPATH]; // Last selected bitmap to save
  34. char pb_inbmp[MAXPATH]; // Last selected bitmap to read
  35. char pb_outfile[MAXPATH]; // Last selected data file to save
  36. char pb_password[PASSLEN]; // Encryption password
  37. int pb_dpi; // Dot raster, dots per inch
  38. int pb_dotpercent; // Dot size, percent of dpi
  39. int pb_compression; // 0: none, 1: fast, 2: maximal
  40. int pb_redundancy; // Redundancy (NGROUPMIN..NGROUPMAX)
  41. int pb_printheader; // Print header and footer
  42. int pb_printborder; // Border around bitmap
  43. int pb_autosave; // Autosave completed files
  44. int pb_bestquality; // Determine best quality
  45. int pb_encryption; // Encrypt data before printing
  46. int pb_opentext; // Enter passwords in open text
  47. int pb_marginunits; // 0:undef, 1:inches, 2:millimeters
  48. int pb_marginleft; // Left printer page margin
  49. int pb_marginright; // Right printer page margin
  50. int pb_margintop; // Top printer page margin
  51. int pb_marginbottom; // Bottom printer page margin
  52. // Function prototypes
  53. void dhelp (const char *exe);
  54. void dversion();
  55. int arguments (int ac, char **av);
  56. int main(int argc, char ** argv) {
  57. // set values needed for cli version
  58. ::pb_autosave = 1;
  59. // set default values for vars affected by arg parsing
  60. ::pb_infile[0] = '\0';
  61. ::pb_outfile[0] = '\0';
  62. ::pb_outbmp[0] = '\0';
  63. ::pb_dpi = 200;
  64. ::pb_dotpercent = 70;
  65. ::pb_redundancy = 5;
  66. ::pb_printheader = 0;
  67. ::pb_printborder = 0;
  68. bool isEncode = arguments(argc, argv);
  69. if (isEncode) {
  70. printf ("Encoding %s to create %s\n"
  71. "DPI: %d\n"
  72. "Dot percent: %d\n"
  73. "Redundancy: 1:%d\n"
  74. "Print header/footer: %d\n"
  75. "Print border: %d\n",
  76. ::pb_infile, ::pb_outbmp,
  77. ::pb_dpi, ::pb_dotpercent, ::pb_redundancy,
  78. ::pb_printheader, ::pb_printborder);
  79. Printfile (::pb_infile, ::pb_outbmp);
  80. while (::pb_printdata.step != 0) {
  81. Nextdataprintingstep (&::pb_printdata);
  82. }
  83. }
  84. else {
  85. printf ("Decoding %s into %s\n", ::pb_infile, ::pb_outfile);
  86. Decodebitmap (::pb_infile);
  87. while (::pb_procdata.step != 0) {
  88. Nextdataprocessingstep (&::pb_procdata);
  89. }
  90. }
  91. return 0;
  92. }
  93. inline void dhelp (const char *exe) {
  94. printf("%s\n\n"
  95. "Usage:\n"
  96. "\t%s --encode -i [infile] -o [out].bmp [OPTION...]\n"
  97. "\t%s --decode -i [in].bmp -o [outfile]\n"
  98. "\t%s --decode -f [inputfolder] -o [outfile]\n\n"
  99. "\t--encode Create a bitmap from the input file\n"
  100. "\t--decode Decode an encoded bitmap/folder of bitmaps\n"
  101. "\t-i, --input File to encode to or decode from\n"
  102. "\t-o, --output Newly encoded bitmap or decoded file\n"
  103. "\t-f, --inputfolder Directory of bitmaps to decode into a single file\n"
  104. "\t-d, --dpi Dots per inch of the output bitmap (40 to 600)\n"
  105. "\t-s, --dotsize Size of the dots in bitmap as percentage of maximum dot\n"
  106. "\t size in pixels, (50 to 100)\n"
  107. "\t-r, --redundancy Data redundancy ratio of input or output bitmap as a\n"
  108. "\t reciprocal, (2 to 10)\n"
  109. "\t-n, --no-header Disable printing of file name, last modify date and time,\n"
  110. "\t file size, and page number\n"
  111. "\t-b, --border Print a black border around the page\n"
  112. "\t-v, --version Display version and information relevant to that version\n"
  113. "\t-h, --help Display all arguments and program description\n\n",
  114. "\nEncodes or decodes high-density printable file backups.",
  115. exe,
  116. exe,
  117. exe);
  118. }
  119. inline void dversion() {
  120. printf("\nPaperBack v%d.%d\n"
  121. "Copyright © 2007 Oleh Yuschuk\n\n"
  122. "----- THIS SOFTWARE IS FREE -----\n"
  123. "Released under GNU Public License (GPL 3+)\n"
  124. "Full sources available\n\n"
  125. "Reed-Solomon ECC:\n"
  126. "Copyright © 2002 Phil Karn (GPL)\n\n"
  127. "Bzip2 data compression:\n"
  128. "Copyright © 1996-2005 Julian R. Seward (see sources)\n\n"
  129. "FIPS-197 compliant AES encryption:\n"
  130. "Copyright © 2001-2004 Christophe Devine (GPL 2+)\n\n",
  131. VERSIONHI, VERSIONLO);
  132. }
  133. int arguments (int ac, char **av) {
  134. bool is_ok = true;
  135. int displayhelp = 0, displayversion = 0, isencode;
  136. struct option long_options[] = {
  137. // options that set flags
  138. {"encode", no_argument, &isencode, 1},
  139. {"decode", no_argument, &isencode, 0},
  140. // options that assign values in switch
  141. {"input", required_argument, NULL, 'i'},
  142. {"output", required_argument, NULL, 'o'},
  143. {"inputfolder", required_argument, NULL, 'f'},
  144. {"dpi", required_argument, NULL, 'd'},
  145. {"dotsize", required_argument, NULL, 's'},
  146. {"redundancy", required_argument, NULL, 'r'},
  147. {"no-header", no_argument, NULL, 'n'},
  148. {"border", no_argument, NULL, 'b'},
  149. {"version", no_argument, NULL, 'v'},
  150. {"help", no_argument, NULL, 'h'},
  151. {0, 0, 0, 0}
  152. };
  153. int c;
  154. while(is_ok) {
  155. int options_index = 0;
  156. c = getopt_long (ac, av, "i:o:f:d:s:r:nbvh", long_options, &options_index);
  157. if (c == -1) {
  158. break;
  159. }
  160. switch(c) {
  161. case 0:
  162. break;
  163. case 'i':
  164. if (optarg == NULL) {
  165. fprintf(stderr, "error: arg is NULL ! \n");
  166. is_ok = false;
  167. } else {
  168. strcpy (::pb_infile, optarg);
  169. }
  170. break;
  171. case 'o':
  172. if (optarg == NULL) {
  173. fprintf(stderr, "error: outfile arg is null \n");
  174. is_ok = false;
  175. } else {
  176. strcpy (::pb_outfile, optarg);
  177. strcpy (::pb_outbmp, optarg);
  178. }
  179. break;
  180. case 'f':
  181. if (optarg == NULL) {
  182. fprintf(stderr, "error: inputfolder arg is null \n");
  183. is_ok = false;
  184. } else {
  185. strcpy (::pb_infile, optarg);
  186. }
  187. break;
  188. case 'd':
  189. ::pb_dpi = atoi(optarg);
  190. break;
  191. case 's':
  192. ::pb_dotpercent = atoi(optarg);
  193. break;
  194. case 'r':
  195. ::pb_redundancy = atoi(optarg);
  196. break;
  197. case 'n':
  198. ::pb_printheader = !(atoi(optarg));
  199. break;
  200. case 'b':
  201. ::pb_printborder = atoi(optarg);
  202. break;
  203. case 'v':
  204. displayversion = true;
  205. break;
  206. case 'h':
  207. displayhelp = true;
  208. break;
  209. default:
  210. exit (EXIT_FAILURE);
  211. }
  212. }
  213. if (displayhelp) {
  214. dhelp (av[0]);
  215. exit(EXIT_SUCCESS);
  216. }
  217. if (displayversion) {
  218. dversion();
  219. exit(EXIT_SUCCESS);
  220. }
  221. if (strlen (::pb_infile) == 0) {
  222. fprintf (stderr, "error: no input file given\n");
  223. is_ok = false;
  224. }
  225. if (strlen (::pb_outfile) == 0) {
  226. fprintf (stderr, "error: no output file given\n");
  227. is_ok = false;
  228. }
  229. if (::pb_dotpercent < 50 || ::pb_dotpercent > 100) {
  230. fprintf (stderr, "error: invalid dotsize given\n");
  231. is_ok = false;
  232. }
  233. if (::pb_dpi < 40 || ::pb_dpi > 600) {
  234. fprintf (stderr, "error: invalid DPI given\n");
  235. is_ok = false;
  236. }
  237. if (::pb_redundancy < 2 || ::pb_redundancy > 10) {
  238. fprintf (stderr, "error: invalid redundancy given\n");
  239. is_ok = false;
  240. }
  241. if (::pb_printheader < 0 || ::pb_printheader > 1) {
  242. fprintf (stderr, "error: invalid header setting given\n");
  243. is_ok = false;
  244. }
  245. if (::pb_printborder < 0 || ::pb_printborder > 1) {
  246. fprintf (stderr, "error: invalid border setting given\n");
  247. is_ok = false;
  248. }
  249. if ( ! is_ok) {
  250. exit(EXIT_FAILURE);
  251. }
  252. return isencode;
  253. }