diff --git a/klvdata/elementparser.py b/klvdata/elementparser.py index aaa38de..03fdcec 100755 --- a/klvdata/elementparser.py +++ b/klvdata/elementparser.py @@ -129,7 +129,10 @@ def __str__(self): class MappedElementParser(ElementParser, metaclass=ABCMeta): def __init__(self, value): - super().__init__(MappedValue(value, self._domain, self._range)) + try: + super().__init__(MappedValue(value, self._domain, self._range)) + except ValueError: + print('Error in decoding Tag n.', self.key.hex(), ' (hexadecimal)') @property @classmethod diff --git a/klvdata/misb0102.py b/klvdata/misb0102.py index af09973..33fd7ee 100755 --- a/klvdata/misb0102.py +++ b/klvdata/misb0102.py @@ -23,10 +23,13 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +from klvdata.common import hexstr_to_bytes from klvdata.element import UnknownElement from klvdata.elementparser import BytesElementParser +from klvdata.elementparser import StringElementParser from klvdata.misb0601 import UASLocalMetadataSet from klvdata.setparser import SetParser +from klvdata.streamparser import StreamParser _classifying_country_coding = { b'\x01': 'ISO-3166 Two Letter', @@ -83,6 +86,13 @@ class SecurityLocalMetadataSet(SetParser): Must be a subclass of Element or duck type Element. """ key, name = b'\x30', "Security Local Metadata Set" + + TAG = 48 + UDSKey = hexstr_to_bytes('06 0E 2B 34 - 02 03 01 01 – 0E 01 03 03 - 02 00 00 00') + LDSName = "Security Local Metadata Set" + ESDName = "" + UDSName = "" + parsers = {} _unknown_element = UnknownElement @@ -97,7 +107,12 @@ class SecurityClassification(BytesElementParser): accordance with U.S. and NATO classification guidance. """ key = b'\x01' - + TAG = 1 + UDSKey = "-" + LDSName = "Security Classification" + ESDName = "" + UDSName = "" + _classification = { b'\x01': 'UNCLASSIFIED', b'\x02': 'RESTRICTED', @@ -105,3 +120,200 @@ class SecurityClassification(BytesElementParser): b'\x04': 'SECRET', b'\x05': 'TOP SECRET', } + + +@SecurityLocalMetadataSet.add_parser +class ClassifyingCountryAndReleasingInstructionCCM(BytesElementParser): + """ + """ + key = b'\x02' + TAG = 2 + UDSKey = "-" + LDSName = "Classifying Country And Releasing Instruction Country Coding Method" + ESDName = "" + UDSName = "" + + _classification = _classifying_country_coding + + +@SecurityLocalMetadataSet.add_parser +class ClassifyingCountry(StringElementParser): + """ + """ + key = b'\x03' + TAG = 3 + UDSKey = "-" + LDSName = "Classyfing Country" + ESDName = "" + UDSName = "" + + +@SecurityLocalMetadataSet.add_parser +class SecuritySCISHIInformation(StringElementParser): + """ + """ + key = b'\x04' + TAG = 4 + UDSKey = "-" + LDSName = 'Security-SCI/SHI Information' + ESDName = "" + UDSName = "" + + +@SecurityLocalMetadataSet.add_parser +class Caveats(StringElementParser): + """ + """ + key = b'\x05' + TAG = 5 + UDSKey = "-" + LDSName = 'Caveats' + ESDName = "" + UDSName = "" + + +@SecurityLocalMetadataSet.add_parser +class ReleasingInstructions(StringElementParser): + """ + """ + key = b'\x06' + TAG = 6 + UDSKey = "-" + LDSName = 'Releasing Instructions' + ESDName = "" + UDSName = "" + + +@SecurityLocalMetadataSet.add_parser +class ClassifiedBy(StringElementParser): + """ + """ + key = b'\x07' + TAG = 7 + UDSKey = "-" + LDSName = 'Classified By' + ESDName = "" + UDSName = "" + + +@SecurityLocalMetadataSet.add_parser +class DerivedFrom(StringElementParser): + """ + """ + key = b'\x08' + TAG = 8 + UDSKey = "-" + LDSName = 'Derived From' + ESDName = "" + UDSName = "" + + +@SecurityLocalMetadataSet.add_parser +class ClassificationReason(StringElementParser): + """ + """ + key = b'\x09' + TAG = 9 + UDSKey = "-" + LDSName = 'Classification Reason' + ESDName = "" + UDSName = "" + + +@SecurityLocalMetadataSet.add_parser +class DeclassificationDate(StringElementParser): + """ + """ + key = b'\x0A' + TAG = 10 + UDSKey = "-" + LDSName = 'Declassification Date' + ESDName = "" + UDSName = "" + min_length, max_length = 8, 8 + + +@SecurityLocalMetadataSet.add_parser +class ClassificationAndMarkingSystem(StringElementParser): + """ + """ + key = b'\x0B' + TAG = 11 + UDSKey = "-" + LDSName = 'Classification And Marking System' + ESDName = "" + UDSName = "" + + +@SecurityLocalMetadataSet.add_parser +class ObjectCountryCodingMethod(BytesElementParser): + """ + """ + key = b'\x0C' + TAG = 12 + UDSKey = "-" + LDSName = 'Object Country Coding Method' + ESDName = "" + UDSName = "" + + _classification = _object_country_coding + + +@SecurityLocalMetadataSet.add_parser +class ObjectCountryCodes(StringElementParser): + """ + """ + key = b'\x0D' + TAG = 13 + UDSKey = "-" + LDSName = 'Object Country Codes' + ESDName = "" + UDSName = "" + + +@SecurityLocalMetadataSet.add_parser +class ClassificationComments(StringElementParser): + """ + """ + key = b'\x0E' + TAG = 14 + UDSKey = "-" + LDSName = 'Classification Comments' + ESDName = "" + UDSName = "" + + +@SecurityLocalMetadataSet.add_parser +class Version(BytesElementParser): + """ + """ + key = b'\x16' + TAG = 22 + UDSKey = "-" + LDSName = 'Version' + ESDName = "" + UDSName = "" + + +@SecurityLocalMetadataSet.add_parser +class ClassifyingCountryAndReleasingInstructionCCMVD(StringElementParser): + """ + """ + key = b'\x17' + TAG = 23 + UDSKey = "-" + LDSName = 'Classifying Country And Releasing Instruction Courntry Coding Method Version Date' + ESDName = "" + UDSName = "" + + +@SecurityLocalMetadataSet.add_parser +class ClassifyingCountryCodeMethodVersionDate(StringElementParser): + """ + """ + key = b'\x18' + TAG = 24 + UDSKey = "-" + LDSName = 'Classifying Country Code Method Version Date' + ESDName = "" + UDSName = "" diff --git a/klvdata/misb0601.py b/klvdata/misb0601.py index 159924e..fbf5aa5 100755 --- a/klvdata/misb0601.py +++ b/klvdata/misb0601.py @@ -660,14 +660,14 @@ class GenericFlagData01(MappedElementParser): _range = (0, 2**8-1) -# @UASLocalMetadataSet.add_parser -# class SecurityLocalMetadataSet(MappedElementParser): -# key = b'\x30' -# TAG = 48 -# UDSKey = "06 0E 2B 34 02 03 01 01 0E 01 03 03 02 00 00 00" -# LDSName = "Security Local Set" -# ESDName = "" -# UDSName = "Security Local Set" +@UASLocalMetadataSet.add_parser +class SecurityLocalMetadataSet(MappedElementParser): + key = b'\x30' + TAG = 48 + UDSKey = "06 0E 2B 34 02 03 01 01 0E 01 03 03 02 00 00 00" + LDSName = "Security Local Set" + ESDName = "" + UDSName = "Security Local Set" @UASLocalMetadataSet.add_parser diff --git a/klvdata/setparser.py b/klvdata/setparser.py index decbcb1..10a5f1c 100755 --- a/klvdata/setparser.py +++ b/klvdata/setparser.py @@ -63,6 +63,7 @@ def parse(self): except KeyError: self.items[key] = self._unknown_element(key, value) + @classmethod def add_parser(cls, obj): """Decorator method used to register a parser to the class parsing repertoire. @@ -100,18 +101,26 @@ def __str__(self): return str_dict(self.items) def MetadataList(self): + ''' Return metadata dictionary''' + metadata = {} - def repeat(items, indent=1): + def repeat(items, indent=1, parentTAG=0): for item in items: try: - metadata[item.TAG] = (item.LDSName, item.ESDName, item.UDSName, str(item.value.value)) + if not parentTAG : + metadata[item.TAG] = (item.LDSName, item.ESDName, item.UDSName, str(item.value.value)) + else : + metadata[parentTAG][len(metadata[parentTAG])-1][item.TAG] = (item.LDSName, item.ESDName, item.UDSName, str(item.value.value)) except: None if hasattr(item, 'items'): - repeat(item.items.values(), indent + 1) + metadata[item.TAG] = (item.LDSName, item.ESDName, item.UDSName, {}) + repeat(item.items.values(), indent + 1, item.TAG) + repeat(self.items.values()) + return OrderedDict(metadata) def structure(self):