(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.

paperbak.h 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // THIS SOFTWARE IS FREE! //
  4. // //
  5. // This program is free software; you can redistribute it and/or modify it //
  6. // under the terms of the GNU General Public License as published by the Free //
  7. // Software Foundation; either version 2 of the License, or (at your option) //
  8. // any later version. This program is distributed in the hope that it will be //
  9. // useful, but WITHOUT ANY WARRANTY; without even the implied warranty of //
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General //
  11. // Public License (http://www.fsf.org/copyleft/gpl.html) for more details. //
  12. // //
  13. ////////////////////////////////////////////////////////////////////////////////
  14. #include <stdio.h>
  15. #include <ctype.h>
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <time.h>
  19. #include <unistd.h>
  20. #if defined(_WIN32) || defined(__CYGWIN__)
  21. #include <windows.h>
  22. #endif
  23. #include "bzlib.h"
  24. #include "Bitmap.h"
  25. #include "FileAttributes.h"
  26. #include "Borland.h"
  27. ////////////////////////////////////////////////////////////////////////////////
  28. ///////////////////////////// GENERAL DEFINITIONS //////////////////////////////
  29. // Size required by Reed-Solomon ECC
  30. #define ECC_SIZE 32
  31. // Oleh's magic numbers
  32. #define FILENAME_SIZE 64
  33. #define VERSIONHI 1 // Major version
  34. #define VERSIONLO 2 // Minor version
  35. #define MAINDX 800 // Max width of the main window, pixels
  36. #define MAINDY 600 // Max height of the main window, pixels
  37. #define TEXTLEN 256 // Maximal length of strings
  38. #define PASSLEN 33 // Maximal length of password, incl. 0
  39. #define USE_SHA1 1
  40. #define AESKEYLEN 24 // AES key length in bytes (16, 24, or 32)
  41. typedef unsigned char uchar;
  42. typedef uint16_t ushort;
  43. typedef unsigned int uint;
  44. ////////////////////////////////////////////////////////////////////////////////
  45. /////////////////////////////// DATA PROPERTIES ////////////////////////////////
  46. // Don't change the definitions below! Program may crash if any modified!
  47. #define NDOT 32 // Block X and Y size, dots
  48. #define NDATA 90 // Number of data bytes in a block
  49. #define MAXSIZE 0x0FFFFF80 // Maximal (theoretical) length of file
  50. #define SUPERBLOCK 0xFFFFFFFF // Address of superblock
  51. #define NGROUP 5 // For NGROUP blocks (1..15), 1 recovery
  52. #define NGROUPMIN 2
  53. #define NGROUPMAX 10
  54. typedef struct __attribute__ ((packed)) t_data { // Block on paper
  55. uint32_t addr; // Offset of the block or special code
  56. uchar data[NDATA]; // Useful data
  57. ushort crc; // Cyclic redundancy of addr and data
  58. uchar ecc[32]; // Reed-Solomon's error correction code
  59. } t_data;
  60. #ifdef __linux__
  61. _Static_assert(sizeof(t_data)==128, "t_data not 128 bytes long");
  62. #endif
  63. #define PBM_COMPRESSED 0x01 // Paper backup is compressed
  64. #define PBM_ENCRYPTED 0x02 // Paper backup is encrypted
  65. // FILETIME is 64-bit data type, time_t typically 64-bit, but was 32-bit in
  66. // older *NIX versions. Assertion failure is likely due to this. 128 bytes
  67. // is necessary for ECC to work properly (and multiples of 16 for CRC)
  68. typedef struct __attribute__ ((packed)) t_superdata { // Id block on paper
  69. uint32_t addr; // Expecting SUPERBLOCK
  70. uint32_t datasize; // Size of (compressed) data
  71. uint32_t pagesize; // Size of (compressed) data on page
  72. uint32_t origsize; // Size of original (uncompressed) data
  73. uchar mode; // Special mode bits, set of PBM_xxx
  74. uchar attributes; // Basic file attributes
  75. ushort page; // Actual page (1-based)
  76. FileTimePortable modified; // last modify time
  77. ushort filecrc; // CRC of compressed decrypted file
  78. char name[FILENAME_SIZE]; // File name - may have all 64 chars
  79. ushort crc; // Cyclic redundancy of previous fields
  80. uchar ecc[ECC_SIZE]; // Reed-Solomon's error correction code
  81. } t_superdata;
  82. #ifdef __linux__
  83. _Static_assert(sizeof(t_superdata)==sizeof(t_data),
  84. "t_superdata is not the same size as t_data");
  85. #endif
  86. typedef struct t_block { // Block in memory
  87. uint32_t addr; // Offset of the block
  88. uint32_t recsize; // 0 for data, or length of covered data
  89. uchar data[NDATA]; // Useful data
  90. } t_block;
  91. typedef struct t_superblock { // Identification block in memory
  92. uint32_t addr; // Expecting SUPERBLOCK
  93. uint32_t datasize; // Size of (compressed) data
  94. uint32_t pagesize; // Size of (compressed) data on page
  95. uint32_t origsize; // Size of original (uncompressed) data
  96. uint32_t mode; // Special mode bits, set of PBM_xxx
  97. ushort page; // Actual page (1-based)
  98. FileTimePortable modified; // last modify time
  99. uint32_t attributes; // Basic file attributes
  100. uint32_t filecrc; // 16-bit CRC of decrypted packed file
  101. char name[FILENAME_SIZE]; // File name - may have all 64 chars
  102. int ngroup; // Actual NGROUP on the page
  103. } t_superblock;
  104. ////////////////////////////////////////////////////////////////////////////////
  105. ///////////////////////////////////// CRC //////////////////////////////////////
  106. ushort Crc16(uchar *data,int length);
  107. ////////////////////////////////////////////////////////////////////////////////
  108. ////////////////////////// REED-SOLOMON ECC ROUTINES ///////////////////////////
  109. void Encode8(uchar *data,uchar *parity,int pad);
  110. int Decode8(uchar *data, int *eras_pos, int no_eras,int pad);
  111. ////////////////////////////////////////////////////////////////////////////////
  112. /////////////////////////////////// PRINTER ////////////////////////////////////
  113. #define PACKLEN 65536 // Length of data read buffer 64 K
  114. typedef struct t_printdata { // Print control structure
  115. int step; // Next data printing step (0 - idle)
  116. char infile[MAXPATH]; // Name of input file
  117. char outbmp[MAXPATH]; // Name of output bitmap (empty: paper)
  118. FILE *hfile; // (Formerly HANDLE) file pointer
  119. FileTimePortable modified; // last modify time
  120. uint32_t attributes; // File attributes
  121. uint32_t origsize; // Original file size, bytes
  122. uint32_t readsize; // Amount of data read from file so far
  123. uint32_t datasize; // Size of (compressed) data
  124. uint32_t alignedsize; // Data size aligned to next 16 bytes
  125. uint32_t pagesize; // Size of (compressed) data on page
  126. int compression; // 0: none, 1: fast, 2: maximal
  127. int encryption; // 0: none, 1: encrypt
  128. int printheader; // Print header and footer
  129. int printborder; // Print border around bitmap
  130. int redundancy; // Redundancy
  131. uchar *buf; // Buffer for compressed file
  132. uint32_t bufsize; // Size of buf, bytes
  133. uchar *readbuf; // Read buffer, PACKLEN bytes long
  134. bz_stream bzstream; // Compression control structure
  135. int bufcrc; // 16-bit CRC of (packed) data in buf
  136. t_superdata superdata; // Identification block on paper
  137. //HDC dc; // Printer device context
  138. int frompage; // First page to print (0-based)
  139. int topage; // Last page (0-based, inclusive)
  140. int ppix; // Printer X resolution, pixels per inch
  141. int ppiy; // Printer Y resolution, pixels per inch
  142. int width; // Page width, pixels
  143. int height; // Page height, pixels
  144. //HFONT hfont6; // Font 1/6 inch high
  145. //HFONT hfont10; // Font 1/10 inch high
  146. int extratop; // Height of title line, pixels
  147. int extrabottom; // Height of info line, pixels
  148. int black; // Palette index of dots colour
  149. int borderleft; // Left page border, pixels
  150. int borderright; // Right page border, pixels
  151. int bordertop; // Top page border, pixels
  152. int borderbottom; // Bottom page border, pixels
  153. int dx,dy; // Distance between dots, pixels
  154. int px,py; // Dot size, pixels
  155. int nx,ny; // Grid dimensions, blocks
  156. int border; // Border around the data grid, pixels
  157. //FIXME bitmap file pointer needed?
  158. //HBITMAP hbmp; // Handle of memory bitmap
  159. uchar *dibbits; // Pointer to DIB bits
  160. uchar *drawbits; // Pointer to file bitmap bits
  161. uchar bmi[sizeof(BITMAPINFO)+256*sizeof(RGBQUAD)]; // Bitmap info
  162. int startdoc; // Print job started
  163. } t_printdata;
  164. int pb_resx, pb_resy; // Printer resolution, dpi (may be 0!)
  165. t_printdata pb_printdata; // Print control structure
  166. void Initializeprintsettings(void);
  167. void Closeprintsettings(void);
  168. void Setuppage(void);
  169. void Stopprinting(t_printdata *print);
  170. void Nextdataprintingstep(t_printdata *print);
  171. void Printfile(const char *path, const char *bmp);
  172. ////////////////////////////////////////////////////////////////////////////////
  173. /////////////////////////////////// DECODER ////////////////////////////////////
  174. #define M_BEST 0x00000001 // Search for best possible quality
  175. typedef struct t_procdata { // Descriptor of processed data
  176. int step; // Next data processing step (0 - idle)
  177. int mode; // Set of M_xxx
  178. uchar *data; // Pointer to bitmap
  179. int sizex; // X bitmap size, pixels
  180. int sizey; // Y bitmap size, pixels
  181. int gridxmin,gridxmax; // Rought X grid limits, pixels
  182. int gridymin,gridymax; // Rought Y grid limits, pixels
  183. int searchx0,searchx1; // X grid search limits, pixels
  184. int searchy0,searchy1; // Y grid search limits, pixels
  185. int cmean; // Mean grid intensity (0..255)
  186. int cmin,cmax; // Minimal and maximal grid intensity
  187. float sharpfactor; // Estimated sharpness correction factor
  188. float xpeak; // Base X grid line, pixels
  189. float xstep; // X grid step, pixels
  190. float xangle; // X tilt, radians
  191. float ypeak; // Base Y grid line, pixels
  192. float ystep; // Y grid step, pixels
  193. float yangle; // Y tilt, radians
  194. float blockborder; // Relative width of border around block
  195. int bufdx,bufdy; // Dimensions of block buffers, pixels
  196. uchar *buf1,*buf2; // Rotated and sharpened block
  197. int *bufx,*bufy; // Block grid data finders
  198. uchar *unsharp; // Either buf1 or buf2
  199. uchar *sharp; // Either buf1 or buf2
  200. float blockxpeak,blockypeak;// Exact block position in unsharp
  201. float blockxstep,blockystep;// Exact block dimensions in unsharp
  202. int nposx; // Number of blocks to scan in X
  203. int nposy; // Number of blocks to scan in X
  204. int posx,posy; // Next block to scan
  205. t_data uncorrected; // Data before ECC for block display
  206. t_block *blocklist; // List of blocks recognized on page
  207. t_superblock superblock; // Page header
  208. int maxdotsize; // Maximal size of the data dot, pixels
  209. int orientation; // Data orientation (-1: unknown)
  210. int ngood; // Page statistics: good blocks
  211. int nbad; // Page statistics: bad blocks
  212. int nsuper; // Page statistics: good superblocks
  213. int nrestored; // Page statistics: restored bytes
  214. } t_procdata;
  215. int pb_orientation; // Orientation of bitmap (-1: unknown)
  216. t_procdata pb_procdata; // Descriptor of processed data
  217. void Nextdataprocessingstep(t_procdata *pdata);
  218. void Freeprocdata(t_procdata *pdata);
  219. void Startbitmapdecoding(t_procdata *pdata,uchar *data,int sizex,int sizey);
  220. void Stopbitmapdecoding(t_procdata *pdata);
  221. int Decodeblock(t_procdata *pdata,int posx,int posy,t_data *result);
  222. ////////////////////////////////////////////////////////////////////////////////
  223. //////////////////////////////// FILE PROCESSOR ////////////////////////////////
  224. #define NFILE 5 // Max number of simultaneous files
  225. typedef struct t_fproc { // Descriptor of processed file
  226. int busy; // In work
  227. // General file data.
  228. char name[64]; // File name - may have all 64 chars
  229. FileTimePortable modified; // last modify time
  230. uint32_t attributes; // Basic file attrributes
  231. uint32_t datasize; // Size of (compressed) data
  232. uint32_t pagesize; // Size of (compressed) data on page
  233. uint32_t origsize; // Size of original (uncompressed) data
  234. uint32_t mode; // Special mode bits, set of PBM_xxx
  235. int npages; // Total number of pages
  236. uint32_t filecrc; // 16-bit CRC of decrypted packed file
  237. // Properties of currently processed page.
  238. int page; // Currently processed page
  239. int ngroup; // Actual NGROUP on the page
  240. uint32_t minpageaddr; // Minimal address of block on page
  241. uint32_t maxpageaddr; // Maximal address of block on page
  242. // Gathered data.
  243. int nblock; // Total number of data blocks
  244. int ndata; // Number of decoded blocks so far
  245. uchar *datavalid; // 0:data invalid, 1:valid, 2:recovery
  246. uchar *data; // Gathered data
  247. // Statistics.
  248. int goodblocks; // Total number of good blocks read
  249. int badblocks; // Total number of unreadable blocks
  250. uint32_t restoredbytes; // Total number of bytes restored by ECC
  251. int recoveredblocks; // Total number of recovered blocks
  252. int rempages[8]; // 1-based list of remaining pages
  253. } t_fproc;
  254. t_fproc pb_fproc[NFILE]; // Processed file
  255. void Closefproc(int slot);
  256. int Startnextpage(t_superblock *superblock);
  257. int Addblock(t_block *block,int slot);
  258. int Finishpage(int slot,int ngood,int nbad,uint32_t nrestored);
  259. int Saverestoredfile(int slot,int force);
  260. ////////////////////////////////////////////////////////////////////////////////
  261. /////////////////////////////////// SCANNER ////////////////////////////////////
  262. int Decodebitmap(char *path);
  263. ////////////////////////////////////////////////////////////////////////////////
  264. //////////////////////////////// USER INTERFACE ////////////////////////////////
  265. char pb_infile[MAXPATH]; // Last selected file to read
  266. char pb_outbmp[MAXPATH]; // Last selected bitmap to save
  267. char pb_inbmp[MAXPATH]; // Last selected bitmap to read
  268. char pb_outfile[MAXPATH]; // Last selected data file to save
  269. char pb_password[PASSLEN]; // Encryption password
  270. int pb_dpi; // Dot raster, dots per inch
  271. int pb_dotpercent; // Dot size, percent of dpi
  272. int pb_compression; // 0: none, 1: fast, 2: maximal
  273. int pb_redundancy; // Redundancy (NGROUPMIN..NGROUPMAX)
  274. int pb_printheader; // Print header and footer
  275. int pb_printborder; // Border around bitmap
  276. int pb_autosave; // Autosave completed files
  277. int pb_bestquality; // Determine best quality
  278. int pb_encryption; // Encrypt data before printing
  279. int pb_opentext; // Enter passwords in open text
  280. int pb_marginunits; // 0:undef, 1:inches, 2:millimeters
  281. int pb_marginleft; // Left printer page margin
  282. int pb_marginright; // Right printer page margin
  283. int pb_margintop; // Top printer page margin
  284. int pb_marginbottom; // Bottom printer page margin
  285. ////////////////////////////////////////////////////////////////////////////////
  286. ////////////////////////////// SERVICE FUNCTIONS ///////////////////////////////
  287. void Reporterror(const char *input);
  288. void Message(const char *input, int progress);
  289. // Formerly standard case insentitive cstring compare
  290. int strnicmp (const char *str1, const char *str2, size_t len);
  291. // returns 0 on success, -1 on failure
  292. int Getpassword();
  293. int max (int a, int b);
  294. int min (int a, int b);
  295. ////////////////////////////////////////////////////////////////////////////////
  296. ////////////////////////// WINDOWS SERVICE FUNCTIONS ///////////////////////////
  297. #if defined(_WIN32) || defined(__CYGWIN__)
  298. // Converts file date and time into the text according to system defaults and
  299. // places into the string s of length n. Returns number of characters in s.
  300. int Filetimetotext(FILETIME *fttime,char *s,int n);
  301. void print_filetime(FILETIME ftime);
  302. #endif