|
| 1 | +# ------------------------------------------------------------------------------ |
| 2 | +# CodeHawk Binary Analyzer |
| 3 | +# Author: Henny Sipma |
| 4 | +# ------------------------------------------------------------------------------ |
| 5 | +# The MIT License (MIT) |
| 6 | +# |
| 7 | +# Copyright (c) 2026 Aarno Labs LLC |
| 8 | +# |
| 9 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +# of this software and associated documentation files (the "Software"), to deal |
| 11 | +# in the Software without restriction, including without limitation the rights |
| 12 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +# copies of the Software, and to permit persons to whom the Software is |
| 14 | +# furnished to do so, subject to the following conditions: |
| 15 | +# |
| 16 | +# The above copyright notice and this permission notice shall be included in all |
| 17 | +# copies or substantial portions of the Software. |
| 18 | +# |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +# SOFTWARE. |
| 26 | +# ------------------------------------------------------------------------------ |
| 27 | + |
| 28 | +from dataclasses import dataclass |
| 29 | +from typing import List, Optional, TYPE_CHECKING |
| 30 | + |
| 31 | +from chb.api.InterfaceDictionaryRecord import InterfaceDictionaryRecord |
| 32 | + |
| 33 | +import chb.util.fileutil as UF |
| 34 | + |
| 35 | +from chb.util.IndexedTable import IndexedTableValue |
| 36 | + |
| 37 | +if TYPE_CHECKING: |
| 38 | + from chb.api.InterfaceDictionary import InterfaceDictionary |
| 39 | + |
| 40 | + |
| 41 | +@dataclass |
| 42 | +class FmtArgFieldWidth: |
| 43 | + s: str |
| 44 | + |
| 45 | + def has_fieldwidth(self) -> bool: |
| 46 | + return self.s.startswith("fwc:") |
| 47 | + |
| 48 | + def has_fieldwidth_argument(self) -> bool: |
| 49 | + return self.s == "fwa" |
| 50 | + |
| 51 | + def fieldwidth(self) -> Optional[int]: |
| 52 | + if self.has_fieldwidth(): |
| 53 | + return int(self.s[4:]) |
| 54 | + return None |
| 55 | + |
| 56 | + def __str__(self) -> str: |
| 57 | + if self.has_fieldwidth(): |
| 58 | + return self.s[4:] |
| 59 | + elif self.has_fieldwidth_argument(): |
| 60 | + return "*" |
| 61 | + else: |
| 62 | + return "" |
| 63 | + |
| 64 | +@dataclass |
| 65 | +class FmtArgPrecision: |
| 66 | + s: str |
| 67 | + |
| 68 | + def has_precision(self) -> bool: |
| 69 | + return self.s.startswith("pc:") |
| 70 | + |
| 71 | + def has_precision_argument(self) -> bool: |
| 72 | + return self.s == "pa" |
| 73 | + |
| 74 | + def precision(self) -> Optional[int]: |
| 75 | + if self.has_precision(): |
| 76 | + return int(self.s[3:]) |
| 77 | + return None |
| 78 | + |
| 79 | + def __str__(self) -> str: |
| 80 | + if self.has_precision(): |
| 81 | + return "." + self.s[3:] |
| 82 | + elif self.has_precision_argument(): |
| 83 | + return ".*" |
| 84 | + else: |
| 85 | + return "" |
| 86 | + |
| 87 | + |
| 88 | +class FormatArgSpec(InterfaceDictionaryRecord): |
| 89 | + |
| 90 | + def __init__( |
| 91 | + self, ixd: "InterfaceDictionary", ixval: IndexedTableValue) -> None: |
| 92 | + InterfaceDictionaryRecord.__init__(self, ixd, ixval) |
| 93 | + |
| 94 | + @property |
| 95 | + def fieldwidth(self) -> FmtArgFieldWidth: |
| 96 | + return FmtArgFieldWidth(self.tags[0]) |
| 97 | + |
| 98 | + @property |
| 99 | + def precision(self) -> FmtArgPrecision: |
| 100 | + return FmtArgPrecision(self.tags[1]) |
| 101 | + |
| 102 | + @property |
| 103 | + def lengthmodifier(self) -> str: |
| 104 | + if self.tags[2] == "none": |
| 105 | + return "" |
| 106 | + else: |
| 107 | + return self.tags[2] |
| 108 | + |
| 109 | + @property |
| 110 | + def conversion(self) -> str: |
| 111 | + return self.tags[3] |
| 112 | + |
| 113 | + @property |
| 114 | + def flags(self) -> str: |
| 115 | + return "".join(chr(i) for i in self.args) |
| 116 | + |
| 117 | + def __str__(self): |
| 118 | + return ( |
| 119 | + "%" |
| 120 | + + self.flags |
| 121 | + + str(self.fieldwidth) |
| 122 | + + str(self.precision) |
| 123 | + + str(self.lengthmodifier) |
| 124 | + + self.conversion) |
| 125 | + |
| 126 | + |
| 127 | +class FormatStringSpec(InterfaceDictionaryRecord): |
| 128 | + |
| 129 | + def __init__( |
| 130 | + self, ixd: "InterfaceDictionary", ixval: IndexedTableValue) -> None: |
| 131 | + InterfaceDictionaryRecord.__init__(self, ixd, ixval) |
| 132 | + |
| 133 | + @property |
| 134 | + def argspecs(self) -> List[FormatArgSpec]: |
| 135 | + return [self.id.formatarg_spec(ix) for ix in self.args[1:]] |
| 136 | + |
| 137 | + @property |
| 138 | + def literal_length(self) -> int: |
| 139 | + return self.args[0] |
| 140 | + |
| 141 | + def __str__(self) -> str: |
| 142 | + return ", ".join(str(s) for s in self.argspecs) |
0 commit comments