src/reportlab/lib/pdfencrypt.py
changeset 4551 d357e2acc856
parent 4536 ea5704e5ff9b
equal deleted inserted replaced
4550:80dd9e83dad9 4551:d357e2acc856
     2 #see license.txt for license details
     2 #see license.txt for license details
     3 __version__='3.3.0'
     3 __version__='3.3.0'
     4 
     4 
     5 """helpers for pdf encryption/decryption"""
     5 """helpers for pdf encryption/decryption"""
     6 import sys, os, tempfile
     6 import sys, os, tempfile
     7 from reportlab.lib.utils import getBytesIO, md5, asBytes, int2Byte, char2int, rawUnicode, rawBytes, isPy3
     7 from binascii import hexlify, unhexlify
       
     8 from reportlab.lib.utils import getBytesIO, md5, asBytes, int2Byte, char2int, rawUnicode, rawBytes, isPy3, asNative
     8 from reportlab.pdfgen.canvas import Canvas
     9 from reportlab.pdfgen.canvas import Canvas
     9 from reportlab.pdfbase import pdfutils
    10 from reportlab.pdfbase import pdfutils
    10 from reportlab.pdfbase.pdfdoc import PDFObject
    11 from reportlab.pdfbase.pdfdoc import PDFObject
    11 from reportlab.platypus.flowables import Flowable
    12 from reportlab.platypus.flowables import Flowable
    12 from reportlab import rl_config, ascii
    13 from reportlab import rl_config, ascii
   307 2E 2E 00 B6 D0 68 3E 80 2F 0C A9 FE 64 53 69 7A
   308 2E 2E 00 B6 D0 68 3E 80 2F 0C A9 FE 64 53 69 7A
   308 """
   309 """
   309 
   310 
   310 def hexText(text):
   311 def hexText(text):
   311     "a legitimate way to show strings in PDF"
   312     "a legitimate way to show strings in PDF"
   312     return '<' + ''.join('%02X' % ord(c) for c in rawUnicode(text)) + '>'
   313     return '<%s>' % asNative(hexlify(rawBytes(text))).upper()
   313 
   314 
   314 def unHexText(hexText):
   315 def unHexText(hexText):
   315     equalityCheck(hexText[0], '<', 'bad hex text')
   316     equalityCheck(hexText[0], '<', 'bad hex text')
   316     equalityCheck(hexText[-1], '>', 'bad hex text')
   317     equalityCheck(hexText[-1], '>', 'bad hex text')
   317     hexText = hexText[1:-1]
   318     return unhexlify(hexText[1:-1])
   318     out = b''
       
   319     for i in range(int(len(hexText)/2.0)):
       
   320         slice = hexText[i*2: i*2+2]
       
   321         char = int2Byte(eval('0x'+slice))
       
   322         out = out + char
       
   323     return out
       
   324 
   319 
   325 PadString = rawBytes(''.join(chr(int(c, 16)) for c in padding.strip().split()))
   320 PadString = rawBytes(''.join(chr(int(c, 16)) for c in padding.strip().split()))
   326 
   321 
   327 def checkRevision(revision):
   322 def checkRevision(revision):
   328     if revision is None:
   323     if revision is None:
   733                     if argv[pos+1] not in ('1', '0'):
   728                     if argv[pos+1] not in ('1', '0'):
   734                         raise ValueError("%s value must be either '1' or '0'!" % thisarg[1])
   729                         raise ValueError("%s value must be either '1' or '0'!" % thisarg[1])
   735                 try:
   730                 try:
   736                     if argv[pos+1] not in known_modes:
   731                     if argv[pos+1] not in known_modes:
   737                         if thisarg[0] in binaryrequired:
   732                         if thisarg[0] in binaryrequired:
   738                             exec(thisarg[1] +' = int(argv[pos+1])')
   733                             exec(thisarg[1] +' = int(argv[pos+1])',vars())
   739                         else:
   734                         else:
   740                             exec(thisarg[1] +' = argv[pos+1]')
   735                             exec(thisarg[1] +' = argv[pos+1]',vars())
   741                         if verbose:
   736                         if verbose:
   742                             print("%s set to: '%s'." % (thisarg[3], argv[pos+1]))
   737                             print("%s set to: '%s'." % (thisarg[3], argv[pos+1]))
   743                         argv.remove(argv[pos+1])
   738                         argv.remove(argv[pos+1])
   744                         argv.remove(thisarg[0])
   739                         argv.remove(thisarg[0])
   745                 except:
   740                 except: