/*-1* Copyright (c) 2007 Kai Wang2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer9* in this position and unchanged.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT19* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*25* $Id: ar.h 3629 2018-09-30 19:26:28Z jkoshy $26*/2728#include <libelf.h>2930#include "_elftc.h"3132/*33* ar(1) options.34*/35#define AR_A 0x0001 /* position-after */36#define AR_B 0x0002 /* position-before */37#define AR_C 0x0004 /* creating new archive */38#define AR_CC 0x0008 /* do not overwrite when extracting */39#define AR_J 0x0010 /* bzip2 compression */40#define AR_O 0x0020 /* preserve original mtime when extracting */41#define AR_S 0x0040 /* write archive symbol table */42#define AR_SS 0x0080 /* do not write archive symbol table */43#define AR_TR 0x0100 /* only keep first 15 chars for member name */44#define AR_U 0x0200 /* only extract or update newer members.*/45#define AR_V 0x0400 /* verbose mode */46#define AR_Z 0x0800 /* gzip compression */47#define AR_D 0x1000 /* insert dummy mode, mtime, uid and gid */48#define AR_BSD 0x2000 /* use the BSD archive format */4950#define DEF_BLKSZ 10240 /* default block size */5152/* Special names. */5354#define AR_STRINGTAB_NAME_SVR4 "//"55#define AR_SYMTAB_NAME_BSD "__.SYMDEF"56#define AR_SYMTAB_NAME_SVR4 "/"5758/*59* Convenient wrapper for general libarchive error handling.60*/61#define AC(CALL) do { \62if ((CALL)) \63bsdar_errc(bsdar, 0, "%s", \64archive_error_string(a)); \65} while (0)6667/*68* The 'ACV' wrapper is used for libarchive APIs that changed from69* returning 'void' to returning an 'int' in later versions of libarchive.70*/71#if ARCHIVE_VERSION_NUMBER >= 200000072#define ACV(CALL) AC(CALL)73#else74#define ACV(CALL) do { \75(CALL); \76} while (0)77#endif7879/*80* In-memory representation of archive member(object).81*/82struct ar_obj {83Elf *elf; /* object file descriptor */84char *name; /* member name */85uid_t uid; /* user id */86gid_t gid; /* group id */87mode_t md; /* octal file permissions */88size_t size; /* member size */89time_t mtime; /* modification time */90dev_t dev; /* inode's device */91ino_t ino; /* inode's number */9293TAILQ_ENTRY(ar_obj) objs;94};9596/*97* Structure encapsulates the "global" data for "ar" program.98*/99struct bsdar {100const char *filename; /* archive name. */101const char *addlib; /* target of ADDLIB. */102const char *posarg; /* position arg for modifiers -a, -b. */103char mode; /* program mode */104int options; /* command line options */105FILE *output; /* default output stream */106107const char *progname; /* program name */108int argc;109char **argv;110111dev_t ar_dev; /* archive device. */112ino_t ar_ino; /* archive inode. */113114/*115* Fields for the archive string table.116*/117char *as; /* buffer for archive string table. */118size_t as_sz; /* current size of as table. */119size_t as_cap; /* capacity of as table buffer. */120121/*122* Fields for the archive symbol table.123*/124uint32_t s_cnt; /* current number of symbols. */125uint32_t *s_so; /* symbol offset table. */126size_t s_so_cap; /* capacity of so table buffer. */127char *s_sn; /* symbol name table */128size_t s_sn_cap; /* capacity of sn table buffer. */129size_t s_sn_sz; /* current size of sn table. */130/* Current member's offset (relative to the end of pseudo members.) */131off_t rela_off;132133TAILQ_HEAD(, ar_obj) v_obj; /* object(member) list */134};135136void ar_mode_script(struct bsdar *ar);137int ar_read_archive(struct bsdar *_ar, int _mode);138int ar_write_archive(struct bsdar *_ar, int _mode);139void bsdar_errc(struct bsdar *, int _code, const char *fmt, ...);140int bsdar_is_pseudomember(struct bsdar *_ar, const char *_name);141const char *bsdar_strmode(mode_t m);142void bsdar_warnc(struct bsdar *, int _code, const char *fmt, ...);143144145