| 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. |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 | 161 |
| 162 | 162 |
| 163 def NeededPaddingForAlignment(value, alignment=8): | 163 def NeededPaddingForAlignment(value, alignment=8): |
| 164 """Returns the padding necessary to align value with the given alignment.""" | 164 """Returns the padding necessary to align value with the given alignment.""" |
| 165 if value % alignment: | 165 if value % alignment: |
| 166 return alignment - (value % alignment) | 166 return alignment - (value % alignment) |
| 167 return 0 | 167 return 0 |
| 168 | 168 |
| 169 | 169 |
| 170 def _GetVersion(groups): | 170 def _GetVersion(groups): |
| 171 return sum([len(x.descriptors) for x in groups]) | 171 if not len(groups): |
| 172 return 0 |
| 173 return max([x.GetMaxVersion() for x in groups]) |
| 172 | 174 |
| 173 | 175 |
| 174 def _FilterGroups(groups, version): | 176 def _FilterGroups(groups, version): |
| 175 return [group for group in groups if group.GetVersion() < version] | 177 return [group.Filter(version) for |
| 178 group in groups if group.GetMinVersion() <= version] |
| 176 | 179 |
| 177 | 180 |
| 178 def _GetStruct(groups): | 181 def _GetStruct(groups): |
| 179 index = 0 | 182 index = 0 |
| 180 codes = [ '<' ] | 183 codes = [ '<' ] |
| 181 for group in groups: | 184 for group in groups: |
| 182 code = group.GetTypeCode() | 185 code = group.GetTypeCode() |
| 183 size = group.GetByteSize() | 186 size = group.GetByteSize() |
| 184 needed_padding = NeededPaddingForAlignment(index, size) | 187 needed_padding = NeededPaddingForAlignment(index, size) |
| 185 if needed_padding: | 188 if needed_padding: |
| 186 codes.append('x' * needed_padding) | 189 codes.append('x' * needed_padding) |
| 187 index = index + needed_padding | 190 index = index + needed_padding |
| 188 codes.append(code) | 191 codes.append(code) |
| 189 index = index + size | 192 index = index + size |
| 190 alignment_needed = NeededPaddingForAlignment(index) | 193 alignment_needed = NeededPaddingForAlignment(index) |
| 191 if alignment_needed: | 194 if alignment_needed: |
| 192 codes.append('x' * alignment_needed) | 195 codes.append('x' * alignment_needed) |
| 193 return struct.Struct(''.join(codes)) | 196 return struct.Struct(''.join(codes)) |
| OLD | NEW |