]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/elftoolchain/libpe/pe_coff.c
Merge ^/vendor/lvm-project/master up to its last change (upstream commit
[FreeBSD/FreeBSD.git] / contrib / elftoolchain / libpe / pe_coff.c
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 <errno.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "_libpe.h"
32
33 ELFTC_VCSID("$Id: pe_coff.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
34
35 PE_CoffHdr *
36 pe_coff_header(PE *pe)
37 {
38
39         if (pe->pe_ch == NULL) {
40                 errno = ENOENT;
41                 return (NULL);
42         }
43
44         return (pe->pe_ch);
45 }
46
47 PE_OptHdr *
48 pe_opt_header(PE *pe)
49 {
50
51         if (pe->pe_oh == NULL) {
52                 errno = ENOENT;
53                 return (NULL);
54         }
55
56         return (pe->pe_oh);
57 }
58
59 PE_DataDir *
60 pe_data_dir(PE *pe)
61 {
62
63         if (pe->pe_dd == NULL) {
64                 errno = ENOENT;
65                 return (NULL);
66         }
67
68         return (pe->pe_dd);
69 }
70
71 int
72 pe_update_coff_header(PE *pe, PE_CoffHdr *ch)
73 {
74
75         if (pe == NULL || ch == NULL) {
76                 errno = EINVAL;
77                 return (-1);
78         }
79
80         if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {
81                 errno = EACCES;
82                 return (-1);
83         }
84
85         if (pe->pe_ch == NULL) {
86                 if ((pe->pe_ch = malloc(sizeof(PE_CoffHdr))) == NULL) {
87                         errno = ENOMEM;
88                         return (-1);
89                 }
90         } else {
91                 /* Rewrite optional header if `optsize' field changed. */
92                 if (pe->pe_ch->ch_optsize != ch->ch_optsize)
93                         pe->pe_flags |= LIBPE_F_DIRTY_OPT_HEADER;
94         }
95
96         *pe->pe_ch = *ch;
97
98         pe->pe_flags |= LIBPE_F_DIRTY_COFF_HEADER;
99
100         return (0);
101 }
102
103 int
104 pe_update_opt_header(PE *pe, PE_OptHdr *oh)
105 {
106
107         if (pe == NULL || oh == NULL) {
108                 errno = EINVAL;
109                 return (-1);
110         }
111
112         if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {
113                 errno = EACCES;
114                 return (-1);
115         }
116
117         if (pe->pe_oh == NULL) {
118                 if ((pe->pe_oh = malloc(sizeof(PE_OptHdr))) == NULL) {
119                         errno = ENOMEM;
120                         return (-1);
121                 }
122         }
123
124         *pe->pe_oh = *oh;
125
126         pe->pe_flags |= LIBPE_F_DIRTY_OPT_HEADER;
127
128         return (0);
129 }
130
131 int
132 pe_update_data_dir(PE *pe, PE_DataDir *dd)
133 {
134
135         if (pe == NULL || dd == NULL) {
136                 errno = EINVAL;
137                 return (-1);
138         }
139
140         if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {
141                 errno = EACCES;
142                 return (-1);
143         }
144
145         if (pe->pe_dd == NULL) {
146                 if ((pe->pe_dd = malloc(sizeof(PE_DataDir))) == NULL) {
147                         errno = ENOMEM;
148                         return (-1);
149                 }
150         }
151
152         *pe->pe_dd = *dd;
153
154         pe->pe_flags |= LIBPE_F_DIRTY_OPT_HEADER;
155
156         return (0);
157 }