1/*
2 * Copyright (c) 2005-2007 Rob Braun
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Rob Braun nor the names of his contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29/*
30 * 03-Apr-2005
31 * DRI: Rob Braun <bbraun@synack.net>
32 */
33/*
34 * Portions Copyright 2006, Apple Computer, Inc.
35 * Christopher Ryan <ryanc@apple.com>
36 */
37
38
39#ifndef _XAR_ARCHIVE_H_
40#define _XAR_ARCHIVE_H_
41#include <zlib.h>
42#include <libxml/hash.h>
43#ifdef __APPLE__
44#include <CommonCrypto/CommonDigest.h>
45#include <CommonCrypto/CommonDigestSPI.h>
46#else
47#include <openssl/evp.h>
48#endif
49#include <sys/types.h>
50#include <sys/stat.h>
51#include "xar.h"
52#include "filetree.h"
53#include "hash.h"
54
55typedef int (*read_callback)(xar_t, xar_file_t, void *, size_t, void *context);
56typedef int (*write_callback)(xar_t, xar_file_t, void *, size_t, void *context);
57
58struct errctx {
59	const char *str;
60	int         saved_errno;
61	xar_file_t  file;
62	void       *usrctx;
63	xar_t       x;
64};
65
66struct __xar_t {
67	xar_prop_t props;
68	xar_attr_t attrs;      /* archive options, such as rsize */
69	const char *prefix;
70	const char *ns;
71	const char *filler1;
72	const char *filler2;
73	xar_file_t files;       /* file forest */
74	const char *filename;   /* name of the archive we are operating on */
75	char *dirname;          /* directory of the archive, used in creation */
76	int fd;                 /* open file descriptor for the archive */
77	int heap_fd;            /* fd for tmp heap archive, used in creation */
78	off_t heap_offset;      /* current offset within the heap */
79	off_t heap_len;         /* current length of the heap */
80	xar_header_t header;    /* header of the xar archive */
81	void *readbuf;          /* buffer for reading/writing compressed toc */
82	size_t readbuf_len;     /* length of readbuf */
83	size_t offset;          /* offset into readbuf for keeping track
84	                         * between callbacks. */
85	size_t toc_count;       /* current bytes read of the toc */
86	z_stream zs;            /* gz state for compressing/decompressing toc */
87	char *path_prefix;      /* used for distinguishing absolute paths */
88	err_handler ercallback; /* callback for errors/warnings */
89	struct errctx errctx;   /* error callback context */
90	xar_subdoc_t subdocs;   /* linked list of subdocs */
91	xar_signature_t signatures; /* linked list of signatures */
92	int32_t (*attrcopy_to_heap)(xar_t, xar_file_t, xar_prop_t, read_callback, void *);
93	int32_t (*attrcopy_from_heap)(xar_t, xar_file_t, xar_prop_t, write_callback, void *);
94	int32_t (*heap_to_archive)(xar_t);
95	uint64_t last_fileid;       /* unique fileid's in the archive */
96	xmlHashTablePtr ino_hash;   /* Hash for looking up hardlinked files (add)*/
97	xmlHashTablePtr link_hash;  /* Hash for looking up hardlinked files (extract)*/
98	xmlHashTablePtr csum_hash;  /* Hash for looking up checksums of files */
99	xar_hash_t toc_hash_ctx;
100	int skipwarn;
101	struct stat sbcache;
102};
103
104#define XAR(x) ((struct __xar_t *)(x))
105
106#endif /* _XAR_ARCHIVE_H_ */
107