Rev 3264 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3264 | Rev 3403 | ||
---|---|---|---|
1 | # |
1 | # |
2 | # Copyright (c) 2008 Martin Decky |
2 | # Copyright (c) 2008 Martin Decky |
3 | # All rights reserved. |
3 | # All rights reserved. |
4 | # |
4 | # |
5 | # Redistribution and use in source and binary forms, with or without |
5 | # Redistribution and use in source and binary forms, with or without |
6 | # modification, are permitted provided that the following conditions |
6 | # modification, are permitted provided that the following conditions |
7 | # are met: |
7 | # are met: |
8 | # |
8 | # |
9 | # - Redistributions of source code must retain the above copyright |
9 | # - Redistributions of source code must retain the above copyright |
10 | # notice, this list of conditions and the following disclaimer. |
10 | # notice, this list of conditions and the following disclaimer. |
11 | # - Redistributions in binary form must reproduce the above copyright |
11 | # - Redistributions in binary form must reproduce the above copyright |
12 | # notice, this list of conditions and the following disclaimer in the |
12 | # notice, this list of conditions and the following disclaimer in the |
13 | # documentation and/or other materials provided with the distribution. |
13 | # documentation and/or other materials provided with the distribution. |
14 | # - The name of the author may not be used to endorse or promote products |
14 | # - The name of the author may not be used to endorse or promote products |
15 | # derived from this software without specific prior written permission. |
15 | # derived from this software without specific prior written permission. |
16 | # |
16 | # |
17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | # |
27 | # |
28 | """ |
28 | """ |
29 | Convert descriptive structure definitions to structure object |
29 | Convert descriptive structure definitions to structure object |
30 | """ |
30 | """ |
31 | 31 | ||
32 | import struct |
32 | import struct |
33 | 33 | ||
34 | class Struct: |
34 | class Struct: |
35 | def size(self): |
35 | def size(self): |
36 | return struct.calcsize(self._format_) |
36 | return struct.calcsize(self._format_) |
37 | 37 | ||
38 | def pack(self): |
38 | def pack(self): |
39 | args = [] |
39 | args = [] |
40 | for variable in self._args_: |
40 | for variable in self._args_: |
41 | if (isinstance(self.__dict__[variable], list)): |
41 | if (isinstance(self.__dict__[variable], list)): |
42 | for item in self.__dict__[variable]: |
42 | for item in self.__dict__[variable]: |
43 | args.append(item) |
43 | args.append(item) |
44 | else: |
44 | else: |
45 | args.append(self.__dict__[variable]) |
45 | args.append(self.__dict__[variable]) |
46 | 46 | ||
47 | return struct.pack(self._format_, *args) |
47 | return struct.pack(self._format_, *args) |
48 | 48 | ||
49 | def create(definition): |
49 | def create(definition): |
50 | "Create structure object" |
50 | "Create structure object" |
51 | 51 | ||
52 | tokens = definition.split(None) |
52 | tokens = definition.split(None) |
53 | 53 | ||
54 | # Initial byte order tag |
54 | # Initial byte order tag |
55 | format = { |
55 | format = { |
56 | "little:": lambda: "<", |
56 | "little:": lambda: "<", |
57 | "big:": lambda: ">", |
57 | "big:": lambda: ">", |
58 | "network:": lambda: "!" |
58 | "network:": lambda: "!" |
59 | }[tokens[0]]() |
59 | }[tokens[0]]() |
60 | inst = Struct() |
60 | inst = Struct() |
61 | args = [] |
61 | args = [] |
62 | 62 | ||
63 | # Member tags |
63 | # Member tags |
64 | comment = False |
64 | comment = False |
65 | variable = None |
65 | variable = None |
66 | for token in tokens[1:]: |
66 | for token in tokens[1:]: |
67 | if (comment): |
67 | if (comment): |
68 | if (token == "*/"): |
68 | if (token == "*/"): |
69 | comment = False |
69 | comment = False |
70 | continue |
70 | continue |
71 | 71 | ||
72 | if (token == "/*"): |
72 | if (token == "/*"): |
73 | comment = True |
73 | comment = True |
74 | continue |
74 | continue |
75 | 75 | ||
76 | if (variable != None): |
76 | if (variable != None): |
77 | subtokens = token.split("[") |
77 | subtokens = token.split("[") |
78 | 78 | ||
79 | if (len(subtokens) > 1): |
79 | if (len(subtokens) > 1): |
80 | format += "%d" % int(subtokens[1].split("]")[0]) |
80 | format += "%d" % int(subtokens[1].split("]")[0]) |
81 | 81 | ||
82 | format += variable |
82 | format += variable |
83 | 83 | ||
84 | inst.__dict__[subtokens[0]] = None |
84 | inst.__dict__[subtokens[0]] = None |
85 | args.append(subtokens[0]) |
85 | args.append(subtokens[0]) |
86 | 86 | ||
87 | variable = None |
87 | variable = None |
88 | continue |
88 | continue |
89 | 89 | ||
90 | if (token[0:8] == "padding["): |
90 | if (token[0:8] == "padding["): |
91 | size = token[8:].split("]")[0] |
91 | size = token[8:].split("]")[0] |
92 | format += "%dx" % int(size) |
92 | format += "%dx" % int(size) |
93 | continue |
93 | continue |
94 | 94 | ||
95 | variable = { |
95 | variable = { |
96 | "char": lambda: "s", |
96 | "char": lambda: "s", |
97 | "uint8_t": lambda: "B", |
97 | "uint8_t": lambda: "B", |
98 | "uint16_t": lambda: "H", |
98 | "uint16_t": lambda: "H", |
99 | "uint32_t": lambda: "L", |
99 | "uint32_t": lambda: "L", |
100 | "uint64_t": lambda: "Q", |
100 | "uint64_t": lambda: "Q", |
101 | 101 | ||
102 | "int8_t": lambda: "b", |
102 | "int8_t": lambda: "b", |
103 | "int16_t": lambda: "h", |
103 | "int16_t": lambda: "h", |
104 | "int32_t": lambda: "l", |
104 | "int32_t": lambda: "l", |
105 | "int64_t": lambda: "q" |
105 | "int64_t": lambda: "q" |
106 | }[token]() |
106 | }[token]() |
107 | 107 | ||
108 | inst.__dict__['_format_'] = format |
108 | inst.__dict__['_format_'] = format |
109 | inst.__dict__['_args_'] = args |
109 | inst.__dict__['_args_'] = args |
110 | return inst |
110 | return inst |
111 | 111 |