zip_source_file.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. zip_source_file.h -- header for common file operations
  3. Copyright (C) 2020 Dieter Baron and Thomas Klausner
  4. This file is part of libzip, a library to manipulate ZIP archives.
  5. The authors can be contacted at <libzip@nih.at>
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. 1. Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in
  13. the documentation and/or other materials provided with the
  14. distribution.
  15. 3. The names of the authors may not be used to endorse or promote
  16. products derived from this software without specific prior
  17. written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
  19. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  24. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  28. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. struct zip_source_file_stat {
  31. zip_uint64_t size; /* must be valid for regular files */
  32. time_t mtime; /* must always be valid, is initialized to current time */
  33. bool exists; /* must always be vaild */
  34. bool regular_file; /* must always be valid */
  35. };
  36. typedef struct zip_source_file_context zip_source_file_context_t;
  37. typedef struct zip_source_file_operations zip_source_file_operations_t;
  38. typedef struct zip_source_file_stat zip_source_file_stat_t;
  39. struct zip_source_file_context {
  40. zip_error_t error; /* last error information */
  41. zip_int64_t supports;
  42. /* reading */
  43. char *fname; /* name of file to read from */
  44. void *f; /* file to read from */
  45. zip_stat_t st; /* stat information passed in */
  46. zip_file_attributes_t attributes; /* additional file attributes */
  47. zip_error_t stat_error; /* error returned for stat */
  48. zip_uint64_t start; /* start offset of data to read */
  49. zip_uint64_t len; /* length of the file, 0 for up to EOF */
  50. zip_uint64_t offset; /* current offset relative to start (0 is beginning of part we read) */
  51. /* writing */
  52. char *tmpname;
  53. void *fout;
  54. zip_source_file_operations_t *ops;
  55. void *ops_userdata;
  56. };
  57. /* The following methods must be implemented to support each feature:
  58. - close, read, seek, and stat must always be implemented.
  59. - To support specifying the file by name, open, and strdup must be implemented.
  60. - For write support, the file must be specified by name and close, commit_write, create_temp_output, remove, rollback_write, and tell must be implemented.
  61. - create_temp_output_cloning is always optional. */
  62. struct zip_source_file_operations {
  63. void (*close)(zip_source_file_context_t *ctx);
  64. zip_int64_t (*commit_write)(zip_source_file_context_t *ctx);
  65. zip_int64_t (*create_temp_output)(zip_source_file_context_t *ctx);
  66. zip_int64_t (*create_temp_output_cloning)(zip_source_file_context_t *ctx, zip_uint64_t len);
  67. bool (*open)(zip_source_file_context_t *ctx);
  68. zip_int64_t (*read)(zip_source_file_context_t *ctx, void *buf, zip_uint64_t len);
  69. zip_int64_t (*remove)(zip_source_file_context_t *ctx);
  70. void (*rollback_write)(zip_source_file_context_t *ctx);
  71. bool (*seek)(zip_source_file_context_t *ctx, void *f, zip_int64_t offset, int whence);
  72. bool (*stat)(zip_source_file_context_t *ctx, zip_source_file_stat_t *st);
  73. char *(*string_duplicate)(zip_source_file_context_t *ctx, const char *);
  74. zip_int64_t (*tell)(zip_source_file_context_t *ctx, void *f);
  75. zip_int64_t (*write)(zip_source_file_context_t *ctx, const void *data, zip_uint64_t len);
  76. };
  77. zip_source_t *zip_source_file_common_new(const char *fname, void *file, zip_uint64_t start, zip_int64_t len, const zip_stat_t *st, zip_source_file_operations_t *ops, void *ops_userdata, zip_error_t *error);