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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Mon Feb 4 17:41:59 2013 UTC (11 years, 1 month ago) by zoff99
File MIME type: text/plain
File size: 76563 byte(s)
new map version, lots of fixes and experimental new features
1 /**
2 * 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 * 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 #include "navit.h"
61
62 static int map_id;
63
64 struct tile
65 {
66 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 struct map_download
80 {
81 int state;
82 struct map_priv *m;
83 struct map_rect_priv *mr;
84 struct file *http, *file;
85 int zipfile, toffset, tlength, progress, read, dl_size;
86 long long offset, start_offset, cd1offset, size;
87 struct zip64_eoc *zip64_eoc;
88 struct zip64_eocl *zip64_eocl;
89 struct zip_eoc *zip_eoc;
90 struct zip_cd *cd_copy, *cd;
91 };
92
93 struct map_priv
94 {
95 int id;
96 char *filename;
97 char *cachedir;
98 struct file *fi, *http;
99 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 struct map_rect_priv
126 {
127 int *start;
128 int *end;
129 enum attr_type attr_last;
130 int label;
131 int *label_attr[5];
132 struct map_selection *sel;
133 struct map_priv *m;
134 struct item item;
135 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 int size;
144 #endif
145 };
146
147 struct map_search_priv
148 {
149 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 static void lfh_to_cpu(struct zip_lfh *lfh)
168 {
169 dbg_assert(lfh != NULL);
170 if (lfh->ziplocsig != zip_lfh_sig)
171 {
172 // dbg(0,"_endian 3\n");
173 lfh->ziplocsig = le32_to_cpu(lfh->ziplocsig);
174 lfh->zipver = le16_to_cpu(lfh->zipver);
175 lfh->zipgenfld = le16_to_cpu(lfh->zipgenfld);
176 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 lfh->zipxtraln = le16_to_cpu(lfh->zipxtraln);
184 }
185 }
186
187 static void cd_to_cpu(struct zip_cd *zcd)
188 {
189 dbg_assert(zcd != NULL);
190 if (zcd->zipcensig != zip_cd_sig)
191 {
192 // dbg(0,"_endian 1\n");
193 zcd->zipcensig = le32_to_cpu(zcd->zipcensig);
194 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 }
205 }
206
207 static void eoc_to_cpu(struct zip_eoc *eoc)
208 {
209 dbg_assert(eoc != NULL);
210 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 }
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 eoc = (struct zip_eoc *) file_data_read(fi, fi->size - sizeof(struct zip_eoc), sizeof(struct zip_eoc));
231 if (eoc)
232 {
233 eoc_to_cpu(eoc);
234 // 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 }
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 eocl = (struct zip64_eocl *) file_data_read(fi, fi->size - sizeof(struct zip_eoc) - sizeof(struct zip64_eocl), sizeof(struct zip64_eocl));
250 if (!eocl)
251 {
252 return NULL;
253 }
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 return NULL;
260 }
261 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 }
270 //dbg(0, "eoc64 ok 0x%Lx 0x%Lx\n", eoc->zip64eofst, eoc->zip64ecsz);
271 }
272 file_data_free(fi, (unsigned char *) eocl);
273 return eoc;
274 }
275
276 static int binfile_cd_extra(struct zip_cd *cd)
277 {
278 return cd->zipcfnl + cd->zipcxtl;
279 }
280
281 static struct zip_cd *
282 binfile_read_cd(struct map_priv *m, int offset, int len)
283 {
284 //dbg(0,"_enter\n");
285 struct zip_cd *cd;
286 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 cd_to_cpu(cd);
294 len = binfile_cd_extra(cd);
295 //dbg(0,"_len = %d\n", len);
296 file_data_free(m->fi, (unsigned char *) cd);
297 }
298
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 cd_to_cpu(cd);
305 //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 }
312 }
313
314 //dbg(0,"leave\n");
315 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 ext = (struct zip_cd_ext *) ((unsigned char *) cd + sizeof(*cd) + cd->zipcfnl);
327 if (ext->tag != 0x0001 || ext->size != 8)
328 return NULL;
329 return ext;
330 }
331
332 static long long binfile_cd_offset(struct zip_cd *cd)
333 {
334 struct zip_cd_ext *ext = binfile_cd_ext(cd);
335 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 lfh = (struct zip_lfh *) (file_data_read(fi, offset, sizeof(struct zip_lfh)));
347 if (lfh)
348 {
349 lfh_to_cpu(lfh);
350 if (lfh->ziplocsig != zip_lfh_sig)
351 {
352 file_data_free(fi, (unsigned char *) lfh);
353 lfh = NULL;
354 }
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 unsigned char *ret = NULL;
364
365 offset += sizeof(struct zip_lfh) + lfh->zipfnln;
366 switch (lfh->zipmthd)
367 {
368 case 0:
369 offset += lfh->zipxtraln;
370 ret = file_data_read(fi, offset, lfh->zipuncmp);
371 break;
372 case 8:
373 offset += lfh->zipxtraln;
374 ret = file_data_read_compressed(fi, offset, lfh->zipsize, lfh->zipuncmp);
375 break;
376 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 default:
398 // dbg(0, "Unknown compression method %d\n", lfh->zipmthd);
399 break;
400 }
401 return ret;
402 }
403
404 static int binfile_search_cd(struct map_priv *m, int offset, char *name, int partial, int skip)
405 {
406 //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 struct zip_cd *cd;
413 #if 0
414 // dbg(0,"end=%d\n",end);
415 #endif
416 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 #if 0
422 // dbg(0,"reload %p %d %d\n", m->search_data, m->search_offset, offset);
423 #endif
424 if (m->search_data)
425 {
426 file_data_free(m->fi, m->search_data);
427 }
428 m->search_offset = offset;
429 m->search_size = end - offset;
430 if (m->search_size > size)
431 {
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 }
437 #if 0
438 // 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 #endif
441 if (!skip && (partial || cd->zipcfnl == len) && !strncmp(cd->zipcfn, name, len))
442 {
443 return offset;
444 }
445 skip = 0;
446 offset += sizeof(*cd) + cd->zipcfnl + cd->zipcxtl + cd->zipccml;
447 ;
448 }
449 return -1;
450 }
451
452 static void map_destroy_binfile(struct map_priv *m)
453 {
454 // dbg(1, "map_destroy_binfile\n");
455 if (m->fi)
456 map_binfile_close(m);
457 map_binfile_destroy(m);
458 }
459
460 static void binfile_coord_rewind(void *priv_data)
461 {
462 struct map_rect_priv *mr = priv_data;
463 struct tile *t = mr->t;
464 t->pos_coord = t->pos_coord_start;
465 }
466
467 static int binfile_coord_get(void *priv_data, struct coord *c, int count)
468 {
469 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 if (count > max)
474 {
475 count = max;
476 }
477 #if __BYTE_ORDER == __LITTLE_ENDIAN
478 memcpy(c, t->pos_coord, count * sizeof(struct coord));
479 #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 while (i++ < end)
485 {
486 *dst++=le32_to_cpu(*src);
487 src++;
488 }
489 }
490 #endif
491 t->pos_coord += count * 2;
492 ret = count;
493 return ret;
494 }
495
496 static void binfile_attr_rewind(void *priv_data)
497 {
498 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 memset(mr->label_attr, 0, sizeof(mr->label_attr));
503
504 }
505
506 static char *
507 binfile_extract(struct map_priv *m, char *dir, char *filename, int partial)
508 {
509 char *full, *fulld, *sep;
510 unsigned char *start;
511 int len, offset = m->index_offset;
512 struct zip_cd *cd;
513 struct zip_lfh *lfh;
514 FILE *f;
515
516 for (;;)
517 {
518 offset = binfile_search_cd(m, offset, filename, partial, 1);
519 if (offset == -1)
520 break;
521 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 file_mkdir(fulld, 1);
534 }
535 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 fwrite(start, lfh->zipuncmp, 1, f);
542 fclose(f);
543 file_data_free(m->fi, start);
544 file_data_free(m->fi, (unsigned char *) lfh);
545 }
546 file_data_free(m->fi, (unsigned char *) cd);
547 g_free(fulld);
548 g_free(full);
549 if (!partial)
550 {
551 break;
552 }
553 }
554
555 return g_strdup_printf("%s/%s", dir, filename);
556 }
557
558 static int binfile_attr_get(void *priv_data, enum attr_type attr_type, struct attr *attr)
559 {
560 #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 struct map_rect_priv *mr = priv_data;
567 struct tile *t = mr->t;
568 enum attr_type type;
569 int i, size;
570
571 if (attr_type != mr->attr_last)
572 {
573 t->pos_attr = t->pos_attr_start;
574 mr->attr_last = attr_type;
575 }
576
577 while (t->pos_attr < t->pos_next)
578 {
579 //dbg(0,"batg:002 %d %d\n",t->pos_attr,(t->pos_attr+1));
580 size = le32_to_cpu(*(t->pos_attr++));
581 // dbg(0,"batg:002.1 %d %d\n",t->pos_attr, t->pos_attr[0]);
582 type = le32_to_cpu(t->pos_attr[0]);
583 // 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 if (type == attr_label)
596 mr->label = 1;
597 if (type == attr_house_number)
598 mr->label_attr[0] = t->pos_attr;
599 if (type == attr_street_name)
600 mr->label_attr[1] = t->pos_attr;
601 if (type == attr_street_name_systematic)
602 mr->label_attr[2] = t->pos_attr;
603 if (type == attr_district_name && mr->item.type < type_line)
604 mr->label_attr[3] = t->pos_attr;
605 if (type == attr_town_name && mr->item.type < type_line)
606 mr->label_attr[4] = t->pos_attr;
607
608 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 }
614 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 i++;
630 }
631
632 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 g_free(mr->url);
642 mr->url = binfile_extract(mr->m, mr->m->cachedir, attr->u.str, 1);
643 attr->u.str = mr->url;
644 }
645
646 if (type == attr_flags && mr->m->map_version < 1)
647 {
648 attr->u.num |= AF_CAR;
649 }
650 }
651 t->pos_attr += size;
652 return 1;
653 }
654 else
655 {
656 t->pos_attr += size;
657 }
658 }
659
660 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 return 1;
670 }
671 }
672 }
673
674 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
675 dbg(0,"+#+:leave\n");
676 #endif
677
678 return 0;
679 }
680
681 struct binfile_hash_entry
682 {
683 struct item_id id;
684 int flags;
685 int data[0];
686 };
687
688 static guint binfile_hash_entry_hash(gconstpointer key)
689 {
690 const struct binfile_hash_entry *entry = key;
691 return (entry->id.id_hi ^ entry->id.id_lo);
692 }
693
694 static gboolean binfile_hash_entry_equal(gconstpointer a, gconstpointer b)
695 {
696 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 }
699
700 static int *
701 binfile_item_dup(struct map_priv *m, struct item *item, struct tile *t, int extend)
702 {
703 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 m->changes = g_hash_table_new_full(binfile_hash_entry_hash, binfile_hash_entry_equal, g_free_func, NULL);
714 g_hash_table_replace(m->changes, entry, entry);
715 // dbg(0, "ret %p\n", ret);
716 return ret;
717 }
718
719 static int binfile_coord_set(void *priv_data, struct coord *c, int count, enum change_mode mode)
720 {
721 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 int *data;
726
727 //{
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 {
743 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 }
781 // 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 push_tile(mr, &new, 0, 0);
789 setup_pos(mr);
790 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 {
805 tn->pos_coord += move_offset;
806 }
807 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 }
815
816 }
817 //{
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 return 1;
826 }
827
828 static int binfile_attr_set(void *priv_data, struct attr *attr, enum change_mode mode)
829 {
830 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 int *data;
836
837 //{
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 {
853 if (mode == change_mode_delete || mode == change_mode_modify)
854 {
855 // dbg(0, "no attribute selected\n");
856 return 0;
857 }
858 if (mode == change_mode_append)
859 {
860 naoffset = t->pos_next - t->pos_attr_start;
861 }
862 }
863 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 }
870 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 }
899 if (mode == change_mode_delete || mode == change_mode_modify)
900 {
901 delta = nattr_len - oattr_len;
902 }
903 else
904 {
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 push_tile(mr, &new, 0, 0);
914 setup_pos(mr);
915 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 if (mode != change_mode_append)
922 tn->pos_attr += delta;
923 {
924 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 }
931
932 if (nattr_len)
933 {
934 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 }
941
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 return 1;
951 }
952
953 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
956 static void push_tile(struct map_rect_priv *mr, struct tile *t, int offset, int length)
957 {
958 dbg_assert(mr->tile_depth < 8);
959 mr->t = &mr->tiles[mr->tile_depth++];
960 *(mr->t) = *t;
961 mr->t->pos = mr->t->pos_next = mr->t->start + offset;
962 if (length == -1)
963 length = le32_to_cpu(mr->t->pos[0]) + 1;
964 if (length > 0)
965 mr->t->end = mr->t->pos + length;
966 }
967
968 static int pop_tile(struct map_rect_priv *mr)
969 {
970 if (mr->tile_depth <= 1)
971 return 0;
972 if (mr->t->mode < 2)
973 file_data_free(mr->m->fi, (unsigned char *) (mr->t->start));
974 #ifdef DEBUG_SIZE
975 #if DEBUG_SIZE > 0
976 // dbg(0,"leave %d\n",mr->t->zipfile_num);
977 #endif
978 #endif
979 mr->t = &mr->tiles[--mr->tile_depth - 1];
980 return 1;
981 }
982
983 static int zipfile_to_tile(struct map_priv *m, struct zip_cd *cd, struct tile *t)
984 {
985 char buffer[1024];
986 struct zip_lfh *lfh;
987 char *zipfn;
988 struct file *fi;
989 // 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 if (m->fis)
994 fi = m->fis[cd->zipdsk];
995 else
996 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 strncpy(buffer, zipfn, lfh->zipfnln);
1000 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 return t->start != NULL;
1008 }
1009
1010 static int map_binfile_handle_redirect(struct map_priv *m)
1011 {
1012 char *location = file_http_header(m->http, "location");
1013 if (!location)
1014 {
1015 m->redirect = 0;
1016 return 0;
1017 }
1018 if (m->redirect)
1019 return 0;
1020 m->redirect = 1;
1021 // dbg(0, "redirected from %s to %s\n", m->url, location);
1022 g_free(m->url);
1023 m->url = g_strdup(location);
1024 file_destroy(m->http);
1025 m->http = NULL;
1026
1027 return 1;
1028 }
1029
1030 static int map_binfile_http_request(struct map_priv *m, struct attr **attrs)
1031 {
1032 if (!m->http)
1033 {
1034 m->http = file_create(NULL, attrs);
1035 }
1036 else
1037 {
1038 file_request(m->http, attrs);
1039 }
1040 return 1;
1041 }
1042
1043 static long long map_binfile_download_size(struct map_priv *m)
1044 {
1045 struct attr url =
1046 { attr_url };
1047 struct attr http_method =
1048 { attr_http_method };
1049 struct attr persistent =
1050 { attr_persistent };
1051 struct attr *attrs[4];
1052 int size_ret;
1053 long long ret;
1054 void *data;
1055
1056 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
1066 map_binfile_http_request(m, attrs);
1067 data = file_data_read_special(m->http, 0, &size_ret);
1068 g_free(data);
1069 if (size_ret < 0)
1070 return 0;
1071 }
1072 while (map_binfile_handle_redirect(m));
1073
1074 ret = file_size(m->http);
1075 // dbg(1,"file size "LONGLONG_FMT"\n",ret);
1076 return ret;
1077 }
1078
1079 static int map_binfile_http_close(struct map_priv *m)
1080 {
1081 if (m->http)
1082 {
1083 file_destroy(m->http);
1084 m->http = NULL;
1085 }
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 struct attr url =
1094 { attr_url };
1095 struct attr http_header =
1096 { attr_http_header };
1097 struct attr persistent =
1098 { attr_persistent };
1099
1100 persistent.u.num = 1;
1101 attrs[0] = &url;
1102 attrs[1] = &http_header;
1103 attrs[2] = &persistent;
1104 attrs[3] = NULL;
1105
1106 url.u.str = m->url;
1107 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 struct file *http = map_binfile_http_range(m, offset, size);
1119
1120 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 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 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 return cd;
1139 }
1140
1141 static int download_request(struct map_download *download)
1142 {
1143 struct attr url =
1144 { attr_url };
1145 struct attr http_header =
1146 { attr_http_header };
1147 struct attr persistent =
1148 { attr_persistent };
1149 struct attr *attrs[4];
1150
1151 if (!download->m->download_enabled)
1152 {
1153 // dbg(0, "Tried downloading while it's not allowed\n");
1154 return 0;
1155 }
1156 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 http_header.u.str=g_strdup_printf("Range: bytes="LONGLONG_FMT"-"LONGLONG_FMT,offset,offset+size-1);
1171 attrs[2] = &http_header;
1172 attrs[3] = NULL;
1173 download->dl_size = size;
1174 }
1175 // 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 map_binfile_http_request(download->m, attrs);
1177 g_free(url.u.str);
1178 download->http = download->m->http;
1179 return 1;
1180 }
1181
1182 static int download_start(struct map_download *download)
1183 {
1184 long long offset;
1185 struct zip_eoc *eoc;
1186
1187 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 memcpy(download->cd_copy, download->cd, download->m->cde_size);
1195 }
1196 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 memcpy(download->zip_eoc, eoc, sizeof(struct zip_eoc));
1203 file_data_remove(download->file, (unsigned char *) eoc);
1204 download->start_offset = download->offset = offset;
1205 return download_request(download);
1206 }
1207
1208 static int download_download(struct map_download *download)
1209 {
1210 int size = 64 * 1024, size_ret;
1211 unsigned char *data;
1212 if (download->dl_size != -1 && size > download->dl_size)
1213 size = download->dl_size;
1214 if (!size)
1215 return 1;
1216 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 g_free(data);
1220 download_request(download);
1221 return 0;
1222 }
1223
1224 // dbg(1,"got %d bytes writing at offset "LONGLONG_FMT"\n",size_ret,download->offset);
1225 if (size_ret <= 0)
1226 {
1227 g_free(data);
1228 return 1;
1229 }
1230 file_data_write(download->file, download->offset, size_ret, data);
1231 download->offset += size_ret;
1232 download->read += size_ret;
1233 download->dl_size -= size_ret;
1234 if (download->dl_size != -1)
1235 download->progress = download->read * 100 / (download->read + download->dl_size);
1236 return 0;
1237 }
1238
1239 static int download_finish(struct map_download *download)
1240 {
1241 struct zip_lfh *lfh;
1242 char *lfh_filename;
1243 struct zip_cd_ext *ext;
1244 long long lfh_offset;
1245 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 if (ext)
1249 ext->zipofst = download->start_offset;
1250 else
1251 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
1263 g_free(download->cd_copy);
1264 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 cd_to_cpu(download->cd);
1266 // dbg(1, "Offset %d\n", download->cd->zipofst);
1267 return 1;
1268 }
1269
1270 static int download_planet_size(struct map_download *download)
1271 {
1272 download->size = map_binfile_download_size(download->m);
1273 // dbg(0,"Planet size "LONGLONG_FMT"\n",download->size);
1274 if (!download->size)
1275 return 0;
1276 return 1;
1277 }
1278
1279 static int download_eoc(struct map_download *download)
1280 {
1281 download->zip64_eoc = (struct zip64_eoc *) map_binfile_download_range(download->m, download->size - 98, 98);
1282 if (!download->zip64_eoc)
1283 return 0;
1284 download->zip64_eocl = (struct zip64_eocl *) (download->zip64_eoc + 1);
1285 download->zip_eoc = (struct zip_eoc *) (download->zip64_eocl + 1);
1286 if (download->zip64_eoc->zip64esig != zip64_eoc_sig || download->zip64_eocl->zip64lsig != zip64_eocl_sig || download->zip_eoc->zipesig != zip_eoc_sig)
1287 {
1288 // dbg(0,"wrong signature on zip64_eoc downloaded from "LONGLONG_FMT"\n",download->size-98);
1289 g_free(download->zip64_eoc);
1290 return 0;
1291 }
1292 return 1;
1293 }
1294
1295 static int download_directory_start(struct map_download *download)
1296 {
1297 download->http = map_binfile_http_range(download->m, download->zip64_eoc->zip64eofst, download->zip64_eoc->zip64ecsz);
1298 if (!download->http)
1299 return 0;
1300 return 1;
1301 }
1302
1303 static int download_directory_do(struct map_download *download)
1304 {
1305 int count;
1306
1307 for (count = 0; count < 100; count++)
1308 {
1309 int cd_xlen, size_ret;
1310 unsigned char *cd_data;
1311 struct zip_cd *cd;
1312 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 if (!size_ret)
1316 return 0;
1317 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 return 0;
1321 }
1322 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 return 0;
1330 }
1331 file_data_write(download->file, download->offset, cd_xlen, cd_data);
1332 download->offset += cd_xlen;
1333 g_free(cd);
1334 g_free(cd_data);
1335 }
1336 return 1;
1337 }
1338
1339 static int download_directory_finish(struct map_download *download)
1340 {
1341 download->http = NULL;
1342 return 1;
1343 }
1344
1345 static int download_initial_finish(struct map_download *download)
1346 {
1347 download->zip64_eoc->zip64eofst = download->cd1offset;
1348 download->zip64_eocl->zip64lofst = download->offset;
1349 download->zip_eoc->zipeofst = download->cd1offset;
1350 #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 file_data_write(download->file, download->offset, sizeof(*download->zip_eoc), (unsigned char *) download->zip_eoc);
1357 download->offset += sizeof(*download->zip_eoc);
1358 g_free(download->zip64_eoc);
1359 download->zip64_eoc = NULL;
1360 return 1;
1361 }
1362
1363 static void push_zipfile_tile_do(struct map_rect_priv *mr, struct zip_cd *cd, int zipfile, int offset, int length)
1364
1365 {
1366 struct tile t;
1367 struct map_priv *m = mr->m;
1368 struct file *f = m->fi;
1369
1370 // dbg(1, "enter %p %d\n", mr, zipfile);
1371 #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 // dbg(0,"enter %d (%s) %d\n",zipfile, filename, cd->zipcunc);
1378 }
1379 #endif
1380 mr->size+=cd->zipcunc;
1381 #endif
1382 t.zipfile_num = zipfile;
1383 if (zipfile_to_tile(m, cd, &t))
1384 push_tile(mr, &t, offset, length);
1385 file_data_free(f, (unsigned char *) cd);
1386 }
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 if (!m->download_enabled)
1394 return NULL;
1395
1396 if (async == 2)
1397 {
1398 download = m->download;
1399 }
1400 else
1401 {
1402 download=g_new0(struct map_download, 1);
1403 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 struct attr *attrs[3];
1423 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 }
1430 }
1431 if (async == 1)
1432 {
1433 m->download = download;
1434 g_free(m->progress);
1435 if (download->mr)
1436 m->progress = g_strdup_printf("Download Tile %d 0%%", download->zipfile);
1437 else
1438 m->progress = g_strdup_printf("Download Map Information 0%%");
1439 callback_list_call_attr_0(m->cbl, attr_progress);
1440 return NULL;
1441 }
1442 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 g_free(m->progress);
1498 m->progress = g_strdup_printf("Download Map Information 50%%");
1499 callback_list_call_attr_0(m->cbl, attr_progress);
1500 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 g_free(m->progress);
1510 m->progress = g_strdup_printf("Download Map Information 100%%");
1511 callback_list_call_attr_0(m->cbl, attr_progress);
1512 if (download_directory_start(download))
1513 download->state = 7;
1514 else
1515 download->state = 0;
1516 break;
1517 case 7:
1518 g_free(m->progress);
1519 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 g_free(download);
1536 if (async)
1537 m->download = NULL;
1538 map_binfile_open(m);
1539 break;
1540 }
1541 if (async)
1542 return NULL;
1543 }
1544 }
1545
1546 static int push_zipfile_tile(struct map_rect_priv *mr, int zipfile, int offset, int length, int async)
1547 {
1548 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 cd_to_cpu(cd);
1554 if (!cd->zipcunc && m->url)
1555 {
1556 cd = download(m, mr, cd, zipfile, offset, length, async);
1557 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 // dbg(1, "map_rect_new_binfile\n");
1571 if (!map->fi && !map->url)
1572 return NULL;
1573 map_binfile_http_close(map);
1574 mr=g_new0(struct map_rect_priv, 1);
1575 mr->m = map;
1576 mr->sel = sel;
1577 mr->item.id_hi = 0;
1578 mr->item.id_lo = 0;
1579 mr->item.meth = &methods_binfile;
1580 mr->item.priv_data = mr;
1581 return mr;
1582 }
1583
1584 static void tile_bbox(char *tile, int len, struct coord_rect *r)
1585 {
1586 struct coord c;
1587 int overlap = 1;
1588 int xo, yo;
1589 struct coord_rect world_bbox =
1590 {
1591 { -20000000, 20000000 }, /* lu */
1592 { 20000000, -20000000 }, /* rl */
1593 };
1594 *r = world_bbox;
1595 while (len)
1596 {
1597 c.x = (r->lu.x + r->rl.x) / 2;
1598 c.y = (r->lu.y + r->rl.y) / 2;
1599 xo = (r->rl.x - r->lu.x) * overlap / 100;
1600 yo = (r->lu.y - r->rl.y) * overlap / 100;
1601 switch (*tile)
1602 {
1603 case 'a':
1604 r->lu.x = c.x - xo;
1605 r->rl.y = c.y - yo;
1606 break;
1607 case 'b':
1608 r->rl.x = c.x + xo;
1609 r->rl.y = c.y - yo;
1610 break;
1611 case 'c':
1612 r->lu.x = c.x - xo;
1613 r->lu.y = c.y + yo;
1614 break;
1615 case 'd':
1616 r->rl.x = c.x + xo;
1617 r->lu.y = c.y + yo;
1618 break;
1619 default:
1620 return;
1621 }
1622 tile++;
1623 len--;
1624 }
1625 }
1626
1627 static int map_download_selection_check(struct zip_cd *cd, struct map_selection *sel)
1628 {
1629 //// dbg(0,"EEnter\n");
1630
1631 struct coord_rect cd_rect;
1632 if (cd->zipcunc)
1633 return 0;
1634 tile_bbox((char *) (cd + 1), cd->zipcfnl, &cd_rect);
1635 while (sel)
1636 {
1637 if (coord_rect_overlap(&cd_rect, &sel->u.c_rect))
1638 return 1;
1639 sel = sel->next;
1640 }
1641 return 0;
1642 }
1643
1644 static void map_download_selection(struct map_priv *m, struct map_rect_priv *mr, struct map_selection *sel)
1645 {
1646 int i;
1647 struct zip_cd *cd;
1648 for (i = 0; i < m->zip_members; i++)
1649 {
1650 cd = binfile_read_cd(m, m->cde_size * i, -1);
1651 if (map_download_selection_check(cd, sel))
1652 download(m, mr, cd, i, 0, 0, 0);
1653 file_data_free(m->fi, (unsigned char *) cd);
1654 }
1655 }
1656
1657 static struct map_rect_priv *
1658 map_rect_new_binfile(struct map_priv *map, struct map_selection *sel)
1659 {
1660 struct map_rect_priv *mr = map_rect_new_binfile_int(map, sel);
1661 struct tile t;
1662 // dbg(1, "zip_members=%d\n", map->zip_members);
1663 if (map->url && map->fi && sel && sel->order == 255)
1664 {
1665 map_download_selection(map, mr, sel);
1666 }
1667 if (map->eoc)
1668 mr->status = 1;
1669 else
1670 {
1671 unsigned char *d;
1672 if (map->fi)
1673 {
1674 d = file_data_read(map->fi, 0, map->fi->size);
1675 t.start = (int *) d;
1676 t.end = (int *) (d + map->fi->size);
1677 t.fi = map->fi;
1678 t.zipfile_num = 0;
1679 t.mode = 0;
1680 push_tile(mr, &t, 0, 0);
1681 }
1682 else if (map->url && !map->download)
1683 {
1684 download(map, NULL, NULL, 0, 0, 0, 1);
1685 mr->status = 1;
1686 }
1687 }
1688 return mr;
1689 }
1690
1691 static void write_changes_do(gpointer key, gpointer value, gpointer user_data)
1692 {
1693 struct binfile_hash_entry *entry = key;
1694 FILE *out = user_data;
1695 if (entry->flags)
1696 {
1697 entry->flags = 0;
1698 fwrite(entry, sizeof(*entry) + (le32_to_cpu(entry->data[0]) + 1) * 4, 1, out);
1699 // dbg(0, "yes\n");
1700 }
1701 }
1702
1703 static void write_changes(struct map_priv *m)
1704 {
1705 FILE *changes;
1706 char *changes_file;
1707 if (!m->changes)
1708 return;
1709 changes_file = g_strdup_printf("%s.log", m->filename);
1710 changes = fopen(changes_file, "ab");
1711 g_hash_table_foreach(m->changes, write_changes_do, changes);
1712 fclose(changes);
1713 g_free(changes_file);
1714 }
1715
1716 static void load_changes(struct map_priv *m)
1717 {
1718 FILE *changes;
1719 char *changes_file;
1720 struct binfile_hash_entry entry, *e;
1721 int size;
1722 changes_file = g_strdup_printf("%s.log", m->filename);
1723 changes = fopen(changes_file, "rb");
1724 if (!changes)
1725 {
1726 g_free(changes_file);
1727 return;
1728 }
1729 m->changes = g_hash_table_new_full(binfile_hash_entry_hash, binfile_hash_entry_equal, g_free_func, NULL);
1730 while (fread(&entry, sizeof(entry), 1, changes) == 1)
1731 {
1732 if (fread(&size, sizeof(size), 1, changes) != 1)
1733 break;
1734 e = g_malloc(sizeof(struct binfile_hash_entry) + (le32_to_cpu(size) + 1) * 4);
1735 *e = entry;
1736 e->data[0] = size;
1737 if (fread(e->data + 1, le32_to_cpu(size) * 4, 1, changes) != 1)
1738 break;
1739 g_hash_table_replace(m->changes, e, e);
1740 }
1741 fclose(changes);
1742 g_free(changes_file);
1743 }
1744
1745 static void map_rect_destroy_binfile(struct map_rect_priv *mr)
1746 {
1747 write_changes(mr->m);
1748 while (pop_tile(mr))
1749 ;
1750 #ifdef DEBUG_SIZE
1751 // dbg(0,"size=%d kb\n",mr->size/1024);
1752 #endif
1753 if (mr->tiles[0].fi && mr->tiles[0].start)
1754 file_data_free(mr->tiles[0].fi, (unsigned char *) (mr->tiles[0].start));
1755 g_free(mr->url);
1756 map_binfile_http_close(mr->m);
1757 g_free(mr);
1758 }
1759
1760 static void setup_pos(struct map_rect_priv *mr)
1761 {
1762 int size, coord_size;
1763 struct tile *t = mr->t;
1764 size = le32_to_cpu(t->pos[0]);
1765 if (size > 1024 * 1024 || size < 0)
1766 {
1767 // dbg(0, "size=0x%x\n", size);
1768 #if 0
1769 fprintf(stderr,"offset=%d\n", (unsigned char *)(mr->pos)-mr->m->f->begin);
1770 #endif
1771 // dbg(0, "size error");
1772 }
1773 t->pos_next = t->pos + size + 1;
1774 mr->item.type = le32_to_cpu(t->pos[1]);
1775 coord_size = le32_to_cpu(t->pos[2]);
1776 t->pos_coord_start = t->pos + 3;
1777 t->pos_attr_start = t->pos_coord_start + coord_size;
1778 }
1779
1780 static int selection_contains(struct map_selection *sel, struct coord_rect *r, struct range *mima)
1781 {
1782 //// dbg(0,"EEnter\n");
1783
1784 int order;
1785 if (!sel)
1786 return 1;
1787 while (sel)
1788 {
1789 if (coord_rect_overlap(r, &sel->u.c_rect))
1790 {
1791 order = sel->order;
1792 // dbg(1, "min %d max %d order %d\n", mima->min, mima->max, order);
1793 if (!mima->min && !mima->max)
1794 return 1;
1795 if (order >= mima->min && order <= mima->max)
1796 return 1;
1797 }
1798 sel = sel->next;
1799 }
1800 return 0;
1801 }
1802
1803 static void map_parse_country_binfile(struct map_rect_priv *mr)
1804 {
1805 struct attr at;
1806 if (binfile_attr_get(mr->item.priv_data, attr_country_id, &at))
1807 {
1808 if (at.u.num == mr->country_id)
1809 {
1810 if (binfile_attr_get(mr->item.priv_data, attr_zipfile_ref, &at))
1811 {
1812 push_zipfile_tile(mr, at.u.num, 0, 0, 0);
1813 }
1814 }
1815 }
1816 }
1817
1818 static int map_parse_submap(struct map_rect_priv *mr, int async)
1819 {
1820 struct coord_rect r;
1821 struct coord c[2];
1822 struct attr at;
1823 struct range mima;
1824 if (binfile_coord_get(mr->item.priv_data, c, 2) != 2)
1825 return 0;
1826 r.lu.x = c[0].x;
1827 r.lu.y = c[1].y;
1828 r.rl.x = c[1].x;
1829 r.rl.y = c[0].y;
1830 if (!binfile_attr_get(mr->item.priv_data, attr_order, &at))
1831 return 0;
1832 #if __BYTE_ORDER == __BIG_ENDIAN
1833 mima.min = le16_to_cpu(at.u.range.max);
1834 mima.max = le16_to_cpu(at.u.range.min);
1835 #else
1836 mima=at.u.range;
1837 #endif
1838 if (!mr->m->eoc || !selection_contains(mr->sel, &r, &mima))
1839 return 0;
1840 if (!binfile_attr_get(mr->item.priv_data, attr_zipfile_ref, &at))
1841 return 0;
1842 // dbg(1, "pushing zipfile %d from %d\n", at.u.num, mr->t->zipfile_num);
1843 return push_zipfile_tile(mr, at.u.num, 0, 0, async);
1844 }
1845
1846 static int push_modified_item(struct map_rect_priv *mr)
1847 {
1848 struct item_id id;
1849 struct binfile_hash_entry *entry;
1850 id.id_hi = mr->item.id_hi;
1851 id.id_lo = mr->item.id_lo;
1852 entry = g_hash_table_lookup(mr->m->changes, &id);
1853 if (entry)
1854 {
1855 struct tile tn;
1856 tn.pos_next = tn.pos = tn.start = entry->data;
1857 tn.zipfile_num = mr->item.id_hi;
1858 tn.mode = 2;
1859 tn.end = tn.start + le32_to_cpu(entry->data[0]) + 1;
1860 push_tile(mr, &tn, 0, 0);
1861 return 1;
1862 }
1863 return 0;
1864 }
1865
1866 static struct item *
1867 map_rect_get_item_binfile(struct map_rect_priv *mr)
1868 {
1869 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1870 dbg(0,"+#+:enter\n");
1871 #endif
1872
1873 struct tile *t;
1874 struct map_priv *m = mr->m;
1875
1876 if (m->download)
1877 {
1878 download(m, NULL, NULL, 0, 0, 0, 2);
1879 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1880 dbg(0,"+#+:leave9\n");
1881 #endif
1882 return &busy_item;
1883 }
1884
1885 if (mr->status == 1)
1886 {
1887 mr->status = 0;
1888 if (push_zipfile_tile(mr, m->zip_members - 1, 0, 0, 1))
1889 {
1890 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1891 dbg(0,"+#+:leave8\n");
1892 #endif
1893 return &busy_item;
1894 }
1895 }
1896
1897 for (;;)
1898 {
1899 t = mr->t;
1900
1901 if (!t)
1902 {
1903 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1904 dbg(0,"+#+:leave6\n");
1905 #endif
1906 return NULL;
1907 }
1908
1909 t->pos = t->pos_next;
1910
1911 if (t->pos >= t->end)
1912 {
1913 if (pop_tile(mr))
1914 {
1915 continue;
1916 }
1917
1918 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1919 dbg(0,"+#+:leave5\n");
1920 #endif
1921
1922 return NULL;
1923 }
1924
1925 setup_pos(mr);
1926 binfile_coord_rewind(mr);
1927 binfile_attr_rewind(mr);
1928 if ((mr->item.type == type_submap) && (!mr->country_id))
1929 {
1930 if (map_parse_submap(mr, 1))
1931 {
1932 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1933 dbg(0,"+#+:leave4\n");
1934 #endif
1935 return &busy_item;
1936 }
1937 continue;
1938 }
1939 if (t->mode != 2)
1940 {
1941 mr->item.id_hi = t->zipfile_num;
1942 mr->item.id_lo = t->pos - t->start;
1943 if (mr->m->changes && push_modified_item(mr))
1944 continue;
1945 }
1946 if (mr->country_id)
1947 {
1948 if (mr->item.type == type_countryindex)
1949 {
1950 map_parse_country_binfile(mr);
1951 }
1952 if (item_is_town(mr->item))
1953 {
1954 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1955 dbg(0,"+#+:leave3\n");
1956 #endif
1957 return &mr->item;
1958 }
1959 else
1960 {
1961 continue;
1962 }
1963 }
1964 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1965 dbg(0,"+#+:leave 2\n");
1966 #endif
1967 return &mr->item;
1968 }
1969
1970 #ifdef NAVIT_FUNC_CALLS_DEBUG_PRINT
1971 dbg(0,"+#+:leave\n");
1972 #endif
1973 }
1974
1975 static struct item *
1976 map_rect_get_item_byid_binfile(struct map_rect_priv *mr, int id_hi, int id_lo)
1977 {
1978 struct tile *t;
1979 if (mr->m->eoc)
1980 {
1981 while (pop_tile(mr))
1982 ;
1983 push_zipfile_tile(mr, id_hi, 0, 0, 0);
1984 }
1985 t = mr->t;
1986 t->pos = t->start + id_lo;
1987 mr->item.id_hi = id_hi;
1988 mr->item.id_lo = id_lo;
1989 if (mr->m->changes)
1990 {
1991 push_modified_item(mr);
1992 }
1993 setup_pos(mr);
1994 binfile_coord_rewind(mr);
1995 binfile_attr_rewind(mr);
1996
1997 return &mr->item;
1998 }
1999
2000 static int binmap_search_by_index(struct map_priv *map, struct item *item, struct map_rect_priv **ret)
2001 {
2002 struct attr zipfile_ref;
2003 int *data;
2004
2005 if (!item)
2006 {
2007 *ret = NULL;
2008 return 0;
2009 }
2010 if (item_attr_get(item, attr_item_id, &zipfile_ref))
2011 {
2012 data = zipfile_ref.u.data;
2013 *ret = map_rect_new_binfile_int(map, NULL);
2014 push_zipfile_tile(*ret, le32_to_cpu(data[0]), le32_to_cpu(data[1]), -1, 0);
2015 return 3;
2016 }
2017 if (item_attr_get(item, attr_zipfile_ref, &zipfile_ref))
2018 {
2019 *ret = map_rect_new_binfile_int(map, NULL);
2020 push_zipfile_tile(*ret, zipfile_ref.u.num, 0, 0, 0);
2021 return 1;
2022 }
2023 if (item_attr_get(item, attr_zipfile_ref_block, &zipfile_ref))
2024 {
2025 data = zipfile_ref.u.data;
2026 *ret = map_rect_new_binfile_int(map, NULL);
2027 push_zipfile_tile(*ret, le32_to_cpu(data[0]), le32_to_cpu(data[1]), le32_to_cpu(data[2]), 0);
2028 return 2;
2029 }
2030 *ret = NULL;
2031 return 0;
2032 }
2033
2034 static struct map_rect_priv *
2035 binmap_search_street_by_place(struct map_priv *map, struct item *town, struct coord *c, struct map_selection *sel)
2036 {
2037 struct attr town_name, poly_town_name;
2038 struct map_rect_priv *map_rec2;
2039 struct item *place;
2040 int found = 0;
2041
2042 if (!item_attr_get(town, attr_label, &town_name))
2043 return NULL;
2044 sel->range = item_range_all;
2045 sel->order = 18;
2046 sel->next = NULL;
2047 sel->u.c_rect.lu = *c;
2048 sel->u.c_rect.rl = *c;
2049 map_rec2 = map_rect_new_binfile(map, sel);
2050 while ((place = map_rect_get_item_binfile(map_rec2)))
2051 {
2052 if (item_is_poly_place(*place) && item_attr_get(place, attr_label, &poly_town_name) && !strcmp(poly_town_name.u.str, town_name.u.str))
2053 {
2054 struct coord c[128];
2055 int i, count;
2056 found = 1;
2057 while ((count = item_coord_get(place, c, 128)))
2058 {
2059 for (i = 0; i < count; i++)
2060 {
2061 coord_rect_extend(&sel->u.c_rect, &c[i]);
2062 }
2063 }
2064 }
2065 }
2066
2067 map_rect_destroy_binfile(map_rec2);
2068 if (found)
2069 {
2070 return map_rect_new_binfile(map, sel);
2071 }
2072 return NULL;
2073 }
2074
2075 static struct map_rect_priv *
2076 binmap_search_street_by_estimate(struct map_priv *map, struct item *town, struct coord *c, struct map_selection *sel)
2077 {
2078 int size = 10000;
2079 switch (town->type)
2080 {
2081 case type_town_label_1e7:
2082 case type_town_label_5e6:
2083 case type_town_label_2e6:
2084 case type_town_label_1e6:
2085 case type_town_label_5e5:
2086 case type_town_label_2e5:
2087 size = 10000;
2088 break;
2089 case type_town_label_1e5:
2090 case type_town_label_5e4:
2091 case type_town_label_2e4:
2092 size = 5000;
2093 break;
2094 case type_town_label_1e4:
2095 case type_town_label_5e3:
2096 case type_town_label_2e3:
2097 size = 2500;
2098 break;
2099 case type_town_label_1e3:
2100 case type_town_label_5e2:
2101 case type_town_label_2e2:
2102 case type_town_label_1e2:
2103 case type_town_label_5e1:
2104 case type_town_label_2e1:
2105 case type_town_label_1e1:
2106 case type_town_label_5e0:
2107 case type_town_label_2e0:
2108 case type_town_label_1e0:
2109 case type_town_label_0e0:
2110 size = 1000;
2111 break;
2112 default:
2113 break;
2114 }
2115
2116 if (global_search_street_size_factor > 1)
2117 {
2118 size = size * global_search_street_size_factor;
2119 }
2120
2121 sel->u.c_rect.lu.x = c->x - size;
2122 sel->u.c_rect.lu.y = c->y + size;
2123 sel->u.c_rect.rl.x = c->x + size;
2124 sel->u.c_rect.rl.y = c->y - size;
2125 return map_rect_new_binfile(map, sel);
2126 }
2127
2128 static struct map_rect_priv *
2129 binmap_search_housenumber_by_estimate(struct map_priv *map, struct coord *c, struct map_selection *sel)
2130 {
2131 int size = 20;
2132 sel->u.c_rect.lu.x = c->x - size;
2133 sel->u.c_rect.lu.y = c->y + size;
2134 sel->u.c_rect.rl.x = c->x + size;
2135 sel->u.c_rect.rl.y = c->y - size;
2136
2137 sel->range = item_range_all;
2138 sel->order = 18;
2139 //sel->next = NULL;
2140
2141 return map_rect_new_binfile(map, sel);
2142 }
2143
2144 static struct map_search_priv *
2145 binmap_search_new(struct map_priv *map, struct item *item, struct attr *search, int partial)
2146 {
2147 struct map_rect_priv *map_rec;
2148 struct map_search_priv *msp=g_new0(struct map_search_priv, 1);
2149 struct item *town;
2150 int willsearch = 0;
2151
2152 msp->search = search;
2153 msp->partial = partial;
2154 /*
2155 * NOTE: If you implement search for other attributes than attr_town_name and attr_street_name,
2156 * please update this comment and the documentation for map_search_new() in map.c
2157 */
2158 switch (search->type)
2159 {
2160 case attr_country_name:
2161 break;
2162 case attr_town_name:
2163 case attr_town_or_district_name:
2164 map_rec = map_rect_new_binfile(map, NULL);
2165 if (!map_rec)
2166 {
2167 break;
2168 }
2169 map_rec->country_id = item->id_lo;
2170 msp->mr = map_rec;
2171 willsearch = 1;
2172 break;
2173 case attr_town_postal:
2174 break;
2175 case attr_street_name:
2176 if (!item->map)
2177 break;
2178 if (!map_priv_is(item->map, map))
2179 break;
2180 map_rec = map_rect_new_binfile(map, NULL);
2181 town = map_rect_get_item_byid_binfile(map_rec, item->id_hi, item->id_lo);
2182 if (town)
2183 {
2184 struct coord c;
2185
2186 if (binmap_search_by_index(map, town, &msp->mr))
2187 {
2188 //dbg(0, "search:mode=1\n");
2189 msp->mode = 1;
2190 }
2191 else
2192 {
2193 if (item_coord_get(town, &c, 1))
2194 {
2195 if ((msp->mr = binmap_search_street_by_place(map, town, &c, &msp->ms)))
2196 {
2197 //dbg(0, "search:mode=2\n");
2198 msp->mode = 2;
2199 }
2200 else
2201 {
2202 msp->mr = binmap_search_street_by_estimate(map, town, &c, &msp->ms);
2203 //dbg(0, "search:mode=3\n");
2204 msp->mode = 3;
2205 }
2206 }
2207 }
2208 map_rect_destroy_binfile(map_rec);
2209 if (!msp->mr)
2210 {
2211 break;
2212 }
2213 willsearch = 1;
2214 break;
2215 }
2216 map_rect_destroy_binfile(map_rec);
2217 break;
2218 case attr_house_number:
2219 // dbg(1, "case house_number");
2220 if (!item->map)
2221 break;
2222 if (!map_priv_is(item->map, map))
2223 break;
2224 msp->map = map;
2225 msp->mr_item = map_rect_new_binfile(map, NULL);
2226 msp->item = map_rect_get_item_byid_binfile(msp->mr_item, item->id_hi, item->id_lo);
2227 if (binmap_search_by_index(map, msp->item, &msp->mr) != 3)
2228 {
2229 struct coord c;
2230 if (item_coord_get(msp->item, &c, 1))
2231 {
2232 msp->mr = binmap_search_housenumber_by_estimate(map, &c, &msp->ms);
2233 msp->mode = 2;
2234 }
2235 map_rect_destroy_binfile(msp->mr_item);
2236 msp->mr_item = NULL;
2237 }
2238 if (!msp->mr)
2239 {
2240 break;
2241 }
2242 willsearch = 1;
2243 break;
2244 default:
2245 break;
2246 }
2247
2248 if (!willsearch)
2249 {
2250 g_free(msp);
2251 msp = NULL;
2252 }
2253 else
2254 {
2255 msp->str = linguistics_casefold(search->u.str);
2256 }
2257 return msp;
2258 }
2259
2260 static int ascii_cmp(char *name, char *match, int partial)
2261 {
2262 int ret = 1;
2263
2264 if (name == NULL)
2265 {
2266 return ret;
2267 }
2268
2269 if (match == NULL)
2270 {
2271 return ret;
2272 }
2273
2274 char *s2 = linguistics_casefold(match); // user entered search string
2275 char *s1 = linguistics_casefold(name); // string from mapfile
2276 char *s1_1 = linguistics_expand_special(s1, 1);
2277 char *s2_1 = linguistics_expand_special(s2, 1);
2278
2279 if (s1_1)
2280 {
2281 if (s2_1)
2282 {
2283 ret = linguistics_compare(s1_1, s2_1, partial);
2284 }
2285 else
2286 {
2287 ret = linguistics_compare(s1_1, s2, partial);
2288 }
2289 }
2290 else
2291 {
2292 if (s2_1)
2293 {
2294 ret = linguistics_compare(s1, s2_1, partial);
2295 }
2296 else
2297 {
2298 ret = linguistics_compare(s1, s2, partial);
2299 }
2300 }
2301
2302 if (s1_1)
2303 g_free(s1_1);
2304 if (s2_1)
2305 g_free(s2_1);
2306 if (s1)
2307 g_free(s1);
2308 if (s2)
2309 g_free(s2);
2310 return ret;
2311 }
2312
2313 struct duplicate
2314 {
2315 struct coord c;
2316 char str[0];
2317 };
2318
2319 static guint duplicate_hash(gconstpointer key)
2320 {
2321 const struct duplicate *d = key;
2322 return d->c.x ^ d->c.y ^ g_str_hash(d->str);
2323 }
2324
2325 static gboolean duplicate_equal(gconstpointer a, gconstpointer b)
2326 {
2327 const struct duplicate *da = a;
2328 const struct duplicate *db = b;
2329 return (da->c.x == db->c.x && da->c.y == da->c.y && g_str_equal(da->str, db->str));
2330 }
2331
2332 static int duplicate(struct map_search_priv *msp, struct item *item, enum attr_type attr_type)
2333 {
2334 struct attr attr;
2335 if (!msp->search_results)
2336 msp->search_results = g_hash_table_new_full(duplicate_hash, duplicate_equal, g_free_func, NULL);
2337 binfile_attr_rewind(item->priv_data);
2338 if (!item_attr_get(item, attr_type, &attr))
2339 return 1;
2340 {
2341 int len = sizeof(struct coord) + strlen(attr.u.str) + 1;
2342 char *buffer = g_alloca(sizeof(char) * len);
2343 struct duplicate *d = (struct duplicate *) buffer;
2344 if (!item_coord_get(item, &d->c, 1))
2345 {
2346 d->c.x = 0;
2347 d->c.y = 0;
2348 }
2349 binfile_coord_rewind(item->priv_data);
2350 strcpy(d->str, attr.u.str);
2351 if (!g_hash_table_lookup(msp->search_results, d))
2352 {
2353 struct duplicate *dc = g_malloc(len);
2354 memcpy(dc, d, len);
2355 g_hash_table_insert(msp->search_results, dc, GINT_TO_POINTER(1));
2356 binfile_attr_rewind(item->priv_data);
2357 return 0;
2358 }
2359 }
2360 return 2;
2361 }
2362
2363 static struct item *
2364 binmap_search_get_item(struct map_search_priv *map_search)
2365 {
2366 struct item* it;
2367 struct attr at;
2368
2369 // // dbg(0,"Enter\n");
2370
2371 for (;;)
2372 {
2373 while ((it = map_rect_get_item_binfile(map_search->mr)))
2374 {
2375 switch (map_search->search->type)
2376 {
2377 case attr_town_name:
2378 case attr_district_name:
2379 case attr_town_or_district_name:
2380
2381 #if 0
2382 if (map_search->mr->tile_depth > 1 && item_is_town(*it) && !item_is_district(*it) && map_search->search->type != attr_district_name)
2383 {
2384 if (binfile_attr_get(it->priv_data, attr_town_name_match, &at) || binfile_attr_get(it->priv_data, attr_town_name, &at))
2385 {
2386 if (!ascii_cmp(at.u.str, map_search->str, map_search->partial) && !duplicate(map_search, it, attr_town_name))
2387 return it;
2388 }
2389 }
2390 #endif
2391
2392 #if 1
2393 if (map_search->mr->tile_depth > 1 && item_is_town(*it) && !item_is_district(*it) && map_search->search->type != attr_district_name)
2394 {
2395 if (binfile_attr_get(it->priv_data, attr_town_name, &at))
2396 {
2397 //// dbg(0,"22town n:%s\n",at.u.str);
2398 if (!ascii_cmp(at.u.str, map_search->str, map_search->partial)
2399 // && !duplicate(map_search, it, attr_town_name)
2400 )
2401 {
2402 //// dbg(0,"town n:%s\n",at.u.str);
2403 return it;
2404 }
2405 }
2406
2407 if (binfile_attr_get(it->priv_data, attr_town_name_match, &at))
2408 {
2409 //// dbg(0,"11town M:%s\n",at.u.str);
2410 if (!ascii_cmp(at.u.str, map_search->str, map_search->partial)
2411 // && !duplicate(map_search, it, attr_town_name)
2412 )
2413 {
2414 //// dbg(0,"town M:%s\n",at.u.str);
2415 return it;
2416 }
2417 }
2418 }
2419 #endif
2420
2421 if (map_search->mr->tile_depth > 1 && item_is_district(*it) && map_search->search->type != attr_town_name)
2422 {
2423 if (binfile_attr_get(it->priv_data, attr_district_name_match, &at) || binfile_attr_get(it->priv_data, attr_district_name, &at))
2424 {
2425 if (!ascii_cmp(at.u.str, map_search->str, map_search->partial) && !duplicate(map_search, it, attr_town_name))
2426 return it;
2427 }
2428 }
2429 break;
2430
2431 case attr_street_name:
2432
2433 if (map_search->mode == 1)
2434 {
2435 //dbg(0,"*in here 00*\n");
2436
2437 //if (binfile_attr_get(it->priv_data, attr_street_name_match, &at) || binfile_attr_get(it->priv_data, attr_street_name, &at)) {
2438 // if (!ascii_cmp(at.u.str, map_search->str, map_search->partial) && !duplicate(map_search, it, attr_street_name)) {
2439 // return it;
2440 // }
2441 //}
2442
2443 if (binfile_attr_get(it->priv_data, attr_street_name, &at))
2444 {
2445 //// dbg(0,"str AA:%s\n", at.u.str);
2446
2447 if (!ascii_cmp(at.u.str, map_search->str, map_search->partial))
2448 {
2449 if ((offline_search_filter_duplicates != 1) || (!duplicate(map_search, it, attr_street_name)))
2450 {
2451 return it;
2452 }
2453 else
2454 {
2455 //// dbg(0,"dup street 1:%s\n",at.u.str);
2456 }
2457 }
2458 }
2459
2460 if (binfile_attr_get(it->priv_data, attr_street_name_match, &at))
2461 {
2462 //// dbg(0,"str BB:%s\n", at.u.str);
2463
2464 if (!ascii_cmp(at.u.str, map_search->str, map_search->partial))
2465 {
2466 if ((offline_search_filter_duplicates != 1) || (!duplicate(map_search, it, attr_street_name_match)))
2467 {
2468 return it;
2469 }
2470 else
2471 {
2472 //// dbg(0,"dup street 2:%s\n",at.u.str);
2473 }
2474 }
2475 }
2476
2477 continue;
2478 }
2479
2480 #if 1
2481 // ------------------------------------
2482 // map_search->mode == 2 or 3
2483 // ------------------------------------
2484 if (item_is_street(*it))
2485 {
2486 // dbg(0,"*in here 11*\n");
2487
2488 struct attr at;
2489 if (map_selection_contains_item_rect(map_search->mr->sel, it))
2490 {
2491 if (binfile_attr_get(it->priv_data, attr_label, &at))
2492 {
2493 int match = 0;
2494 char *str = g_strdup(at.u.str);
2495 // dbg(0,"search=%s str is:%s\n",map_search->str, at.u.str);
2496 if (!ascii_cmp(str, map_search->str, map_search->partial))
2497 {
2498 match = 1;
2499 }
2500 g_free(str);
2501 if (match)
2502 {
2503 if ((offline_search_filter_duplicates != 1) || (!duplicate(map_search, it, attr_label)))
2504 {
2505 item_coord_rewind(it);
2506 return it;
2507 }
2508 }
2509 }
2510
2511 if (binfile_attr_get(it->priv_data, attr_street_name_match, &at))
2512 {
2513 int match = 0;
2514 char *str = g_strdup(at.u.str);
2515 // dbg(0,"search=%s mstr is:%s\n",map_search->str, at.u.str);
2516 if (!ascii_cmp(str, map_search->str, map_search->partial))
2517 {
2518 match = 1;
2519 }
2520 g_free(str);
2521 if (match)
2522 {
2523 if ((offline_search_filter_duplicates != 1) || (!duplicate(map_search, it, attr_street_name_match)))
2524 {
2525 item_coord_rewind(it);
2526 return it;
2527 }
2528 }
2529 }
2530 //else
2531 //{
2532 // if (binfile_attr_get(it->priv_data, attr_street_name, &at))
2533 // {
2534 // // dbg(0,"---str X*X:%s\n", at.u.str);
2535 // }
2536 //}
2537 }
2538 }
2539 #endif
2540 break;
2541 case attr_house_number:
2542 //if (it->type == type_house_number)
2543 if ((it->type == type_house_number) || (type_house_number_interpolation_even) || (type_house_number_interpolation_odd) || (type_house_number_interpolation_all))
2544 {
2545 // is it a housenumber?
2546 if (binfile_attr_get(it->priv_data, attr_house_number, &at))
2547 {
2548 // match housenumber to our string
2549 if (!ascii_cmp(at.u.str, map_search->str, map_search->partial))
2550 {
2551 //binfile_attr_get(it->priv_data, attr_street_name, &at);
2552 //// dbg(0,"hnnn B1 street_name=%s",at.u.str);
2553
2554 if (!duplicate(map_search, it, attr_house_number))
2555 {
2556 binfile_attr_rewind(it->priv_data);
2557 return it;
2558 }
2559 }
2560 }
2561 else
2562 {
2563 return it;
2564 }
2565 }
2566 continue;
2567 default:
2568 return NULL;
2569 }
2570 }
2571
2572 if (!map_search->mr_item)
2573 {
2574 return NULL;
2575 }
2576 map_rect_destroy_binfile(map_search->mr);
2577 if (!binmap_search_by_index(map_search->map, map_search->item, &map_search->mr))
2578 {
2579 return NULL;
2580 }
2581 } // endless for-loop
2582
2583 return NULL; // this is never reached
2584 }
2585
2586 static void binmap_search_destroy(struct map_search_priv *ms)
2587 {
2588 if (ms->search_results)
2589 g_hash_table_destroy(ms->search_results);
2590 if (ms->mr_item)
2591 map_rect_destroy_binfile(ms->mr_item);
2592 if (ms->mr)
2593 map_rect_destroy_binfile(ms->mr);
2594 if (ms->str)
2595 g_free(ms->str);
2596 g_free(ms);
2597 }
2598
2599 static int binmap_get_attr(struct map_priv *m, enum attr_type type, struct attr *attr)
2600 {
2601 attr->type = type;
2602 switch (type)
2603 {
2604 case attr_map_release:
2605 if (m->map_release)
2606 {
2607 attr->u.str = m->map_release;
2608 return 1;
2609 }
2610 break;
2611 case attr_progress:
2612 if (m->progress)
2613 {
2614 attr->u.str = m->progress;
2615 return 1;
2616 }
2617 default:
2618 break;
2619 }
2620 return 0;
2621 }
2622
2623 static int binmap_set_attr(struct map_priv *map, struct attr *attr)
2624 {
2625 switch (attr->type)
2626 {
2627 case attr_update:
2628 map->download_enabled = attr->u.num;
2629 return 1;
2630 default:
2631 return 0;
2632 }
2633
2634 }
2635
2636 static struct map_methods map_methods_binfile =
2637 { projection_mg, "utf-8", map_destroy_binfile, map_rect_new_binfile, map_rect_destroy_binfile, map_rect_get_item_binfile, map_rect_get_item_byid_binfile, binmap_search_new, binmap_search_destroy, binmap_search_get_item, NULL, binmap_get_attr, binmap_set_attr, };
2638
2639 static int binfile_get_index(struct map_priv *m)
2640 {
2641 //dbg(0,"_enter\n");
2642 int len;
2643 int cde_index_size;
2644 long long offset; // int or long long ?
2645 struct zip_cd *cd;
2646
2647 len = strlen("index");
2648 cde_index_size = sizeof(struct zip_cd) + len;
2649 //dbg(0,"_cde_index_size=%d\n", cde_index_size);
2650 if (m->eoc64)
2651 {
2652 //dbg(0,"_m->eoc64->zip64ecsz=%d\n", m->eoc64->zip64ecsz);
2653 offset = m->eoc64->zip64ecsz - cde_index_size;
2654 //dbg(0,"_eoc64 offset="LONGLONG_FMT"\n", offset);
2655 }
2656 else
2657 {
2658 //dbg(0,"_m->eoc->zipecsz=%d\n", m->eoc->zipecsz);
2659 offset = m->eoc->zipecsz - cde_index_size;
2660 //dbg(0,"_eoc offset="LONGLONG_FMT"\n", offset);
2661 }
2662 cd = binfile_read_cd(m, offset, len);
2663
2664 if (!cd)
2665 {
2666 //dbg(0,"_!cd\n");
2667 cde_index_size += sizeof(struct zip_cd_ext);
2668 //dbg(0,"_cde_index_size=%d\n", cde_index_size);
2669 if (m->eoc64)
2670 {
2671 offset = m->eoc64->zip64ecsz - cde_index_size;
2672 //dbg(0,"_eoc64 offset aa.1="LONGLONG_FMT"\n", offset);
2673 }
2674 else
2675 {
2676 offset = m->eoc->zipecsz - cde_index_size;
2677 //dbg(0,"_offset 22.0="LONGLONG_FMT"\n", offset);
2678 }
2679 cd = binfile_read_cd(m, offset, len + sizeof(struct zip_cd_ext));
2680 }
2681 //dbg(0,"_offset 22="LONGLONG_FMT"\n", offset);
2682
2683 if (cd)
2684 {
2685 //dbg(0,"_cd 001\n");
2686 if (cd->zipcfnl == len && !strncmp(cd->zipcfn, "index", len))
2687 {
2688 m->index_offset = offset;
2689 m->index_cd = cd;
2690 //dbg(0,"_leave:offset 33="LONGLONG_FMT"\n", offset);
2691 return 1;
2692 }
2693 //dbg(0,"_cd 002\n");
2694 }
2695
2696 offset = binfile_search_cd(m, 0, "index", 0, 0);
2697 //dbg(0,"_offset 44="LONGLONG_FMT"\n", offset);
2698
2699 if (offset == -1)
2700 {
2701 //dbg(0,"_leave:55\n");
2702 return 0;
2703 }
2704
2705 cd = binfile_read_cd(m, offset, -1);
2706
2707 if (!cd)
2708 {
2709 //dbg(0,"_leave:66\n");
2710 return 0;
2711 }
2712
2713 m->index_offset = offset;
2714 m->index_cd = cd;
2715
2716 //dbg(0,"_leave:normal:offset 77="LONGLONG_FMT"\n", offset);
2717 return 1;
2718 }
2719
2720 static int map_binfile_zip_setup(struct map_priv *m, char *filename, int mmap)
2721 {
2722 //dbg(0,"_enter\n");
2723
2724 struct zip_cd *first_cd;
2725 int i;
2726 if (!(m->eoc = binfile_read_eoc(m->fi)))
2727 {
2728 //dbg(0,"_leave:1\n");
2729 return 0;
2730 }
2731 dbg_assert(m->eoc->zipedsk == m->eoc->zipecen);
2732 if (m->eoc->zipedsk && strlen(filename) > 3)
2733 {
2734 char *tmpfilename = g_strdup(filename);
2735 char *ext = tmpfilename + strlen(tmpfilename) - 3;
2736
2737 //dbg(0,"_ff-1:%s\n", tmpfilename);
2738 //dbg(0,"_ff-2:%s\n", ext);
2739
2740 m->fis=g_new(struct file *,m->eoc->zipedsk);
2741 for (i = 0; i < m->eoc->zipedsk - 1; i++)
2742 {
2743 sprintf(ext, "b%02d", i + 1);
2744 //dbg(0,"_ff--3:%s\n", ext);
2745 m->fis[i] = file_create(tmpfilename, 0);
2746 if (mmap)
2747 {
2748 //dbg(0,"_want mmap 2 %s\n", m->fis[i]->name);
2749 file_mmap(m->fis[i]);
2750 }
2751 }
2752 m->fis[m->eoc->zipedsk - 1] = m->fi;
2753 g_free(tmpfilename);
2754 }
2755 else
2756 {
2757 //dbg(0,"_ff-XX\n");
2758 }
2759 //dbg(0, "_num_disk %d\n", m->eoc->zipedsk);
2760 m->eoc64 = binfile_read_eoc64(m->fi);
2761
2762 if (!binfile_get_index(m))
2763 {
2764 //dbg(0,"_leave:2\n");
2765 return 0;
2766 }
2767
2768 if (!(first_cd = binfile_read_cd(m, 0, 0)))
2769 {
2770 //dbg(0,"_leave:3\n");
2771 return 0;
2772 }
2773
2774 m->cde_size = sizeof(struct zip_cd) + first_cd->zipcfnl + first_cd->zipcxtl;
2775 m->zip_members = m->index_offset / m->cde_size + 1;
2776 //dbg(0, "_cde_size %d\n", m->cde_size);
2777 //dbg(0, "_members %d\n", m->zip_members);
2778 file_data_free(m->fi, (unsigned char *) first_cd);
2779 if (mmap)
2780 {
2781 //dbg(0,"_want mmap 3 %s\n", m->fi->name);
2782 file_mmap(m->fi);
2783 }
2784 return 1;
2785 }
2786
2787 #if 0
2788 static int
2789 map_binfile_download_initial(struct map_priv *m)
2790 {
2791 struct attr readwrite=
2792 { attr_readwrite,
2793 { (void *)1}};
2794 struct attr create=
2795 { attr_create,
2796 { (void *)1}};
2797 struct attr *attrs[4];
2798 struct file *out;
2799 long long woffset=0,planet_size;
2800 int size_ret;
2801 int cd1size,cdisize;
2802 long long cd1offset,cdioffset;
2803 struct zip64_eoc *zip64_eoc;
2804 struct zip64_eocl *zip64_eocl;
2805 struct zip_eoc *zip_eoc;
2806 struct zip_cd *cd1,*cdn,*cdi;
2807 int count,chunk,cdoffset=0;
2808 int mode=1;
2809 struct map_download *download=g_new0(struct map_download, 1);
2810
2811 attrs[0]=&readwrite;
2812 attrs[1]=&create;
2813 attrs[2]=NULL;
2814 download->file=file_create(m->filename,attrs);
2815 download->m=m;
2816 download_planet_size(download);
2817 download_eoc(download);
2818 download_directory_start(download);
2819 while (download_directory_do(download));
2820 download_directory_finish(download);
2821 download_initial_finish(download);
2822 m->fi=download->file;
2823 g_free(download);
2824 return 1;
2825
2826 cd1size=sizeof(*cd1);
2827 cd1offset=zip64_eoc->zip64eofst;
2828 cd1=(struct zip_cd *)map_binfile_download_range(m, cd1offset, cd1size);
2829 if (!cd1)
2830 return 0;
2831 cd1size=sizeof(*cd1)+binfile_cd_extra(cd1);
2832 g_free(cd1);
2833 cd1=(struct zip_cd *)map_binfile_download_range(m, cd1offset, cd1size);
2834 if (!cd1)
2835 return 0;
2836 cd1->zipcunc=0;
2837 cdisize=sizeof(*cdi)+strlen("index")+cd1->zipcxtl;
2838 cdioffset=zip64_eoc->zip64eofst+zip64_eoc->zip64ecsz-cdisize;
2839 cdi=(struct zip_cd *)map_binfile_download_range(m, cdioffset, cdisize);
2840 if (!cdi)
2841 {
2842 g_free(cd1);
2843 return 0;
2844 }
2845 cdi->zipcunc=0;
2846 cdn=g_malloc0(cd1size*256);
2847
2848 file_data_write(out, woffset, sizeof(*zip64_eoc), (unsigned char *)zip64_eoc);
2849 woffset+=sizeof(*zip64_eoc);
2850 cdoffset=woffset;
2851
2852 file_data_write(out, woffset, cd1size, (unsigned char *)cd1);
2853 woffset+=cd1size;
2854 count=(cdioffset-cd1offset)/cd1size-1;
2855 while (count > 0)
2856 {
2857 if (count > 256)
2858 chunk=256;
2859 else
2860 chunk=count;
2861 file_data_write(out, woffset, cd1size*chunk, (unsigned char *)cdn);
2862 woffset+=cd1size*chunk;
2863 count-=chunk;
2864 }
2865 g_free(cdn);
2866 g_free(cd1);
2867 file_data_write(out, woffset, cdisize, (unsigned char *)cdi);
2868 woffset+=cdisize;
2869
2870 }
2871 #endif
2872
2873 static int map_binfile_open(struct map_priv *m)
2874 {
2875 int *magic;
2876 struct map_rect_priv *mr;
2877 struct item *item;
2878 struct attr attr;
2879 struct attr readwrite =
2880 { attr_readwrite,
2881 { (void *) 1 } };
2882 struct attr *attrs[] =
2883 { &readwrite, NULL };
2884
2885 // dbg(0, "file_create %s\n", m->filename);
2886 m->fi = file_create(m->filename, m->url ? attrs : NULL);
2887 if (!m->fi && m->url)
2888 {
2889 return 0;
2890 }
2891 if (!m->fi)
2892 {
2893 // dbg(0, "Failed to load '%s'\n", m->filename);
2894 return 0;
2895 }
2896 if (m->check_version)
2897 {
2898 m->version = file_version(m->fi, m->check_version);
2899 }
2900
2901 magic = (int *) file_data_read(m->fi, 0, 4);
2902 if (!magic)
2903 {
2904 file_destroy(m->fi);
2905 m->fi = NULL;
2906 return 0;
2907 }
2908
2909 //dbg(0,"_zip_lfh_sig=%d\n", zip_lfh_sig);
2910
2911 //dbg(0,"_magic1=%d\n", magic[0]);
2912 //dbg(0,"_magic1=%d\n", magic[1]);
2913 //dbg(0,"_magic1=%d\n", magic[2]);
2914 //dbg(0,"_magic1=%d\n", magic[3]);
2915 *magic = le32_to_cpu(*magic);
2916 //dbg(0,"_magic2=%d\n", magic[0]);
2917 //dbg(0,"_magic2=%d\n", magic[1]);
2918 //dbg(0,"_magic2=%d\n", magic[2]);
2919 //dbg(0,"_magic2=%d\n", magic[3]);
2920 if (*magic == zip_lfh_sig || *magic == zip_split_sig || *magic == zip_cd_sig || *magic == zip64_eoc_sig)
2921 {
2922 if (!map_binfile_zip_setup(m, m->filename, m->flags & 1))
2923 {
2924 dbg(0, "invalid file format for '%s'\n", m->filename);
2925 file_destroy(m->fi);
2926 m->fi = NULL;
2927 return 0;
2928 }
2929 }
2930 else
2931 {
2932 //dbg(0,"_want mmap %s\n", m->fi->name);
2933 file_mmap(m->fi);
2934 }
2935
2936 //dbg(0,"_cont\n");
2937
2938 file_data_free(m->fi, (unsigned char *) magic);
2939 m->cachedir = g_strdup("/tmp/navit");
2940 m->map_version = 0;
2941
2942 mr = map_rect_new_binfile(m, NULL);
2943 if (mr)
2944 {
2945 while ((item = map_rect_get_item_binfile(mr)) == &busy_item)
2946 {
2947 ;
2948 }
2949
2950 if (item && item->type == type_map_information)
2951 {
2952 if (binfile_attr_get(item->priv_data, attr_version, &attr))
2953 {
2954 m->map_version = attr.u.num;
2955 dbg(0, "map_version=%d\n", m->map_version);
2956 }
2957 if (binfile_attr_get(item->priv_data, attr_map_release, &attr))
2958 {
2959 m->map_release = g_strdup(attr.u.str);
2960 dbg(0, "map_release=%s\n", m->map_release);
2961 }
2962 if (m->url && binfile_attr_get(item->priv_data, attr_url, &attr))
2963 {
2964 //dbg(0, "url config %s map %s\n", m->url, attr.u.str);
2965 if (strcmp(m->url, attr.u.str))
2966 {
2967 m->update_available = 1;
2968 }
2969 g_free(m->url);
2970 m->url = g_strdup(attr.u.str);
2971 }
2972 }
2973
2974 map_rect_destroy_binfile(mr);
2975
2976 if (m->map_version >= 16)
2977 {
2978 dbg(0, "Warning: This map is incompatible with your versionof ZANavi. Please update ZANavi.\n");
2979 return 0;
2980 }
2981 }
2982 return 1;
2983 }
2984
2985 static void map_binfile_close(struct map_priv *m)
2986 {
2987 int i;
2988 file_data_free(m->fi, (unsigned char *) m->index_cd);
2989 file_data_free(m->fi, (unsigned char *) m->eoc);
2990 file_data_free(m->fi, (unsigned char *) m->eoc64);
2991 g_free(m->cachedir);
2992 g_free(m->map_release);
2993 if (m->fis)
2994 {
2995 for (i = 0; i < m->eoc->zipedsk; i++)
2996 {
2997 file_destroy(m->fis[i]);
2998 }
2999 }
3000 else
3001 file_destroy(m->fi);
3002 }
3003
3004 static void map_binfile_destroy(struct map_priv *m)
3005 {
3006 g_free(m->filename);
3007 g_free(m->url);
3008 g_free(m->progress);
3009 g_free(m);
3010 }
3011
3012 static void binfile_check_version(struct map_priv *m)
3013 {
3014 int version = -1;
3015 if (!m->check_version)
3016 return;
3017 if (m->fi)
3018 version = file_version(m->fi, m->check_version);
3019 if (version != m->version)
3020 {
3021 if (m->fi)
3022 map_binfile_close(m);
3023 map_binfile_open(m);
3024 }
3025 }
3026
3027 static struct map_priv *
3028 map_new_binfile(struct map_methods *meth, struct attr **attrs, struct callback_list *cbl)
3029 {
3030 struct map_priv *m;
3031 struct attr *data = attr_search(attrs, NULL, attr_data);
3032 struct attr *check_version, *map_pass, *flags, *url, *download_enabled;
3033 struct file_wordexp *wexp;
3034 char **wexp_data;
3035 if (!data)
3036 return NULL;
3037
3038 // wexp = file_wordexp_new(data->u.str);
3039 // wexp_data = file_wordexp_get_array(wexp);
3040 // dbg(0, "map_new_binfile %s\n", data->u.str);
3041 *meth = map_methods_binfile;
3042
3043 m=g_new0(struct map_priv, 1);
3044 m->cbl = cbl;
3045 m->id = ++map_id;
3046 m->filename = g_strdup(data->u.str);
3047 // file_wordexp_destroy(wexp);
3048 check_version = attr_search(attrs, NULL, attr_check_version);
3049 if (check_version)
3050 m->check_version = check_version->u.num;
3051 map_pass = attr_search(attrs, NULL, attr_map_pass);
3052 if (map_pass)
3053 m->passwd = g_strdup(map_pass->u.str);
3054 flags = attr_search(attrs, NULL, attr_flags);
3055 if (flags)
3056 m->flags = flags->u.num;
3057 url = attr_search(attrs, NULL, attr_url);
3058 if (url)
3059 m->url = g_strdup(url->u.str);
3060 download_enabled = attr_search(attrs, NULL, attr_update);
3061 if (download_enabled)
3062 m->download_enabled = download_enabled->u.num;
3063
3064 if (!map_binfile_open(m) && !m->check_version && !m->url)
3065 {
3066 map_binfile_destroy(m);
3067 m = NULL;
3068 }
3069 else
3070 {
3071 load_changes(m);
3072 }
3073 return m;
3074 }
3075
3076 void plugin_init(void)
3077 {
3078 // dbg(1, "binfile: plugin_init\n");
3079 if (sizeof(struct zip_cd) != 46)
3080 {
3081 // dbg(0, "error: sizeof(struct zip_cd)=%d\n", sizeof(struct zip_cd));
3082 }
3083 plugin_register_map_type("binfile", map_new_binfile);
3084 }
3085

   
Visit the ZANavi Wiki