/[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 34 - (hide annotations) (download)
Sat Dec 14 12:13:17 2013 UTC (10 years, 4 months ago) by zoff99
File MIME type: text/plain
File size: 77479 byte(s)
lots of fixes and changes, new world map
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 34 attr->u.num |= NAVIT_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 zoff99 34 {
1573 zoff99 2 return NULL;
1574 zoff99 34 }
1575 zoff99 2 map_binfile_http_close(map);
1576     mr=g_new0(struct map_rect_priv, 1);
1577 zoff99 27 mr->m = map;
1578     mr->sel = sel;
1579     mr->item.id_hi = 0;
1580     mr->item.id_lo = 0;
1581     mr->item.meth = &methods_binfile;
1582     mr->item.priv_data = mr;
1583 zoff99 2 return mr;
1584     }
1585    
1586 zoff99 27 static void tile_bbox(char *tile, int len, struct coord_rect *r)
1587 zoff99 2 {
1588 zoff99 27 struct coord c;
1589     int overlap = 1;
1590     int xo, yo;
1591     struct coord_rect world_bbox =
1592     {
1593     { -20000000, 20000000 }, /* lu */
1594     { 20000000, -20000000 }, /* rl */
1595 zoff99 2 };
1596 zoff99 27 *r = world_bbox;
1597     while (len)
1598     {
1599     c.x = (r->lu.x + r->rl.x) / 2;
1600     c.y = (r->lu.y + r->rl.y) / 2;
1601     xo = (r->rl.x - r->lu.x) * overlap / 100;
1602     yo = (r->lu.y - r->rl.y) * overlap / 100;
1603     switch (*tile)
1604     {
1605     case 'a':
1606     r->lu.x = c.x - xo;
1607     r->rl.y = c.y - yo;
1608     break;
1609     case 'b':
1610     r->rl.x = c.x + xo;
1611     r->rl.y = c.y - yo;
1612     break;
1613     case 'c':
1614     r->lu.x = c.x - xo;
1615     r->lu.y = c.y + yo;
1616     break;
1617     case 'd':
1618     r->rl.x = c.x + xo;
1619     r->lu.y = c.y + yo;
1620     break;
1621     default:
1622     return;
1623     }
1624     tile++;
1625     len--;
1626     }
1627 zoff99 2 }
1628    
1629 zoff99 27 static int map_download_selection_check(struct zip_cd *cd, struct map_selection *sel)
1630 zoff99 2 {
1631 zoff99 27 //// dbg(0,"EEnter\n");
1632 zoff99 2
1633     struct coord_rect cd_rect;
1634     if (cd->zipcunc)
1635     return 0;
1636 zoff99 27 tile_bbox((char *) (cd + 1), cd->zipcfnl, &cd_rect);
1637     while (sel)
1638     {
1639 zoff99 2 if (coord_rect_overlap(&cd_rect, &sel->u.c_rect))
1640     return 1;
1641 zoff99 27 sel = sel->next;
1642 zoff99 2 }
1643     return 0;
1644     }
1645    
1646 zoff99 27 static void map_download_selection(struct map_priv *m, struct map_rect_priv *mr, struct map_selection *sel)
1647 zoff99 2 {
1648     int i;
1649     struct zip_cd *cd;
1650 zoff99 27 for (i = 0; i < m->zip_members; i++)
1651     {
1652     cd = binfile_read_cd(m, m->cde_size * i, -1);
1653     if (map_download_selection_check(cd, sel))
1654 zoff99 2 download(m, mr, cd, i, 0, 0, 0);
1655 zoff99 27 file_data_free(m->fi, (unsigned char *) cd);
1656 zoff99 2 }
1657     }
1658    
1659     static struct map_rect_priv *
1660     map_rect_new_binfile(struct map_priv *map, struct map_selection *sel)
1661     {
1662 zoff99 27 struct map_rect_priv *mr = map_rect_new_binfile_int(map, sel);
1663 zoff99 2 struct tile t;
1664 zoff99 27 // dbg(1, "zip_members=%d\n", map->zip_members);
1665     if (map->url && map->fi && sel && sel->order == 255)
1666     {
1667 zoff99 2 map_download_selection(map, mr, sel);
1668     }
1669     if (map->eoc)
1670 zoff99 27 mr->status = 1;
1671     else
1672     {
1673 zoff99 2 unsigned char *d;
1674 zoff99 27 if (map->fi)
1675     {
1676     d = file_data_read(map->fi, 0, map->fi->size);
1677     t.start = (int *) d;
1678     t.end = (int *) (d + map->fi->size);
1679     t.fi = map->fi;
1680     t.zipfile_num = 0;
1681     t.mode = 0;
1682 zoff99 2 push_tile(mr, &t, 0, 0);
1683 zoff99 27 }
1684     else if (map->url && !map->download)
1685     {
1686 zoff99 2 download(map, NULL, NULL, 0, 0, 0, 1);
1687 zoff99 27 mr->status = 1;
1688 zoff99 2 }
1689     }
1690     return mr;
1691     }
1692    
1693 zoff99 27 static void write_changes_do(gpointer key, gpointer value, gpointer user_data)
1694 zoff99 2 {
1695 zoff99 27 struct binfile_hash_entry *entry = key;
1696     FILE *out = user_data;
1697     if (entry->flags)
1698     {
1699     entry->flags = 0;
1700     fwrite(entry, sizeof(*entry) + (le32_to_cpu(entry->data[0]) + 1) * 4, 1, out);
1701     // dbg(0, "yes\n");
1702 zoff99 2 }
1703     }
1704    
1705 zoff99 27 static void write_changes(struct map_priv *m)
1706 zoff99 2 {
1707     FILE *changes;
1708     char *changes_file;
1709     if (!m->changes)
1710     return;
1711 zoff99 27 changes_file = g_strdup_printf("%s.log", m->filename);
1712     changes = fopen(changes_file, "ab");
1713 zoff99 2 g_hash_table_foreach(m->changes, write_changes_do, changes);
1714     fclose(changes);
1715     g_free(changes_file);
1716     }
1717    
1718 zoff99 27 static void load_changes(struct map_priv *m)
1719 zoff99 2 {
1720     FILE *changes;
1721     char *changes_file;
1722 zoff99 27 struct binfile_hash_entry entry, *e;
1723 zoff99 2 int size;
1724 zoff99 27 changes_file = g_strdup_printf("%s.log", m->filename);
1725     changes = fopen(changes_file, "rb");
1726     if (!changes)
1727     {
1728 zoff99 2 g_free(changes_file);
1729     return;
1730     }
1731 zoff99 31 m->changes = g_hash_table_new_full(binfile_hash_entry_hash, binfile_hash_entry_equal, g_free_func, NULL);
1732 zoff99 27 while (fread(&entry, sizeof(entry), 1, changes) == 1)
1733     {
1734 zoff99 2 if (fread(&size, sizeof(size), 1, changes) != 1)
1735     break;
1736 zoff99 27 e = g_malloc(sizeof(struct binfile_hash_entry) + (le32_to_cpu(size) + 1) * 4);
1737     *e = entry;
1738     e->data[0] = size;
1739     if (fread(e->data + 1, le32_to_cpu(size) * 4, 1, changes) != 1)
1740 zoff99 2 break;
1741     g_hash_table_replace(m->changes, e, e);
1742     }
1743     fclose(changes);
1744     g_free(changes_file);
1745     }
1746    
1747 zoff99 27 static void map_rect_destroy_binfile(struct map_rect_priv *mr)
1748 zoff99 2 {
1749     write_changes(mr->m);
1750 zoff99 27 while (pop_tile(mr))
1751     ;
1752 zoff99 2 #ifdef DEBUG_SIZE
1753 zoff99 27 // dbg(0,"size=%d kb\n",mr->size/1024);
1754 zoff99 2 #endif
1755     if (mr->tiles[0].fi && mr->tiles[0].start)
1756 zoff99 27 file_data_free(mr->tiles[0].fi, (unsigned char *) (mr->tiles[0].start));
1757 zoff99 2 g_free(mr->url);
1758     map_binfile_http_close(mr->m);
1759 zoff99 27 g_free(mr);
1760 zoff99 2 }
1761    
1762 zoff99 27 static void setup_pos(struct map_rect_priv *mr)
1763 zoff99 2 {
1764 zoff99 27 int size, coord_size;
1765     struct tile *t = mr->t;
1766     size = le32_to_cpu(t->pos[0]);
1767     if (size > 1024 * 1024 || size < 0)
1768     {
1769     // dbg(0, "size=0x%x\n", size);
1770 zoff99 2 #if 0
1771     fprintf(stderr,"offset=%d\n", (unsigned char *)(mr->pos)-mr->m->f->begin);
1772     #endif
1773 zoff99 27 // dbg(0, "size error");
1774 zoff99 2 }
1775 zoff99 27 t->pos_next = t->pos + size + 1;
1776     mr->item.type = le32_to_cpu(t->pos[1]);
1777     coord_size = le32_to_cpu(t->pos[2]);
1778     t->pos_coord_start = t->pos + 3;
1779     t->pos_attr_start = t->pos_coord_start + coord_size;
1780 zoff99 2 }
1781    
1782 zoff99 27 static int selection_contains(struct map_selection *sel, struct coord_rect *r, struct range *mima)
1783 zoff99 2 {
1784 zoff99 27 //// dbg(0,"EEnter\n");
1785 zoff99 2
1786     int order;
1787 zoff99 27 if (!sel)
1788 zoff99 2 return 1;
1789 zoff99 27 while (sel)
1790     {
1791     if (coord_rect_overlap(r, &sel->u.c_rect))
1792     {
1793     order = sel->order;
1794     // dbg(1, "min %d max %d order %d\n", mima->min, mima->max, order);
1795 zoff99 2 if (!mima->min && !mima->max)
1796     return 1;
1797     if (order >= mima->min && order <= mima->max)
1798     return 1;
1799     }
1800 zoff99 27 sel = sel->next;
1801 zoff99 2 }
1802     return 0;
1803     }
1804    
1805 zoff99 27 static void map_parse_country_binfile(struct map_rect_priv *mr)
1806 zoff99 2 {
1807     struct attr at;
1808 zoff99 27 if (binfile_attr_get(mr->item.priv_data, attr_country_id, &at))
1809     {
1810 zoff99 2 if (at.u.num == mr->country_id)
1811     {
1812     if (binfile_attr_get(mr->item.priv_data, attr_zipfile_ref, &at))
1813     {
1814     push_zipfile_tile(mr, at.u.num, 0, 0, 0);
1815     }
1816     }
1817     }
1818     }
1819    
1820 zoff99 27 static int map_parse_submap(struct map_rect_priv *mr, int async)
1821 zoff99 2 {
1822     struct coord_rect r;
1823     struct coord c[2];
1824     struct attr at;
1825     struct range mima;
1826     if (binfile_coord_get(mr->item.priv_data, c, 2) != 2)
1827     return 0;
1828 zoff99 27 r.lu.x = c[0].x;
1829     r.lu.y = c[1].y;
1830     r.rl.x = c[1].x;
1831     r.rl.y = c[0].y;
1832 zoff99 2 if (!binfile_attr_get(mr->item.priv_data, attr_order, &at))
1833     return 0;
1834     #if __BYTE_ORDER == __BIG_ENDIAN
1835 zoff99 27 mima.min = le16_to_cpu(at.u.range.max);
1836     mima.max = le16_to_cpu(at.u.range.min);
1837 zoff99 2 #else
1838     mima=at.u.range;
1839     #endif
1840     if (!mr->m->eoc || !selection_contains(mr->sel, &r, &mima))
1841     return 0;
1842     if (!binfile_attr_get(mr->item.priv_data, attr_zipfile_ref, &at))
1843     return 0;
1844 zoff99 27 // dbg(1, "pushing zipfile %d from %d\n", at.u.num, mr->t->zipfile_num);
1845 zoff99 2 return push_zipfile_tile(mr, at.u.num, 0, 0, async);
1846     }
1847    
1848 zoff99 27 static int push_modified_item(struct map_rect_priv *mr)
1849 zoff99 2 {
1850     struct item_id id;
1851     struct binfile_hash_entry *entry;
1852 zoff99 27 id.id_hi = mr->item.id_hi;
1853     id.id_lo = mr->item.id_lo;
1854     entry = g_hash_table_lookup(mr->m->changes, &id);
1855     if (entry)
1856     {
1857 zoff99 2 struct tile tn;
1858 zoff99 27 tn.pos_next = tn.pos = tn.start = entry->data;
1859     tn.zipfile_num = mr->item.id_hi;
1860     tn.mode = 2;
1861     tn.end = tn.start + le32_to_cpu(entry->data[0]) + 1;
1862 zoff99 2 push_tile(mr, &tn, 0, 0);
1863     return 1;
1864     }
1865     return 0;
1866     }
1867    
1868     static struct item *
1869     map_rect_get_item_binfile(struct map_rect_priv *mr)
1870     {
1871 zoff99 31 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1872     dbg(0,"+#+:enter\n");
1873     #endif
1874    
1875 zoff99 2 struct tile *t;
1876 zoff99 27 struct map_priv *m = mr->m;
1877 zoff99 31
1878 zoff99 27 if (m->download)
1879     {
1880 zoff99 2 download(m, NULL, NULL, 0, 0, 0, 2);
1881 zoff99 31 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1882     dbg(0,"+#+:leave9\n");
1883     #endif
1884 zoff99 2 return &busy_item;
1885     }
1886 zoff99 31
1887 zoff99 27 if (mr->status == 1)
1888     {
1889     mr->status = 0;
1890     if (push_zipfile_tile(mr, m->zip_members - 1, 0, 0, 1))
1891 zoff99 31 {
1892     #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1893     dbg(0,"+#+:leave8\n");
1894     #endif
1895 zoff99 2 return &busy_item;
1896 zoff99 31 }
1897 zoff99 2 }
1898 zoff99 31
1899 zoff99 27 for (;;)
1900     {
1901     t = mr->t;
1902 zoff99 31
1903 zoff99 27 if (!t)
1904 zoff99 31 {
1905     #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1906     dbg(0,"+#+:leave6\n");
1907     #endif
1908 zoff99 2 return NULL;
1909 zoff99 31 }
1910    
1911 zoff99 27 t->pos = t->pos_next;
1912 zoff99 31
1913 zoff99 27 if (t->pos >= t->end)
1914     {
1915 zoff99 2 if (pop_tile(mr))
1916 zoff99 31 {
1917 zoff99 2 continue;
1918 zoff99 31 }
1919    
1920     #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1921     dbg(0,"+#+:leave5\n");
1922     #endif
1923    
1924 zoff99 2 return NULL;
1925     }
1926 zoff99 31
1927 zoff99 2 setup_pos(mr);
1928     binfile_coord_rewind(mr);
1929     binfile_attr_rewind(mr);
1930 zoff99 27 if ((mr->item.type == type_submap) && (!mr->country_id))
1931     {
1932 zoff99 2 if (map_parse_submap(mr, 1))
1933 zoff99 31 {
1934     #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1935     dbg(0,"+#+:leave4\n");
1936     #endif
1937 zoff99 2 return &busy_item;
1938 zoff99 31 }
1939 zoff99 2 continue;
1940     }
1941 zoff99 27 if (t->mode != 2)
1942     {
1943     mr->item.id_hi = t->zipfile_num;
1944     mr->item.id_lo = t->pos - t->start;
1945 zoff99 2 if (mr->m->changes && push_modified_item(mr))
1946     continue;
1947     }
1948     if (mr->country_id)
1949     {
1950 zoff99 27 if (mr->item.type == type_countryindex)
1951     {
1952 zoff99 2 map_parse_country_binfile(mr);
1953     }
1954     if (item_is_town(mr->item))
1955     {
1956 zoff99 31 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1957     dbg(0,"+#+:leave3\n");
1958     #endif
1959 zoff99 2 return &mr->item;
1960 zoff99 27 }
1961     else
1962     {
1963 zoff99 2 continue;
1964     }
1965     }
1966 zoff99 31 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1967     dbg(0,"+#+:leave 2\n");
1968     #endif
1969 zoff99 2 return &mr->item;
1970     }
1971 zoff99 31
1972     #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1973     dbg(0,"+#+:leave\n");
1974     #endif
1975 zoff99 2 }
1976    
1977     static struct item *
1978     map_rect_get_item_byid_binfile(struct map_rect_priv *mr, int id_hi, int id_lo)
1979     {
1980     struct tile *t;
1981 zoff99 15 if (mr->m->eoc)
1982     {
1983 zoff99 27 while (pop_tile(mr))
1984     ;
1985 zoff99 2 push_zipfile_tile(mr, id_hi, 0, 0, 0);
1986     }
1987 zoff99 27 t = mr->t;
1988     t->pos = t->start + id_lo;
1989     mr->item.id_hi = id_hi;
1990     mr->item.id_lo = id_lo;
1991 zoff99 2 if (mr->m->changes)
1992 zoff99 15 {
1993 zoff99 2 push_modified_item(mr);
1994 zoff99 15 }
1995 zoff99 2 setup_pos(mr);
1996     binfile_coord_rewind(mr);
1997     binfile_attr_rewind(mr);
1998 zoff99 15
1999 zoff99 2 return &mr->item;
2000     }
2001    
2002 zoff99 27 static int binmap_search_by_index(struct map_priv *map, struct item *item, struct map_rect_priv **ret)
2003 zoff99 2 {
2004     struct attr zipfile_ref;
2005     int *data;
2006    
2007 zoff99 27 if (!item)
2008     {
2009     *ret = NULL;
2010 zoff99 2 return 0;
2011     }
2012 zoff99 27 if (item_attr_get(item, attr_item_id, &zipfile_ref))
2013     {
2014     data = zipfile_ref.u.data;
2015     *ret = map_rect_new_binfile_int(map, NULL);
2016 zoff99 2 push_zipfile_tile(*ret, le32_to_cpu(data[0]), le32_to_cpu(data[1]), -1, 0);
2017     return 3;
2018     }
2019 zoff99 27 if (item_attr_get(item, attr_zipfile_ref, &zipfile_ref))
2020     {
2021     *ret = map_rect_new_binfile_int(map, NULL);
2022 zoff99 2 push_zipfile_tile(*ret, zipfile_ref.u.num, 0, 0, 0);
2023     return 1;
2024     }
2025 zoff99 27 if (item_attr_get(item, attr_zipfile_ref_block, &zipfile_ref))
2026     {
2027     data = zipfile_ref.u.data;
2028     *ret = map_rect_new_binfile_int(map, NULL);
2029 zoff99 2 push_zipfile_tile(*ret, le32_to_cpu(data[0]), le32_to_cpu(data[1]), le32_to_cpu(data[2]), 0);
2030     return 2;
2031     }
2032 zoff99 27 *ret = NULL;
2033 zoff99 2 return 0;
2034     }
2035    
2036     static struct map_rect_priv *
2037     binmap_search_street_by_place(struct map_priv *map, struct item *town, struct coord *c, struct map_selection *sel)
2038     {
2039     struct attr town_name, poly_town_name;
2040     struct map_rect_priv *map_rec2;
2041     struct item *place;
2042 zoff99 27 int found = 0;
2043 zoff99 2
2044     if (!item_attr_get(town, attr_label, &town_name))
2045     return NULL;
2046     sel->range = item_range_all;
2047     sel->order = 18;
2048     sel->next = NULL;
2049 zoff99 27 sel->u.c_rect.lu = *c;
2050     sel->u.c_rect.rl = *c;
2051     map_rec2 = map_rect_new_binfile(map, sel);
2052     while ((place = map_rect_get_item_binfile(map_rec2)))
2053     {
2054     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))
2055     {
2056     struct coord c[128];
2057     int i, count;
2058     found = 1;
2059     while ((count = item_coord_get(place, c, 128)))
2060     {
2061     for (i = 0; i < count; i++)
2062     {
2063     coord_rect_extend(&sel->u.c_rect, &c[i]);
2064 zoff99 2 }
2065 zoff99 27 }
2066 zoff99 2 }
2067     }
2068 zoff99 27
2069 zoff99 2 map_rect_destroy_binfile(map_rec2);
2070     if (found)
2071 zoff99 27 {
2072 zoff99 2 return map_rect_new_binfile(map, sel);
2073 zoff99 27 }
2074 zoff99 2 return NULL;
2075     }
2076    
2077     static struct map_rect_priv *
2078     binmap_search_street_by_estimate(struct map_priv *map, struct item *town, struct coord *c, struct map_selection *sel)
2079     {
2080     int size = 10000;
2081 zoff99 27 switch (town->type)
2082     {
2083 zoff99 2 case type_town_label_1e7:
2084     case type_town_label_5e6:
2085     case type_town_label_2e6:
2086     case type_town_label_1e6:
2087     case type_town_label_5e5:
2088     case type_town_label_2e5:
2089     size = 10000;
2090     break;
2091     case type_town_label_1e5:
2092     case type_town_label_5e4:
2093     case type_town_label_2e4:
2094     size = 5000;
2095     break;
2096     case type_town_label_1e4:
2097     case type_town_label_5e3:
2098     case type_town_label_2e3:
2099     size = 2500;
2100     break;
2101     case type_town_label_1e3:
2102     case type_town_label_5e2:
2103     case type_town_label_2e2:
2104     case type_town_label_1e2:
2105     case type_town_label_5e1:
2106     case type_town_label_2e1:
2107     case type_town_label_1e1:
2108     case type_town_label_5e0:
2109     case type_town_label_2e0:
2110     case type_town_label_1e0:
2111     case type_town_label_0e0:
2112     size = 1000;
2113     break;
2114     default:
2115     break;
2116     }
2117 zoff99 27
2118     if (global_search_street_size_factor > 1)
2119     {
2120     size = size * global_search_street_size_factor;
2121     }
2122    
2123     sel->u.c_rect.lu.x = c->x - size;
2124     sel->u.c_rect.lu.y = c->y + size;
2125     sel->u.c_rect.rl.x = c->x + size;
2126     sel->u.c_rect.rl.y = c->y - size;
2127 zoff99 2 return map_rect_new_binfile(map, sel);
2128     }
2129    
2130     static struct map_rect_priv *
2131     binmap_search_housenumber_by_estimate(struct map_priv *map, struct coord *c, struct map_selection *sel)
2132     {
2133     int size = 20;
2134 zoff99 27 sel->u.c_rect.lu.x = c->x - size;
2135     sel->u.c_rect.lu.y = c->y + size;
2136     sel->u.c_rect.rl.x = c->x + size;
2137     sel->u.c_rect.rl.y = c->y - size;
2138 zoff99 2
2139     sel->range = item_range_all;
2140     sel->order = 18;
2141     //sel->next = NULL;
2142    
2143     return map_rect_new_binfile(map, sel);
2144     }
2145    
2146     static struct map_search_priv *
2147     binmap_search_new(struct map_priv *map, struct item *item, struct attr *search, int partial)
2148     {
2149     struct map_rect_priv *map_rec;
2150     struct map_search_priv *msp=g_new0(struct map_search_priv, 1);
2151     struct item *town;
2152 zoff99 27 int willsearch = 0;
2153    
2154 zoff99 2 msp->search = search;
2155     msp->partial = partial;
2156     /*
2157 zoff99 27 * NOTE: If you implement search for other attributes than attr_town_name and attr_street_name,
2158     * please update this comment and the documentation for map_search_new() in map.c
2159     */
2160 zoff99 15 switch (search->type)
2161     {
2162 zoff99 2 case attr_country_name:
2163     break;
2164     case attr_town_name:
2165     case attr_town_or_district_name:
2166     map_rec = map_rect_new_binfile(map, NULL);
2167 zoff99 27 if (!map_rec)
2168 zoff99 28 {
2169 zoff99 2 break;
2170 zoff99 28 }
2171 zoff99 2 map_rec->country_id = item->id_lo;
2172     msp->mr = map_rec;
2173 zoff99 27 willsearch = 1;
2174 zoff99 2 break;
2175     case attr_town_postal:
2176     break;
2177     case attr_street_name:
2178 zoff99 27 if (!item->map)
2179 zoff99 2 break;
2180     if (!map_priv_is(item->map, map))
2181     break;
2182     map_rec = map_rect_new_binfile(map, NULL);
2183     town = map_rect_get_item_byid_binfile(map_rec, item->id_hi, item->id_lo);
2184 zoff99 15 if (town)
2185     {
2186 zoff99 2 struct coord c;
2187    
2188     if (binmap_search_by_index(map, town, &msp->mr))
2189 zoff99 15 {
2190 zoff99 30 //dbg(0, "search:mode=1\n");
2191 zoff99 2 msp->mode = 1;
2192 zoff99 15 }
2193     else
2194     {
2195     if (item_coord_get(town, &c, 1))
2196     {
2197 zoff99 27 if ((msp->mr = binmap_search_street_by_place(map, town, &c, &msp->ms)))
2198 zoff99 15 {
2199 zoff99 30 //dbg(0, "search:mode=2\n");
2200 zoff99 2 msp->mode = 2;
2201 zoff99 15 }
2202     else
2203     {
2204 zoff99 27 msp->mr = binmap_search_street_by_estimate(map, town, &c, &msp->ms);
2205 zoff99 30 //dbg(0, "search:mode=3\n");
2206 zoff99 2 msp->mode = 3;
2207 zoff99 27 }
2208 zoff99 2 }
2209     }
2210     map_rect_destroy_binfile(map_rec);
2211     if (!msp->mr)
2212 zoff99 15 {
2213 zoff99 2 break;
2214 zoff99 15 }
2215 zoff99 27 willsearch = 1;
2216 zoff99 2 break;
2217     }
2218     map_rect_destroy_binfile(map_rec);
2219     break;
2220     case attr_house_number:
2221 zoff99 27 // dbg(1, "case house_number");
2222     if (!item->map)
2223 zoff99 2 break;
2224     if (!map_priv_is(item->map, map))
2225     break;
2226 zoff99 27 msp->map = map;
2227 zoff99 2 msp->mr_item = map_rect_new_binfile(map, NULL);
2228     msp->item = map_rect_get_item_byid_binfile(msp->mr_item, item->id_hi, item->id_lo);
2229 zoff99 15 if (binmap_search_by_index(map, msp->item, &msp->mr) != 3)
2230     {
2231 zoff99 2 struct coord c;
2232     if (item_coord_get(msp->item, &c, 1))
2233     {
2234 zoff99 27 msp->mr = binmap_search_housenumber_by_estimate(map, &c, &msp->ms);
2235 zoff99 2 msp->mode = 2;
2236     }
2237     map_rect_destroy_binfile(msp->mr_item);
2238 zoff99 27 msp->mr_item = NULL;
2239 zoff99 2 }
2240     if (!msp->mr)
2241     {
2242     break;
2243     }
2244 zoff99 27 willsearch = 1;
2245 zoff99 2 break;
2246     default:
2247     break;
2248     }
2249 zoff99 15
2250     if (!willsearch)
2251     {
2252 zoff99 2 g_free(msp);
2253 zoff99 27 msp = NULL;
2254 zoff99 15 }
2255     else
2256     {
2257 zoff99 27 msp->str = linguistics_casefold(search->u.str);
2258 zoff99 2 }
2259     return msp;
2260     }
2261    
2262 zoff99 27 static int ascii_cmp(char *name, char *match, int partial)
2263 zoff99 2 {
2264 zoff99 27 int ret = 1;
2265 zoff99 15
2266 zoff99 27 if (name == NULL)
2267 zoff99 15 {
2268     return ret;
2269     }
2270    
2271 zoff99 27 if (match == NULL)
2272 zoff99 15 {
2273     return ret;
2274     }
2275    
2276 zoff99 27 char *s2 = linguistics_casefold(match); // user entered search string
2277     char *s1 = linguistics_casefold(name); // string from mapfile
2278     char *s1_1 = linguistics_expand_special(s1, 1);
2279     char *s2_1 = linguistics_expand_special(s2, 1);
2280 zoff99 15
2281     if (s1_1)
2282     {
2283     if (s2_1)
2284     {
2285 zoff99 27 ret = linguistics_compare(s1_1, s2_1, partial);
2286 zoff99 15 }
2287     else
2288     {
2289 zoff99 27 ret = linguistics_compare(s1_1, s2, partial);
2290 zoff99 15 }
2291     }
2292     else
2293     {
2294     if (s2_1)
2295     {
2296 zoff99 27 ret = linguistics_compare(s1, s2_1, partial);
2297 zoff99 15 }
2298     else
2299     {
2300 zoff99 27 ret = linguistics_compare(s1, s2, partial);
2301 zoff99 15 }
2302     }
2303    
2304     if (s1_1)
2305     g_free(s1_1);
2306     if (s2_1)
2307     g_free(s2_1);
2308     if (s1)
2309     g_free(s1);
2310     if (s2)
2311     g_free(s2);
2312 zoff99 2 return ret;
2313     }
2314    
2315     struct duplicate
2316     {
2317     struct coord c;
2318     char str[0];
2319     };
2320    
2321 zoff99 27 static guint duplicate_hash(gconstpointer key)
2322 zoff99 2 {
2323 zoff99 27 const struct duplicate *d = key;
2324     return d->c.x ^ d->c.y ^ g_str_hash(d->str);
2325 zoff99 2 }
2326    
2327 zoff99 27 static gboolean duplicate_equal(gconstpointer a, gconstpointer b)
2328 zoff99 2 {
2329 zoff99 27 const struct duplicate *da = a;
2330     const struct duplicate *db = b;
2331     return (da->c.x == db->c.x && da->c.y == da->c.y && g_str_equal(da->str, db->str));
2332 zoff99 2 }
2333    
2334 zoff99 27 static int duplicate(struct map_search_priv *msp, struct item *item, enum attr_type attr_type)
2335 zoff99 2 {
2336     struct attr attr;
2337     if (!msp->search_results)
2338 zoff99 31 msp->search_results = g_hash_table_new_full(duplicate_hash, duplicate_equal, g_free_func, NULL);
2339 zoff99 2 binfile_attr_rewind(item->priv_data);
2340     if (!item_attr_get(item, attr_type, &attr))
2341     return 1;
2342     {
2343 zoff99 27 int len = sizeof(struct coord) + strlen(attr.u.str) + 1;
2344     char *buffer = g_alloca(sizeof(char) * len);
2345     struct duplicate *d = (struct duplicate *) buffer;
2346     if (!item_coord_get(item, &d->c, 1))
2347     {
2348     d->c.x = 0;
2349     d->c.y = 0;
2350 zoff99 2 }
2351     binfile_coord_rewind(item->priv_data);
2352     strcpy(d->str, attr.u.str);
2353 zoff99 27 if (!g_hash_table_lookup(msp->search_results, d))
2354     {
2355     struct duplicate *dc = g_malloc(len);
2356 zoff99 2 memcpy(dc, d, len);
2357     g_hash_table_insert(msp->search_results, dc, GINT_TO_POINTER(1));
2358     binfile_attr_rewind(item->priv_data);
2359     return 0;
2360     }
2361     }
2362     return 2;
2363     }
2364    
2365     static struct item *
2366     binmap_search_get_item(struct map_search_priv *map_search)
2367     {
2368     struct item* it;
2369     struct attr at;
2370    
2371 zoff99 27 // // dbg(0,"Enter\n");
2372    
2373 zoff99 15 for (;;)
2374     {
2375 zoff99 27 while ((it = map_rect_get_item_binfile(map_search->mr)))
2376 zoff99 15 {
2377     switch (map_search->search->type)
2378     {
2379 zoff99 27 case attr_town_name:
2380     case attr_district_name:
2381     case attr_town_or_district_name:
2382 zoff99 15
2383     #if 0
2384 zoff99 27 if (map_search->mr->tile_depth > 1 && item_is_town(*it) && !item_is_district(*it) && map_search->search->type != attr_district_name)
2385     {
2386     if (binfile_attr_get(it->priv_data, attr_town_name_match, &at) || binfile_attr_get(it->priv_data, attr_town_name, &at))
2387     {
2388     if (!ascii_cmp(at.u.str, map_search->str, map_search->partial) && !duplicate(map_search, it, attr_town_name))
2389 zoff99 2 return it;
2390 zoff99 27 }
2391 zoff99 2 }
2392 zoff99 15 #endif
2393    
2394     #if 1
2395 zoff99 27 if (map_search->mr->tile_depth > 1 && item_is_town(*it) && !item_is_district(*it) && map_search->search->type != attr_district_name)
2396 zoff99 15 {
2397 zoff99 27 if (binfile_attr_get(it->priv_data, attr_town_name, &at))
2398 zoff99 15 {
2399 zoff99 27 //// dbg(0,"22town n:%s\n",at.u.str);
2400     if (!ascii_cmp(at.u.str, map_search->str, map_search->partial)
2401     // && !duplicate(map_search, it, attr_town_name)
2402 zoff99 15 )
2403 zoff99 27 {
2404     //// dbg(0,"town n:%s\n",at.u.str);
2405     return it;
2406     }
2407 zoff99 15 }
2408    
2409 zoff99 27 if (binfile_attr_get(it->priv_data, attr_town_name_match, &at))
2410 zoff99 15 {
2411 zoff99 27 //// dbg(0,"11town M:%s\n",at.u.str);
2412     if (!ascii_cmp(at.u.str, map_search->str, map_search->partial)
2413     // && !duplicate(map_search, it, attr_town_name)
2414     )
2415 zoff99 15 {
2416 zoff99 27 //// dbg(0,"town M:%s\n",at.u.str);
2417 zoff99 15 return it;
2418     }
2419 zoff99 2 }
2420     }
2421 zoff99 27 #endif
2422 zoff99 15
2423 zoff99 27 if (map_search->mr->tile_depth > 1 && item_is_district(*it) && map_search->search->type != attr_town_name)
2424 zoff99 15 {
2425 zoff99 27 if (binfile_attr_get(it->priv_data, attr_district_name_match, &at) || binfile_attr_get(it->priv_data, attr_district_name, &at))
2426 zoff99 15 {
2427 zoff99 27 if (!ascii_cmp(at.u.str, map_search->str, map_search->partial) && !duplicate(map_search, it, attr_town_name))
2428 zoff99 15 return it;
2429     }
2430     }
2431 zoff99 27 break;
2432 zoff99 15
2433 zoff99 27 case attr_street_name:
2434 zoff99 15
2435 zoff99 27 if (map_search->mode == 1)
2436 zoff99 15 {
2437 zoff99 27 //dbg(0,"*in here 00*\n");
2438 zoff99 15
2439 zoff99 27 //if (binfile_attr_get(it->priv_data, attr_street_name_match, &at) || binfile_attr_get(it->priv_data, attr_street_name, &at)) {
2440     // if (!ascii_cmp(at.u.str, map_search->str, map_search->partial) && !duplicate(map_search, it, attr_street_name)) {
2441     // return it;
2442     // }
2443     //}
2444 zoff99 15
2445 zoff99 27 if (binfile_attr_get(it->priv_data, attr_street_name, &at))
2446     {
2447     //// dbg(0,"str AA:%s\n", at.u.str);
2448 zoff99 15
2449 zoff99 27 if (!ascii_cmp(at.u.str, map_search->str, map_search->partial))
2450 zoff99 15 {
2451 zoff99 27 if ((offline_search_filter_duplicates != 1) || (!duplicate(map_search, it, attr_street_name)))
2452 zoff99 15 {
2453 zoff99 27 return it;
2454 zoff99 15 }
2455     else
2456     {
2457 zoff99 27 //// dbg(0,"dup street 1:%s\n",at.u.str);
2458 zoff99 15 }
2459 zoff99 27 }
2460     }
2461 zoff99 15
2462 zoff99 27 if (binfile_attr_get(it->priv_data, attr_street_name_match, &at))
2463     {
2464     //// dbg(0,"str BB:%s\n", at.u.str);
2465    
2466     if (!ascii_cmp(at.u.str, map_search->str, map_search->partial))
2467 zoff99 15 {
2468 zoff99 27 if ((offline_search_filter_duplicates != 1) || (!duplicate(map_search, it, attr_street_name_match)))
2469 zoff99 15 {
2470     return it;
2471     }
2472 zoff99 27 else
2473     {
2474     //// dbg(0,"dup street 2:%s\n",at.u.str);
2475     }
2476 zoff99 15 }
2477 zoff99 2 }
2478 zoff99 27
2479     continue;
2480 zoff99 2 }
2481 zoff99 27
2482     #if 1
2483     // ------------------------------------
2484     // map_search->mode == 2 or 3
2485     // ------------------------------------
2486     if (item_is_street(*it))
2487 zoff99 2 {
2488 zoff99 27 // dbg(0,"*in here 11*\n");
2489    
2490     struct attr at;
2491     if (map_selection_contains_item_rect(map_search->mr->sel, it))
2492 zoff99 2 {
2493 zoff99 27 if (binfile_attr_get(it->priv_data, attr_label, &at))
2494     {
2495     int match = 0;
2496     char *str = g_strdup(at.u.str);
2497     // dbg(0,"search=%s str is:%s\n",map_search->str, at.u.str);
2498     if (!ascii_cmp(str, map_search->str, map_search->partial))
2499     {
2500     match = 1;
2501     }
2502     g_free(str);
2503     if (match)
2504     {
2505     if ((offline_search_filter_duplicates != 1) || (!duplicate(map_search, it, attr_label)))
2506     {
2507     item_coord_rewind(it);
2508     return it;
2509     }
2510     }
2511     }
2512 zoff99 15
2513 zoff99 27 if (binfile_attr_get(it->priv_data, attr_street_name_match, &at))
2514 zoff99 2 {
2515 zoff99 27 int match = 0;
2516     char *str = g_strdup(at.u.str);
2517     // dbg(0,"search=%s mstr is:%s\n",map_search->str, at.u.str);
2518     if (!ascii_cmp(str, map_search->str, map_search->partial))
2519     {
2520     match = 1;
2521     }
2522     g_free(str);
2523     if (match)
2524     {
2525     if ((offline_search_filter_duplicates != 1) || (!duplicate(map_search, it, attr_street_name_match)))
2526     {
2527     item_coord_rewind(it);
2528     return it;
2529     }
2530     }
2531 zoff99 2 }
2532 zoff99 27 //else
2533     //{
2534     // if (binfile_attr_get(it->priv_data, attr_street_name, &at))
2535     // {
2536     // // dbg(0,"---str X*X:%s\n", at.u.str);
2537     // }
2538     //}
2539 zoff99 2 }
2540 zoff99 15 }
2541 zoff99 27 #endif
2542     break;
2543     case attr_house_number:
2544     //if (it->type == type_house_number)
2545     if ((it->type == type_house_number) || (type_house_number_interpolation_even) || (type_house_number_interpolation_odd) || (type_house_number_interpolation_all))
2546 zoff99 15 {
2547 zoff99 27 // is it a housenumber?
2548     if (binfile_attr_get(it->priv_data, attr_house_number, &at))
2549     {
2550     // match housenumber to our string
2551     if (!ascii_cmp(at.u.str, map_search->str, map_search->partial))
2552     {
2553     //binfile_attr_get(it->priv_data, attr_street_name, &at);
2554     //// dbg(0,"hnnn B1 street_name=%s",at.u.str);
2555    
2556     if (!duplicate(map_search, it, attr_house_number))
2557     {
2558     binfile_attr_rewind(it->priv_data);
2559     return it;
2560     }
2561     }
2562     }
2563     else
2564     {
2565     return it;
2566     }
2567 zoff99 15 }
2568 zoff99 27 continue;
2569     default:
2570     return NULL;
2571 zoff99 2 }
2572     }
2573 zoff99 27
2574 zoff99 2 if (!map_search->mr_item)
2575 zoff99 27 {
2576 zoff99 2 return NULL;
2577 zoff99 27 }
2578 zoff99 2 map_rect_destroy_binfile(map_search->mr);
2579     if (!binmap_search_by_index(map_search->map, map_search->item, &map_search->mr))
2580 zoff99 27 {
2581 zoff99 2 return NULL;
2582 zoff99 27 }
2583     } // endless for-loop
2584    
2585     return NULL; // this is never reached
2586 zoff99 2 }
2587    
2588 zoff99 27 static void binmap_search_destroy(struct map_search_priv *ms)
2589 zoff99 2 {
2590     if (ms->search_results)
2591     g_hash_table_destroy(ms->search_results);
2592     if (ms->mr_item)
2593     map_rect_destroy_binfile(ms->mr_item);
2594     if (ms->mr)
2595     map_rect_destroy_binfile(ms->mr);
2596     if (ms->str)
2597     g_free(ms->str);
2598     g_free(ms);
2599     }
2600    
2601 zoff99 27 static int binmap_get_attr(struct map_priv *m, enum attr_type type, struct attr *attr)
2602 zoff99 2 {
2603 zoff99 27 attr->type = type;
2604     switch (type)
2605     {
2606     case attr_map_release:
2607     if (m->map_release)
2608     {
2609     attr->u.str = m->map_release;
2610     return 1;
2611     }
2612     break;
2613     case attr_progress:
2614     if (m->progress)
2615     {
2616     attr->u.str = m->progress;
2617     return 1;
2618     }
2619     default:
2620     break;
2621 zoff99 2 }
2622     return 0;
2623     }
2624    
2625 zoff99 27 static int binmap_set_attr(struct map_priv *map, struct attr *attr)
2626 zoff99 2 {
2627 zoff99 27 switch (attr->type)
2628     {
2629     case attr_update:
2630     map->download_enabled = attr->u.num;
2631     return 1;
2632     default:
2633     return 0;
2634 zoff99 2 }
2635    
2636     }
2637    
2638 zoff99 27 static struct map_methods map_methods_binfile =
2639     { 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, };
2640 zoff99 2
2641 zoff99 27 static int binfile_get_index(struct map_priv *m)
2642 zoff99 2 {
2643 zoff99 27 //dbg(0,"_enter\n");
2644 zoff99 2 int len;
2645     int cde_index_size;
2646 zoff99 27 long long offset; // int or long long ?
2647 zoff99 2 struct zip_cd *cd;
2648    
2649     len = strlen("index");
2650 zoff99 27 cde_index_size = sizeof(struct zip_cd) + len;
2651     //dbg(0,"_cde_index_size=%d\n", cde_index_size);
2652     if (m->eoc64)
2653     {
2654     //dbg(0,"_m->eoc64->zip64ecsz=%d\n", m->eoc64->zip64ecsz);
2655     offset = m->eoc64->zip64ecsz - cde_index_size;
2656     //dbg(0,"_eoc64 offset="LONGLONG_FMT"\n", offset);
2657     }
2658 zoff99 2 else
2659 zoff99 27 {
2660     //dbg(0,"_m->eoc->zipecsz=%d\n", m->eoc->zipecsz);
2661     offset = m->eoc->zipecsz - cde_index_size;
2662     //dbg(0,"_eoc offset="LONGLONG_FMT"\n", offset);
2663     }
2664 zoff99 2 cd = binfile_read_cd(m, offset, len);
2665    
2666 zoff99 27 if (!cd)
2667     {
2668     //dbg(0,"_!cd\n");
2669     cde_index_size += sizeof(struct zip_cd_ext);
2670     //dbg(0,"_cde_index_size=%d\n", cde_index_size);
2671 zoff99 2 if (m->eoc64)
2672 zoff99 27 {
2673     offset = m->eoc64->zip64ecsz - cde_index_size;
2674     //dbg(0,"_eoc64 offset aa.1="LONGLONG_FMT"\n", offset);
2675     }
2676 zoff99 2 else
2677 zoff99 27 {
2678     offset = m->eoc->zipecsz - cde_index_size;
2679     //dbg(0,"_offset 22.0="LONGLONG_FMT"\n", offset);
2680     }
2681     cd = binfile_read_cd(m, offset, len + sizeof(struct zip_cd_ext));
2682 zoff99 2 }
2683 zoff99 27 //dbg(0,"_offset 22="LONGLONG_FMT"\n", offset);
2684    
2685     if (cd)
2686     {
2687     //dbg(0,"_cd 001\n");
2688     if (cd->zipcfnl == len && !strncmp(cd->zipcfn, "index", len))
2689     {
2690     m->index_offset = offset;
2691     m->index_cd = cd;
2692     //dbg(0,"_leave:offset 33="LONGLONG_FMT"\n", offset);
2693 zoff99 2 return 1;
2694     }
2695 zoff99 27 //dbg(0,"_cd 002\n");
2696 zoff99 2 }
2697 zoff99 27
2698     offset = binfile_search_cd(m, 0, "index", 0, 0);
2699     //dbg(0,"_offset 44="LONGLONG_FMT"\n", offset);
2700    
2701 zoff99 2 if (offset == -1)
2702 zoff99 27 {
2703     //dbg(0,"_leave:55\n");
2704 zoff99 2 return 0;
2705 zoff99 27 }
2706    
2707     cd = binfile_read_cd(m, offset, -1);
2708    
2709 zoff99 2 if (!cd)
2710 zoff99 27 {
2711     //dbg(0,"_leave:66\n");
2712 zoff99 2 return 0;
2713 zoff99 27 }
2714    
2715     m->index_offset = offset;
2716     m->index_cd = cd;
2717    
2718     //dbg(0,"_leave:normal:offset 77="LONGLONG_FMT"\n", offset);
2719 zoff99 2 return 1;
2720     }
2721    
2722 zoff99 27 static int map_binfile_zip_setup(struct map_priv *m, char *filename, int mmap)
2723 zoff99 2 {
2724 zoff99 27 //dbg(0,"_enter\n");
2725    
2726 zoff99 2 struct zip_cd *first_cd;
2727     int i;
2728 zoff99 27 if (!(m->eoc = binfile_read_eoc(m->fi)))
2729     {
2730     //dbg(0,"_leave:1\n");
2731 zoff99 2 return 0;
2732 zoff99 27 }
2733 zoff99 2 dbg_assert(m->eoc->zipedsk == m->eoc->zipecen);
2734 zoff99 27 if (m->eoc->zipedsk && strlen(filename) > 3)
2735     {
2736     char *tmpfilename = g_strdup(filename);
2737     char *ext = tmpfilename + strlen(tmpfilename) - 3;
2738    
2739     //dbg(0,"_ff-1:%s\n", tmpfilename);
2740     //dbg(0,"_ff-2:%s\n", ext);
2741    
2742 zoff99 2 m->fis=g_new(struct file *,m->eoc->zipedsk);
2743 zoff99 27 for (i = 0; i < m->eoc->zipedsk - 1; i++)
2744     {
2745     sprintf(ext, "b%02d", i + 1);
2746     //dbg(0,"_ff--3:%s\n", ext);
2747     m->fis[i] = file_create(tmpfilename, 0);
2748 zoff99 2 if (mmap)
2749 zoff99 27 {
2750     //dbg(0,"_want mmap 2 %s\n", m->fis[i]->name);
2751 zoff99 2 file_mmap(m->fis[i]);
2752 zoff99 27 }
2753 zoff99 2 }
2754 zoff99 27 m->fis[m->eoc->zipedsk - 1] = m->fi;
2755 zoff99 2 g_free(tmpfilename);
2756     }
2757 zoff99 27 else
2758     {
2759     //dbg(0,"_ff-XX\n");
2760     }
2761     //dbg(0, "_num_disk %d\n", m->eoc->zipedsk);
2762     m->eoc64 = binfile_read_eoc64(m->fi);
2763    
2764 zoff99 2 if (!binfile_get_index(m))
2765 zoff99 27 {
2766     //dbg(0,"_leave:2\n");
2767 zoff99 2 return 0;
2768 zoff99 27 }
2769    
2770     if (!(first_cd = binfile_read_cd(m, 0, 0)))
2771     {
2772     //dbg(0,"_leave:3\n");
2773 zoff99 2 return 0;
2774 zoff99 27 }
2775    
2776     m->cde_size = sizeof(struct zip_cd) + first_cd->zipcfnl + first_cd->zipcxtl;
2777     m->zip_members = m->index_offset / m->cde_size + 1;
2778     //dbg(0, "_cde_size %d\n", m->cde_size);
2779     //dbg(0, "_members %d\n", m->zip_members);
2780     file_data_free(m->fi, (unsigned char *) first_cd);
2781 zoff99 2 if (mmap)
2782 zoff99 27 {
2783     //dbg(0,"_want mmap 3 %s\n", m->fi->name);
2784 zoff99 2 file_mmap(m->fi);
2785 zoff99 27 }
2786 zoff99 2 return 1;
2787     }
2788    
2789     #if 0
2790     static int
2791     map_binfile_download_initial(struct map_priv *m)
2792     {
2793 zoff99 27 struct attr readwrite=
2794     { attr_readwrite,
2795     { (void *)1}};
2796     struct attr create=
2797     { attr_create,
2798     { (void *)1}};
2799 zoff99 2 struct attr *attrs[4];
2800     struct file *out;
2801     long long woffset=0,planet_size;
2802     int size_ret;
2803     int cd1size,cdisize;
2804     long long cd1offset,cdioffset;
2805     struct zip64_eoc *zip64_eoc;
2806     struct zip64_eocl *zip64_eocl;
2807     struct zip_eoc *zip_eoc;
2808     struct zip_cd *cd1,*cdn,*cdi;
2809     int count,chunk,cdoffset=0;
2810     int mode=1;
2811     struct map_download *download=g_new0(struct map_download, 1);
2812    
2813     attrs[0]=&readwrite;
2814     attrs[1]=&create;
2815     attrs[2]=NULL;
2816     download->file=file_create(m->filename,attrs);
2817     download->m=m;
2818     download_planet_size(download);
2819     download_eoc(download);
2820     download_directory_start(download);
2821     while (download_directory_do(download));
2822     download_directory_finish(download);
2823     download_initial_finish(download);
2824     m->fi=download->file;
2825     g_free(download);
2826     return 1;
2827    
2828 zoff99 27 cd1size=sizeof(*cd1);
2829     cd1offset=zip64_eoc->zip64eofst;
2830     cd1=(struct zip_cd *)map_binfile_download_range(m, cd1offset, cd1size);
2831     if (!cd1)
2832     return 0;
2833     cd1size=sizeof(*cd1)+binfile_cd_extra(cd1);
2834     g_free(cd1);
2835     cd1=(struct zip_cd *)map_binfile_download_range(m, cd1offset, cd1size);
2836     if (!cd1)
2837     return 0;
2838     cd1->zipcunc=0;
2839     cdisize=sizeof(*cdi)+strlen("index")+cd1->zipcxtl;
2840     cdioffset=zip64_eoc->zip64eofst+zip64_eoc->zip64ecsz-cdisize;
2841     cdi=(struct zip_cd *)map_binfile_download_range(m, cdioffset, cdisize);
2842     if (!cdi)
2843     {
2844 zoff99 2 g_free(cd1);
2845 zoff99 27 return 0;
2846     }
2847     cdi->zipcunc=0;
2848     cdn=g_malloc0(cd1size*256);
2849 zoff99 2
2850 zoff99 27 file_data_write(out, woffset, sizeof(*zip64_eoc), (unsigned char *)zip64_eoc);
2851     woffset+=sizeof(*zip64_eoc);
2852     cdoffset=woffset;
2853 zoff99 2
2854 zoff99 27 file_data_write(out, woffset, cd1size, (unsigned char *)cd1);
2855     woffset+=cd1size;
2856     count=(cdioffset-cd1offset)/cd1size-1;
2857     while (count > 0)
2858     {
2859     if (count > 256)
2860     chunk=256;
2861     else
2862     chunk=count;
2863     file_data_write(out, woffset, cd1size*chunk, (unsigned char *)cdn);
2864     woffset+=cd1size*chunk;
2865     count-=chunk;
2866     }
2867     g_free(cdn);
2868     g_free(cd1);
2869     file_data_write(out, woffset, cdisize, (unsigned char *)cdi);
2870     woffset+=cdisize;
2871 zoff99 2
2872     }
2873     #endif
2874    
2875 zoff99 34
2876     static int string_endswith(const char* ending, const char* instring)
2877     {
2878     int l1;
2879     int l2;
2880    
2881     if (!ending)
2882     {
2883     return 0;
2884     }
2885    
2886     if (!instring)
2887     {
2888     return 0;
2889     }
2890    
2891     l1 = strlen(ending);
2892     l2 = strlen(instring);
2893    
2894     if (l1 < 1)
2895     {
2896     return 0;
2897     }
2898    
2899     if (l1 > l2)
2900     {
2901     return 0;
2902     }
2903    
2904     int ret = strcmp(ending, instring + (l2 - l1));
2905     //dbg(0, "ending=%s in=%s ret=%d\n", ending, instring + (l2 - l1), (ret == 0));
2906     return (ret == 0);
2907     }
2908    
2909    
2910 zoff99 27 static int map_binfile_open(struct map_priv *m)
2911 zoff99 2 {
2912     int *magic;
2913 zoff99 34 int version_exception = 0;
2914 zoff99 2 struct map_rect_priv *mr;
2915     struct item *item;
2916     struct attr attr;
2917 zoff99 34
2918 zoff99 27 struct attr readwrite =
2919     { attr_readwrite,
2920     { (void *) 1 } };
2921 zoff99 34
2922 zoff99 27 struct attr *attrs[] =
2923     { &readwrite, NULL };
2924 zoff99 2
2925 zoff99 27 // dbg(0, "file_create %s\n", m->filename);
2926     m->fi = file_create(m->filename, m->url ? attrs : NULL);
2927     if (!m->fi && m->url)
2928     {
2929 zoff99 2 return 0;
2930 zoff99 27 }
2931 zoff99 34
2932 zoff99 27 if (!m->fi)
2933     {
2934     // dbg(0, "Failed to load '%s'\n", m->filename);
2935 zoff99 2 return 0;
2936     }
2937 zoff99 34
2938 zoff99 2 if (m->check_version)
2939 zoff99 27 {
2940     m->version = file_version(m->fi, m->check_version);
2941     }
2942    
2943     magic = (int *) file_data_read(m->fi, 0, 4);
2944     if (!magic)
2945     {
2946 zoff99 2 file_destroy(m->fi);
2947 zoff99 27 m->fi = NULL;
2948 zoff99 2 return 0;
2949     }
2950 zoff99 27
2951     //dbg(0,"_zip_lfh_sig=%d\n", zip_lfh_sig);
2952    
2953     //dbg(0,"_magic1=%d\n", magic[0]);
2954     //dbg(0,"_magic1=%d\n", magic[1]);
2955     //dbg(0,"_magic1=%d\n", magic[2]);
2956     //dbg(0,"_magic1=%d\n", magic[3]);
2957 zoff99 2 *magic = le32_to_cpu(*magic);
2958 zoff99 27 //dbg(0,"_magic2=%d\n", magic[0]);
2959     //dbg(0,"_magic2=%d\n", magic[1]);
2960     //dbg(0,"_magic2=%d\n", magic[2]);
2961     //dbg(0,"_magic2=%d\n", magic[3]);
2962     if (*magic == zip_lfh_sig || *magic == zip_split_sig || *magic == zip_cd_sig || *magic == zip64_eoc_sig)
2963     {
2964     if (!map_binfile_zip_setup(m, m->filename, m->flags & 1))
2965     {
2966     dbg(0, "invalid file format for '%s'\n", m->filename);
2967 zoff99 2 file_destroy(m->fi);
2968 zoff99 27 m->fi = NULL;
2969 zoff99 2 return 0;
2970     }
2971 zoff99 27 }
2972     else
2973     {
2974     //dbg(0,"_want mmap %s\n", m->fi->name);
2975 zoff99 2 file_mmap(m->fi);
2976 zoff99 27 }
2977    
2978     //dbg(0,"_cont\n");
2979    
2980     file_data_free(m->fi, (unsigned char *) magic);
2981     m->cachedir = g_strdup("/tmp/navit");
2982     m->map_version = 0;
2983    
2984     mr = map_rect_new_binfile(m, NULL);
2985     if (mr)
2986     {
2987     while ((item = map_rect_get_item_binfile(mr)) == &busy_item)
2988     {
2989     ;
2990     }
2991 zoff99 30
2992 zoff99 27 if (item && item->type == type_map_information)
2993     {
2994 zoff99 2 if (binfile_attr_get(item->priv_data, attr_version, &attr))
2995 zoff99 27 {
2996     m->map_version = attr.u.num;
2997 zoff99 34 dbg(0, "map version=%d\n", m->map_version);
2998 zoff99 27 }
2999 zoff99 34
3000 zoff99 2 if (binfile_attr_get(item->priv_data, attr_map_release, &attr))
3001 zoff99 27 {
3002     m->map_release = g_strdup(attr.u.str);
3003 zoff99 34 dbg(0, "maptool version used=%s\n", m->map_release);
3004 zoff99 27 }
3005 zoff99 34
3006     if (string_endswith("borders.bin", m->filename) == 1)
3007 zoff99 27 {
3008 zoff99 34 dbg(0, "map exception added!\n");
3009     version_exception = 1;
3010 zoff99 2 }
3011 zoff99 34
3012     if (string_endswith("coastline.bin", m->filename) == 1)
3013     {
3014     dbg(0, "map exception added!\n");
3015     version_exception = 1;
3016     }
3017    
3018     //if (m->url && binfile_attr_get(item->priv_data, attr_url, &attr))
3019     //{
3020     // dbg(0, "url config %s map %s\n", m->url, attr.u.str);
3021     // if (strcmp(m->url, attr.u.str))
3022     // {
3023     // m->update_available = 1;
3024     // }
3025     // g_free(m->url);
3026     // m->url = g_strdup(attr.u.str);
3027     //}
3028 zoff99 2 }
3029 zoff99 30
3030 zoff99 2 map_rect_destroy_binfile(mr);
3031 zoff99 30
3032 zoff99 34 if (version_exception == 0)
3033 zoff99 27 {
3034 zoff99 34 if (m->map_version < (int)NEED_MIN_BINFILE_MAPVERSION)
3035     {
3036     dbg(0, "!!**Warning**!!: This map is too old for your version of ZANavi. You need at least a version %d map\n", (int)NEED_MIN_BINFILE_MAPVERSION);
3037     return 0;
3038     }
3039 zoff99 2 }
3040     }
3041     return 1;
3042     }
3043    
3044 zoff99 27 static void map_binfile_close(struct map_priv *m)
3045 zoff99 2 {
3046     int i;
3047 zoff99 27 file_data_free(m->fi, (unsigned char *) m->index_cd);
3048     file_data_free(m->fi, (unsigned char *) m->eoc);
3049     file_data_free(m->fi, (unsigned char *) m->eoc64);
3050 zoff99 2 g_free(m->cachedir);
3051     g_free(m->map_release);
3052 zoff99 34
3053 zoff99 27 if (m->fis)
3054     {
3055     for (i = 0; i < m->eoc->zipedsk; i++)
3056     {
3057 zoff99 2 file_destroy(m->fis[i]);
3058     }
3059 zoff99 27 }
3060     else
3061 zoff99 34 {
3062 zoff99 2 file_destroy(m->fi);
3063 zoff99 34 }
3064 zoff99 2 }
3065    
3066 zoff99 27 static void map_binfile_destroy(struct map_priv *m)
3067 zoff99 2 {
3068     g_free(m->filename);
3069     g_free(m->url);
3070     g_free(m->progress);
3071     g_free(m);
3072     }
3073    
3074 zoff99 27 static void binfile_check_version(struct map_priv *m)
3075 zoff99 2 {
3076 zoff99 27 int version = -1;
3077 zoff99 2 if (!m->check_version)
3078     return;
3079 zoff99 27 if (m->fi)
3080     version = file_version(m->fi, m->check_version);
3081     if (version != m->version)
3082     {
3083 zoff99 2 if (m->fi)
3084     map_binfile_close(m);
3085     map_binfile_open(m);
3086     }
3087     }
3088    
3089     static struct map_priv *
3090     map_new_binfile(struct map_methods *meth, struct attr **attrs, struct callback_list *cbl)
3091     {
3092     struct map_priv *m;
3093 zoff99 27 struct attr *data = attr_search(attrs, NULL, attr_data);
3094     struct attr *check_version, *map_pass, *flags, *url, *download_enabled;
3095 zoff99 2 struct file_wordexp *wexp;
3096     char **wexp_data;
3097 zoff99 27 if (!data)
3098 zoff99 2 return NULL;
3099    
3100 zoff99 31 // wexp = file_wordexp_new(data->u.str);
3101     // wexp_data = file_wordexp_get_array(wexp);
3102 zoff99 27 // dbg(0, "map_new_binfile %s\n", data->u.str);
3103     *meth = map_methods_binfile;
3104 zoff99 2
3105     m=g_new0(struct map_priv, 1);
3106 zoff99 27 m->cbl = cbl;
3107     m->id = ++map_id;
3108 zoff99 31 m->filename = g_strdup(data->u.str);
3109     // file_wordexp_destroy(wexp);
3110 zoff99 27 check_version = attr_search(attrs, NULL, attr_check_version);
3111 zoff99 34
3112 zoff99 27 if (check_version)
3113     m->check_version = check_version->u.num;
3114 zoff99 34
3115 zoff99 27 map_pass = attr_search(attrs, NULL, attr_map_pass);
3116 zoff99 34
3117 zoff99 2 if (map_pass)
3118 zoff99 27 m->passwd = g_strdup(map_pass->u.str);
3119 zoff99 34
3120 zoff99 27 flags = attr_search(attrs, NULL, attr_flags);
3121 zoff99 34
3122 zoff99 2 if (flags)
3123 zoff99 27 m->flags = flags->u.num;
3124 zoff99 34
3125 zoff99 27 url = attr_search(attrs, NULL, attr_url);
3126 zoff99 34
3127 zoff99 2 if (url)
3128 zoff99 27 m->url = g_strdup(url->u.str);
3129 zoff99 34
3130 zoff99 2 download_enabled = attr_search(attrs, NULL, attr_update);
3131 zoff99 34
3132 zoff99 2 if (download_enabled)
3133 zoff99 27 m->download_enabled = download_enabled->u.num;
3134 zoff99 2
3135 zoff99 27 if (!map_binfile_open(m) && !m->check_version && !m->url)
3136     {
3137 zoff99 2 map_binfile_destroy(m);
3138 zoff99 27 m = NULL;
3139     }
3140     else
3141     {
3142 zoff99 2 load_changes(m);
3143     }
3144     return m;
3145     }
3146    
3147 zoff99 27 void plugin_init(void)
3148 zoff99 2 {
3149 zoff99 27 // dbg(1, "binfile: plugin_init\n");
3150     if (sizeof(struct zip_cd) != 46)
3151     {
3152     // dbg(0, "error: sizeof(struct zip_cd)=%d\n", sizeof(struct zip_cd));
3153 zoff99 2 }
3154     plugin_register_map_type("binfile", map_new_binfile);
3155     }
3156    

   
Visit the ZANavi Wiki