ZIP_SOURCE_FUNCTION(3)
π zip_source_function β create data source from function π
okay bestie, so this is from the Library Functions Manual, it's a libzip thing (libzip (-lzip), we love her), and it's all about building your very own zip data source out of a function you write yourself. main character energy fr fr. π¦
β¨ NAME β¨
zip_source_function, zip_source_function_create β create data source from function. that's it, that's the whole vibe.
πΈ LIBRARY πΈ
libzip (-lzip) β link against her, she's iconic.
π SYNOPSIS π
#include <zip.h>
zip_source_t *
zip_source_function(zip_t *archive, zip_source_callback fn,
void *userdata);
zip_source_t *
zip_source_function_create(zip_source_callback fn, void *userdata,
zip_error_t *error);
π DESCRIPTION π
so zip_source_function() and zip_source_function_create() both do the same core thing: they take your function fn and turn it into a zip source. and fn has to be exactly this type, no substitutions bestie:
typedef zip_int64_t (*zip_source_callback)(void *userdata, void *data,
zip_uint64_t len, zip_source_cmd_t cmd)
archive or error are just there so errors have somewhere to go β they can be NULL if you don't care. π
when the library calls your function, the first argument is whatever userdata you handed over. the next two are a buffer data of size len β that shows up when data is being passed in or is expected back β otherwise you get NULL and 0. and the last one, cmd, tells your function which action it's supposed to perform. it's giving "you have one job". β¨
depending on how you're using this, there are three useful sets of commands a zip_source_callback() might need to support:
- read source β you're providing streamed data (like for file data added to archives). must support
ZIP_SOURCE_OPEN,ZIP_SOURCE_READ,ZIP_SOURCE_CLOSE,ZIP_SOURCE_STAT, andZIP_SOURCE_ERROR.- also: if your source uses any allocated memory (including
userdata) it should also implementZIP_SOURCE_FREEso you don't leak memory. no cap, leaks are not the vibe. π
- also: if your source uses any allocated memory (including
- seekable read source β same as the previous one, but from a source that lets you read from arbitrary offsets (also for a read-only zip archive). must additionally support
ZIP_SOURCE_SEEK,ZIP_SOURCE_TELL, andZIP_SOURCE_SUPPORTS. - read/write source β same as previous, but now you can write too (also for writable zip archives). must additionally support
ZIP_SOURCE_BEGIN_WRITE,ZIP_SOURCE_COMMIT_WRITE,ZIP_SOURCE_ROLLBACK_WRITE,ZIP_SOURCE_SEEK_WRITE,ZIP_SOURCE_TELL_WRITE, andZIP_SOURCE_REMOVE.- and on top of all of that, supporting the pseudo-command
ZIP_SOURCE_SUPPORTS_REOPENlets you callzip_source_open()again after callingzip_source_close(). re-opening era. π
- and on top of all of that, supporting the pseudo-command
π ZIP_SOURCE_ACCEPT_EMPTY π
return 1 if an empty source should be accepted as a valid zip archive. this is the default if this command isn't supported by a source. file system backed sources should return 0.
π¦ ZIP_SOURCE_BEGIN_WRITE π¦
get the source ready for writing. use this to create any temporary file(s).
β ZIP_SOURCE_BEGIN_WRITE_CLONING β
get the source ready for writing, but keep the first len bytes of the original file. only implement this command if it's actually more efficient than copying the data, and if it doesn't destructively overwrite the original file (you still have to be able to execute ZIP_SOURCE_ROLLBACK_WRITE, so like... be careful bestie).
the next write should happen at byte offset.
π ZIP_SOURCE_CLOSE π
reading is done. that's it. bye. π
π₯ ZIP_SOURCE_COMMIT_WRITE π₯
finish writing to the source. replace the original data with the newly written data. clean up temporary files or internal buffers. after this, opening and reading from the source should give you the newly written data.
π ZIP_SOURCE_ERROR π
get error info. data points to an array of two ints, which should be filled with the libzip error code and the corresponding system error code for whatever error happened. see zip_errors(3) for the deets on error codes. if the source stores error information in a zip_error_t, use zip_error_to_data(3) and return its return value. otherwise, return 2 * sizeof(int).
π ZIP_SOURCE_FREE π
clean up and free all the resources, including userdata. the callback function will not be called again β this is the finale, periodt.
πΈ ZIP_SOURCE_GET_FILE_ATTRIBUTES πΈ
provide information about various data. put the data in the appropriate entry in the zip_file_attributes_t argument that got passed in, and the appropriate ZIP_FILE_ATTRIBUTES_* value must be or'ed into the valid member to denote that the corresponding data has been provided. you can initialize a zip_file_attributes_t structure with zip_file_attributes_init(3).
- ASCII mode β if a file is a plaintext file in ASCII. can be used by extraction tools to automatically convert line endings (part of the internal file attributes). member
ascii, flagZIP_FILE_ATTRIBUTES_ASCII. - General Purpose Bit Flags (limited to Compression Flags) β the general purpose bit flag in the zip in the local and central directory headers contain information about the compression method. member
general_purpose_bit_flagsandgeneral_purpose_bit_maskto denote which members have been set; flagZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS. - External File Attributes β the external file attributes (usually operating system-specific). member
external_file_attributes, flagZIP_FILE_ATTRIBUTES_EXTERNAL_FILE_ATTRIBUTES. - Version Needed β a minimum version needed required to unpack this entry (in the usual
"major * 10 + minor"format). memberversion_needed, flagZIP_FILE_ATTRIBUTES_VERSION_NEEDED. - Operating System β one of the operating systems as defined by the
ZIP_OPSYS_*variables (seezip.h). this value affects the interpretation of the external file attributes. memberhost_system, flagZIP_FILE_ATTRIBUTES_HOST_SYSTEM.
π«Ά ZIP_SOURCE_OPEN π«Ά
prepare for reading. get comfy. π
π ZIP_SOURCE_READ π
read data into the buffer data of size len. return the number of bytes placed into data on success, and zero for end-of-file.
π ZIP_SOURCE_REMOVE π
remove the underlying file. this gets called if a zip archive is empty when closed.
π§ ZIP_SOURCE_ROLLBACK_WRITE π§
abort writing to the source. discard written data. clean up temporary files or internal buffers. after this, opening and reading from the source should give you back the original data. we love a good undo. β©οΈ
β¨ ZIP_SOURCE_SEEK β¨
specify the position to read the next byte from, like fseek(3). use ZIP_SOURCE_GET_ARGS(3) to decode the arguments into this struct:
struct zip_source_args_seek {
zip_int64_t offset;
int whence;
};
if the size of the source's data is known, use zip_source_seek_compute_offset(3) to validate the arguments and compute the new offset.
π ZIP_SOURCE_SEEK_WRITE π
specify the position to write the next byte to, like fseek(3). see ZIP_SOURCE_SEEK for details.
π ZIP_SOURCE_STAT π
get meta information for the input data. data points to an allocated struct zip_stat, which should be initialized using zip_stat_init(3) and then filled in.
for uncompressed, unencrypted data, all information is optional. but like, fill in as much info as is readily available, bestie, it's free.
if the data is compressed, ZIP_STAT_COMP_METHOD, ZIP_STAT_SIZE, and ZIP_STAT_CRC must be filled in.
if the data is encrypted, ZIP_STAT_ENCRYPTION_METHOD, ZIP_STAT_COMP_METHOD, ZIP_STAT_SIZE, and ZIP_STAT_CRC must be filled in.
information that's only available after the source has been read (e.g., size) can be omitted in an earlier call. NOTE: zip_source_function() may be called with this argument even after being called with ZIP_SOURCE_CLOSE. yeah, she's like that.
return sizeof(struct zip_stat) on success.
π ZIP_SOURCE_SUPPORTS π
return a bitmap specifying which commands are supported. use zip_source_make_command_bitmap(3). if this command isn't implemented, the source is assumed to be a read source without seek support.
π ZIP_SOURCE_TELL π
return the current read offset in the source, like ftell(3).
π¦ ZIP_SOURCE_TELL_WRITE π¦
return the current write offset in the source, like ftell(3).
π₯ ZIP_SOURCE_WRITE π₯
write data to the source. return the number of bytes written.
π ZIP_SOURCE_SUPPORTS_REOPEN π
this command is never actually invoked β support for it just signals the ability to handle multiple open/read/close cycles.
π Return Values π
commands should return -1 on error. ZIP_SOURCE_ERROR will be called to retrieve the error code. on success, commands return 0, unless it says otherwise up in the description above.
πΈ Calling Conventions πΈ
the library will always issue ZIP_SOURCE_OPEN before issuing ZIP_SOURCE_READ, ZIP_SOURCE_SEEK, or ZIP_SOURCE_TELL. when it no longer wants to read from this source, it'll issue ZIP_SOURCE_CLOSE. if the library wants to read the data again, it'll issue ZIP_SOURCE_OPEN a second time. if your function can't provide the data again, it should return -1.
ZIP_SOURCE_BEGIN_WRITE or ZIP_SOURCE_BEGIN_WRITE_CLONING will be called before ZIP_SOURCE_WRITE, ZIP_SOURCE_SEEK_WRITE, or ZIP_SOURCE_TELL_WRITE. when writing is complete, either ZIP_SOURCE_COMMIT_WRITE or ZIP_SOURCE_ROLLBACK_WRITE will be called.
ZIP_SOURCE_ACCEPT_EMPTY, ZIP_SOURCE_GET_FILE_ATTRIBUTES, and ZIP_SOURCE_STAT can be issued at any time.
ZIP_SOURCE_ERROR will only be issued in response to the function returning -1.
ZIP_SOURCE_FREE will be the last command issued; if ZIP_SOURCE_OPEN was called and succeeded, ZIP_SOURCE_CLOSE will be called before ZIP_SOURCE_FREE, and similarly for ZIP_SOURCE_BEGIN_WRITE or ZIP_SOURCE_BEGIN_WRITE_CLONING and ZIP_SOURCE_COMMIT_WRITE or ZIP_SOURCE_ROLLBACK_WRITE. she always cleans up after herself. π
β RETURN VALUES β
upon successful completion, the created source is returned. otherwise, NULL is returned and the error code in archive or error is set to indicate the error (unless it's NULL).
π¨ ERRORS π¨
zip_source_function() fails if:
[ZIP_ER_MEMORY]β required memory could not be allocated. π (this is the only one, bestie)
π SEE ALSO π
libzip(3), zip_file_add(3), zip_file_attributes_init(3), zip_file_replace(3), zip_source(5), zip_stat_init(3)
π HISTORY π
zip_source_function() and zip_source_function_create() were added in libzip 1.0. throwback era.
π«Ά AUTHORS π«Ά
Dieter Baron <dillo@nih.at> and Thomas Klausner <wiz@gatalith.at>. absolute legends, we stan. π
and that's the whole page, babe β brought to you by the Debian crew, this version is from January 5, 2023, and it lives at ZIP_SOURCE_FUNCTION(3) in the Library Functions Manual. go write your callback, you've got this. πβ¨ππ¦
π¦ where this page comes from
- arch rolling: libzip 1.11.4-1 Β· BSD
- fedora 43: libzip-devel 1.11.4-2.fc43 Β· BSD-3-Clause
- fedora 43: libzip-devel 1.11.4-2.fc43 Β· BSD-3-Clause
- fedora 44: libzip-devel 1.11.4-3.fc44 Β· BSD-3-Clause
- fedora 44: libzip-devel 1.11.4-3.fc44 Β· BSD-3-Clause
- nixpkgs unstable: libzip 1.11.4
- ubuntu resolute: libzip-dev 1.11.4-2 Β· BSD-3-Clause
- ubuntu stonking: libzip-dev 1.11.4-2build1 Β· BSD-3-Clause
- Copyright (C) 2004-2022 Dieter Baron and Thomas Klausner
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms