OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Utility classes for serialization""" | 5 """Utility classes for serialization""" |
6 | 6 |
7 import struct | 7 import struct |
8 | 8 |
9 | 9 |
10 # Format of a header for a struct or an array. | 10 # Format of a header for a struct or an array. |
11 HEADER_STRUCT = struct.Struct("=II") | 11 HEADER_STRUCT = struct.Struct("<II") |
12 | 12 |
13 | 13 |
14 class SerializationException(Exception): | 14 class SerializationException(Exception): |
15 """Error when strying to serialize a struct.""" | 15 """Error when strying to serialize a struct.""" |
16 pass | 16 pass |
17 | 17 |
18 | 18 |
19 class DeserializationException(Exception): | 19 class DeserializationException(Exception): |
20 """Error when strying to deserialize a struct.""" | 20 """Error when strying to deserialize a struct.""" |
21 pass | 21 pass |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 def _GetVersion(groups): | 103 def _GetVersion(groups): |
104 return sum([len(x.descriptors) for x in groups]) | 104 return sum([len(x.descriptors) for x in groups]) |
105 | 105 |
106 | 106 |
107 def _FilterGroups(groups, version): | 107 def _FilterGroups(groups, version): |
108 return [group for group in groups if group.GetVersion() < version] | 108 return [group for group in groups if group.GetVersion() < version] |
109 | 109 |
110 | 110 |
111 def _GetStruct(groups): | 111 def _GetStruct(groups): |
112 index = 0 | 112 index = 0 |
113 codes = [ '=' ] | 113 codes = [ '<' ] |
114 for group in groups: | 114 for group in groups: |
115 code = group.GetTypeCode() | 115 code = group.GetTypeCode() |
116 size = group.GetByteSize() | 116 size = group.GetByteSize() |
117 needed_padding = NeededPaddingForAlignment(index, size) | 117 needed_padding = NeededPaddingForAlignment(index, size) |
118 if needed_padding: | 118 if needed_padding: |
119 codes.append('x' * needed_padding) | 119 codes.append('x' * needed_padding) |
120 index = index + needed_padding | 120 index = index + needed_padding |
121 codes.append(code) | 121 codes.append(code) |
122 index = index + size | 122 index = index + size |
123 alignment_needed = NeededPaddingForAlignment(index) | 123 alignment_needed = NeededPaddingForAlignment(index) |
124 if alignment_needed: | 124 if alignment_needed: |
125 codes.append('x' * alignment_needed) | 125 codes.append('x' * alignment_needed) |
126 return struct.Struct(''.join(codes)) | 126 return struct.Struct(''.join(codes)) |
OLD | NEW |