1 /*
2  * Copyright 2011-2012 Red Hat, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Author(s): Peter Jones <pjones@redhat.com>
18  */
19 #ifndef LIBDPE_PRIV_H
20 #define LIBDPE_PRIV_H 1
21 
22 #include <libdpe/libdpe.h>
23 #include "endian.h"
24 #include "lock.h"
25 
26 enum {
27 	PE_F_DIRTY = 0x1,
28 	PE_F_MMAPPED = 0x40,
29 	PE_F_MALLOCED = 0x80,
30 	PE_F_FILEDATA = 0x100,
31 };
32 
33 enum {
34 	PE_E_NOERROR = 0,
35 	PE_E_UNKNOWN_ERROR,
36 	PE_E_INVALID_HANDLE,
37 	PE_E_NOMEM,
38 	PE_E_INVALID_FILE,
39 	PE_E_WRITE_ERROR,
40 	PE_E_INVALID_INDEX,
41 	PE_E_INVALID_OP,
42 	PE_E_INVALID_CMD,
43 	PE_E_INVALID_OPERAND,
44 	PE_E_WRONG_ORDER_PEHDR,
45 	PE_E_FD_DISABLED,
46 	PE_E_FD_MISMATCH,
47 	PE_E_UPDATE_RO,
48 	PE_E_NUM /* terminating entry */
49 };
50 
51 extern void __libpe_seterrno(int value);
52 
53 struct Pe_Scn {
54 	size_t index;
55 	struct Pe *pe;
56 	struct section_header *shdr;
57 	unsigned int shdr_flags;
58 	unsigned int flags;
59 
60 	char *rawdata_base;
61 	char *data_base;
62 
63 	struct Pe_ScnList *list;
64 };
65 
66 typedef struct Pe_ScnList
67 {
68 	unsigned int cnt;
69 	unsigned int max;
70 	struct Pe_ScnList *next;
71 	struct Pe_Scn data[0];
72 } Pe_ScnList;
73 
74 struct Pe {
75 	/* Address to which the file was mapped.  NULL if not mapped. */
76 	char *map_address;
77 
78 	Pe *parent;
79 	Pe *next;
80 
81 	/* command used to create this object */
82 	Pe_Cmd cmd;
83 	Pe_Kind kind;
84 
85 	int fildes;
86 	size_t maximum_size;
87 
88 	int flags;
89 
90 	int ref_count;
91 	rwlock_define(,lock);
92 
93 	union {
94 		struct {
95 			struct mz_hdr *mzhdr;
96 			struct pe_hdr *pehdr;
97 			void *reserved0;
98 			void *reserved1;
99 			struct section_header *shdr;
100 
101 			Pe_ScnList *scns_last;
102 			unsigned int scnincr;
103 
104 			Pe_ScnList scns;
105 		} pe;
106 
107 		struct {
108 			struct mz_hdr *mzhdr;
109 			struct pe_hdr *pehdr;
110 			void *reserved0;
111 			void *reserved1;
112 			struct section_header *shdr;
113 
114 			Pe_ScnList *scns_last;
115 			unsigned int scnincr;
116 
117 			Pe_ScnList scns;
118 		} pe32_obj;
119 
120 		struct {
121 			struct mz_hdr *mzhdr;
122 			struct pe_hdr *pehdr;
123 			void *reserved0;
124 			void *reserved1;
125 			struct section_header *shdr;
126 
127 			Pe_ScnList *scns_last;
128 			unsigned int scnincr;
129 
130 			Pe_ScnList scns;
131 		} pe32_rom;
132 
133 		struct {
134 			struct mz_hdr *mzhdr;
135 			struct pe_hdr *pehdr;
136 			struct pe32_opt_hdr *opthdr;
137 			data_directory *datadir;
138 			struct section_header *shdr;
139 
140 			Pe_ScnList *scns_last;
141 			unsigned int scnincr;
142 
143 			Pe_ScnList scns;
144 		} pe32_exe;
145 
146 		struct {
147 			struct mz_hdr *mzhdr;
148 			struct pe_hdr *pehdr;
149 			void *reserved0;
150 			void *reserved1;
151 			struct section_header *shdr;
152 
153 			Pe_ScnList *scns_last;
154 			unsigned int scnincr;
155 
156 			Pe_ScnList scns;
157 		} pe32plus_obj;
158 
159 		struct {
160 			struct mz_hdr *mzhdr;
161 			struct pe_hdr *pehdr;
162 			struct pe32plus_opt_hdr *opthdr;
163 			data_directory *datadir;
164 			struct section_header *shdr;
165 
166 			Pe_ScnList *scns_last;
167 			unsigned int scnincr;
168 
169 			Pe_ScnList scns;
170 		} pe32plus_exe;
171 	} state;
172 };
173 
174 #include "common.h"
175 
176 extern off_t __pe_updatemmap(Pe *pe, size_t shnum);
177 extern int __pe_updatefile(Pe *pe, size_t shnum);
178 extern off_t __pe_updatenull_wrlock(Pe *pe, size_t shnum);
179 extern char *__libpe_readall(Pe *pe);
180 
181 #endif /* LIBDPE_PRIV_H */
182