示例#1
0
    def checkJSON(field):
        id_ = checkAttr('id', field)
        checkInteger('id', id_, 1, 2**15 - 1)

        pen = None
        if ('pen' in field):
            pen = checkAttr('pen', field)
            checkInteger('pen', pen, 1, 2**32 - 1)

        name = checkAttr('name', field)
        fieldInfo = getIANAFieldById(id_) if (
            pen is None) else getPENFieldById(id_, pen)
        fieldName = fieldInfo[1]['name']
        if (name != fieldName):
            raise Exception('Field name(%s) does not match id(%d)' %
                            (name, id_))

        length = None
        if ('length' in field):
            length = checkAttr('length', field)
            checkInteger('length', length, 1)

        type_ = checkAttr('type', field)
        getStructForType(ie_type=type_, fieldName=name, length=length)

        return (id_, pen, name, length, type_)
示例#2
0
    def write(self, rawData):
        try:
            if(self.value is None): self.value = 0
            if(self.field.variableLength):
                from ADT_BasicList import ADT_BasicList
                from ADT_SubTemplateList import ADT_SubTemplateList
                if(isinstance(self.value, (str, basestring, unicode))):
                    if(self.field.type not in ['string', 'octetArray']):
                        raise Exception('Invalid field type(%s). Should be one of %s' % (str(type(self.value)), str(('string', 'octetArray'))))
                    self.length = len(self.value)
                elif(isinstance(self.value, (ADT_BasicList,))):
                    if(self.field.type not in ['basicList']):
                        raise Exception('Invalid field type(%s). Should be one of %s' % (str(type(self.value)), str(('basicList',))))
                elif(isinstance(self.value, (ADT_SubTemplateList,))):
                    if(self.field.type not in ['subTemplateList']):
                        raise Exception('Invalid field type(%s). Should be one of %s' % (str(type(self.value)), str(('subTemplateList',))))
                else:
                    raise Exception('Invalid value type(%s). Should be one of %s' % (str(type(self.value)), str((str, basestring, unicode, ADT_BasicList, ADT_SubTemplateList))))

                if(self.length < 255):
                    rawData.write(FieldValue._strShortLength.pack(self.length))
                else:
                    rawData.write(FieldValue._strShortLength.pack(255))
                    rawData.write(FieldValue._strLongLength.pack(self.length))

                if(self.field.type == 'string'):
                    self.value = tuple(str(self.value)) # convert string to char values
                    struct_ = getStructForType(self.field.type, self.field.name, self.length)
                    rawData.write(struct_.pack(*self.value))
                elif(self.field.type == 'octetArray'):
                    self.value = tuple(map(ord, tuple(self.value))) # convert string to ascii values
                    struct_ = getStructForType(self.field.type, self.field.name, self.length)
                    rawData.write(struct_.pack(*self.value))
                elif(self.field.type in ['basicList', 'subTemplateList']):
                    self.value.write(rawData)

            else:
                if(isinstance(self.value, (str, basestring, unicode))):
                    self.length = len(self.value)
                    if(self.field.type=='string'):
                        if(self.field.struct_.size > self.length):
                            self.value = self.value + ' ' * (self.field.struct_.size - self.length)
                        elif(self.field.struct_.size < self.length):
                            self.value = self.value[:self.field.struct_.size]
                        self.value = tuple(str(self.value)) # convert string to char values
                        #self.value = tuple('{:\0<16}'.format(self.value[:16]))
                    elif(self.field.type=='octetArray'):
                        self.value = tuple(map(ord, tuple(self.value))) # convert string to ascii values
                if(isinstance(self.value, (tuple, list))):
                    rawData.write(self.field.struct_.pack(*self.value))
                else:
                    rawData.write(self.field.struct_.pack(self.value))
        except Exception as e:
            logger = logging.getLogger(__name__)
            logger.error('Error writing field(%s) with struct(%s) length(%d) rawData(%s)' % (
                            self.field.name,
                            self.field.struct_.format,
                            self.field.struct_.size,
                            str(self.value)))
            logger.exception(e)
示例#3
0
    def _validateFieldAttr(cls, obj, field):
        obj.name = field['name']
        obj.type = field['type']
        obj.struct_ = getStructForType(obj.type, obj.name, obj.length)
        if (not obj.variableLength):
            #obj.struct_ = getStructForType(obj.type, obj.name, obj.length)
            if (isinstance(obj.struct_, (struct.Struct, ))):
                if (obj.length is None):
                    obj.length = obj.struct_.size
            elif (isinstance(obj.struct_,
                             (TypeBasicList, TypeSubTemplateList))):
                pass
            else:
                raise Exception('Unknown FieldType(%s)' % str(obj.struct_))

        if (isinstance(obj.struct_, (TypeBasicList, TypeSubTemplateList))):
            obj.length = VARIABLE_LENGTH
            obj.variableLength = True
        else:
            cls._reducePattern(obj)
            obj.minValue = field['minValue'] if (
                field.has_key('minValue')) else None
            obj.maxValue = field['maxValue'] if (
                field.has_key('maxValue')) else None
            obj.choose = field['choose'] if (field.has_key('choose')) else None
示例#4
0
 def create(cls, field, value, recordLength):
     checkType('field', (FieldSpecifier,), field)
     
     length = None
     struct_ = field.struct_
     if(field.variableLength):
         if(isinstance(struct_, (TypeBasicList, TypeSubTemplateList))):
             length = value._computeLength()
         else:
             length = len(value)
         if(length > 65535): raise Exception('Maximum length(65535) exceeded: %d' % length)
         struct_ = getStructForType(field.type, field.name, length=length)
     
     if(struct_ is None):
         raise Exception('Undefined struct for FieldSpecifier(%s)' % field.name)
     
     if(field.variableLength):
         if(isinstance(struct_, (TypeBasicList, TypeSubTemplateList))):
             recordLength[0] += length + (3 if(length > 254) else 1)
         else:
             recordLength[0] += struct_.size + (3 if(struct_.size > 254) else 1)
     else:
         recordLength[0] += struct_.size
     
     obj = cls()
     obj.field = field
     obj.struct_ = struct_
     obj.length = length
     obj.value = value
     
     if(not isinstance(struct_, (TypeBasicList, TypeSubTemplateList))):
         if((field.minValue is not None) and (obj.value < field.minValue)): raise Exception('Underflow value(%s) < minValue(%s) in field(%s)' % (str(obj.value), str(field.minValue), field.name))
         if((field.maxValue is not None) and (obj.value > field.maxValue)): raise Exception('Overflow value(%s) > maxValue(%s) in field(%s)' % (str(obj.value), str(field.minValue), field.name))
         if((field.choose is not None) and (obj.value not in field.choose)): raise Exception('Invalid choice(%s) in field(%s)' % (str(obj.value), field.name))
     return(obj)
示例#5
0
    def read(cls, field, rawData, domain):
        checkType('field', (FieldSpecifier,), field)
        
        obj = cls()
        obj.field = field
        
        length = None
        struct_ = None
        if(field.variableLength):
            data = rawData.read(1)
            length = FieldValue._strShortLength.unpack_from(data)[0]
            if(length == 255):
                data = rawData.read(2)
                length = FieldValue._strLongLength.unpack_from(data)[0]
            struct_ = getStructForType(field.type, field.name, length=length)
        else:
            if(field.struct_ is None):
                raise Exception('Undefined struct for FieldSpecifier(%s)' % field.name)
            struct_ = obj.field.struct_
            length = struct_.size
        data = None
        try:
            if(isinstance(struct_, (TypeBasicList,))):
                from ADT_BasicList import ADT_BasicList
                obj.value = ADT_BasicList.read(rawData, length, domain)
            elif(isinstance(struct_, (TypeSubTemplateList,))):
                from ADT_SubTemplateList import ADT_SubTemplateList
                obj.value = ADT_SubTemplateList.read(rawData, length, domain)
            else:
                data = rawData.read(length)
                obj.value = struct_.unpack_from(data)
                if(isinstance(obj.value, (tuple,))):
                    if(obj.field.type=='string'):
                        obj.value = ''.join(map(str, obj.value)) # convert chars to string
                        obj.value = obj.value.strip() # remove leading and tailing whitespaces
                    elif(obj.field.type=='octetArray'):
                        obj.value = ''.join(map(chr, obj.value)) # convert ascii values to string
        except Exception as e:
            logger = logging.getLogger(__name__)
            if(field.variableLength):
                logger.error('Error reading field(%s) with struct(%s) length(%d) rawData(%s)' % (
                                obj.field.name,
                                struct_.format if(struct_ is not None) else None,
                                struct_.size if(struct_ is not None) else length,
                                binascii.hexlify(data) if(data is not None) else data))
            else:
                logger.error('Error reading field(%s) with struct(%s) length(%d) rawData(%s)' % (
                                obj.field.name,
                                obj.field.struct_.format,
                                obj.field.struct_.size,
                                binascii.hexlify(data)))
            logger.exception(e)

        if(not isinstance(struct_, (TypeBasicList, TypeSubTemplateList))):
            if(len(obj.value) == 1): obj.value = obj.value[0]
            if((field.minValue is not None) and (obj.value < field.minValue)): raise Exception('Underflow value(%s) < minValue(%s) in field(%s)' % (str(obj.value), str(field.minValue), field.name))
            if((field.maxValue is not None) and (obj.value > field.maxValue)): raise Exception('Overflow value(%s) > maxValue(%s) in field(%s)' % (str(obj.value), str(field.minValue), field.name))
            if((field.choose is not None) and (obj.value not in field.choose)): raise Exception('Invalid choice(%s) in field(%s)' % (str(obj.value), field.name))
        return(obj)
示例#6
0
 def _reducePattern(cls, field):
     if (field.struct_ is None): return
     if (field.length == field.struct_.size): return
     if (field.variableLength): return
     try:
         reducedType = getReducedType(field.type, field.length)
         field.struct_ = getStructForType(reducedType, field.name)
     except Exception as e:
         logger = logging.getLogger(__name__)
         logger.debug(
             'Exception Reducing Field: name(%s) type(%s) length(%s)' %
             (field.name, field.type, field.length))
         logger.exception(e)