xref: /freebsd/contrib/elftoolchain/libpe/pe_init.c (revision 3078531de10dcae44b253a35125c949ff4235284)
1 /*-
2  * Copyright (c) 2015 Kai Wang
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/queue.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 
31 #include "_libpe.h"
32 
33 ELFTC_VCSID("$Id: pe_init.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
34 
35 PE *
36 pe_init(int fd, PE_Cmd c, PE_Object o)
37 {
38 	PE *pe;
39 
40 	if ((pe = calloc(1, sizeof(*pe))) == NULL) {
41 		errno = ENOMEM;
42 		return (NULL);
43 	}
44 	pe->pe_fd = fd;
45 	pe->pe_cmd = c;
46 	pe->pe_obj = o;
47 	STAILQ_INIT(&pe->pe_scn);
48 
49 	switch (c) {
50 	case PE_C_READ:
51 	case PE_C_RDWR:
52 		if (libpe_open_object(pe) < 0)
53 			goto init_fail;
54 		break;
55 
56 	case PE_C_WRITE:
57 		if (o < PE_O_PE32 || o > PE_O_COFF) {
58 			errno = EINVAL;
59 			goto init_fail;
60 		}
61 		break;
62 
63 	default:
64 		errno = EINVAL;
65 		goto init_fail;
66 	}
67 
68 	return (pe);
69 
70 init_fail:
71 	pe_finish(pe);
72 	return (NULL);
73 }
74 
75 void
76 pe_finish(PE *pe)
77 {
78 
79 	if (pe == NULL)
80 		return;
81 
82 	libpe_release_object(pe);
83 }
84 
85 PE_Object
86 pe_object(PE *pe)
87 {
88 
89 	if (pe == NULL) {
90 		errno = EINVAL;
91 		return (PE_O_UNKNOWN);
92 	}
93 
94 	return (pe->pe_obj);
95 }
96