Changeset 316 for pkpgcounter/trunk/pkpgpdls/pclxl.py
- Timestamp:
- 02/09/06 22:52:21 (19 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pkpgcounter/trunk/pkpgpdls/pclxl.py
r314 r316 88 88 return 0 89 89 90 def beginPage(self ) :90 def beginPage(self, prevpos) : 91 91 """Indicates the beginning of a new page, and extracts media information.""" 92 92 self.pagecount += 1 … … 102 102 # this saves time because we don't need a complete parser ! 103 103 minfile = self.minfile 104 105 # self.logdebug("--------------") 106 # for i in range(100) : 107 # self.logdebug("%08i ==> 0x%02x ==> '%s'" % (self.pos - 98 + i, ord(self.minfile[self.pos - 98 + i]), self.minfile[self.pos - 98 + i])) 108 # self.logdebug("--------------") 109 110 pos = self.pos - 2 104 pos = prevpos - 2 111 105 while pos > 0 : # safety check : don't go back to far ! 112 106 val = ord(minfile[pos]) … … 153 147 elif length == 2 : 154 148 startpos = pos + 5 155 size = unpack(self. endianness + "H", self.minfile[pos+3:startpos])[0]149 size = unpack(self.unpackShort, self.minfile[pos+3:startpos])[0] 156 150 elif length == 4 : 157 151 startpos = pos + 7 158 size = unpack(self. endianness + "I", self.minfile[pos+3:startpos])[0]152 size = unpack(self.unpackLong, self.minfile[pos+3:startpos])[0] 159 153 else : 160 154 raise pdlparser.PDLParserError, "Error on size at %s : %s" % (pos+2, length) … … 180 174 return 0 181 175 182 def endPage(self ) :176 def endPage(self, prevpos) : 183 177 """Indicates the end of a page.""" 184 pos = self.pos178 pos = prevpos 185 179 pos3 = pos - 3 186 180 minfile = self.minfile … … 191 185 # of copies is an unsigned 16 bits integer 192 186 try : 193 self.pages[self.pagecount]["copies"] = unpack(self. endianness + "H", minfile[pos-5:pos3])[0]187 self.pages[self.pagecount]["copies"] = unpack(self.unpackShort, minfile[pos-5:pos3])[0] 194 188 except KeyError : 195 189 self.logdebug("It looks like this PCLXL file is corrupted.") 196 190 return 0 197 191 198 def setColorSpace(self ) :192 def setColorSpace(self, prevpos) : 199 193 """Changes the color space.""" 200 if self.minfile[ self.pos-4:self.pos-1] == self.RGBColorSpace :194 if self.minfile[prevpos-4:prevpos-1] == self.RGBColorSpace : 201 195 self.iscolor = 1 202 196 return 0 203 197 204 def array_8(self ) :198 def array_8(self, prevpos) : 205 199 """Handles byte arrays.""" 206 pos = self.pos200 pos = prevpos 207 201 datatype = self.minfile[pos] 208 202 pos += 1 209 203 length = self.tags[ord(datatype)] 210 204 if callable(length) : 211 self.pos = pos 212 length = length() 213 pos = self.pos 205 length = length(pos) 214 206 posl = pos + length 215 self.pos = posl216 207 if length == 1 : 217 return unpack("B", self.minfile[pos:posl])[0]208 return 1 + length + unpack("B", self.minfile[pos:posl])[0] 218 209 elif length == 2 : 219 return unpack(self.endianness + "H", self.minfile[pos:posl])[0]210 return 1 + length + unpack(self.unpackShort, self.minfile[pos:posl])[0] 220 211 elif length == 4 : 221 return unpack(self.endianness + "I", self.minfile[pos:posl])[0]212 return 1 + length + unpack(self.unpackLong, self.minfile[pos:posl])[0] 222 213 else : 223 raise pdlparser.PDLParserError, "Error on array size at %x" % self.pos224 225 def array_16(self ) :214 raise pdlparser.PDLParserError, "Error on array size at %x" % prevpos 215 216 def array_16(self, prevpos) : 226 217 """Handles byte arrays.""" 227 pos = self.pos218 pos = prevpos 228 219 datatype = self.minfile[pos] 229 220 pos += 1 230 221 length = self.tags[ord(datatype)] 231 222 if callable(length) : 232 self.pos = pos 233 length = length() 234 pos = self.pos 223 length = length(pos) 235 224 posl = pos + length 236 self.pos = posl237 225 if length == 1 : 238 return 2 * unpack("B", self.minfile[pos:posl])[0]226 return 1 + length + 2 * unpack("B", self.minfile[pos:posl])[0] 239 227 elif length == 2 : 240 return 2 * unpack(self.endianness + "H", self.minfile[pos:posl])[0]228 return 1 + length + 2 * unpack(self.unpackShort, self.minfile[pos:posl])[0] 241 229 elif length == 4 : 242 return 2 * unpack(self.endianness + "I", self.minfile[pos:posl])[0]230 return 1 + length + 2 * unpack(self.unpackLong, self.minfile[pos:posl])[0] 243 231 else : 244 raise pdlparser.PDLParserError, "Error on array size at %x" % self.pos245 246 def array_32(self ) :232 raise pdlparser.PDLParserError, "Error on array size at %x" % prevpos 233 234 def array_32(self, prevpos) : 247 235 """Handles byte arrays.""" 248 pos = self.pos236 pos = prevpos 249 237 datatype = self.minfile[pos] 250 238 pos += 1 251 239 length = self.tags[ord(datatype)] 252 240 if callable(length) : 253 self.pos = pos 254 length = length() 255 pos = self.pos 241 length = length(pos) 256 242 posl = pos + length 257 self.pos = posl258 243 if length == 1 : 259 return 4 * unpack("B", self.minfile[pos:posl])[0]244 return 1 + length + 4 * unpack("B", self.minfile[pos:posl])[0] 260 245 elif length == 2 : 261 return 4 * unpack(self.endianness + "H", self.minfile[pos:posl])[0]246 return 1 + length + 4 * unpack(self.unpackShort, self.minfile[pos:posl])[0] 262 247 elif length == 4 : 263 return 4 * unpack(self.endianness + "I", self.minfile[pos:posl])[0]248 return 1 + length + 4 * unpack(self.unpackLong, self.minfile[pos:posl])[0] 264 249 else : 265 raise pdlparser.PDLParserError, "Error on array size at %x" % self.pos266 267 def embeddedDataSmall(self ) :250 raise pdlparser.PDLParserError, "Error on array size at %x" % prevpos 251 252 def embeddedDataSmall(self, prevpos) : 268 253 """Handle small amounts of data.""" 269 pos = self.pos 270 length = ord(self.minfile[pos]) 271 self.pos = pos + 1 272 return length 273 274 def embeddedData(self) : 254 return 1 + ord(self.minfile[prevpos]) 255 256 def embeddedData(self, prevpos) : 275 257 """Handle normal amounts of data.""" 276 pos = self.pos 277 pos4 = pos + 4 278 self.pos = pos4 279 return unpack(self.endianness + "I", self.minfile[pos:pos4])[0] 280 281 def littleEndian(self) : 258 return 4 + unpack(self.unpackLong, self.minfile[prevpos:prevpos+4])[0] 259 260 def littleEndian(self, prevpos) : 282 261 """Toggles to little endianness.""" 283 262 self.endianness = "<" # little endian 263 self.unpackShort = "<H" 264 self.unpackLong = "<I" 284 265 return 0 285 266 286 def bigEndian(self ) :267 def bigEndian(self, prevpos) : 287 268 """Toggles to big endianness.""" 288 269 self.endianness = ">" # big endian 270 self.unpackShort = ">H" 271 self.unpackLong = ">I" 289 272 return 0 290 273 291 def reservedForFutureUse(self ) :274 def reservedForFutureUse(self, prevpos) : 292 275 """Outputs something when a reserved byte is encountered.""" 293 self.logdebug("Byte at %x is out of the PCLXL Protocol Class 2.0 Specification" % self.pos)276 self.logdebug("Byte at %x is out of the PCLXL Protocol Class 2.0 Specification" % prevpos) 294 277 return 0 295 278 296 def passThrough(self) : 297 """Passthrough mode, as detailed in PCLXL Feature Reference Protocol Class 3.0 Supplement.""" 298 # TODO : do something here to skip the block. 299 self.logdebug("PassThrough marker detected at %x" % self.pos) 300 return 0 301 302 def x46_class3(self) : 279 def x46_class3(self, prevpos) : 303 280 """Undocumented tag 0x46 in class 3.0 streams.""" 304 pos = self.pos281 pos = prevpos 305 282 minfile = self.minfile 306 283 while pos > 0 : # safety check : don't go back to far ! … … 317 294 return unpack("B", self.minfile[pos:posl])[0] 318 295 elif length == 2 : 319 return unpack(self. endianness + "H", self.minfile[pos:posl])[0]296 return unpack(self.unpackShort, self.minfile[pos:posl])[0] 320 297 elif length == 4 : 321 return unpack(self. endianness + "I", self.minfile[pos:posl])[0]298 return unpack(self.unpackLong, self.minfile[pos:posl])[0] 322 299 else : 323 raise pdlparser.PDLParserError, "Error on size at %x" % self.pos300 raise pdlparser.PDLParserError, "Error on size at %x" % prevpos 324 301 else : 325 302 pos -= 1 326 303 return 0 327 304 328 def escape(self ) :305 def escape(self, prevpos) : 329 306 """Handles the ESC code.""" 330 pos = endpos = self.pos307 pos = endpos = prevpos 331 308 minfile = self.minfile 332 309 if minfile[pos : pos+8] == r"%-12345X" : … … 348 325 return endpos - pos 349 326 350 def skipKyoceraPrescribe(self ) :327 def skipKyoceraPrescribe(self, prevpos) : 351 328 """Skips Kyocera Prescribe commands.""" 352 pos = self.pos - 1329 pos = prevpos - 1 353 330 minfile = self.minfile 354 331 if minfile[pos:pos+3] == "!R!" : 355 while (pos - self.pos) < 1024 : # This is a realistic upper bound, to avoid infinite loops332 while (pos - prevpos) < 1024 : # This is a realistic upper bound, to avoid infinite loops 356 333 if (minfile[pos] == ";") and (minfile[pos-4:pos] == "EXIT") : 357 334 pos += 1 358 335 prescribe = self.prescribeStuff.setdefault(self.pagecount, []) 359 prescribe.append(minfile[ self.pos-1:pos])360 self.logdebug("Prescribe commands : [%s]" % repr(minfile[ self.pos-1:pos]))361 return (pos - self.pos)336 prescribe.append(minfile[prevpos-1:pos]) 337 self.logdebug("Prescribe commands : [%s]" % repr(minfile[prevpos-1:pos])) 338 return (pos - prevpos) 362 339 pos += 1 363 340 else : … … 393 370 endian = ord(line[pos - 1]) 394 371 if endian == 0x29 : 395 self.littleEndian( )372 self.littleEndian(0) 396 373 elif endian == 0x28 : 397 self.bigEndian( )374 self.bigEndian(0) 398 375 # elif endian == 0x27 : # TODO : This is the ESC code : parse it for PJL statements ! 399 376 # … … 476 453 self.tags[0xbe] = self.reservedForFutureUse # reserved 477 454 478 self.tags[0xbf] = self.passThrough # From PCLXL Feature Reference Protocol Class 3.0 Supplement455 # self.tags[0xbf] = self.passThrough # PassThrough mode should already be taken care of automatically 479 456 480 457 self.tags[0xc0] = 1 # ubyte … … 574 551 self.escapedStuff = {} # For escaped datas, mostly PJL commands 575 552 self.prescribeStuff = {} # For Kyocera Prescribe commands 576 self.pos =pos = oldpos = 0553 pos = oldpos = 0 577 554 try : 578 555 try : 579 556 while 1 : 580 557 try : 581 char = minfile[pos]558 tag = ord(minfile[pos]) 582 559 except OverflowError : 583 560 pos = oldpos + 1 584 561 pos += 1 585 length = tags[ ord(char)]562 length = tags[tag] 586 563 if length : 587 564 if callable(length) : 588 self.pos = pos 589 length = length() 590 pos = self.pos 565 length = length(pos) 591 566 oldpos = pos 592 567 pos += length