{"record":{"id":"d2de542036956761","repo":"pypa/pip","slug":"unrecognized-capacity-self-capacity-or-encodin","errorCode":null,"errorMessage":"unrecognized capacity ({self.capacity}) or encoding ({self.encoding})","messagePattern":"unrecognized capacity \\((.+?)\\) or encoding \\((.+?)\\)","errorType":"validation","errorClass":"ELFInvalid","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/packaging/_elffile.py","lineNumber":70,"sourceCode":"        magic = bytes(ident[:4])\n        if magic != b\"\\x7fELF\":\n            raise ELFInvalid(f\"invalid magic: {magic!r}\")\n\n        self.capacity = ident[4]  # Format for program header (bitness).\n        self.encoding = ident[5]  # Data structure encoding (endianness).\n\n        try:\n            # e_fmt: Format for the ELF header.\n            # p_fmt: Format for a program header.\n            # p_idx: Indexes to find p_type, p_offset, and p_filesz.\n            e_fmt, self._p_fmt, self._p_idx = {\n                (1, 1): (\"<HHIIIIIHHH\", \"<IIIIIIII\", (0, 1, 4)),  # 32-bit LSB.\n                (1, 2): (\">HHIIIIIHHH\", \">IIIIIIII\", (0, 1, 4)),  # 32-bit MSB.\n                (2, 1): (\"<HHIQQQIHHH\", \"<IIQQQQQQ\", (0, 2, 5)),  # 64-bit LSB.\n                (2, 2): (\">HHIQQQIHHH\", \">IIQQQQQQ\", (0, 2, 5)),  # 64-bit MSB.\n            }[(self.capacity, self.encoding)]\n        except KeyError as e:\n            raise ELFInvalid(\n                f\"unrecognized capacity ({self.capacity}) or encoding ({self.encoding})\"\n            ) from e\n\n        try:\n            (\n                _,\n                self.machine,  # Architecture type.\n                _,\n                _,\n                self._e_phoff,  # Offset of program header.\n                _,\n                self.flags,  # Processor-specific flags.\n                _,\n                self._e_phentsize,  # Size of a program header entry.\n                self._e_phnum,  # Number of program headers.\n            ) = self._read(e_fmt)\n        except struct.error as e:\n            raise ELFInvalid(\"unable to parse machine and section information\") from e","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_vendor/packaging/_elffile.py#L52-L88","documentation":"Bytes 4 and 5 of the ELF identification array specify EI_CLASS (1=32-bit, 2=64-bit) and EI_DATA (1=little-endian, 2=big-endian). ELFFile looks up a struct format tuple keyed by (capacity, encoding). Values 0 and 3+ are reserved/invalid per the ELF spec. If the pair is absent from the lookup dict, KeyError is caught and re-raised as ELFInvalid showing both values.","triggerScenarios":"An ELF file whose EI_CLASS (byte 4) or EI_DATA (byte 5) is 0 (ELFCLASSNONE/ELFDATANONE) or any value >2. Happens with deliberately malformed/fuzzed ELF files, unusual embedded firmware, or non-standard toolchains.","commonSituations":"Analyzing obfuscated or packed malware, processing exotic embedded firmware, handling ELF files from non-standard compilers that use reserved EI_CLASS/EI_DATA values.","solutions":["Inspect bytes 4-5 of the file to understand the capacity/encoding values before parsing","Fall back to a full-featured ELF parser like pyelftools for unusual variants","Skip files with unsupported class/encoding values in batch processing"],"exampleFix":"# before\nelf = ELFFile(f)  # raises on unusual EI_CLASS/EI_DATA\n\n# after\nimport struct\nf.seek(4)\nei_class, ei_data = struct.unpack(\"BB\", f.read(2))\nf.seek(0)\nif ei_class not in (1, 2) or ei_data not in (1, 2):\n    print(f\"Unsupported ELF class={ei_class} data={ei_data}, skipping\")\n    continue\nelf = ELFFile(f)","handlingStrategy":"validation","validationCode":"import struct\n\ndef validate_elf_class_encoding(path):\n    with open(path, \"rb\") as f:\n        f.seek(4)\n        ei_class, ei_data = struct.unpack(\"BB\", f.read(2))\n    if ei_class not in (1, 2):\n        raise ValueError(f\"Unsupported ELF class: {ei_class}\")\n    if ei_data not in (1, 2):\n        raise ValueError(f\"Unsupported ELF data encoding: {ei_data}\")","typeGuard":null,"tryCatchPattern":"from packaging._elffile import ELFFile, ELFInvalid\n\ntry:\n    elf = ELFFile(f)\nexcept ELFInvalid as e:\n    if \"unrecognized capacity\" in str(e):\n        # unsupported architecture; fall back to pyelftools\n        elf = None","preventionTips":["Pre-validate EI_CLASS and EI_DATA bytes (offsets 4 and 5)","Fall back to pyelftools for exotic ELF variants","Skip binaries from untrusted or fuzzed sources"],"tags":["elf","packaging","binary-parsing","architecture","vendored"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}