2 * lar - Lua Archive Library
4 * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 static int lar_read32( int fd, uint32_t *val )
26 if( read(fd, buffer, 4) < 4 )
27 LAR_DIE("Unexpected EOF while reading data");
30 *val = ntohl(*((uint32_t *) buffer));
35 static int lar_read16( int fd, uint16_t *val )
39 if( read(fd, buffer, 2) < 2 )
40 LAR_DIE("Unexpected EOF while reading data");
43 *val = ntohs(*((uint16_t *) buffer));
48 static void lar_md5( char *md5, const char *data, int len )
53 md5_append(&state, (const md5_byte_t *)data, len);
54 md5_finish(&state, (md5_byte_t *)md5);
57 static int lar_read_filenames( lar_archive *ar )
63 size_t pgsz = getpagesize();
65 lar_index *idx_filelist = ar->index;
69 if( idx_filelist->type == LAR_TYPE_FILELIST )
72 idx_filelist = idx_filelist->next;
75 if( idx_filelist != NULL )
77 pgof = ( idx_filelist->offset % pgsz );
80 0, idx_filelist->length + pgof, PROT_READ, MAP_PRIVATE,
81 ar->fd, idx_filelist->offset - pgof
84 if( filelist == MAP_FAILED )
85 LAR_DIE("Failed to mmap() file list");
93 if( idx_ptr->type == LAR_TYPE_REGULAR )
95 j = strlen(&filelist[i]) + 1;
97 if( (j >= LAR_FNAME_BUFFER) ||
98 ((i+j) > (idx_filelist->length+pgof)) )
99 LAR_DIE("Filename exceeds maximum allowed length");
101 idx_ptr->filename = (char *)malloc(j);
102 memcpy(idx_ptr->filename, &filelist[i], j);
107 idx_ptr = idx_ptr->next;
110 munmap(filelist, idx_filelist->length + pgof);
118 lar_index * lar_get_index( lar_archive *ar )
126 if( lseek(ar->fd, -(sizeof(idx_offset)), SEEK_END) == -1 )
127 LAR_DIE("Unable to seek to end of archive");
129 lar_read32(ar->fd, &idx_offset);
130 idx_length = ( ar->length - idx_offset - sizeof(idx_offset) );
132 if( lseek(ar->fd, idx_offset, SEEK_SET) == -1 )
133 LAR_DIE("Unable to seek to archive index");
138 for( i = 0; i < idx_length; i += (sizeof(lar_index) - 2 * sizeof(char *)) )
140 idx_ptr = (lar_index *)malloc(sizeof(lar_index));
141 idx_ptr->filename = NULL;
143 lar_read32(ar->fd, &idx_ptr->offset);
144 lar_read32(ar->fd, &idx_ptr->length);
145 lar_read16(ar->fd, &idx_ptr->type);
146 lar_read16(ar->fd, &idx_ptr->flags);
148 if(read(ar->fd,&idx_ptr->id,sizeof(idx_ptr->id)) < sizeof(idx_ptr->id))
149 LAR_DIE("Unexpected EOF while reading member id");
151 idx_ptr->next = idx_map;
158 lar_member * lar_mmap_member( lar_archive *ar, lar_index *idx_ptr )
161 size_t pgsz = getpagesize();
162 size_t pgof = ( idx_ptr->offset % pgsz );
164 char *memberdata = mmap(
165 0, idx_ptr->length + pgof, PROT_READ, MAP_PRIVATE,
166 ar->fd, idx_ptr->offset - pgof
169 if( memberdata == MAP_FAILED )
170 LAR_DIE("Failed to mmap() member data");
172 member = (lar_member *)malloc(sizeof(lar_member));
173 member->type = idx_ptr->type;
174 member->flags = idx_ptr->flags;
175 member->length = idx_ptr->length;
176 member->data = &memberdata[pgof];
178 member->mmap = memberdata;
179 member->mlen = idx_ptr->length + pgof;
184 lar_member * lar_open_member( lar_archive *ar, const char *name )
186 lar_index *idx_ptr = ar->index;
187 char mbid[sizeof(idx_ptr->id)];
189 lar_md5(mbid, name, strlen(name));
193 if( !strncmp(mbid, idx_ptr->id, sizeof(mbid)) )
194 return lar_mmap_member(ar, idx_ptr);
196 idx_ptr = idx_ptr->next;
202 int lar_close_member( lar_member *member )
204 int stat = munmap(member->mmap, member->mlen);
211 lar_archive * lar_open( const char *filename )
217 if( stat(filename, &as) == -1 )
220 if( !(as.st_mode & S_IFREG) )
223 if( (fd = open(filename, O_RDONLY)) != -1 )
225 ar = (lar_archive *)malloc(sizeof(lar_archive));
227 ar->length = as.st_size;
228 ar->index = lar_get_index(ar);
229 strncpy(ar->filename, filename, sizeof(ar->filename));
231 ar->has_filenames = lar_read_filenames(ar);
239 int lar_close( lar_archive *ar )
246 idx_head = ar->index;
248 idx_next = idx_head->next;
249 free(idx_head->filename);
251 } while( (idx_head = idx_next) != NULL );
259 lar_archive * lar_find_archive( const char *package, const char *path, int pkg )
266 char sep = ( pkg ? '.' : '/' );
272 for( pln = 0; path[pln] != '\0'; pln++ )
273 if( pln >= (sizeof(buffer) - 5) )
274 LAR_DIE("Library path exceeds maximum allowed length");
276 memcpy(buffer, path, pln);
279 if( buffer[pln-1] != '/' )
282 for( len = 0; package[len] != '\0'; len++ )
284 if( len >= (sizeof(buffer) - 5 - pln) )
285 LAR_DIE("Package name exceeds maximum allowed length");
287 if( package[len] == sep ) seg++;
292 for( i = 0, j = 1; (i < len) && (j <= seg); i++ )
294 if( package[i] == sep ) {
295 if( j < seg ) j++; else break;
298 buffer[pln+i] = ( package[i] == sep ) ? LAR_DIRSEP : package[i];
301 strcpy(&buffer[pln+i], ".lar");
303 if( (stat(buffer, &s) > -1) && (s.st_mode & S_IFREG) )
304 return lar_open(buffer);
312 lar_member * lar_find_member( lar_archive *ar, const char *package )
317 for( len = 0; package[len] != '\0'; len++ )
319 if( len >= (sizeof(buffer) - 5) )
320 LAR_DIE("Package name exceeds maximum allowed length");
322 buffer[len] = ( package[len] == '.' ) ? '/' : package[len];
325 strcpy(&buffer[len], ".lua");
327 return lar_open_member(ar, buffer);