1/* 2 * Copyright (c) 2005 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@opendarwin.org> 32 */ 33/* 34 * Portions Copyright 2006, Apple Computer, Inc. 35 * Christopher Ryan <ryanc@apple.com> 36 */ 37 38#ifndef _XAR_H_ 39#define _XAR_H_ 40 41#define XAR_VERSION "@XAR_VERSION@" 42 43#include <sys/types.h> 44#include <stdint.h> 45#include <sys/stat.h> 46 47#pragma pack(4) 48 49struct xar_header { 50 uint32_t magic; 51 uint16_t size; 52 uint16_t version; 53 uint64_t toc_length_compressed; 54 uint64_t toc_length_uncompressed; 55 uint32_t cksum_alg; 56}; 57#pragma pack() 58typedef struct xar_header xar_header_t; 59 60#define XAR_HEADER_MAGIC 0x78617221 61#define XAR_EA_FORK "ea" 62 63#define XAR_CKSUM_NONE 0 64#define XAR_CKSUM_SHA1 1 65#define XAR_CKSUM_MD5 2 66#define XAR_CKSUM_SHA256 3 67#define XAR_CKSUM_SHA512 4 68 69typedef void *xar_errctx_t; 70typedef const struct __xar_file_t *xar_file_t; 71typedef const struct __xar_iter_t *xar_iter_t; 72typedef const struct __xar_t *xar_t; 73typedef const struct __xar_subdoc_t *xar_subdoc_t; 74typedef const struct __xar_signature_t *xar_signature_t; 75 76typedef struct { 77 char *next_out; 78 unsigned int avail_out; 79 80 unsigned long long total_in; 81 unsigned long long total_out; 82 83 void *state; 84} xar_stream; 85 86typedef int32_t (*err_handler)(int32_t severit, int32_t instance, xar_errctx_t ctx, void *usrctx); 87/* the signed_data must be allocated durring the callback and will be released by the xar lib after the callback */ 88typedef int32_t (*xar_signer_callback)(xar_signature_t sig, void *context, uint8_t *data, uint32_t length, uint8_t **signed_data, uint32_t *signed_len); 89 90#define READ 0 91#define WRITE 1 92 93/* xar stream return codes */ 94#define XAR_STREAM_OK 0 95#define XAR_STREAM_END 1 96#define XAR_STREAM_ERR -1 97 98/* Valid xar options & values */ 99#define XAR_OPT_OWNERSHIP "ownership" /* setting owner/group behavior */ 100#define XAR_OPT_VAL_SYMBOLIC "symbolic" /* set owner/group based on names */ 101#define XAR_OPT_VAL_NUMERIC "numeric" /* set owner/group based on uid/gid */ 102 103#define XAR_OPT_TOCCKSUM "toc-cksum" /* set the toc checksum algorithm */ 104#define XAR_OPT_FILECKSUM "file-chksum" /* set the file checksum algorithm */ 105#define XAR_OPT_VAL_NONE "none" 106#define XAR_OPT_VAL_SHA1 "sha1" 107#define XAR_OPT_VAL_SHA256 "sha256" 108#define XAR_OPT_VAL_SHA512 "sha512" 109#define XAR_OPT_VAL_MD5 "md5" 110 111#define XAR_OPT_COMPRESSION "compression" /* set the file compression type */ 112#define XAR_OPT_COMPRESSIONARG "compression-arg" /* set the compression opts */ 113#define XAR_OPT_VAL_GZIP "gzip" 114#define XAR_OPT_VAL_BZIP "bzip2" 115#define XAR_OPT_VAL_LZMA "lzma" 116 117#define XAR_OPT_RSIZE "rsize" /* Read io buffer size */ 118 119#define XAR_OPT_COALESCE "coalesce" /* Coalesce identical heap blocks */ 120#define XAR_OPT_LINKSAME "linksame" /* Hardlink identical files */ 121 122#define XAR_OPT_PROPINCLUDE "prop-include" /* File property to include */ 123#define XAR_OPT_PROPEXCLUDE "prop-exclude" /* File property to exclude */ 124 125#define XAR_OPT_SAVESUID "savesuid" /* Preserve setuid/setgid bits */ 126#define XAR_OPT_VAL_TRUE "true" 127#define XAR_OPT_VAL_FALSE "false" 128 129/* xar signing algorithms */ 130#define XAR_SIG_SHA1RSA 1 131 132 133/* xar error handler macros */ 134#define XAR_SEVERITY_DEBUG 1 135#define XAR_SEVERITY_INFO 2 136#define XAR_SEVERITY_NORMAL 3 137#define XAR_SEVERITY_WARNING 4 138#define XAR_SEVERITY_NONFATAL 5 139#define XAR_SEVERITY_FATAL 6 140 141#define XAR_ERR_ARCHIVE_CREATION 1 142#define XAR_ERR_ARCHIVE_EXTRACTION 2 143 144xar_t xar_open(const char *file, int32_t flags); 145int xar_close(xar_t x); 146xar_file_t xar_add(xar_t x, const char *path); 147 148xar_file_t xar_add_frombuffer(xar_t x, xar_file_t parent, const char *name, char *buffer, size_t length); 149xar_file_t xar_add_folder(xar_t x, xar_file_t f, const char *name, struct stat *info); 150xar_file_t xar_add_frompath(xar_t x, xar_file_t parent, const char *name, const char *realpath); 151 152xar_file_t xar_add_from_archive(xar_t x, xar_file_t parent, const char *name, xar_t sourcearchive, xar_file_t sourcefile); 153 154int32_t xar_extract(xar_t x, xar_file_t f); 155int32_t xar_extract_tofile(xar_t x, xar_file_t f, const char *path); 156int32_t xar_extract_tobuffer(xar_t x, xar_file_t f, char **buffer); 157int32_t xar_extract_tobuffersz(xar_t x, xar_file_t f, char **buffer, size_t *size); 158int32_t xar_extract_tostream_init(xar_t x, xar_file_t f, xar_stream *stream); 159int32_t xar_extract_tostream(xar_stream *stream); 160int32_t xar_extract_tostream_end(xar_stream *stream); 161 162int32_t xar_verify(xar_t x, xar_file_t f); 163 164 165const char *xar_opt_get(xar_t x, const char *option); 166int32_t xar_opt_set(xar_t x, const char *option, const char *value); 167int32_t xar_opt_unset(xar_t x, const char *option); 168 169int32_t xar_prop_set(xar_file_t f, const char *key, const char *value); 170int32_t xar_prop_create(xar_file_t f, const char *key, const char *value); 171int32_t xar_prop_get(xar_file_t f, const char *key, const char **value); 172 173xar_iter_t xar_iter_new(void); 174void xar_iter_free(xar_iter_t i); 175 176const char *xar_prop_first(xar_file_t f, xar_iter_t i); 177const char *xar_prop_next(xar_iter_t i); 178 179void xar_prop_unset(xar_file_t f, const char *key); 180xar_file_t xar_file_first(xar_t x, xar_iter_t i); 181xar_file_t xar_file_next(xar_iter_t i); 182 183const char *xar_attr_get(xar_file_t f, const char *prop, const char *key); 184int32_t xar_attr_set(xar_file_t f, const char *prop, const char *key, const char *value); 185const char *xar_attr_first(xar_file_t f, const char *prop, xar_iter_t i); 186const char *xar_attr_next(xar_iter_t i); 187 188xar_subdoc_t xar_subdoc_new(xar_t x, const char *name); 189int32_t xar_subdoc_prop_set(xar_subdoc_t s, const char *key, const char *value); 190int32_t xar_subdoc_prop_get(xar_subdoc_t s, const char *key, const char **value); 191int32_t xar_subdoc_attr_set(xar_subdoc_t s, const char *prop, const char *key, const char *value); 192const char *xar_subdoc_attr_get(xar_subdoc_t s, const char *prop, const char *key); 193xar_subdoc_t xar_subdoc_first(xar_t x); 194xar_subdoc_t xar_subdoc_next(xar_subdoc_t s); 195const char *xar_subdoc_name(xar_subdoc_t s); 196int32_t xar_subdoc_copyout(xar_subdoc_t s, unsigned char **, unsigned int *); 197int32_t xar_subdoc_copyin(xar_subdoc_t s, const unsigned char *, unsigned int); 198void xar_subdoc_remove(xar_subdoc_t s); 199 200/* signature api for adding various signature types */ 201xar_signature_t xar_signature_new(xar_t x,const char *type, int32_t length, xar_signer_callback callback, void *callback_context); 202/* extended signatures are ignored by previous versions of xar */ 203xar_signature_t xar_signature_new_extended(xar_t x,const char *type, int32_t length, xar_signer_callback callback, void *callback_context); 204 205const char *xar_signature_type(xar_signature_t s); 206 207xar_signature_t xar_signature_first(xar_t x); 208xar_signature_t xar_signature_next(xar_signature_t s); 209 210int32_t xar_signature_add_x509certificate(xar_signature_t sig, const uint8_t *cert_data, uint32_t cert_len ); 211 212int32_t xar_signature_get_x509certificate_count(xar_signature_t sig); 213int32_t xar_signature_get_x509certificate_data(xar_signature_t sig, int32_t index, const uint8_t **cert_data, uint32_t *cert_len); 214 215uint8_t xar_signature_copy_signed_data(xar_signature_t sig, uint8_t **data, uint32_t *length, uint8_t **signed_data, uint32_t *signed_length, off_t *signed_offset); 216 217/* Helper functions - caller must free returned memory */ 218char *xar_get_size(xar_t x, xar_file_t f); 219char *xar_get_type(xar_t x, xar_file_t f); 220char *xar_get_mode(xar_t x, xar_file_t f); 221char *xar_get_owner(xar_t x, xar_file_t f); 222char *xar_get_group(xar_t x, xar_file_t f); 223char *xar_get_mtime(xar_t x, xar_file_t f); 224 225/* For helping calling apps harden against hacked archives that attempt to escape their extraction roots. */ 226int xar_path_issane(char* path); 227 228/* These are for xar modules and should never be needed from a calling app */ 229void xar_register_errhandler(xar_t x, err_handler callback, void *usrctx); 230xar_t xar_err_get_archive(xar_errctx_t ctx); 231xar_file_t xar_err_get_file(xar_errctx_t ctx); 232const char *xar_err_get_string(xar_errctx_t ctx); 233int xar_err_get_errno(xar_errctx_t ctx); 234void xar_err_set_file(xar_t x, xar_file_t f); 235void xar_err_set_formatted_string(xar_t x, const char *format, ...); 236void xar_err_set_string(xar_t x, const char *str); 237void xar_err_set_errno(xar_t x, int e); 238void xar_err_new(xar_t x); 239int32_t xar_err_callback(xar_t x, int32_t sev, int32_t err); 240 241void xar_serialize(xar_t x, const char *file); 242char *xar_get_path(xar_file_t f); 243off_t xar_get_heap_offset(xar_t x); 244uint64_t xar_ntoh64(uint64_t num); 245 246#endif /* _XAR_H_ */ 247