This commit is contained in:
Kovid Goyal 2008-11-30 11:10:31 -08:00
commit f96e9cce0c

View File

@ -47,7 +47,6 @@ FLAG_CLOSING = (1 << 1)
FLAG_BLOCK = (1 << 2)
FLAG_HEAD = (1 << 3)
FLAG_ATOM = (1 << 4)
XML_ENTITIES = ['&amp;', '&apos;', '&lt;', '&gt;', '&quot;']
def u32(bytes):
return struct.unpack('<L', bytes[:4])[0]
@ -114,7 +113,7 @@ class UnBinary(object):
CLOSE_ANGLE_RE = re.compile(r'(?<!--)>>')
DOUBLE_ANGLE_RE = re.compile(r'([<>])\1')
def __init__(self, bin, path, manifest, map=OPF_MAP):
def __init__(self, bin, path, manifest={}, map=HTML_MAP):
self.manifest = manifest
self.tag_map, self.attr_map, self.tag_to_attr_map = map
self.opf = map is OPF_MAP
@ -368,8 +367,10 @@ class ManifestItem(object):
return self.internal == other
def __repr__(self):
return "ManifestItem(internal=%s, path=%s)" \
% (repr(self.internal), repr(self.path))
return "ManifestItem(internal=%r, path=%r, mime_type=%r, " \
"offset=%d, root=%r, state=%r)" \
% (self.internal, self.path, self.mime_type, self.offset,
self.root, self.state)
def preserve(function):
def wrapper(self, *args, **kwargs):
@ -641,14 +642,15 @@ class LitReader(object):
def _read_meta(self):
path = 'content.opf'
raw = self.get_file('/meta')
xml = OPF_DECL
try:
xml = OPF_DECL + unicode(UnBinary(raw, path, self.manifest, OPF_MAP))
xml += unicode(UnBinary(raw, path, self.manifest, OPF_MAP))
except LitError:
if 'PENGUIN group' not in raw: raise
print "WARNING: attempting PENGUIN malformed OPF fix"
raw = raw.replace(
'PENGUIN group', '\x00\x01\x18\x00PENGUIN group', 1)
xml = OPF_DECL + unicode(UnBinary(raw, path, self.manifest, OPF_MAP))
xml += unicode(UnBinary(raw, path, self.manifest, OPF_MAP))
self.meta = xml
def _read_drm(self):
@ -668,7 +670,7 @@ class LitReader(object):
raise LitError('Unable to decrypt title key!')
self.bookkey = bookkey[1:9]
else:
raise DRMError()
raise DRMError("Cannot access DRM-protected book")
def _calculate_deskey(self):
hashfiles = ['/meta', '/DRMStorage/DRMSource']
@ -731,6 +733,11 @@ class LitReader(object):
return content
def _decrypt(self, content):
length = len(content)
extra = length & 0x7
if extra > 0:
self._warn("content length not a multiple of block size")
content += "\0" * (8 - extra)
msdes.deskey(self.bookkey, msdes.DE1)
return msdes.des(content)
@ -768,7 +775,7 @@ class LitReader(object):
if u != 0:
raise LitError("Reset table entry greater than 32 bits")
if size >= len(content):
raise("Reset table entry out of bounds")
raise LitError("Reset table entry out of bounds")
if bytes_remaining >= window_bytes:
lzx.reset()
try: