1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/*
*
* Copyright (c) 2008-2011 Erich Hoover
*
* libr - Add resources into ELF binaries
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* To provide feedback, report bugs, or otherwise contact me:
* ehoover at mines dot edu
*
*/
#ifndef __LIBR_H
#define __LIBR_H
#include <sys/types.h>
#define DEPRECATED_FN __attribute__ ((deprecated))
#define ALIAS_FN(fn) __attribute__ ((weak, alias (#fn)))
/**
* @addtogroup libr_status libr_status
* @brief Enumeration of possible libr status values.
* @{
* \#include <libr.h>
*/
/** Possible libr status values */
typedef enum {
LIBR_OK = 0, /**< Success */
LIBR_ERROR_GETEHDR = -1, /**< Failed to obtain ELF header: */
LIBR_ERROR_NOTABLE = -2, /**< No ELF string table */
LIBR_ERROR_TABLE = -3, /**< Failed to open string table: */
LIBR_ERROR_GETDATA = -4, /**< Failed to obtain data of section */
LIBR_ERROR_GETSHDR = -5, /**< Failed to obtain ELF section header: */
LIBR_ERROR_SIZEMISMATCH = -6, /**< Section's data size does not make sense */
LIBR_ERROR_UPDATE = -7, /**< Failed to perform dynamic update: */
LIBR_ERROR_NEWSECTION = -8, /**< Failed to create new section */
LIBR_ERROR_NEWDATA = -9, /**< Failed to create data for section */
LIBR_ERROR_REMOVESECTION = -10, /**< Failed to remove section: */
LIBR_ERROR_NOSECTION = -11, /**< ELF resource section not found */
LIBR_ERROR_STRPTR = -12, /**< Failed to obtain section string pointer: */
LIBR_ERROR_NOTRESOURCE = -13, /**< Not a valid libr-resource */
LIBR_ERROR_EXPANDSECTION = -14, /**< Failed to expand section */
LIBR_ERROR_WRONGFORMAT = -15, /**< Invalid input file format */
LIBR_ERROR_SETFLAGS = -16, /**< Failed to set flags for section */
LIBR_ERROR_NOPERM = -17, /**< Open handle with LIBR_READ_WRITE access */
LIBR_ERROR_NOSIZE = -18, /**< Failed to obtain file size */
LIBR_ERROR_SETFORMAT = -19, /**< Failed to set output file format to input file format */
LIBR_ERROR_SETARCH = -20, /**< Failed to set output file architecture to input file architecture */
LIBR_ERROR_OVERWRITE = -21, /**< Section already exists, over-write not specified */
LIBR_ERROR_COMPRESS = -22, /**< Failed to compress resource data */
LIBR_ERROR_INVALIDTYPE = -23, /**< Invalid data storage type specified */
LIBR_ERROR_MEMALLOC = -24, /**< Failed to allocate memory for data */
LIBR_ERROR_INVALIDPARAMS = -25, /**< Invalid parameters passed to function */
LIBR_ERROR_UNCOMPRESS = -26, /**< Failed to uncompress resource data */
LIBR_ERROR_ZLIBINIT = -27, /**< zlib library initialization failed */
LIBR_ERROR_OPENFAILED = -28, /**< Failed to open input file */
LIBR_ERROR_BEGINFAILED = -29, /**< Failed to open ELF file: */
LIBR_ERROR_WRITEPERM = -30, /**< No write permission for file */
LIBR_ERROR_UNSUPPORTED = -31, /**< The requested operation is not supported by the backend */
} libr_status;
/**
* @}
*/
typedef enum {
LIBR_READ = 0,
LIBR_READ_WRITE = 1,
} libr_access_t;
typedef enum {
LIBR_UNCOMPRESSED = 0,
LIBR_COMPRESSED = 1
} libr_type_t;
typedef enum {
LIBR_NOOVERWRITE = 0,
LIBR_OVERWRITE = 1
} libr_overwrite_t;
#ifdef __LIBR_BUILD__
#include "libr-internal.h"
#if __LIBR_BACKEND_libbfd__
#include "libr-bfd.h"
#elif __LIBR_BACKEND_libelf__
#include "libr-elf.h"
#elif __LIBR_BACKEND_readonly__
#include "libr-ro.h"
#else /* LIBR_BACKEND */
#error "Unhandled backend"
#endif /* LIBR_BACKEND */
#include "libr-backends.h"
#else
struct _libr_file;
typedef struct _libr_file libr_file;
#endif /* __LIBR_BUILD__ */
/*************************************************************************
* libr Resource Management API
*************************************************************************/
/**
* @page libr_clear Remove a resource from an ELF executable.
* @section SYNOPSIS
* \#include <libr.h>
*
* <b>int libr_clear(libr_file *handle, char *resourcename);</b>
*
* @section DESCRIPTION
* Removes a libr-compatible resource from an ELF executable. The handle
* must be opened using <b>libr_open</b>(3) with either <b>LIBR_WRITE</b>
* or <b>LIBR_READ_WRITE</b> access in order to remove a resource.
*
* Please note that resource removal does not occur until the handle is
* closed using <b>libr_close</b>(3).
*
* @param handle A handle returned by <b>libr_open</b>(3).
* @param resourcename The name of the libr-compatible resource to remove.
*
* @section SA SEE ALSO
* <b>libr_open</b>(3), <b>libr_close</b>(3)
*
* @section AUTHOR
* Erich Hoover <ehoover@mines.edu>
*/
int libr_clear(libr_file *handle, char *resourcename);
/**
* @page libr_close Close a handle to an ELF executable.
* @section SYNOPSIS
* \#include <libr.h>
*
* <b>void libr_close(libr_file *handle);</b>
*
* @section DESCRIPTION
* Handles opened with <b>libr_open</b>(3) should be closed with
* <b>libr_close</b>() when they are no-longer needed by the calling
* application.
*
* @param handle The handle to close.
*
* @section SA SEE ALSO
* <b>libr_open</b>(3)
*
* @section AUTHOR
* Erich Hoover <ehoover@mines.edu>
*/
void libr_close(libr_file *handle);
/**
* @page libr_errmsg Return a detailed description of the last
* libr-related error.
* @section SYNOPSIS
* \#include <libr.h>
*
* <b>char *libr_errmsg(void);</b>
*
* @section DESCRIPTION
* Returns a detailed string describing the last error encountered by
* the libr resource library. The string is an internal error
* description, so it should not be freed.
*
* If no errors have been encountered then NULL is returned.
*
* @section SA SEE ALSO
* <b>libr_errno</b>(3)
*
* @section AUTHOR
* Erich Hoover <ehoover@mines.edu>
*/
char *libr_errmsg(void);
/**
* @page libr_errno Return a status code describing the last
* libr-related error.
* @section SYNOPSIS
* \#include <libr.h>
*
* <b>libr_status libr_errno(void);</b>
*
* @section DESCRIPTION
* Returns a code corresponding to the last error encountered by
* the libr resource library. For a detailed description of possible
* return values see <b>libr_status</b>(3).
*
* To get a user-readable string corresponding to the last error the
* <b>libr_errmsg</b>(3) function should be used instead.
*
* If no errors have been encountered then <b>LIBR_OK</b> is returned.
*
* @section SA SEE ALSO
* <b>libr_errmsg</b>(3)
*
* @section AUTHOR
* Erich Hoover <ehoover@mines.edu>
*/
libr_status libr_errno(void);
/**
* @page libr_list Obtain the name of a libr ELF resource (by index).
* @section SYNOPSIS
* \#include <libr.h>
*
* <b>char *libr_list(libr_file *file_handle, unsigned int resourceid);</b>
*
* @section DESCRIPTION
* Returns the name of a libr-compatible resource stored in an ELF binary
* corresponding to the given resource index. The index value ranges from
* 0 to the value returned by <b>libr_resources</b>(3), which returns the
* total number of libr-compatible resources stored in the ELF binary.
*
* @param handle A handle returned by <b>libr_open</b>(3).
* @param resourceid The index of the libr-compatible resource for which
* the name will be returned.
*
* @return Returns a string containing the name of the resource section. This
* string is allocated when the function is called, so it <i>must be
* unallocated</i> with a call to <b>free</b>(3) when it is no-longer
* needed. NULL is returned on failure.
*
* @section SA SEE ALSO
* <b>libr_open</b>(3), <b>free</b>(3)
*
* @section AUTHOR
* Erich Hoover <ehoover@mines.edu>
*/
char *libr_list(libr_file *file_handle, unsigned int resourceid);
/**
* @page libr_malloc Obtain the data corresponding to a libr ELF resource.
* @section SYNOPSIS
* \#include <libr.h>
*
* <b>char *libr_malloc(libr_file *handle, char *resourcename, size_t *size);</b>
*
* @section DESCRIPTION
* Returns the contents of a libr-compatible resource stored in an ELF binary
* corresponding to the given resource name.
*
* @param handle A handle returned by <b>libr_open</b>(3).
* @param resourcename The name of the libr-compatible resource for which
* the data will be returned.
* @param size A pointer for storing the length of the data contained in the
* the resource. May be NULL.
*
* @return Returns NULL on failure, the pointer to a buffer containing the data
* for the resource on success. When the buffer is no-longer used it must
* be unallocated using a call to <b>free</b>(3).
*
* @section SA SEE ALSO
* <b>libr_open</b>(3), <b>free</b>(3)
*
* @section AUTHOR
* Erich Hoover <ehoover@mines.edu>
*/
char *libr_malloc(libr_file *handle, char *resourcename, size_t *size);
/**
* @page libr_open Open an ELF executable file for resource management.
* @section SYNOPSIS
* \#include <libr.h>
*
* <b>libr_file *libr_open(char *filename, libr_access_t access);</b>
*
* @section DESCRIPTION
* <b>libr_open</b>() can be used on any ELF executable, however,
* <b>libr_open</b>() called with <b>LIBR_READ</b> access is only useful
* for executables that already contain libr-compatible stored resources.
*
* An application can easily access its own resources by passing NULL for
* the filename and requesting <b>LIBR_READ</b> access. For the obvious
* reason that an actively-open application cannot edit itself, the
* calling binary may only request <b>LIBR_READ</b> access.
*
* @param filename ELF executable to manage. Pass a NULL pointer as the
* filename in order to access the calling binary (<b>LIBR_READ</b>
* access only) @param access Requested access type (<b>LIBR_READ</b>,
* <b>LIBR_WRITE</b>, <b>LIBR_READ_WRITE</b>), the valid operations for
* the returned handle will be restricted based upon the requested access.
* @return Returns a libr file handle on success, NULL on failure. The
* handle should be freed with <b>libr_close</b>(3) when no-longer used.
*
* @section SA SEE ALSO
* <b>libr_close</b>(3)
*
* @section AUTHOR
* Erich Hoover <ehoover@mines.edu>
*/
libr_file *libr_open(char *filename, libr_access_t access);
/**
* @page libr_read Read out the contents of a libr ELF resource.
* @section SYNOPSIS
* \#include <libr.h>
*
* <b>int libr_read(libr_file *handle, char *resourcename, char *buffer);</b>
*
* @section WARNING
* This function does not allocate memory for the buffer, so the buffer must
* be large enough to fit the resource data. For this reason it is suggested
* that <b>libr_malloc</b>(3) be used in preference over this function.
*
* @section DESCRIPTION
* Reads the contents of a resource embedded in an ELF binary, the resource
* must be compatible with the libr specification.
*
* @param handle A handle returned by <b>libr_open</b>(3).
* @return Returns 1 on success, 0 on failure.
*
* @section SA SEE ALSO
* <b>libr_open</b>(3)
*
* @section AUTHOR
* Erich Hoover <ehoover@mines.edu>
*/
int libr_read(libr_file *handle, char *resourcename, char *buffer);
/**
* @page libr_resources Returns the number of resources contained in
* the ELF binary.
* @section SYNOPSIS
* \#include <libr.h>
*
* <b>unsigned int libr_resources(libr_file *handle);</b>
*
* @section DESCRIPTION
* Returns the total number of libr-compatible resources contained
* in the ELF binary. Intended to be used with <b>libr_list</b>(3)
* to return the full list of resources contained in the binary.
*
* @param handle A handle returned by <b>libr_open</b>(3).
* @return The total number of libr resources in the binary.
*
* @section SA SEE ALSO
* <b>libr_open</b>(3), <b>libr_list</b>(3)
*
* @section AUTHOR
* Erich Hoover <ehoover@mines.edu>
*/
unsigned int libr_resources(libr_file *handle);
/**
* @page libr_size Returns the uncompressed size of a libr resource.
* @section SYNOPSIS
* \#include <libr.h>
*
* <b>int libr_size(libr_file *handle, char *resourcename, size_t *size);</b>
*
* @section DESCRIPTION
* Obtain the total number of bytes consumed by the uncompressed
* version of the specific libr-resource. Intended to be used with
* <b>libr_read</b>(3) in order to allocate a large enough buffer
* for the resource.
*
* @param handle A handle returned by <b>libr_open</b>(3).
* @param resourcename The name of the resource for which the
* size of the data section will be returned.
* @param size A pointer for storing the size of the data section.
* This pointer cannot be NULL.
* @return Returns 1 on success, 0 on failure.
*
* @section SA SEE ALSO
* <b>libr_open</b>(3), <b>libr_read</b>(3)
*
* @section AUTHOR
* Erich Hoover <ehoover@mines.edu>
*/
int libr_size(libr_file *handle, char *resourcename, size_t *size);
/**
* @page libr_write Adds a libr resource to an ELF binary.
* @section SYNOPSIS
* \#include <libr.h>
*
* <b>int libr_write(libr_file *handle, char *resourcename, char *buffer, size_t size, libr_type_t type, libr_overwrite_t overwrite);</b>
*
* @section DESCRIPTION
* Adds a libr-compatible resource into the ELF binary. The handle
* must be opened using <b>libr_open</b>(3) with either <b>LIBR_WRITE</b>
* or <b>LIBR_READ_WRITE</b> access in order to add a resource.
*
* @param handle A handle returned by <b>libr_open</b>(3).
* @param resourcename The name of the resource to create.
* @param buffer A string containing the data of the resource.
* @param size The total size of the buffer.
* @param type The method which should be used for storing the
* data (either <b>LIBR_UNCOMPRESSED</b> or
* <b>LIBR_COMPRESSED</b>).
* @param overwrite Whether overwriting an existing resource
* should be permitted (either <b>LIBR_NOOVERWRITE</b> or
* <b>LIBR_OVERWRITE</b>).
* @return Returns 1 on success, 0 on failure.
*
* @section SA SEE ALSO
* <b>libr_open</b>(3)
*
* @section AUTHOR
* Erich Hoover <ehoover@mines.edu>
*/
int libr_write(libr_file *handle, char *resourcename, char *buffer, size_t size, libr_type_t type, libr_overwrite_t overwrite);
#endif /* __LIBR_H */
|