Advertisement
topin89

C Struct to dict, unistr or namedtuple

Sep 23rd, 2015
2,033
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.99 KB | None | 0 0
  1. '''
  2.  
  3. The MIT License (MIT)
  4.  
  5. Copyright (c) 2015 Mikhail Gorodetsky
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. THE SOFTWARE.
  24. '''
  25.  
  26. MY_STRUCT="""typedef struct __attribute__ ((__packed__)){
  27.    uint8_t u8;
  28.    uint16_t u16;
  29.    uint32_t u32;
  30.    uint64_t u64;
  31.    int8_t i8;
  32.    int16_t i16;
  33.    int32_t i32;
  34.    int64_t i64;
  35.    long long int lli;
  36.    float flt;
  37.    double dbl;
  38.    char string[12];
  39.    uint64_t array[5];
  40. } debugInfo;"""
  41.  
  42.  
  43. UNPACKED_STRUCT=[1,256,65536,2**32,-1,-256,-65536,-(2**32),42,2.1,3.01,'t','e','s','t','S','t','r','i','n','g','\0','\0',1,2,3,4,5]
  44. PACKED_STRUCT='\x01\x00\x01\x00\x00\x01\x00\x00\x00\x00\x00\x01\x00\x00\x00\xff\x00\xff\x00\x00\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff*\x00\x00\x00\x00\x00\x00\x00ff\x06@\x14\xaeG\xe1z\x14\x08@testString\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00'
  45.  
  46. import re
  47. import struct
  48. from collections import OrderedDict
  49. from collections import namedtuple
  50.  
  51. TYPE_REGEX=re.compile("\\s*([a-zA-Z0-9_ ]+)\\s+(\\w+)(?:\\[(\\d*)\\])?;")
  52. #\s* -- all spaces on left from type name
  53. #([a-zA-Z0-9_ ]+) -- type name
  54. #\s+ -- spaces between type and name
  55. #(\w+) -- variable name
  56. #(?:\[(\d*)\\])? -- elements in array, optional
  57.  
  58. TYPES_DICT={"uint64_t":"Q","uint32_t":"I","uint16_t":"H","uint8_t":"B",
  59.                  "int64_t":"q","int32_t":"i","int16_t":"h","int8_t":"b",
  60.                  "char":"c","bool":"?","float":"f","double":"d",
  61.                  "long long int":"q","unsigned long long int":"Q"};
  62.  
  63.  
  64.  
  65. def structInfo(cStruct, alignment="="):
  66.     pack_format=alignment
  67.     varlist=[];
  68.     for line in cStruct.splitlines():
  69.         try:
  70.             vartype, varname, arrayLength=TYPE_REGEX.findall(line)[0]
  71.             vartype=TYPES_DICT[vartype];
  72.             if arrayLength:
  73.                 arrayLength=int(arrayLength);
  74.             else:
  75.                 arrayLength=1;
  76.  
  77.             pack_format+=vartype*arrayLength
  78.             varlist.append([varname,arrayLength])
  79.         except IndexError:
  80.             pass
  81.     return varlist, pack_format
  82.  
  83.  
  84. def depack_bytearray_to_dict(bindata, cStruct, alignment="="):
  85.     result=OrderedDict()
  86.     varlist,pack_format=structInfo(cStruct,alignment)
  87.     unpacked=struct.unpack(pack_format,bindata);
  88.  
  89.     if pack_format[0] in '@=<>!':
  90.         pack_format=pack_format[1:]
  91.  
  92.     ind=0;
  93.     for varname,arrlen in varlist:
  94.         varname=unicode(varname)
  95.         if arrlen>1:
  96.             if pack_format[ind]=="c":
  97.                 result[varname]=repr(
  98.                     u''.join([unicode(i) for i in unpacked[ind:ind+arrlen]]))
  99.             else:
  100.                 result[varname]=unpacked[ind:ind+arrlen]
  101.             ind+=arrlen
  102.         else:
  103.             result[varname]=unpacked[ind]
  104.             ind+=1
  105.     return result
  106.  
  107. def depack_bytearray_to_str(bindata, cStruct, alignment="="):
  108.     out_str=u''
  109.     for key,value in depack_bytearray_to_dict(bindata,cStruct, alignment).items():
  110.         out_str+=key+':'+unicode(value)+'\n'
  111.     return out_str
  112.  
  113. def depack_bytearray_to_namedtuple(bindata, cStruct, alignment="="):
  114.     d=depack_bytearray_to_dict(bindata,cStruct, alignment)
  115.     return namedtuple('CStruct',d.keys())(*d.values())
  116.  
  117. def structSize(cStruct, alignment="="):
  118.     varlist,pack_format=structInfo(cStruct, alignment)
  119.     return struct.calcsize(pack_format)
  120.  
  121. if __name__ == '__main__':
  122.     print "C struct definition:"
  123.     print MY_STRUCT
  124.     print ""
  125.  
  126.     print "Test data:"
  127.     print UNPACKED_STRUCT;
  128.     print ""
  129.  
  130.     varlist,pack_format=structInfo(MY_STRUCT,'<')
  131. #    packed_struct=struct.pack(pack_format,*MY_STRUCT_UNPACKED)
  132.     packed_struct=PACKED_STRUCT
  133.     print repr(packed_struct)
  134.  
  135.     print "Result:"
  136.  
  137.     print depack_bytearray_to_str(packed_struct,MY_STRUCT,'<' )
  138.  
  139.     print "Bytes in Stuct:"+str(structSize(MY_STRUCT))
  140.  
  141.     nt=depack_bytearray_to_namedtuple(packed_struct,MY_STRUCT,'<' )
  142.     print "named tuple nt:"
  143.     print nt
  144.     print "nt.string="+nt.string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement