blob: 95788485bc85d8853f918d975b732ffc729630bf [file] [log] [blame]
Thomas Klausner7edc9e22004-11-18 16:48:08 +00001.\" zip_source_function.mdoc -- create data source from function
Thomas Klausner86a346b2014-08-02 12:10:27 +02002.\" Copyright (C) 2004-2014 Dieter Baron and Thomas Klausner
Thomas Klausner7edc9e22004-11-18 16:48:08 +00003.\"
4.\" This file is part of libzip, a library to manipulate ZIP archives.
Dieter Baronb86c4332007-11-07 14:35:13 +01005.\" The authors can be contacted at <libzip@nih.at>
Thomas Klausner7edc9e22004-11-18 16:48:08 +00006.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\" notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\" notice, this list of conditions and the following disclaimer in
14.\" the documentation and/or other materials provided with the
15.\" distribution.
16.\" 3. The names of the authors may not be used to endorse or promote
17.\" products derived from this software without specific prior
18.\" written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
21.\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
24.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26.\" GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28.\" IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30.\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31.\"
Thomas Klausnerf87bb852014-10-08 12:36:14 +020032.Dd October 9, 2014
Thomas Klausner7edc9e22004-11-18 16:48:08 +000033.Dt ZIP_SOURCE_FUNCTION 3
34.Os
35.Sh NAME
36.Nm zip_source_function
37.Nd create data source from function
38.Sh LIBRARY
39libzip (-lzip)
40.Sh SYNOPSIS
41.In zip.h
Dieter Baron1d9dfeb2014-09-28 23:02:54 +020042.Ft zip_source_t *
43.Fn zip_source_function "zip_t *archive" "zip_source_callback fn" "void *userdata"
Thomas Klausner7edc9e22004-11-18 16:48:08 +000044.Sh DESCRIPTION
45The function
46.Fn zip_source_function
Dieter Barond0aff312004-12-10 06:13:16 +000047creates a zip source from the user-provided function
48.Ar fn ,
49which must be of the following type:
Thomas Klausner7edc9e22004-11-18 16:48:08 +000050.Bd -literal
Dieter Baroneac5ee92009-03-30 16:35:28 +020051typedef zip_int64_t (*zip_source_callback)(void *state,
Dieter Baron1d9dfeb2014-09-28 23:02:54 +020052 void *data, zip_uint64_t len, zip_source_cmd_t cmd);
Thomas Klausner7edc9e22004-11-18 16:48:08 +000053.Ed
54.Pp
Thomas Klausnerea8ba492014-09-23 16:54:47 +020055.Ar archive
56is used for reporting errors and can be
57.Dv NULL .
58.Pp
Dieter Barond0aff312004-12-10 06:13:16 +000059When called by the library, the first argument is the
Thomas Klausner119086f2006-03-25 11:23:36 +000060.Ar userdata
Dieter Barond0aff312004-12-10 06:13:16 +000061argument supplied to
62.Fn zip_source_function .
63The next two arguments are a buffer
Thomas Klausner7edc9e22004-11-18 16:48:08 +000064.Ar data
65of size
66.Ar len
Dieter Barond0aff312004-12-10 06:13:16 +000067when data is expected to be returned, or else
68.Dv NULL
69and 0.
70The last argument,
71.Ar cmd ,
Thomas Klausnerf87bb852014-10-08 12:36:14 +020072specifies which action the function should perform.
73.Pp
74Depending on the uses, there are three useful sets of commands to be supported by a
75.Fn zip_source_callback :
76.Bl -tag -width seekable-read-sourceXX
77.It read source
78Providing streamed data (for file data added to archives).
79Must support
80.Dv ZIP_SOURCE_OPEN ,
81.Dv ZIP_SOURCE_READ ,
82.Dv ZIP_SOURCE_CLOSE ,
83.Dv ZIP_SOURCE_STAT ,
84and
85.Dv ZIP_SOURCE_ERROR .
86.It seekable read source
87Same as previous, but from a source allowing reading from arbitrary
88offsets (also for read-only zip archive).
89Must additionally support
90.Dv ZIP_SOURCE_SEEK ,
91.Dv ZIP_SOURCE_TELL ,
92and
93.Dv ZIP_SOURCE_SUPPORTS .
94.It read/write source
95Same as previous, but additionally allowing writing (also for writable
96zip archives).
97Must additionally support
98.Dv ZIP_SOURCE_BEGIN_WRITE ,
99.Dv ZIP_SOURCE_COMMIT_WRITE ,
100.Dv ZIP_SOURCE_ROLLBACK_WRITE ,
101.Dv ZIP_SOURCE_SEEK_WRITE ,
102.Dv ZIP_SOURCE_TELL_WRITE ,
103and
104.Dv ZIP_SOURCE_REMOVE .
105.El
106.Ss Dv ZIP_SOURCE_BEGIN_WRITE
107Prepare the source for writing.
108Use this to create any temporary file(s).
109.Ss Dv ZIP_SOURCE_CLOSE
110Reading is done.
111.Ss Dv ZIP_SOURCE_COMMIT_WRITE
112Finish writing to the source.
113Replace the original data with the newly written data.
114Clean up temporary files or internal buffers.
115Subsequently opening and reading from the source should return the
116newly written data.
117.Ss Dv ZIP_SOURCE_ERROR
118Get error information.
119.Ar data
120points to an array of two ints, which should be filled with the libzip
121error code and the corresponding system error code for the error that
122occurred.
123See
124.Xr zip_errors 3
125for details on the error codes.
126If the source stores error information in a zip_error_t, use
127.Xr zip_error_to_data 3
128and return its return value.
129Otherwise, return 2 * sizeof(int).
130.Ss Dv ZIP_SOURCE_FREE
131Clean up and free all resources.
132.Ss Dv ZIP_SOURCE_OPEN
Thomas Klausner8d19d682005-06-09 18:54:24 +0000133Prepare for reading.
Thomas Klausnerf87bb852014-10-08 12:36:14 +0200134.Ss Dv ZIP_SOURCE_READ
Thomas Klausner7edc9e22004-11-18 16:48:08 +0000135Read data into the buffer
136.Ar data
137of size
Dieter Barond0aff312004-12-10 06:13:16 +0000138.Ar len .
139Return the number of bytes placed into
140.Ar data
Thomas Klausnerf87bb852014-10-08 12:36:14 +0200141on success.
142.Ss Dv ZIP_SOURCE_REMOVE
143Remove the underlying file.
144This is called if a zip archive is empty when closed.
145.Ss Dv ZIP_SOURCE_ROLLBACK_WRITE
146Abort writing to the source.
147Discard written data.
148Clean up temporary files or internal buffers.
149Subsequently opening and reading from the source should return the
150original data.
151.Ss Dv ZIP_SOURCE_SEEK
152Specify position to read next byte from, like
153.Xr fseek 3 .
154Use
Thomas Klausner59cb39c2014-10-27 16:52:05 +0100155.Xr ZIP_SOURCE_GET_ARGS 3
Thomas Klausnerf87bb852014-10-08 12:36:14 +0200156to decode the arguments into the following struct:
157.Bd -literal
158struct zip_source_args_seek {
159 zip_int64_t offset;
160 int whence;
161};
162.Ed
163.Ss Dv ZIP_SOURCE_SEEK_WRITE
164Specify position to write next byte to, like
165.Xr fseek 3 .
166See
167.Dv ZIP_SOURCE_SEEK
168for details.
169.Ss Dv ZIP_SOURCE_STAT
Dieter Baron960c89d2004-12-22 15:35:49 +0000170Get meta information for the input data.
Dieter Barond0aff312004-12-10 06:13:16 +0000171.Ar data
Thomas Klausner7cba6032013-09-22 10:49:07 +0200172points to an allocated
Thomas Klausner1bf5b1a2009-03-30 16:44:34 +0200173.Vt struct zip_stat ,
Thomas Klausner7cba6032013-09-22 10:49:07 +0200174which should be initialized using
175.Xr zip_stat_init 3
176and then filled in.
Thomas Klausner1bf5b1a2009-03-30 16:44:34 +0200177Information only available after the source has been read (e.g. size)
Dieter Baroneac5ee92009-03-30 16:35:28 +0200178can be omitted in an earlier call.
Thomas Klausnerf87bb852014-10-08 12:36:14 +0200179Return sizeof(struct zip_stat) on success.
Thomas Klausnere8dd01d2011-03-22 16:09:15 +0100180.Em NOTE :
181.Fn zip_source_function
182may be called with this argument even after being called with
183.Dv ZIP_SOURCE_CLOSE .
Thomas Klausnerf87bb852014-10-08 12:36:14 +0200184.Ss Dv ZIP_SOURCE_SUPPORTS
185Return bitmap specifying which commands are supported.
186Use
187.Xr zip_source_make_command_bitmap 3 .
188If this command is not implemented, the source is assumed to be a
189read source without seek support.
190.Ss Dv ZIP_SOURCE_TELL
191Return the current read offset in the source, like
192.Xr ftell 3 .
193.Ss Dv ZIP_SOURCE_TELL_WRITE
194Return the current write offset in the source, like
195.Xr ftell 3 .
196.Ss Dv ZIP_SOURCE_WRITE
197Write data to the source.
198Return number of bytes written.
199.Ss Return Values
200Commands should return \-1 on error.
201.Dv ZIP_SOURCE_ERROR
202will be called to retrieve the error code.
203On success, commands return 0, unless specified otherwise in the
204description above.
205.Ss Calling Conventions
Dieter Barond0aff312004-12-10 06:13:16 +0000206The library will always issue
Thomas Klausner10687232004-12-22 16:19:54 +0000207.Dv ZIP_SOURCE_OPEN
Dieter Barond0aff312004-12-10 06:13:16 +0000208before issuing
Thomas Klausner10687232004-12-22 16:19:54 +0000209.Dv ZIP_SOURCE_READ .
Dieter Barond0aff312004-12-10 06:13:16 +0000210When it no longer wishes to read from this source, it will issue
Thomas Klausner10687232004-12-22 16:19:54 +0000211.Dv ZIP_SOURCE_CLOSE .
Dieter Barond0aff312004-12-10 06:13:16 +0000212If the library wishes to read the data again, it will issue
Thomas Klausner10687232004-12-22 16:19:54 +0000213.Dv ZIP_SOURCE_OPEN
Thomas Klausner8d19d682005-06-09 18:54:24 +0000214a second time.
215If the function is unable to provide the data again, it should
216return \-1.
Dieter Barond0aff312004-12-10 06:13:16 +0000217.Pp
Thomas Klausner10687232004-12-22 16:19:54 +0000218.Dv ZIP_SOURCE_STAT
Dieter Barond0aff312004-12-10 06:13:16 +0000219can be issued at any time.
Thomas Klausner10687232004-12-22 16:19:54 +0000220.Dv ZIP_SOURCE_ERROR
Dieter Barond0aff312004-12-10 06:13:16 +0000221will only be issued in response to the function
222returning \-1.
Thomas Klausner10687232004-12-22 16:19:54 +0000223.Dv ZIP_SOURCE_FREE
Dieter Baroneac5ee92009-03-30 16:35:28 +0200224will be the last command issued;
225if
226.Dv ZIP_SOURCE_OPEN
227was called and succeeded,
Thomas Klausner1bf5b1a2009-03-30 16:44:34 +0200228.Dv ZIP_SOURCE_CLOSE
229will be called before
Dieter Baroneac5ee92009-03-30 16:35:28 +0200230.Dv ZIP_SOURCE_FREE .
Thomas Klausner7edc9e22004-11-18 16:48:08 +0000231.Sh RETURN VALUES
232Upon successful completion, the created source is returned.
233Otherwise,
234.Dv NULL
235is returned and the error code in
236.Ar archive
Thomas Klausnerea8ba492014-09-23 16:54:47 +0200237is set to indicate the error (unless
238.Ar archive
239is
240.Dv NULL ) .
Thomas Klausner7edc9e22004-11-18 16:48:08 +0000241.Sh ERRORS
242.Fn zip_source_function
243fails if:
244.Bl -tag -width Er
245.It Bq Er ZIP_ER_MEMORY
246Required memory could not be allocated.
247.El
248.Sh SEE ALSO
Thomas Klausnerbec1cfe2005-06-09 18:50:27 +0000249.Xr libzip 3 ,
Thomas Klausner7edc9e22004-11-18 16:48:08 +0000250.Xr zip_add 3 ,
251.Xr zip_replace 3 ,
Thomas Klausner86a346b2014-08-02 12:10:27 +0200252.Xr zip_source 3 ,
Thomas Klausnerb443aa72006-12-16 10:13:13 +0000253.Xr zip_stat_init 3
Thomas Klausnera456cf52005-06-09 21:13:13 +0000254.Sh AUTHORS
255.An -nosplit
Thomas Klausner0acbab42013-07-28 23:29:08 +0200256.An Dieter Baron Aq Mt dillo@nih.at
Thomas Klausner39b5f9e2005-06-09 21:14:54 +0000257and
Thomas Klausner0acbab42013-07-28 23:29:08 +0200258.An Thomas Klausner Aq Mt tk@giga.or.at