summaryrefslogtreecommitdiffhomepage
path: root/contrib/lar/lar.c
blob: f968634af614ad8be749c4ffe08f4827402ae171 (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
#include "lar.h"

int lar_read32( int fd, uint32_t *val )
{
	uint8_t buffer[5];

	if( read(fd, buffer, 4) < 4 )
		LAR_DIE("Unexpected EOF while reading data");

	buffer[4] = 0;
	*val = ntohl(*((uint32_t *) buffer));

	return 0;
}

int lar_read16( int fd, uint16_t *val )
{
	uint8_t buffer[3];

	if( read(fd, buffer, 2) < 2 )
		LAR_DIE("Unexpected EOF while reading data");

	buffer[2] = 0;
	*val = ntohs(*((uint16_t *) buffer));

	return 0;
}

lar_index * lar_get_index( lar_archive *ar )
{
	uint32_t i;
	uint32_t idx_offset;
	uint32_t idx_length;
	lar_index *idx_map;
	lar_index *idx_ptr;

	if( lseek(ar->fd, -(sizeof(idx_offset)), SEEK_END) == -1 )
		LAR_DIE("Unable to seek to end of archive");

	lar_read32(ar->fd, &idx_offset);
	idx_length = ( ar->length - idx_offset - sizeof(idx_offset) );

	if( lseek(ar->fd, idx_offset, SEEK_SET) == -1 )
		LAR_DIE("Unable to seek to archive index");


	idx_map = NULL;

	for( i = 0; i < idx_length; i += (sizeof(lar_index) - sizeof(char *)) ) {
		idx_ptr = (lar_index *)malloc(sizeof(lar_index));

		lar_read32(ar->fd, &idx_ptr->noffset);
		lar_read32(ar->fd, &idx_ptr->nlength);
		lar_read32(ar->fd, &idx_ptr->foffset);
		lar_read32(ar->fd, &idx_ptr->flength);
		lar_read16(ar->fd, &idx_ptr->type);
		lar_read16(ar->fd, &idx_ptr->flags);

		idx_ptr->next = idx_map;
		idx_map = idx_ptr;
	}

	return idx_map;
}

uint32_t lar_get_filename( lar_archive *ar, lar_index *idx_ptr, char *filename )
{
	if( idx_ptr->nlength >= LAR_FNAME_BUFFER )
		LAR_DIE("Filename exceeds maximum allowed length");

	if( lseek(ar->fd, idx_ptr->noffset, SEEK_SET) == -1 )
		LAR_DIE("Unexpected EOF while seeking filename");

	if( read(ar->fd, filename, idx_ptr->nlength) < idx_ptr->nlength )
		LAR_DIE("Unexpected EOF while reading filename");

	filename[idx_ptr->nlength] = '\0';

	return idx_ptr->nlength;
}

lar_member * lar_open_member( lar_archive *ar, const char *name )
{
	lar_index *idx_ptr = ar->index;
	lar_member *member;
	char *memberdata;
	size_t pgof;
	size_t pgsz = getpagesize();
	LAR_FNAME(memberfile);

	while(idx_ptr)
	{
		lar_get_filename(ar, idx_ptr, memberfile);

		if( !strncmp(memberfile, name, idx_ptr->nlength) )
		{
			pgof = ( idx_ptr->foffset % pgsz );

			memberdata = mmap(
				0, idx_ptr->flength + pgof, PROT_READ, MAP_PRIVATE,
				ar->fd, idx_ptr->foffset - pgof
			);

			if( memberdata == MAP_FAILED )
				LAR_DIE("Failed to mmap() member data");

			member = (lar_member *)malloc(sizeof(lar_member));
			member->type   = idx_ptr->type;
			member->flags  = idx_ptr->flags;
			member->length = idx_ptr->flength;
			member->data   = &memberdata[pgof];

			member->mmap   = memberdata;
			member->mlen   = idx_ptr->flength + pgof;

			return member;
		}

		idx_ptr = idx_ptr->next;
	}

	return NULL;
}

int lar_close_member( lar_member *member )
{
	int stat = munmap(member->mmap, member->mlen);
	free(member);

	return stat;
}

lar_archive * lar_open( const char *filename )
{
	int fd;
	struct stat as;
	lar_archive *ar;

	if( stat(filename, &as) == -1 )
		return NULL;

	if( !(as.st_mode & S_IFREG) )
		return NULL;

	if( (fd = open(filename, O_RDONLY)) != -1 )
	{
		ar = (lar_archive *)malloc(sizeof(lar_archive));
		ar->fd       = fd;
		ar->length   = as.st_size;
		ar->index    = lar_get_index(ar);
		strncpy(ar->filename, filename, sizeof(ar->filename));

		return ar;
	}

	return NULL;
}

int lar_close( lar_archive *ar )
{
	lar_index *idx_head;
	lar_index *idx_next;

	close(ar->fd);

	idx_head = ar->index;
	do {
		idx_next = idx_head->next;
		free(idx_head);
	} while( (idx_head = idx_next) != NULL );

	free(ar);

	return 0;
}

lar_archive * lar_find_archive( const char *package, const char *path )
{
	uint32_t i;
	uint32_t j;
	uint32_t seg = 1;
	uint32_t len = 0;
	uint32_t pln = 0;
	struct stat s;
	LAR_FNAME(buffer);

	if( path )
	{
		for( pln = 0; path[pln] != '\0'; pln++ )
			if( pln >= (sizeof(buffer) - 5) )
				LAR_DIE("Library path exceeds maximum allowed length");

		memcpy(buffer, path, pln);
	}

	for( len = 0; package[len] != '\0'; len++ )
	{
		if( len >= (sizeof(buffer) - 5 - pln) )
			LAR_DIE("Package name exceeds maximum allowed length");

		if( package[len] == '.' ) seg++;
	}

	while( seg > 0 )
	{
		for( i = 0, j = 1; (i < len) && (j <= seg); i++ )
		{
			if( package[i] == '.' ) {
				if( j < seg ) j++; else break;
			}

			buffer[pln+i] = ( package[i] == '.' ) ? LAR_DIRSEP : package[i];
		}

		strcpy(&buffer[pln+i], ".lar");

		if( (stat(buffer, &s) > -1) && (s.st_mode & S_IFREG) )
			return lar_open(buffer);

		seg--;
	}

	return NULL;
}

lar_member * lar_find_member( lar_archive *ar, const char *package )
{
	int len;
	LAR_FNAME(buffer);

	for( len = 0; package[len] != '\0'; len++ )
	{
		if( len >= (sizeof(buffer) - 5) )
			LAR_DIE("Package name exceeds maximum allowed length");

		buffer[len] = ( package[len] == '.' ) ? '/' : package[len];
	}

	strcpy(&buffer[len], ".lua");

	return lar_open_member(ar, buffer);
}