Skip to content

Commit

Permalink
Ops. Forgot this.
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanem committed Dec 1, 2024
1 parent 1e60c8c commit ec2667d
Showing 1 changed file with 156 additions and 3 deletions.
159 changes: 156 additions & 3 deletions src/externals/zip.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define ZIP_H

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>

Expand Down Expand Up @@ -47,7 +48,7 @@ typedef long ssize_t; /* byte count or error */
/**
* @mainpage
*
* Documenation for @ref zip.
* Documentation for @ref zip.
*/

/**
Expand Down Expand Up @@ -92,11 +93,16 @@ typedef long ssize_t; /* byte count or error */
#define ZIP_EFSEEK -27 // fseek error
#define ZIP_EFREAD -28 // fread error
#define ZIP_EFWRITE -29 // fwrite error
#define ZIP_ERINIT -30 // cannot initialize reader
#define ZIP_EWINIT -31 // cannot initialize writer
#define ZIP_EWRINIT -32 // cannot initialize writer from reader
#define ZIP_EINVAL -33 // invalid argument
#define ZIP_ENORITER -34 // cannot initialize reader iterator

/**
* Looks up the error message string coresponding to an error number.
* Looks up the error message string corresponding to an error number.
* @param errnum error number
* @return error message string coresponding to errnum or NULL if error is not
* @return error message string corresponding to errnum or NULL if error is not
* found.
*/
extern ZIP_EXPORT const char *zip_strerror(int errnum);
Expand Down Expand Up @@ -124,6 +130,23 @@ struct zip_t;
extern ZIP_EXPORT struct zip_t *zip_open(const char *zipname, int level,
char mode);

/**
* Opens zip archive with compression level using the given mode.
* The function additionally returns @param errnum -
*
* @param zipname zip archive file name.
* @param level compression level (0-9 are the standard zlib-style levels).
* @param mode file access mode.
* - 'r': opens a file for reading/extracting (the file must exists).
* - 'w': creates an empty file for writing.
* - 'a': appends to an existing archive.
* @param errnum 0 on success, negative number (< 0) on error.
*
* @return the zip archive handler or NULL on error
*/
extern ZIP_EXPORT struct zip_t *
zip_openwitherror(const char *zipname, int level, char mode, int *errnum);

/**
* Closes the zip archive, releases resources - always finalize.
*
Expand All @@ -141,6 +164,16 @@ extern ZIP_EXPORT void zip_close(struct zip_t *zip);
*/
extern ZIP_EXPORT int zip_is64(struct zip_t *zip);

/**
* Returns the offset in the stream where the zip header is located.
*
* @param zip zip archive handler.
* @param offset zip header offset.
*
* @return the return code - 0 if successful, negative number (< 0) on error.
*/
extern ZIP_EXPORT int zip_offset(struct zip_t *zip, uint64_t *offset);

/**
* Opens an entry by name in the zip archive.
*
Expand Down Expand Up @@ -263,6 +296,25 @@ extern ZIP_EXPORT unsigned long long zip_entry_comp_size(struct zip_t *zip);
*/
extern ZIP_EXPORT unsigned int zip_entry_crc32(struct zip_t *zip);

/**
* Returns byte offset of the current zip entry
* in the archive's central directory.
*
* @param zip zip archive handler.
*
* @return the offset in bytes.
*/
extern ZIP_EXPORT unsigned long long zip_entry_dir_offset(struct zip_t *zip);

/**
* Returns the current zip entry's local header file offset in bytes.
*
* @param zip zip archive handler.
*
* @return the entry's local header file offset in bytes.
*/
extern ZIP_EXPORT unsigned long long zip_entry_header_offset(struct zip_t *zip);

/**
* Compresses an input buffer for the current zip entry.
*
Expand Down Expand Up @@ -323,6 +375,27 @@ extern ZIP_EXPORT ssize_t zip_entry_read(struct zip_t *zip, void **buf,
extern ZIP_EXPORT ssize_t zip_entry_noallocread(struct zip_t *zip, void *buf,
size_t bufsize);

/**
* Extracts the part of the current zip entry into a memory buffer using no
* memory allocation for the buffer.
*
* @param zip zip archive handler.
* @param offset the offset of the entry (in bytes).
* @param size requested number of bytes (in bytes).
* @param buf preallocated output buffer.
*
* @note the iterator api uses an allocation to create its state
* @note each call will iterate from the start of the entry
*
* @return the return code - the number of bytes actually read on success.
* Otherwise a negative number (< 0) on error (e.g. offset is too
* large).
*/
extern ZIP_EXPORT ssize_t zip_entry_noallocreadwithoffset(struct zip_t *zip,
size_t offset,
size_t size,
void *buf);

/**
* Extracts the current zip entry into output file.
*
Expand Down Expand Up @@ -370,6 +443,18 @@ extern ZIP_EXPORT ssize_t zip_entries_total(struct zip_t *zip);
extern ZIP_EXPORT ssize_t zip_entries_delete(struct zip_t *zip,
char *const entries[], size_t len);

/**
* Deletes zip archive entries.
*
* @param zip zip archive handler.
* @param entries array of zip archive entries indices to be deleted.
* @param len the number of entries to be deleted.
* @return the number of deleted entries, or negative number (< 0) on error.
*/
extern ZIP_EXPORT ssize_t zip_entries_deletebyindex(struct zip_t *zip,
size_t entries[],
size_t len);

/**
* Extracts a zip archive stream into directory.
*
Expand Down Expand Up @@ -397,12 +482,37 @@ zip_stream_extract(const char *stream, size_t size, const char *dir,
*
* @param stream zip archive stream.
* @param size stream size.
* @param level compression level (0-9 are the standard zlib-style levels).
* @param mode file access mode.
* - 'r': opens a file for reading/extracting (the file must exists).
* - 'w': creates an empty file for writing.
* - 'a': appends to an existing archive.
*
* @return the zip archive handler or NULL on error
*/
extern ZIP_EXPORT struct zip_t *zip_stream_open(const char *stream, size_t size,
int level, char mode);

/**
* Opens zip archive stream into memory.
* The function additionally returns @param errnum -
*
* @param stream zip archive stream.
* @param size stream size.*
* @param level compression level (0-9 are the standard zlib-style levels).
* @param mode file access mode.
* - 'r': opens a file for reading/extracting (the file must exists).
* - 'w': creates an empty file for writing.
* - 'a': appends to an existing archive.
* @param errnum 0 on success, negative number (< 0) on error.
*
* @return the zip archive handler or NULL on error
*/
extern ZIP_EXPORT struct zip_t *zip_stream_openwitherror(const char *stream,
size_t size, int level,
char mode,
int *errnum);

/**
* Copy zip archive stream output buffer.
*
Expand All @@ -424,6 +534,49 @@ extern ZIP_EXPORT ssize_t zip_stream_copy(struct zip_t *zip, void **buf,
*/
extern ZIP_EXPORT void zip_stream_close(struct zip_t *zip);

/**
* Opens zip archive from existing FILE stream with compression level using the
* given mode. The stream will not be closed when calling zip_close.
*
* @param stream C FILE stream.
* @param level compression level (0-9 are the standard zlib-style levels).
* @param mode file access mode. This mode should be equivalent to the mode
* provided when opening the file.
* - 'r': opens a file for reading/extracting (the file must exists).
* - 'w': creates an empty file for writing.
* - 'a': appends to an existing archive.
*
* @return the zip archive handler or NULL on error
*/
extern ZIP_EXPORT struct zip_t *zip_cstream_open(FILE *stream, int level,
char mode);

/**
* Opens zip archive from existing FILE stream with compression level using the
* given mode. The function additionally returns @param errnum - The stream will
* not be closed when calling zip_close.
*
* @param stream C FILE stream.
* @param level compression level (0-9 are the standard zlib-style levels).
* @param mode file access mode.
* - 'r': opens a file for reading/extracting (the file must exists).
* - 'w': creates an empty file for writing.
* - 'a': appends to an existing archive.
* @param errnum 0 on success, negative number (< 0) on error.
*
* @return the zip archive handler or NULL on error
*/
extern ZIP_EXPORT struct zip_t *
zip_cstream_openwitherror(FILE *stream, int level, char mode, int *errnum);

/**
* Closes the zip archive, releases resources - always finalize.
* This function is an alias for zip_close function.
*
* @param zip zip archive handler.
*/
extern ZIP_EXPORT void zip_cstream_close(struct zip_t *zip);

/**
* Creates a new archive and puts files into a single zip archive.
*
Expand Down

0 comments on commit ec2667d

Please sign in to comment.