paperbak.h 18 KB

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