0
|
1 |
#
|
|
2 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
#
|
|
16 |
import re
|
3
|
17 |
from cone.public import exceptions, plugin
|
0
|
18 |
import crml_reader
|
|
19 |
from crml_model import *
|
|
20 |
|
|
21 |
class CenRepEntry(object):
|
|
22 |
"""
|
|
23 |
Class representing an entry in a CenRep text file.
|
|
24 |
"""
|
|
25 |
def __init__(self, **kwargs):
|
|
26 |
self.int = kwargs.get('int')
|
|
27 |
self.crml_type = kwargs.get('crml_type')
|
|
28 |
self.confml_type = kwargs.get('confml_type')
|
|
29 |
self.value = kwargs.get('value')
|
|
30 |
self.orig_value = kwargs.get('orig_value')
|
|
31 |
self.access = kwargs.get('access')
|
|
32 |
self.backup = kwargs.get('backup', False)
|
|
33 |
|
|
34 |
@property
|
|
35 |
def metadata(self):
|
|
36 |
return _get_metadata(self.backup)
|
|
37 |
|
|
38 |
def __lt__(self, other):
|
|
39 |
return crml_reader.convert_num(self.int) < crml_reader.convert_num(other.int)
|
|
40 |
|
|
41 |
class CenRepRfsRecord(object):
|
|
42 |
"""
|
|
43 |
Class representing an entry in the CenRep RFS text file.
|
|
44 |
"""
|
|
45 |
def __init__(self, repo_uid, key_uids=None):
|
|
46 |
self.repo_uid = repo_uid
|
|
47 |
self.key_uids = key_uids or []
|
|
48 |
|
|
49 |
def __eq__(self, other):
|
|
50 |
if type(self) == type(other): return self.repo_uid == other.repo_uid
|
|
51 |
else: return False
|
|
52 |
|
|
53 |
def __ne__(self, other):
|
|
54 |
return not (self == other)
|
|
55 |
|
|
56 |
def __lt__(self, other):
|
|
57 |
return self.repo_uid < other.repo_uid
|
|
58 |
|
|
59 |
def __repr__(self):
|
|
60 |
return "CenRepRfsRecord(repo_uid=%s, key_uids=%r)" % (self.repo_uid, self.key_uids)
|
|
61 |
|
|
62 |
class CrmlTxtWriter(object):
|
|
63 |
"""
|
|
64 |
Writer class for generating CenRep .txt files based on a CRML model.
|
|
65 |
"""
|
|
66 |
|
3
|
67 |
def __init__(self, context, log):
|
|
68 |
self.context = context
|
0
|
69 |
self.log = log
|
|
70 |
|
3
|
71 |
def get_cenrep_txt_data(self, repository, changed_refs=None):
|
0
|
72 |
"""
|
|
73 |
Return the text data for the CenRep txt generated based on the given
|
|
74 |
CRML repository model.
|
3
|
75 |
@param changed_refs: List of changed refs. If this is passed, only a delta
|
|
76 |
CenRep txt file is generated (i.e. containing only the changed settings).
|
|
77 |
If None, the whole CenRep file is generated normally.
|
0
|
78 |
@return: Text data for the CenRep text file.
|
|
79 |
"""
|
3
|
80 |
delta_cenrep = changed_refs is not None
|
0
|
81 |
data = []
|
|
82 |
|
|
83 |
# Generate header lines
|
3
|
84 |
data.extend(self.get_header_lines(repository, delta_cenrep))
|
0
|
85 |
|
|
86 |
self._check_repository_attrs(repository)
|
|
87 |
|
|
88 |
# Generate CenRep entries for all keys
|
|
89 |
cenrep_entries = []
|
|
90 |
for key in repository.keys:
|
3
|
91 |
# If generating a delta CenRep file, ignore keys that don't
|
|
92 |
# use any of the changed settings
|
|
93 |
if delta_cenrep:
|
|
94 |
if not plugin.uses_ref(changed_refs, key.get_refs()):
|
|
95 |
continue
|
0
|
96 |
cenrep_entries.extend(self.get_cenrep_entries(key))
|
|
97 |
|
|
98 |
# Generate entry lines based on the entries
|
|
99 |
cenrep_entries.sort()
|
|
100 |
for entry in cenrep_entries:
|
3
|
101 |
data.append(self.get_cenrep_entry_line(entry, delta_cenrep))
|
0
|
102 |
|
|
103 |
data.append('')
|
|
104 |
|
|
105 |
# Remove Nones from the line list
|
|
106 |
data = filter(lambda val: val is not None, data)
|
|
107 |
|
|
108 |
return '\r\n'.join(data)
|
|
109 |
|
|
110 |
def get_cenrep_rfs_txt_data(self, rfs_records):
|
|
111 |
"""
|
|
112 |
Return the text data for the CenRep RFS txt generated based on the given
|
|
113 |
CenRep RFS record list.
|
|
114 |
"""
|
|
115 |
data = []
|
|
116 |
|
|
117 |
# Make a distinct and sorted array of the records
|
|
118 |
records = []
|
|
119 |
for r in rfs_records:
|
|
120 |
if r not in records: records.append(r)
|
|
121 |
records.sort()
|
|
122 |
|
|
123 |
for record in records:
|
|
124 |
repo_uid = record.repo_uid
|
|
125 |
|
|
126 |
# Add padding zeros to the UID
|
|
127 |
if len(repo_uid) < 8:
|
|
128 |
repo_uid = (8 - (len(repo_uid) % 8)) * '0' + repo_uid
|
|
129 |
temp = "CR %s" % repo_uid
|
|
130 |
if record.key_uids:
|
|
131 |
temp += " %s" % ' '.join(record.key_uids)
|
|
132 |
data.append(temp)
|
|
133 |
|
|
134 |
return '\r\n'.join(data)
|
|
135 |
|
|
136 |
def get_cenrep_rfs_record(self, repository):
|
|
137 |
"""
|
|
138 |
Return the RFS record for the given CRML repository.
|
|
139 |
|
|
140 |
@return: A CenRepRfsRecord object if the repository should be listed
|
|
141 |
in cenrep_rfs.txt, None if not.
|
|
142 |
"""
|
|
143 |
# Get the UID as a hex value without the leading 0x
|
|
144 |
repo_uid = _translate_key_uid(repository.uid_value)[2:]
|
|
145 |
|
|
146 |
# Check if the whole repository has RFS=true
|
|
147 |
if repository.rfs:
|
|
148 |
return CenRepRfsRecord(repo_uid)
|
|
149 |
|
|
150 |
# Collect the UIDs of the keys that should be listed
|
|
151 |
rfs_key_uids = []
|
|
152 |
for key in repository.keys:
|
|
153 |
if self._key_is_rfs(key) and key.int:
|
|
154 |
# Get the UID as a hex value without the leading 0x
|
|
155 |
uid = _translate_key_uid(key.int)[2:]
|
|
156 |
rfs_key_uids.append(uid)
|
|
157 |
|
|
158 |
if rfs_key_uids:
|
|
159 |
return CenRepRfsRecord(repo_uid, rfs_key_uids)
|
|
160 |
else:
|
|
161 |
return None
|
|
162 |
|
|
163 |
def _key_is_rfs(self, key):
|
|
164 |
"""
|
|
165 |
@return: True if the key UID should be listed in cenrep_rfs.txt
|
|
166 |
"""
|
|
167 |
if isinstance(key, CrmlSimpleKey):
|
|
168 |
return bool(self._get_rfs_value(key.ref))
|
|
169 |
elif isinstance(key, CrmlBitmaskKey):
|
|
170 |
for bit in key.bits:
|
|
171 |
if self._get_rfs_value(bit.ref):
|
|
172 |
return True
|
|
173 |
else:
|
|
174 |
return False
|
|
175 |
|
|
176 |
def _get_rfs_value(self, ref):
|
|
177 |
"""
|
|
178 |
@return: The RFS value for the given setting, or None if not available.
|
|
179 |
"""
|
|
180 |
if ref is None: return
|
|
181 |
|
|
182 |
try:
|
|
183 |
feature = self._get_feature(ref)
|
|
184 |
except exceptions.NotFound:
|
|
185 |
# Feature not found in the configuration
|
|
186 |
return None
|
|
187 |
|
|
188 |
return feature.get_value(attr='rfs')
|
|
189 |
|
3
|
190 |
def get_header_lines(self, repository, delta_cenrep=False):
|
0
|
191 |
"""
|
|
192 |
Return a list of lines to be written in the header section of the CenRep text file.
|
3
|
193 |
@param delta_cenrep: If True, only the data needed for a delta CenRep
|
|
194 |
file is returned.
|
0
|
195 |
"""
|
|
196 |
data = ['cenrep',
|
|
197 |
'version %s' % repository.version]
|
|
198 |
|
3
|
199 |
# Owner seems to be required even for delta CenReps
|
0
|
200 |
if repository.owner:
|
|
201 |
data.append('[owner]')
|
|
202 |
data.append(repository.owner)
|
|
203 |
|
|
204 |
data.append('[defaultmeta]')
|
3
|
205 |
if not delta_cenrep:
|
|
206 |
data.append(' %d' % _get_metadata(repository.backup))
|
|
207 |
for key in repository.keys:
|
|
208 |
data.append(self.get_defaultmeta_line(key))
|
0
|
209 |
|
|
210 |
data.append('[platsec]')
|
3
|
211 |
if not delta_cenrep:
|
|
212 |
acc_text = self.get_access_line(repository.access)
|
|
213 |
if acc_text: acc_text = ' ' + acc_text
|
|
214 |
data.append(acc_text)
|
|
215 |
for key in repository.keys:
|
0
|
216 |
data.append(self.get_platsec_line(key, repository))
|
|
217 |
|
|
218 |
data.append('[Main]')
|
|
219 |
return data
|
|
220 |
|
|
221 |
def get_cenrep_entries(self, key):
|
|
222 |
"""
|
|
223 |
Generate CenRep entries based on the given CRML key object.
|
|
224 |
@return: A list of CenRepEntry objects.
|
|
225 |
"""
|
|
226 |
if isinstance(key, CrmlSimpleKey):
|
|
227 |
feature = self._get_feature(key.ref)
|
|
228 |
entry = CenRepEntry(int = key.int,
|
|
229 |
crml_type = key.type,
|
|
230 |
confml_type = feature.get_type(),
|
|
231 |
value = feature.get_value(),
|
|
232 |
orig_value = feature.get_original_value(),
|
|
233 |
backup = key.backup,
|
|
234 |
access = key.access)
|
|
235 |
return [entry]
|
|
236 |
elif isinstance(key, CrmlBitmaskKey):
|
|
237 |
return self.get_bitmask_key_cenrep_entries(key)
|
|
238 |
elif isinstance(key, CrmlKeyRange):
|
|
239 |
return self.get_key_range_cenrep_entries(key)
|
|
240 |
else:
|
|
241 |
raise TypeError("Unsupported CRML key object type %s" % type(key))
|
|
242 |
|
|
243 |
def get_key_range_cenrep_entries(self, key_range):
|
|
244 |
"""
|
|
245 |
Generate CenRep entries based on the given CrmlKeyRange object.
|
|
246 |
@return: A list of CenRepEntry objects.
|
|
247 |
"""
|
|
248 |
entries = []
|
|
249 |
count = 0
|
|
250 |
|
|
251 |
# Generate the countInt entry if necessary
|
|
252 |
if key_range.count_int is not None and key_range.ref is not None:
|
|
253 |
try:
|
|
254 |
feature = self._get_feature(key_range.ref)
|
|
255 |
|
|
256 |
# For CT2 output compatibility
|
|
257 |
if feature.get_type() != 'sequence':
|
|
258 |
return []
|
|
259 |
|
|
260 |
values = feature.get_value()
|
|
261 |
except exceptions.NotFound:
|
|
262 |
values = []
|
|
263 |
|
|
264 |
count = len(values)
|
|
265 |
|
|
266 |
entry = CenRepEntry(int = key_range.count_int,
|
|
267 |
crml_type = 'int',
|
|
268 |
confml_type = 'int',
|
|
269 |
value = count,
|
|
270 |
backup = key_range.backup,
|
|
271 |
access = key_range.access)
|
|
272 |
entries.append(entry)
|
|
273 |
|
|
274 |
# Generate entries based on the sequence values
|
|
275 |
for subkey in key_range.subkeys:
|
|
276 |
full_ref = "%s.%s"% (key_range.ref, subkey.ref)
|
|
277 |
|
|
278 |
try:
|
|
279 |
feature = self._get_feature(full_ref)
|
|
280 |
values = feature.get_value()
|
|
281 |
confml_type = feature.get_type()
|
|
282 |
backup = key_range.backup
|
|
283 |
except exceptions.NotFound:
|
|
284 |
# For CT2 output compatibility
|
|
285 |
values = ['null' for i in xrange(count)]
|
|
286 |
confml_type = None
|
|
287 |
backup = False
|
|
288 |
|
|
289 |
for i, value in enumerate(values):
|
|
290 |
# Calculate the index of the entry
|
|
291 |
index = self.get_index(crml_reader.convert_num(key_range.first_int),
|
|
292 |
crml_reader.convert_num(key_range.first_index),
|
|
293 |
crml_reader.convert_num(key_range.index_bits),
|
|
294 |
i,
|
|
295 |
crml_reader.convert_num(subkey.int))
|
|
296 |
|
|
297 |
entry = CenRepEntry(int = "0x%x" % index,
|
|
298 |
crml_type = subkey.type,
|
|
299 |
confml_type = confml_type,
|
|
300 |
value = value,
|
|
301 |
orig_value = value,
|
|
302 |
backup = backup,
|
|
303 |
access = key_range.access)
|
|
304 |
entries.append(entry)
|
|
305 |
|
|
306 |
return entries
|
|
307 |
|
|
308 |
def get_bitmask_key_cenrep_entries(self, key):
|
|
309 |
"""
|
|
310 |
Generate CenRep entries based on the given CrmlBitmaskKey object.
|
|
311 |
@return: A list of CenRepEntry objects.
|
|
312 |
"""
|
|
313 |
# Calculate the value based on the bit values
|
|
314 |
# -------------------------------------------
|
|
315 |
value = 0
|
|
316 |
for bit in key.bits:
|
|
317 |
feature = self._get_feature(bit.ref)
|
|
318 |
bit_value = feature.get_value()
|
|
319 |
if bit.invert: bit_value = not bit_value
|
|
320 |
if bit_value: value |= 1 << (bit.index - 1)
|
|
321 |
|
|
322 |
# Generate the textual representation of the bitmask value.
|
|
323 |
# This is done at this point because in get_cenrep_entry_line()
|
|
324 |
# we don't know anymore if the key was a bitmask key or a
|
|
325 |
# simple key.
|
|
326 |
# -------------------------------------------------------------
|
|
327 |
if key.type == 'binary':
|
|
328 |
orig_value = "%X" % value
|
|
329 |
# Add padding zeroes so that the number of digits
|
|
330 |
# is divisible by 8 (done manually since the length
|
|
331 |
# of a binary bitmask is unbounded).
|
|
332 |
padding_zeroes = (8 - len(orig_value) % 8) * '0'
|
|
333 |
# 4 is a special case for CT2 output compatibility
|
|
334 |
if len(orig_value) != 4:
|
|
335 |
orig_value = padding_zeroes + orig_value
|
|
336 |
else:
|
|
337 |
orig_value = str(value)
|
|
338 |
|
|
339 |
entry = CenRepEntry(int = key.int,
|
|
340 |
crml_type = key.type,
|
|
341 |
confml_type = 'int',
|
|
342 |
value = value,
|
|
343 |
orig_value = orig_value,
|
|
344 |
backup = key.backup,
|
|
345 |
access = key.access)
|
|
346 |
return [entry]
|
|
347 |
|
|
348 |
def get_defaultmeta_line(self, key):
|
|
349 |
"""
|
|
350 |
Return the defaultmeta section line for the given CRML key object.
|
|
351 |
"""
|
|
352 |
if not isinstance(key, CrmlKeyRange): return None
|
|
353 |
|
|
354 |
return "%s %s %d" % (key.first_int,
|
|
355 |
key.last_int,
|
|
356 |
_get_metadata(key.backup))
|
|
357 |
|
|
358 |
def get_platsec_line(self, key, repository):
|
|
359 |
"""
|
|
360 |
Return the platsec section line for the given CRML key object.
|
|
361 |
"""
|
|
362 |
if not isinstance(key, CrmlKeyRange): return None
|
|
363 |
|
|
364 |
# In a key range platsec entry something must be present, so if
|
|
365 |
# the access object is empty, use cap_rd and cap_wr from the repository's
|
|
366 |
# global access definition
|
|
367 |
access = key.access.copy()
|
|
368 |
is_empty = True
|
|
369 |
for attrname in ('sid_rd', 'cap_rd', 'sid_wr', 'cap_wr'):
|
|
370 |
if getattr(access, attrname) not in ('', None):
|
|
371 |
is_empty = False
|
|
372 |
if is_empty:
|
|
373 |
access.cap_rd = repository.access.cap_rd
|
|
374 |
access.cap_wr = repository.access.cap_wr
|
|
375 |
|
|
376 |
acc_text = self.get_access_line(access)
|
|
377 |
if acc_text: acc_text = ' ' + acc_text
|
|
378 |
|
|
379 |
return "%s %s%s" % (key.first_int,
|
|
380 |
key.last_int,
|
|
381 |
acc_text)
|
|
382 |
|
|
383 |
|
3
|
384 |
def get_cenrep_entry_line(self, entry, delta_cenrep=False):
|
0
|
385 |
"""
|
|
386 |
Return the text line for a CenRepEntry object.
|
3
|
387 |
@param delta_cenrep: If True, only the data needed for a delta CenRep
|
|
388 |
file is returned.
|
0
|
389 |
"""
|
|
390 |
value = None
|
|
391 |
if entry.crml_type in ('string', 'string8'):
|
|
392 |
if entry.confml_type is None:
|
|
393 |
pass
|
|
394 |
else:
|
|
395 |
if entry.orig_value is None:
|
|
396 |
value = '""'
|
|
397 |
else:
|
|
398 |
value = '"%s"' % entry.orig_value
|
|
399 |
elif entry.crml_type == 'int':
|
|
400 |
if entry.confml_type == 'boolean':
|
|
401 |
if entry.value: value = '1'
|
|
402 |
else: value = '0'
|
|
403 |
else:
|
|
404 |
value = entry.orig_value
|
|
405 |
elif entry.crml_type == 'real':
|
|
406 |
value = entry.orig_value or ''
|
|
407 |
elif entry.crml_type == 'binary':
|
|
408 |
# Empty binary values are denoted by a single dash
|
|
409 |
value = entry.orig_value or '-'
|
|
410 |
|
|
411 |
if value != '-':
|
|
412 |
# Make sure that the number of digits is divisible by two
|
|
413 |
if len(value) % 2 != 0:
|
|
414 |
value = '0' + value
|
|
415 |
|
|
416 |
if value is None:
|
|
417 |
value = unicode(entry.value)
|
|
418 |
|
|
419 |
self._check_value(entry, value)
|
|
420 |
|
3
|
421 |
if delta_cenrep:
|
|
422 |
return '%s %s %s' % (_translate_key_uid(entry.int),
|
|
423 |
entry.crml_type,
|
|
424 |
value)
|
|
425 |
else:
|
|
426 |
acc_text = self.get_access_line(entry.access)
|
|
427 |
if acc_text: acc_text = ' ' + acc_text
|
|
428 |
|
|
429 |
return '%s %s %s %d%s' % (_translate_key_uid(entry.int),
|
|
430 |
entry.crml_type,
|
|
431 |
value,
|
|
432 |
entry.metadata,
|
|
433 |
acc_text)
|
|
434 |
|
0
|
435 |
def _check_value(self, entry, value):
|
|
436 |
"""
|
|
437 |
Check that the given value is valid for the given CenRep entry,
|
|
438 |
and log a warning if it is not.
|
|
439 |
"""
|
|
440 |
if entry.crml_type == 'int':
|
|
441 |
# Check if the value is a string, since it may already
|
|
442 |
# be an integer
|
|
443 |
if not isinstance(value, basestring):
|
|
444 |
return
|
|
445 |
|
|
446 |
try:
|
|
447 |
value = value.strip()
|
|
448 |
if value.lower().startswith('0x'):
|
|
449 |
long(value, 16)
|
|
450 |
else:
|
|
451 |
long(value)
|
|
452 |
except ValueError:
|
|
453 |
self.log.warn("Key %s: Invalid integer value '%s'" % (entry.int, value))
|
|
454 |
elif entry.crml_type == 'real':
|
|
455 |
try:
|
|
456 |
float(value)
|
|
457 |
except ValueError:
|
|
458 |
self.log.warn("Key %s: Invalid real value '%s'" % (entry.int, value))
|
|
459 |
elif entry.crml_type == 'binary':
|
|
460 |
if value != '-' and re.match(r'^(0[xX])?[0-9a-fA-F]+$', value) is None:
|
|
461 |
self.log.warn("Key %s: Invalid binary value '%s'" % (entry.int, value))
|
|
462 |
|
|
463 |
def _check_repository_attrs(self, repository):
|
|
464 |
"""
|
|
465 |
Check that the attributes of the given repository are valid and
|
|
466 |
log warnings if not.
|
|
467 |
"""
|
|
468 |
if repository.owner is not None:
|
|
469 |
owner = repository.owner.strip()
|
|
470 |
# An empty owner UID is OK, it doesn't generate anything
|
|
471 |
# invalid into the output
|
|
472 |
if owner != '':
|
|
473 |
try:
|
|
474 |
if owner.lower().startswith('0x'):
|
|
475 |
long(owner, 16)
|
|
476 |
else:
|
|
477 |
long(owner)
|
|
478 |
except ValueError:
|
|
479 |
self.log.warn("Invalid owner UID '%s'" % owner)
|
|
480 |
|
|
481 |
def get_access_line(self, access):
|
|
482 |
"""
|
|
483 |
Generate a line containing access information based on a CrmlAccess object.
|
|
484 |
"""
|
|
485 |
# Write the access information in a specific order, because it
|
|
486 |
# won't work otherwise
|
|
487 |
var_order = ['sid_rd', 'cap_rd', 'sid_wr', 'cap_wr']
|
|
488 |
data = []
|
|
489 |
for varname in var_order:
|
|
490 |
val = getattr(access, varname)
|
|
491 |
if val not in ('', None):
|
|
492 |
# Using _translate_capability_string() on all, since a SID should
|
|
493 |
# not contain anything that could be messed up by the function
|
|
494 |
data.append('%s=%s' % (varname, _translate_capability_string(val)))
|
|
495 |
|
|
496 |
return ' '.join(data)
|
|
497 |
|
|
498 |
def _get_feature(self, ref):
|
3
|
499 |
return self.context.configuration.get_default_view().get_feature(ref)
|
0
|
500 |
|
|
501 |
@classmethod
|
|
502 |
def get_index(cls,firstInt,firstIndex,indexBits,seqIndex, subIndex):
|
|
503 |
"""
|
|
504 |
@param firstIndex: The first value available in the keyrange
|
|
505 |
@param lastInt: The last value available in the keyrange
|
|
506 |
@param indexBits: The index bits or mask for sequence index
|
|
507 |
@param seqIndex: The sequence index
|
|
508 |
@param subIndex: The sequence sub element index
|
|
509 |
@return: an numeric value for the encoded index
|
|
510 |
"""
|
|
511 |
rangeshift = cls.get_range_shift(indexBits)
|
|
512 |
return (((seqIndex+firstIndex) << rangeshift) + firstInt) + subIndex
|
|
513 |
|
|
514 |
@classmethod
|
|
515 |
def get_seqid(cls,firstInt,firstIndex,indexBits,cenrepkey):
|
|
516 |
"""
|
|
517 |
@param firstIndex: The first value available in the keyrange
|
|
518 |
@param lastInt: The last value available in the keyrange
|
|
519 |
@param indexBits: The index bits or mask for sequence index
|
|
520 |
@param cenrepkey: Crml key id
|
|
521 |
@return: an numeric value for the encoded index
|
|
522 |
"""
|
|
523 |
rangeshift = cls.get_range_shift(indexBits)
|
|
524 |
return (((cenrepkey & indexBits) -firstInt) >> rangeshift)-firstIndex
|
|
525 |
|
|
526 |
@classmethod
|
|
527 |
def get_subseqid(cls,firstInt,firstIndex,indexBits,cenrepkey):
|
|
528 |
"""
|
|
529 |
@param firstIndex: The first value available in the keyrange
|
|
530 |
@param lastInt: The last value available in the keyrange
|
|
531 |
@param indexBits: The index bits or mask for sequence index
|
|
532 |
@param cenrepkey: Crml key id
|
|
533 |
@return: an numeric value for the encoded index
|
|
534 |
"""
|
|
535 |
range = cls.get_range(indexBits)
|
|
536 |
return (cenrepkey - firstInt) & range
|
|
537 |
|
|
538 |
@classmethod
|
|
539 |
def get_range_shift(cls,indexBits):
|
|
540 |
""" Get the bit left to the """
|
|
541 |
seqrange = cls.get_range(indexBits)
|
|
542 |
shiftamount = 0
|
|
543 |
for i in range(0,32):
|
|
544 |
if (seqrange >> i) == 0:
|
|
545 |
shiftamount = i
|
|
546 |
break
|
|
547 |
return shiftamount
|
|
548 |
|
|
549 |
@classmethod
|
|
550 |
def get_range(cls,indexBits):
|
|
551 |
""" Get the bit left to the """
|
|
552 |
indexshift = 0
|
|
553 |
for i in range(0,32):
|
|
554 |
if (indexBits >> i) == 0:
|
|
555 |
indexshift = i
|
|
556 |
break
|
|
557 |
return ((0x1 << indexshift) - indexBits)-1
|
|
558 |
|
|
559 |
|
|
560 |
# =============================================================================
|
|
561 |
# Utility functions
|
|
562 |
# =============================================================================
|
|
563 |
|
|
564 |
def _get_metadata(backup):
|
|
565 |
metadata = 0
|
|
566 |
if backup: metadata |= 0x1000000
|
|
567 |
return metadata
|
|
568 |
|
|
569 |
def _translate_key_uid(uid):
|
|
570 |
"""Translate a key UID given in CRML so that it matches the output of CT2."""
|
|
571 |
if uid.lower().startswith("0x"):
|
|
572 |
prefix = uid[:2]
|
|
573 |
temp = uid[2:]
|
|
574 |
if int(temp, 16) == 0: return prefix + "0"
|
|
575 |
else: return prefix + uid[2:].lstrip('0')
|
|
576 |
else:
|
|
577 |
if int(uid) == 0: return "0"
|
|
578 |
else: return uid.lstrip('0')
|
|
579 |
|
|
580 |
def _translate_capability_string(cap_str):
|
|
581 |
"""
|
|
582 |
Translate a capability string so that it is
|
|
583 |
suitable for writing to a CenRep txt file.
|
|
584 |
"""
|
|
585 |
cap_str = cap_str.replace('AlwaysPass', 'alwayspass').replace('AlwaysFail', 'alwaysfail')
|
|
586 |
|
|
587 |
# The capability string can be a list separated either by
|
|
588 |
# whitespace or commas
|
|
589 |
if ',' in cap_str: caps = cap_str.split(',')
|
|
590 |
else: caps = cap_str.split()
|
|
591 |
|
|
592 |
# The output must always be comma-separated
|
|
593 |
return ','.join(caps)
|
|
594 |
|