/[zanavi_public1]/navit/navit/map/binfile/binfile.c
ZANavi

Contents of /navit/navit/map/binfile/binfile.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (hide annotations) (download)
Mon Feb 4 17:41:59 2013 UTC (7 years, 7 months ago) by zoff99
File MIME type: text/plain
File size: 76563 byte(s)
new map version, lots of fixes and experimental new features
1 zoff99 2 /**
2 zoff99 27 * ZANavi, Zoff Android Navigation system.
3     * Copyright (C) 2011-2012 Zoff <zoff@zoff.cc>
4     *
5     * This program is free software; you can redistribute it and/or
6     * modify it under the terms of the GNU General Public License
7     * version 2 as published by the Free Software Foundation.
8     *
9     * This program is distributed in the hope that it will be useful,
10     * but WITHOUT ANY WARRANTY; without even the implied warranty of
11     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     * GNU General Public License for more details.
13     *
14     * You should have received a copy of the GNU General Public License
15     * along with this program; if not, write to the
16     * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     * Boston, MA 02110-1301, USA.
18     */
19    
20     /**
21 zoff99 2 * Navit, a modular navigation system.
22     * Copyright (C) 2005-2008 Navit Team
23     *
24     * This program is free software; you can redistribute it and/or
25     * modify it under the terms of the GNU General Public License
26     * version 2 as published by the Free Software Foundation.
27     *
28     * This program is distributed in the hope that it will be useful,
29     * but WITHOUT ANY WARRANTY; without even the implied warranty of
30     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31     * GNU General Public License for more details.
32     *
33     * You should have received a copy of the GNU General Public License
34     * along with this program; if not, write to the
35     * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
36     * Boston, MA 02110-1301, USA.
37     */
38    
39     #include <stdlib.h>
40     #include <glib.h>
41     #include <stdio.h>
42     #include <string.h>
43     #include <math.h>
44     #include "config.h"
45     #include "debug.h"
46     #include "plugin.h"
47     #include "projection.h"
48     #include "item.h"
49     #include "map.h"
50     #include "maptype.h"
51     #include "attr.h"
52     #include "coord.h"
53     #include "transform.h"
54     #include "file.h"
55     #include "zipfile.h"
56     #include "linguistics.h"
57     #include "endianess.h"
58     #include "callback.h"
59     #include "types.h"
60 zoff99 15 #include "navit.h"
61 zoff99 2
62     static int map_id;
63    
64 zoff99 27 struct tile
65     {
66 zoff99 2 int *start;
67     int *end;
68     int *pos;
69     int *pos_coord_start;
70     int *pos_coord;
71     int *pos_attr_start;
72     int *pos_attr;
73     int *pos_next;
74     struct file *fi;
75     int zipfile_num;
76     int mode;
77     };
78    
79 zoff99 27 struct map_download
80     {
81 zoff99 2 int state;
82     struct map_priv *m;
83     struct map_rect_priv *mr;
84 zoff99 27 struct file *http, *file;
85     int zipfile, toffset, tlength, progress, read, dl_size;
86     long long offset, start_offset, cd1offset, size;
87 zoff99 2 struct zip64_eoc *zip64_eoc;
88     struct zip64_eocl *zip64_eocl;
89     struct zip_eoc *zip_eoc;
90 zoff99 27 struct zip_cd *cd_copy, *cd;
91 zoff99 2 };
92    
93 zoff99 27 struct map_priv
94     {
95 zoff99 2 int id;
96     char *filename;
97     char *cachedir;
98 zoff99 27 struct file *fi, *http;
99 zoff99 2 struct file **fis;
100     struct zip_cd *index_cd;
101     int index_offset;
102     int cde_size;
103     struct zip_eoc *eoc;
104     struct zip64_eoc *eoc64;
105     int zip_members;
106     unsigned char *search_data;
107     int search_offset;
108     int search_size;
109     int version;
110     int check_version;
111     int map_version;
112     GHashTable *changes;
113     char *passwd;
114     char *map_release;
115     int flags;
116     char *url;
117     int update_available;
118     char *progress;
119     struct callback_list *cbl;
120     struct map_download *download;
121     int redirect;
122     long download_enabled;
123     };
124    
125 zoff99 27 struct map_rect_priv
126     {
127 zoff99 2 int *start;
128     int *end;
129     enum attr_type attr_last;
130     int label;
131     int *label_attr[5];
132 zoff99 27 struct map_selection *sel;
133     struct map_priv *m;
134     struct item item;
135 zoff99 2 int tile_depth;
136     struct tile tiles[8];
137     struct tile *t;
138     int country_id;
139     char *url;
140     struct attr attrs[8];
141     int status;
142     #ifdef DEBUG_SIZE
143 zoff99 27 int size;
144 zoff99 2 #endif
145     };
146    
147 zoff99 27 struct map_search_priv
148     {
149 zoff99 2 struct map_priv *map;
150     struct map_rect_priv *mr;
151     struct map_rect_priv *mr_item;
152     struct item *item;
153     struct attr *search;
154     struct map_selection ms;
155     int partial;
156     int mode;
157     GHashTable *search_results;
158     char *str;
159     };
160    
161     static void push_tile(struct map_rect_priv *mr, struct tile *t, int offset, int length);
162     static void setup_pos(struct map_rect_priv *mr);
163     static void map_binfile_close(struct map_priv *m);
164     static int map_binfile_open(struct map_priv *m);
165     static void map_binfile_destroy(struct map_priv *m);
166    
167 zoff99 27 static void lfh_to_cpu(struct zip_lfh *lfh)
168     {
169 zoff99 2 dbg_assert(lfh != NULL);
170 zoff99 27 if (lfh->ziplocsig != zip_lfh_sig)
171     {
172     // dbg(0,"_endian 3\n");
173 zoff99 2 lfh->ziplocsig = le32_to_cpu(lfh->ziplocsig);
174 zoff99 27 lfh->zipver = le16_to_cpu(lfh->zipver);
175 zoff99 2 lfh->zipgenfld = le16_to_cpu(lfh->zipgenfld);
176 zoff99 27 lfh->zipmthd = le16_to_cpu(lfh->zipmthd);
177     lfh->ziptime = le16_to_cpu(lfh->ziptime);
178     lfh->zipdate = le16_to_cpu(lfh->zipdate);
179     lfh->zipcrc = le32_to_cpu(lfh->zipcrc);
180     lfh->zipsize = le32_to_cpu(lfh->zipsize);
181     lfh->zipuncmp = le32_to_cpu(lfh->zipuncmp);
182     lfh->zipfnln = le16_to_cpu(lfh->zipfnln);
183 zoff99 2 lfh->zipxtraln = le16_to_cpu(lfh->zipxtraln);
184 zoff99 27 }
185 zoff99 2 }
186    
187 zoff99 27 static void cd_to_cpu(struct zip_cd *zcd)
188     {
189 zoff99 2 dbg_assert(zcd != NULL);
190 zoff99 27 if (zcd->zipcensig != zip_cd_sig)
191     {
192     // dbg(0,"_endian 1\n");
193 zoff99 2 zcd->zipcensig = le32_to_cpu(zcd->zipcensig);
194 zoff99 27 zcd->zipccrc = le32_to_cpu(zcd->zipccrc);
195     zcd->zipcsiz = le32_to_cpu(zcd->zipcsiz);
196     zcd->zipcunc = le32_to_cpu(zcd->zipcunc);
197     zcd->zipcfnl = le16_to_cpu(zcd->zipcfnl);
198     zcd->zipcxtl = le16_to_cpu(zcd->zipcxtl);
199     zcd->zipccml = le16_to_cpu(zcd->zipccml);
200     zcd->zipdsk = le16_to_cpu(zcd->zipdsk);
201     zcd->zipint = le16_to_cpu(zcd->zipint);
202     zcd->zipext = le32_to_cpu(zcd->zipext);
203     zcd->zipofst = le32_to_cpu(zcd->zipofst);
204 zoff99 2 }
205     }
206    
207 zoff99 27 static void eoc_to_cpu(struct zip_eoc *eoc)
208     {
209 zoff99 2 dbg_assert(eoc != NULL);
210 zoff99 27 if (eoc->zipesig != zip_eoc_sig)
211     {
212     // dbg(0,"_endian 2\n");
213     eoc->zipesig = le32_to_cpu(eoc->zipesig);
214     eoc->zipedsk = le16_to_cpu(eoc->zipedsk);
215     eoc->zipecen = le16_to_cpu(eoc->zipecen);
216     eoc->zipenum = le16_to_cpu(eoc->zipenum);
217     eoc->zipecenn = le16_to_cpu(eoc->zipecenn);
218     eoc->zipecsz = le32_to_cpu(eoc->zipecsz);
219     eoc->zipeofst = le32_to_cpu(eoc->zipeofst);
220     eoc->zipecoml = le16_to_cpu(eoc->zipecoml);
221 zoff99 2 }
222     }
223    
224     static void binfile_check_version(struct map_priv *m);
225    
226     static struct zip_eoc *
227     binfile_read_eoc(struct file *fi)
228     {
229     struct zip_eoc *eoc;
230 zoff99 27 eoc = (struct zip_eoc *) file_data_read(fi, fi->size - sizeof(struct zip_eoc), sizeof(struct zip_eoc));
231     if (eoc)
232     {
233 zoff99 2 eoc_to_cpu(eoc);
234 zoff99 27 // dbg(0, "sig 0x%x\n", eoc->zipesig);
235     if (eoc->zipesig != zip_eoc_sig)
236     {
237     file_data_free(fi, (unsigned char *) eoc);
238     eoc = NULL;
239 zoff99 2 }
240     }
241     return eoc;
242     }
243    
244     static struct zip64_eoc *
245     binfile_read_eoc64(struct file *fi)
246     {
247     struct zip64_eocl *eocl;
248     struct zip64_eoc *eoc;
249 zoff99 27 eocl = (struct zip64_eocl *) file_data_read(fi, fi->size - sizeof(struct zip_eoc) - sizeof(struct zip64_eocl), sizeof(struct zip64_eocl));
250 zoff99 2 if (!eocl)
251 zoff99 27 {
252 zoff99 2 return NULL;
253 zoff99 27 }
254     //dbg(0, "sig 0x%x\n", eocl->zip64lsig);
255     if (eocl->zip64lsig != zip64_eocl_sig)
256     {
257     file_data_free(fi, (unsigned char *) eocl);
258     //dbg(0, "eocl wrong\n");
259 zoff99 2 return NULL;
260     }
261 zoff99 27 eoc = (struct zip64_eoc *) file_data_read(fi, eocl->zip64lofst, sizeof(struct zip64_eoc));
262     if (eoc)
263     {
264     if (eoc->zip64esig != zip64_eoc_sig)
265     {
266     file_data_free(fi, (unsigned char *) eoc);
267     //dbg(0, "eoc wrong\n");
268     eoc = NULL;
269 zoff99 2 }
270 zoff99 27 //dbg(0, "eoc64 ok 0x%Lx 0x%Lx\n", eoc->zip64eofst, eoc->zip64ecsz);
271 zoff99 2 }
272 zoff99 27 file_data_free(fi, (unsigned char *) eocl);
273 zoff99 2 return eoc;
274     }
275    
276 zoff99 27 static int binfile_cd_extra(struct zip_cd *cd)
277 zoff99 2 {
278 zoff99 27 return cd->zipcfnl + cd->zipcxtl;
279 zoff99 2 }
280    
281     static struct zip_cd *
282     binfile_read_cd(struct map_priv *m, int offset, int len)
283     {
284 zoff99 27 //dbg(0,"_enter\n");
285 zoff99 2 struct zip_cd *cd;
286 zoff99 27 long long cdoffset = m->eoc64 ? m->eoc64->zip64eofst : m->eoc->zipeofst;
287     //dbg(0,"_cdoffset="LONGLONG_FMT"\n", cdoffset);
288    
289     if (len == -1)
290     {
291     //dbg(0,"_len = -1\n");
292     cd = (struct zip_cd *) file_data_read(m->fi, cdoffset + offset, sizeof(*cd));
293 zoff99 2 cd_to_cpu(cd);
294 zoff99 27 len = binfile_cd_extra(cd);
295     //dbg(0,"_len = %d\n", len);
296     file_data_free(m->fi, (unsigned char *) cd);
297 zoff99 2 }
298 zoff99 27
299     cd = (struct zip_cd *) file_data_read(m->fi, cdoffset + offset, sizeof(*cd) + len);
300    
301     if (cd)
302     {
303     //dbg(0,"cd at "LONGLONG_FMT" %d bytes\n",cdoffset+offset, sizeof(*cd)+len);
304 zoff99 2 cd_to_cpu(cd);
305 zoff99 27 //dbg(0, "sig 0x%x\n", cd->zipcensig);
306     if (cd->zipcensig != zip_cd_sig)
307     {
308     file_data_free(m->fi, (unsigned char *) cd);
309     //dbg(0,"_cd=NULL\n");
310     cd = NULL;
311 zoff99 2 }
312     }
313 zoff99 27
314     //dbg(0,"leave\n");
315 zoff99 2 return cd;
316     }
317    
318     static struct zip_cd_ext *
319     binfile_cd_ext(struct zip_cd *cd)
320     {
321     struct zip_cd_ext *ext;
322     if (cd->zipofst != 0xffffffff)
323     return NULL;
324     if (cd->zipcxtl != sizeof(*ext))
325     return NULL;
326 zoff99 27 ext = (struct zip_cd_ext *) ((unsigned char *) cd + sizeof(*cd) + cd->zipcfnl);
327 zoff99 2 if (ext->tag != 0x0001 || ext->size != 8)
328     return NULL;
329     return ext;
330     }
331    
332 zoff99 27 static long long binfile_cd_offset(struct zip_cd *cd)
333 zoff99 2 {
334 zoff99 27 struct zip_cd_ext *ext = binfile_cd_ext(cd);
335 zoff99 2 if (ext)
336     return ext->zipofst;
337     else
338     return cd->zipofst;
339     }
340    
341     static struct zip_lfh *
342     binfile_read_lfh(struct file *fi, long long offset)
343     {
344     struct zip_lfh *lfh;
345    
346 zoff99 27 lfh = (struct zip_lfh *) (file_data_read(fi, offset, sizeof(struct zip_lfh)));
347     if (lfh)
348     {
349 zoff99 2 lfh_to_cpu(lfh);
350 zoff99 27 if (lfh->ziplocsig != zip_lfh_sig)
351     {
352     file_data_free(fi, (unsigned char *) lfh);
353     lfh = NULL;
354 zoff99 2 }
355     }
356     return lfh;
357     }
358    
359     static unsigned char *
360     binfile_read_content(struct map_priv *m, struct file *fi, long long offset, struct zip_lfh *lfh)
361     {
362     struct zip_enc *enc;
363 zoff99 27 unsigned char *ret = NULL;
364 zoff99 2
365 zoff99 27 offset += sizeof(struct zip_lfh) + lfh->zipfnln;
366     switch (lfh->zipmthd)
367     {
368 zoff99 2 case 0:
369 zoff99 27 offset += lfh->zipxtraln;
370     ret = file_data_read(fi, offset, lfh->zipuncmp);
371 zoff99 2 break;
372     case 8:
373 zoff99 27 offset += lfh->zipxtraln;
374     ret = file_data_read_compressed(fi, offset, lfh->zipsize, lfh->zipuncmp);
375 zoff99 2 break;
376 zoff99 27 case 99:
377     if (!m->passwd)
378     {
379     break;
380     }
381     enc = (struct zip_enc *) file_data_read(fi, offset, sizeof(*enc));
382     offset += lfh->zipxtraln;
383     switch (enc->compress_method)
384     {
385     case 0:
386     ret = file_data_read_encrypted(fi, offset, lfh->zipsize, lfh->zipuncmp, 0, m->passwd);
387     break;
388     case 8:
389     ret = file_data_read_encrypted(fi, offset, lfh->zipsize, lfh->zipuncmp, 1, m->passwd);
390     break;
391     default:
392     // dbg(0, "Unknown encrypted compression method %d\n", enc->compress_method);
393     break;
394     }
395     file_data_free(fi, (unsigned char *) enc);
396     break;
397 zoff99 2 default:
398 zoff99 27 // dbg(0, "Unknown compression method %d\n", lfh->zipmthd);
399     break;
400 zoff99 2 }
401     return ret;
402     }
403    
404 zoff99 27 static int binfile_search_cd(struct map_priv *m, int offset, char *name, int partial, int skip)
405 zoff99 2 {
406 zoff99 27 //dbg(0,"_enter\n");
407    
408     int size = 4096;
409     int end = m->eoc64 ? m->eoc64->zip64ecsz : m->eoc->zipecsz;
410     int len = strlen(name);
411     long long cdoffset = m->eoc64 ? m->eoc64->zip64eofst : m->eoc->zipeofst;
412 zoff99 2 struct zip_cd *cd;
413     #if 0
414 zoff99 27 // dbg(0,"end=%d\n",end);
415 zoff99 2 #endif
416 zoff99 27 while (offset < end)
417     {
418     cd = (struct zip_cd *) (m->search_data + offset - m->search_offset);
419     if (!m->search_data || m->search_offset > offset || offset - m->search_offset + sizeof(*cd) > m->search_size || offset - m->search_offset + sizeof(*cd) + cd->zipcfnl + cd->zipcxtl > m->search_size)
420     {
421 zoff99 2 #if 0
422 zoff99 27 // dbg(0,"reload %p %d %d\n", m->search_data, m->search_offset, offset);
423 zoff99 2 #endif
424     if (m->search_data)
425 zoff99 27 {
426     file_data_free(m->fi, m->search_data);
427     }
428     m->search_offset = offset;
429     m->search_size = end - offset;
430 zoff99 2 if (m->search_size > size)
431 zoff99 27 {
432     m->search_size = size;
433     }
434     m->search_data = file_data_read(m->fi, cdoffset + m->search_offset, m->search_size);
435     cd = (struct zip_cd *) m->search_data;
436 zoff99 2 }
437     #if 0
438 zoff99 27 // dbg(0,"offset=%d search_offset=%d search_size=%d search_data=%p cd=%p\n", offset, m->search_offset, m->search_size, m->search_data, cd);
439     // dbg(0,"offset=%d fn='%s'\n",offset,cd->zipcfn);
440 zoff99 2 #endif
441 zoff99 27 if (!skip && (partial || cd->zipcfnl == len) && !strncmp(cd->zipcfn, name, len))
442     {
443 zoff99 2 return offset;
444 zoff99 27 }
445     skip = 0;
446     offset += sizeof(*cd) + cd->zipcfnl + cd->zipcxtl + cd->zipccml;
447     ;
448 zoff99 2 }
449     return -1;
450     }
451    
452 zoff99 27 static void map_destroy_binfile(struct map_priv *m)
453 zoff99 2 {
454 zoff99 27 // dbg(1, "map_destroy_binfile\n");
455 zoff99 2 if (m->fi)
456     map_binfile_close(m);
457     map_binfile_destroy(m);
458     }
459    
460 zoff99 27 static void binfile_coord_rewind(void *priv_data)
461 zoff99 2 {
462 zoff99 27 struct map_rect_priv *mr = priv_data;
463     struct tile *t = mr->t;
464     t->pos_coord = t->pos_coord_start;
465 zoff99 2 }
466    
467 zoff99 27 static int binfile_coord_get(void *priv_data, struct coord *c, int count)
468 zoff99 2 {
469 zoff99 27 struct map_rect_priv *mr = priv_data;
470     struct tile *t = mr->t;
471     int max, ret = 0;
472     max = (t->pos_attr_start - t->pos_coord) / 2;
473 zoff99 2 if (count > max)
474 zoff99 27 {
475     count = max;
476     }
477 zoff99 2 #if __BYTE_ORDER == __LITTLE_ENDIAN
478 zoff99 27 memcpy(c, t->pos_coord, count * sizeof(struct coord));
479 zoff99 2 #else
480     {
481     int i=0,end=count*sizeof(struct coord)/sizeof(int);
482     int *src=(int *)t->pos_coord;
483     int *dst=(int *)c;
484 zoff99 27 while (i++ < end)
485     {
486 zoff99 2 *dst++=le32_to_cpu(*src);
487     src++;
488     }
489     }
490     #endif
491 zoff99 27 t->pos_coord += count * 2;
492     ret = count;
493 zoff99 2 return ret;
494     }
495    
496 zoff99 27 static void binfile_attr_rewind(void *priv_data)
497 zoff99 2 {
498 zoff99 27 struct map_rect_priv *mr = priv_data;
499     struct tile *t = mr->t;
500     t->pos_attr = t->pos_attr_start;
501     mr->label = 0;
502 zoff99 2 memset(mr->label_attr, 0, sizeof(mr->label_attr));
503 zoff99 27
504 zoff99 2 }
505    
506     static char *
507     binfile_extract(struct map_priv *m, char *dir, char *filename, int partial)
508     {
509 zoff99 27 char *full, *fulld, *sep;
510 zoff99 2 unsigned char *start;
511 zoff99 27 int len, offset = m->index_offset;
512 zoff99 2 struct zip_cd *cd;
513     struct zip_lfh *lfh;
514     FILE *f;
515    
516 zoff99 27 for (;;)
517     {
518     offset = binfile_search_cd(m, offset, filename, partial, 1);
519 zoff99 2 if (offset == -1)
520     break;
521 zoff99 27 cd = binfile_read_cd(m, offset, -1);
522     len = strlen(dir) + 1 + cd->zipcfnl + 1;
523     full = g_malloc(len);
524     strcpy(full, dir);
525     strcpy(full + strlen(full), "/");
526     strncpy(full + strlen(full), cd->zipcfn, cd->zipcfnl);
527     full[len - 1] = '\0';
528     fulld = g_strdup(full);
529     sep = strrchr(fulld, '/');
530     if (sep)
531     {
532     *sep = '\0';
533 zoff99 2 file_mkdir(fulld, 1);
534     }
535 zoff99 27 if (full[len - 2] != '/')
536     {
537     lfh = binfile_read_lfh(m->fi, binfile_cd_offset(cd));
538     start = binfile_read_content(m, m->fi, binfile_cd_offset(cd), lfh);
539     // dbg(0, "fopen '%s'\n", full);
540     f = fopen(full, "w");
541 zoff99 2 fwrite(start, lfh->zipuncmp, 1, f);
542     fclose(f);
543     file_data_free(m->fi, start);
544 zoff99 27 file_data_free(m->fi, (unsigned char *) lfh);
545 zoff99 2 }
546 zoff99 27 file_data_free(m->fi, (unsigned char *) cd);
547 zoff99 2 g_free(fulld);
548     g_free(full);
549 zoff99 27 if (!partial)
550     {
551 zoff99 2 break;
552 zoff99 27 }
553 zoff99 2 }
554 zoff99 27
555     return g_strdup_printf("%s/%s", dir, filename);
556 zoff99 2 }
557    
558 zoff99 27 static int binfile_attr_get(void *priv_data, enum attr_type attr_type, struct attr *attr)
559     {
560 zoff99 31 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
561     dbg(0,"+#+:enter\n");
562     #endif
563    
564     // try to return "0" on any problem, to avoid crash!!
565    
566 zoff99 27 struct map_rect_priv *mr = priv_data;
567     struct tile *t = mr->t;
568 zoff99 2 enum attr_type type;
569 zoff99 27 int i, size;
570 zoff99 2
571 zoff99 27 if (attr_type != mr->attr_last)
572     {
573     t->pos_attr = t->pos_attr_start;
574     mr->attr_last = attr_type;
575 zoff99 2 }
576 zoff99 31
577 zoff99 27 while (t->pos_attr < t->pos_next)
578     {
579 zoff99 31 //dbg(0,"batg:002 %d %d\n",t->pos_attr,(t->pos_attr+1));
580 zoff99 27 size = le32_to_cpu(*(t->pos_attr++));
581 zoff99 31 // dbg(0,"batg:002.1 %d %d\n",t->pos_attr, t->pos_attr[0]);
582 zoff99 27 type = le32_to_cpu(t->pos_attr[0]);
583 zoff99 31 // dbg(0, "type=%d\n", type);
584    
585     #ifdef NAVIT_ATTR_SAFETY_CHECK
586     // safety check (always keep value in sync with "attr_def.h" !!) ---------------
587     if (type > 0x000effff)
588     {
589     return 0;
590     }
591     // safety check (always keep value in sync with "attr_def.h" !!) ---------------
592     #endif
593    
594     //dbg(0,"batg:002.2\n");
595 zoff99 27 if (type == attr_label)
596     mr->label = 1;
597 zoff99 2 if (type == attr_house_number)
598 zoff99 27 mr->label_attr[0] = t->pos_attr;
599 zoff99 2 if (type == attr_street_name)
600 zoff99 27 mr->label_attr[1] = t->pos_attr;
601 zoff99 2 if (type == attr_street_name_systematic)
602 zoff99 27 mr->label_attr[2] = t->pos_attr;
603 zoff99 2 if (type == attr_district_name && mr->item.type < type_line)
604 zoff99 27 mr->label_attr[3] = t->pos_attr;
605 zoff99 2 if (type == attr_town_name && mr->item.type < type_line)
606 zoff99 27 mr->label_attr[4] = t->pos_attr;
607 zoff99 31
608 zoff99 27 if (type == attr_type || attr_type == attr_any)
609     {
610     if (attr_type == attr_any)
611     {
612     // dbg(1, "pos %p attr %s size %d\n", t->pos_attr - 1, attr_to_name(type), size);
613 zoff99 2 }
614 zoff99 27 attr->type = type;
615     if (ATTR_IS_GROUP(type))
616     {
617     int i = 0;
618     int *subpos = t->pos_attr + 1;
619     int size_rem = size - 1;
620     i = 0;
621     while (size_rem > 0 && i < 7)
622     {
623     int subsize = le32_to_cpu(*subpos++);
624     int subtype = le32_to_cpu(subpos[0]);
625     mr->attrs[i].type = subtype;
626     attr_data_set_le(&mr->attrs[i], subpos + 1);
627     subpos += subsize;
628     size_rem -= subsize + 1;
629 zoff99 2 i++;
630     }
631 zoff99 31
632 zoff99 27 mr->attrs[i].type = type_none;
633     mr->attrs[i].u.data = NULL;
634     attr->u.attrs = mr->attrs;
635     }
636     else
637     {
638     attr_data_set_le(attr, t->pos_attr + 1);
639     if (type == attr_url_local)
640     {
641 zoff99 2 g_free(mr->url);
642 zoff99 27 mr->url = binfile_extract(mr->m, mr->m->cachedir, attr->u.str, 1);
643     attr->u.str = mr->url;
644 zoff99 2 }
645 zoff99 31
646 zoff99 27 if (type == attr_flags && mr->m->map_version < 1)
647 zoff99 31 {
648 zoff99 2 attr->u.num |= AF_CAR;
649 zoff99 31 }
650 zoff99 2 }
651 zoff99 27 t->pos_attr += size;
652 zoff99 2 return 1;
653     }
654 zoff99 27 else
655     {
656     t->pos_attr += size;
657     }
658 zoff99 2 }
659 zoff99 31
660 zoff99 27 if (!mr->label && (attr_type == attr_any || attr_type == attr_label))
661     {
662     for (i = 0; i < sizeof(mr->label_attr) / sizeof(int *); i++)
663     {
664     if (mr->label_attr[i])
665     {
666     mr->label = 1;
667     attr->type = attr_label;
668     attr_data_set_le(attr, mr->label_attr[i] + 1);
669 zoff99 2 return 1;
670     }
671     }
672     }
673 zoff99 31
674     #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
675     dbg(0,"+#+:leave\n");
676     #endif
677    
678 zoff99 2 return 0;
679     }
680    
681 zoff99 27 struct binfile_hash_entry
682     {
683 zoff99 2 struct item_id id;
684     int flags;
685     int data[0];
686     };
687    
688 zoff99 27 static guint binfile_hash_entry_hash(gconstpointer key)
689 zoff99 2 {
690 zoff99 27 const struct binfile_hash_entry *entry = key;
691 zoff99 2 return (entry->id.id_hi ^ entry->id.id_lo);
692     }
693    
694 zoff99 27 static gboolean binfile_hash_entry_equal(gconstpointer a, gconstpointer b)
695 zoff99 2 {
696 zoff99 27 const struct binfile_hash_entry *entry1 = a, *entry2 = b;
697     return (entry1->id.id_hi == entry2->id.id_hi && entry1->id.id_lo == entry2->id.id_lo);
698 zoff99 2 }
699    
700     static int *
701     binfile_item_dup(struct map_priv *m, struct item *item, struct tile *t, int extend)
702     {
703 zoff99 27 int size = le32_to_cpu(t->pos[0]);
704     struct binfile_hash_entry *entry = g_malloc(sizeof(struct binfile_hash_entry) + (size + 1 + extend) * sizeof(int));
705     void *ret = entry->data;
706     entry->id.id_hi = item->id_hi;
707     entry->id.id_lo = item->id_lo;
708     entry->flags = 1;
709     // dbg(0, "id 0x%x,0x%x\n", entry->id.id_hi, entry->id.id_lo);
710    
711     memcpy(ret, t->pos, (size + 1) * sizeof(int));
712     if (!m->changes)
713 zoff99 31 m->changes = g_hash_table_new_full(binfile_hash_entry_hash, binfile_hash_entry_equal, g_free_func, NULL);
714 zoff99 2 g_hash_table_replace(m->changes, entry, entry);
715 zoff99 27 // dbg(0, "ret %p\n", ret);
716 zoff99 2 return ret;
717     }
718    
719 zoff99 27 static int binfile_coord_set(void *priv_data, struct coord *c, int count, enum change_mode mode)
720 zoff99 2 {
721 zoff99 27 struct map_rect_priv *mr = priv_data;
722     struct tile *t = mr->t, *tn, new;
723     int i, delta, move_len;
724     int write_offset, move_offset, aoffset, coffset, clen;
725 zoff99 2 int *data;
726    
727 zoff99 27 //{
728     //int *i = t->pos, j = 0;
729     // dbg(0, "Before: pos_coord=%d\n", t->pos_coord - i);
730     //while (i < t->pos_next)
731     //{
732     // // dbg(0, "%d:0x%x\n", j++, *i++);
733     //}
734    
735     //}
736    
737     aoffset = t->pos_attr - t->pos_attr_start;
738     coffset = t->pos_coord - t->pos_coord_start - 2;
739     clen = t->pos_attr_start - t->pos_coord + 2;
740     // dbg(0, "coffset=%d clen=%d\n", coffset, clen);
741     switch (mode)
742 zoff99 2 {
743 zoff99 27 case change_mode_delete:
744     if (count * 2 > clen)
745     count = clen / 2;
746     delta = -count * 2;
747     move_offset = coffset + count * 2;
748     move_len = t->pos_next - t->pos_coord_start - move_offset;
749     write_offset = 0;
750     break;
751     case change_mode_modify:
752     write_offset = coffset;
753     if (count * 2 > clen)
754     {
755     delta = count * 2 - clen;
756     move_offset = t->pos_attr_start - t->pos_coord_start;
757     move_len = t->pos_next - t->pos_coord_start - move_offset;
758     }
759     else
760     {
761     move_len = 0;
762     move_offset = 0;
763     delta = 0;
764     }
765     break;
766     case change_mode_prepend:
767     delta = count * 2;
768     move_offset = coffset - 2;
769     move_len = t->pos_next - t->pos_coord_start - move_offset;
770     write_offset = coffset - 2;
771     break;
772     case change_mode_append:
773     delta = count * 2;
774     move_offset = coffset;
775     move_len = t->pos_next - t->pos_coord_start - move_offset;
776     write_offset = coffset;
777     break;
778     default:
779     return 0;
780 zoff99 2 }
781 zoff99 27 // dbg(0, "delta %d\n", delta);
782     data = binfile_item_dup(mr->m, &mr->item, t, delta > 0 ? delta : 0);
783     data[0] = cpu_to_le32(le32_to_cpu(data[0]) + delta);
784     data[2] = cpu_to_le32(le32_to_cpu(data[2]) + delta);
785     new.pos = new.start = data;
786     new.zipfile_num = t->zipfile_num;
787     new.mode = 2;
788 zoff99 2 push_tile(mr, &new, 0, 0);
789     setup_pos(mr);
790 zoff99 27 tn = mr->t;
791     tn->pos_coord = tn->pos_coord_start + coffset;
792     tn->pos_attr = tn->pos_attr_start + aoffset;
793     // dbg(0, "moving %d ints from offset %d to %d\n", move_len, tn->pos_coord_start + move_offset - data, tn->pos_coord_start + move_offset + delta - data);
794     memmove(tn->pos_coord_start + move_offset + delta, tn->pos_coord_start + move_offset, move_len * 4);
795     //{
796     //int *i = tn->pos, j = 0;
797     // dbg(0, "After move: pos_coord=%d\n", tn->pos_coord - i);
798     //while (i < tn->pos_next)
799     //{
800     // // dbg(0, "%d:0x%x\n", j++, *i++);
801     //}
802     //}
803     if (mode != change_mode_append)
804 zoff99 2 {
805 zoff99 27 tn->pos_coord += move_offset;
806 zoff99 2 }
807 zoff99 27 if (mode != change_mode_delete)
808     {
809     // dbg(0, "writing %d ints at offset %d\n", count * 2, write_offset + tn->pos_coord_start - data);
810     for (i = 0; i < count; i++)
811     {
812     tn->pos_coord_start[write_offset++] = c[i].x;
813     tn->pos_coord_start[write_offset++] = c[i].y;
814 zoff99 2 }
815 zoff99 27
816 zoff99 2 }
817 zoff99 27 //{
818     //int *i = tn->pos, j = 0;
819     //// dbg(0, "After: pos_coord=%d\n", tn->pos_coord - i);
820     //while (i < tn->pos_next)
821     //{
822     // // dbg(0, "%d:0x%x\n", j++, *i++);
823     //}
824     //}
825 zoff99 2 return 1;
826     }
827    
828 zoff99 27 static int binfile_attr_set(void *priv_data, struct attr *attr, enum change_mode mode)
829 zoff99 2 {
830 zoff99 27 struct map_rect_priv *mr = priv_data;
831     struct tile *t = mr->t, *tn, new;
832     int extend, offset, delta, move_len;
833     int write_offset, move_offset, naoffset, coffset, oattr_len;
834     int nattr_size, nattr_len, pad;
835 zoff99 2 int *data;
836    
837 zoff99 27 //{
838     //int *i = t->pos, j = 0;
839     //// dbg(0, "Before: pos_attr=%d\n", t->pos_attr - i);
840     //while (i < t->pos_next)
841     //{
842     // // dbg(0, "%d:0x%x\n", j++, *i++);
843     //}
844     //}
845    
846     write_offset = 0;
847     naoffset = t->pos_attr - t->pos_attr_start;
848     coffset = t->pos_coord - t->pos_coord_start;
849     offset = 0;
850     oattr_len = 0;
851     if (!naoffset)
852 zoff99 2 {
853 zoff99 27 if (mode == change_mode_delete || mode == change_mode_modify)
854     {
855     // dbg(0, "no attribute selected\n");
856 zoff99 2 return 0;
857     }
858     if (mode == change_mode_append)
859 zoff99 27 {
860     naoffset = t->pos_next - t->pos_attr_start;
861     }
862 zoff99 2 }
863 zoff99 27 while (offset < naoffset)
864     {
865     oattr_len = le32_to_cpu(t->pos_attr_start[offset]) + 1;
866     // dbg(0, "len %d\n", oattr_len);
867     write_offset = offset;
868     offset += oattr_len;
869 zoff99 2 }
870 zoff99 27 move_len = t->pos_next - t->pos_attr_start - offset;
871     move_offset = offset;
872     switch (mode)
873     {
874     case change_mode_delete:
875     nattr_size = 0;
876     nattr_len = 0;
877     pad = 0;
878     extend = 0;
879     break;
880     case change_mode_modify:
881     case change_mode_prepend:
882     case change_mode_append:
883     nattr_size = attr_data_size(attr);
884     pad = (4 - (nattr_size % 4)) % 4;
885     nattr_len = (nattr_size + pad) / 4 + 2;
886     if (mode == change_mode_prepend)
887     {
888     move_offset = write_offset;
889     move_len += oattr_len;
890     }
891     if (mode == change_mode_append)
892     {
893     write_offset = move_offset;
894     }
895     break;
896     default:
897     return 0;
898 zoff99 2 }
899 zoff99 27 if (mode == change_mode_delete || mode == change_mode_modify)
900     {
901     delta = nattr_len - oattr_len;
902     }
903 zoff99 2 else
904 zoff99 27 {
905     delta = nattr_len;
906     }
907     // dbg(0, "delta %d oattr_len %d nattr_len %d\n", delta, oattr_len, nattr_len);
908     data = binfile_item_dup(mr->m, &mr->item, t, delta > 0 ? delta : 0);
909     data[0] = cpu_to_le32(le32_to_cpu(data[0]) + delta);
910     new.pos = new.start = data;
911     new.zipfile_num = t->zipfile_num;
912     new.mode = 2;
913 zoff99 2 push_tile(mr, &new, 0, 0);
914     setup_pos(mr);
915 zoff99 27 tn = mr->t;
916     tn->pos_coord = tn->pos_coord_start + coffset;
917     tn->pos_attr = tn->pos_attr_start + offset;
918     // dbg(0, "attr start %d offset %d\n", tn->pos_attr_start - data, offset);
919     // dbg(0, "moving %d ints from offset %d to %d\n", move_len, tn->pos_attr_start + move_offset - data, tn->pos_attr_start + move_offset + delta - data);
920     memmove(tn->pos_attr_start + move_offset + delta, tn->pos_attr_start + move_offset, move_len * 4);
921 zoff99 2 if (mode != change_mode_append)
922 zoff99 27 tn->pos_attr += delta;
923 zoff99 2 {
924 zoff99 27 int *i = tn->pos, j = 0;
925     // dbg(0, "After move: pos_attr=%d\n", tn->pos_attr - i);
926     while (i < tn->pos_next)
927     {
928     // dbg(0, "%d:0x%x\n", j++, *i++);
929     }
930 zoff99 2 }
931 zoff99 27
932     if (nattr_len)
933 zoff99 2 {
934 zoff99 27 int *nattr = tn->pos_attr_start + write_offset;
935     // dbg(0, "writing %d ints at %d\n", nattr_len, nattr - data);
936     nattr[0] = cpu_to_le32(nattr_len - 1);
937     nattr[1] = cpu_to_le32(attr->type);
938     memcpy(nattr + 2, attr_data_get(attr), nattr_size);
939     memset((unsigned char *) (nattr + 2) + nattr_size, 0, pad);
940 zoff99 2 }
941 zoff99 27
942     //{
943     //int *i = tn->pos, j = 0;
944     //// dbg(0, "After: pos_attr=%d\n", tn->pos_attr - i);
945     //while (i < tn->pos_next)
946     //{
947     // // dbg(0, "%d:0x%x\n", j++, *i++);
948     //}
949     //}
950 zoff99 2 return 1;
951     }
952    
953 zoff99 27 static struct item_methods methods_binfile =
954     { binfile_coord_rewind, binfile_coord_get, binfile_attr_rewind, binfile_attr_get, NULL, binfile_attr_set, binfile_coord_set, };
955 zoff99 2
956 zoff99 27 static void push_tile(struct map_rect_priv *mr, struct tile *t, int offset, int length)
957 zoff99 2 {
958     dbg_assert(mr->tile_depth < 8);
959 zoff99 27 mr->t = &mr->tiles[mr->tile_depth++];
960     *(mr->t) = *t;
961     mr->t->pos = mr->t->pos_next = mr->t->start + offset;
962 zoff99 2 if (length == -1)
963 zoff99 27 length = le32_to_cpu(mr->t->pos[0]) + 1;
964     if (length > 0)
965     mr->t->end = mr->t->pos + length;
966 zoff99 2 }
967    
968 zoff99 27 static int pop_tile(struct map_rect_priv *mr)
969 zoff99 2 {
970     if (mr->tile_depth <= 1)
971     return 0;
972     if (mr->t->mode < 2)
973 zoff99 27 file_data_free(mr->m->fi, (unsigned char *) (mr->t->start));
974 zoff99 2 #ifdef DEBUG_SIZE
975     #if DEBUG_SIZE > 0
976 zoff99 27 // dbg(0,"leave %d\n",mr->t->zipfile_num);
977 zoff99 2 #endif
978     #endif
979 zoff99 27 mr->t = &mr->tiles[--mr->tile_depth - 1];
980 zoff99 2 return 1;
981     }
982    
983 zoff99 27 static int zipfile_to_tile(struct map_priv *m, struct zip_cd *cd, struct tile *t)
984 zoff99 2 {
985     char buffer[1024];
986     struct zip_lfh *lfh;
987     char *zipfn;
988     struct file *fi;
989 zoff99 27 // dbg(1, "enter %p %p %p\n", m, cd, t);
990     // dbg(1, "cd->zipofst=0x%Lx\n", binfile_cd_offset(cd));
991     t->start = NULL;
992     t->mode = 1;
993 zoff99 2 if (m->fis)
994 zoff99 27 fi = m->fis[cd->zipdsk];
995 zoff99 2 else
996 zoff99 27 fi = m->fi;
997     lfh = binfile_read_lfh(fi, binfile_cd_offset(cd));
998     zipfn = (char *) (file_data_read(fi, binfile_cd_offset(cd) + sizeof(struct zip_lfh), lfh->zipfnln));
999 zoff99 2 strncpy(buffer, zipfn, lfh->zipfnln);
1000 zoff99 27 buffer[lfh->zipfnln] = '\0';
1001     t->start = (int *) binfile_read_content(m, fi, binfile_cd_offset(cd), lfh);
1002     t->end = t->start + lfh->zipuncmp / 4;
1003     t->fi = fi;
1004     // dbg(1, "0x%x '%s' %d %d,%d\n", lfh->ziplocsig, buffer, sizeof(*cd) + cd->zipcfnl, lfh->zipsize, lfh->zipuncmp);
1005     file_data_free(fi, (unsigned char *) zipfn);
1006     file_data_free(fi, (unsigned char *) lfh);
1007 zoff99 2 return t->start != NULL;
1008     }
1009    
1010 zoff99 27 static int map_binfile_handle_redirect(struct map_priv *m)
1011 zoff99 2 {
1012 zoff99 27 char *location = file_http_header(m->http, "location");
1013     if (!location)
1014     {
1015     m->redirect = 0;
1016 zoff99 2 return 0;
1017     }
1018 zoff99 27 if (m->redirect)
1019 zoff99 2 return 0;
1020 zoff99 27 m->redirect = 1;
1021     // dbg(0, "redirected from %s to %s\n", m->url, location);
1022 zoff99 2 g_free(m->url);
1023 zoff99 27 m->url = g_strdup(location);
1024 zoff99 2 file_destroy(m->http);
1025 zoff99 27 m->http = NULL;
1026 zoff99 2
1027     return 1;
1028     }
1029    
1030 zoff99 27 static int map_binfile_http_request(struct map_priv *m, struct attr **attrs)
1031 zoff99 2 {
1032 zoff99 27 if (!m->http)
1033     {
1034     m->http = file_create(NULL, attrs);
1035     }
1036     else
1037     {
1038 zoff99 2 file_request(m->http, attrs);
1039     }
1040     return 1;
1041     }
1042    
1043 zoff99 27 static long long map_binfile_download_size(struct map_priv *m)
1044 zoff99 2 {
1045 zoff99 27 struct attr url =
1046     { attr_url };
1047     struct attr http_method =
1048     { attr_http_method };
1049     struct attr persistent =
1050     { attr_persistent };
1051 zoff99 2 struct attr *attrs[4];
1052     int size_ret;
1053     long long ret;
1054     void *data;
1055    
1056 zoff99 27 do
1057     {
1058     attrs[0] = &url;
1059     url.u.str = m->url;
1060     attrs[1] = &http_method;
1061     http_method.u.str = "HEAD";
1062     persistent.u.num = 1;
1063     attrs[2] = &persistent;
1064     attrs[3] = NULL;
1065 zoff99 2
1066     map_binfile_http_request(m, attrs);
1067 zoff99 27 data = file_data_read_special(m->http, 0, &size_ret);
1068 zoff99 2 g_free(data);
1069 zoff99 27 if (size_ret < 0)
1070 zoff99 2 return 0;
1071 zoff99 27 }
1072     while (map_binfile_handle_redirect(m));
1073    
1074     ret = file_size(m->http);
1075     // dbg(1,"file size "LONGLONG_FMT"\n",ret);
1076 zoff99 2 return ret;
1077     }
1078    
1079 zoff99 27 static int map_binfile_http_close(struct map_priv *m)
1080 zoff99 2 {
1081 zoff99 27 if (m->http)
1082     {
1083 zoff99 2 file_destroy(m->http);
1084 zoff99 27 m->http = NULL;
1085 zoff99 2 }
1086     return 1;
1087     }
1088    
1089     static struct file *
1090     map_binfile_http_range(struct map_priv *m, long long offset, int size)
1091     {
1092     struct attr *attrs[4];
1093 zoff99 27 struct attr url =
1094     { attr_url };
1095     struct attr http_header =
1096     { attr_http_header };
1097     struct attr persistent =
1098     { attr_persistent };
1099 zoff99 2
1100 zoff99 27 persistent.u.num = 1;
1101     attrs[0] = &url;
1102     attrs[1] = &http_header;
1103     attrs[2] = &persistent;
1104     attrs[3] = NULL;
1105 zoff99 2
1106 zoff99 27 url.u.str = m->url;
1107 zoff99 2 http_header.u.str=g_strdup_printf("Range: bytes="LONGLONG_FMT"-"LONGLONG_FMT,offset, offset+size-1);
1108     map_binfile_http_request(m, attrs);
1109     g_free(http_header.u.str);
1110     return m->http;
1111     }
1112    
1113     static unsigned char *
1114     map_binfile_download_range(struct map_priv *m, long long offset, int size)
1115     {
1116     unsigned char *ret;
1117     int size_ret;
1118 zoff99 27 struct file *http = map_binfile_http_range(m, offset, size);
1119 zoff99 2
1120 zoff99 27 ret = file_data_read_special(http, size, &size_ret);
1121     if (size_ret != size)
1122     {
1123     // dbg(0, "size %d vs %d\n", size, size_ret);
1124 zoff99 2 g_free(ret);
1125     return NULL;
1126     }
1127     return ret;
1128     }
1129    
1130     static struct zip_cd *
1131     download_cd(struct map_download *download)
1132     {
1133 zoff99 27 struct map_priv *m = download->m;
1134     struct zip64_eoc *zip64_eoc = (struct zip64_eoc *) file_data_read(m->fi, 0, sizeof(*zip64_eoc));
1135     struct zip_cd *cd = (struct zip_cd *) map_binfile_download_range(m, zip64_eoc->zip64eofst + download->zipfile * m->cde_size, m->cde_size);
1136     file_data_free(m->fi, (unsigned char *) zip64_eoc);
1137     // dbg(0, "needed cd, result %p\n", cd);
1138 zoff99 2 return cd;
1139     }
1140    
1141 zoff99 27 static int download_request(struct map_download *download)
1142 zoff99 2 {
1143 zoff99 27 struct attr url =
1144     { attr_url };
1145     struct attr http_header =
1146     { attr_http_header };
1147     struct attr persistent =
1148     { attr_persistent };
1149 zoff99 2 struct attr *attrs[4];
1150    
1151 zoff99 27 if (!download->m->download_enabled)
1152 zoff99 2 {
1153 zoff99 27 // dbg(0, "Tried downloading while it's not allowed\n");
1154 zoff99 2 return 0;
1155     }
1156 zoff99 27 attrs[0] = &url;
1157     persistent.u.num = 1;
1158     attrs[1] = &persistent;
1159     attrs[2] = NULL;
1160     if (strchr(download->m->url, '?'))
1161     {
1162     url.u.str = g_strdup_printf("%smemberid=%d", download->m->url, download->zipfile);
1163     download->dl_size = -1;
1164     }
1165     else
1166     {
1167     long long offset = binfile_cd_offset(download->cd_copy);
1168     int size = download->cd_copy->zipcsiz + sizeof(struct zip_lfh) + download->cd_copy->zipcfnl;
1169     url.u.str = g_strdup(download->m->url);
1170 zoff99 2 http_header.u.str=g_strdup_printf("Range: bytes="LONGLONG_FMT"-"LONGLONG_FMT,offset,offset+size-1);
1171 zoff99 27 attrs[2] = &http_header;
1172     attrs[3] = NULL;
1173     download->dl_size = size;
1174 zoff99 2 }
1175 zoff99 27 // dbg(0,"encountered missing tile %d %s(%s), Downloading %d bytes at "LONGLONG_FMT"\n",download->zipfile, url.u.str,(char *)(download->cd_copy+1), download->dl_size, download->offset);
1176 zoff99 2 map_binfile_http_request(download->m, attrs);
1177     g_free(url.u.str);
1178 zoff99 27 download->http = download->m->http;
1179 zoff99 2 return 1;
1180     }
1181    
1182 zoff99 27 static int download_start(struct map_download *download)
1183 zoff99 2 {
1184     long long offset;
1185     struct zip_eoc *eoc;
1186    
1187 zoff99 27 if (!download->cd->zipcensig)
1188     {
1189     download->cd_copy = download_cd(download);
1190     }
1191     else
1192     {
1193     download->cd_copy = g_malloc(download->m->cde_size);
1194 zoff99 2 memcpy(download->cd_copy, download->cd, download->m->cde_size);
1195     }
1196 zoff99 27 file_data_remove(download->file, (unsigned char *) download->cd);
1197     download->cd = NULL;
1198     offset = file_size(download->file);
1199     offset -= sizeof(struct zip_eoc);
1200     eoc = (struct zip_eoc *) file_data_read(download->file, offset, sizeof(struct zip_eoc));
1201     download->zip_eoc = g_malloc(sizeof(struct zip_eoc));
1202 zoff99 2 memcpy(download->zip_eoc, eoc, sizeof(struct zip_eoc));
1203 zoff99 27 file_data_remove(download->file, (unsigned char *) eoc);
1204     download->start_offset = download->offset = offset;
1205 zoff99 2 return download_request(download);
1206     }
1207    
1208 zoff99 27 static int download_download(struct map_download *download)
1209 zoff99 2 {
1210 zoff99 27 int size = 64 * 1024, size_ret;
1211 zoff99 2 unsigned char *data;
1212     if (download->dl_size != -1 && size > download->dl_size)
1213 zoff99 27 size = download->dl_size;
1214 zoff99 2 if (!size)
1215     return 1;
1216 zoff99 27 data = file_data_read_special(download->http, size, &size_ret);
1217     if (!download->read && download->m->http && map_binfile_handle_redirect(download->m))
1218     {
1219 zoff99 2 g_free(data);
1220     download_request(download);
1221     return 0;
1222     }
1223    
1224 zoff99 27 // dbg(1,"got %d bytes writing at offset "LONGLONG_FMT"\n",size_ret,download->offset);
1225     if (size_ret <= 0)
1226     {
1227 zoff99 2 g_free(data);
1228     return 1;
1229     }
1230     file_data_write(download->file, download->offset, size_ret, data);
1231 zoff99 27 download->offset += size_ret;
1232     download->read += size_ret;
1233     download->dl_size -= size_ret;
1234 zoff99 2 if (download->dl_size != -1)
1235 zoff99 27 download->progress = download->read * 100 / (download->read + download->dl_size);
1236 zoff99 2 return 0;
1237     }
1238    
1239 zoff99 27 static int download_finish(struct map_download *download)
1240 zoff99 2 {
1241     struct zip_lfh *lfh;
1242     char *lfh_filename;
1243     struct zip_cd_ext *ext;
1244     long long lfh_offset;
1245 zoff99 27 file_data_write(download->file, download->offset, sizeof(struct zip_eoc), (void *) download->zip_eoc);
1246     lfh = (struct zip_lfh *) (file_data_read(download->file, download->start_offset, sizeof(struct zip_lfh)));
1247     ext = binfile_cd_ext(download->cd_copy);
1248 zoff99 2 if (ext)
1249 zoff99 27 ext->zipofst = download->start_offset;
1250 zoff99 2 else
1251 zoff99 27 download->cd_copy->zipofst = download->start_offset;
1252     download->cd_copy->zipcsiz = lfh->zipsize;
1253     download->cd_copy->zipcunc = lfh->zipuncmp;
1254     download->cd_copy->zipccrc = lfh->zipcrc;
1255     lfh_offset = binfile_cd_offset(download->cd_copy) + sizeof(struct zip_lfh);
1256     lfh_filename = (char *) file_data_read(download->file, lfh_offset, lfh->zipfnln);
1257     memcpy(download->cd_copy + 1, lfh_filename, lfh->zipfnln);
1258     file_data_remove(download->file, (void *) lfh_filename);
1259     file_data_remove(download->file, (void *) lfh);
1260     file_data_write(download->file, download->m->eoc->zipeofst + download->zipfile * download->m->cde_size, binfile_cd_extra(download->cd_copy) + sizeof(struct zip_cd), (void *) download->cd_copy);
1261     file_data_flush(download->file, download->m->eoc->zipeofst + download->zipfile * download->m->cde_size, sizeof(struct zip_cd));
1262 zoff99 2
1263     g_free(download->cd_copy);
1264 zoff99 27 download->cd = (struct zip_cd *) (file_data_read(download->file, download->m->eoc->zipeofst + download->zipfile * download->m->cde_size, download->m->cde_size));
1265 zoff99 2 cd_to_cpu(download->cd);
1266 zoff99 27 // dbg(1, "Offset %d\n", download->cd->zipofst);
1267 zoff99 2 return 1;
1268     }
1269    
1270 zoff99 27 static int download_planet_size(struct map_download *download)
1271 zoff99 2 {
1272 zoff99 27 download->size = map_binfile_download_size(download->m);
1273     // dbg(0,"Planet size "LONGLONG_FMT"\n",download->size);
1274 zoff99 2 if (!download->size)
1275     return 0;
1276     return 1;
1277     }
1278    
1279 zoff99 27 static int download_eoc(struct map_download *download)
1280 zoff99 2 {
1281 zoff99 27 download->zip64_eoc = (struct zip64_eoc *) map_binfile_download_range(download->m, download->size - 98, 98);
1282 zoff99 2 if (!download->zip64_eoc)
1283     return 0;
1284 zoff99 27 download->zip64_eocl = (struct zip64_eocl *) (download->zip64_eoc + 1);
1285     download->zip_eoc = (struct zip_eoc *) (download->zip64_eocl + 1);
1286 zoff99 2 if (download->zip64_eoc->zip64esig != zip64_eoc_sig || download->zip64_eocl->zip64lsig != zip64_eocl_sig || download->zip_eoc->zipesig != zip_eoc_sig)
1287     {
1288 zoff99 27 // dbg(0,"wrong signature on zip64_eoc downloaded from "LONGLONG_FMT"\n",download->size-98);
1289 zoff99 2 g_free(download->zip64_eoc);
1290     return 0;
1291     }
1292     return 1;
1293     }
1294    
1295 zoff99 27 static int download_directory_start(struct map_download *download)
1296 zoff99 2 {
1297 zoff99 27 download->http = map_binfile_http_range(download->m, download->zip64_eoc->zip64eofst, download->zip64_eoc->zip64ecsz);
1298 zoff99 2 if (!download->http)
1299     return 0;
1300     return 1;
1301     }
1302    
1303 zoff99 27 static int download_directory_do(struct map_download *download)
1304 zoff99 2 {
1305     int count;
1306    
1307 zoff99 27 for (count = 0; count < 100; count++)
1308     {
1309 zoff99 2 int cd_xlen, size_ret;
1310     unsigned char *cd_data;
1311     struct zip_cd *cd;
1312 zoff99 27 cd = (struct zip_cd *) file_data_read_special(download->http, sizeof(*cd), &size_ret);
1313     cd->zipcunc = 0;
1314     // dbg(1, "size_ret=%d\n", size_ret);
1315 zoff99 2 if (!size_ret)
1316     return 0;
1317 zoff99 27 if (size_ret != sizeof(*cd) || cd->zipcensig != zip_cd_sig)
1318     {
1319     // dbg(0, "error1 size=%d vs %d\n", size_ret, sizeof(*cd));
1320 zoff99 2 return 0;
1321     }
1322 zoff99 27 file_data_write(download->file, download->offset, sizeof(*cd), (unsigned char *) cd);
1323     download->offset += sizeof(*cd);
1324     cd_xlen = cd->zipcfnl + cd->zipcxtl;
1325     cd_data = file_data_read_special(download->http, cd_xlen, &size_ret);
1326     if (size_ret != cd_xlen)
1327     {
1328     // dbg(0, "error2 size=%d vs %d\n", size_ret, cd_xlen);
1329 zoff99 2 return 0;
1330     }
1331     file_data_write(download->file, download->offset, cd_xlen, cd_data);
1332 zoff99 27 download->offset += cd_xlen;
1333 zoff99 2 g_free(cd);
1334     g_free(cd_data);
1335     }
1336     return 1;
1337     }
1338    
1339 zoff99 27 static int download_directory_finish(struct map_download *download)
1340 zoff99 2 {
1341 zoff99 27 download->http = NULL;
1342 zoff99 2 return 1;
1343     }
1344    
1345 zoff99 27 static int download_initial_finish(struct map_download *download)
1346 zoff99 2 {
1347 zoff99 27 download->zip64_eoc->zip64eofst = download->cd1offset;
1348     download->zip64_eocl->zip64lofst = download->offset;
1349     download->zip_eoc->zipeofst = download->cd1offset;
1350 zoff99 2 #if 0
1351     file_data_write(download->file, download->offset, sizeof(*download->zip64_eoc), (unsigned char *)download->zip64_eoc);
1352     download->offset+=sizeof(*download->zip64_eoc);
1353     file_data_write(download->file, download->offset, sizeof(*download->zip64_eocl), (unsigned char *)download->zip64_eocl);
1354     download->offset+=sizeof(*download->zip64_eocl);
1355     #endif
1356 zoff99 27 file_data_write(download->file, download->offset, sizeof(*download->zip_eoc), (unsigned char *) download->zip_eoc);
1357     download->offset += sizeof(*download->zip_eoc);
1358 zoff99 2 g_free(download->zip64_eoc);
1359 zoff99 27 download->zip64_eoc = NULL;
1360 zoff99 2 return 1;
1361     }
1362    
1363 zoff99 27 static void push_zipfile_tile_do(struct map_rect_priv *mr, struct zip_cd *cd, int zipfile, int offset, int length)
1364 zoff99 2
1365     {
1366     struct tile t;
1367 zoff99 27 struct map_priv *m = mr->m;
1368     struct file *f = m->fi;
1369 zoff99 2
1370 zoff99 27 // dbg(1, "enter %p %d\n", mr, zipfile);
1371 zoff99 2 #ifdef DEBUG_SIZE
1372     #if DEBUG_SIZE > 0
1373     {
1374     char filename[cd->zipcfnl+1];
1375     memcpy(filename, cd+1, cd->zipcfnl);
1376     filename[cd->zipcfnl]='\0';
1377 zoff99 27 // dbg(0,"enter %d (%s) %d\n",zipfile, filename, cd->zipcunc);
1378 zoff99 2 }
1379     #endif
1380     mr->size+=cd->zipcunc;
1381     #endif
1382 zoff99 27 t.zipfile_num = zipfile;
1383     if (zipfile_to_tile(m, cd, &t))
1384 zoff99 2 push_tile(mr, &t, offset, length);
1385 zoff99 27 file_data_free(f, (unsigned char *) cd);
1386 zoff99 2 }
1387    
1388     static struct zip_cd *
1389     download(struct map_priv *m, struct map_rect_priv *mr, struct zip_cd *cd, int zipfile, int offset, int length, int async)
1390     {
1391     struct map_download *download;
1392    
1393 zoff99 27 if (!m->download_enabled)
1394 zoff99 2 return NULL;
1395    
1396 zoff99 27 if (async == 2)
1397     {
1398     download = m->download;
1399     }
1400     else
1401     {
1402 zoff99 2 download=g_new0(struct map_download, 1);
1403 zoff99 27 if (mr)
1404     {
1405     download->m = m;
1406     download->mr = mr;
1407     download->file = m->fi;
1408     download->cd = cd;
1409     download->zipfile = zipfile;
1410     download->toffset = offset;
1411     download->tlength = length;
1412     download->state = 1;
1413     }
1414     else
1415     {
1416     struct attr readwrite =
1417     { attr_readwrite,
1418     { (void *) 1 } };
1419     struct attr create =
1420     { attr_create,
1421     { (void *) 1 } };
1422 zoff99 2 struct attr *attrs[3];
1423 zoff99 27 attrs[0] = &readwrite;
1424     attrs[1] = &create;
1425     attrs[2] = NULL;
1426     download->file = file_create(m->filename, attrs);
1427     download->m = m;
1428     download->state = 4;
1429 zoff99 2 }
1430     }
1431 zoff99 27 if (async == 1)
1432     {
1433     m->download = download;
1434 zoff99 2 g_free(m->progress);
1435 zoff99 27 if (download->mr)
1436     m->progress = g_strdup_printf("Download Tile %d 0%%", download->zipfile);
1437 zoff99 2 else
1438 zoff99 27 m->progress = g_strdup_printf("Download Map Information 0%%");
1439 zoff99 2 callback_list_call_attr_0(m->cbl, attr_progress);
1440     return NULL;
1441     }
1442 zoff99 27 for (;;)
1443     {
1444     // dbg(0, "state=%d\n", download->state);
1445     switch (download->state)
1446     {
1447     case 0:
1448     // dbg(0, "error\n");
1449     break;
1450     case 1:
1451     if (download_start(download))
1452     download->state = 2;
1453     else
1454     download->state = 0;
1455     break;
1456     case 2:
1457     if (download_download(download))
1458     download->state = 3;
1459     else
1460     {
1461     g_free(m->progress);
1462     m->progress = g_strdup_printf("Download Tile %d %d%%", download->zipfile, download->progress);
1463     callback_list_call_attr_0(m->cbl, attr_progress);
1464     }
1465     break;
1466     case 3:
1467     if (download_finish(download))
1468     {
1469     struct zip_cd *ret;
1470     g_free(m->progress);
1471     m->progress = g_strdup_printf("Download Tile %d 100%%", download->zipfile);
1472     callback_list_call_attr_0(m->cbl, attr_progress);
1473     if (async)
1474     {
1475     push_zipfile_tile_do(download->mr, download->cd, download->zipfile, download->toffset, download->tlength);
1476     ret = NULL;
1477     }
1478     else
1479     ret = download->cd;
1480     g_free(m->progress);
1481     m->progress = NULL;
1482     g_free(download);
1483     if (async)
1484     m->download = NULL;
1485     return ret;
1486     }
1487     else
1488     download->state = 0;
1489     break;
1490     case 4:
1491     if (download_planet_size(download))
1492     download->state = 5;
1493     else
1494     download->state = 0;
1495     break;
1496     case 5:
1497 zoff99 2 g_free(m->progress);
1498 zoff99 27 m->progress = g_strdup_printf("Download Map Information 50%%");
1499 zoff99 2 callback_list_call_attr_0(m->cbl, attr_progress);
1500 zoff99 27 if (download_eoc(download))
1501     download->state = 6;
1502     else
1503     {
1504     // dbg(0, "download of eoc failed\n");
1505     download->state = 0;
1506     }
1507     break;
1508     case 6:
1509 zoff99 2 g_free(m->progress);
1510 zoff99 27 m->progress = g_strdup_printf("Download Map Information 100%%");
1511 zoff99 2 callback_list_call_attr_0(m->cbl, attr_progress);
1512 zoff99 27 if (download_directory_start(download))
1513     download->state = 7;
1514     else
1515     download->state = 0;
1516     break;
1517     case 7:
1518 zoff99 2 g_free(m->progress);
1519 zoff99 27 m->progress = g_strdup_printf("Download Map Directory %d%%", (int) (download->offset * 100 / download->zip64_eoc->zip64ecsz));
1520     callback_list_call_attr_0(m->cbl, attr_progress);
1521     if (!download_directory_do(download))
1522     download->state = 8;
1523     break;
1524     case 8:
1525     if (download_directory_finish(download))
1526     download->state = 9;
1527     else
1528     download->state = 0;
1529     break;
1530     case 9:
1531     download_initial_finish(download);
1532     m->fi = download->file;
1533     g_free(m->progress);
1534     m->progress = NULL;
1535 zoff99 2 g_free(download);
1536     if (async)
1537 zoff99 27 m->download = NULL;
1538     map_binfile_open(m);
1539     break;
1540 zoff99 2 }
1541     if (async)
1542     return NULL;
1543     }
1544     }
1545    
1546 zoff99 27 static int push_zipfile_tile(struct map_rect_priv *mr, int zipfile, int offset, int length, int async)
1547 zoff99 2 {
1548 zoff99 27 struct map_priv *m = mr->m;
1549     struct file *f = m->fi;
1550     long long cdoffset = m->eoc64 ? m->eoc64->zip64eofst : m->eoc->zipeofst;
1551     struct zip_cd *cd = (struct zip_cd *) (file_data_read(f, cdoffset + zipfile * m->cde_size, m->cde_size));
1552     // dbg(1,"read from "LONGLONG_FMT" %d bytes\n",cdoffset + zipfile*m->cde_size, m->cde_size);
1553 zoff99 2 cd_to_cpu(cd);
1554 zoff99 27 if (!cd->zipcunc && m->url)
1555     {
1556     cd = download(m, mr, cd, zipfile, offset, length, async);
1557 zoff99 2 if (!cd)
1558     return 1;
1559     }
1560     push_zipfile_tile_do(mr, cd, zipfile, offset, length);
1561     return 0;
1562     }
1563    
1564     static struct map_rect_priv *
1565     map_rect_new_binfile_int(struct map_priv *map, struct map_selection *sel)
1566     {
1567     struct map_rect_priv *mr;
1568    
1569     binfile_check_version(map);
1570 zoff99 27 // dbg(1, "map_rect_new_binfile\n");
1571 zoff99 2 if (!map->fi && !map->url)
1572     return NULL;
1573     map_binfile_http_close(map);
1574     mr=g_new0(struct map_rect_priv, 1);
1575 zoff99 27 mr->m = map;
1576     mr->sel = sel;
1577     mr->item.id_hi = 0;
1578     mr->item.id_lo = 0;
1579     mr->item.meth = &methods_binfile;
1580     mr->item.priv_data = mr;
1581 zoff99 2 return mr;
1582     }
1583    
1584 zoff99 27 static void tile_bbox(char *tile, int len, struct coord_rect *r)
1585 zoff99 2 {
1586 zoff99 27 struct coord c;
1587     int overlap = 1;
1588     int xo, yo;
1589     struct coord_rect world_bbox =
1590     {
1591     { -20000000, 20000000 }, /* lu */
1592     { 20000000, -20000000 }, /* rl */
1593 zoff99 2 };
1594 zoff99 27 *r = world_bbox;
1595     while (len)
1596     {
1597     c.x = (r->lu.x + r->rl.x) / 2;
1598     c.y = (r->lu.y + r->rl.y) / 2;
1599     xo = (r->rl.x - r->lu.x) * overlap / 100;
1600     yo = (r->lu.y - r->rl.y) * overlap / 100;
1601     switch (*tile)
1602     {
1603     case 'a':
1604     r->lu.x = c.x - xo;
1605     r->rl.y = c.y - yo;
1606     break;
1607     case 'b':
1608     r->rl.x = c.x + xo;
1609     r->rl.y = c.y - yo;
1610     break;
1611     case 'c':
1612     r->lu.x = c.x - xo;
1613     r->lu.y = c.y + yo;
1614     break;
1615     case 'd':
1616     r->rl.x = c.x + xo;
1617     r->lu.y = c.y + yo;
1618     break;
1619     default:
1620     return;
1621     }
1622     tile++;
1623     len--;
1624     }
1625 zoff99 2 }
1626    
1627 zoff99 27 static int map_download_selection_check(struct zip_cd *cd, struct map_selection *sel)
1628 zoff99 2 {
1629 zoff99 27 //// dbg(0,"EEnter\n");
1630 zoff99 2
1631     struct coord_rect cd_rect;
1632     if (cd->zipcunc)
1633     return 0;
1634 zoff99 27 tile_bbox((char *) (cd + 1), cd->zipcfnl, &cd_rect);
1635     while (sel)
1636     {
1637 zoff99 2 if (coord_rect_overlap(&cd_rect, &sel->u.c_rect))
1638     return 1;
1639 zoff99 27 sel = sel->next;
1640 zoff99 2 }
1641     return 0;
1642     }
1643    
1644 zoff99 27 static void map_download_selection(struct map_priv *m, struct map_rect_priv *mr, struct map_selection *sel)
1645 zoff99 2 {
1646     int i;
1647     struct zip_cd *cd;
1648 zoff99 27 for (i = 0; i < m->zip_members; i++)
1649     {
1650     cd = binfile_read_cd(m, m->cde_size * i, -1);
1651     if (map_download_selection_check(cd, sel))
1652 zoff99 2 download(m, mr, cd, i, 0, 0, 0);
1653 zoff99 27 file_data_free(m->fi, (unsigned char *) cd);
1654 zoff99 2 }
1655     }
1656    
1657     static struct map_rect_priv *
1658     map_rect_new_binfile(struct map_priv *map, struct map_selection *sel)
1659     {
1660 zoff99 27 struct map_rect_priv *mr = map_rect_new_binfile_int(map, sel);
1661 zoff99 2 struct tile t;
1662 zoff99 27 // dbg(1, "zip_members=%d\n", map->zip_members);
1663     if (map->url && map->fi && sel && sel->order == 255)
1664     {
1665 zoff99 2 map_download_selection(map, mr, sel);
1666     }
1667     if (map->eoc)
1668 zoff99 27 mr->status = 1;
1669     else
1670     {
1671 zoff99 2 unsigned char *d;
1672 zoff99 27 if (map->fi)
1673     {
1674     d = file_data_read(map->fi, 0, map->fi->size);
1675     t.start = (int *) d;
1676     t.end = (int *) (d + map->fi->size);
1677     t.fi = map->fi;
1678     t.zipfile_num = 0;
1679     t.mode = 0;
1680 zoff99 2 push_tile(mr, &t, 0, 0);
1681 zoff99 27 }
1682     else if (map->url && !map->download)
1683     {
1684 zoff99 2 download(map, NULL, NULL, 0, 0, 0, 1);
1685 zoff99 27 mr->status = 1;
1686 zoff99 2 }
1687     }
1688     return mr;
1689     }
1690    
1691 zoff99 27 static void write_changes_do(gpointer key, gpointer value, gpointer user_data)
1692 zoff99 2 {
1693 zoff99 27 struct binfile_hash_entry *entry = key;
1694     FILE *out = user_data;
1695     if (entry->flags)
1696     {
1697     entry->flags = 0;
1698     fwrite(entry, sizeof(*entry) + (le32_to_cpu(entry->data[0]) + 1) * 4, 1, out);
1699     // dbg(0, "yes\n");
1700 zoff99 2 }
1701     }
1702    
1703 zoff99 27 static void write_changes(struct map_priv *m)
1704 zoff99 2 {
1705     FILE *changes;
1706     char *changes_file;
1707     if (!m->changes)
1708     return;
1709 zoff99 27 changes_file = g_strdup_printf("%s.log", m->filename);
1710     changes = fopen(changes_file, "ab");
1711 zoff99 2 g_hash_table_foreach(m->changes, write_changes_do, changes);
1712     fclose(changes);
1713     g_free(changes_file);
1714     }
1715    
1716 zoff99 27 static void load_changes(struct map_priv *m)
1717 zoff99 2 {
1718     FILE *changes;
1719     char *changes_file;
1720 zoff99 27 struct binfile_hash_entry entry, *e;
1721 zoff99 2 int size;
1722 zoff99 27 changes_file = g_strdup_printf("%s.log", m->filename);
1723     changes = fopen(changes_file, "rb");
1724     if (!changes)
1725     {
1726 zoff99 2 g_free(changes_file);
1727     return;
1728     }
1729 zoff99 31 m->changes = g_hash_table_new_full(binfile_hash_entry_hash, binfile_hash_entry_equal, g_free_func, NULL);
1730 zoff99 27 while (fread(&entry, sizeof(entry), 1, changes) == 1)
1731     {
1732 zoff99 2 if (fread(&size, sizeof(size), 1, changes) != 1)
1733     break;
1734 zoff99 27 e = g_malloc(sizeof(struct binfile_hash_entry) + (le32_to_cpu(size) + 1) * 4);
1735     *e = entry;
1736     e->data[0] = size;
1737     if (fread(e->data + 1, le32_to_cpu(size) * 4, 1, changes) != 1)
1738 zoff99 2 break;
1739     g_hash_table_replace(m->changes, e, e);
1740     }
1741     fclose(changes);
1742     g_free(changes_file);
1743     }
1744    
1745 zoff99 27 static void map_rect_destroy_binfile(struct map_rect_priv *mr)
1746 zoff99 2 {
1747     write_changes(mr->m);
1748 zoff99 27 while (pop_tile(mr))
1749     ;
1750 zoff99 2 #ifdef DEBUG_SIZE
1751 zoff99 27 // dbg(0,"size=%d kb\n",mr->size/1024);
1752 zoff99 2 #endif
1753     if (mr->tiles[0].fi && mr->tiles[0].start)
1754 zoff99 27 file_data_free(mr->tiles[0].fi, (unsigned char *) (mr->tiles[0].start));
1755 zoff99 2 g_free(mr->url);
1756     map_binfile_http_close(mr->m);
1757 zoff99 27 g_free(mr);
1758 zoff99 2 }
1759    
1760 zoff99 27 static void setup_pos(struct map_rect_priv *mr)
1761 zoff99 2 {
1762 zoff99 27 int size, coord_size;
1763     struct tile *t = mr->t;
1764     size = le32_to_cpu(t->pos[0]);
1765     if (size > 1024 * 1024 || size < 0)
1766     {
1767     // dbg(0, "size=0x%x\n", size);
1768 zoff99 2 #if 0
1769     fprintf(stderr,"offset=%d\n", (unsigned char *)(mr->pos)-mr->m->f->begin);
1770     #endif
1771 zoff99 27 // dbg(0, "size error");
1772 zoff99 2 }
1773 zoff99 27 t->pos_next = t->pos + size + 1;
1774     mr->item.type = le32_to_cpu(t->pos[1]);
1775     coord_size = le32_to_cpu(t->pos[2]);
1776     t->pos_coord_start = t->pos + 3;
1777     t->pos_attr_start = t->pos_coord_start + coord_size;
1778 zoff99 2 }
1779    
1780 zoff99 27 static int selection_contains(struct map_selection *sel, struct coord_rect *r, struct range *mima)
1781 zoff99 2 {
1782 zoff99 27 //// dbg(0,"EEnter\n");
1783 zoff99 2
1784     int order;
1785 zoff99 27 if (!sel)
1786 zoff99 2 return 1;
1787 zoff99 27 while (sel)
1788     {
1789     if (coord_rect_overlap(r, &sel->u.c_rect))
1790     {
1791     order = sel->order;
1792     // dbg(1, "min %d max %d order %d\n", mima->min, mima->max, order);
1793 zoff99 2 if (!mima->min && !mima->max)
1794     return 1;
1795     if (order >= mima->min && order <= mima->max)
1796     return 1;
1797     }
1798 zoff99 27 sel = sel->next;
1799 zoff99 2 }
1800     return 0;
1801     }
1802    
1803 zoff99 27 static void map_parse_country_binfile(struct map_rect_priv *mr)
1804 zoff99 2 {
1805     struct attr at;
1806 zoff99 27 if (binfile_attr_get(mr->item.priv_data, attr_country_id, &at))
1807     {
1808 zoff99 2 if (at.u.num == mr->country_id)
1809     {
1810     if (binfile_attr_get(mr->item.priv_data, attr_zipfile_ref, &at))
1811     {
1812     push_zipfile_tile(mr, at.u.num, 0, 0, 0);
1813     }
1814     }
1815     }
1816     }
1817    
1818 zoff99 27 static int map_parse_submap(struct map_rect_priv *mr, int async)
1819 zoff99 2 {
1820     struct coord_rect r;
1821     struct coord c[2];
1822     struct attr at;
1823     struct range mima;
1824     if (binfile_coord_get(mr->item.priv_data, c, 2) != 2)
1825     return 0;
1826 zoff99 27 r.lu.x = c[0].x;
1827     r.lu.y = c[1].y;
1828     r.rl.x = c[1].x;
1829     r.rl.y = c[0].y;
1830 zoff99 2 if (!binfile_attr_get(mr->item.priv_data, attr_order, &at))
1831     return 0;
1832     #if __BYTE_ORDER == __BIG_ENDIAN
1833 zoff99 27 mima.min = le16_to_cpu(at.u.range.max);
1834     mima.max = le16_to_cpu(at.u.range.min);
1835 zoff99 2 #else
1836     mima=at.u.range;
1837     #endif
1838     if (!mr->m->eoc || !selection_contains(mr->sel, &r, &mima))
1839     return 0;
1840     if (!binfile_attr_get(mr->item.priv_data, attr_zipfile_ref, &at))
1841     return 0;
1842 zoff99 27 // dbg(1, "pushing zipfile %d from %d\n", at.u.num, mr->t->zipfile_num);
1843 zoff99 2 return push_zipfile_tile(mr, at.u.num, 0, 0, async);
1844     }
1845    
1846 zoff99 27 static int push_modified_item(struct map_rect_priv *mr)
1847 zoff99 2 {
1848     struct item_id id;
1849     struct binfile_hash_entry *entry;
1850 zoff99 27 id.id_hi = mr->item.id_hi;
1851     id.id_lo = mr->item.id_lo;
1852     entry = g_hash_table_lookup(mr->m->changes, &id);
1853     if (entry)
1854     {
1855 zoff99 2 struct tile tn;
1856 zoff99 27 tn.pos_next = tn.pos = tn.start = entry->data;
1857     tn.zipfile_num = mr->item.id_hi;
1858     tn.mode = 2;
1859     tn.end = tn.start + le32_to_cpu(entry->data[0]) + 1;
1860 zoff99 2 push_tile(mr, &tn, 0, 0);
1861     return 1;
1862     }
1863     return 0;
1864     }
1865    
1866     static struct item *
1867     map_rect_get_item_binfile(struct map_rect_priv *mr)
1868     {
1869 zoff99 31 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1870     dbg(0,"+#+:enter\n");
1871     #endif
1872    
1873 zoff99 2 struct tile *t;
1874 zoff99 27 struct map_priv *m = mr->m;
1875 zoff99 31
1876 zoff99 27 if (m->download)
1877     {
1878 zoff99 2 download(m, NULL, NULL, 0, 0, 0, 2);
1879 zoff99 31 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1880     dbg(0,"+#+:leave9\n");
1881     #endif
1882 zoff99 2 return &busy_item;
1883     }
1884 zoff99 31
1885 zoff99 27 if (mr->status == 1)
1886     {
1887     mr->status = 0;
1888     if (push_zipfile_tile(mr, m->zip_members - 1, 0, 0, 1))
1889 zoff99 31 {
1890     #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1891     dbg(0,"+#+:leave8\n");
1892     #endif
1893 zoff99 2 return &busy_item;
1894 zoff99 31 }
1895 zoff99 2 }
1896 zoff99 31
1897 zoff99 27 for (;;)
1898     {
1899     t = mr->t;
1900 zoff99 31
1901 zoff99 27 if (!t)
1902 zoff99 31 {
1903     #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1904     dbg(0,"+#+:leave6\n");
1905     #endif
1906 zoff99 2 return NULL;
1907 zoff99 31 }
1908    
1909 zoff99 27 t->pos = t->pos_next;
1910 zoff99 31
1911 zoff99 27 if (t->pos >= t->end)
1912     {
1913 zoff99 2 if (pop_tile(mr))
1914 zoff99 31 {
1915 zoff99 2 continue;
1916 zoff99 31 }
1917    
1918     #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1919     dbg(0,"+#+:leave5\n");
1920     #endif
1921    
1922 zoff99 2 return NULL;
1923     }
1924 zoff99 31
1925 zoff99 2 setup_pos(mr);
1926     binfile_coord_rewind(mr);
1927     binfile_attr_rewind(mr);
1928 zoff99 27 if ((mr->item.type == type_submap) && (!mr->country_id))
1929     {
1930 zoff99 2 if (map_parse_submap(mr, 1))
1931 zoff99 31 {
1932     #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1933     dbg(0,"+#+:leave4\n");
1934     #endif
1935 zoff99 2 return &busy_item;
1936 zoff99 31 }
1937 zoff99 2 continue;
1938     }
1939 zoff99 27 if (t->mode != 2)
1940     {
1941     mr->item.id_hi = t->zipfile_num;
1942     mr->item.id_lo = t->pos - t->start;
1943 zoff99 2 if (mr->m->changes && push_modified_item(mr))
1944     continue;
1945     }
1946     if (mr->country_id)
1947     {
1948 zoff99 27 if (mr->item.type == type_countryindex)
1949     {
1950 zoff99 2 map_parse_country_binfile(mr);
1951     }
1952     if (item_is_town(mr->item))
1953     {
1954 zoff99 31 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1955     dbg(0,"+#+:leave3\n");
1956     #endif
1957 zoff99 2 return &mr->item;
1958 zoff99 27 }
1959     else
1960     {
1961 zoff99 2 continue;
1962     }
1963     }
1964 zoff99 31 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1965     dbg(0,"+#+:leave 2\n");
1966     #endif
1967 zoff99 2 return &mr->item;
1968     }
1969 zoff99 31
1970     #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1971     dbg(0,"+#+:leave\n");
1972     #endif
1973 zoff99 2 }
1974    
1975     static struct item *
1976     map_rect_get_item_byid_binfile(struct map_rect_priv *mr, int id_hi, int id_lo)
1977     {
1978     struct tile *t;
1979 zoff99 15 if (mr->m->eoc)
1980     {
1981 zoff99 27 while (pop_tile(mr))
1982     ;
1983 zoff99 2 push_zipfile_tile(mr, id_hi, 0, 0, 0);
1984     }
1985 zoff99 27 t = mr->t;
1986     t->pos = t->start + id_lo;
1987     mr->item.id_hi = id_hi;
1988     mr->item.id_lo = id_lo;
1989 zoff99 2 if (mr->m->changes)
1990 zoff99 15 {
1991 zoff99 2 push_modified_item(mr);
1992 zoff99 15 }
1993 zoff99 2 setup_pos(mr);
1994     binfile_coord_rewind(mr);
1995     binfile_attr_rewind(mr);
1996 zoff99 15
1997 zoff99 2 return &mr->item;
1998     }
1999    
2000 zoff99 27 static int binmap_search_by_index(struct map_priv *map, struct item *item, struct map_rect_priv **ret)
2001 zoff99 2 {
2002     struct attr zipfile_ref;
2003     int *data;
2004    
2005 zoff99 27 if (!item)
2006     {
2007     *ret = NULL;
2008 zoff99 2 return 0;
2009     }
2010 zoff99 27 if (item_attr_get(item, attr_item_id, &zipfile_ref))
2011     {
2012     data = zipfile_ref.u.data;
2013     *ret = map_rect_new_binfile_int(map, NULL);
2014 zoff99 2 push_zipfile_tile(*ret, le32_to_cpu(data[0]), le32_to_cpu(data[1]), -1, 0);
2015     return 3;
2016     }
2017 zoff99 27 if (item_attr_get(item, attr_zipfile_ref, &zipfile_ref))
2018     {
2019     *ret = map_rect_new_binfile_int(map, NULL);
2020 zoff99 2 push_zipfile_tile(*ret, zipfile_ref.u.num, 0, 0, 0);
2021     return 1;
2022     }
2023 zoff99 27 if (item_attr_get(item, attr_zipfile_ref_block, &zipfile_ref))
2024     {
2025     data = zipfile_ref.u.data;
2026     *ret = map_rect_new_binfile_int(map, NULL);
2027 zoff99 2 push_zipfile_tile(*ret, le32_to_cpu(data[0]), le32_to_cpu(data[1]), le32_to_cpu(data[2]), 0);
2028     return 2;
2029     }
2030 zoff99 27 *ret = NULL;
2031 zoff99 2 return 0;
2032     }
2033    
2034     static struct map_rect_priv *
2035     binmap_search_street_by_place(struct map_priv *map, struct item *town, struct coord *c, struct map_selection *sel)
2036     {
2037     struct attr town_name, poly_town_name;
2038     struct map_rect_priv *map_rec2;
2039     struct item *place;
2040 zoff99 27 int found = 0;
2041 zoff99 2
2042     if (!item_attr_get(town, attr_label, &town_name))
2043     return NULL;
2044     sel->range = item_range_all;
2045     sel->order = 18;
2046     sel->next = NULL;
2047 zoff99 27 sel->u.c_rect.lu = *c;
2048     sel->u.c_rect.rl = *c;
2049     map_rec2 = map_rect_new_binfile(map, sel);
2050     while ((place = map_rect_get_item_binfile(map_rec2)))
2051     {
2052     if (item_is_poly_place(*place) && item_attr_get(place, attr_label, &poly_town_name) && !strcmp(poly_town_name.u.str, town_name.u.str))
2053     {
2054     struct coord c[128];
2055     int i, count;
2056     found = 1;
2057     while ((count = item_coord_get(place, c, 128)))
2058     {
2059     for (i = 0; i < count; i++)
2060     {
2061     coord_rect_extend(&sel->u.c_rect, &c[i]);
2062 zoff99 2 }
2063 zoff99 27 }
2064 zoff99 2 }
2065     }
2066 zoff99 27
2067 zoff99 2 map_rect_destroy_binfile(map_rec2);
2068     if (found)
2069 zoff99 27 {
2070 zoff99 2 return map_rect_new_binfile(map, sel);
2071 zoff99 27 }
2072 zoff99 2 return NULL;
2073     }
2074    
2075     static struct map_rect_priv *
2076     binmap_search_street_by_estimate(struct map_priv *map, struct item *town, struct coord *c, struct map_selection *sel)
2077     {
2078     int size = 10000;
2079 zoff99 27 switch (town->type)
2080     {
2081 zoff99 2 case type_town_label_1e7:
2082     case type_town_label_5e6:
2083     case type_town_label_2e6:
2084     case type_town_label_1e6:
2085     case type_town_label_5e5:
2086     case type_town_label_2e5:
2087     size = 10000;
2088     break;
2089     case type_town_label_1e5:
2090     case type_town_label_5e4:
2091     case type_town_label_2e4:
2092     size = 5000;
2093     break;
2094     case type_town_label_1e4:
2095     case type_town_label_5e3:
2096     case type_town_label_2e3:
2097     size = 2500;
2098     break;
2099     case type_town_label_1e3:
2100     case type_town_label_5e2:
2101     case type_town_label_2e2:
2102     case type_town_label_1e2:
2103     case type_town_label_5e1:
2104     case type_town_label_2e1:
2105     case type_town_label_1e1:
2106     case type_town_label_5e0:
2107     case type_town_label_2e0:
2108     case type_town_label_1e0:
2109     case type_town_label_0e0:
2110     size = 1000;
2111     break;
2112     default:
2113     break;
2114     }
2115 zoff99 27
2116     if (global_search_street_size_factor > 1)
2117     {
2118     size = size * global_search_street_size_factor;
2119     }
2120    
2121     sel->u.c_rect.lu.x = c->x - size;
2122     sel->u.c_rect.lu.y = c->y + size;
2123     sel->u.c_rect.rl.x = c->x + size;
2124     sel->u.c_rect.rl.y = c->y - size;
2125 zoff99 2 return map_rect_new_binfile(map, sel);
2126     }
2127    
2128     static struct map_rect_priv *
2129     binmap_search_housenumber_by_estimate(struct map_priv *map, struct coord *c, struct map_selection *sel)
2130     {
2131     int size = 20;
2132 zoff99 27 sel->u.c_rect.lu.x = c->x - size;
2133     sel->u.c_rect.lu.y = c->y + size;
2134     sel->u.c_rect.rl.x = c->x + size;
2135     sel->u.c_rect.rl.y = c->y - size;
2136 zoff99 2
2137     sel->range = item_range_all;
2138     sel->order = 18;
2139     //sel->next = NULL;
2140    
2141     return map_rect_new_binfile(map, sel);
2142     }
2143    
2144     static struct map_search_priv *
2145     binmap_search_new(struct map_priv *map, struct item *item, struct attr *search, int partial)
2146     {
2147     struct map_rect_priv *map_rec;
2148     struct map_search_priv *msp=g_new0(struct map_search_priv, 1);
2149     struct item *town;
2150 zoff99 27 int willsearch = 0;
2151    
2152 zoff99 2 msp->search = search;
2153     msp->partial = partial;
2154     /*
2155 zoff99 27 * NOTE: If you implement search for other attributes than attr_town_name and attr_street_name,
2156     * please update this comment and the documentation for map_search_new() in map.c
2157     */
2158 zoff99 15 switch (search->type)
2159     {
2160 zoff99 2 case attr_country_name:
2161     break;
2162     case attr_town_name:
2163     case attr_town_or_district_name:
2164     map_rec = map_rect_new_binfile(map, NULL);
2165 zoff99 27 if (!map_rec)
2166 zoff99 28 {
2167 zoff99 2 break;
2168 zoff99 28 }
2169 zoff99 2 map_rec->country_id = item->id_lo;
2170     msp->mr = map_rec;
2171 zoff99 27 willsearch = 1;
2172 zoff99 2 break;
2173     case attr_town_postal:
2174     break;
2175     case attr_street_name:
2176 zoff99 27 if (!item->map)
2177 zoff99 2 break;
2178     if (!map_priv_is(item->map, map))
2179     break;
2180     map_rec = map_rect_new_binfile(map, NULL);
2181     town = map_rect_get_item_byid_binfile(map_rec, item->id_hi, item->id_lo);
2182 zoff99 15 if (town)
2183     {
2184 zoff99 2 struct coord c;
2185    
2186     if (binmap_search_by_index(map, town, &msp->mr))
2187 zoff99 15 {
2188 zoff99 30 //dbg(0, "search:mode=1\n");
2189 zoff99 2 msp->mode = 1;
2190 zoff99 15 }
2191     else
2192     {
2193     if (item_coord_get(town, &c, 1))
2194     {
2195 zoff99 27 if ((msp->mr = binmap_search_street_by_place(map, town, &c, &msp->ms)))
2196 zoff99 15 {
2197 zoff99 30 //dbg(0, "search:mode=2\n");
2198 zoff99 2 msp->mode = 2;
2199 zoff99 15 }
2200     else
2201     {
2202 zoff99 27 msp->mr = binmap_search_street_by_estimate(map, town, &c, &msp->ms);
2203 zoff99 30 //dbg(0, "search:mode=3\n");
2204 zoff99 2 msp->mode = 3;
2205 zoff99 27 }
2206 zoff99 2 }
2207     }
2208     map_rect_destroy_binfile(map_rec);
2209     if (!msp->mr)
2210 zoff99 15 {
2211 zoff99 2 break;
2212 zoff99 15 }
2213 zoff99 27 willsearch = 1;
2214 zoff99 2 break;
2215     }
2216     map_rect_destroy_binfile(map_rec);
2217     break;
2218     case attr_house_number:
2219 zoff99 27 // dbg(1, "case house_number");
2220     if (!item->map)
2221 zoff99 2 break;
2222     if (!map_priv_is(item->map, map))
2223     break;
2224 zoff99 27 msp->map = map;
2225 zoff99 2 msp->mr_item = map_rect_new_binfile(map, NULL);
2226     msp->item = map_rect_get_item_byid_binfile(msp->mr_item, item->id_hi, item->id_lo);
2227 zoff99 15 if (binmap_search_by_index(map, msp->item, &msp->mr) != 3)
2228     {
2229 zoff99 2 struct coord c;
2230     if (item_coord_get(msp->item, &c, 1))
2231     {
2232 zoff99 27 msp->mr = binmap_search_housenumber_by_estimate(map, &c, &msp->ms);
2233 zoff99 2 msp->mode = 2;
2234     }
2235     map_rect_destroy_binfile(msp->mr_item);
2236 zoff99 27 msp->mr_item = NULL;
2237 zoff99 2 }
2238     if (!msp->mr)
2239     {
2240     break;
2241     }
2242 zoff99 27 willsearch = 1;
2243 zoff99 2 break;
2244     default:
2245     break;
2246     }
2247 zoff99 15
2248     if (!willsearch)
2249     {
2250 zoff99 2 g_free(msp);
2251 zoff99 27 msp = NULL;
2252 zoff99 15 }
2253     else
2254     {
2255 zoff99 27 msp->str = linguistics_casefold(search->u.str);
2256 zoff99 2 }
2257     return msp;
2258     }
2259    
2260 zoff99 27 static int ascii_cmp(char *name, char *match, int partial)
2261 zoff99 2 {
2262 zoff99 27 int ret = 1;
2263 zoff99 15
2264 zoff99 27 if (name == NULL)
2265 zoff99 15 {
2266     return ret;
2267     }
2268    
2269 zoff99 27 if (match == NULL)
2270 zoff99 15 {
2271     return ret;
2272     }
2273    
2274 zoff99 27 char *s2 = linguistics_casefold(match); // user entered search string
2275     char *s1 = linguistics_casefold(name); // string from mapfile
2276     char *s1_1 = linguistics_expand_special(s1, 1);
2277     char *s2_1 = linguistics_expand_special(s2, 1);
2278 zoff99 15
2279     if (s1_1)
2280     {
2281     if (s2_1)
2282     {
2283 zoff99 27 ret = linguistics_compare(s1_1, s2_1, partial);
2284 zoff99 15 }
2285     else
2286     {
2287 zoff99 27 ret = linguistics_compare(s1_1, s2, partial);
2288 zoff99 15 }
2289     }
2290     else
2291     {
2292     if (s2_1)
2293     {
2294 zoff99 27 ret = linguistics_compare(s1, s2_1, partial);
2295 zoff99 15 }
2296     else
2297     {
2298 zoff99 27 ret = linguistics_compare(s1, s2, partial);
2299 zoff99 15 }
2300     }
2301    
2302     if (s1_1)
2303     g_free(s1_1);
2304     if (s2_1)
2305     g_free(s2_1);
2306     if (s1)
2307     g_free(s1);
2308     if (s2)
2309     g_free(s2);
2310 zoff99 2 return ret;
2311     }
2312    
2313     struct duplicate
2314     {
2315     struct coord c;
2316     char str[0];
2317     };
2318    
2319 zoff99 27 static guint duplicate_hash(gconstpointer key)
2320 zoff99 2 {
2321 zoff99 27 const struct duplicate *d = key;
2322     return d->c.x ^ d->c.y ^ g_str_hash(d->str);
2323 zoff99 2 }
2324    
2325 zoff99 27 static gboolean duplicate_equal(gconstpointer a, gconstpointer b)
2326 zoff99 2 {
2327 zoff99 27 const struct duplicate *da = a;
2328     const struct duplicate *db = b;
2329     return (da->c.x == db->c.x && da->c.y == da->c.y && g_str_equal(da->str, db->str));
2330 zoff99 2 }
2331    
2332 zoff99 27 static int duplicate(struct map_search_priv *msp, struct item *item, enum attr_type attr_type)
2333 zoff99 2 {
2334     struct attr attr;
2335     if (!msp->search_results)
2336 zoff99 31 msp->search_results = g_hash_table_new_full(duplicate_hash, duplicate_equal, g_free_func, NULL);
2337 zoff99 2 binfile_attr_rewind(item->priv_data);
2338     if (!item_attr_get(item, attr_type, &attr))
2339     return 1;
2340     {
2341 zoff99 27 int len = sizeof(struct coord) + strlen(attr.u.str) + 1;
2342     char *buffer = g_alloca(sizeof(char) * len);
2343     struct duplicate *d = (struct duplicate *) buffer;
2344     if (!item_coord_get(item, &d->c, 1))
2345     {
2346     d->c.x = 0;
2347     d->c.y = 0;
2348 zoff99 2 }
2349     binfile_coord_rewind(item->priv_data);
2350     strcpy(d->str, attr.u.str);
2351 zoff99 27 if (!g_hash_table_lookup(msp->search_results, d))
2352     {
2353     struct duplicate *dc = g_malloc(len);
2354 zoff99 2 memcpy(dc, d, len);
2355     g_hash_table_insert(msp->search_results, dc, GINT_TO_POINTER(1));
2356     binfile_attr_rewind(item->priv_data);
2357     return 0;
2358     }
2359     }
2360     return 2;
2361     }
2362    
2363     static struct item *
2364     binmap_search_get_item(struct map_search_priv *map_search)
2365     {
2366     struct item* it;
2367     struct attr at;
2368    
2369 zoff99 27 // // dbg(0,"Enter\n");
2370    
2371 zoff99 15 for (;;)
2372     {
2373 zoff99 27 while ((it = map_rect_get_item_binfile(map_search->mr)))
2374 zoff99 15 {
2375     switch (map_search->search->type)
2376     {
2377 zoff99 27 case attr_town_name:
2378     case attr_district_name:
2379     case attr_town_or_district_name:
2380 zoff99 15
2381     #if 0
2382 zoff99 27 if (map_search->mr->tile_depth > 1 && item_is_town(*it) && !item_is_district(*it) && map_search->search->type != attr_district_name)
2383     {
2384     if (binfile_attr_get(it->priv_data, attr_town_name_match, &at) || binfile_attr_get(it->priv_data, attr_town_name, &at))
2385     {
2386     if (!ascii_cmp(at.u.str, map_search->str, map_search->partial) && !duplicate(map_search, it, attr_town_name))
2387 zoff99 2 return it;
2388 zoff99 27 }
2389 zoff99 2 }
2390 zoff99 15 #endif
2391    
2392     #if 1
2393 zoff99 27 if (map_search->mr->tile_depth > 1 && item_is_town(*it) && !item_is_district(*it) && map_search->search->type != attr_district_name)
2394 zoff99 15 {
2395 zoff99 27 if (binfile_attr_get(it->priv_data, attr_town_name, &at))
2396 zoff99 15 {
2397 zoff99 27 //// dbg(0,"22town n:%s\n",at.u.str);
2398     if (!ascii_cmp(at.u.str, map_search->str, map_search->partial)
2399     // && !duplicate(map_search, it, attr_town_name)
2400 zoff99 15 )
2401 zoff99 27 {
2402     //// dbg(0,"town n:%s\n",at.u.str);
2403     return it;
2404     }
2405 zoff99 15 }
2406    
2407 zoff99 27 if (binfile_attr_get(it->priv_data, attr_town_name_match, &at))
2408 zoff99 15 {
2409 zoff99 27 //// dbg(0,"11town M:%s\n",at.u.str);
2410     if (!ascii_cmp(at.u.str, map_search->str, map_search->partial)
2411     // && !duplicate(map_search, it, attr_town_name)
2412     )
2413 zoff99 15 {
2414 zoff99 27 //// dbg(0,"town M:%s\n",at.u.str);
2415 zoff99 15 return it;
2416     }
2417 zoff99 2 }
2418     }
2419 zoff99 27 #endif
2420 zoff99 15
2421 zoff99 27 if (map_search->mr->tile_depth > 1 && item_is_district(*it) && map_search->search->type != attr_town_name)
2422 zoff99 15 {
2423 zoff99 27 if (binfile_attr_get(it->priv_data, attr_district_name_match, &at) || binfile_attr_get(it->priv_data, attr_district_name, &at))
2424 zoff99 15 {
2425 zoff99 27 if (!ascii_cmp(at.u.str, map_search->str, map_search->partial) && !duplicate(map_search, it, attr_town_name))
2426 zoff99 15 return it;
2427     }
2428     }
2429 zoff99 27 break;
2430 zoff99 15
2431 zoff99 27 case attr_street_name:
2432 zoff99 15
2433 zoff99 27 if (map_search->mode == 1)
2434 zoff99 15 {
2435 zoff99 27 //dbg(0,"*in here 00*\n");
2436 zoff99 15
2437 zoff99 27 //if (binfile_attr_get(it->priv_data, attr_street_name_match, &at) || binfile_attr_get(it->priv_data, attr_street_name, &at)) {
2438     // if (!ascii_cmp(at.u.str, map_search->str, map_search->partial) && !duplicate(map_search, it, attr_street_name)) {
2439     // return it;
2440     // }
2441     //}
2442 zoff99 15
2443 zoff99 27 if (binfile_attr_get(it->priv_data, attr_street_name, &at))
2444     {
2445     //// dbg(0,"str AA:%s\n", at.u.str);
2446 zoff99 15
2447 zoff99 27 if (!ascii_cmp(at.u.str, map_search->str, map_search->partial))
2448 zoff99 15 {
2449 zoff99 27 if ((offline_search_filter_duplicates != 1) || (!duplicate(map_search, it, attr_street_name)))
2450 zoff99 15 {
2451 zoff99 27 return it;
2452 zoff99 15 }
2453     else
2454     {
2455 zoff99 27 //// dbg(0,"dup street 1:%s\n",at.u.str);
2456 zoff99 15 }
2457 zoff99 27 }
2458     }
2459 zoff99 15
2460 zoff99 27 if (binfile_attr_get(it->priv_data, attr_street_name_match, &at))
2461     {
2462     //// dbg(0,"str BB:%s\n", at.u.str);
2463    
2464     if (!ascii_cmp(at.u.str, map_search->str, map_search->partial))
2465 zoff99 15 {
2466 zoff99 27 if ((offline_search_filter_duplicates != 1) || (!duplicate(map_search, it, attr_street_name_match)))
2467 zoff99 15 {
2468     return it;
2469     }
2470 zoff99 27 else
2471     {
2472     //// dbg(0,"dup street 2:%s\n",at.u.str);
2473     }
2474 zoff99 15 }
2475 zoff99 2 }
2476 zoff99 27
2477     continue;
2478 zoff99 2 }
2479 zoff99 27
2480     #if 1
2481     // ------------------------------------
2482     // map_search->mode == 2 or 3
2483     // ------------------------------------
2484     if (item_is_street(*it))
2485 zoff99 2 {
2486 zoff99 27 // dbg(0,"*in here 11*\n");
2487    
2488     struct attr at;
2489     if (map_selection_contains_item_rect(map_search->mr->sel, it))
2490 zoff99 2 {
2491 zoff99 27 if (binfile_attr_get(it->priv_data, attr_label, &at))
2492     {
2493     int match = 0;
2494     char *str = g_strdup(at.u.str);
2495     // dbg(0,"search=%s str is:%s\n",map_search->str, at.u.str);
2496     if (!ascii_cmp(str, map_search->str, map_search->partial))
2497     {
2498     match = 1;
2499     }
2500     g_free(str);
2501     if (match)
2502     {
2503     if ((offline_search_filter_duplicates != 1) || (!duplicate(map_search, it, attr_label)))
2504     {
2505     item_coord_rewind(it);
2506     return it;
2507     }
2508     }
2509     }
2510 zoff99 15
2511 zoff99 27 if (binfile_attr_get(it->priv_data, attr_street_name_match, &at))
2512 zoff99 2 {
2513 zoff99 27 int match = 0;
2514     char *str = g_strdup(at.u.str);
2515     // dbg(0,"search=%s mstr is:%s\n",map_search->str, at.u.str);
2516     if (!ascii_cmp(str, map_search->str, map_search->partial))
2517     {
2518     match = 1;
2519     }
2520     g_free(str);
2521     if (match)
2522     {
2523     if ((offline_search_filter_duplicates != 1) || (!duplicate(map_search, it, attr_street_name_match)))
2524     {
2525     item_coord_rewind(it);
2526     return it;
2527     }
2528     }
2529 zoff99 2 }
2530 zoff99 27 //else
2531     //{
2532     // if (binfile_attr_get(it->priv_data, attr_street_name, &at))
2533     // {
2534     // // dbg(0,"---str X*X:%s\n", at.u.str);
2535     // }
2536     //}
2537 zoff99 2 }
2538 zoff99 15 }
2539 zoff99 27 #endif
2540     break;
2541     case attr_house_number:
2542     //if (it->type == type_house_number)
2543     if ((it->type == type_house_number) || (type_house_number_interpolation_even) || (type_house_number_interpolation_odd) || (type_house_number_interpolation_all))
2544 zoff99 15 {
2545 zoff99 27 // is it a housenumber?
2546     if (binfile_attr_get(it->priv_data, attr_house_number, &at))
2547     {
2548     // match housenumber to our string
2549     if (!ascii_cmp(at.u.str, map_search->str, map_search->partial))
2550     {
2551     //binfile_attr_get(it->priv_data, attr_street_name, &at);
2552     //// dbg(0,"hnnn B1 street_name=%s",at.u.str);
2553    
2554     if (!duplicate(map_search, it, attr_house_number))
2555     {
2556     binfile_attr_rewind(it->priv_data);
2557     return it;
2558     }
2559     }
2560     }
2561     else
2562     {
2563     return it;
2564     }
2565 zoff99 15 }
2566 zoff99 27 continue;
2567     default:
2568     return NULL;
2569 zoff99 2 }
2570     }
2571 zoff99 27
2572 zoff99 2 if (!map_search->mr_item)
2573 zoff99 27 {
2574 zoff99 2 return NULL;
2575 zoff99 27 }
2576 zoff99 2 map_rect_destroy_binfile(map_search->mr);
2577     if (!binmap_search_by_index(map_search->map, map_search->item, &map_search->mr))
2578 zoff99 27 {
2579 zoff99 2 return NULL;
2580 zoff99 27 }
2581     } // endless for-loop
2582    
2583     return NULL; // this is never reached
2584 zoff99 2 }
2585    
2586 zoff99 27 static void binmap_search_destroy(struct map_search_priv *ms)
2587 zoff99 2 {
2588     if (ms->search_results)
2589     g_hash_table_destroy(ms->search_results);
2590     if (ms->mr_item)
2591     map_rect_destroy_binfile(ms->mr_item);
2592     if (ms->mr)
2593     map_rect_destroy_binfile(ms->mr);
2594     if (ms->str)
2595     g_free(ms->str);
2596     g_free(ms);
2597     }
2598    
2599 zoff99 27 static int binmap_get_attr(struct map_priv *m, enum attr_type type, struct attr *attr)
2600 zoff99 2 {
2601 zoff99 27 attr->type = type;
2602     switch (type)
2603     {
2604     case attr_map_release:
2605     if (m->map_release)
2606     {
2607     attr->u.str = m->map_release;
2608     return 1;
2609     }
2610     break;
2611     case attr_progress:
2612     if (m->progress)
2613     {
2614     attr->u.str = m->progress;
2615     return 1;
2616     }
2617     default:
2618     break;
2619 zoff99 2 }
2620     return 0;
2621     }
2622    
2623 zoff99 27 static int binmap_set_attr(struct map_priv *map, struct attr *attr)
2624 zoff99 2 {
2625 zoff99 27 switch (attr->type)
2626     {
2627     case attr_update:
2628     map->download_enabled = attr->u.num;
2629     return 1;
2630     default:
2631     return 0;
2632 zoff99 2 }
2633    
2634     }
2635    
2636 zoff99 27 static struct map_methods map_methods_binfile =
2637     { projection_mg, "utf-8", map_destroy_binfile, map_rect_new_binfile, map_rect_destroy_binfile, map_rect_get_item_binfile, map_rect_get_item_byid_binfile, binmap_search_new, binmap_search_destroy, binmap_search_get_item, NULL, binmap_get_attr, binmap_set_attr, };
2638 zoff99 2
2639 zoff99 27 static int binfile_get_index(struct map_priv *m)
2640 zoff99 2 {
2641 zoff99 27 //dbg(0,"_enter\n");
2642 zoff99 2 int len;
2643     int cde_index_size;
2644 zoff99 27 long long offset; // int or long long ?
2645 zoff99 2 struct zip_cd *cd;
2646    
2647     len = strlen("index");
2648 zoff99 27 cde_index_size = sizeof(struct zip_cd) + len;
2649     //dbg(0,"_cde_index_size=%d\n", cde_index_size);
2650     if (m->eoc64)
2651     {
2652     //dbg(0,"_m->eoc64->zip64ecsz=%d\n", m->eoc64->zip64ecsz);
2653     offset = m->eoc64->zip64ecsz - cde_index_size;
2654     //dbg(0,"_eoc64 offset="LONGLONG_FMT"\n", offset);
2655     }
2656 zoff99 2 else
2657 zoff99 27 {
2658     //dbg(0,"_m->eoc->zipecsz=%d\n", m->eoc->zipecsz);
2659     offset = m->eoc->zipecsz - cde_index_size;
2660     //dbg(0,"_eoc offset="LONGLONG_FMT"\n", offset);
2661     }
2662 zoff99 2 cd = binfile_read_cd(m, offset, len);
2663    
2664 zoff99 27 if (!cd)
2665     {
2666     //dbg(0,"_!cd\n");
2667     cde_index_size += sizeof(struct zip_cd_ext);
2668     //dbg(0,"_cde_index_size=%d\n", cde_index_size);
2669 zoff99 2 if (m->eoc64)
2670 zoff99 27 {
2671     offset = m->eoc64->zip64ecsz - cde_index_size;
2672     //dbg(0,"_eoc64 offset aa.1="LONGLONG_FMT"\n", offset);
2673     }
2674 zoff99 2 else
2675 zoff99 27 {
2676     offset = m->eoc->zipecsz - cde_index_size;
2677     //dbg(0,"_offset 22.0="LONGLONG_FMT"\n", offset);
2678     }
2679     cd = binfile_read_cd(m, offset, len + sizeof(struct zip_cd_ext));
2680 zoff99 2 }
2681 zoff99 27 //dbg(0,"_offset 22="LONGLONG_FMT"\n", offset);
2682    
2683     if (cd)
2684     {
2685     //dbg(0,"_cd 001\n");
2686     if (cd->zipcfnl == len && !strncmp(cd->zipcfn, "index", len))
2687     {
2688     m->index_offset = offset;
2689     m->index_cd = cd;
2690     //dbg(0,"_leave:offset 33="LONGLONG_FMT"\n", offset);
2691 zoff99 2 return 1;
2692     }
2693 zoff99 27 //dbg(0,"_cd 002\n");
2694 zoff99 2 }
2695 zoff99 27
2696     offset = binfile_search_cd(m, 0, "index", 0, 0);
2697     //dbg(0,"_offset 44="LONGLONG_FMT"\n", offset);
2698    
2699 zoff99 2 if (offset == -1)
2700 zoff99 27 {
2701     //dbg(0,"_leave:55\n");
2702 zoff99 2 return 0;
2703 zoff99 27 }
2704    
2705     cd = binfile_read_cd(m, offset, -1);
2706    
2707 zoff99 2 if (!cd)
2708 zoff99 27 {
2709     //dbg(0,"_leave:66\n");
2710 zoff99 2 return 0;
2711 zoff99 27 }
2712    
2713     m->index_offset = offset;
2714     m->index_cd = cd;
2715    
2716     //dbg(0,"_leave:normal:offset 77="LONGLONG_FMT"\n", offset);
2717 zoff99 2 return 1;
2718     }
2719    
2720 zoff99 27 static int map_binfile_zip_setup(struct map_priv *m, char *filename, int mmap)
2721 zoff99 2 {
2722 zoff99 27 //dbg(0,"_enter\n");
2723    
2724 zoff99 2 struct zip_cd *first_cd;
2725     int i;
2726 zoff99 27 if (!(m->eoc = binfile_read_eoc(m->fi)))
2727     {
2728     //dbg(0,"_leave:1\n");
2729 zoff99 2 return 0;
2730 zoff99 27 }
2731 zoff99 2 dbg_assert(m->eoc->zipedsk == m->eoc->zipecen);
2732 zoff99 27 if (m->eoc->zipedsk && strlen(filename) > 3)
2733     {
2734     char *tmpfilename = g_strdup(filename);
2735     char *ext = tmpfilename + strlen(tmpfilename) - 3;
2736    
2737     //dbg(0,"_ff-1:%s\n", tmpfilename);
2738     //dbg(0,"_ff-2:%s\n", ext);
2739    
2740 zoff99 2 m->fis=g_new(struct file *,m->eoc->zipedsk);
2741 zoff99 27 for (i = 0; i < m->eoc->zipedsk - 1; i++)
2742     {
2743     sprintf(ext, "b%02d", i + 1);
2744     //dbg(0,"_ff--3:%s\n", ext);
2745     m->fis[i] = file_create(tmpfilename, 0);
2746 zoff99 2 if (mmap)
2747 zoff99 27 {
2748     //dbg(0,"_want mmap 2 %s\n", m->fis[i]->name);
2749 zoff99 2 file_mmap(m->fis[i]);
2750 zoff99 27 }
2751 zoff99 2 }
2752 zoff99 27 m->fis[m->eoc->zipedsk - 1] = m->fi;
2753 zoff99 2 g_free(tmpfilename);
2754     }
2755 zoff99 27 else
2756     {
2757     //dbg(0,"_ff-XX\n");
2758     }
2759     //dbg(0, "_num_disk %d\n", m->eoc->zipedsk);
2760     m->eoc64 = binfile_read_eoc64(m->fi);
2761    
2762 zoff99 2 if (!binfile_get_index(m))
2763 zoff99 27 {
2764     //dbg(0,"_leave:2\n");
2765 zoff99 2 return 0;
2766 zoff99 27 }
2767    
2768     if (!(first_cd = binfile_read_cd(m, 0, 0)))
2769     {
2770     //dbg(0,"_leave:3\n");
2771 zoff99 2 return 0;
2772 zoff99 27 }
2773    
2774     m->cde_size = sizeof(struct zip_cd) + first_cd->zipcfnl + first_cd->zipcxtl;
2775     m->zip_members = m->index_offset / m->cde_size + 1;
2776     //dbg(0, "_cde_size %d\n", m->cde_size);
2777     //dbg(0, "_members %d\n", m->zip_members);
2778     file_data_free(m->fi, (unsigned char *) first_cd);
2779 zoff99 2 if (mmap)
2780 zoff99 27 {
2781     //dbg(0,"_want mmap 3 %s\n", m->fi->name);
2782 zoff99 2 file_mmap(m->fi);
2783 zoff99 27 }
2784 zoff99 2 return 1;
2785     }
2786    
2787     #if 0
2788     static int
2789     map_binfile_download_initial(struct map_priv *m)
2790     {
2791 zoff99 27 struct attr readwrite=
2792     { attr_readwrite,
2793     { (void *)1}};
2794     struct attr create=
2795     { attr_create,
2796     { (void *)1}};
2797 zoff99 2 struct attr *attrs[4];
2798     struct file *out;
2799     long long woffset=0,planet_size;
2800     int size_ret;
2801     int cd1size,cdisize;
2802     long long cd1offset,cdioffset;
2803     struct zip64_eoc *zip64_eoc;
2804     struct zip64_eocl *zip64_eocl;
2805     struct zip_eoc *zip_eoc;
2806     struct zip_cd *cd1,*cdn,*cdi;
2807     int count,chunk,cdoffset=0;
2808     int mode=1;
2809     struct map_download *download=g_new0(struct map_download, 1);
2810    
2811     attrs[0]=&readwrite;
2812     attrs[1]=&create;
2813     attrs[2]=NULL;
2814     download->file=file_create(m->filename,attrs);
2815     download->m=m;
2816     download_planet_size(download);
2817     download_eoc(download);
2818     download_directory_start(download);
2819     while (download_directory_do(download));
2820     download_directory_finish(download);
2821     download_initial_finish(download);
2822     m->fi=download->file;
2823     g_free(download);
2824     return 1;
2825    
2826 zoff99 27 cd1size=sizeof(*cd1);
2827     cd1offset=zip64_eoc->zip64eofst;
2828     cd1=(struct zip_cd *)map_binfile_download_range(m, cd1offset, cd1size);
2829     if (!cd1)
2830     return 0;
2831     cd1size=sizeof(*cd1)+binfile_cd_extra(cd1);
2832     g_free(cd1);
2833     cd1=(struct zip_cd *)map_binfile_download_range(m, cd1offset, cd1size);
2834     if (!cd1)
2835     return 0;
2836     cd1->zipcunc=0;
2837     cdisize=sizeof(*cdi)+strlen("index")+cd1->zipcxtl;
2838     cdioffset=zip64_eoc->zip64eofst+zip64_eoc->zip64ecsz-cdisize;
2839     cdi=(struct zip_cd *)map_binfile_download_range(m, cdioffset, cdisize);
2840     if (!cdi)
2841     {
2842 zoff99 2 g_free(cd1);
2843 zoff99 27 return 0;
2844     }
2845     cdi->zipcunc=0;
2846     cdn=g_malloc0(cd1size*256);
2847 zoff99 2
2848 zoff99 27 file_data_write(out, woffset, sizeof(*zip64_eoc), (unsigned char *)zip64_eoc);
2849     woffset+=sizeof(*zip64_eoc);
2850     cdoffset=woffset;
2851 zoff99 2
2852 zoff99 27 file_data_write(out, woffset, cd1size, (unsigned char *)cd1);
2853     woffset+=cd1size;
2854     count=(cdioffset-cd1offset)/cd1size-1;
2855     while (count > 0)
2856     {
2857     if (count > 256)
2858     chunk=256;
2859     else
2860     chunk=count;
2861     file_data_write(out, woffset, cd1size*chunk, (unsigned char *)cdn);
2862     woffset+=cd1size*chunk;
2863     count-=chunk;
2864     }
2865     g_free(cdn);
2866     g_free(cd1);
2867     file_data_write(out, woffset, cdisize, (unsigned char *)cdi);
2868     woffset+=cdisize;
2869 zoff99 2
2870     }
2871     #endif
2872    
2873 zoff99 27 static int map_binfile_open(struct map_priv *m)
2874 zoff99 2 {
2875     int *magic;
2876     struct map_rect_priv *mr;
2877     struct item *item;
2878     struct attr attr;
2879 zoff99 27 struct attr readwrite =
2880     { attr_readwrite,
2881     { (void *) 1 } };
2882     struct attr *attrs[] =
2883     { &readwrite, NULL };
2884 zoff99 2
2885 zoff99 27 // dbg(0, "file_create %s\n", m->filename);
2886     m->fi = file_create(m->filename, m->url ? attrs : NULL);
2887     if (!m->fi && m->url)
2888     {
2889 zoff99 2 return 0;
2890 zoff99 27 }
2891     if (!m->fi)
2892     {
2893     // dbg(0, "Failed to load '%s'\n", m->filename);
2894 zoff99 2 return 0;
2895     }
2896     if (m->check_version)
2897 zoff99 27 {
2898     m->version = file_version(m->fi, m->check_version);
2899     }
2900    
2901     magic = (int *) file_data_read(m->fi, 0, 4);
2902     if (!magic)
2903     {
2904 zoff99 2 file_destroy(m->fi);
2905 zoff99 27 m->fi = NULL;
2906 zoff99 2 return 0;
2907     }
2908 zoff99 27
2909     //dbg(0,"_zip_lfh_sig=%d\n", zip_lfh_sig);
2910    
2911     //dbg(0,"_magic1=%d\n", magic[0]);
2912     //dbg(0,"_magic1=%d\n", magic[1]);
2913     //dbg(0,"_magic1=%d\n", magic[2]);
2914     //dbg(0,"_magic1=%d\n", magic[3]);
2915 zoff99 2 *magic = le32_to_cpu(*magic);
2916 zoff99 27 //dbg(0,"_magic2=%d\n", magic[0]);
2917     //dbg(0,"_magic2=%d\n", magic[1]);
2918     //dbg(0,"_magic2=%d\n", magic[2]);
2919     //dbg(0,"_magic2=%d\n", magic[3]);
2920     if (*magic == zip_lfh_sig || *magic == zip_split_sig || *magic == zip_cd_sig || *magic == zip64_eoc_sig)
2921     {
2922     if (!map_binfile_zip_setup(m, m->filename, m->flags & 1))
2923     {
2924     dbg(0, "invalid file format for '%s'\n", m->filename);
2925 zoff99 2 file_destroy(m->fi);
2926 zoff99 27 m->fi = NULL;
2927 zoff99 2 return 0;
2928     }
2929 zoff99 27 }
2930     else
2931     {
2932     //dbg(0,"_want mmap %s\n", m->fi->name);
2933 zoff99 2 file_mmap(m->fi);
2934 zoff99 27 }
2935    
2936     //dbg(0,"_cont\n");
2937    
2938     file_data_free(m->fi, (unsigned char *) magic);
2939     m->cachedir = g_strdup("/tmp/navit");
2940     m->map_version = 0;
2941    
2942     mr = map_rect_new_binfile(m, NULL);
2943     if (mr)
2944     {
2945     while ((item = map_rect_get_item_binfile(mr)) == &busy_item)
2946     {
2947     ;
2948     }
2949 zoff99 30
2950 zoff99 27 if (item && item->type == type_map_information)
2951     {
2952 zoff99 2 if (binfile_attr_get(item->priv_data, attr_version, &attr))
2953 zoff99 27 {
2954     m->map_version = attr.u.num;
2955     dbg(0, "map_version=%d\n", m->map_version);
2956     }
2957 zoff99 2 if (binfile_attr_get(item->priv_data, attr_map_release, &attr))
2958 zoff99 27 {
2959     m->map_release = g_strdup(attr.u.str);
2960     dbg(0, "map_release=%s\n", m->map_release);
2961     }
2962     if (m->url && binfile_attr_get(item->priv_data, attr_url, &attr))
2963     {
2964 zoff99 30 //dbg(0, "url config %s map %s\n", m->url, attr.u.str);
2965 zoff99 27 if (strcmp(m->url, attr.u.str))
2966     {
2967     m->update_available = 1;
2968     }
2969 zoff99 2 g_free(m->url);
2970 zoff99 27 m->url = g_strdup(attr.u.str);
2971 zoff99 2 }
2972     }
2973 zoff99 30
2974 zoff99 2 map_rect_destroy_binfile(mr);
2975 zoff99 30
2976 zoff99 27 if (m->map_version >= 16)
2977     {
2978     dbg(0, "Warning: This map is incompatible with your versionof ZANavi. Please update ZANavi.\n");
2979 zoff99 2 return 0;
2980     }
2981     }
2982     return 1;
2983     }
2984    
2985 zoff99 27 static void map_binfile_close(struct map_priv *m)
2986 zoff99 2 {
2987     int i;
2988 zoff99 27 file_data_free(m->fi, (unsigned char *) m->index_cd);
2989     file_data_free(m->fi, (unsigned char *) m->eoc);
2990     file_data_free(m->fi, (unsigned char *) m->eoc64);
2991 zoff99 2 g_free(m->cachedir);
2992     g_free(m->map_release);
2993 zoff99 27 if (m->fis)
2994     {
2995     for (i = 0; i < m->eoc->zipedsk; i++)
2996     {
2997 zoff99 2 file_destroy(m->fis[i]);
2998     }
2999 zoff99 27 }
3000     else
3001 zoff99 2 file_destroy(m->fi);
3002     }
3003    
3004 zoff99 27 static void map_binfile_destroy(struct map_priv *m)
3005 zoff99 2 {
3006     g_free(m->filename);
3007     g_free(m->url);
3008     g_free(m->progress);
3009     g_free(m);
3010     }
3011    
3012 zoff99 27 static void binfile_check_version(struct map_priv *m)
3013 zoff99 2 {
3014 zoff99 27 int version = -1;
3015 zoff99 2 if (!m->check_version)
3016     return;
3017 zoff99 27 if (m->fi)
3018     version = file_version(m->fi, m->check_version);
3019     if (version != m->version)
3020     {
3021 zoff99 2 if (m->fi)
3022     map_binfile_close(m);
3023     map_binfile_open(m);
3024     }
3025     }
3026    
3027     static struct map_priv *
3028     map_new_binfile(struct map_methods *meth, struct attr **attrs, struct callback_list *cbl)
3029     {
3030     struct map_priv *m;
3031 zoff99 27 struct attr *data = attr_search(attrs, NULL, attr_data);
3032     struct attr *check_version, *map_pass, *flags, *url, *download_enabled;
3033 zoff99 2 struct file_wordexp *wexp;
3034     char **wexp_data;
3035 zoff99 27 if (!data)
3036 zoff99 2 return NULL;
3037    
3038 zoff99 31 // wexp = file_wordexp_new(data->u.str);
3039     // wexp_data = file_wordexp_get_array(wexp);
3040 zoff99 27 // dbg(0, "map_new_binfile %s\n", data->u.str);
3041     *meth = map_methods_binfile;
3042 zoff99 2
3043     m=g_new0(struct map_priv, 1);
3044 zoff99 27 m->cbl = cbl;
3045     m->id = ++map_id;
3046 zoff99 31 m->filename = g_strdup(data->u.str);
3047     // file_wordexp_destroy(wexp);
3048 zoff99 27 check_version = attr_search(attrs, NULL, attr_check_version);
3049     if (check_version)
3050     m->check_version = check_version->u.num;
3051     map_pass = attr_search(attrs, NULL, attr_map_pass);
3052 zoff99 2 if (map_pass)
3053 zoff99 27 m->passwd = g_strdup(map_pass->u.str);
3054     flags = attr_search(attrs, NULL, attr_flags);
3055 zoff99 2 if (flags)
3056 zoff99 27 m->flags = flags->u.num;
3057     url = attr_search(attrs, NULL, attr_url);
3058 zoff99 2 if (url)
3059 zoff99 27 m->url = g_strdup(url->u.str);
3060 zoff99 2 download_enabled = attr_search(attrs, NULL, attr_update);
3061     if (download_enabled)
3062 zoff99 27 m->download_enabled = download_enabled->u.num;
3063 zoff99 2
3064 zoff99 27 if (!map_binfile_open(m) && !m->check_version && !m->url)
3065     {
3066 zoff99 2 map_binfile_destroy(m);
3067 zoff99 27 m = NULL;
3068     }
3069     else
3070     {
3071 zoff99 2 load_changes(m);
3072     }
3073     return m;
3074     }
3075    
3076 zoff99 27 void plugin_init(void)
3077 zoff99 2 {
3078 zoff99 27 // dbg(1, "binfile: plugin_init\n");
3079     if (sizeof(struct zip_cd) != 46)
3080     {
3081     // dbg(0, "error: sizeof(struct zip_cd)=%d\n", sizeof(struct zip_cd));
3082 zoff99 2 }
3083     plugin_register_map_type("binfile", map_new_binfile);
3084     }
3085    

   
Visit the ZANavi Wiki