diff options
Diffstat (limited to 'apps')
-rw-r--r-- | apps/open_plugin.c | 144 | ||||
-rw-r--r-- | apps/open_plugin.h | 3 |
2 files changed, 64 insertions, 83 deletions
diff --git a/apps/open_plugin.c b/apps/open_plugin.c index fe18071454..d16502ecbe 100644 --- a/apps/open_plugin.c +++ b/apps/open_plugin.c | |||
@@ -42,10 +42,6 @@ static const uint32_t open_plugin_csum = OPEN_PLUGIN_CHECKSUM; | |||
42 | 42 | ||
43 | static const int op_entry_sz = sizeof(struct open_plugin_entry_t); | 43 | static const int op_entry_sz = sizeof(struct open_plugin_entry_t); |
44 | 44 | ||
45 | static int open_plugin_hash_get_entry(uint32_t hash, | ||
46 | struct open_plugin_entry_t *entry, | ||
47 | const char* dat_file); | ||
48 | |||
49 | static const char* strip_rockbox_root(const char *path) | 45 | static const char* strip_rockbox_root(const char *path) |
50 | { | 46 | { |
51 | int dlen = strlen(ROCKBOX_DIR); | 47 | int dlen = strlen(ROCKBOX_DIR); |
@@ -64,7 +60,7 @@ static inline void op_clear_entry(struct open_plugin_entry_t *entry) | |||
64 | 60 | ||
65 | static int op_entry_checksum(struct open_plugin_entry_t *entry) | 61 | static int op_entry_checksum(struct open_plugin_entry_t *entry) |
66 | { | 62 | { |
67 | if (!entry || entry->checksum != open_plugin_csum) | 63 | if (entry == NULL || entry->checksum != open_plugin_csum) |
68 | return 0; | 64 | return 0; |
69 | return 1; | 65 | return 1; |
70 | } | 66 | } |
@@ -73,38 +69,43 @@ static int op_find_entry(int fd, struct open_plugin_entry_t *entry, | |||
73 | uint32_t hash, int32_t lang_id) | 69 | uint32_t hash, int32_t lang_id) |
74 | { | 70 | { |
75 | int ret = OPEN_PLUGIN_NOT_FOUND; | 71 | int ret = OPEN_PLUGIN_NOT_FOUND; |
76 | int record = -1; | 72 | int record = 0; |
77 | if (fd >= 0 && entry != NULL) | 73 | if (hash == 0) |
74 | hash = OPEN_PLUGIN_SEED; | ||
75 | if (fd >= 0) | ||
78 | { | 76 | { |
79 | logf("OP find_entry *Searching* hash: %x lang_id: %d", hash, lang_id); | 77 | logf("OP find_entry *Searching* hash: %x lang_id: %d", hash, lang_id); |
78 | |||
80 | while (read(fd, entry, op_entry_sz) == op_entry_sz) | 79 | while (read(fd, entry, op_entry_sz) == op_entry_sz) |
81 | { | 80 | { |
82 | record++; | 81 | if (entry->lang_id == lang_id || entry->hash == hash || |
83 | if (entry->lang_id == lang_id || | ||
84 | (entry->hash == hash && hash != 0) || | ||
85 | (lang_id == OPEN_PLUGIN_LANG_IGNOREALL))/* return first entry found */ | 82 | (lang_id == OPEN_PLUGIN_LANG_IGNOREALL))/* return first entry found */ |
86 | { | 83 | { |
87 | /* sanity check */ | 84 | ret = record; |
88 | if (op_entry_checksum(entry) <= 0) | ||
89 | { | ||
90 | splashf(HZ * 2, "OpenPlugin Invalid entry"); | ||
91 | ret = OPEN_PLUGIN_NOT_FOUND; | ||
92 | break; | ||
93 | } | ||
94 | /* NULL terminate fields NOTE -- all are actually +1 larger */ | 85 | /* NULL terminate fields NOTE -- all are actually +1 larger */ |
95 | entry->name[OPEN_PLUGIN_NAMESZ] = '\0'; | 86 | entry->name[OPEN_PLUGIN_NAMESZ] = '\0'; |
96 | /*entry->key[OPEN_PLUGIN_BUFSZ] = '\0';*/ | 87 | /*entry->key[OPEN_PLUGIN_BUFSZ] = '\0';*/ |
97 | entry->path[OPEN_PLUGIN_BUFSZ] = '\0'; | 88 | entry->path[OPEN_PLUGIN_BUFSZ] = '\0'; |
98 | entry->param[OPEN_PLUGIN_BUFSZ] = '\0'; | 89 | entry->param[OPEN_PLUGIN_BUFSZ] = '\0'; |
99 | ret = record; | ||
100 | logf("OP find_entry *Found* hash: %x lang_id: %d", | 90 | logf("OP find_entry *Found* hash: %x lang_id: %d", |
101 | entry->hash, entry->lang_id); | 91 | entry->hash, entry->lang_id); |
102 | logf("OP find_entry rec: %d name: %s %s %s", record, | 92 | logf("OP find_entry rec: %d name: %s %s %s", record, |
103 | entry->name, entry->path, entry->param); | 93 | entry->name, entry->path, entry->param); |
104 | break; | 94 | break; |
105 | } | 95 | } |
96 | record++; | ||
106 | } | 97 | } |
107 | } | 98 | } |
99 | |||
100 | /* sanity check */ | ||
101 | if (ret > OPEN_PLUGIN_NOT_FOUND && op_entry_checksum(entry) <= 0) | ||
102 | { | ||
103 | splash(HZ * 2, "OpenPlugin Invalid entry"); | ||
104 | ret = OPEN_PLUGIN_NOT_FOUND; | ||
105 | } | ||
106 | if (ret == OPEN_PLUGIN_NOT_FOUND) | ||
107 | op_clear_entry(entry); | ||
108 | |||
108 | return ret; | 109 | return ret; |
109 | } | 110 | } |
110 | 111 | ||
@@ -113,7 +114,7 @@ static int op_update_dat(struct open_plugin_entry_t *entry, bool clear) | |||
113 | int fd; | 114 | int fd; |
114 | uint32_t hash; | 115 | uint32_t hash; |
115 | int32_t lang_id; | 116 | int32_t lang_id; |
116 | if (!entry || entry->hash == 0) | 117 | if (entry == NULL|| entry->hash == 0) |
117 | { | 118 | { |
118 | logf("OP update *No entry*"); | 119 | logf("OP update *No entry*"); |
119 | return OPEN_PLUGIN_NOT_FOUND; | 120 | return OPEN_PLUGIN_NOT_FOUND; |
@@ -178,8 +179,7 @@ static int op_update_dat(struct open_plugin_entry_t *entry, bool clear) | |||
178 | { | 179 | { |
179 | logf("OP update *Loading original entry*"); | 180 | logf("OP update *Loading original entry*"); |
180 | lseek(fd, 0, SEEK_SET); | 181 | lseek(fd, 0, SEEK_SET); |
181 | int opret = op_find_entry(fd, entry, hash, lang_id); | 182 | op_find_entry(fd, entry, hash, lang_id); |
182 | clear = (opret == OPEN_PLUGIN_NOT_FOUND); | ||
183 | } | 183 | } |
184 | close(fd); | 184 | close(fd); |
185 | rename(OPEN_PLUGIN_DAT ".tmp", OPEN_PLUGIN_DAT); | 185 | rename(OPEN_PLUGIN_DAT ".tmp", OPEN_PLUGIN_DAT); |
@@ -194,6 +194,41 @@ static int op_update_dat(struct open_plugin_entry_t *entry, bool clear) | |||
194 | return 0; | 194 | return 0; |
195 | } | 195 | } |
196 | 196 | ||
197 | static int op_get_entry(uint32_t hash, int32_t lang_id, | ||
198 | struct open_plugin_entry_t *entry, const char *dat_file) | ||
199 | { | ||
200 | int opret = OPEN_PLUGIN_NOT_FOUND; | ||
201 | |||
202 | if (entry != NULL) | ||
203 | { | ||
204 | /* Is the entry we want already loaded? */ | ||
205 | if(hash != 0 && entry->hash == hash) | ||
206 | return OPEN_PLUGIN_NEEDS_FLUSHED; | ||
207 | |||
208 | if(lang_id <= OPEN_PLUGIN_LANG_INVALID) | ||
209 | { | ||
210 | lang_id = OPEN_PLUGIN_LANG_IGNORE; | ||
211 | if (hash == 0)/* no hash or langid -- returns first entry found */ | ||
212 | lang_id = OPEN_PLUGIN_LANG_IGNOREALL; | ||
213 | } | ||
214 | else if(entry->lang_id == lang_id) | ||
215 | { | ||
216 | return OPEN_PLUGIN_NEEDS_FLUSHED; | ||
217 | } | ||
218 | |||
219 | /* if another entry is loaded; flush it to disk before we destroy it */ | ||
220 | op_update_dat(&open_plugin_entry, true); | ||
221 | |||
222 | logf("OP get_entry hash: %x lang id: %d db: %s", hash, lang_id, dat_file); | ||
223 | |||
224 | int fd = open(dat_file, O_RDONLY); | ||
225 | opret = op_find_entry(fd, entry, hash, lang_id); | ||
226 | close(fd); | ||
227 | } | ||
228 | |||
229 | return opret; | ||
230 | } | ||
231 | |||
197 | uint32_t open_plugin_add_path(const char *key, const char *plugin, const char *parameter) | 232 | uint32_t open_plugin_add_path(const char *key, const char *plugin, const char *parameter) |
198 | { | 233 | { |
199 | int len; | 234 | int len; |
@@ -201,7 +236,7 @@ uint32_t open_plugin_add_path(const char *key, const char *plugin, const char *p | |||
201 | int32_t lang_id; | 236 | int32_t lang_id; |
202 | char *pos = "\0"; | 237 | char *pos = "\0"; |
203 | 238 | ||
204 | if(!key) | 239 | if(key == NULL) |
205 | { | 240 | { |
206 | logf("OP add_path No Key, *Clearing entry*"); | 241 | logf("OP add_path No Key, *Clearing entry*"); |
207 | op_clear_entry(&open_plugin_entry); | 242 | op_clear_entry(&open_plugin_entry); |
@@ -242,7 +277,7 @@ uint32_t open_plugin_add_path(const char *key, const char *plugin, const char *p | |||
242 | } | 277 | } |
243 | else if (len > OP_LEN && strcasecmp(&(pos[len-OP_LEN]), "." OP_EXT) == 0) | 278 | else if (len > OP_LEN && strcasecmp(&(pos[len-OP_LEN]), "." OP_EXT) == 0) |
244 | { | 279 | { |
245 | open_plugin_hash_get_entry(0, &open_plugin_entry, plugin); | 280 | op_get_entry(0, OPEN_PLUGIN_LANG_IGNORE, &open_plugin_entry, plugin); |
246 | goto retnhash; | 281 | goto retnhash; |
247 | } | 282 | } |
248 | } | 283 | } |
@@ -285,72 +320,17 @@ void open_plugin_browse(const char *key) | |||
285 | open_plugin_add_path(key, tmp_buf, NULL); | 320 | open_plugin_add_path(key, tmp_buf, NULL); |
286 | } | 321 | } |
287 | 322 | ||
288 | static int op_get_entry(uint32_t hash, int32_t lang_id, | ||
289 | struct open_plugin_entry_t *entry, const char *dat_file) | ||
290 | { | ||
291 | int opret = OPEN_PLUGIN_NOT_FOUND; | ||
292 | |||
293 | if (entry) | ||
294 | { | ||
295 | /* Is the entry we want already loaded? */ | ||
296 | if(hash != 0 && entry->hash == hash) | ||
297 | return OPEN_PLUGIN_NEEDS_FLUSHED; | ||
298 | |||
299 | if(lang_id <= OPEN_PLUGIN_LANG_INVALID) | ||
300 | { | ||
301 | lang_id = OPEN_PLUGIN_LANG_IGNORE; | ||
302 | if (hash == 0)/* no hash or langid -- returns first entry found */ | ||
303 | lang_id = OPEN_PLUGIN_LANG_IGNOREALL; | ||
304 | } | ||
305 | else if(entry->lang_id == lang_id) | ||
306 | { | ||
307 | return OPEN_PLUGIN_NEEDS_FLUSHED; | ||
308 | } | ||
309 | |||
310 | /* if another entry is loaded; flush it to disk before we destroy it */ | ||
311 | op_update_dat(&open_plugin_entry, true); | ||
312 | |||
313 | logf("OP get_entry hash: %x lang id: %d db: %s", hash, lang_id, dat_file); | ||
314 | |||
315 | int fd = open(dat_file, O_RDONLY); | ||
316 | opret = op_find_entry(fd, entry, hash, lang_id); | ||
317 | close(fd); | ||
318 | |||
319 | if (opret < 0) /* nothing found */ | ||
320 | { | ||
321 | op_clear_entry(entry); | ||
322 | } | ||
323 | } | ||
324 | |||
325 | return opret; | ||
326 | } | ||
327 | |||
328 | #if 0 //unused | ||
329 | static int open_plugin_langid_get_entry(int32_t lang_id, | ||
330 | struct open_plugin_entry_t *entry, | ||
331 | const char *dat_file) | ||
332 | { | ||
333 | return op_get_entry(0, lang_id, entry, dat_file); | ||
334 | } | ||
335 | #endif | ||
336 | |||
337 | static int open_plugin_hash_get_entry(uint32_t hash, | ||
338 | struct open_plugin_entry_t *entry, | ||
339 | const char *dat_file) | ||
340 | { | ||
341 | return op_get_entry(hash, OPEN_PLUGIN_LANG_IGNORE, entry, dat_file); | ||
342 | } | ||
343 | |||
344 | int open_plugin_get_entry(const char *key, struct open_plugin_entry_t *entry) | 323 | int open_plugin_get_entry(const char *key, struct open_plugin_entry_t *entry) |
345 | { | 324 | { |
346 | if (!key || !entry) | 325 | if (key == NULL || entry == NULL) |
347 | return OPEN_PLUGIN_NOT_FOUND; | 326 | return OPEN_PLUGIN_NOT_FOUND; |
348 | int opret; | 327 | int opret; |
349 | uint32_t hash; | 328 | uint32_t hash = 0; |
350 | int32_t lang_id = P2ID((unsigned char *)key); | 329 | int32_t lang_id = P2ID((unsigned char *)key); |
351 | const char* skey = P2STR((unsigned char *)key); /* string|LANGPTR => string */ | 330 | const char* skey = P2STR((unsigned char *)key); /* string|LANGPTR => string */ |
352 | 331 | ||
353 | open_plugin_get_hash(strip_rockbox_root(skey), &hash); /* in open_plugin.h */ | 332 | if (lang_id <= OPEN_PLUGIN_LANG_INVALID) |
333 | open_plugin_get_hash(strip_rockbox_root(skey), &hash); /* in open_plugin.h */ | ||
354 | 334 | ||
355 | opret = op_get_entry(hash, lang_id, entry, OPEN_PLUGIN_DAT); | 335 | opret = op_get_entry(hash, lang_id, entry, OPEN_PLUGIN_DAT); |
356 | logf("OP entry hash: %x lang id: %d ret: %d key: %s", hash, lang_id, opret, skey); | 336 | logf("OP entry hash: %x lang id: %d ret: %d key: %s", hash, lang_id, opret, skey); |
diff --git a/apps/open_plugin.h b/apps/open_plugin.h index adfb9a75bc..d16be2052c 100644 --- a/apps/open_plugin.h +++ b/apps/open_plugin.h | |||
@@ -67,11 +67,12 @@ struct open_plugin_entry_t | |||
67 | offsetof(struct open_plugin_entry_t, path) + \ | 67 | offsetof(struct open_plugin_entry_t, path) + \ |
68 | offsetof(struct open_plugin_entry_t, param)) | 68 | offsetof(struct open_plugin_entry_t, param)) |
69 | 69 | ||
70 | #define OPEN_PLUGIN_SEED 0x811C9DC5; //seed, 2166136261; | ||
70 | inline static void open_plugin_get_hash(const char *key, uint32_t *hash) | 71 | inline static void open_plugin_get_hash(const char *key, uint32_t *hash) |
71 | { | 72 | { |
72 | /* Calculate modified FNV1a hash of string */ | 73 | /* Calculate modified FNV1a hash of string */ |
73 | const uint32_t p = 16777619; | 74 | const uint32_t p = 16777619; |
74 | *hash = 0x811C9DC5; //seed, 2166136261; | 75 | *hash = OPEN_PLUGIN_SEED; |
75 | while(*key) | 76 | while(*key) |
76 | *hash = (*key++ ^ *hash) * p; | 77 | *hash = (*key++ ^ *hash) * p; |
77 | } | 78 | } |