{"id":6553,"library":"bsdiff4","title":"bsdiff4 Library","description":"bsdiff4 is a Python library providing functions for binary diff and patch operations, based on the `bsdiff4` algorithm. It allows computing the difference between two byte sequences or files, and then reconstructing the new sequence/file from the old one and the diff. As of version 1.2.6, it provides a stable interface for managing binary data changes.","status":"active","version":"1.2.6","language":"python","source_language":"en","source_url":"https://github.com/ilanschnell/bsdiff4","tags":["binary diff","patch","delta encoding","file comparison"],"install":[{"cmd":"pip install bsdiff4","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"diff","correct":"import bsdiff4\n# bsdiff4.diff(...)"},{"symbol":"patch","correct":"import bsdiff4\n# bsdiff4.patch(...)"},{"symbol":"file_diff","correct":"import bsdiff4\n# bsdiff4.file_diff(...)"},{"symbol":"file_patch","correct":"import bsdiff4\n# bsdiff4.file_patch(...)"}],"quickstart":{"code":"import bsdiff4\nimport os\n\n# Example with bytes\nold_data = b\"This is the old data string.\"\nnew_data = b\"This is the new and updated data string.\"\n\n# Generate a binary diff\ndiff_data = bsdiff4.diff(old_data, new_data)\nprint(f\"Diff data length: {len(diff_data)} bytes\")\n\n# Apply the patch to get back the new data\npatched_data = bsdiff4.patch(old_data, diff_data)\nassert patched_data == new_data\nprint(f\"Patched data (bytes): {patched_data.decode('utf-8')}\\n\")\n\n# Example with files\nfile_old = 'old_file.txt'\nfile_new = 'new_file.txt'\nfile_diff = 'diff.bin'\nfile_patched = 'patched_file.txt'\n\nwith open(file_old, 'wb') as f:\n    f.write(old_data)\nwith open(file_new, 'wb') as f:\n    f.write(new_data)\n\n# Generate diff between files\nbsdiff4.file_diff(file_old, file_new, file_diff)\nprint(f\"File diff created: {file_diff}\")\n\n# Apply patch to recreate new file\nbsdiff4.file_patch(file_old, file_patched, file_diff)\n\nwith open(file_patched, 'rb') as f:\n    recreated_data = f.read()\nassert recreated_data == new_data\nprint(f\"Patched file created: {file_patched} (content matches new_file.txt)\")\n\n# Clean up generated files\nos.remove(file_old)\nos.remove(file_new)\nos.remove(file_diff)\nos.remove(file_patched)\n","lang":"python","description":"This quickstart demonstrates how to use `bsdiff4` to generate and apply binary differences for both `bytes` objects in memory and actual files on disk. It shows how to compute a `diff` from old and new data, and then apply that `diff` to the old data to reconstruct the new data."},"warnings":[{"fix":"Ensure all string data is encoded to bytes (e.g., `my_string.encode('utf-8')`) before passing it to bsdiff4 functions.","message":"All input data (old, new, and diff) for `bsdiff4.diff` and `bsdiff4.patch` functions must be `bytes` objects, not strings. Passing strings will result in `TypeError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consider processing data in smaller chunks if feasible, or ensure sufficient RAM is available for operations on large files. The `file_diff` and `file_patch` functions might manage memory slightly better by handling file streams, but the core algorithm still requires memory proportional to the input size.","message":"For very large files, `bsdiff4` can be memory intensive, as it often loads entire files or significant portions into memory during diffing and patching operations. Monitor memory usage for performance-critical applications.","severity":"gotcha","affected_versions":"All versions"},{"fix":"When exchanging diffs with other systems or languages, ensure that all parties are using a `bsdiff4` compatible implementation to avoid parsing errors or incorrect patches.","message":"The `bsdiff4` library implements the `bsdiff4` format, which is a specific version of the bsdiff algorithm. Diffs generated by this library may not be compatible with other bsdiff implementations (e.g., bsdiff3, or other variations) if they use a different format version.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Install the necessary build tools and C compiler for your operating system (e.g., `build-essential` on Debian/Ubuntu, Xcode Command Line Tools on macOS, Visual C++ Build Tools on Windows) or use an environment with pre-built wheels.","message":"On some systems, particularly those without pre-built binary wheels available on PyPI (e.g., specific Linux distributions, older macOS versions, or Windows without development tools), `pip install bsdiff4` might fail due to the need for a C compiler to build its native extensions.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'1.2.6':48 'algorithm':20 'allow':22 'base':16 'binari':11,56,59 'bsdiff4':1,3,19 'byte':28 'chang':58 'comparison':65 'comput':23 'data':57 'delta':62 'diff':12,44,60 'differ':25 'encod':63 'file':31,64 'function':9 'interfac':53 'librari':2,7 'manag':55 'new':36 'old':40 'one':41 'oper':15 'patch':14,61 'provid':8,50 'python':6 'reconstruct':34 'sequenc':29 'sequence/file':37 'stabl':52 'two':27 'version':47","created_at":"2026-04-15T18:32:49.801043+00:00","updated_at":"2026-04-16T01:02:19.660536+00:00","problems":[{"fix":"Install the package using pip: `pip install bsdiff4`","cause":"The 'bsdiff4' package is not installed in your Python environment.","error":"ModuleNotFoundError: No module named 'bsdiff4'"},{"fix":"On Debian/Ubuntu, install `python3-dev` and `build-essential` (or `gcc`): `sudo apt-get install python3-dev build-essential`. On macOS, install Xcode Command Line Tools: `xcode-select --install`.","cause":"The 'bsdiff4' library includes a C extension that requires a C compiler and Python development headers to be present for successful installation.","error":"ERROR: Command errored out with exit status 1: ... running build_ext building 'bsdiff4.core' extension ... error: command 'gcc' failed with exit status 1"},{"fix":"Convert your string data to bytes using the `.encode()` method, specifying an appropriate encoding (e.g., `my_string.encode('utf-8')`).","cause":"The `bsdiff4.diff` and `bsdiff4.patch` functions operate on binary data and require byte sequences (`bytes`) as input, but a string (`str`) was provided.","error":"TypeError: expected bytes-like object, not str"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.2.6","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/ilanschnell/bsdiff4","docs":null,"changelog":null,"pypi":"https://pypi.org/project/bsdiff4/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["data","serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":null}}