Changeset 488

Show
Ignore:
Timestamp:
11/17/07 16:10:02 (17 years ago)
Author:
jerome
Message:

Improved parser against cheaters, just in case.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pkpgcounter/trunk/pkpgpdls/lidil.py

    r485 r488  
    3232import os 
    3333import mmap 
    34 from struct import unpack 
     34import struct 
    3535 
    3636import pdlparser 
     
    7272    def getJobSize(self) : 
    7373        """Computes the number of pages in a HP LIDIL document.""" 
    74         pagecount = 0 
     74        unpack = struct.unpack 
     75        ejectpage = loadpage = 0 
    7576        infileno = self.infile.fileno() 
    7677        minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED) 
     
    9192                    except struct.error :     
    9293                        raise pdlparser.PDLParserError, "This file doesn't seem to be valid Hewlett-Packard LIDIL datas" 
    93                     if (packettype == PACKET_TYPE_COMMAND) \ 
    94                         and (commandnumber == LDL_EJECT_PAGE) : 
    95                         pagecount += 1 
     94                    if packettype == PACKET_TYPE_COMMAND : 
     95                        if commandnumber == LDL_LOAD_PAGE : 
     96                            loadpage += 1 
     97                        elif commandnumber == LDL_EJECT_PAGE : 
     98                            ejectpage += 1 
    9699                    pos += (cmdlength + datalength) 
    97100            except IndexError : # EOF ? 
     
    99102        finally :         
    100103            minfile.close() 
    101         return pagecount 
     104             
     105        # Number of page eject commands should be sufficient, 
     106        # but we never know : someone could try to cheat the printer 
     107        # by loading a page but not ejecting it, and ejecting it manually 
     108        # later on. Not sure if the printers would support this, but 
     109        # taking the max value works around the problem in any case. 
     110        self.logdebug("Load : %i    Eject : %i" % (loadpage, ejectpage)) 
     111        return max(loadpage, ejectpage) 
    102112 
    103113if __name__ == "__main__" :