• src/sftp/sftp.h sftp_str.c

    From Deucе@1:103/705 to Git commit to main/sbbs/master on Sat Apr 25 04:38:43 2026
    https://gitlab.synchro.net/main/sbbs/-/commit/4e2a032e2c395d54b2f8bccc
    Modified Files:
    src/sftp/sftp.h sftp_str.c
    Log Message:
    sftp: support static and borrowed sftp_str_t variants

    Generalize sftp_string to three lifetime modes selected at construction:

    - heap: library single-allocates header+bytes; a release callback
    on the struct frees the whole thing in free_sftp_str() (existing
    behavior, preserved).
    - static: caller provides both struct and bytes with program/scope
    lifetime; release is NULL and free_sftp_str() is a no-op.
    - borrowed: caller provides the struct, owns the bytes externally,
    and supplies a release callback (refcount, mmap teardown, etc.)
    that fires when the library is done with the data. NULL release
    is supported and functionally equivalent to the static form.

    To make this work, c_str moves from a flexible array member to a
    plain uint8_t pointer. Consumers that read ->c_str and ->len keep
    working unchanged; the only internal site that depended on the FAM
    layout (sftp_alloc_str's offsetof) now uses sizeof(struct) and
    points c_str just past the header.

    New helpers:

    void sftp_strstatic(struct sftp_string *out, const char *str);
    void sftp_memstatic(struct sftp_string *out, const uint8_t *buf,
    uint32_t len);
    void sftp_strborrow(struct sftp_string *out, const char *str,
    void (*release)(struct sftp_string *), void *cbdata);
    void sftp_memborrow(struct sftp_string *out, const uint8_t *buf,
    uint32_t len,
    void (*release)(struct sftp_string *), void *cbdata);

    Lets consumers wrap their own buffers in an sftp_str_t without the strdup/memdup round-trip.

    Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
    --- SBBSecho 3.37-Linux
    * Origin: Vertrauen - [vert/cvs/bbs].synchro.net (1:103/705)