Python plistlib.InvalidFileException() Examples
The following are 30
code examples of plistlib.InvalidFileException().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
plistlib
, or try the search function
.
Example #1
Source File: apple_sus_checkin.py From sal-scripts with Apache License 2.0 | 6 votes |
def get_sus_install_report(): """Return installed apple updates from softwareupdate""" try: history = plistlib.loads( pathlib.Path('/Library/Receipts/InstallHistory.plist').read_bytes()) except (IOError, plistlib.InvalidFileException): history = [] return { i['displayName']: { 'date_managed': i['date'], 'status': 'PRESENT', 'data': { 'type': 'Apple SUS Install', 'version': i['displayVersion'].strip() } } for i in history if i['processName'] == 'softwareupdated'}
Example #2
Source File: plist.py From Web-Driver-Toolkit with MIT License | 6 votes |
def parse(self, fp): try: # The basic file format: # HEADER # object... # refid->offset... # TRAILER self._fp = fp self._fp.seek(-32, os.SEEK_END) trailer = self._fp.read(32) if len(trailer) != 32: raise InvalidFileException() ( offset_size, self._ref_size, num_objects, top_object, offset_table_offset ) = struct.unpack('>6xBBQQQ', trailer) self._fp.seek(offset_table_offset) self._object_offsets = self._read_ints(num_objects, offset_size) self._objects = [_undefined] * num_objects return self._read_object(top_object) except (OSError, IndexError, struct.error, OverflowError, UnicodeDecodeError): raise InvalidFileException()
Example #3
Source File: plist.py From SSDTTime with MIT License | 6 votes |
def parse(self, fp): try: # The basic file format: # HEADER # object... # refid->offset... # TRAILER self._fp = fp self._fp.seek(-32, os.SEEK_END) trailer = self._fp.read(32) if len(trailer) != 32: raise InvalidFileException() ( offset_size, self._ref_size, num_objects, top_object, offset_table_offset ) = struct.unpack('>6xBBQQQ', trailer) self._fp.seek(offset_table_offset) self._object_offsets = self._read_ints(num_objects, offset_size) self._objects = [_undefined] * num_objects return self._read_object(top_object) except (OSError, IndexError, struct.error, OverflowError, UnicodeDecodeError): raise InvalidFileException()
Example #4
Source File: plist.py From ProperTree with BSD 3-Clause "New" or "Revised" License | 6 votes |
def parse(self, fp): try: # The basic file format: # HEADER # object... # refid->offset... # TRAILER self._fp = fp self._fp.seek(-32, os.SEEK_END) trailer = self._fp.read(32) if len(trailer) != 32: raise InvalidFileException() ( offset_size, self._ref_size, num_objects, top_object, offset_table_offset ) = struct.unpack('>6xBBQQQ', trailer) self._fp.seek(offset_table_offset) self._object_offsets = self._read_ints(num_objects, offset_size) self._objects = [_undefined] * num_objects return self._read_object(top_object) except (OSError, IndexError, struct.error, OverflowError, UnicodeDecodeError): raise InvalidFileException()
Example #5
Source File: plist.py From GenSMBIOS with MIT License | 6 votes |
def parse(self, fp): try: # The basic file format: # HEADER # object... # refid->offset... # TRAILER self._fp = fp self._fp.seek(-32, os.SEEK_END) trailer = self._fp.read(32) if len(trailer) != 32: raise InvalidFileException() ( offset_size, self._ref_size, num_objects, top_object, offset_table_offset ) = struct.unpack('>6xBBQQQ', trailer) self._fp.seek(offset_table_offset) self._object_offsets = self._read_ints(num_objects, offset_size) self._objects = [_undefined] * num_objects return self._read_object(top_object) except (OSError, IndexError, struct.error, OverflowError, UnicodeDecodeError): raise InvalidFileException()
Example #6
Source File: munki_checkin.py From sal-scripts with Apache License 2.0 | 6 votes |
def get_managed_install_report(): """Return Munki ManagedInstallsReport.plist as a plist dict. Returns: ManagedInstalls report for last Munki run as a plist dict, or an empty dict. """ # Checks munki preferences to see where the install directory is set to. managed_install_dir = munkicommon.pref('ManagedInstallDir') # set the paths based on munki's configuration. managed_install_report = pathlib.Path(managed_install_dir) / 'ManagedInstallReport.plist' try: munki_report = plistlib.loads(managed_install_report.read_bytes()) except (IOError, plistlib.InvalidFileException): munki_report = {} if 'MachineInfo' not in munki_report: munki_report['MachineInfo'] = {} return sal.unobjctify(munki_report)
Example #7
Source File: plist.py From gibMacOS with MIT License | 6 votes |
def parse(self, fp): try: # The basic file format: # HEADER # object... # refid->offset... # TRAILER self._fp = fp self._fp.seek(-32, os.SEEK_END) trailer = self._fp.read(32) if len(trailer) != 32: raise InvalidFileException() ( offset_size, self._ref_size, num_objects, top_object, offset_table_offset ) = struct.unpack('>6xBBQQQ', trailer) self._fp.seek(offset_table_offset) self._object_offsets = self._read_ints(num_objects, offset_size) self._objects = [_undefined] * num_objects return self._read_object(top_object) except (OSError, IndexError, struct.error, OverflowError, UnicodeDecodeError): raise InvalidFileException()
Example #8
Source File: munki_checkin.py From sal-scripts with Apache License 2.0 | 6 votes |
def get_optional_manifest(): """Return Munki SelfServeManifest as a plist dict. Returns: SelfServeManifest for last Munki run as a plist dict, or an empty dict. """ # Checks munki preferences to see where the install directory is set to. managed_install_dir = munkicommon.pref('ManagedInstallDir') # set the paths based on munki's configuration. optional_manifest_path = pathlib.Path(managed_install_dir) / 'manifests/SelfServeManifest' try: optional_manifest = plistlib.loads(optional_manifest_path.read_bytes()) except (IOError, plistlib.InvalidFileException): optional_manifest = {} return optional_manifest
Example #9
Source File: profile_checkin.py From sal-scripts with Apache License 2.0 | 6 votes |
def get_profiles(): try: temp_dir = pathlib.Path(tempfile.mkdtemp()) profile_out = temp_dir / 'profiles.plist' cmd = ['/usr/bin/profiles', '-C', '-o', profile_out] # dev_null = open(os.devnull, 'w') try: subprocess.call(cmd, stdout=subprocess.PIPE) except OSError: return {} try: profiles = plistlib.loads(profile_out.read_bytes()) except plistlib.InvalidFileException: return {} finally: profile_out.unlink(missing_ok=True) temp_dir.rmdir() return profiles
Example #10
Source File: plist.py From USBMap with MIT License | 6 votes |
def parse(self, fp): try: # The basic file format: # HEADER # object... # refid->offset... # TRAILER self._fp = fp self._fp.seek(-32, os.SEEK_END) trailer = self._fp.read(32) if len(trailer) != 32: raise InvalidFileException() ( offset_size, self._ref_size, num_objects, top_object, offset_table_offset ) = struct.unpack('>6xBBQQQ', trailer) self._fp.seek(offset_table_offset) self._object_offsets = self._read_ints(num_objects, offset_size) self._objects = [_undefined] * num_objects return self._read_object(top_object) except (OSError, IndexError, struct.error, OverflowError, UnicodeDecodeError): raise InvalidFileException()
Example #11
Source File: plistinfo.py From macbuild-ansible with MIT License | 6 votes |
def main(): try: try: plist_file = determine_plist_path(sys.argv[1]) with open(plist_file, 'rb') as f: plist_data = plistlib.load(f) except IndexError: plist_file = '<stdin>' plist_data = plistlib.loads(sys.stdin.buffer.read()) print(yaml.dump(plist_data, default_flow_style=False), end='') except IOError: print(f'{RED}Error: The requested plist file {plist_file} was not found{ENDC}') exit(1) except plistlib.InvalidFileException: print(f'{RED}Error: Unable to parse the requested plist file {plist_file}{ENDC}') exit(1) except KeyboardInterrupt: pass
Example #12
Source File: plist.py From macbuild-ansible with MIT License | 5 votes |
def do_plist(module, filename, values, backup=False): working_values = values changed = False try: f = open(filename, 'rb') plist = plistlib.load(f) except IOError: plist = {} except plistlib.InvalidFileException: module.fail_json(msg="an invalid plist already exists") changed = not equal(plist, working_values) if changed and not module.check_mode: if backup: module.backup_local(filename) try: update(plist, working_values) plist_dir = os.path.dirname(filename) if not os.path.exists(plist_dir): os.makedirs(plist_dir) f = open(filename, 'wb') plistlib.dump(plist, f) except Exception as e: module.fail_json(msg="Can't change %s" % filename, error=str(e)) return changed
Example #13
Source File: plist.py From GenSMBIOS with MIT License | 5 votes |
def _read_ints(self, n, size): data = self._fp.read(size * n) if size in _BINARY_FORMAT: return struct.unpack('>' + _BINARY_FORMAT[size] * n, data) else: if not size or len(data) != size * n: raise InvalidFileException() return tuple(int.from_bytes(data[i: i + size], 'big') for i in range(0, size * n, size))
Example #14
Source File: text_utils.py From sal with Apache License 2.0 | 5 votes |
def is_valid_plist(data: Text) -> bool: if isinstance(data, str): data = data.encode() try: plistlib.loads(data) return True except (plistlib.InvalidFileException, ExpatError): return False
Example #15
Source File: text_utils.py From sal with Apache License 2.0 | 5 votes |
def submission_plist_loads(data: Text, compression: str = '') -> Plist: if compression: data = decode_submission_data(data, compression) if isinstance(data, str): data = data.encode() try: plist = plistlib.loads(data) except (plistlib.InvalidFileException, ExpatError): logger.warning("Submission data failed plist deserialization: '%s'", data) plist = {} return plist
Example #16
Source File: utils.py From truegaze with Apache License 2.0 | 5 votes |
def get_ios_manifest(zip_file): """ Check if this is an iOS application by looking for the application and its plist :param zip_file: zipfile.ZipFile to scan :return: path to the iOS plist file """ # IPA files have a /Payload/[something].app directory with the plist file in it, try to find it via regex paths = TruegazeUtils.get_matching_paths_from_zip(zip_file, IOS_PATTERN, True) # Check if the path was found and try to parse if len(paths) > 0: plist_path = paths[0] plist_contents = zip_file.read(plist_path) try: plist_dic = plistlib.loads(plist_contents) except plistlib.InvalidFileException: return None # Test to make sure some required keys are present if ('CFBundleIdentifier' in plist_dic) and \ ('CFBundleShortVersionString' in plist_dic): return plist_path # Otherwise, return None if not detected return None
Example #17
Source File: plist.py From Web-Driver-Toolkit with MIT License | 5 votes |
def _read_ints(self, n, size): data = self._fp.read(size * n) if size in _BINARY_FORMAT: return struct.unpack('>' + _BINARY_FORMAT[size] * n, data) else: if not size or len(data) != size * n: raise InvalidFileException() return tuple(int.from_bytes(data[i: i + size], 'big') for i in range(0, size * n, size))
Example #18
Source File: plist.py From SSDTTime with MIT License | 5 votes |
def _read_ints(self, n, size): data = self._fp.read(size * n) if size in _BINARY_FORMAT: return struct.unpack('>' + _BINARY_FORMAT[size] * n, data) else: if not size or len(data) != size * n: raise InvalidFileException() return tuple(int.from_bytes(data[i: i + size], 'big') for i in range(0, size * n, size))
Example #19
Source File: plist.py From ProperTree with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _read_ints(self, n, size): data = self._fp.read(size * n) if size in _BINARY_FORMAT: return struct.unpack('>' + _BINARY_FORMAT[size] * n, data) else: if not size or len(data) != size * n: raise InvalidFileException() return tuple(int.from_bytes(data[i: i + size], 'big') for i in range(0, size * n, size))
Example #20
Source File: attachments.py From zentral with Apache License 2.0 | 5 votes |
def __init__(self, f): try: self._pl = plistlib.load(f) except plistlib.InvalidFileException: # maybe a signed plist infile, _ = self.save_tempory_file(f) outfile_fd, outfile = tempfile.mkstemp() outfile_f = os.fdopen(outfile_fd, "rb") try: # TODO: noverify -> verify signature ??? subprocess.check_call(["/usr/bin/openssl", "smime", "-verify", "-in", infile, "-inform", "DER", "-noverify", "-out", outfile]) except subprocess.CalledProcessError: # not a valid raise AttachmentError("Unable to read plist") else: try: self._pl = plistlib.load(outfile_f) except plistlib.InvalidFileException: raise AttachmentError("Signed data not a plist") finally: os.remove(infile) outfile_f.close() os.unlink(outfile) # extract attributes for attr, pl_attr in (("name", "PayloadDisplayName"), ("identifier", "PayloadIdentifier")): try: setattr(self, attr, self._pl[pl_attr]) except KeyError: raise AttachmentError("Plist without {}".format(pl_attr))
Example #21
Source File: plist.py From gibMacOS with MIT License | 5 votes |
def _read_ints(self, n, size): data = self._fp.read(size * n) if size in _BINARY_FORMAT: return struct.unpack('>' + _BINARY_FORMAT[size] * n, data) else: if not size or len(data) != size * n: raise InvalidFileException() return tuple(int.from_bytes(data[i: i + size], 'big') for i in range(0, size * n, size))
Example #22
Source File: plist.py From USBMap with MIT License | 5 votes |
def _read_ints(self, n, size): data = self._fp.read(size * n) if size in _BINARY_FORMAT: return struct.unpack('>' + _BINARY_FORMAT[size] * n, data) else: if not size or len(data) != size * n: raise InvalidFileException() return tuple(int.from_bytes(data[i: i + size], 'big') for i in range(0, size * n, size))
Example #23
Source File: plist.py From Web-Driver-Toolkit with MIT License | 4 votes |
def load(fp, fmt=None, use_builtin_types=None, dict_type=dict): if _check_py3(): use_builtin_types = True if use_builtin_types == None else use_builtin_types # We need to monkey patch this to allow for hex integers - code taken/modified from # https://github.com/python/cpython/blob/3.8/Lib/plistlib.py if fmt is None: header = fp.read(32) fp.seek(0) for info in plistlib._FORMATS.values(): if info['detect'](header): P = info['parser'] break else: raise plistlib.InvalidFileException() else: P = plistlib._FORMATS[fmt]['parser'] p = P(use_builtin_types=use_builtin_types, dict_type=dict_type) if isinstance(p,plistlib._PlistParser): # Monkey patch! def end_integer(): d = p.get_data() p.add_object(int(d,16) if d.lower().startswith("0x") else int(d)) p.end_integer = end_integer return p.parse(fp) elif not _is_binary(fp): # Is not binary - assume a string - and try to load # We avoid using readPlistFromString() as that uses # cStringIO and fails when Unicode strings are detected # Don't subclass - keep the parser local from xml.parsers.expat import ParserCreate # Create a new PlistParser object - then we need to set up # the values and parse. p = plistlib.PlistParser() # We also need to monkey patch this to allow for other dict_types def begin_dict(attrs): d = dict_type() p.addObject(d) p.stack.append(d) def end_integer(): d = p.getData() p.addObject(int(d,16) if d.lower().startswith("0x") else int(d)) p.begin_dict = begin_dict p.end_integer = end_integer parser = ParserCreate() parser.StartElementHandler = p.handleBeginElement parser.EndElementHandler = p.handleEndElement parser.CharacterDataHandler = p.handleData if isinstance(fp, unicode): # Encode unicode -> string; use utf-8 for safety fp = fp.encode("utf-8") if isinstance(fp, basestring): # It's a string - let's wrap it up fp = StringIO(fp) # Parse it parser.ParseFile(fp) return p.root else: use_builtin_types = False if use_builtin_types == None else use_builtin_types p = _BinaryPlistParser(use_builtin_types=use_builtin_types, dict_type=dict_type) return p.parse(fp)
Example #24
Source File: plist.py From SSDTTime with MIT License | 4 votes |
def load(fp, fmt=None, use_builtin_types=None, dict_type=dict): if _check_py3(): use_builtin_types = True if use_builtin_types == None else use_builtin_types # We need to monkey patch this to allow for hex integers - code taken/modified from # https://github.com/python/cpython/blob/3.8/Lib/plistlib.py if fmt is None: header = fp.read(32) fp.seek(0) for info in plistlib._FORMATS.values(): if info['detect'](header): P = info['parser'] break else: raise plistlib.InvalidFileException() else: P = plistlib._FORMATS[fmt]['parser'] p = P(use_builtin_types=use_builtin_types, dict_type=dict_type) if isinstance(p,plistlib._PlistParser): # Monkey patch! def end_integer(): d = p.get_data() p.add_object(int(d,16) if d.lower().startswith("0x") else int(d)) p.end_integer = end_integer return p.parse(fp) elif not _is_binary(fp): # Is not binary - assume a string - and try to load # We avoid using readPlistFromString() as that uses # cStringIO and fails when Unicode strings are detected # Don't subclass - keep the parser local from xml.parsers.expat import ParserCreate # Create a new PlistParser object - then we need to set up # the values and parse. p = plistlib.PlistParser() # We also need to monkey patch this to allow for other dict_types def begin_dict(attrs): d = dict_type() p.addObject(d) p.stack.append(d) def end_integer(): d = p.getData() p.addObject(int(d,16) if d.lower().startswith("0x") else int(d)) p.begin_dict = begin_dict p.end_integer = end_integer parser = ParserCreate() parser.StartElementHandler = p.handleBeginElement parser.EndElementHandler = p.handleEndElement parser.CharacterDataHandler = p.handleData if isinstance(fp, unicode): # Encode unicode -> string; use utf-8 for safety fp = fp.encode("utf-8") if isinstance(fp, basestring): # It's a string - let's wrap it up fp = StringIO(fp) # Parse it parser.ParseFile(fp) return p.root else: use_builtin_types = False if use_builtin_types == None else use_builtin_types p = _BinaryPlistParser(use_builtin_types=use_builtin_types, dict_type=dict_type) return p.parse(fp)
Example #25
Source File: plist.py From GenSMBIOS with MIT License | 4 votes |
def load(fp, fmt=None, use_builtin_types=None, dict_type=dict): if _check_py3(): use_builtin_types = True if use_builtin_types == None else use_builtin_types # We need to monkey patch this to allow for hex integers - code taken/modified from # https://github.com/python/cpython/blob/3.8/Lib/plistlib.py if fmt is None: header = fp.read(32) fp.seek(0) for info in plistlib._FORMATS.values(): if info['detect'](header): P = info['parser'] break else: raise plistlib.InvalidFileException() else: P = plistlib._FORMATS[fmt]['parser'] p = P(use_builtin_types=use_builtin_types, dict_type=dict_type) if isinstance(p,plistlib._PlistParser): # Monkey patch! def end_integer(): d = p.get_data() p.add_object(int(d,16) if d.lower().startswith("0x") else int(d)) p.end_integer = end_integer return p.parse(fp) elif not _is_binary(fp): # Is not binary - assume a string - and try to load # We avoid using readPlistFromString() as that uses # cStringIO and fails when Unicode strings are detected # Don't subclass - keep the parser local from xml.parsers.expat import ParserCreate # Create a new PlistParser object - then we need to set up # the values and parse. p = plistlib.PlistParser() # We also need to monkey patch this to allow for other dict_types def begin_dict(attrs): d = dict_type() p.addObject(d) p.stack.append(d) def end_integer(): d = p.getData() p.addObject(int(d,16) if d.lower().startswith("0x") else int(d)) p.begin_dict = begin_dict p.end_integer = end_integer parser = ParserCreate() parser.StartElementHandler = p.handleBeginElement parser.EndElementHandler = p.handleEndElement parser.CharacterDataHandler = p.handleData if isinstance(fp, unicode): # Encode unicode -> string; use utf-8 for safety fp = fp.encode("utf-8") if isinstance(fp, basestring): # It's a string - let's wrap it up fp = StringIO(fp) # Parse it parser.ParseFile(fp) return p.root else: use_builtin_types = False if use_builtin_types == None else use_builtin_types p = _BinaryPlistParser(use_builtin_types=use_builtin_types, dict_type=dict_type) return p.parse(fp)
Example #26
Source File: plist.py From ProperTree with BSD 3-Clause "New" or "Revised" License | 4 votes |
def load(fp, fmt=None, use_builtin_types=None, dict_type=dict): if _check_py3(): use_builtin_types = True if use_builtin_types == None else use_builtin_types # We need to monkey patch this to allow for hex integers - code taken/modified from # https://github.com/python/cpython/blob/3.8/Lib/plistlib.py if fmt is None: header = fp.read(32) fp.seek(0) for info in plistlib._FORMATS.values(): if info['detect'](header): P = info['parser'] break else: raise plistlib.InvalidFileException() else: P = plistlib._FORMATS[fmt]['parser'] p = P(use_builtin_types=use_builtin_types, dict_type=dict_type) if isinstance(p,plistlib._PlistParser): # Monkey patch! def end_integer(): d = p.get_data() p.add_object(int(d,16) if d.lower().startswith("0x") else int(d)) p.end_integer = end_integer return p.parse(fp) elif not _is_binary(fp): # Is not binary - assume a string - and try to load # We avoid using readPlistFromString() as that uses # cStringIO and fails when Unicode strings are detected # Don't subclass - keep the parser local from xml.parsers.expat import ParserCreate # Create a new PlistParser object - then we need to set up # the values and parse. p = plistlib.PlistParser() # We also need to monkey patch this to allow for other dict_types def begin_dict(attrs): d = dict_type() p.addObject(d) p.stack.append(d) def end_integer(): d = p.getData() p.addObject(int(d,16) if d.lower().startswith("0x") else int(d)) p.begin_dict = begin_dict p.end_integer = end_integer parser = ParserCreate() parser.StartElementHandler = p.handleBeginElement parser.EndElementHandler = p.handleEndElement parser.CharacterDataHandler = p.handleData if isinstance(fp, unicode): # Encode unicode -> string; use utf-8 for safety fp = fp.encode("utf-8") if isinstance(fp, basestring): # It's a string - let's wrap it up fp = StringIO(fp) # Parse it parser.ParseFile(fp) return p.root else: use_builtin_types = False if use_builtin_types == None else use_builtin_types p = _BinaryPlistParser(use_builtin_types=use_builtin_types, dict_type=dict_type) return p.parse(fp)
Example #27
Source File: app_json.py From commandment with MIT License | 4 votes |
def upload_profile(): """Upload a custom profile using multipart/form-data I.E from an upload input. Encrypted profiles are not supported. The profiles contents will be stored using the following process: - For the top level profile (and each payload) there is a marshmallow schema which maps the payload keys into the SQLAlchemy model keys. It is also the responsibility of the marshmallow schema to be the validator for uploaded profiles. - The profile itself is inserted as a Profile model. - Each payload is unmarshalled using marshmallow to a specific Payload model. Each specific model contains a join table inheritance to the base ``payloads`` table. The returned body contains a jsonapi object with details of the newly created profile and associated payload ID's. Note: Does not support ``application/x-www-form-urlencoded`` TODO: - Support signed profiles :reqheader Accept: application/vnd.api+json :reqheader Content-Type: multipart/form-data :resheader Content-Type: application/vnd.api+json :statuscode 201: profile created :statuscode 400: If the request contained malformed or missing payload data. :statuscode 500: If something else went wrong with parsing or persisting the payload(s) """ if 'file' not in request.files: abort(400, 'no file uploaded in request data') f = request.files['file'] if not f.content_type == 'application/x-apple-aspen-config': abort(400, 'incorrect MIME type in request') try: data = f.read() plist = plistlib.loads(data) profile = ProfilePlistSchema().load(plist).data except plistlib.InvalidFileException as e: current_app.logger.error(e) abort(400, 'invalid plist format supplied') except BaseException as e: # TODO: separate errors for exceptions caught here current_app.logger.error(e) abort(400, 'cannot parse the supplied profile') profile.data = data db.session.add(profile) db.session.commit() profile_schema = ProfileSchema() model_data = profile_schema.dump(profile).data resp = make_response(jsonify(model_data), 201, {'Content-Type': 'application/vnd.api+json'}) return resp
Example #28
Source File: plist.py From gibMacOS with MIT License | 4 votes |
def load(fp, fmt=None, use_builtin_types=None, dict_type=dict): if _check_py3(): use_builtin_types = True if use_builtin_types == None else use_builtin_types # We need to monkey patch this to allow for hex integers - code taken/modified from # https://github.com/python/cpython/blob/3.8/Lib/plistlib.py if fmt is None: header = fp.read(32) fp.seek(0) for info in plistlib._FORMATS.values(): if info['detect'](header): P = info['parser'] break else: raise plistlib.InvalidFileException() else: P = plistlib._FORMATS[fmt]['parser'] p = P(use_builtin_types=use_builtin_types, dict_type=dict_type) if isinstance(p,plistlib._PlistParser): # Monkey patch! def end_integer(): d = p.get_data() p.add_object(int(d,16) if d.lower().startswith("0x") else int(d)) p.end_integer = end_integer return p.parse(fp) elif not _is_binary(fp): # Is not binary - assume a string - and try to load # We avoid using readPlistFromString() as that uses # cStringIO and fails when Unicode strings are detected # Don't subclass - keep the parser local from xml.parsers.expat import ParserCreate # Create a new PlistParser object - then we need to set up # the values and parse. p = plistlib.PlistParser() # We also need to monkey patch this to allow for other dict_types def begin_dict(attrs): d = dict_type() p.addObject(d) p.stack.append(d) def end_integer(): d = p.getData() p.addObject(int(d,16) if d.lower().startswith("0x") else int(d)) p.begin_dict = begin_dict p.end_integer = end_integer parser = ParserCreate() parser.StartElementHandler = p.handleBeginElement parser.EndElementHandler = p.handleEndElement parser.CharacterDataHandler = p.handleData if isinstance(fp, unicode): # Encode unicode -> string; use utf-8 for safety fp = fp.encode("utf-8") if isinstance(fp, basestring): # It's a string - let's wrap it up fp = StringIO(fp) # Parse it parser.ParseFile(fp) return p.root else: use_builtin_types = False if use_builtin_types == None else use_builtin_types p = _BinaryPlistParser(use_builtin_types=use_builtin_types, dict_type=dict_type) return p.parse(fp)
Example #29
Source File: plist.py From USBMap with MIT License | 4 votes |
def load(fp, fmt=None, use_builtin_types=None, dict_type=dict): if _check_py3(): use_builtin_types = True if use_builtin_types == None else use_builtin_types # We need to monkey patch this to allow for hex integers - code taken/modified from # https://github.com/python/cpython/blob/3.8/Lib/plistlib.py if fmt is None: header = fp.read(32) fp.seek(0) for info in plistlib._FORMATS.values(): if info['detect'](header): P = info['parser'] break else: raise plistlib.InvalidFileException() else: P = plistlib._FORMATS[fmt]['parser'] p = P(use_builtin_types=use_builtin_types, dict_type=dict_type) if isinstance(p,plistlib._PlistParser): # Monkey patch! def end_integer(): d = p.get_data() p.add_object(int(d,16) if d.lower().startswith("0x") else int(d)) p.end_integer = end_integer return p.parse(fp) elif not _is_binary(fp): # Is not binary - assume a string - and try to load # We avoid using readPlistFromString() as that uses # cStringIO and fails when Unicode strings are detected # Don't subclass - keep the parser local from xml.parsers.expat import ParserCreate # Create a new PlistParser object - then we need to set up # the values and parse. p = plistlib.PlistParser() # We also need to monkey patch this to allow for other dict_types def begin_dict(attrs): d = dict_type() p.addObject(d) p.stack.append(d) def end_integer(): d = p.getData() p.addObject(int(d,16) if d.lower().startswith("0x") else int(d)) p.begin_dict = begin_dict p.end_integer = end_integer parser = ParserCreate() parser.StartElementHandler = p.handleBeginElement parser.EndElementHandler = p.handleEndElement parser.CharacterDataHandler = p.handleData if isinstance(fp, unicode): # Encode unicode -> string; use utf-8 for safety fp = fp.encode("utf-8") if isinstance(fp, basestring): # It's a string - let's wrap it up fp = StringIO(fp) # Parse it parser.ParseFile(fp) return p.root else: use_builtin_types = False if use_builtin_types == None else use_builtin_types p = _BinaryPlistParser(use_builtin_types=use_builtin_types, dict_type=dict_type) return p.parse(fp)
Example #30
Source File: test_plistlib.py From android_universal with MIT License | 4 votes |
def test_invalid_binary(self): for data in [ # too short data b'', # too large offset_table_offset and nonstandard offset_size b'\x00\x08' b'\x00\x00\x00\x00\x00\x00\x03\x01' b'\x00\x00\x00\x00\x00\x00\x00\x01' b'\x00\x00\x00\x00\x00\x00\x00\x00' b'\x00\x00\x00\x00\x00\x00\x00\x2a', # integer overflow in offset_table_offset b'\x00\x08' b'\x00\x00\x00\x00\x00\x00\x01\x01' b'\x00\x00\x00\x00\x00\x00\x00\x01' b'\x00\x00\x00\x00\x00\x00\x00\x00' b'\xff\xff\xff\xff\xff\xff\xff\xff', # offset_size = 0 b'\x00\x08' b'\x00\x00\x00\x00\x00\x00\x00\x01' b'\x00\x00\x00\x00\x00\x00\x00\x01' b'\x00\x00\x00\x00\x00\x00\x00\x00' b'\x00\x00\x00\x00\x00\x00\x00\x09', # ref_size = 0 b'\xa1\x01\x00\x08\x0a' b'\x00\x00\x00\x00\x00\x00\x01\x00' b'\x00\x00\x00\x00\x00\x00\x00\x02' b'\x00\x00\x00\x00\x00\x00\x00\x00' b'\x00\x00\x00\x00\x00\x00\x00\x0b', # integer overflow in offset b'\x00\xff\xff\xff\xff\xff\xff\xff\xff' b'\x00\x00\x00\x00\x00\x00\x08\x01' b'\x00\x00\x00\x00\x00\x00\x00\x01' b'\x00\x00\x00\x00\x00\x00\x00\x00' b'\x00\x00\x00\x00\x00\x00\x00\x09', # invalid ASCII b'\x51\xff\x08' b'\x00\x00\x00\x00\x00\x00\x01\x01' b'\x00\x00\x00\x00\x00\x00\x00\x01' b'\x00\x00\x00\x00\x00\x00\x00\x00' b'\x00\x00\x00\x00\x00\x00\x00\x0a', # invalid UTF-16 b'\x61\xd8\x00\x08' b'\x00\x00\x00\x00\x00\x00\x01\x01' b'\x00\x00\x00\x00\x00\x00\x00\x01' b'\x00\x00\x00\x00\x00\x00\x00\x00' b'\x00\x00\x00\x00\x00\x00\x00\x0b', ]: with self.assertRaises(plistlib.InvalidFileException): plistlib.loads(b'bplist00' + data, fmt=plistlib.FMT_BINARY)