summaryrefslogtreecommitdiffstats
path: root/readtextfile.c
blob: ca74689497e08ce8c5eea6aaee265909cc7670f4 (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
/*  This file is part of "reprepro"
 *  Copyright (C) 2007 Bernhard R. Link
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  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., 51 Franklin St, Fifth Floor, Boston, MA  02111-1301  USA
 */
#include <config.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include "error.h"
#include "names.h"
#include "chunks.h"
#include "readtextfile.h"

/* This file supplies code to read a text file (.changes, .dsc, Release, ...)
 * into a chunk, warning if it is too long or if it contains binary data */

static bool isbinarydata(const char *buffer, size_t len, const char *source) {
	size_t i;
	unsigned char c;

	for (i = 0 ; i < len ; i++) {
		c = (unsigned char)buffer[i];
		if (c < ' ' && c != '\t' && c != '\n' && c != '\r') {
			fprintf(stderr,
"Unexpected binary character \\%03hho in %s\n",
					c, source);
			return true;
		}
	}
	return false;
}

retvalue readtextfilefd(int fd, const char *source, char **data, size_t *len) {
	size_t buffersize = 102400, readdata = 0;
	ssize_t readbytes;
	char *buffer, *h;

	buffer = malloc(buffersize);
	if (FAILEDTOALLOC(buffer))
		return RET_ERROR_OOM;
	errno = 0;
	while ((readbytes = read(fd, buffer + readdata, buffersize-readdata))
			> 0) {

		/* text files are normally small, so it does not hurt to check
		 * the whole of them always */
		if (isbinarydata(buffer + readdata, (size_t)readbytes, source)) {
			free(buffer);
			return RET_ERROR;
		}
		readdata += readbytes;
		assert (readdata <= buffersize);
		if (readdata + 1024 >= buffersize) {
			if (buffersize >= 10*1024*1024) {
				fprintf(stderr, "Ridiculously large %s\n", source);
				free(buffer);
				return RET_ERROR;
			}
			buffersize += 51200;
			h = realloc(buffer, buffersize);
			if (FAILEDTOALLOC(h)) {
				free(buffer);
				return RET_ERROR_OOM;
			}
			buffer = h;
		}
	}
	if (readbytes < 0) {
		int e = errno;
		free(buffer);
		fprintf(stderr, "Error reading %s: %s\n", source,
				strerror(e));
		return RET_ERRNO(e);
	}
	h = realloc(buffer, readdata + 1);
	if (h == NULL) {
#ifdef SPLINT
		h = NULL;
#endif
		if (readdata >= buffersize) {
			free(buffer);
			return RET_ERROR_OOM;
		}
	} else
		buffer = h;
	buffer[readdata] = '\0';
	*data = buffer;
	if (len != NULL)
		*len = readdata;
	return RET_OK;
}

retvalue readtextfile(const char *source, const char *sourcetoshow, char **data, size_t *len) {
	int fd; char *buffer; size_t bufferlen;
	retvalue r;
	int ret;

	fd = open(source, O_RDONLY|O_NOCTTY);
	if (fd < 0) {
		int e = errno;
		fprintf(stderr, "Error opening '%s': %s\n",
				sourcetoshow, strerror(e));
		return RET_ERRNO(e);
	}
	r = readtextfilefd(fd, sourcetoshow, &buffer, &bufferlen);
	if (!RET_IS_OK(r)) {
		(void)close(fd);
		return r;
	}
	ret = close(fd);
	if (ret != 0) {
		int e = errno;
		free(buffer);
		fprintf(stderr, "Error reading %s: %s\n", sourcetoshow,
				strerror(e));
		return RET_ERRNO(e);
	}
	*data = buffer;
	if (len != NULL)
		*len = bufferlen;
	return RET_OK;
}