├── .github ├── ISSUE_TEMPLATE │ └── bug_report.md └── workflows │ ├── python-app.yml │ └── python-publish.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── objc_types_decoder ├── __init__.py ├── __main__.py └── decode.py ├── pyproject.toml └── tests ├── test_decode_sanity.py └── test_decode_type_with_tail_sanity.py /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/workflows/python-app.yml: -------------------------------------------------------------------------------- 1 | # This workflow will install Python dependencies, run tests and lint with a single version of Python 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions 3 | 4 | name: Python application 5 | 6 | on: 7 | push: 8 | branches: [ '**' ] 9 | pull_request: 10 | branches: [ '**' ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | strategy: 18 | matrix: 19 | python-version: [3.7, 3.8, 3.9, "3.10", 3.11, 3.12, 3.13] 20 | 21 | steps: 22 | - uses: actions/checkout@v2 23 | - name: Set up Python ${{ matrix.python-version }} 24 | uses: actions/setup-python@v2 25 | with: 26 | python-version: ${{ matrix.python-version }} 27 | - name: Install dependencies 28 | run: | 29 | python -m pip install . 30 | - name: Lint with flake8 31 | run: | 32 | pip install flake8 33 | # stop the build if there are Python syntax errors or undefined names 34 | flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics 35 | # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide 36 | flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics 37 | - name: Test with pytest 38 | run: | 39 | pip install pytest 40 | pytest -------------------------------------------------------------------------------- /.github/workflows/python-publish.yml: -------------------------------------------------------------------------------- 1 | # This workflows will upload a Python Package using Twine when a release is created 2 | # For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries 3 | 4 | name: Upload Python Package 5 | 6 | on: 7 | release: 8 | types: [created] 9 | 10 | jobs: 11 | deploy: 12 | 13 | runs-on: ubuntu-latest 14 | 15 | steps: 16 | - uses: actions/checkout@v2 17 | - name: Set up Python 18 | uses: actions/setup-python@v2 19 | with: 20 | python-version: '3.x' 21 | - name: Install dependencies 22 | run: | 23 | python -m pip install --upgrade pip 24 | pip install -U build setuptools wheel twine 25 | - name: Build and publish 26 | env: 27 | TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} 28 | TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} 29 | run: | 30 | python -m build 31 | twine upload dist/* -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | .idea/ 10 | 11 | # Distribution / packaging 12 | .Python 13 | build/ 14 | develop-eggs/ 15 | dist/ 16 | downloads/ 17 | eggs/ 18 | .eggs/ 19 | lib/ 20 | lib64/ 21 | parts/ 22 | sdist/ 23 | var/ 24 | wheels/ 25 | pip-wheel-metadata/ 26 | share/python-wheels/ 27 | *.egg-info/ 28 | .installed.cfg 29 | *.egg 30 | MANIFEST 31 | 32 | # PyInstaller 33 | # Usually these files are written by a python script from a template 34 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 35 | *.manifest 36 | *.spec 37 | 38 | # Installer logs 39 | pip-log.txt 40 | pip-delete-this-directory.txt 41 | 42 | # Unit test / coverage reports 43 | htmlcov/ 44 | .tox/ 45 | .nox/ 46 | .coverage 47 | .coverage.* 48 | .cache 49 | nosetests.xml 50 | coverage.xml 51 | *.cover 52 | *.py,cover 53 | .hypothesis/ 54 | .pytest_cache/ 55 | 56 | # Translations 57 | *.mo 58 | *.pot 59 | 60 | # Django stuff: 61 | *.log 62 | local_settings.py 63 | db.sqlite3 64 | db.sqlite3-journal 65 | 66 | # Flask stuff: 67 | instance/ 68 | .webassets-cache 69 | 70 | # Scrapy stuff: 71 | .scrapy 72 | 73 | # Sphinx documentation 74 | docs/_build/ 75 | 76 | # PyBuilder 77 | target/ 78 | 79 | # Jupyter Notebook 80 | .ipynb_checkpoints 81 | 82 | # IPython 83 | profile_default/ 84 | ipython_config.py 85 | 86 | # pyenv 87 | .python-version 88 | 89 | # pipenv 90 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 91 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 92 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 93 | # install all needed dependencies. 94 | #Pipfile.lock 95 | 96 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 97 | __pypackages__/ 98 | 99 | # Celery stuff 100 | celerybeat-schedule 101 | celerybeat.pid 102 | 103 | # SageMath parsed files 104 | *.sage.py 105 | 106 | # Environments 107 | .env 108 | .venv 109 | env/ 110 | venv/ 111 | ENV/ 112 | env.bak/ 113 | venv.bak/ 114 | 115 | # Spyder project settings 116 | .spyderproject 117 | .spyproject 118 | 119 | # Rope project settings 120 | .ropeproject 121 | 122 | # mkdocs documentation 123 | /site 124 | 125 | # mypy 126 | .mypy_cache/ 127 | .dmypy.json 128 | dmypy.json 129 | 130 | # Pyre type checker 131 | .pyre/ 132 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at matan1008@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Matan Perelman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # objc-types-decoder 2 | 3 | [![Python application](https://github.com/matan1008/objc-types-decoder/workflows/Python%20application/badge.svg)](https://github.com/matan1008/objc-types-decoder/actions/workflows/python-app.yml "Python application action") 4 | [![Pypi version](https://img.shields.io/pypi/v/objc-types-decoder.svg)](https://pypi.org/project/objc-types-decoder/ "PyPi package") 5 | [![Downloads](https://static.pepy.tech/personalized-badge/objc-types-decoder?period=total&units=none&left_color=grey&right_color=blue&left_text=Downloads)](https://pepy.tech/project/objc-types-decoder) 6 | 7 | 8 | A type decoder for Objective-C types. 9 | 10 | It translates the encoded Objective-C type notation, the notation that the `@encode` function returns, into a readable 11 | form that tries to be as close as possible to the original type definition. 12 | 13 | For example, lets look at the following `@encode`: 14 | 15 | ```objective-c 16 | NSLog(@"%s", @encode(float **)); // "^^f" will be printed. 17 | ``` 18 | 19 | Using our decoder, we can "reverse" the process: 20 | 21 | ```python 22 | from objc_types_decoder.decode import decode 23 | 24 | print(decode('^^f')) # 'float * *' will be printed. 25 | ``` 26 | 27 | ## Installation 28 | 29 | In order to install this package, just use a regular `pip` installation: 30 | 31 | ```shell 32 | pip install objc_types_decoder 33 | ``` 34 | 35 | ## Usage 36 | 37 | In order to use the decoder, just run the main with your desired encoded type: 38 | 39 | ```shell 40 | >> py -m objc_types_decoder ^f 41 | float * 42 | ``` 43 | 44 | You can also decode by importing it in your python code: 45 | 46 | ```python 47 | >> from objc_types_decoder.decode import decode 48 | >> decode('{NSObject=#}') 49 | 'struct NSObject { Class x0; }' 50 | ``` 51 | 52 | Sometimes, you might want to keep the tail of the parsed data. For this case, you can use `decode_with_tail`. 53 | 54 | ```python 55 | >> from objc_types_decoder.decode import decode_with_tail 56 | >> decode_with_tail('fyour boat') 57 | ('float', 'your boat') 58 | ``` 59 | 60 | -------------------------------------------------------------------------------- /objc_types_decoder/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/matan1008/objc-types-decoder/28dcd42084d4c09cdd5553cacd24ca475dead946/objc_types_decoder/__init__.py -------------------------------------------------------------------------------- /objc_types_decoder/__main__.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from .decode import decode 4 | 5 | 6 | def main(): 7 | parser = argparse.ArgumentParser(description='Parse ObjectiveC encoded types') 8 | parser.add_argument('encoded', help='Type to decode') 9 | args = parser.parse_args() 10 | print(decode(args.encoded)) 11 | 12 | 13 | if __name__ == '__main__': 14 | main() 15 | -------------------------------------------------------------------------------- /objc_types_decoder/decode.py: -------------------------------------------------------------------------------- 1 | SIMPLE_TYPES = { 2 | 'c': 'char', 3 | 'i': 'int', 4 | 's': 'short', 5 | 'l': 'long', 6 | 'q': 'long long', 7 | 'C': 'unsigned char', 8 | 'I': 'unsigned int', 9 | 'S': 'unsigned short', 10 | 'L': 'unsigned long', 11 | 'Q': 'unsigned long long', 12 | 'f': 'float', 13 | 'd': 'double', 14 | 'B': 'BOOL', 15 | 'v': 'void', 16 | '*': 'char *', 17 | '@': 'id', 18 | '#': 'Class', 19 | ':': 'SEL', 20 | '?': '', 21 | } 22 | 23 | TYPE_SPECIFIERS = { 24 | 'r': 'const', 25 | 'n': 'in', 26 | 'N': 'inout', 27 | 'o': 'out', 28 | 'O': 'bycopy', 29 | 'R': 'byref', 30 | 'V': 'oneway' 31 | } 32 | 33 | 34 | def index_of_closing_char(s: str, open_: str, close: str) -> int: 35 | depth = 0 36 | for i in range(len(s)): 37 | depth += {open_: 1, close: -1}.get(s[i], 0) 38 | if not depth: 39 | return i 40 | 41 | 42 | def get_digits(s: str): 43 | digits = '' 44 | for i in range(len(s)): 45 | if s[i].isdigit(): 46 | digits += s[i] 47 | else: 48 | break 49 | return digits 50 | 51 | 52 | # Decoders 53 | 54 | def decode_pointer(encoded): 55 | decoded = decode_type_recursive(encoded[1:]) 56 | return {'kind': 'pointer', 'type': decoded, 'tail': decoded['tail']} 57 | 58 | 59 | def decode_complex(encoded): 60 | decoded = decode_type_recursive(encoded[1:]) 61 | return {'kind': 'complex', 'type': decoded, 'tail': decoded['tail']} 62 | 63 | 64 | def decode_type_specifier(encoded): 65 | decoded = decode_type_recursive(encoded[1:]) 66 | return {'kind': 'specifier', 'type': decoded, 'tail': decoded['tail'], 'specifier': TYPE_SPECIFIERS[encoded[0]]} 67 | 68 | 69 | def decode_fielded_type(encoded, kind, open, close): 70 | close_index = index_of_closing_char(encoded, open, close) 71 | long_tail = encoded[close_index + 1:] 72 | type_str = encoded[1:close_index] 73 | if '=' not in type_str: 74 | return {'kind': kind, 'types': None, 'name': type_str, 'tail': long_tail} 75 | name, type_str = type_str.split('=', 1) 76 | types_in_str = [] 77 | while type_str: 78 | decoded = decode_type_recursive(type_str) 79 | types_in_str.append(decoded) 80 | type_str = decoded['tail'] 81 | return {'kind': kind, 'types': types_in_str, 'name': name, 'tail': long_tail} 82 | 83 | 84 | def decode_struct(encoded: str): 85 | return decode_fielded_type(encoded, 'struct', '{', '}') 86 | 87 | 88 | def decode_union(encoded: str): 89 | return decode_fielded_type(encoded, 'union', '(', ')') 90 | 91 | 92 | def decode_array(encoded: str): 93 | close_index = index_of_closing_char(encoded, '[', ']') 94 | array_str = encoded[1:close_index] 95 | digits = get_digits(array_str) 96 | type_encoded = array_str[len(digits):] 97 | # If the type is omitted, assume 'void *' 98 | decoded = decode_type_recursive(type_encoded if type_encoded else '^v') 99 | return {'kind': 'array', 'count': digits, 'type': decoded, 'tail': encoded[close_index + 1:]} 100 | 101 | 102 | def decode_name(encoded): 103 | close_index = encoded.index('"', 1) 104 | return {'kind': 'name', 'name': encoded[1:close_index], 'tail': encoded[close_index + 1:]} 105 | 106 | 107 | def decode_bit_fields(encoded): 108 | count_str = encoded[len('b'):] 109 | digits = get_digits(count_str) 110 | return {'kind': 'bitfield', 'count': digits, 'tail': count_str[len(digits):]} 111 | 112 | 113 | def decode_type_recursive(encoded: str): 114 | if encoded[0] in SIMPLE_TYPES: 115 | return {'kind': 'simple', 'type': SIMPLE_TYPES[encoded[0]], 'tail': encoded[1:]} 116 | elif encoded[0] in TYPE_SPECIFIERS: 117 | return decode_type_specifier(encoded) 118 | elif encoded[0] == '^': 119 | return decode_pointer(encoded) 120 | elif encoded[0] == 'j': 121 | return decode_complex(encoded) 122 | elif encoded[0] == '{': 123 | return decode_struct(encoded) 124 | elif encoded[0] == '[': 125 | return decode_array(encoded) 126 | elif encoded[0] == '"': 127 | return decode_name(encoded) 128 | elif encoded[0] == 'b': 129 | return decode_bit_fields(encoded) 130 | elif encoded[0] == '(': 131 | return decode_union(encoded) 132 | return decode_name(f'"{encoded}"') 133 | 134 | 135 | # Descriptions 136 | 137 | def description_for_pointer(type_dictionary): 138 | return description_for_type(type_dictionary['type']) + ' *' 139 | 140 | 141 | def description_for_complex(type_dictionary): 142 | return description_for_type(type_dictionary['type']) + ' complex' 143 | 144 | 145 | def description_for_specifier(type_dictionary): 146 | return type_dictionary['specifier'] + ' ' + description_for_type(type_dictionary['type']) 147 | 148 | 149 | def description_for_simple(type_dictionary): 150 | tail = type_dictionary['tail'] 151 | if type_dictionary['type'] == 'id' and tail: 152 | if tail[0] == '"': 153 | name = decode_type_recursive(tail)['name'] 154 | return name + ' *' 155 | elif tail[0] == '?': 156 | return 'id /* block */' 157 | return type_dictionary['type'] 158 | 159 | 160 | def description_for_fielded_type(type_dictionary, type_name): 161 | name = type_dictionary['name'] if type_dictionary['name'] != '?' else '' 162 | desc = f'{type_name} ' + name 163 | if type_dictionary['types'] is None: 164 | return desc 165 | desc = desc.rstrip(' ') + ' { ' 166 | for i, type_ in enumerate(type_dictionary['types']): 167 | if type_['kind'] == 'array': 168 | desc += description_for_type(type_['type']) + f' x{i}[{type_["count"]}]; ' 169 | elif type_['kind'] == 'bitfield': 170 | desc += f'int x{i} : {type_["count"]}; ' 171 | else: 172 | desc += description_for_type(type_) + f' x{i}; ' 173 | desc += '}' 174 | return desc 175 | 176 | 177 | def description_for_struct(type_dictionary): 178 | return description_for_fielded_type(type_dictionary, 'struct') 179 | 180 | 181 | def description_for_union(type_dictionary): 182 | return description_for_fielded_type(type_dictionary, 'union') 183 | 184 | 185 | def description_for_array(type_dictionary): 186 | return description_for_type(type_dictionary['type']) + f' x[{type_dictionary["count"]}]' 187 | 188 | 189 | def description_for_name(type_dictionary): 190 | return type_dictionary['name'] 191 | 192 | 193 | def description_for_bitfield(type_dictionary): 194 | return f'int x : {type_dictionary["count"]}' 195 | 196 | 197 | def description_for_type(type_dictionary): 198 | return { 199 | 'pointer': description_for_pointer, 200 | 'complex': description_for_complex, 201 | 'specifier': description_for_specifier, 202 | 'simple': description_for_simple, 203 | 'struct': description_for_struct, 204 | 'array': description_for_array, 205 | 'name': description_for_name, 206 | 'bitfield': description_for_bitfield, 207 | 'union': description_for_union, 208 | }[type_dictionary['kind']](type_dictionary) 209 | 210 | 211 | def decode(encoded): 212 | return description_for_type(decode_type_recursive(encoded)) 213 | 214 | 215 | def decode_with_tail(encoded): 216 | decoded = decode_type_recursive(encoded) 217 | return description_for_type(decoded), decoded.get('tail', '') 218 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [project] 2 | name = "objc_types_decoder" 3 | version = "0.0.5" 4 | description = "A type decoder for Objective-C types" 5 | readme = "README.md" 6 | requires-python = ">=3.7" 7 | license = { file = "LICENSE" } 8 | keywords = ["ios", "Objective-C"] 9 | authors = [ 10 | { name = "matan", email = "matan1008@gmail.com" } 11 | ] 12 | maintainers = [ 13 | { name = "matan", email = "matan1008@gmail.com" } 14 | ] 15 | classifiers = [ 16 | "Development Status :: 5 - Production/Stable", 17 | "License :: OSI Approved :: MIT License", 18 | "Programming Language :: Python :: 3", 19 | "Programming Language :: Python :: 3.7", 20 | "Programming Language :: Python :: 3.8", 21 | "Programming Language :: Python :: 3.9", 22 | "Programming Language :: Python :: 3.10", 23 | "Programming Language :: Python :: 3.11", 24 | "Programming Language :: Python :: 3.12", 25 | "Programming Language :: Python :: 3.13", 26 | "Programming Language :: Python :: 3 :: Only", 27 | ] 28 | 29 | [project.optional-dependencies] 30 | test = ["pytest"] 31 | 32 | [project.urls] 33 | "Homepage" = "https://github.com/matan1008/objc-types-decoder" 34 | "Bug Reports" = "https://github.com/matan1008/objc-types-decoder/issues" 35 | 36 | [project.scripts] 37 | objc_types_decoder = "objc_types_decoder.__main__:main" 38 | 39 | [tool.setuptools.packages.find] 40 | exclude = ["tests*"] 41 | 42 | [build-system] 43 | requires = ["setuptools>=43.0.0", "wheel"] 44 | build-backend = "setuptools.build_meta" 45 | -------------------------------------------------------------------------------- /tests/test_decode_sanity.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from objc_types_decoder.decode import decode 4 | 5 | 6 | @pytest.mark.parametrize('encoded, decoded', [ 7 | ('c', 'char'), 8 | ('i', 'int'), 9 | ('s', 'short'), 10 | ('l', 'long'), 11 | ('q', 'long long'), 12 | ('C', 'unsigned char'), 13 | ('I', 'unsigned int'), 14 | ('S', 'unsigned short'), 15 | ('L', 'unsigned long'), 16 | ('Q', 'unsigned long long'), 17 | ('f', 'float'), 18 | ('d', 'double'), 19 | ('B', 'BOOL'), 20 | ('v', 'void'), 21 | ('*', 'char *'), 22 | ('@', 'id'), 23 | ('#', 'Class'), 24 | (':', 'SEL'), 25 | ]) 26 | def test_decoding_simple_types(encoded, decoded): 27 | assert decode(encoded) == decoded 28 | 29 | 30 | def test_decoding_block(): 31 | assert decode('@?') == 'id /* block */' 32 | 33 | 34 | @pytest.mark.parametrize('encoded, decoded', [ 35 | ('{example=@*i}', 'struct example { id x0; char * x1; int x2; }'), 36 | ('{NSObject=#}', 'struct NSObject { Class x0; }'), 37 | ('{example=}', 'struct example { }'), 38 | ('{example}', 'struct example'), 39 | ('{?=}', 'struct { }'), 40 | ('{?=i}', 'struct { int x0; }'), 41 | ('^{tmp=I[2:]b16b16*^{__CFString}}', 42 | 'struct tmp { unsigned int x0; SEL x1[2]; int x2 : 16; int x3 : 16; char * x4; struct __CFString * x5; } *'), 43 | ('^{tmp=I[2:]I}', 'struct tmp { unsigned int x0; SEL x1[2]; unsigned int x2; } *'), 44 | ('{bStruct={aStruct=iq@}{aStruct=iq@}}', 'struct bStruct { struct aStruct { int x0; long long x1; id x2; } x0;' 45 | ' struct aStruct { int x0; long long x1; id x2; } x1; }'), 46 | ]) 47 | def test_decoding_struct(encoded, decoded): 48 | assert decode(encoded) == decoded 49 | 50 | 51 | @pytest.mark.parametrize('encoded, decoded', [ 52 | ('^{example=@*i}', 'struct example { id x0; char * x1; int x2; } *'), 53 | ('^jf', 'float complex *'), 54 | ]) 55 | def test_decoding_pointer(encoded, decoded): 56 | assert decode(encoded) == decoded 57 | 58 | 59 | @pytest.mark.parametrize('encoded, decoded', [ 60 | ('jd', 'double complex'), 61 | ]) 62 | def test_decoding_complex(encoded, decoded): 63 | assert decode(encoded) == decoded 64 | 65 | 66 | @pytest.mark.parametrize('encoded, decoded', [ 67 | ('b16', 'int x : 16'), 68 | ]) 69 | def test_decoding_bitfield(encoded, decoded): 70 | assert decode(encoded) == decoded 71 | 72 | 73 | @pytest.mark.parametrize('encoded, decoded', [ 74 | ('[12^f]', 'float * x[12]'), 75 | ('[4]', 'void * x[4]'), 76 | ]) 77 | def test_decoding_array(encoded, decoded): 78 | assert decode(encoded) == decoded 79 | 80 | 81 | @pytest.mark.parametrize('encoded, decoded', [ 82 | ('(example=^v*i)', 'union example { void * x0; char * x1; int x2; }'), 83 | ('(NSObject=#)', 'union NSObject { Class x0; }'), 84 | ('(example=)', 'union example { }'), 85 | ('(example)', 'union example'), 86 | ('(?=)', 'union { }'), 87 | ('(?=i)', 'union { int x0; }'), 88 | ('^(tmp=I[2:]b16b16*^(Data))', 89 | 'union tmp { unsigned int x0; SEL x1[2]; int x2 : 16; int x3 : 16; char * x4; union Data * x5; } *'), 90 | ('^(tmp=I[2:]I)', 'union tmp { unsigned int x0; SEL x1[2]; unsigned int x2; } *'), 91 | ('(bStruct={aStruct=iq@}(aStruct=iq@))', 'union bStruct { struct aStruct { int x0; long long x1; id x2; } x0;' 92 | ' union aStruct { int x0; long long x1; id x2; } x1; }'), 93 | ]) 94 | def test_decoding_union(encoded, decoded): 95 | assert decode(encoded) == decoded 96 | -------------------------------------------------------------------------------- /tests/test_decode_type_with_tail_sanity.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from objc_types_decoder.decode import decode_with_tail 4 | 5 | 6 | @pytest.mark.parametrize('encoded, decoded, tail', [ 7 | ('labcd', 'long', 'abcd'), 8 | ('{?=i},hey you', 'struct { int x0; }', ',hey you'), 9 | ('(?=i),hey you', 'union { int x0; }', ',hey you'), 10 | ('[4^f]| Baby just say yes!', 'float * x[4]', '| Baby just say yes!'), 11 | ]) 12 | def test_with_tail(encoded, decoded, tail): 13 | assert decode_with_tail(encoded) == (decoded, tail) 14 | 15 | 16 | @pytest.mark.parametrize('encoded, decoded', [ 17 | ('l', 'long'), 18 | ('{?=i}', 'struct { int x0; }'), 19 | ('(?=i)', 'union { int x0; }'), 20 | ('[4^f]', 'float * x[4]'), 21 | ]) 22 | def test_without_tail(encoded, decoded): 23 | assert decode_with_tail(encoded) == (decoded, '') 24 | --------------------------------------------------------------------------------