1*839529caSEd Maste /*-
2*839529caSEd Maste  * Copyright (c) 2016 Kai Wang
3*839529caSEd Maste  * All rights reserved.
4*839529caSEd Maste  *
5*839529caSEd Maste  * Redistribution and use in source and binary forms, with or without
6*839529caSEd Maste  * modification, are permitted provided that the following conditions
7*839529caSEd Maste  * are met:
8*839529caSEd Maste  * 1. Redistributions of source code must retain the above copyright
9*839529caSEd Maste  *    notice, this list of conditions and the following disclaimer.
10*839529caSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
11*839529caSEd Maste  *    notice, this list of conditions and the following disclaimer in the
12*839529caSEd Maste  *    documentation and/or other materials provided with the distribution.
13*839529caSEd Maste  *
14*839529caSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*839529caSEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*839529caSEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*839529caSEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*839529caSEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*839529caSEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*839529caSEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*839529caSEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*839529caSEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*839529caSEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*839529caSEd Maste  * SUCH DAMAGE.
25*839529caSEd Maste  */
26*839529caSEd Maste 
27*839529caSEd Maste #include <errno.h>
28*839529caSEd Maste 
29*839529caSEd Maste #include "_libpe.h"
30*839529caSEd Maste 
31*839529caSEd Maste ELFTC_VCSID("$Id: pe_flag.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
32*839529caSEd Maste 
33*839529caSEd Maste int
pe_flag(PE * pe,PE_Cmd c,unsigned int flags)34*839529caSEd Maste pe_flag(PE *pe, PE_Cmd c, unsigned int flags)
35*839529caSEd Maste {
36*839529caSEd Maste 
37*839529caSEd Maste 	if (pe == NULL || (c != PE_C_SET && c != PE_C_CLR)) {
38*839529caSEd Maste 		errno = EINVAL;
39*839529caSEd Maste 		return (-1);
40*839529caSEd Maste 	}
41*839529caSEd Maste 
42*839529caSEd Maste 	if ((flags & ~(PE_F_STRIP_DOS_STUB | PE_F_STRIP_RICH_HEADER |
43*839529caSEd Maste 	    PE_F_STRIP_SYMTAB | PE_F_STRIP_DEBUG)) != 0) {
44*839529caSEd Maste 		errno = EINVAL;
45*839529caSEd Maste 		return (-1);
46*839529caSEd Maste 	}
47*839529caSEd Maste 
48*839529caSEd Maste 	if (c == PE_C_SET)
49*839529caSEd Maste 		pe->pe_flags |= flags;
50*839529caSEd Maste 	else
51*839529caSEd Maste 		pe->pe_flags &= ~flags;
52*839529caSEd Maste 
53*839529caSEd Maste 	return (0);
54*839529caSEd Maste }
55*839529caSEd Maste 
56*839529caSEd Maste int
pe_flag_dos_header(PE * pe,PE_Cmd c,unsigned int flags)57*839529caSEd Maste pe_flag_dos_header(PE *pe, PE_Cmd c, unsigned int flags)
58*839529caSEd Maste {
59*839529caSEd Maste 
60*839529caSEd Maste 	if (pe == NULL || (c != PE_C_SET && c != PE_C_CLR) ||
61*839529caSEd Maste 	    (flags & ~PE_F_DIRTY) != 0) {
62*839529caSEd Maste 		errno = EINVAL;
63*839529caSEd Maste 		return (-1);
64*839529caSEd Maste 	}
65*839529caSEd Maste 
66*839529caSEd Maste 	if (c == PE_C_SET)
67*839529caSEd Maste 		pe->pe_flags |= LIBPE_F_DIRTY_DOS_HEADER;
68*839529caSEd Maste 	else
69*839529caSEd Maste 		pe->pe_flags &= ~LIBPE_F_DIRTY_DOS_HEADER;
70*839529caSEd Maste 
71*839529caSEd Maste 	return (0);
72*839529caSEd Maste }
73*839529caSEd Maste 
74*839529caSEd Maste int
pe_flag_coff_header(PE * pe,PE_Cmd c,unsigned int flags)75*839529caSEd Maste pe_flag_coff_header(PE *pe, PE_Cmd c, unsigned int flags)
76*839529caSEd Maste {
77*839529caSEd Maste 
78*839529caSEd Maste 	if (pe == NULL || (c != PE_C_SET && c != PE_C_CLR) ||
79*839529caSEd Maste 	    (flags & ~PE_F_DIRTY) != 0) {
80*839529caSEd Maste 		errno = EINVAL;
81*839529caSEd Maste 		return (-1);
82*839529caSEd Maste 	}
83*839529caSEd Maste 
84*839529caSEd Maste 	if (c == PE_C_SET)
85*839529caSEd Maste 		pe->pe_flags |= LIBPE_F_DIRTY_COFF_HEADER;
86*839529caSEd Maste 	else
87*839529caSEd Maste 		pe->pe_flags &= ~LIBPE_F_DIRTY_COFF_HEADER;
88*839529caSEd Maste 
89*839529caSEd Maste 	return (0);
90*839529caSEd Maste }
91*839529caSEd Maste 
92*839529caSEd Maste int
pe_flag_opt_header(PE * pe,PE_Cmd c,unsigned int flags)93*839529caSEd Maste pe_flag_opt_header(PE *pe, PE_Cmd c, unsigned int flags)
94*839529caSEd Maste {
95*839529caSEd Maste 
96*839529caSEd Maste 	if (pe == NULL || (c != PE_C_SET && c != PE_C_CLR) ||
97*839529caSEd Maste 	    (flags & ~PE_F_DIRTY) != 0) {
98*839529caSEd Maste 		errno = EINVAL;
99*839529caSEd Maste 		return (-1);
100*839529caSEd Maste 	}
101*839529caSEd Maste 
102*839529caSEd Maste 	if (c == PE_C_SET)
103*839529caSEd Maste 		pe->pe_flags |= LIBPE_F_DIRTY_OPT_HEADER;
104*839529caSEd Maste 	else
105*839529caSEd Maste 		pe->pe_flags &= ~LIBPE_F_DIRTY_OPT_HEADER;
106*839529caSEd Maste 
107*839529caSEd Maste 	return (0);
108*839529caSEd Maste }
109*839529caSEd Maste 
110*839529caSEd Maste int
pe_flag_data_dir(PE * pe,PE_Cmd c,unsigned int flags)111*839529caSEd Maste pe_flag_data_dir(PE *pe, PE_Cmd c, unsigned int flags)
112*839529caSEd Maste {
113*839529caSEd Maste 
114*839529caSEd Maste 	if (pe == NULL || (c != PE_C_SET && c != PE_C_CLR) ||
115*839529caSEd Maste 	    (flags & ~PE_F_DIRTY) != 0) {
116*839529caSEd Maste 		errno = EINVAL;
117*839529caSEd Maste 		return (-1);
118*839529caSEd Maste 	}
119*839529caSEd Maste 
120*839529caSEd Maste 	if (c == PE_C_SET)
121*839529caSEd Maste 		pe->pe_flags |= LIBPE_F_DIRTY_OPT_HEADER;
122*839529caSEd Maste 	else
123*839529caSEd Maste 		pe->pe_flags &= ~LIBPE_F_DIRTY_OPT_HEADER;
124*839529caSEd Maste 
125*839529caSEd Maste 	return (0);
126*839529caSEd Maste }
127*839529caSEd Maste 
128*839529caSEd Maste int
pe_flag_scn(PE_Scn * ps,PE_Cmd c,unsigned int flags)129*839529caSEd Maste pe_flag_scn(PE_Scn *ps, PE_Cmd c, unsigned int flags)
130*839529caSEd Maste {
131*839529caSEd Maste 
132*839529caSEd Maste 	if (ps == NULL || (c != PE_C_SET && c != PE_C_CLR) ||
133*839529caSEd Maste 	    (flags & ~(PE_F_DIRTY | PE_F_STRIP_SECTION)) == 0) {
134*839529caSEd Maste 		errno = EINVAL;
135*839529caSEd Maste 		return (-1);
136*839529caSEd Maste 	}
137*839529caSEd Maste 
138*839529caSEd Maste 	if (c == PE_C_SET)
139*839529caSEd Maste 		ps->ps_flags |= flags;
140*839529caSEd Maste 	else
141*839529caSEd Maste 		ps->ps_flags &= ~flags;
142*839529caSEd Maste 
143*839529caSEd Maste 	return (0);
144*839529caSEd Maste }
145*839529caSEd Maste 
146*839529caSEd Maste int
pe_flag_section_header(PE_Scn * ps,PE_Cmd c,unsigned int flags)147*839529caSEd Maste pe_flag_section_header(PE_Scn *ps, PE_Cmd c, unsigned int flags)
148*839529caSEd Maste {
149*839529caSEd Maste 	PE *pe;
150*839529caSEd Maste 
151*839529caSEd Maste 	if (ps == NULL || (c != PE_C_SET && c != PE_C_CLR) ||
152*839529caSEd Maste 	    (flags & ~PE_F_DIRTY) != 0) {
153*839529caSEd Maste 		errno = EINVAL;
154*839529caSEd Maste 		return (-1);
155*839529caSEd Maste 	}
156*839529caSEd Maste 
157*839529caSEd Maste 	pe = ps->ps_pe;
158*839529caSEd Maste 
159*839529caSEd Maste 	/* The library doesn't support per section header dirty flag. */
160*839529caSEd Maste 	if (c == PE_C_SET)
161*839529caSEd Maste 		pe->pe_flags |= LIBPE_F_DIRTY_SEC_HEADER;
162*839529caSEd Maste 	else
163*839529caSEd Maste 		pe->pe_flags &= ~LIBPE_F_DIRTY_SEC_HEADER;
164*839529caSEd Maste 
165*839529caSEd Maste 	return (0);
166*839529caSEd Maste }
167*839529caSEd Maste 
168*839529caSEd Maste int
pe_flag_buffer(PE_Buffer * pb,PE_Cmd c,unsigned int flags)169*839529caSEd Maste pe_flag_buffer(PE_Buffer *pb, PE_Cmd c, unsigned int flags)
170*839529caSEd Maste {
171*839529caSEd Maste 	PE_SecBuf *sb;
172*839529caSEd Maste 
173*839529caSEd Maste 	if (pb == NULL || (c != PE_C_SET && c != PE_C_CLR) ||
174*839529caSEd Maste 	    (flags & ~PE_F_DIRTY) != 0) {
175*839529caSEd Maste 		errno = EINVAL;
176*839529caSEd Maste 		return (-1);
177*839529caSEd Maste 	}
178*839529caSEd Maste 
179*839529caSEd Maste 	sb = (PE_SecBuf *) pb;
180*839529caSEd Maste 
181*839529caSEd Maste 	if (c == PE_C_SET)
182*839529caSEd Maste 		sb->sb_flags |= flags;
183*839529caSEd Maste 	else
184*839529caSEd Maste 		sb->sb_flags &= ~flags;
185*839529caSEd Maste 
186*839529caSEd Maste 	return (0);
187*839529caSEd Maste }
188