xref: /aosp_15_r20/test/vts/vndk_utils/library/elf/utils.py (revision 9a74111979c139a065a9a7e4d45972320c5732c7)
1*9a741119SAndroid Build Coastguard Worker#
2*9a741119SAndroid Build Coastguard Worker# Copyright (C) 2018 The Android Open Source Project
3*9a741119SAndroid Build Coastguard Worker#
4*9a741119SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
5*9a741119SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
6*9a741119SAndroid Build Coastguard Worker# You may obtain a copy of the License at
7*9a741119SAndroid Build Coastguard Worker#
8*9a741119SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
9*9a741119SAndroid Build Coastguard Worker#
10*9a741119SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
11*9a741119SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
12*9a741119SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*9a741119SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
14*9a741119SAndroid Build Coastguard Worker# limitations under the License.
15*9a741119SAndroid Build Coastguard Worker#
16*9a741119SAndroid Build Coastguard Worker"""This file contains ELF utility functions."""
17*9a741119SAndroid Build Coastguard Worker
18*9a741119SAndroid Build Coastguard Worker
19*9a741119SAndroid Build Coastguard Workerdef ByteToInt(obj):
20*9a741119SAndroid Build Coastguard Worker    """Converts an element of a bytes object to an integer."""
21*9a741119SAndroid Build Coastguard Worker    return obj if isinstance(obj, int) else ord(obj)
22*9a741119SAndroid Build Coastguard Worker
23*9a741119SAndroid Build Coastguard Worker
24*9a741119SAndroid Build Coastguard Workerdef BytesToString(obj):
25*9a741119SAndroid Build Coastguard Worker    """Converts bytes to a python3 string."""
26*9a741119SAndroid Build Coastguard Worker    return obj if isinstance(obj, str) else obj.decode("utf-8")
27*9a741119SAndroid Build Coastguard Worker
28*9a741119SAndroid Build Coastguard Worker
29*9a741119SAndroid Build Coastguard Workerdef DecodeSLEB128(data, begin_offset=0):
30*9a741119SAndroid Build Coastguard Worker    """Decode one int64 from SLEB128 encoded bytes.
31*9a741119SAndroid Build Coastguard Worker
32*9a741119SAndroid Build Coastguard Worker    Args:
33*9a741119SAndroid Build Coastguard Worker        data: A bytes object to decode.
34*9a741119SAndroid Build Coastguard Worker        begin_offset: An integer, offset in data to start decode from.
35*9a741119SAndroid Build Coastguard Worker
36*9a741119SAndroid Build Coastguard Worker    Returns:
37*9a741119SAndroid Build Coastguard Worker        A tuple (value, num), the decoded value and number of consumed bytes.
38*9a741119SAndroid Build Coastguard Worker
39*9a741119SAndroid Build Coastguard Worker    Raises:
40*9a741119SAndroid Build Coastguard Worker        IndexError: String index out of range.
41*9a741119SAndroid Build Coastguard Worker    """
42*9a741119SAndroid Build Coastguard Worker    cur = begin_offset
43*9a741119SAndroid Build Coastguard Worker    value = 0
44*9a741119SAndroid Build Coastguard Worker    shift = 0
45*9a741119SAndroid Build Coastguard Worker    while True:
46*9a741119SAndroid Build Coastguard Worker        try:
47*9a741119SAndroid Build Coastguard Worker            byte, cur = ByteToInt(data[cur]), cur + 1
48*9a741119SAndroid Build Coastguard Worker        except IndexError:
49*9a741119SAndroid Build Coastguard Worker            raise
50*9a741119SAndroid Build Coastguard Worker        value |= (byte & 0x7F) << shift
51*9a741119SAndroid Build Coastguard Worker        shift += 7
52*9a741119SAndroid Build Coastguard Worker        if byte & 0x80 == 0:
53*9a741119SAndroid Build Coastguard Worker            break
54*9a741119SAndroid Build Coastguard Worker    if byte & 0x40:
55*9a741119SAndroid Build Coastguard Worker        value |= (-1) << shift
56*9a741119SAndroid Build Coastguard Worker    return value, cur - begin_offset
57