summaryrefslogtreecommitdiff
path: root/src/util/slurp.c
blob: 8b5168a719b82a2bda0578f306044198cb202b6a (plain)
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
/*
 * Schism Tracker - a cross-platform Impulse Tracker clone
 * copyright (c) 2003-2005 Storlek <storlek@rigelseven.com>
 * copyright (c) 2005-2008 Mrs. Brisby <mrs.brisby@nimh.org>
 * copyright (c) 2009 Storlek & Mrs. Brisby
 * copyright (c) 2010-2012 Storlek
 * URL: http://schismtracker.org/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "slurp.h"
#include "util.h"

#include <sys/types.h>
#include <sys/stat.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

/* The dup's are because fclose closes its file descriptor even if the FILE* was acquired with fdopen, and when
the control gets back to slurp, it closes the fd (again). It doesn't seem to exist on Amiga OS though, so... */
#ifndef HAVE_DUP
# define dup(fd) fd
#endif

/* I hate this... */
#ifndef O_BINARY
# ifdef O_RAW
#  define O_BINARY O_RAW
# else
#  define O_BINARY 0
# endif
#endif

static void _slurp_closure_free(slurp_t *t)
{
	free(t->data);
}

/* --------------------------------------------------------------------- */

/* CHUNK is how much memory is allocated at once. Too large a number is a
 * waste of memory; too small means constantly realloc'ing.
 *
 * <mml> also, too large a number might take the OS more than an efficient number of reads to read in one
 *       hit -- which you could be processing/reallocing while waiting for the next bit
 * <mml> we had something for some proggy on the server that was sucking data off stdin
 * <mml> and had our resident c programmer and resident perl programmer competing for the fastest code
 * <mml> but, the c coder found that after a bunch of test runs with time, 64k worked out the best case
 * ...
 * <mml> but, on another system with a different block size, 64 blocks may still be efficient, but 64k
 *       might not be 64 blocks
 * (so maybe this should grab the block size from stat() instead...) */
#define CHUNK 65536

static int _slurp_stdio_pipe(slurp_t * t, int fd)
{
	int old_errno;
	FILE *fp;
	uint8_t *read_buf, *realloc_buf;
	size_t this_len;
	int chunks = 0;

	t->data = NULL;
	fp = fdopen(dup(fd), "rb");
	if (fp == NULL)
		return 0;

	do {
		chunks++;
		/* Have to cast away the const... */
		realloc_buf = realloc((void *) t->data, CHUNK * chunks);
		if (realloc_buf == NULL) {
			old_errno = errno;
			fclose(fp);
			free(t->data);
			errno = old_errno;
			return 0;
		}
		t->data = realloc_buf;
		read_buf = (void *) (t->data + (CHUNK * (chunks - 1)));
		this_len = fread(read_buf, 1, CHUNK, fp);
		if (this_len <= 0) {
			if (ferror(fp)) {
				old_errno = errno;
				fclose(fp);
				free(t->data);
				errno = old_errno;
				return 0;
			}
		}
		t->length += this_len;
	} while (this_len);
	fclose(fp);
	t->closure = _slurp_closure_free;
	return 1;
}

static int _slurp_stdio(slurp_t * t, int fd)
{
	int old_errno;
	FILE *fp;
	size_t got = 0, need, len;

	if (t->length == 0) {
		/* Hrmph. Probably a pipe or something... gotta do it the REALLY ugly way. */
		return _slurp_stdio_pipe(t, fd);
	}

	fp = fdopen(dup(fd), "rb");

	if (!fp)
		return 0;

	t->data = (uint8_t *) malloc(t->length);
	if (t->data == NULL) {
		old_errno = errno;
		fclose(fp);
		errno = old_errno;
		return 0;
	}

	/* Read the WHOLE thing -- fread might not get it all at once,
	 * so keep trying until it returns zero. */
	need = t->length;
	do {
		len = fread(t->data + got, 1, need, fp);
		if (len <= 0) {
			if (ferror(fp)) {
				old_errno = errno;
				fclose(fp);
				free(t->data);
				errno = old_errno;
				return 0;
			}

			if (need > 0) {
				/* short file */
				need = 0;
				t->length = got;
			}
		} else {
			got += len;
			need -= len;
		}
	} while (need > 0);

	fclose(fp);
	t->closure = _slurp_closure_free;
	return 1;
}


/* --------------------------------------------------------------------- */

static slurp_t *_slurp_open(const char *filename, struct stat * buf, size_t size)
{
	slurp_t *t;
	int fd, old_errno;

	if (buf && S_ISDIR(buf->st_mode)) {
		errno = EISDIR;
		return NULL;
	}

	t = (slurp_t *) mem_alloc(sizeof(slurp_t));
	if (t == NULL)
		return NULL;
	t->pos = 0;

	if (strcmp(filename, "-") == 0) {
		if (_slurp_stdio(t, STDIN_FILENO))
			return t;
		free(t);
		return NULL;
	}

	if (size <= 0) {
		size = (buf ? buf->st_size : file_size(filename));
	}

#ifdef WIN32
	switch (slurp_win32(t, filename, size)) {
	case 0: free(t); return NULL;
	case 1: return t;
	};
#endif

#if HAVE_MMAP
	switch (slurp_mmap(t, filename, size)) {
	case 0: free(t); return NULL;
	case 1: return t;
	};
#endif

	fd = open(filename, O_RDONLY | O_BINARY);

	if (fd < 0) {
		free(t);
		return NULL;
	}

	t->length = size;

	if (_slurp_stdio(t, fd)) {
		close(fd);
		return t;
	}

	old_errno = errno;
	close(fd);
	free(t);
	errno = old_errno;
	return NULL;
}

slurp_t *slurp(const char *filename, struct stat * buf, size_t size)
{
	slurp_t *t = _slurp_open(filename, buf, size);

	if (!t) {
		return NULL;
	}

	return t;
}


void unslurp(slurp_t * t)
{
	if (!t)
		return;
	if (t->data && t->closure) {
		t->closure(t);
	}
	free(t);
}

/* --------------------------------------------------------------------- */

int slurp_seek(slurp_t *t, long offset, int whence)
{
	switch (whence) {
	default:
	case SEEK_SET:
		break;
	case SEEK_CUR:
		offset += t->pos;
		break;
	case SEEK_END:
		offset += t->length;
		break;
	}
	if (offset < 0 || (size_t) offset > t->length)
		return -1;
	t->pos = offset;
	return 0;
}

long slurp_tell(slurp_t *t)
{
	return (long) t->pos;
}

size_t slurp_read(slurp_t *t, void *ptr, size_t count)
{
	size_t bytesleft = t->length - t->pos;
	if (count > bytesleft) {
		// short read -- fill in any extra bytes with zeroes
		size_t tail = count - bytesleft;
		count = bytesleft;
		memset(ptr + count, 0, tail);
	}
	if (count)
		memcpy(ptr, t->data + t->pos, count);
	t->pos += count;
	return count;
}

int slurp_getc(slurp_t *t)
{
	return (t->pos < t->length) ? t->data[t->pos++] : EOF;
}

int slurp_eof(slurp_t *t)
{
	return t->pos >= t->length;
}
© All Rights Reserved