2 * lmo - Lua Machine Objects - Base functions
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.
21 extern char _lmo_error[1024];
23 static int lmo_read32( int fd, uint32_t *val )
27 if( read(fd, buffer, 4) < 4 )
31 *val = ntohl(*((uint32_t *) buffer));
36 static char * error(const char *message, int add_errno)
38 memset(_lmo_error, 0, sizeof(_lmo_error));
41 snprintf(_lmo_error, sizeof(_lmo_error),
42 "%s: %s", message, strerror(errno));
44 snprintf(_lmo_error, sizeof(_lmo_error), "%s", message);
49 const char * lmo_error(void)
54 lmo_archive_t * lmo_open(const char *file)
57 uint32_t idx_offset = 0;
61 lmo_archive_t *ar = NULL;
62 lmo_entry_t *head = NULL;
63 lmo_entry_t *entry = NULL;
65 if( stat(file, &s) == -1 )
67 error("Can not stat file", 1);
71 if( (in = open(file, O_RDONLY)) == -1 )
73 error("Can not open file", 1);
77 if( lseek(in, -sizeof(uint32_t), SEEK_END) == -1 )
79 error("Can not seek to eof", 1);
83 if( lmo_read32(in, &idx_offset) != 4 )
85 error("Unexpected EOF while reading index offset", 0);
89 if( lseek(in, idx_offset, SEEK_SET) == -1 )
91 error("Can not seek to index offset", 1);
95 if( (ar = (lmo_archive_t *) malloc(sizeof(lmo_archive_t))) != NULL )
98 ar->length = idx_offset;
101 i < (s.st_size - sizeof(uint32_t));
102 i += (4 * sizeof(uint32_t))
104 if( (entry = (lmo_entry_t *) malloc(sizeof(lmo_entry_t))) != NULL )
106 if( (lmo_read32(ar->fd, &entry->key_id) == 4) &&
107 (lmo_read32(ar->fd, &entry->val_id) == 4) &&
108 (lmo_read32(ar->fd, &entry->offset) == 4) &&
109 (lmo_read32(ar->fd, &entry->length) == 4)
116 error("Unexpected EOF while reading index entry", 0);
122 error("Out of memory", 0);
129 if( lseek(ar->fd, 0, SEEK_SET) == -1 )
131 error("Can not seek to start", 1);
135 if( (ar->mmap = mmap(NULL, ar->length, PROT_READ, MAP_PRIVATE, ar->fd, 0)) == MAP_FAILED )
137 error("Failed to memory map archive contents", 1);
145 error("Out of memory", 0);
159 while( entry != NULL )
171 if( (ar->mmap != NULL) && (ar->mmap != MAP_FAILED) )
172 munmap(ar->mmap, ar->length);
181 void lmo_close(lmo_archive_t *ar)
183 lmo_entry_t *head = NULL;
184 lmo_entry_t *entry = NULL;
190 while( entry != NULL )
199 if( (ar->mmap != NULL) && (ar->mmap != MAP_FAILED) )
200 munmap(ar->mmap, ar->length);
209 int lmo_lookup(lmo_archive_t *ar, const char *key, char *dest, int len)
211 uint32_t look_key = sfh_hash(key, strlen(key));
214 lmo_entry_t *entry = ar->index;
216 while( entry != NULL )
218 if( entry->key_id == look_key )
220 copy_len = (len > entry->length) ? entry->length : len;
221 memcpy(dest, &ar->mmap[entry->offset], copy_len);