13fe401a5SEd Maste /*- 23fe401a5SEd Maste * Copyright (c) 2007 Kai Wang 33fe401a5SEd Maste * All rights reserved. 43fe401a5SEd Maste * 53fe401a5SEd Maste * Redistribution and use in source and binary forms, with or without 63fe401a5SEd Maste * modification, are permitted provided that the following conditions 73fe401a5SEd Maste * are met: 83fe401a5SEd Maste * 1. Redistributions of source code must retain the above copyright 93fe401a5SEd Maste * notice, this list of conditions and the following disclaimer 103fe401a5SEd Maste * in this position and unchanged. 113fe401a5SEd Maste * 2. Redistributions in binary form must reproduce the above copyright 123fe401a5SEd Maste * notice, this list of conditions and the following disclaimer in the 133fe401a5SEd Maste * documentation and/or other materials provided with the distribution. 143fe401a5SEd Maste * 153fe401a5SEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 163fe401a5SEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 173fe401a5SEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 183fe401a5SEd Maste * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 193fe401a5SEd Maste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 203fe401a5SEd Maste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 213fe401a5SEd Maste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 223fe401a5SEd Maste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 233fe401a5SEd Maste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 243fe401a5SEd Maste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 253fe401a5SEd Maste * 26*ae500c1fSEd Maste * $Id: ar.h 3629 2018-09-30 19:26:28Z jkoshy $ 273fe401a5SEd Maste */ 283fe401a5SEd Maste 293fe401a5SEd Maste #include <libelf.h> 303fe401a5SEd Maste 313fe401a5SEd Maste #include "_elftc.h" 323fe401a5SEd Maste 333fe401a5SEd Maste /* 343fe401a5SEd Maste * ar(1) options. 353fe401a5SEd Maste */ 363fe401a5SEd Maste #define AR_A 0x0001 /* position-after */ 373fe401a5SEd Maste #define AR_B 0x0002 /* position-before */ 383fe401a5SEd Maste #define AR_C 0x0004 /* creating new archive */ 393fe401a5SEd Maste #define AR_CC 0x0008 /* do not overwrite when extracting */ 403fe401a5SEd Maste #define AR_J 0x0010 /* bzip2 compression */ 413fe401a5SEd Maste #define AR_O 0x0020 /* preserve original mtime when extracting */ 423fe401a5SEd Maste #define AR_S 0x0040 /* write archive symbol table */ 433fe401a5SEd Maste #define AR_SS 0x0080 /* do not write archive symbol table */ 443fe401a5SEd Maste #define AR_TR 0x0100 /* only keep first 15 chars for member name */ 453fe401a5SEd Maste #define AR_U 0x0200 /* only extract or update newer members.*/ 463fe401a5SEd Maste #define AR_V 0x0400 /* verbose mode */ 473fe401a5SEd Maste #define AR_Z 0x0800 /* gzip compression */ 483fe401a5SEd Maste #define AR_D 0x1000 /* insert dummy mode, mtime, uid and gid */ 493fe401a5SEd Maste #define AR_BSD 0x2000 /* use the BSD archive format */ 503fe401a5SEd Maste 513fe401a5SEd Maste #define DEF_BLKSZ 10240 /* default block size */ 523fe401a5SEd Maste 533fe401a5SEd Maste /* Special names. */ 543fe401a5SEd Maste 553fe401a5SEd Maste #define AR_STRINGTAB_NAME_SVR4 "//" 563fe401a5SEd Maste #define AR_SYMTAB_NAME_BSD "__.SYMDEF" 573fe401a5SEd Maste #define AR_SYMTAB_NAME_SVR4 "/" 583fe401a5SEd Maste 593fe401a5SEd Maste /* 603fe401a5SEd Maste * Convenient wrapper for general libarchive error handling. 613fe401a5SEd Maste */ 623fe401a5SEd Maste #define AC(CALL) do { \ 633fe401a5SEd Maste if ((CALL)) \ 643fe401a5SEd Maste bsdar_errc(bsdar, 0, "%s", \ 653fe401a5SEd Maste archive_error_string(a)); \ 663fe401a5SEd Maste } while (0) 673fe401a5SEd Maste 683fe401a5SEd Maste /* 693fe401a5SEd Maste * The 'ACV' wrapper is used for libarchive APIs that changed from 703fe401a5SEd Maste * returning 'void' to returning an 'int' in later versions of libarchive. 713fe401a5SEd Maste */ 723fe401a5SEd Maste #if ARCHIVE_VERSION_NUMBER >= 2000000 733fe401a5SEd Maste #define ACV(CALL) AC(CALL) 743fe401a5SEd Maste #else 753fe401a5SEd Maste #define ACV(CALL) do { \ 763fe401a5SEd Maste (CALL); \ 773fe401a5SEd Maste } while (0) 783fe401a5SEd Maste #endif 793fe401a5SEd Maste 803fe401a5SEd Maste /* 813fe401a5SEd Maste * In-memory representation of archive member(object). 823fe401a5SEd Maste */ 833fe401a5SEd Maste struct ar_obj { 843fe401a5SEd Maste Elf *elf; /* object file descriptor */ 853fe401a5SEd Maste char *name; /* member name */ 863fe401a5SEd Maste uid_t uid; /* user id */ 873fe401a5SEd Maste gid_t gid; /* group id */ 883fe401a5SEd Maste mode_t md; /* octal file permissions */ 893fe401a5SEd Maste size_t size; /* member size */ 903fe401a5SEd Maste time_t mtime; /* modification time */ 913fe401a5SEd Maste dev_t dev; /* inode's device */ 923fe401a5SEd Maste ino_t ino; /* inode's number */ 933fe401a5SEd Maste 943fe401a5SEd Maste TAILQ_ENTRY(ar_obj) objs; 953fe401a5SEd Maste }; 963fe401a5SEd Maste 973fe401a5SEd Maste /* 983fe401a5SEd Maste * Structure encapsulates the "global" data for "ar" program. 993fe401a5SEd Maste */ 1003fe401a5SEd Maste struct bsdar { 1013fe401a5SEd Maste const char *filename; /* archive name. */ 1023fe401a5SEd Maste const char *addlib; /* target of ADDLIB. */ 1033fe401a5SEd Maste const char *posarg; /* position arg for modifiers -a, -b. */ 1043fe401a5SEd Maste char mode; /* program mode */ 1053fe401a5SEd Maste int options; /* command line options */ 1063fe401a5SEd Maste FILE *output; /* default output stream */ 1073fe401a5SEd Maste 1083fe401a5SEd Maste const char *progname; /* program name */ 1093fe401a5SEd Maste int argc; 1103fe401a5SEd Maste char **argv; 1113fe401a5SEd Maste 1123fe401a5SEd Maste dev_t ar_dev; /* archive device. */ 1133fe401a5SEd Maste ino_t ar_ino; /* archive inode. */ 1143fe401a5SEd Maste 1153fe401a5SEd Maste /* 1163fe401a5SEd Maste * Fields for the archive string table. 1173fe401a5SEd Maste */ 1183fe401a5SEd Maste char *as; /* buffer for archive string table. */ 1193fe401a5SEd Maste size_t as_sz; /* current size of as table. */ 1203fe401a5SEd Maste size_t as_cap; /* capacity of as table buffer. */ 1213fe401a5SEd Maste 1223fe401a5SEd Maste /* 1233fe401a5SEd Maste * Fields for the archive symbol table. 1243fe401a5SEd Maste */ 1253fe401a5SEd Maste uint32_t s_cnt; /* current number of symbols. */ 1263fe401a5SEd Maste uint32_t *s_so; /* symbol offset table. */ 1273fe401a5SEd Maste size_t s_so_cap; /* capacity of so table buffer. */ 1283fe401a5SEd Maste char *s_sn; /* symbol name table */ 1293fe401a5SEd Maste size_t s_sn_cap; /* capacity of sn table buffer. */ 1303fe401a5SEd Maste size_t s_sn_sz; /* current size of sn table. */ 1313fe401a5SEd Maste /* Current member's offset (relative to the end of pseudo members.) */ 1323fe401a5SEd Maste off_t rela_off; 1333fe401a5SEd Maste 1343fe401a5SEd Maste TAILQ_HEAD(, ar_obj) v_obj; /* object(member) list */ 1353fe401a5SEd Maste }; 1363fe401a5SEd Maste 1373fe401a5SEd Maste void ar_mode_script(struct bsdar *ar); 138*ae500c1fSEd Maste int ar_read_archive(struct bsdar *_ar, int _mode); 139*ae500c1fSEd Maste int ar_write_archive(struct bsdar *_ar, int _mode); 1403fe401a5SEd Maste void bsdar_errc(struct bsdar *, int _code, const char *fmt, ...); 1413fe401a5SEd Maste int bsdar_is_pseudomember(struct bsdar *_ar, const char *_name); 1423fe401a5SEd Maste const char *bsdar_strmode(mode_t m); 1433fe401a5SEd Maste void bsdar_warnc(struct bsdar *, int _code, const char *fmt, ...); 144