def save(self, fn, preview=None, dviPreview=None): cf = not hasattr(fn, 'write') if cf: f = open(fn, 'wb') else: f = fn try: ps = self._postscript(dviPreview) if preview: import struct A = (b'\xc5', b'\xd0', b'\xd3', b'\xc6') hdr = struct.pack( *(("<4c7i", ) + A + (32, len(ps), 0, 0, 32 + len(ps), len(preview), 0xffff))) f.write(hdr) f.write(rawBytes(ps)) f.write(preview) else: f.write(rawBytes(ps)) finally: if cf: f.close() if cf and os.name == 'mac': from reportlab.lib.utils import markfilename markfilename(fn, ext='EPSF')
def save(self,fn, preview=None, dviPreview=None): cf = not hasattr(fn,'write') if cf: f = open(fn,'wb') else: f = fn try: ps = self._postscript(dviPreview) if preview: import struct A = (b'\xc5',b'\xd0',b'\xd3',b'\xc6')if isPy3 else (chr(0xc5),chr(0xd0),chr(0xd3),chr(0xc6)) hdr=struct.pack(*( ("<4c7i",) +A +( 32,len(ps),0,0,32+len(ps),len(preview),0xffff) ) ) f.write(hdr) f.write(rawBytes(ps)) f.write(preview) else: f.write(rawBytes(ps)) finally: if cf: f.close() if cf and os.name=='mac': from reportlab.lib.utils import markfilename markfilename(fn,ext='EPSF')
def computeU(encryptionkey, encodestring=PadString, revision=None, documentId=None): revision = checkRevision(revision) from reportlab.lib.arciv import ArcIV if revision == 2: result = ArcIV(encryptionkey).encode(encodestring) elif revision == 3: assert documentId is not None, "Revision 3 algorithm needs the document ID!" h = md5(PadString) h.update(rawBytes(documentId)) tmp = h.digest() tmp = ArcIV(encryptionkey).encode(tmp) for n in range(1, 20): thisKey = xorKey(n, encryptionkey) tmp = ArcIV(thisKey).encode(tmp) while len(tmp) < 32: tmp += b'\0' result = tmp if DEBUG: print('computeU(%s,%s,%s,%s)==>%s' % tuple([ hexText(str(x)) for x in (encryptionkey, encodestring, revision, documentId, result) ])) return result
def computeU(encryptionkey, encodestring=PadString,revision=2,documentId=None): from reportlab.lib.arciv import ArcIV if revision == 2: result = ArcIV(encryptionkey).encode(encodestring) elif revision == 3: assert documentId is not None, "Revision 3 algorithm needs the document ID!" h = md5(PadString) h.update(rawBytes(documentId)) tmp = h.digest() tmp = ArcIV(encryptionkey).encode(tmp) for n in range(1,20): thisKey = xorKey(n, encryptionkey) tmp = ArcIV(thisKey).encode(tmp) while len(tmp) < 32: tmp += b'\0' result = tmp if DEBUG: print('computeU(%s,%s,%s,%s)==>%s' % tuple([hexText(str(x)) for x in (encryptionkey, encodestring,revision,documentId,result)])) return result
def save(self,f=None): if not hasattr(f,'write'): _f = open(f,'wb') else: _f = f if self.code[-1]!='showpage': self.clear() self.code.insert(0,'''\ %%!PS-Adobe-3.0 EPSF-3.0 %%%%BoundingBox: 0 0 %d %d %%%% Initialization: /m {moveto} bind def /l {lineto} bind def /c {curveto} bind def ''' % (self.width,self.height)) self._t1_re_encode() _f.write(rawBytes(self._sep.join(self.code))) if _f is not f: _f.close() from reportlab.lib.utils import markfilename markfilename(f,creatorcode='XPR3',filetype='EPSF')
return '<' + ''.join('%02X' % ord(c) for c in rawUnicode(text)) + '>' def unHexText(hexText): equalityCheck(hexText[0], '<', 'bad hex text') equalityCheck(hexText[-1], '>', 'bad hex text') hexText = hexText[1:-1] out = b'' for i in range(int(len(hexText) / 2.0)): slice = hexText[i * 2:i * 2 + 2] char = int2Byte(eval('0x' + slice)) out = out + char return out PadString = rawBytes(''.join(chr(int(c, 16)) for c in padding.strip().split())) def checkRevision(revision): if revision is None: strength = rl_config.encryptionStrength if strength == 40: revision = 2 elif strength == 128: revision = 3 elif strength == 256: if not pyaes: raise ValueError( 'strength==256 is not supported as package pyaes is not importable' ) revision = 5
def calcChecksum(data): """Calculates TTF-style checksums""" data = rawBytes(data) if len(data)&3: data = data + (4-(len(data)&3))*b"\0" return sum(unpack(">%dl" % (len(data)>>2), data)) & 0xFFFFFFFF
def calcChecksum(data): """Calculates TTF-style checksums""" data = rawBytes(data) if len(data) & 3: data = data + (4 - (len(data) & 3)) * b"\0" return sum(unpack(">%dl" % (len(data) >> 2), data)) & 0xFFFFFFFF
def hexText(text): "a legitimate way to show strings in PDF" return '<%s>' % asNative(hexlify(rawBytes(text))).upper()
def hexText(text): "a legitimate way to show strings in PDF" return '<' + ''.join('%02X' % ord(c) for c in rawUnicode(text)) + '>' def unHexText(hexText): equalityCheck(hexText[0], '<', 'bad hex text') equalityCheck(hexText[-1], '>', 'bad hex text') hexText = hexText[1:-1] out = b'' for i in range(int(len(hexText)/2.0)): slice = hexText[i*2: i*2+2] char = int2Byte(eval('0x'+slice)) out = out + char return out PadString = rawBytes(''.join(chr(int(c, 16)) for c in padding.strip().split())) def encryptionkey(password, OwnerKey, Permissions, FileId1, revision=2): # FileId1 is first string of the fileid array # add padding string #AR force same as iText example #Permissions = -1836 #int(Permissions - 2**31) password = asBytes(password) + PadString # truncate to 32 bytes password = password[:32] # translate permissions to string, low order byte first p = Permissions# + 2**32L permissionsString = b"" for i in range(4): byte = (p & 0xff) # seems to match what iText does p = p>>8