Files
simplex-chat/packages/simplex-chat-python/src/simplex_chat/_native.py
T
fbca2cae81 core, ui: resize images in the core for sharper previews and images (#7941)
* WIP: Haskell impl of resizeImageToStrSize

* WIP: add FFI export wrapper

* WIP: switch ios resizeImageToStrSizeSync to backend

* WIP: fix resizeImageToStrSizeSync

* WIP: add JNI wrappers

* Add non-base64 resize and cWriteImage

* WIP: add chat_write_image

* fix resizeToStr

* fixes

* fix syntax, raise jpeg quality back

* add chatWriteImage

* Fix cfArgs in tests

* Use jpeg-turbo encoder

* add nix pins

* bump jpeg-turbo

* add CI deps

* add encoding funs to android lib

* bump jpeg-turbo for 20.04 fix

* jpeg-turbo fix for 8.10

* android fixes

* Bump jpeg-turbo

* set static flag for jpeg-turbo

* Fix nix for aarch64/android

* Fix android C api

* Fix resize in android

* Bump hs-libjpeg-turbo

* target correct override

* override android lib from android pkgs

* add macos build tweaks

* bump hs-jpeg-turbo for 8.10 fixes

* more build fixes

* make ios compile

* Always use pkgconfig for libjpeg-turbo

* WIP: debug libjpeg in ubuntu pkg-config CI

* Add pkg-config to dockerfiles

* Add libturbojpeg to dockerfiles

* Use -dev version of libturbojpeg in dockerfiles

* Install pkg-config in windows CI

* Disable flag for custom libturbojpeg linking options

Workarounds relocation error on x86_64-linux with gcc:
  /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/libturbojpeg.a(turbojpeg.c.o): relocation R_X86_64_TPOFF32 against `errStr' can not be used when making a shared object; recompile with -fPIC
  /usr/bin/ld: failed to set dynamic section sizes: bad value

* WIP: debug libjpeg in windows pkg-config CI

* WIP: debug libjpeg package in windows CI

* core: image resize module and buffer FFI

* ci: link libjpeg-turbo statically on all platforms

* ui: resize images in the core

* core: rename image resize functions to chat_resize_image and chat_resize_image_uri

* ci: restore whitespace in build.yml

* ci: build libjpeg-turbo 3.1.4.1 from a submodule

* core: hs-jpeg-turbo from github.com/simplex-chat

* nodejs: resize images

* python: resize images

* core: hs-jpeg-turbo from gitlab.com, the GitHub import lacks LFS objects

* core: generate /img previews

* ci: link libjpeg-turbo on Windows without pkg-config

* ci: build libjpeg-turbo on Windows with the GHC toolchain

* docs: plan for image resizing in the core

* docs: update image resizing plan

* ios: prevent crash when wallpaper image cannot be resized

* android, desktop: raise an error when an image cannot be resized

* docs: update spec references for image resizing

* docker: build the CLI image from a clone without submodules

* docs: apply review fixes to image resizing plan

* docker: build the CLI from a git worktree with the submodule present

* docs: apply second review to image resizing plan

* docs: point image resizing plan at the rewritten commit

* core: choose image format by transparency in chat_resize_image

* nodejs: resize images to the app sizes

* python: resize images to the app sizes

* nodejs, python: describe the core modules as used for image resizing

* docs: add license notices of the image libraries

* ci: describe the avatar method without attributing it to the apps

* ci, ios, nix: remove the libjpeg-turbo build outside cabal

* python: name resize_image in the core module text

* nodejs, python: describe resizing to the app image sizes

* core: refuse images larger than the supported size

* core: resize to JPEG images with a side above 65500 pixels

* docs: record image resizing decisions for the bundled libjpeg-turbo

* core, docs: build libjpeg-turbo and stb from the fork submodules

* docs: record the fork pins in the image resizing plan

* core: resize images to at most 4320 pixels per side, refuse above 16384

* docs: record the image side limits in the image resizing plan

* core: pin the rebased hs-jpeg-turbo fork

* core: pin the hs-jpeg-turbo fork without package.yaml

* docs: plan scaled JPEG decoding and EXIF orientation of images

* docs: copy the JuicyPixels-stbir licence of the pinned fork

* core: pin the hs-jpeg-turbo fork with scaled JPEG decoding

* core: decode JPEG at the display size, up to 65500 pixels per side

* core: rotate JPEG images by their EXIF orientation

* docs: refer the image plan to the JPEG decoding code

* docs: record the approved image processing decisions

* core: limit JPEG decoding memory to 1024 MB outside iOS and Android

* docs: name JPEG decoding in the libjpeg-turbo notice

* docs: plan image resizing from files and the decisions of 26 Oct

* core: scale images in one Catmull-Rom pass with sharpening

* docs: plan image sizes at a fixed quality

* core: resize images at a fixed quality to fill the size limit

* core: resize images from a file path or a file descriptor

* ios, android, desktop: resize picked images from their files

* core: encode resized images with mozjpeg

* tests: check descriptor closing by the file it refers to

* core: raise JPEG quality only for a full-size image below 97% of the limit

* ci: name the mozjpeg submodule in the Windows cleanup comments

* docs: refer the image plan to the current code

* docs: refer the image plan to the code merged with master

* core: export every image resizing function from each library

* core: close the file descriptor when its handle cannot be created

* docs: count the C sources of mozjpeg with the arithmetic decoder

* core: use the reviewed hs-jpeg-turbo and JuicyPixels-stbir forks

* ci: build the armv7a Android core library on request

* core: round image heights half up in the size search

* ios: describe the file image resizing functions in SimpleX.h

* ci: pick, preview and send images in the Claude app tests

* core: decode PNG images with libspng and refuse other formats

* core: encode PNG with libspng, keeping the smaller filter choice

* ci: pick, preview and send images in the scripted app smoke tests

* flake: build the PNG decoder package in nix builds

* ios: remove the copy of a picked image after sending it

* core: refuse image files larger than the decode memory limit

* core: scale a JPEG image to its largest size once per resize

* core: bisect JPEG quality without a step function

* core: reduce the memory used to sharpen and to decode PNG images

* android, desktop: pass the image format to the core as an integer

* android, desktop: fuse the preview conditions of picked media

* android, desktop, ci: check that the core resized the smoke test images

* python: remove the docstrings of the explicit image resizing functions

* docs: add the second review fixes to the image plan

* ci: tap the iOS attachment button by its accessibility label

* core: keep the colour profiles of resized images

* docs: add the colour profiles to the image plan

* ci: wait up to 30 s for the new group screens in the iOS smoke test

* ci: pick the Android smoke test images from their own photo folders

* ci: tap the iOS photo picker by position, as its tree is not shown

* ci: pick the test card and the logo by their iOS picker positions

* core: update JuicyPixels-stbir with the stb_image_resize licence terms

* core: write a fixed Display P3 profile for Display P3 images only

* core: do not keep the input image in memory for its colour profile

* core: read the JPEG segments once for the orientation and the profile

* core: read an image file into one buffer of its size

* docs: add the third review fixes to the image plan

* core: resize images with Display P3 or no profile, not profile bytes

* ci: check the pixel sizes of the images sent in the iOS smoke test

* core: update hs-jpeg-turbo with PNG chunk limits and encoder fixes

* docs: list Compact ICC Profiles, libspng and miniz in the dependencies

* android, desktop: reword the picked media preview comment

* docs: refer the image plan to the code merged with master

* ci: render the iOS snapshots before the images case

* ci: dismiss the snapshot tests' camera alert before the iOS images case

* core: update hs-jpeg-turbo with tests and the JuicyPixels-spng synopsis

* core: pin hs-jpeg-turbo and JuicyPixels-stbir in simplex-chat repositories

* update ios core library

* core, ui: accept only JPEG and PNG image data

* core: update JuicyPixels-stbir

* docs: plan for the iOS copy of picked images

---------

Co-authored-by: IC Rainbow <aenor.realm@gmail.com>
Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com>
Co-authored-by: Alexandre Esteves <alexfmpe@proton.me>
2026-10-30 12:14:51 +00:00

307 lines
11 KiB
Python

"""Native libsimplex loader: platform detection, lazy download, ctypes setup.
Internal — users interact with `Bot` / `ChatApi`, never with this module.
"""
from __future__ import annotations
import ctypes
import errno
import hashlib
import os
import platform
import sys
import tempfile
import threading
import urllib.request
import zipfile
from ctypes import POINTER, c_char_p, c_int, c_uint8, c_void_p
from pathlib import Path
from typing import Any, Literal
from ._version import LIBS_RELEASE, LIBS_SHA256
Backend = Literal["sqlite", "postgres"]
_GITHUB_REPO = "simplex-chat/simplex-chat-libs"
_PLATFORM_MAP = {
"linux": ("linux", {"x86_64": "x86_64", "aarch64": "aarch64"}),
"darwin": ("macos", {"x86_64": "x86_64", "arm64": "aarch64"}),
"win32": ("windows", {"AMD64": "x86_64", "x86_64": "x86_64"}),
}
_LIBNAME = {"linux": "libsimplex.so", "darwin": "libsimplex.dylib", "win32": "libsimplex.dll"}
SUPPORTED = (
"linux-x86_64",
"linux-aarch64",
"macos-x86_64",
"macos-aarch64",
"windows-x86_64",
)
def _platform_tag() -> str:
info = _PLATFORM_MAP.get(sys.platform)
if not info:
raise RuntimeError(f"Unsupported platform: {sys.platform}")
sysname, archs = info
arch = archs.get(platform.machine())
if not arch:
raise RuntimeError(f"Unsupported architecture: {sys.platform}/{platform.machine()}")
tag = f"{sysname}-{arch}"
if tag not in SUPPORTED:
raise RuntimeError(f"Unsupported combination: {tag}; supported: {SUPPORTED}")
return tag
def _libname() -> str:
return _LIBNAME[sys.platform]
def _libs_asset(backend: Backend) -> str:
suffix = "-postgres" if backend == "postgres" else ""
return f"simplex-chat-libs-{_platform_tag()}{suffix}.zip"
def _libs_url(backend: Backend) -> str:
return (
f"https://github.com/{_GITHUB_REPO}/releases/download/{LIBS_RELEASE}/{_libs_asset(backend)}"
)
def _cache_root() -> Path:
if sys.platform == "darwin":
return Path.home() / "Library" / "Caches" / "simplex-chat"
if sys.platform == "win32":
return Path(os.environ["LOCALAPPDATA"]) / "simplex-chat"
base = os.environ.get("XDG_CACHE_HOME") or str(Path.home() / ".cache")
return Path(base) / "simplex-chat"
def _resolve_libs_dir(backend: Backend) -> Path:
if override := os.environ.get("SIMPLEX_LIBS_DIR"):
return Path(override)
if backend == "postgres" and _platform_tag() != "linux-x86_64":
raise RuntimeError(
"postgres backend is only supported on linux-x86_64; "
f"current platform is {_platform_tag()}"
)
target = _cache_root() / LIBS_RELEASE / backend
if not (target / _libname()).exists():
_download(target, backend)
return target
_DOWNLOAD_CHUNK = 1 << 16 # 64 KiB
def _stream_to_file(url: str, dest: Path, *, timeout: float = 60.0) -> None:
"""Stream `url` → `dest`, printing a carriage-return progress bar.
`timeout` is per-request; we don't touch `socket.setdefaulttimeout`
so other socket users in the same process aren't affected.
"""
with urllib.request.urlopen(url, timeout=timeout) as resp:
total = int(resp.headers.get("Content-Length") or 0)
received = 0
with dest.open("wb") as out:
while chunk := resp.read(_DOWNLOAD_CHUNK):
out.write(chunk)
received += len(chunk)
if total > 0:
pct = min(100, received * 100 // total)
msg = f"\r download: {received >> 20} / {total >> 20} MiB ({pct}%)"
else:
msg = f"\r download: {received >> 20} MiB"
print(msg, end="", file=sys.stderr, flush=True)
print(file=sys.stderr, flush=True) # newline after final progress line
def _download(target: Path, backend: Backend) -> None:
"""Download libs zip → atomic rename into `target`. Concurrent processes safe.
Atomicity strategy: each process extracts to its own sibling tempdir on the same
filesystem, then `os.rename` the `libs/` subdir to `target`. POSIX `os.rename`
onto a NON-EXISTENT path is atomic; if the target exists (another process won
the race), `os.rename` fails on most platforms — we then verify the winner has
what we need and proceed. NEVER rmtree the target: that creates a TOCTOU
window where another process is reading/loading the file we're deleting.
"""
asset = _libs_asset(backend)
sha256 = LIBS_SHA256.get(asset)
if sha256 is None:
raise RuntimeError(f"no SHA-256 is pinned for {asset}")
target.parent.mkdir(parents=True, exist_ok=True)
url = _libs_url(backend)
print(
f"Downloading libsimplex ({_platform_tag()}, {backend}) {LIBS_RELEASE} from {url} ...",
file=sys.stderr,
flush=True,
)
with tempfile.TemporaryDirectory(dir=target.parent) as tmp:
zip_path = Path(tmp) / "libs.zip"
_stream_to_file(url, zip_path, timeout=60.0)
_verify_sha256(zip_path, sha256, url)
with zipfile.ZipFile(zip_path) as zf:
zf.extractall(tmp)
# zip layout: <tmp>/libs/libsimplex.* + libHS*.*
extracted_libs = Path(tmp) / "libs"
if not extracted_libs.is_dir():
raise RuntimeError(f"libs/ missing from {_libs_url(backend)}")
try:
os.rename(extracted_libs, target)
except OSError as e:
# EEXIST / ENOTEMPTY mean another process won the race — fall through
# and check that the winner left a usable libsimplex behind. Anything
# else (ENOSPC, EACCES, EROFS, Windows codes mapped to None) is a real
# failure and must propagate. Same VERSION cached → same content →
# safe to proceed once we've confirmed the file is there.
if e.errno not in (errno.EEXIST, errno.ENOTEMPTY):
raise
if not (target / _libname()).exists():
raise RuntimeError(
f"another process partially populated {target} but libsimplex "
f"is missing; remove the directory manually and retry"
) from e
def _verify_sha256(file: Path, expected: str, url: str) -> None:
digest = hashlib.sha256()
with file.open("rb") as f:
while chunk := f.read(_DOWNLOAD_CHUNK):
digest.update(chunk)
actual = digest.hexdigest()
if actual != expected:
raise RuntimeError(f"SHA-256 of {url} is {actual}, expected {expected}")
_lock = threading.Lock()
_lib: ctypes.CDLL | None = None
_libc: ctypes.CDLL | None = None
_backend: Backend | None = None
def _load_libc() -> ctypes.CDLL:
if sys.platform == "win32":
# libsimplex.dll allocates results with UCRT malloc; msvcrt free would corrupt the heap.
return ctypes.CDLL("ucrtbase")
return ctypes.CDLL(None) # libc on POSIX is the process's own symbol table
def _setup_signatures(lib: ctypes.CDLL) -> None:
"""Declare argtypes/restype for the 8 chat_* functions exported by libsimplex.
All result strings come back as raw c_void_p so the caller can free them
after copying — matches HandleCResult in cpp/simplex.cc:157-165.
"""
lib.chat_migrate_init.argtypes = [c_char_p, c_char_p, c_char_p, POINTER(c_void_p)]
lib.chat_migrate_init.restype = c_void_p
lib.chat_close_store.argtypes = [c_void_p]
lib.chat_close_store.restype = c_void_p
lib.chat_send_cmd.argtypes = [c_void_p, c_char_p]
lib.chat_send_cmd.restype = c_void_p
lib.chat_recv_msg_wait.argtypes = [c_void_p, c_int]
lib.chat_recv_msg_wait.restype = c_void_p
# chat_write_file's payload is treated read-only by libsimplex; passing
# `bytes` via c_char_p avoids the from_buffer_copy doubling. ctypes pins
# the bytes buffer for the duration of the call.
lib.chat_write_file.argtypes = [c_void_p, c_char_p, c_char_p, c_int]
lib.chat_write_file.restype = c_void_p
lib.chat_read_file.argtypes = [c_char_p, c_char_p, c_char_p]
lib.chat_read_file.restype = POINTER(c_uint8)
lib.chat_encrypt_file.argtypes = [c_void_p, c_char_p, c_char_p]
lib.chat_encrypt_file.restype = c_void_p
lib.chat_decrypt_file.argtypes = [c_char_p, c_char_p, c_char_p, c_char_p]
lib.chat_decrypt_file.restype = c_void_p
def _hs_init(lib: ctypes.CDLL) -> None:
"""Initialize the Haskell runtime exactly once. Mirrors cpp/simplex.cc:13-32."""
if sys.platform == "win32":
argv_strs = [b"simplex", b"+RTS", b"-A64m", b"-H64m", b"--install-signal-handlers=no"]
else:
argv_strs = [
b"simplex",
b"+RTS",
b"-A64m",
b"-H64m",
b"-xn",
b"--install-signal-handlers=no",
]
argc = c_int(len(argv_strs))
arr = (c_char_p * (len(argv_strs) + 1))(*argv_strs, None)
arr_ptr = ctypes.byref(ctypes.cast(arr, POINTER(c_char_p)))
lib.hs_init_with_rtsopts.argtypes = [POINTER(c_int), POINTER(POINTER(c_char_p))]
lib.hs_init_with_rtsopts.restype = None
lib.hs_init_with_rtsopts(ctypes.byref(argc), arr_ptr)
def lib_for(backend: Backend) -> ctypes.CDLL:
"""Resolve, load, and initialize libsimplex for the given backend.
Idempotent for the same backend; raises if called with a different backend.
Concurrent calls serialize on the module-level lock.
"""
global _lib, _libc, _backend
with _lock:
if _lib is not None:
if _backend != backend:
raise RuntimeError(
f"libsimplex already loaded with backend={_backend}; "
f"cannot switch to {backend} in the same process"
)
return _lib
libs_dir = _resolve_libs_dir(backend)
lib = ctypes.CDLL(str(libs_dir / _libname()))
_setup_signatures(lib)
_hs_init(lib)
_libc = _load_libc()
_lib = lib
_backend = backend
return lib
def libc() -> ctypes.CDLL:
"""libc — needed by `core` to free Haskell-allocated result strings."""
if _libc is None:
raise RuntimeError("lib_for() must be called before libc()")
return _libc
def lib() -> ctypes.CDLL:
"""Loaded libsimplex handle. Raises if `lib_for()` has not been called."""
if _lib is None:
raise RuntimeError("lib_for() must be called before lib()")
return _lib
def _optional_export(name: str, feature: str, argtypes: list[Any]) -> Any:
try:
fn = getattr(lib(), name)
except AttributeError as e:
raise RuntimeError(
f"loaded libsimplex does not export {name}; {feature} needs a newer libsimplex"
) from e
fn.argtypes = argtypes
fn.restype = c_void_p
return fn
def migrate_init_queue() -> Any:
"""`chat_migrate_init_queue`, which older libsimplex releases do not export."""
return _optional_export(
"chat_migrate_init_queue",
"queue size",
[c_char_p, c_char_p, c_char_p, c_int, POINTER(c_void_p)],
)
def resize_image() -> Any:
return _optional_export("chat_resize_image", "image resizing", [c_char_p, c_int, c_int, c_int])
def resize_image_uri() -> Any:
return _optional_export("chat_resize_image_uri", "image resizing", [c_char_p, c_int, c_int])