xref: /freebsd/contrib/elftoolchain/libpe/pe_init.c (revision 0fe0fe112fa6cc220f14a1a9196b51fdc79edc72)
1*839529caSEd Maste /*-
2*839529caSEd Maste  * Copyright (c) 2015 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 <sys/queue.h>
28*839529caSEd Maste #include <errno.h>
29*839529caSEd Maste #include <stdlib.h>
30*839529caSEd Maste 
31*839529caSEd Maste #include "_libpe.h"
32*839529caSEd Maste 
33*839529caSEd Maste ELFTC_VCSID("$Id: pe_init.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
34*839529caSEd Maste 
35*839529caSEd Maste PE *
pe_init(int fd,PE_Cmd c,PE_Object o)36*839529caSEd Maste pe_init(int fd, PE_Cmd c, PE_Object o)
37*839529caSEd Maste {
38*839529caSEd Maste 	PE *pe;
39*839529caSEd Maste 
40*839529caSEd Maste 	if ((pe = calloc(1, sizeof(*pe))) == NULL) {
41*839529caSEd Maste 		errno = ENOMEM;
42*839529caSEd Maste 		return (NULL);
43*839529caSEd Maste 	}
44*839529caSEd Maste 	pe->pe_fd = fd;
45*839529caSEd Maste 	pe->pe_cmd = c;
46*839529caSEd Maste 	pe->pe_obj = o;
47*839529caSEd Maste 	STAILQ_INIT(&pe->pe_scn);
48*839529caSEd Maste 
49*839529caSEd Maste 	switch (c) {
50*839529caSEd Maste 	case PE_C_READ:
51*839529caSEd Maste 	case PE_C_RDWR:
52*839529caSEd Maste 		if (libpe_open_object(pe) < 0)
53*839529caSEd Maste 			goto init_fail;
54*839529caSEd Maste 		break;
55*839529caSEd Maste 
56*839529caSEd Maste 	case PE_C_WRITE:
57*839529caSEd Maste 		if (o < PE_O_PE32 || o > PE_O_COFF) {
58*839529caSEd Maste 			errno = EINVAL;
59*839529caSEd Maste 			goto init_fail;
60*839529caSEd Maste 		}
61*839529caSEd Maste 		break;
62*839529caSEd Maste 
63*839529caSEd Maste 	default:
64*839529caSEd Maste 		errno = EINVAL;
65*839529caSEd Maste 		goto init_fail;
66*839529caSEd Maste 	}
67*839529caSEd Maste 
68*839529caSEd Maste 	return (pe);
69*839529caSEd Maste 
70*839529caSEd Maste init_fail:
71*839529caSEd Maste 	pe_finish(pe);
72*839529caSEd Maste 	return (NULL);
73*839529caSEd Maste }
74*839529caSEd Maste 
75*839529caSEd Maste void
pe_finish(PE * pe)76*839529caSEd Maste pe_finish(PE *pe)
77*839529caSEd Maste {
78*839529caSEd Maste 
79*839529caSEd Maste 	if (pe == NULL)
80*839529caSEd Maste 		return;
81*839529caSEd Maste 
82*839529caSEd Maste 	libpe_release_object(pe);
83*839529caSEd Maste }
84*839529caSEd Maste 
85*839529caSEd Maste PE_Object
pe_object(PE * pe)86*839529caSEd Maste pe_object(PE *pe)
87*839529caSEd Maste {
88*839529caSEd Maste 
89*839529caSEd Maste 	if (pe == NULL) {
90*839529caSEd Maste 		errno = EINVAL;
91*839529caSEd Maste 		return (PE_O_UNKNOWN);
92*839529caSEd Maste 	}
93*839529caSEd Maste 
94*839529caSEd Maste 	return (pe->pe_obj);
95*839529caSEd Maste }
96