diff -r 000000000000 -r 2e8eeb919028 configurationengine/source/plugins/symbian/ConeHCRPlugin/hcrplugin/hcr_reader.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/configurationengine/source/plugins/symbian/ConeHCRPlugin/hcrplugin/hcr_reader.py Thu Mar 11 17:04:37 2010 +0200 @@ -0,0 +1,151 @@ +# +# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +# All rights reserved. +# This component and the accompanying materials are made available +# under the terms of "Eclipse Public License v1.0" +# which accompanies this distribution, and is available +# at the URL "http://www.eclipse.org/legal/epl-v10.html". +# +# Initial Contributors: +# Nokia Corporation - initial contribution. +# +# Contributors: +# +# Description: +# + +import logging +import __init__ +from hcrplugin.hcrrepository import HcrRecord, HcrRepository +from struct import pack, unpack +from hcrplugin.hcr_writer import VALUE_TYPE_MAP,VALUE_TYPES_WITH_LSD,VALUE_TYPES_UNSIGNED_INT +from hcrplugin.hcr_header import HcrHeader +from hcrplugin.hcr_exceptions import * + + +class HcrReader(object): + + def parse_repository_from_bindata(self, data): + """ + @return: HcrRepository object constructed from the given binary data. + """ + header_data = data[:32] + header = HcrHeader() + header.loads(header_data) + + expected_len = 32 + header.nrecords * 20 + header.lsd_size + if len(data) < expected_len: + raise InvalidHcrDataSizeError("File size is %d, expected at least %d based on header" \ + % (len(data), expected_len)) + + expected_lsd_offset = 32 + header.nrecords * 20 + if header.lsd_offset != expected_lsd_offset: + raise InvalidLsdSectionOffsetError("LSD offset is %d, expected %d" \ + % (header.lsd_offset, expected_lsd_offset)) + + records = [] + for i in xrange(header.nrecords): + record_offset = 32 + i * 20 + record_data = data[record_offset:record_offset+20] + record,lsd_pos = self.parse_record_from_bindata(record_data) + if lsd_pos != None: + if lsd_pos[0] > header.lsd_size or (lsd_pos[0] + lsd_pos[1]) > header.lsd_size: + raise InvalidRecordLsdPositionError( + "LSD offset of record %d (category=%d, element=%d) is %r, but LSD section size is %d" \ + % (i, record.category_id, record.element_id, lsd_pos, header.lsd_size)) + lsd_offset = lsd_pos[0] + header.lsd_offset + lsd_data = data[lsd_offset:lsd_offset+lsd_pos[1]] + record.value = self.parse_record_value_from_lsd_bindata(record.type,lsd_data) + records.append(record) + + return HcrRepository(records,header.version,header.flags) + + def parse_record_from_bindata(self, data): + """ + @return: Tuple: (record, lsd_pos) where + record = HcrRecord object constructed from the given binary data. + lsd_pos = The position of the record's data in the LSD section in the + form of a tuple (offset, size), or None if the record does + not have any data in the LSD section. + """ + + if len(data) != 20: + raise HcrReaderError("Invalid record length: %d, expected 20" % len(data)) + + result = unpack("