-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-32072: Fix issues with binary plists. #4455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
serhiy-storchaka
merged 3 commits into
python:master
from
serhiy-storchaka:plistlib-refs
Nov 30, 2017
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
bpo-32072: Fix issues with binary plists.
* Fixed saving bytearrays. * Identical objects will be saved only once. * Equal references will be load as identical objects. * Added support for saving and loading recursive data structures.
- Loading branch information
commit a67efd7c3e16e39f21b0ae733df916d1ee563847
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -525,6 +525,8 @@ def __init__(self, message="Invalid file"): | |
|
||
_BINARY_FORMAT = {1: 'B', 2: 'H', 4: 'L', 8: 'Q'} | ||
|
||
_undefined = object() | ||
|
||
class _BinaryPlistParser: | ||
""" | ||
Read or write a binary plist file, following the description of the binary | ||
|
@@ -555,7 +557,8 @@ def parse(self, fp): | |
) = struct.unpack('>6xBBQQQ', trailer) | ||
self._fp.seek(offset_table_offset) | ||
self._object_offsets = self._read_ints(num_objects, offset_size) | ||
return self._read_object(self._object_offsets[top_object]) | ||
self._objects = [_undefined] * num_objects | ||
return self._read_object(top_object) | ||
|
||
except (OSError, IndexError, struct.error, OverflowError, | ||
UnicodeDecodeError): | ||
|
@@ -584,71 +587,79 @@ def _read_ints(self, n, size): | |
def _read_refs(self, n): | ||
return self._read_ints(n, self._ref_size) | ||
|
||
def _read_object(self, offset): | ||
def _read_object(self, ref): | ||
""" | ||
read the object at offset. | ||
read the object by reference. | ||
|
||
May recursively read sub-objects (content of an array/dict/set) | ||
""" | ||
result = self._objects[ref] | ||
if result is not _undefined: | ||
return result | ||
|
||
offset = self._object_offsets[ref] | ||
self._fp.seek(offset) | ||
token = self._fp.read(1)[0] | ||
tokenH, tokenL = token & 0xF0, token & 0x0F | ||
|
||
if token == 0x00: | ||
return None | ||
result = None | ||
|
||
elif token == 0x08: | ||
return False | ||
result = False | ||
|
||
elif token == 0x09: | ||
return True | ||
result = True | ||
|
||
# The referenced source code also mentions URL (0x0c, 0x0d) and | ||
# UUID (0x0e), but neither can be generated using the Cocoa libraries. | ||
|
||
elif token == 0x0f: | ||
return b'' | ||
result = b'' | ||
|
||
elif tokenH == 0x10: # int | ||
return int.from_bytes(self._fp.read(1 << tokenL), | ||
'big', signed=tokenL >= 3) | ||
result = int.from_bytes(self._fp.read(1 << tokenL), | ||
'big', signed=tokenL >= 3) | ||
|
||
elif token == 0x22: # real | ||
return struct.unpack('>f', self._fp.read(4))[0] | ||
result = struct.unpack('>f', self._fp.read(4))[0] | ||
|
||
elif token == 0x23: # real | ||
return struct.unpack('>d', self._fp.read(8))[0] | ||
result = struct.unpack('>d', self._fp.read(8))[0] | ||
|
||
elif token == 0x33: # date | ||
f = struct.unpack('>d', self._fp.read(8))[0] | ||
# timestamp 0 of binary plists corresponds to 1/1/2001 | ||
# (year of Mac OS X 10.0), instead of 1/1/1970. | ||
return datetime.datetime(2001, 1, 1) + datetime.timedelta(seconds=f) | ||
result = (datetime.datetime(2001, 1, 1) + | ||
datetime.timedelta(seconds=f)) | ||
|
||
elif tokenH == 0x40: # data | ||
s = self._get_size(tokenL) | ||
if self._use_builtin_types: | ||
return self._fp.read(s) | ||
result = self._fp.read(s) | ||
else: | ||
return Data(self._fp.read(s)) | ||
result = Data(self._fp.read(s)) | ||
|
||
elif tokenH == 0x50: # ascii string | ||
s = self._get_size(tokenL) | ||
result = self._fp.read(s).decode('ascii') | ||
return result | ||
result = result | ||
|
||
elif tokenH == 0x60: # unicode string | ||
s = self._get_size(tokenL) | ||
return self._fp.read(s * 2).decode('utf-16be') | ||
result = self._fp.read(s * 2).decode('utf-16be') | ||
|
||
# tokenH == 0x80 is documented as 'UID' and appears to be used for | ||
# keyed-archiving, not in plists. | ||
|
||
elif tokenH == 0xA0: # array | ||
s = self._get_size(tokenL) | ||
obj_refs = self._read_refs(s) | ||
return [self._read_object(self._object_offsets[x]) | ||
for x in obj_refs] | ||
result = [] | ||
self._objects[ref] = result | ||
result.extend(self._read_object(x) for x in obj_refs) | ||
return result | ||
|
||
# tokenH == 0xB0 is documented as 'ordset', but is not actually | ||
# implemented in the Apple reference code. | ||
|
@@ -661,12 +672,16 @@ def _read_object(self, offset): | |
key_refs = self._read_refs(s) | ||
obj_refs = self._read_refs(s) | ||
result = self._dict_type() | ||
self._objects[ref] = result | ||
for k, o in zip(key_refs, obj_refs): | ||
result[self._read_object(self._object_offsets[k]) | ||
] = self._read_object(self._object_offsets[o]) | ||
result[self._read_object(k)] = self._read_object(o) | ||
return result | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the same applies to this (existing) return statement. |
||
|
||
raise InvalidFileException() | ||
else: | ||
raise InvalidFileException() | ||
|
||
self._objects[ref] = result | ||
return result | ||
|
||
def _count_to_size(count): | ||
if count < 1 << 8: | ||
|
@@ -681,6 +696,8 @@ def _count_to_size(count): | |
else: | ||
return 8 | ||
|
||
_scalars = (str, int, float, datetime.datetime, bytes) | ||
|
||
class _BinaryPlistWriter (object): | ||
def __init__(self, fp, sort_keys, skipkeys): | ||
self._fp = fp | ||
|
@@ -736,24 +753,25 @@ def _flatten(self, value): | |
# First check if the object is in the object table, not used for | ||
# containers to ensure that two subcontainers with the same contents | ||
# will be 8000 serialized as distinct values. | ||
if isinstance(value, ( | ||
str, int, float, datetime.datetime, bytes, bytearray)): | ||
if isinstance(value, _scalars): | ||
if (type(value), value) in self._objtable: | ||
return | ||
|
||
elif isinstance(value, Data): | ||
if (type(value.data), value.data) in self._objtable: | ||
return | ||
|
||
elif id(value) in self._objidtable: | ||
return | ||
|
||
# Add to objectreference map | ||
refnum = len(self._objlist) | ||
self._objlist.append(value) | ||
try: | ||
if isinstance(value, Data): | ||
self._objtable[(type(value.data), value.data)] = refnum | ||
else: | ||
self._objtable[(type(value), value)] = refnum | ||
except TypeError: | ||
if isinstance(value, _scalars): | ||
self._objtable[(type(value), value)] = refnum | ||
elif isinstance(value, Data): | ||
self._objtable[(type(value.data), value.data)] = refnum | ||
else: | ||
self._objidtable[id(value)] = refnum | ||
|
||
# And finally recurse into containers | ||
|
@@ -780,12 +798,11 @@ def _flatten(self, value): | |
self._flatten(o) | ||
|
||
def _getrefnum(self, value): | ||
try: | ||
if isinstance(value, Data): | ||
return self._objtable[(type(value.data), value.data)] | ||
else: | ||
return self._objtable[(type(value), value)] | ||
except TypeError: | ||
if isinstance(value, _scalars): | ||
return self._objtable[(type(value), value)] | ||
elif isinstance(value, Data): | ||
return self._objtable[(type(value.data), value.data)] | ||
else: | ||
return self._objidtable[id(value)] | ||
|
||
def _write_size(self, token, size): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
Misc/NEWS.d/next/Library
72D7
/2017-11-18-21-13-52.bpo-32072.nwDV8L.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Fixed issues with binary plists: | ||
|
||
* Fixed saving bytearrays. | ||
* Identical objects will be saved only once. | ||
* Equal references will be load as identical objects. | ||
* Added support for saving and loading recursive data structures. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the return statement? This saves a duplicate store to self._objects[ref] at the end of the method, but makes handling this token different from he previous ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tokens "array" and "dict" are special. Since they are containers and can contain a cyclic reference to itself,
self._objects[ref]
should be set before reading the content. These tokens already handled different from tokens for scalar types, and the difference will be kept even if remove the return statement here.