CANON CARPS (Canon Advanced Raster Printing System) print format
================================================================
(reverse-engineered from LaserBase MF5730)

Data is organized into blocks, each with 20-byte header:
0: 0xCD - magic value
1: 0xCA - magic value
2: 0x10 - magic value
3: data type (0x00 = control data, 0x02 = print data)
4: 0x00
5: block type (0x11, 0x12, 0x13, 0x14, 0x16, 0x17, 0x18, 0x19, 0x1a)
6: 0x00
7: 0x01
8: length (high byte) of data following the header (number of bytes)
9: length (low byte)
10-19: padding (zeros)

maximum block (URB?) size is 4KB (4096B)

DOCUMENT structure:
-------------------
0x11: ???: 13 bytes: \x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00
0x12: document name: \x00\x04\x00\x11 + character count + name
0x12: user name: \x00\x06\x00\x11 + character count + name
0x12: time: \x00\x09\x7D\xE1\x43\x00\x0C\x12\x46\xEC	(UTC)
                      ^^  ^ 2014
                           ^ JANUARY
                              ^^ DAY (5bit) + DAY OF WEEK (3bit)
                                  ^^ SEPARATOR
                                      ^^ HOUR
                                          ^^ MINUTES
                                              ^^  ^^ SECONDS (6bit) + MILLISECONDS (2+8bit) (?)
0x12: ???: 10 bytes: \x00\x09\x00\x00\x07\x00\x00\x00\x00\x00
0x14: ???: 4 zero bytes
0x17: ???: 4 zero bytes
0x18: ???: 5 bytes: \x00\x2e\x82\x00\x00
0x18: image refinement: 3 bytes: \x08\x2d + \x01 (off) or \x02 (on)
0x18: toner save: 3 bytes: \x08\x5a + \x01 (off) or \x02 (on), if this block is not present, "printer default" is used
PRINT DATA
0x1a: end of page: 1 byte: \x01
0x19: 0 bytes
0x16: 0 bytes
0x13: end of document: 1 zero byte


PRINT DATA structure:
---------------------
all print data is in blocks with data type 0x02, block type 0x1a

each page begins with a header (first page has header in separate block, others don't (?)), such as:
\x01.%@.P42;600;1J;ImgColor.\.[11h.[?7;600 I.[20't.[14;;;;;;p.[?2h.[1v.[600;1;0;32;;64;0'c
P42: ???
600: 600dpi (300 = 300dpi)
1J: ???
ImgColor: ???
20't = Plain paper (15't = Plain L, 20't = Plain, 30't = Heavy, 35't = Heavy H, 40't = Transparencies, 55't = Envelope)
14 = A4, 16 = A5, 26 = B5, 30 = letter, 32 = legal, 40 = executive, 60 = env. monarch, 62 = env. com10, 64 = env. DL, 66 = env. C5, 80 = CUSTOM
	(CUSTOM: 3x5" @ 300dpi = ESC[80;1500;900;60;60;60;60p, 8.5x14" @ 300dpi = ESC[80;4200;2550;60;60;60;60p )
	(	 3x5" @ 600dpi = ESC[80;3000;1800;120;120;120;120p						)
1v: 1 copy (2v = 2 copies, 99v = 99 copies)

then page data follows in STRIPs, each STRIP in one or more blocks
each STRIP includes a header, such as:
\x01.[;4724;110;15.P
4724: strip witdh (in dots)
110: strip height (in dots)
15: ???

then COMPRESSED DATA HEADER follows (13 bytes):
0: 0x01 - magic value
1: 0x02 - magic value
2: 0x04 - magic value
3: 0x08 - magic value
4-5: 0x0000
6: 0x50 - magic value
7: 0x00
8: last strip indication (0x00 = last, 0x01 = not last)
9-10: length (little endian??) - number of following data bytes (excluding ending 0x80 byte), can span multiple blocks,
	subsequent blocks do not have this header
11-12: 0x0000


then COMPRESSED DATA follows

strip can span multiple blocks (next blocks are without any headers)
strip ends with 0x80 byte

line ends with zero byte (1111 1101)
compression is per line (repeat stream can't pass line end)


each page ends with a 2-byte block containing \x01\x0c

print data ends with a 7-byte block containing \x01\x1b\x50\x30\x4a\x1b\x5c

		in		mm		px @ 600dpi Bytes/line	px @ 300dpi Bytes/line	hwmargin (pt) @ 600dpi	hwmargin (pt) @ 300dpi
Letter:		8.50 x 11.00	215.9 x 279.4	4863 x 6363 608		2431 x 3181 304		14.22 x 14.22		14.28 x 14.28
Legal:		8.50 x 14.00	215.9 x 355.6	4863 x 8163 608		2431 x 4081 304		14.22 x 14.22		14.28 x 14.28
Executive:	7.25 x 10.50	184.1 x 267.6	4112 x 6063 516		2056 x 3031 260		14.28 x 14.22		14.28 x 14.28
A5:		5.83 x  8.27	148   x 210	3259 x 4724 408		1629 x 2362 204		14.34 x 14.28		14.40 x 14.28
B5:		7.17 x 10.12	182   x 257	4062 x 5834 508		2031 x 2917 256		14.40 x 14.28		14.40 x 14.28
A4:		8.27 x 11.69	210   x 297	4724 x 6779 592		2362 x 3389 296		14.16 x 14.16		14.28 x 14.16
Env. Monarch	3.88 x  7.50	98.5  x 190.5	2090 x 4263 264		1045 x 2131 132		14.28 x 14.22		14.28 x 14.28
Env. COM10	4.13 x  9.50	104.9 x 241.3	2241 x 5463 284		1120 x 2731 140		14.22 x 14.22		14.28 x 14.28
Env. DL		4.33 x  8.66	110   x 220	2362 x 4960 296		1181 x 2480 148		14.16 x 14.16		14.16 x 14.16
Env. C5		6.38 x  9.02	162   x 229	3590 x 5173 452		1795 x 2586 228		14.28 x 14.34		14.28 x 14.40

Bytes per line = width/8 rounded up to multiples of 4

COMPRESSED DATA format:
-----------------------
All compressed data is XORed by 0x43.
Compression operates on byte level (of image data) but the encoding is bit-level (in compressed data).
Data can be stored using 8 methods:

1. Byte immediate
Used as a last resort, when there's no compression possible.
ENCODING: 0b1101 + 0bXXXXXXXX (data byte)

2. Zero byte
As zero bytes are very common, this is more efficient than "byte immediate".
ENCODING: 0b11111101

3. Byte from dictionary
Dictionary stores 16 last recently used bytes. A byte encoded using one of these first three methods (immediate, zero byte, dictionary)
is then added to the beginning (position zero) of the dictionary. If the dictionary already contains the same byte, it's removed and only kept
at the beginning. If the byte is not already present, the last byte (position 15) is discarded and the remaining 15 bytes in the dictionary are
moved by one towards the end. This means that frequently used bytes are kept at the beginning and rarely used bytes move to the end, eventually
being removed.
ENCODING: 0b10+0bXXXX (negated position in dictionary)

The following methods use number and prefix encoding:

4. Copy bytes from current line from position -80
Copies N bytes from the current line, from (current - 80) position. Oviously, this can be used only if the current byte position is at least 80.
This method does not use prefix, so the maximum byte count is 127.
ENCODING: 0b11110 + NUMBER(byte count)

5. Repeat the last byte multiple times
Repeats the last byte N times (run-length encoding).
The encoding is same as in the next method. The encoder and decoder (printer) maintain a flag (called TWOBYTE_FLAG here), which determines if this
encoding goes 1 byte or 2 bytes backwards. If the flag needs to be changed, TWOBYTE_FLAG CHANGE bits are inserted after the PREFIX.
The flag is reset to 0 at the beginning of each print data block.
TWOBYTE_FLAG CHANGE ENCODING: 0b11
ENCODING: [PREFIX(byte count)] + [TWOBYTE_FLAG CHANGE] + 0b1110 + NUMBER(byte count)

6. Copy bytes from current line from position -2
Copies N bytes from (current - 2) position. The encoding is same as in the previous method.
ENCODING: [PREFIX(byte count)] + [TWOBYTE_FLAG CHANGE] + 0b1110 + NUMBER(byte count)

7. Copy bytes from 3rd previous line from current position
Copies N bytes from the current position from . The encoder and decoder (printer) maintain a flag (called PREV8_FLAG here), which determines if this
encoding uses 3rd or 7th previous line.

PREV8_FLAG CHANGE ENCODING: 0b110

8. Copy bytes from 7th previous line from current position


Number and prefix encoding:
Number encoding can encode number from 0 to 127.
ENCODING: 0b111111 (special case if the number is 0)
ENCODING: 0b00 (special case if the number is 1)
ENCODING: 0b01 + 0bX (special case if the number is 2 [X=1] or 3 [X=0])
ENCODING: 0b1....0 (sequence of ones followed by zero, count of ones = number of following bits minus 1) + 0bX... (negated N lower bits of the number)

If a bigger number needs to be encoded, a prefix is used before the number. The prefix indicates multiples of 128 to be added to the number.
ENCODING: 0b11111100 + NUMBER(prefix/128)


OTHER PRINTERS
--------------

CARPS_Driver_V300_W32_us_EN.exe (CNAR0U.INF):
"Canon imageCLASS D300" 	= ICD300,USBPRINT\CanonimageCLASS_D30094B2,"imageCLASS D300"
(D320 D340)
"Canon LASERCLASS 500"  	= LC500,USBPRINT\CanonLASERCLASS_50034BE,"LASERCLASS 500"
(510)
"Canon FP-L170/MF350/L380/L398"	= MF350,USBPRINT\CanonFP-L170/MF350/LF4FD,CanonFP-L170/MF350/LF4FD,"FP-L170/MF350/L380/L398"
"Canon L120"			= L120,USBPRINT\CanonL120acdf,CanonL120acdf,"L120"
"Canon LC310/L390/L408S"	= LC310,USBPRINT\CanonLC310/L390/L408bffd,CanonLC310/L390/L408bffd,"LC310/L390/L408S"

@ imageCLASS D300: 15ppm, no image refinement
@ LASERCLASS 500: 15ppm, no image refinement
@ FP-L170/MF350/L380/L398: 19ppm, has image refinement
! L120: 13ppm, image refinement always off, different data format??????, ESC[600;1;0;256;;0;0'c
@ LC310/L390/L408S: 19ppm, has image refinement

--------------

CARPS_Driver_V300_W32_uk_EN.exe (CNAR0K.INF):
"Canon PC-D300/FAX-L400/ICD300"	= ICD300,USBPRINT\CanonPC-D300/FAX-L405C4D,"PC-D300/FAX-L400/ICD300"
"Canon FP-L170/MF350/L380/L398"	= MF350,USBPRINT\CanonFP-L170/MF350/LF4FD,CanonFP-L170/MF350/LF4FD,"FP-L170/MF350/L380/L398"
"Canon L120"			= L120,USBPRINT\CanonL120acdf,CanonL120acdf,"L120"
"Canon L180/L380S/L398S"	= L180,USBPRINT\CanonL180/L380S/L398c786,CanonL180/L380S/L398c786,"L180/L380S/L398S"
"Canon LC310/L390/L408S"	= LC310,USBPRINT\CanonLC310/L390/L408bffd,CanonLC310/L390/L408bffd,"LC310/L390/L408S"

@ PC-D300/FAX-L400/ICD300: 14ppm, no image refinement
@ L180/L380S/L398S: 18ppm, has image refinement

--------------

MF5600_Series_MFDrivers_W32_uk_EN.exe (MF5600AK.INF):
"Canon MF5600 Series"		= MF5600,USBPRINT\CanonMF56300658,CanonMF56300658,"MF5630"
"Canon MF5600 Series"		= MF5600,USBPRINT\CanonMF5650a65b,CanonMF5650a65b,"MF5650"

@ MF5630: 18ppm, has image refinement
@ MF5650: 18ppm, has image refinement

--------------

MF3110_MFDrivers_W32_us_EN.exe (MF3110AU.INF):
"Canon MF3110"		= MF3110,USBPRINT\CanonMF31102fe8,"MF3110"

@ MF3110: 21ppm, has image refinement (also for MF3111)

--------------

MF3240_MFDrivers_W32_us_EN.exe (MF3200AU.INF):
"Canon MF3200 Series"	= MF3200,USBPRINT\CanonMF3200_Series7652

! MF3200_Series: 21ppm, has image refinement, UNKNOWN BLOCK 0x6b, different data format, ESC[600;1;0;256;;0;0'c

--------------

MF8170_8180_MFDrivers_W32_us_EN-1.exe (MF8100AU.INF): 
"Canon MF8100 Series"	= MF8100,USBPRINT\CanonMF8100_Series5D87

! MF8100_Series: 4 ppm, COLOR!!!!!, 2 paper sources, ESC[600;2;32;32;;64;0'c (BW = ESC[600;2;0;32;;64;0'c)
ESC[0"p = B/W
ESC[1"p = COLOR
ESC[11q = casette 1
ESC[10q = multi-purpose tray
