Path: blob/main/contrib/elftoolchain/libpe/pe_flag.c
39478 views
/*-1* Copyright (c) 2016 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 disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <errno.h>2728#include "_libpe.h"2930ELFTC_VCSID("$Id: pe_flag.c 3312 2016-01-10 09:23:51Z kaiwang27 $");3132int33pe_flag(PE *pe, PE_Cmd c, unsigned int flags)34{3536if (pe == NULL || (c != PE_C_SET && c != PE_C_CLR)) {37errno = EINVAL;38return (-1);39}4041if ((flags & ~(PE_F_STRIP_DOS_STUB | PE_F_STRIP_RICH_HEADER |42PE_F_STRIP_SYMTAB | PE_F_STRIP_DEBUG)) != 0) {43errno = EINVAL;44return (-1);45}4647if (c == PE_C_SET)48pe->pe_flags |= flags;49else50pe->pe_flags &= ~flags;5152return (0);53}5455int56pe_flag_dos_header(PE *pe, PE_Cmd c, unsigned int flags)57{5859if (pe == NULL || (c != PE_C_SET && c != PE_C_CLR) ||60(flags & ~PE_F_DIRTY) != 0) {61errno = EINVAL;62return (-1);63}6465if (c == PE_C_SET)66pe->pe_flags |= LIBPE_F_DIRTY_DOS_HEADER;67else68pe->pe_flags &= ~LIBPE_F_DIRTY_DOS_HEADER;6970return (0);71}7273int74pe_flag_coff_header(PE *pe, PE_Cmd c, unsigned int flags)75{7677if (pe == NULL || (c != PE_C_SET && c != PE_C_CLR) ||78(flags & ~PE_F_DIRTY) != 0) {79errno = EINVAL;80return (-1);81}8283if (c == PE_C_SET)84pe->pe_flags |= LIBPE_F_DIRTY_COFF_HEADER;85else86pe->pe_flags &= ~LIBPE_F_DIRTY_COFF_HEADER;8788return (0);89}9091int92pe_flag_opt_header(PE *pe, PE_Cmd c, unsigned int flags)93{9495if (pe == NULL || (c != PE_C_SET && c != PE_C_CLR) ||96(flags & ~PE_F_DIRTY) != 0) {97errno = EINVAL;98return (-1);99}100101if (c == PE_C_SET)102pe->pe_flags |= LIBPE_F_DIRTY_OPT_HEADER;103else104pe->pe_flags &= ~LIBPE_F_DIRTY_OPT_HEADER;105106return (0);107}108109int110pe_flag_data_dir(PE *pe, PE_Cmd c, unsigned int flags)111{112113if (pe == NULL || (c != PE_C_SET && c != PE_C_CLR) ||114(flags & ~PE_F_DIRTY) != 0) {115errno = EINVAL;116return (-1);117}118119if (c == PE_C_SET)120pe->pe_flags |= LIBPE_F_DIRTY_OPT_HEADER;121else122pe->pe_flags &= ~LIBPE_F_DIRTY_OPT_HEADER;123124return (0);125}126127int128pe_flag_scn(PE_Scn *ps, PE_Cmd c, unsigned int flags)129{130131if (ps == NULL || (c != PE_C_SET && c != PE_C_CLR) ||132(flags & ~(PE_F_DIRTY | PE_F_STRIP_SECTION)) == 0) {133errno = EINVAL;134return (-1);135}136137if (c == PE_C_SET)138ps->ps_flags |= flags;139else140ps->ps_flags &= ~flags;141142return (0);143}144145int146pe_flag_section_header(PE_Scn *ps, PE_Cmd c, unsigned int flags)147{148PE *pe;149150if (ps == NULL || (c != PE_C_SET && c != PE_C_CLR) ||151(flags & ~PE_F_DIRTY) != 0) {152errno = EINVAL;153return (-1);154}155156pe = ps->ps_pe;157158/* The library doesn't support per section header dirty flag. */159if (c == PE_C_SET)160pe->pe_flags |= LIBPE_F_DIRTY_SEC_HEADER;161else162pe->pe_flags &= ~LIBPE_F_DIRTY_SEC_HEADER;163164return (0);165}166167int168pe_flag_buffer(PE_Buffer *pb, PE_Cmd c, unsigned int flags)169{170PE_SecBuf *sb;171172if (pb == NULL || (c != PE_C_SET && c != PE_C_CLR) ||173(flags & ~PE_F_DIRTY) != 0) {174errno = EINVAL;175return (-1);176}177178sb = (PE_SecBuf *) pb;179180if (c == PE_C_SET)181sb->sb_flags |= flags;182else183sb->sb_flags &= ~flags;184185return (0);186}187188189