#!/usr/bin/python ############################################################################# # Copyright (C) 2011 Spidcom ############################################################################# from scapy.fields import * class XLEShortField (LEShortField): """This class is used to represent LEShortField (scapy) in Hexadecimal representation, this functionality should exist in the future version of scapy called 'XLEShortField' """ def i2repr(self, pkt, x): if x is None: x = 0 return lhex(self.i2h(pkt, x)) class LEBitField (BitField): def __init__(self, name, default, size): BitField.__init__(self, name, default, size) def addfield (self, pkt, s, val): val = self.i2m(pkt, val) if type(s) is tuple: s,bitsdone,v = s else: bitsdone = 0 v = 0 if self.rev: val = self.reverse(val) v |= val << bitsdone bitsdone += self.size while bitsdone >= 8: bitsdone -= 8 s = s+struct.pack("B", v >> bitsdone) v &= (1L<> bn # get rid of high order bits b &= (1L << (self.size)) - 1 if self.rev: b = self.reverse(b) bn += self.size s = s[bn/8:] if bn % 8 != 0: sbyte = struct.unpack ('B', s[0])[0] decal_bits = (8 - (nb_bytes * 8 - self.size)) sbyte &= ~((1 << decal_bits) - 1) sbyte = struct.pack ('B', sbyte) s = sbyte + s[1:] bn = bn%8 b = self.m2i(pkt, b) if bn: return (s,bn),b else: return s,b class XLEBitField (LEBitField): """This class is used to represent LEShortField (scapy) in Hexadecimal representation, this functionality should exist in the future version of scapy called 'XLEShortField' """ def i2repr(self, pkt, x): if x is None: x = 0 return lhex(self.i2h(pkt, x)) class HPAVNIDField (Field): """Special Field for NID HPAV field.""" def __init__(self, name, default): Field.__init__(self, name, default, "7s") def addfield (self, pkt, s, val): nb_bytes = 7 w = struct.pack ('Q', val) w = w[:nb_bytes] return s + w def getfield (self, pkt, s): nb_bytes = 7 w = "%s\0" % s[0:nb_bytes] w = struct.unpack ('Q', w)[0] s = s[nb_bytes:] return s,w class HPAVTonemapField (Field): """Special Field for Tonemap HPAV field.""" def __init__(self, name, default): Field.__init__(self, name, default, "768c") # TODO: Implement addfield (self, pkt, s, val) if needed def getfield (self, pkt, s): w = s[0:self.sz] result = "" for c in w: valh = (ord(c) >> 4) & 0xf vall = ord(c) & 0xf if valh <= 9: valh += ord ('0') else: valh += ord ('a') - 10 if vall <= 9: vall += ord ('0') else: vall += ord ('a') - 10 result += chr (vall) result += chr (valh) s = s[self.sz:] return s, result def extract_padding(self, s): return "", s