/usr/share/pyshared/pkpgpdls/spl1.py is in pkpgcounter 3.50-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | # -*- coding: ISO-8859-15 -*-
#
# pkpgcounter : a generic Page Description Language parser
#
# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# $Id: spl1.py 374 2007-12-09 14:26:15Z jerome $
#
"""This modules implements a page counter for SPL1 documents."""
import os
import mmap
import struct
import pdlparser
import version
ESCAPECHARS = (chr(0x1b), chr(0x24))
class Parser(pdlparser.PDLParser) :
"""A parser for SPL1 documents."""
format = "SPL1 (aka GDI)"
def isValid(self) :
"""Returns True if data is SPL1, else False."""
if ((self.firstblock[:128].find("\033%-12345X") != -1) and \
(self.firstblock.find("$PJL ") != -1) and \
((self.firstblock.find("LANGUAGE=SMART") != -1) or \
(self.firstblock.find("LANGUAGE = SMART") != -1))) :
return True
else :
return False
def littleEndian(self) :
"""Toggles to little endianness."""
self.unpackType = { 1 : "B", 2 : "<H", 4 : "<I" }
self.unpackShort = self.unpackType[2]
self.unpackLong = self.unpackType[4]
# self.logdebug("Little Endian")
return 0
def bigEndian(self) :
"""Toggles to big endianness."""
self.unpackType = { 1 : "B", 2 : ">H", 4 : ">I" }
self.unpackShort = self.unpackType[2]
self.unpackLong = self.unpackType[4]
# self.logdebug("Big Endian")
return 0
def escape(self, nextpos) :
"""Handles the ESC code."""
self.isbitmap = False
pos = endpos = nextpos
minfile = self.minfile
if minfile[pos : pos+8] == r"%-12345X" :
endpos = pos + 9
elif minfile[pos-1] in ESCAPECHARS :
endpos = pos
else :
return 0
endmark = (chr(0x1b), chr(0x00))
asciilimit = chr(0x80)
quotes = 0
while (minfile[endpos] not in endmark) and \
((minfile[endpos] < asciilimit) or (quotes % 2)) :
if minfile[endpos] == '"' :
quotes += 1
endpos += 1
# Store this in a per page mapping.
# NB : First time will be at page 0 (i.e. **before** page 1) !
stuff = self.escapedStuff.setdefault(self.pagecount, [])
datas = minfile[pos-1 : endpos]
stuff.append(datas)
if datas.endswith("$PJL BITMAP START\r\n") :
self.isbitmap = True
# self.logdebug("New bitmap")
self.logdebug("Escaped datas : [%s]" % repr(datas))
return endpos - pos + 1
def getJobSize(self) :
"""Counts pages in an SPL1 document.
Algorithm by Jerome Alet.
"""
infileno = self.infile.fileno()
self.minfile = minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
self.pagecount = 0
self.escapedStuff = {} # For escaped datas, mostly PJL commands
self.bigEndian()
self.isbitmap = False
pos = 0
unpack = struct.unpack
try :
try :
while 1 :
tag = minfile[pos]
if tag in ESCAPECHARS :
pos += self.escape(pos+1)
else :
if not self.isbitmap :
raise pdlparser.PDLParserError, "Unfortunately SPL1 is incompletely recognized. Parsing aborted. Please report the problem to %s" % version.__authoremail__
(offset,
seqnum) = unpack(">IH", minfile[pos:pos+6])
# self.logdebug("Offset : %i Sequence Number : %i" % (offset, seqnum))
if not seqnum :
# Sequence number resets to 0 for each new page.
self.pagecount += 1
pos += 4 + offset
except struct.error, msg :
raise pdlparser.PDLParserError, "Unfortunately SPL1 is incompletely recognized (%s). Parsing aborted. Please report the problem to %s" % (msg, version.__authoremail__)
except IndexError : # EOF ?
pass
finally :
minfile.close()
return self.pagecount
|