4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
30 #define BLOCKSIZE (1024 * 16)
32 void file_write_from_buffer(int fd, unsigned char *buffer, int size)
34 unsigned char *ptr = buffer;
37 int ret = write(fd, ptr, size);
47 unsigned char *file_read_to_buffer(int fd, int *fsize)
49 unsigned char *buffer = malloc(BLOCKSIZE);
50 unsigned char *ptr = buffer;
52 int bsize = BLOCKSIZE;
53 int remain = BLOCKSIZE;
61 ret = read(fd, ptr, remain);
70 /* Allocate more memory */
73 buffer = realloc(buffer, bsize + BLOCKSIZE);
95 unsigned char *file_read(const char *filename, int *fsize)
97 int fd = open(filename, O_RDONLY);
98 unsigned char *buffer;
103 buffer = file_read_to_buffer(fd, fsize);
108 void file_write(const char *filename, unsigned char *buffer, int size)
110 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC,
114 fprintf(stderr, "E: Could not create %s: %m\n", filename);
118 file_write_from_buffer(fd, buffer, size);
122 int iself(unsigned char *input)
124 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
125 return !memcmp(ehdr->e_ident, ELFMAG, 4);