2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
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.
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.
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.
28 #include "projection.h"
34 #include "transform.h"
37 #include "linguistics.h"
38 #include "endianess.h"
61 struct map_rect_priv *mr;
62 struct file *http,*file;
63 int zipfile,toffset,tlength,progress,read,dl_size;
64 long long offset,start_offset,cd1offset,size;
65 struct zip64_eoc *zip64_eoc;
66 struct zip64_eocl *zip64_eocl;
67 struct zip_eoc *zip_eoc;
68 struct zip_cd *cd_copy,*cd;
75 struct file *fi,*http;
77 struct zip_cd *index_cd;
81 struct zip64_eoc *eoc64;
83 unsigned char *search_data;
96 struct callback_list *cbl;
97 struct map_download *download;
99 long download_enabled;
102 struct map_rect_priv {
105 enum attr_type attr_last;
108 struct map_selection *sel;
112 struct tile tiles[8];
116 struct attr attrs[8];
123 struct map_search_priv {
124 struct map_priv *map;
125 struct map_rect_priv *mr;
126 struct map_rect_priv *mr_item;
129 struct map_selection ms;
132 GHashTable *search_results;
136 static void push_tile(struct map_rect_priv *mr, struct tile *t, int offset, int length);
137 static void setup_pos(struct map_rect_priv *mr);
138 static void map_binfile_close(struct map_priv *m);
139 static int map_binfile_open(struct map_priv *m);
140 static void map_binfile_destroy(struct map_priv *m);
142 static void lfh_to_cpu(struct zip_lfh *lfh) {
143 dbg_assert(lfh != NULL);
144 if (lfh->ziplocsig != zip_lfh_sig) {
145 lfh->ziplocsig = le32_to_cpu(lfh->ziplocsig);
146 lfh->zipver = le16_to_cpu(lfh->zipver);
147 lfh->zipgenfld = le16_to_cpu(lfh->zipgenfld);
148 lfh->zipmthd = le16_to_cpu(lfh->zipmthd);
149 lfh->ziptime = le16_to_cpu(lfh->ziptime);
150 lfh->zipdate = le16_to_cpu(lfh->zipdate);
151 lfh->zipcrc = le32_to_cpu(lfh->zipcrc);
152 lfh->zipsize = le32_to_cpu(lfh->zipsize);
153 lfh->zipuncmp = le32_to_cpu(lfh->zipuncmp);
154 lfh->zipfnln = le16_to_cpu(lfh->zipfnln);
155 lfh->zipxtraln = le16_to_cpu(lfh->zipxtraln);
159 static void cd_to_cpu(struct zip_cd *zcd) {
160 dbg_assert(zcd != NULL);
161 if (zcd->zipcensig != zip_cd_sig) {
162 zcd->zipcensig = le32_to_cpu(zcd->zipcensig);
163 zcd->zipccrc = le32_to_cpu(zcd->zipccrc);
164 zcd->zipcsiz = le32_to_cpu(zcd->zipcsiz);
165 zcd->zipcunc = le32_to_cpu(zcd->zipcunc);
166 zcd->zipcfnl = le16_to_cpu(zcd->zipcfnl);
167 zcd->zipcxtl = le16_to_cpu(zcd->zipcxtl);
168 zcd->zipccml = le16_to_cpu(zcd->zipccml);
169 zcd->zipdsk = le16_to_cpu(zcd->zipdsk);
170 zcd->zipint = le16_to_cpu(zcd->zipint);
171 zcd->zipext = le32_to_cpu(zcd->zipext);
172 zcd->zipofst = le32_to_cpu(zcd->zipofst);
176 static void eoc_to_cpu(struct zip_eoc *eoc) {
177 dbg_assert(eoc != NULL);
178 if (eoc->zipesig != zip_eoc_sig) {
179 eoc->zipesig = le32_to_cpu(eoc->zipesig);
180 eoc->zipedsk = le16_to_cpu(eoc->zipedsk);
181 eoc->zipecen = le16_to_cpu(eoc->zipecen);
182 eoc->zipenum = le16_to_cpu(eoc->zipenum);
183 eoc->zipecenn = le16_to_cpu(eoc->zipecenn);
184 eoc->zipecsz = le32_to_cpu(eoc->zipecsz);
185 eoc->zipeofst = le32_to_cpu(eoc->zipeofst);
186 eoc->zipecoml = le16_to_cpu(eoc->zipecoml);
190 static void binfile_check_version(struct map_priv *m);
192 static struct zip_eoc *
193 binfile_read_eoc(struct file *fi)
196 eoc=(struct zip_eoc *)file_data_read(fi,fi->size-sizeof(struct zip_eoc), sizeof(struct zip_eoc));
199 dbg(1,"sig 0x%x\n", eoc->zipesig);
200 if (eoc->zipesig != zip_eoc_sig) {
201 file_data_free(fi,(unsigned char *)eoc);
208 static struct zip64_eoc *
209 binfile_read_eoc64(struct file *fi)
211 struct zip64_eocl *eocl;
212 struct zip64_eoc *eoc;
213 eocl=(struct zip64_eocl *)file_data_read(fi,fi->size-sizeof(struct zip_eoc)-sizeof(struct zip64_eocl), sizeof(struct zip64_eocl));
216 dbg(1,"sig 0x%x\n", eocl->zip64lsig);
217 if (eocl->zip64lsig != zip64_eocl_sig) {
218 file_data_free(fi,(unsigned char *)eocl);
219 dbg(1,"eocl wrong\n");
222 eoc=(struct zip64_eoc *)file_data_read(fi,eocl->zip64lofst, sizeof(struct zip64_eoc));
224 if (eoc->zip64esig != zip64_eoc_sig) {
225 file_data_free(fi,(unsigned char *)eoc);
226 dbg(1,"eoc wrong\n");
229 dbg(1,"eoc64 ok 0x%Lx 0x%Lx\n",eoc->zip64eofst,eoc->zip64ecsz);
231 file_data_free(fi,(unsigned char *)eocl);
236 binfile_cd_extra(struct zip_cd *cd)
238 return cd->zipcfnl+cd->zipcxtl;
241 static struct zip_cd *
242 binfile_read_cd(struct map_priv *m, int offset, int len)
245 long long cdoffset=m->eoc64?m->eoc64->zip64eofst:m->eoc->zipeofst;
247 cd=(struct zip_cd *)file_data_read(m->fi,cdoffset+offset, sizeof(*cd));
249 len=binfile_cd_extra(cd);
250 file_data_free(m->fi,(unsigned char *)cd);
252 cd=(struct zip_cd *)file_data_read(m->fi,cdoffset+offset, sizeof(*cd)+len);
254 dbg(1,"cd at "LONGLONG_FMT" %d bytes\n",cdoffset+offset, sizeof(*cd)+len);
256 dbg(1,"sig 0x%x\n", cd->zipcensig);
257 if (cd->zipcensig != zip_cd_sig) {
258 file_data_free(m->fi,(unsigned char *)cd);
265 static struct zip_cd_ext *
266 binfile_cd_ext(struct zip_cd *cd)
268 struct zip_cd_ext *ext;
269 if (cd->zipofst != 0xffffffff)
271 if (cd->zipcxtl != sizeof(*ext))
273 ext=(struct zip_cd_ext *)((unsigned char *)cd+sizeof(*cd)+cd->zipcfnl);
274 if (ext->tag != 0x0001 || ext->size != 8)
280 binfile_cd_offset(struct zip_cd *cd)
282 struct zip_cd_ext *ext=binfile_cd_ext(cd);
289 static struct zip_lfh *
290 binfile_read_lfh(struct file *fi, long long offset)
294 lfh=(struct zip_lfh *)(file_data_read(fi,offset,sizeof(struct zip_lfh)));
297 if (lfh->ziplocsig != zip_lfh_sig) {
298 file_data_free(fi,(unsigned char *)lfh);
305 static unsigned char *
306 binfile_read_content(struct map_priv *m, struct file *fi, long long offset, struct zip_lfh *lfh)
309 unsigned char *ret=NULL;
311 offset+=sizeof(struct zip_lfh)+lfh->zipfnln;
312 switch (lfh->zipmthd) {
314 offset+=lfh->zipxtraln;
315 ret=file_data_read(fi,offset, lfh->zipuncmp);
318 offset+=lfh->zipxtraln;
319 ret=file_data_read_compressed(fi,offset, lfh->zipsize, lfh->zipuncmp);
324 enc=(struct zip_enc *)file_data_read(fi, offset, sizeof(*enc));
325 offset+=lfh->zipxtraln;
326 switch (enc->compress_method) {
328 ret=file_data_read_encrypted(fi, offset, lfh->zipsize, lfh->zipuncmp, 0, m->passwd);
331 ret=file_data_read_encrypted(fi, offset, lfh->zipsize, lfh->zipuncmp, 1, m->passwd);
334 dbg(0,"Unknown encrypted compression method %d\n",enc->compress_method);
336 file_data_free(fi, (unsigned char *)enc);
339 dbg(0,"Unknown compression method %d\n", lfh->zipmthd);
345 binfile_search_cd(struct map_priv *m, int offset, char *name, int partial, int skip)
348 int end=m->eoc64?m->eoc64->zip64ecsz:m->eoc->zipecsz;
349 int len=strlen(name);
350 long long cdoffset=m->eoc64?m->eoc64->zip64eofst:m->eoc->zipeofst;
353 dbg(0,"end=%d\n",end);
355 while (offset < end) {
356 cd=(struct zip_cd *)(m->search_data+offset-m->search_offset);
357 if (! m->search_data ||
358 m->search_offset > offset ||
359 offset-m->search_offset+sizeof(*cd) > m->search_size ||
360 offset-m->search_offset+sizeof(*cd)+cd->zipcfnl+cd->zipcxtl > m->search_size
363 dbg(0,"reload %p %d %d\n", m->search_data, m->search_offset, offset);
366 file_data_free(m->fi,m->search_data);
367 m->search_offset=offset;
368 m->search_size=end-offset;
369 if (m->search_size > size)
371 m->search_data=file_data_read(m->fi,cdoffset+m->search_offset,m->search_size);
372 cd=(struct zip_cd *)m->search_data;
375 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);
376 dbg(0,"offset=%d fn='%s'\n",offset,cd->zipcfn);
379 (partial || cd->zipcfnl == len) &&
380 !strncmp(cd->zipcfn, name, len))
383 offset+=sizeof(*cd)+cd->zipcfnl+cd->zipcxtl+cd->zipccml;
390 map_destroy_binfile(struct map_priv *m)
392 dbg(1,"map_destroy_binfile\n");
394 map_binfile_close(m);
395 map_binfile_destroy(m);
399 binfile_coord_rewind(void *priv_data)
401 struct map_rect_priv *mr=priv_data;
402 struct tile *t=mr->t;
403 t->pos_coord=t->pos_coord_start;
407 binfile_coord_get(void *priv_data, struct coord *c, int count)
409 struct map_rect_priv *mr=priv_data;
410 struct tile *t=mr->t;
412 max=(t->pos_attr_start-t->pos_coord)/2;
415 #if __BYTE_ORDER == __LITTLE_ENDIAN
416 memcpy(c, t->pos_coord, count*sizeof(struct coord));
419 int i=0,end=count*sizeof(struct coord)/sizeof(int);
420 int *src=(int *)t->pos_coord;
423 *dst++=le32_to_cpu(*src);
428 t->pos_coord+=count*2;
434 binfile_attr_rewind(void *priv_data)
436 struct map_rect_priv *mr=priv_data;
437 struct tile *t=mr->t;
438 t->pos_attr=t->pos_attr_start;
440 memset(mr->label_attr, 0, sizeof(mr->label_attr));
445 binfile_extract(struct map_priv *m, char *dir, char *filename, int partial)
447 char *full,*fulld,*sep;
448 unsigned char *start;
449 int len,offset=m->index_offset;
455 offset=binfile_search_cd(m, offset, filename, partial, 1);
458 cd=binfile_read_cd(m, offset, -1);
459 len=strlen(dir)+1+cd->zipcfnl+1;
462 strcpy(full+strlen(full),"/");
463 strncpy(full+strlen(full),cd->zipcfn,cd->zipcfnl);
465 fulld=g_strdup(full);
466 sep=strrchr(fulld, '/');
469 file_mkdir(fulld, 1);
471 if (full[len-2] != '/') {
472 lfh=binfile_read_lfh(m->fi, binfile_cd_offset(cd));
473 start=binfile_read_content(m, m->fi, binfile_cd_offset(cd), lfh);
474 dbg(0,"fopen '%s'\n", full);
476 fwrite(start, lfh->zipuncmp, 1, f);
478 file_data_free(m->fi, start);
479 file_data_free(m->fi, (unsigned char *)lfh);
481 file_data_free(m->fi, (unsigned char *)cd);
488 return g_strdup_printf("%s/%s",dir,filename);
492 binfile_attr_get(void *priv_data, enum attr_type attr_type, struct attr *attr)
494 struct map_rect_priv *mr=priv_data;
495 struct tile *t=mr->t;
499 if (attr_type != mr->attr_last) {
500 t->pos_attr=t->pos_attr_start;
501 mr->attr_last=attr_type;
503 while (t->pos_attr < t->pos_next) {
504 size=le32_to_cpu(*(t->pos_attr++));
505 type=le32_to_cpu(t->pos_attr[0]);
506 if (type == attr_label)
508 if (type == attr_house_number)
509 mr->label_attr[0]=t->pos_attr;
510 if (type == attr_street_name)
511 mr->label_attr[1]=t->pos_attr;
512 if (type == attr_street_name_systematic)
513 mr->label_attr[2]=t->pos_attr;
514 if (type == attr_district_name && mr->item.type < type_line)
515 mr->label_attr[3]=t->pos_attr;
516 if (type == attr_town_name && mr->item.type < type_line)
517 mr->label_attr[4]=t->pos_attr;
518 if (type == attr_type || attr_type == attr_any) {
519 if (attr_type == attr_any) {
520 dbg(1,"pos %p attr %s size %d\n", t->pos_attr-1, attr_to_name(type), size);
523 if (ATTR_IS_GROUP(type)) {
525 int *subpos=t->pos_attr+1;
528 while (size_rem > 0 && i < 7) {
529 int subsize=le32_to_cpu(*subpos++);
530 int subtype=le32_to_cpu(subpos[0]);
531 mr->attrs[i].type=subtype;
532 attr_data_set_le(&mr->attrs[i], subpos+1);
537 mr->attrs[i].type=type_none;
538 mr->attrs[i].u.data=NULL;
539 attr->u.attrs=mr->attrs;
541 attr_data_set_le(attr, t->pos_attr+1);
542 if (type == attr_url_local) {
544 mr->url=binfile_extract(mr->m, mr->m->cachedir, attr->u.str, 1);
547 if (type == attr_flags && mr->m->map_version < 1)
548 attr->u.num |= AF_CAR;
556 if (!mr->label && (attr_type == attr_any || attr_type == attr_label)) {
557 for (i = 0 ; i < sizeof(mr->label_attr)/sizeof(int *) ; i++) {
558 if (mr->label_attr[i]) {
560 attr->type=attr_label;
561 attr_data_set_le(attr,mr->label_attr[i]+1);
569 struct binfile_hash_entry {
576 binfile_hash_entry_hash(gconstpointer key)
578 const struct binfile_hash_entry *entry=key;
579 return (entry->id.id_hi ^ entry->id.id_lo);
583 binfile_hash_entry_equal(gconstpointer a, gconstpointer b)
585 const struct binfile_hash_entry *entry1=a,*entry2=b;
586 return (entry1->id.id_hi==entry2->id.id_hi && entry1->id.id_lo == entry2->id.id_lo);
590 binfile_item_dup(struct map_priv *m, struct item *item, struct tile *t, int extend)
592 int size=le32_to_cpu(t->pos[0]);
593 struct binfile_hash_entry *entry=g_malloc(sizeof(struct binfile_hash_entry)+(size+1+extend)*sizeof(int));
594 void *ret=entry->data;
595 entry->id.id_hi=item->id_hi;
596 entry->id.id_lo=item->id_lo;
598 dbg(0,"id 0x%x,0x%x\n",entry->id.id_hi,entry->id.id_lo);
600 memcpy(ret, t->pos, (size+1)*sizeof(int));
602 m->changes=g_hash_table_new_full(binfile_hash_entry_hash, binfile_hash_entry_equal, g_free, NULL);
603 g_hash_table_replace(m->changes, entry, entry);
604 dbg(0,"ret %p\n",ret);
609 binfile_coord_set(void *priv_data, struct coord *c, int count, enum change_mode mode)
611 struct map_rect_priv *mr=priv_data;
612 struct tile *t=mr->t,*tn,new;
613 int i,delta,move_len;
614 int write_offset,move_offset,aoffset,coffset,clen;
619 dbg(0,"Before: pos_coord=%d\n",t->pos_coord-i);
620 while (i < t->pos_next)
621 dbg(0,"%d:0x%x\n",j++,*i++);
624 aoffset=t->pos_attr-t->pos_attr_start;
625 coffset=t->pos_coord-t->pos_coord_start-2;
626 clen=t->pos_attr_start-t->pos_coord+2;
627 dbg(0,"coffset=%d clen=%d\n",coffset,clen);
629 case change_mode_delete:
633 move_offset=coffset+count*2;
634 move_len=t->pos_next-t->pos_coord_start-move_offset;
637 case change_mode_modify:
638 write_offset=coffset;
639 if (count*2 > clen) {
641 move_offset=t->pos_attr_start-t->pos_coord_start;
642 move_len=t->pos_next-t->pos_coord_start-move_offset;
649 case change_mode_prepend:
651 move_offset=coffset-2;
652 move_len=t->pos_next-t->pos_coord_start-move_offset;
653 write_offset=coffset-2;
655 case change_mode_append:
658 move_len=t->pos_next-t->pos_coord_start-move_offset;
659 write_offset=coffset;
664 dbg(0,"delta %d\n",delta);
665 data=binfile_item_dup(mr->m, &mr->item, t, delta > 0 ? delta:0);
666 data[0]=cpu_to_le32(le32_to_cpu(data[0])+delta);
667 data[2]=cpu_to_le32(le32_to_cpu(data[2])+delta);
668 new.pos=new.start=data;
669 new.zipfile_num=t->zipfile_num;
671 push_tile(mr, &new, 0, 0);
674 tn->pos_coord=tn->pos_coord_start+coffset;
675 tn->pos_attr=tn->pos_attr_start+aoffset;
676 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);
677 memmove(tn->pos_coord_start+move_offset+delta, tn->pos_coord_start+move_offset, move_len*4);
680 dbg(0,"After move: pos_coord=%d\n",tn->pos_coord-i);
681 while (i < tn->pos_next)
682 dbg(0,"%d:0x%x\n",j++,*i++);
684 if (mode != change_mode_append)
685 tn->pos_coord+=move_offset;
686 if (mode != change_mode_delete) {
687 dbg(0,"writing %d ints at offset %d\n",count*2,write_offset+tn->pos_coord_start-data);
688 for (i = 0 ; i < count ; i++) {
689 tn->pos_coord_start[write_offset++]=c[i].x;
690 tn->pos_coord_start[write_offset++]=c[i].y;
696 dbg(0,"After: pos_coord=%d\n",tn->pos_coord-i);
697 while (i < tn->pos_next)
698 dbg(0,"%d:0x%x\n",j++,*i++);
704 binfile_attr_set(void *priv_data, struct attr *attr, enum change_mode mode)
706 struct map_rect_priv *mr=priv_data;
707 struct tile *t=mr->t,*tn,new;
708 int extend,offset,delta,move_len;
709 int write_offset,move_offset,naoffset,coffset,oattr_len;
710 int nattr_size,nattr_len,pad;
715 dbg(0,"Before: pos_attr=%d\n",t->pos_attr-i);
716 while (i < t->pos_next)
717 dbg(0,"%d:0x%x\n",j++,*i++);
722 naoffset=t->pos_attr-t->pos_attr_start;
723 coffset=t->pos_coord-t->pos_coord_start;
727 if (mode == change_mode_delete || mode == change_mode_modify) {
728 dbg(0,"no attribute selected\n");
731 if (mode == change_mode_append)
732 naoffset=t->pos_next-t->pos_attr_start;
734 while (offset < naoffset) {
735 oattr_len=le32_to_cpu(t->pos_attr_start[offset])+1;
736 dbg(0,"len %d\n",oattr_len);
740 move_len=t->pos_next-t->pos_attr_start-offset;
743 case change_mode_delete:
749 case change_mode_modify:
750 case change_mode_prepend:
751 case change_mode_append:
752 nattr_size=attr_data_size(attr);
753 pad=(4-(nattr_size%4))%4;
754 nattr_len=(nattr_size+pad)/4+2;
755 if (mode == change_mode_prepend) {
756 move_offset=write_offset;
759 if (mode == change_mode_append) {
760 write_offset=move_offset;
766 if (mode == change_mode_delete || mode == change_mode_modify)
767 delta=nattr_len-oattr_len;
770 dbg(0,"delta %d oattr_len %d nattr_len %d\n",delta,oattr_len, nattr_len);
771 data=binfile_item_dup(mr->m, &mr->item, t, delta > 0 ? delta:0);
772 data[0]=cpu_to_le32(le32_to_cpu(data[0])+delta);
773 new.pos=new.start=data;
774 new.zipfile_num=t->zipfile_num;
776 push_tile(mr, &new, 0, 0);
779 tn->pos_coord=tn->pos_coord_start+coffset;
780 tn->pos_attr=tn->pos_attr_start+offset;
781 dbg(0,"attr start %d offset %d\n",tn->pos_attr_start-data,offset);
782 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);
783 memmove(tn->pos_attr_start+move_offset+delta, tn->pos_attr_start+move_offset, move_len*4);
784 if (mode != change_mode_append)
788 dbg(0,"After move: pos_attr=%d\n",tn->pos_attr-i);
789 while (i < tn->pos_next)
790 dbg(0,"%d:0x%x\n",j++,*i++);
793 int *nattr=tn->pos_attr_start+write_offset;
794 dbg(0,"writing %d ints at %d\n",nattr_len,nattr-data);
795 nattr[0]=cpu_to_le32(nattr_len-1);
796 nattr[1]=cpu_to_le32(attr->type);
797 memcpy(nattr+2, attr_data_get(attr), nattr_size);
798 memset((unsigned char *)(nattr+2)+nattr_size, 0, pad);
802 dbg(0,"After: pos_attr=%d\n",tn->pos_attr-i);
803 while (i < tn->pos_next)
804 dbg(0,"%d:0x%x\n",j++,*i++);
809 static struct item_methods methods_binfile = {
810 binfile_coord_rewind,
820 push_tile(struct map_rect_priv *mr, struct tile *t, int offset, int length)
822 dbg_assert(mr->tile_depth < 8);
823 mr->t=&mr->tiles[mr->tile_depth++];
825 mr->t->pos=mr->t->pos_next=mr->t->start+offset;
827 length=le32_to_cpu(mr->t->pos[0])+1;
829 mr->t->end=mr->t->pos+length;
833 pop_tile(struct map_rect_priv *mr)
835 if (mr->tile_depth <= 1)
838 file_data_free(mr->m->fi, (unsigned char *)(mr->t->start));
841 dbg(0,"leave %d\n",mr->t->zipfile_num);
844 mr->t=&mr->tiles[--mr->tile_depth-1];
850 zipfile_to_tile(struct map_priv *m, struct zip_cd *cd, struct tile *t)
856 dbg(1,"enter %p %p %p\n", m, cd, t);
857 dbg(1,"cd->zipofst=0x%Lx\n", binfile_cd_offset(cd));
861 fi=m->fis[cd->zipdsk];
864 lfh=binfile_read_lfh(fi, binfile_cd_offset(cd));
865 zipfn=(char *)(file_data_read(fi,binfile_cd_offset(cd)+sizeof(struct zip_lfh), lfh->zipfnln));
866 strncpy(buffer, zipfn, lfh->zipfnln);
867 buffer[lfh->zipfnln]='\0';
868 t->start=(int *)binfile_read_content(m, fi, binfile_cd_offset(cd), lfh);
869 t->end=t->start+lfh->zipuncmp/4;
871 dbg(1,"0x%x '%s' %d %d,%d\n", lfh->ziplocsig, buffer, sizeof(*cd)+cd->zipcfnl, lfh->zipsize, lfh->zipuncmp);
872 file_data_free(fi, (unsigned char *)zipfn);
873 file_data_free(fi, (unsigned char *)lfh);
874 return t->start != NULL;
879 map_binfile_handle_redirect(struct map_priv *m)
881 char *location=file_http_header(m->http, "location");
889 dbg(0,"redirected from %s to %s\n",m->url,location);
891 m->url=g_strdup(location);
892 file_destroy(m->http);
899 map_binfile_http_request(struct map_priv *m, struct attr **attrs)
902 m->http=file_create(NULL, attrs);
904 file_request(m->http, attrs);
911 map_binfile_download_size(struct map_priv *m)
913 struct attr url={attr_url};
914 struct attr http_method={attr_http_method};
915 struct attr persistent={attr_persistent};
916 struct attr *attrs[4];
924 attrs[1]=&http_method;
925 http_method.u.str="HEAD";
927 attrs[2]=&persistent;
930 map_binfile_http_request(m, attrs);
931 data=file_data_read_special(m->http, 0, &size_ret);
935 } while (map_binfile_handle_redirect(m));
937 ret=file_size(m->http);
938 dbg(1,"file size "LONGLONG_FMT"\n",ret);
944 map_binfile_http_close(struct map_priv *m)
947 file_destroy(m->http);
955 map_binfile_http_range(struct map_priv *m, long long offset, int size)
957 struct attr *attrs[4];
958 struct attr url={attr_url};
959 struct attr http_header={attr_http_header};
960 struct attr persistent={attr_persistent};
964 attrs[1]=&http_header;
965 attrs[2]=&persistent;
969 http_header.u.str=g_strdup_printf("Range: bytes="LONGLONG_FMT"-"LONGLONG_FMT,offset, offset+size-1);
970 map_binfile_http_request(m, attrs);
971 g_free(http_header.u.str);
975 static unsigned char *
976 map_binfile_download_range(struct map_priv *m, long long offset, int size)
980 struct file *http=map_binfile_http_range(m, offset, size);
982 ret=file_data_read_special(http, size, &size_ret);
983 if (size_ret != size) {
984 dbg(0,"size %d vs %d\n",size,size_ret);
991 static struct zip_cd *
992 download_cd(struct map_download *download)
994 struct map_priv *m=download->m;
995 struct zip64_eoc *zip64_eoc=(struct zip64_eoc *)file_data_read(m->fi, 0, sizeof(*zip64_eoc));
996 struct zip_cd *cd=(struct zip_cd *)map_binfile_download_range(m, zip64_eoc->zip64eofst+download->zipfile*m->cde_size,m->cde_size);
997 file_data_free(m->fi, (unsigned char *)zip64_eoc);
998 dbg(0,"needed cd, result %p\n",cd);
1003 download_request(struct map_download *download)
1005 struct attr url={attr_url};
1006 struct attr http_header={attr_http_header};
1007 struct attr persistent={attr_persistent};
1008 struct attr *attrs[4];
1010 if(!download->m->download_enabled)
1012 dbg(0,"Tried downloading while it's not allowed\n");
1017 attrs[1]=&persistent;
1019 if (strchr(download->m->url,'?')) {
1020 url.u.str=g_strdup_printf("%smemberid=%d",download->m->url,download->zipfile);
1021 download->dl_size=-1;
1023 long long offset=binfile_cd_offset(download->cd_copy);
1024 int size=download->cd_copy->zipcsiz+sizeof(struct zip_lfh)+download->cd_copy->zipcfnl;
1025 url.u.str=g_strdup(download->m->url);
1026 http_header.u.str=g_strdup_printf("Range: bytes="LONGLONG_FMT"-"LONGLONG_FMT,offset,offset+size-1);
1027 attrs[2]=&http_header;
1029 download->dl_size=size;
1031 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);
1032 map_binfile_http_request(download->m, attrs);
1034 download->http=download->m->http;
1040 download_start(struct map_download *download)
1043 struct zip_eoc *eoc;
1045 if (!download->cd->zipcensig) {
1046 download->cd_copy=download_cd(download);
1048 download->cd_copy=g_malloc(download->m->cde_size);
1049 memcpy(download->cd_copy, download->cd, download->m->cde_size);
1051 file_data_remove(download->file, (unsigned char *)download->cd);
1053 offset=file_size(download->file);
1054 offset-=sizeof(struct zip_eoc);
1055 eoc=(struct zip_eoc *)file_data_read(download->file, offset, sizeof(struct zip_eoc));
1056 download->zip_eoc=g_malloc(sizeof(struct zip_eoc));
1057 memcpy(download->zip_eoc, eoc, sizeof(struct zip_eoc));
1058 file_data_remove(download->file, (unsigned char *)eoc);
1059 download->start_offset=download->offset=offset;
1060 return download_request(download);
1064 download_download(struct map_download *download)
1066 int size=64*1024,size_ret;
1067 unsigned char *data;
1068 if (download->dl_size != -1 && size > download->dl_size)
1069 size=download->dl_size;
1072 data=file_data_read_special(download->http, size, &size_ret);
1073 if (!download->read && download->m->http && map_binfile_handle_redirect(download->m)) {
1075 download_request(download);
1079 dbg(1,"got %d bytes writing at offset "LONGLONG_FMT"\n",size_ret,download->offset);
1080 if (size_ret <= 0) {
1084 file_data_write(download->file, download->offset, size_ret, data);
1085 download->offset+=size_ret;
1086 download->read+=size_ret;
1087 download->dl_size-=size_ret;
1088 if (download->dl_size != -1)
1089 download->progress=download->read*100/(download->read+download->dl_size);
1094 download_finish(struct map_download *download)
1096 struct zip_lfh *lfh;
1098 struct zip_cd_ext *ext;
1099 long long lfh_offset;
1100 file_data_write(download->file, download->offset, sizeof(struct zip_eoc), (void *)download->zip_eoc);
1101 lfh=(struct zip_lfh *)(file_data_read(download->file,download->start_offset, sizeof(struct zip_lfh)));
1102 ext=binfile_cd_ext(download->cd_copy);
1104 ext->zipofst=download->start_offset;
1106 download->cd_copy->zipofst=download->start_offset;
1107 download->cd_copy->zipcsiz=lfh->zipsize;
1108 download->cd_copy->zipcunc=lfh->zipuncmp;
1109 download->cd_copy->zipccrc=lfh->zipcrc;
1110 lfh_offset = binfile_cd_offset(download->cd_copy)+sizeof(struct zip_lfh);
1111 lfh_filename=(char *)file_data_read(download->file,lfh_offset,lfh->zipfnln);
1112 memcpy(download->cd_copy+1,lfh_filename,lfh->zipfnln);
1113 file_data_remove(download->file,(void *)lfh_filename);
1114 file_data_remove(download->file,(void *)lfh);
1115 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);
1116 file_data_flush(download->file, download->m->eoc->zipeofst + download->zipfile*download->m->cde_size, sizeof(struct zip_cd));
1118 g_free(download->cd_copy);
1119 download->cd=(struct zip_cd *)(file_data_read(download->file, download->m->eoc->zipeofst + download->zipfile*download->m->cde_size, download->m->cde_size));
1120 cd_to_cpu(download->cd);
1121 dbg(1,"Offset %d\n",download->cd->zipofst);
1126 download_planet_size(struct map_download *download)
1128 download->size=map_binfile_download_size(download->m);
1129 dbg(0,"Planet size "LONGLONG_FMT"\n",download->size);
1130 if (!download->size)
1136 download_eoc(struct map_download *download)
1138 download->zip64_eoc=(struct zip64_eoc *)map_binfile_download_range(download->m, download->size-98, 98);
1139 if (!download->zip64_eoc)
1141 download->zip64_eocl=(struct zip64_eocl *)(download->zip64_eoc+1);
1142 download->zip_eoc=(struct zip_eoc *)(download->zip64_eocl+1);
1143 if (download->zip64_eoc->zip64esig != zip64_eoc_sig || download->zip64_eocl->zip64lsig != zip64_eocl_sig || download->zip_eoc->zipesig != zip_eoc_sig)
1145 dbg(0,"wrong signature on zip64_eoc downloaded from "LONGLONG_FMT"\n",download->size-98);
1146 g_free(download->zip64_eoc);
1153 download_directory_start(struct map_download *download)
1155 download->http=map_binfile_http_range(download->m, download->zip64_eoc->zip64eofst, download->zip64_eoc->zip64ecsz);
1156 if (!download->http)
1162 download_directory_do(struct map_download *download)
1166 for (count = 0 ; count < 100 ; count++) {
1167 int cd_xlen, size_ret;
1168 unsigned char *cd_data;
1170 cd=(struct zip_cd *)file_data_read_special(download->http, sizeof(*cd), &size_ret);
1172 dbg(1,"size_ret=%d\n",size_ret);
1175 if (size_ret != sizeof(*cd) || cd->zipcensig != zip_cd_sig) {
1176 dbg(0,"error1 size=%d vs %d\n",size_ret, sizeof(*cd));
1179 file_data_write(download->file, download->offset, sizeof(*cd), (unsigned char *)cd);
1180 download->offset+=sizeof(*cd);
1181 cd_xlen=cd->zipcfnl+cd->zipcxtl;
1182 cd_data=file_data_read_special(download->http, cd_xlen, &size_ret);
1183 if (size_ret != cd_xlen) {
1184 dbg(0,"error2 size=%d vs %d\n",size_ret,cd_xlen);
1187 file_data_write(download->file, download->offset, cd_xlen, cd_data);
1188 download->offset+=cd_xlen;
1196 download_directory_finish(struct map_download *download)
1198 download->http=NULL;
1203 download_initial_finish(struct map_download *download)
1205 download->zip64_eoc->zip64eofst=download->cd1offset;
1206 download->zip64_eocl->zip64lofst=download->offset;
1207 download->zip_eoc->zipeofst=download->cd1offset;
1209 file_data_write(download->file, download->offset, sizeof(*download->zip64_eoc), (unsigned char *)download->zip64_eoc);
1210 download->offset+=sizeof(*download->zip64_eoc);
1211 file_data_write(download->file, download->offset, sizeof(*download->zip64_eocl), (unsigned char *)download->zip64_eocl);
1212 download->offset+=sizeof(*download->zip64_eocl);
1214 file_data_write(download->file, download->offset, sizeof(*download->zip_eoc), (unsigned char *)download->zip_eoc);
1215 download->offset+=sizeof(*download->zip_eoc);
1216 g_free(download->zip64_eoc);
1217 download->zip64_eoc=NULL;
1222 push_zipfile_tile_do(struct map_rect_priv *mr, struct zip_cd *cd, int zipfile, int offset, int length)
1226 struct map_priv *m=mr->m;
1227 struct file *f=m->fi;
1229 dbg(1,"enter %p %d\n", mr, zipfile);
1233 char filename[cd->zipcfnl+1];
1234 memcpy(filename, cd+1, cd->zipcfnl);
1235 filename[cd->zipcfnl]='\0';
1236 dbg(0,"enter %d (%s) %d\n",zipfile, filename, cd->zipcunc);
1239 mr->size+=cd->zipcunc;
1241 t.zipfile_num=zipfile;
1242 if (zipfile_to_tile(m, cd, &t))
1243 push_tile(mr, &t, offset, length);
1244 file_data_free(f, (unsigned char *)cd);
1248 static struct zip_cd *
1249 download(struct map_priv *m, struct map_rect_priv *mr, struct zip_cd *cd, int zipfile, int offset, int length, int async)
1251 struct map_download *download;
1253 if(!m->download_enabled)
1257 download=m->download;
1259 download=g_new0(struct map_download, 1);
1263 download->file=m->fi;
1265 download->zipfile=zipfile;
1266 download->toffset=offset;
1267 download->tlength=length;
1270 struct attr readwrite={attr_readwrite,{(void *)1}};
1271 struct attr create={attr_create,{(void *)1}};
1272 struct attr *attrs[3];
1273 attrs[0]=&readwrite;
1276 download->file=file_create(m->filename,attrs);
1282 m->download=download;
1283 g_free(m->progress);
1285 m->progress=g_strdup_printf("Download Tile %d 0%%",download->zipfile);
1287 m->progress=g_strdup_printf("Download Map Information 0%%");
1288 callback_list_call_attr_0(m->cbl, attr_progress);
1292 dbg(0,"state=%d\n",download->state);
1293 switch (download->state) {
1298 if (download_start(download))
1304 if (download_download(download))
1307 g_free(m->progress);
1308 m->progress=g_strdup_printf("Download Tile %d %d%%",download->zipfile,download->progress);
1309 callback_list_call_attr_0(m->cbl, attr_progress);
1313 if (download_finish(download)) {
1315 g_free(m->progress);
1316 m->progress=g_strdup_printf("Download Tile %d 100%%",download->zipfile);
1317 callback_list_call_attr_0(m->cbl, attr_progress);
1319 push_zipfile_tile_do(download->mr, download->cd, download->zipfile, download->toffset, download->tlength);
1323 g_free(m->progress);
1333 if (download_planet_size(download))
1339 g_free(m->progress);
1340 m->progress=g_strdup_printf("Download Map Information 50%%");
1341 callback_list_call_attr_0(m->cbl, attr_progress);
1342 if (download_eoc(download))
1345 dbg(0,"download of eoc failed\n");
1350 g_free(m->progress);
1351 m->progress=g_strdup_printf("Download Map Information 100%%");
1352 callback_list_call_attr_0(m->cbl, attr_progress);
1353 if (download_directory_start(download))
1359 g_free(m->progress);
1360 m->progress=g_strdup_printf("Download Map Directory %d%%",(int)(download->offset*100/download->zip64_eoc->zip64ecsz));
1361 callback_list_call_attr_0(m->cbl, attr_progress);
1362 if (!download_directory_do(download))
1366 if (download_directory_finish(download))
1372 download_initial_finish(download);
1373 m->fi=download->file;
1374 g_free(m->progress);
1379 map_binfile_open(m);
1388 push_zipfile_tile(struct map_rect_priv *mr, int zipfile, int offset, int length, int async)
1390 struct map_priv *m=mr->m;
1391 struct file *f=m->fi;
1392 long long cdoffset=m->eoc64?m->eoc64->zip64eofst:m->eoc->zipeofst;
1393 struct zip_cd *cd=(struct zip_cd *)(file_data_read(f, cdoffset + zipfile*m->cde_size, m->cde_size));
1394 dbg(1,"read from "LONGLONG_FMT" %d bytes\n",cdoffset + zipfile*m->cde_size, m->cde_size);
1396 if (!cd->zipcunc && m->url) {
1397 cd=download(m, mr, cd, zipfile, offset, length, async);
1401 push_zipfile_tile_do(mr, cd, zipfile, offset, length);
1405 static struct map_rect_priv *
1406 map_rect_new_binfile_int(struct map_priv *map, struct map_selection *sel)
1408 struct map_rect_priv *mr;
1410 binfile_check_version(map);
1411 dbg(1,"map_rect_new_binfile\n");
1412 if (!map->fi && !map->url)
1414 map_binfile_http_close(map);
1415 mr=g_new0(struct map_rect_priv, 1);
1420 mr->item.meth=&methods_binfile;
1421 mr->item.priv_data=mr;
1426 tile_bbox(char *tile, int len, struct coord_rect *r)
1431 struct coord_rect world_bbox = {
1432 { -20000000, 20000000}, /* lu */
1433 { 20000000, -20000000}, /* rl */
1437 c.x=(r->lu.x+r->rl.x)/2;
1438 c.y=(r->lu.y+r->rl.y)/2;
1439 xo=(r->rl.x-r->lu.x)*overlap/100;
1440 yo=(r->lu.y-r->rl.y)*overlap/100;
1467 map_download_selection_check(struct zip_cd *cd, struct map_selection *sel)
1469 struct coord_rect cd_rect;
1472 tile_bbox((char *)(cd+1), cd->zipcfnl, &cd_rect);
1474 if (coord_rect_overlap(&cd_rect, &sel->u.c_rect))
1482 map_download_selection(struct map_priv *m, struct map_rect_priv *mr, struct map_selection *sel)
1486 for (i = 0 ; i < m->zip_members ; i++) {
1487 cd=binfile_read_cd(m, m->cde_size*i, -1);
1488 if (map_download_selection_check(cd, sel))
1489 download(m, mr, cd, i, 0, 0, 0);
1490 file_data_free(m->fi, (unsigned char *)cd);
1494 static struct map_rect_priv *
1495 map_rect_new_binfile(struct map_priv *map, struct map_selection *sel)
1497 struct map_rect_priv *mr=map_rect_new_binfile_int(map, sel);
1499 dbg(1,"zip_members=%d\n", map->zip_members);
1500 if (map->url && map->fi && sel && sel->order == 255) {
1501 map_download_selection(map, mr, sel);
1508 d=file_data_read(map->fi, 0, map->fi->size);
1510 t.end=(int *)(d+map->fi->size);
1514 push_tile(mr, &t, 0, 0);
1515 } else if (map->url && !map->download) {
1516 download(map, NULL, NULL, 0, 0, 0, 1);
1524 write_changes_do(gpointer key, gpointer value, gpointer user_data)
1526 struct binfile_hash_entry *entry=key;
1527 FILE *out=user_data;
1530 fwrite(entry, sizeof(*entry)+(le32_to_cpu(entry->data[0])+1)*4, 1, out);
1536 write_changes(struct map_priv *m)
1542 changes_file=g_strdup_printf("%s.log",m->filename);
1543 changes=fopen(changes_file,"ab");
1544 g_hash_table_foreach(m->changes, write_changes_do, changes);
1546 g_free(changes_file);
1550 load_changes(struct map_priv *m)
1554 struct binfile_hash_entry entry,*e;
1556 changes_file=g_strdup_printf("%s.log",m->filename);
1557 changes=fopen(changes_file,"rb");
1559 g_free(changes_file);
1562 m->changes=g_hash_table_new_full(binfile_hash_entry_hash, binfile_hash_entry_equal, g_free, NULL);
1563 while (fread(&entry, sizeof(entry), 1, changes) == 1) {
1564 if (fread(&size, sizeof(size), 1, changes) != 1)
1566 e=g_malloc(sizeof(struct binfile_hash_entry)+(le32_to_cpu(size)+1)*4);
1569 if (fread(e->data+1, le32_to_cpu(size)*4, 1, changes) != 1)
1571 g_hash_table_replace(m->changes, e, e);
1574 g_free(changes_file);
1579 map_rect_destroy_binfile(struct map_rect_priv *mr)
1581 write_changes(mr->m);
1582 while (pop_tile(mr));
1584 dbg(0,"size=%d kb\n",mr->size/1024);
1586 if (mr->tiles[0].fi && mr->tiles[0].start)
1587 file_data_free(mr->tiles[0].fi, (unsigned char *)(mr->tiles[0].start));
1589 map_binfile_http_close(mr->m);
1594 setup_pos(struct map_rect_priv *mr)
1596 int size,coord_size;
1597 struct tile *t=mr->t;
1598 size=le32_to_cpu(t->pos[0]);
1599 if (size > 1024*1024 || size < 0) {
1600 dbg(0,"size=0x%x\n", size);
1602 fprintf(stderr,"offset=%d\n", (unsigned char *)(mr->pos)-mr->m->f->begin);
1604 dbg(0,"size error");
1606 t->pos_next=t->pos+size+1;
1607 mr->item.type=le32_to_cpu(t->pos[1]);
1608 coord_size=le32_to_cpu(t->pos[2]);
1609 t->pos_coord_start=t->pos+3;
1610 t->pos_attr_start=t->pos_coord_start+coord_size;
1614 selection_contains(struct map_selection *sel, struct coord_rect *r, struct range *mima)
1620 if (coord_rect_overlap(r, &sel->u.c_rect)) {
1622 dbg(1,"min %d max %d order %d\n", mima->min, mima->max, order);
1623 if (!mima->min && !mima->max)
1625 if (order >= mima->min && order <= mima->max)
1634 map_parse_country_binfile(struct map_rect_priv *mr)
1637 if (binfile_attr_get(mr->item.priv_data, attr_country_id, &at)) {
1638 if (at.u.num == mr->country_id)
1640 if (binfile_attr_get(mr->item.priv_data, attr_zipfile_ref, &at))
1642 push_zipfile_tile(mr, at.u.num, 0, 0, 0);
1649 map_parse_submap(struct map_rect_priv *mr, int async)
1651 struct coord_rect r;
1655 if (binfile_coord_get(mr->item.priv_data, c, 2) != 2)
1661 if (!binfile_attr_get(mr->item.priv_data, attr_order, &at))
1663 #if __BYTE_ORDER == __BIG_ENDIAN
1664 mima.min=le16_to_cpu(at.u.range.max);
1665 mima.max=le16_to_cpu(at.u.range.min);
1669 if (!mr->m->eoc || !selection_contains(mr->sel, &r, &mima))
1671 if (!binfile_attr_get(mr->item.priv_data, attr_zipfile_ref, &at))
1673 dbg(1,"pushing zipfile %d from %d\n", at.u.num, mr->t->zipfile_num);
1674 return push_zipfile_tile(mr, at.u.num, 0, 0, async);
1678 push_modified_item(struct map_rect_priv *mr)
1681 struct binfile_hash_entry *entry;
1682 id.id_hi=mr->item.id_hi;
1683 id.id_lo=mr->item.id_lo;
1684 entry=g_hash_table_lookup(mr->m->changes, &id);
1687 tn.pos_next=tn.pos=tn.start=entry->data;
1688 tn.zipfile_num=mr->item.id_hi;
1690 tn.end=tn.start+le32_to_cpu(entry->data[0])+1;
1691 push_tile(mr, &tn, 0, 0);
1697 static struct item *
1698 map_rect_get_item_binfile(struct map_rect_priv *mr)
1701 struct map_priv *m=mr->m;
1703 download(m, NULL, NULL, 0, 0, 0, 2);
1706 if (mr->status == 1) {
1708 if (push_zipfile_tile(mr, m->zip_members-1, 0, 0, 1))
1716 if (t->pos >= t->end) {
1722 binfile_coord_rewind(mr);
1723 binfile_attr_rewind(mr);
1724 if ((mr->item.type == type_submap) && (!mr->country_id)) {
1725 if (map_parse_submap(mr, 1))
1730 mr->item.id_hi=t->zipfile_num;
1731 mr->item.id_lo=t->pos-t->start;
1732 if (mr->m->changes && push_modified_item(mr))
1737 if (mr->item.type == type_countryindex) {
1738 map_parse_country_binfile(mr);
1740 if (item_is_town(mr->item))
1751 static struct item *
1752 map_rect_get_item_byid_binfile(struct map_rect_priv *mr, int id_hi, int id_lo)
1756 while (pop_tile(mr));
1757 push_zipfile_tile(mr, id_hi, 0, 0, 0);
1760 t->pos=t->start+id_lo;
1761 mr->item.id_hi=id_hi;
1762 mr->item.id_lo=id_lo;
1764 push_modified_item(mr);
1766 binfile_coord_rewind(mr);
1767 binfile_attr_rewind(mr);
1772 binmap_search_by_index(struct map_priv *map, struct item *item, struct map_rect_priv **ret)
1774 struct attr zipfile_ref;
1781 if (item_attr_get(item, attr_item_id, &zipfile_ref)) {
1782 data=zipfile_ref.u.data;
1783 *ret=map_rect_new_binfile_int(map, NULL);
1784 push_zipfile_tile(*ret, le32_to_cpu(data[0]), le32_to_cpu(data[1]), -1, 0);
1787 if (item_attr_get(item, attr_zipfile_ref, &zipfile_ref)) {
1788 *ret=map_rect_new_binfile_int(map, NULL);
1789 push_zipfile_tile(*ret, zipfile_ref.u.num, 0, 0, 0);
1792 if (item_attr_get(item, attr_zipfile_ref_block, &zipfile_ref)) {
1793 data=zipfile_ref.u.data;
1794 *ret=map_rect_new_binfile_int(map, NULL);
1795 push_zipfile_tile(*ret, le32_to_cpu(data[0]), le32_to_cpu(data[1]), le32_to_cpu(data[2]), 0);
1802 static struct map_rect_priv *
1803 binmap_search_street_by_place(struct map_priv *map, struct item *town, struct coord *c, struct map_selection *sel)
1805 struct attr town_name, poly_town_name;
1806 struct map_rect_priv *map_rec2;
1810 if (!item_attr_get(town, attr_label, &town_name))
1812 sel->range = item_range_all;
1815 sel->u.c_rect.lu=*c;
1816 sel->u.c_rect.rl=*c;
1817 map_rec2=map_rect_new_binfile(map, sel);
1818 while ((place=map_rect_get_item_binfile(map_rec2))) {
1819 if (item_is_poly_place(*place) &&
1820 item_attr_get(place, attr_label, &poly_town_name) &&
1821 !strcmp(poly_town_name.u.str,town_name.u.str)) {
1822 struct coord c[128];
1825 while ((count=item_coord_get(place, c, 128))) {
1826 for (i = 0 ; i < count ; i++)
1827 coord_rect_extend(&sel->u.c_rect, &c[i]);
1831 map_rect_destroy_binfile(map_rec2);
1833 return map_rect_new_binfile(map, sel);
1837 static struct map_rect_priv *
1838 binmap_search_street_by_estimate(struct map_priv *map, struct item *town, struct coord *c, struct map_selection *sel)
1841 switch (town->type) {
1842 case type_town_label_1e7:
1843 case type_town_label_5e6:
1844 case type_town_label_2e6:
1845 case type_town_label_1e6:
1846 case type_town_label_5e5:
1847 case type_town_label_2e5:
1850 case type_town_label_1e5:
1851 case type_town_label_5e4:
1852 case type_town_label_2e4:
1855 case type_town_label_1e4:
1856 case type_town_label_5e3:
1857 case type_town_label_2e3:
1860 case type_town_label_1e3:
1861 case type_town_label_5e2:
1862 case type_town_label_2e2:
1863 case type_town_label_1e2:
1864 case type_town_label_5e1:
1865 case type_town_label_2e1:
1866 case type_town_label_1e1:
1867 case type_town_label_5e0:
1868 case type_town_label_2e0:
1869 case type_town_label_1e0:
1870 case type_town_label_0e0:
1876 sel->u.c_rect.lu.x = c->x-size;
1877 sel->u.c_rect.lu.y = c->y+size;
1878 sel->u.c_rect.rl.x = c->x+size;
1879 sel->u.c_rect.rl.y = c->y-size;
1880 return map_rect_new_binfile(map, sel);
1883 static struct map_rect_priv *
1884 binmap_search_housenumber_by_estimate(struct map_priv *map, struct coord *c, struct map_selection *sel)
1887 sel->u.c_rect.lu.x = c->x-size;
1888 sel->u.c_rect.lu.y = c->y+size;
1889 sel->u.c_rect.rl.x = c->x+size;
1890 sel->u.c_rect.rl.y = c->y-size;
1892 sel->range = item_range_all;
1896 return map_rect_new_binfile(map, sel);
1899 static struct map_search_priv *
1900 binmap_search_new(struct map_priv *map, struct item *item, struct attr *search, int partial)
1902 struct map_rect_priv *map_rec;
1903 struct map_search_priv *msp=g_new0(struct map_search_priv, 1);
1906 msp->search = search;
1907 msp->partial = partial;
1909 * NOTE: If you implement search for other attributes than attr_town_name and attr_street_name,
1910 * please update this comment and the documentation for map_search_new() in map.c
1912 switch (search->type) {
1913 case attr_country_name:
1915 case attr_town_name:
1916 case attr_town_or_district_name:
1917 map_rec = map_rect_new_binfile(map, NULL);
1920 map_rec->country_id = item->id_lo;
1924 case attr_town_postal:
1926 case attr_street_name:
1929 if (!map_priv_is(item->map, map))
1931 map_rec = map_rect_new_binfile(map, NULL);
1932 town = map_rect_get_item_byid_binfile(map_rec, item->id_hi, item->id_lo);
1936 if (binmap_search_by_index(map, town, &msp->mr))
1939 if (item_coord_get(town, &c, 1)) {
1940 if ((msp->mr=binmap_search_street_by_place(map, town, &c, &msp->ms)))
1943 msp->mr=binmap_search_street_by_estimate(map, town, &c, &msp->ms);
1948 map_rect_destroy_binfile(map_rec);
1953 map_rect_destroy_binfile(map_rec);
1955 case attr_house_number:
1956 dbg(1,"case house_number");
1959 if (!map_priv_is(item->map, map))
1962 msp->mr_item = map_rect_new_binfile(map, NULL);
1963 msp->item = map_rect_get_item_byid_binfile(msp->mr_item, item->id_hi, item->id_lo);
1964 if (binmap_search_by_index(map, msp->item, &msp->mr) != 3) {
1966 if (item_coord_get(msp->item, &c, 1))
1968 msp->mr=binmap_search_housenumber_by_estimate(map, &c, &msp->ms);
1971 map_rect_destroy_binfile(msp->mr_item);
1987 ascii_cmp(char *name, char *match, int partial)
1990 return g_ascii_strncasecmp(name, match, strlen(match));
1992 return g_ascii_strcasecmp(name, match);
2002 duplicate_hash(gconstpointer key)
2004 const struct duplicate *d=key;
2005 return d->c.x^d->c.y^g_str_hash(d->str);
2009 duplicate_equal(gconstpointer a, gconstpointer b)
2011 const struct duplicate *da=a;
2012 const struct duplicate *db=b;
2013 return (da->c.x == db->c.x && da->c.y == da->c.y && g_str_equal(da->str,db->str));
2017 duplicate(struct map_search_priv *msp, struct item *item, enum attr_type attr_type)
2020 if (!msp->search_results)
2021 msp->search_results = g_hash_table_new_full(duplicate_hash, duplicate_equal, g_free, NULL);
2022 binfile_attr_rewind(item->priv_data);
2023 if (!item_attr_get(item, attr_type, &attr))
2026 int len=sizeof(struct coord)+strlen(attr.u.str)+1;
2027 char *buffer=g_alloca(sizeof(char)*len);
2028 struct duplicate *d=(struct duplicate *)buffer;
2029 if (!item_coord_get(item, &d->c, 1)) {
2033 binfile_coord_rewind(item->priv_data);
2034 strcpy(d->str, attr.u.str);
2035 if (!g_hash_table_lookup(msp->search_results, d)) {
2036 struct duplicate *dc=g_malloc(len);
2038 g_hash_table_insert(msp->search_results, dc, GINT_TO_POINTER(1));
2039 binfile_attr_rewind(item->priv_data);
2046 static struct item *
2047 binmap_search_get_item(struct map_search_priv *map_search)
2053 while ((it = map_rect_get_item_binfile(map_search->mr))) {
2054 switch (map_search->search->type) {
2055 case attr_town_name:
2056 case attr_district_name:
2057 case attr_town_or_district_name:
2058 if (map_search->mr->tile_depth > 1 && item_is_town(*it) && !item_is_district(*it) && map_search->search->type != attr_district_name) {
2059 if (binfile_attr_get(it->priv_data, attr_town_name_match, &at) || binfile_attr_get(it->priv_data, attr_town_name, &at)) {
2060 if (!ascii_cmp(at.u.str, map_search->search->u.str, map_search->partial) && !duplicate(map_search, it, attr_town_name))
2064 if (map_search->mr->tile_depth > 1 && item_is_district(*it) && map_search->search->type != attr_town_name) {
2065 if (binfile_attr_get(it->priv_data, attr_district_name_match, &at) || binfile_attr_get(it->priv_data, attr_district_name, &at)) {
2066 if (!ascii_cmp(at.u.str, map_search->search->u.str, map_search->partial) && !duplicate(map_search, it, attr_town_name))
2071 case attr_street_name:
2072 if (map_search->mode == 1) {
2073 if (binfile_attr_get(it->priv_data, attr_street_name_match, &at) || binfile_attr_get(it->priv_data, attr_street_name, &at)) {
2074 if (!ascii_cmp(at.u.str, map_search->search->u.str, map_search->partial) && !duplicate(map_search, it, attr_street_name)) {
2080 if (item_is_street(*it)) {
2082 if (map_selection_contains_item_rect(map_search->mr->sel, it) && binfile_attr_get(it->priv_data, attr_label, &at)) {
2084 char *str=g_strdup(at.u.str);
2087 for (i = 0 ; i < 3 ; i++) {
2088 char *name=linguistics_expand_special(word,i);
2089 if (name && !ascii_cmp(name, map_search->search->u.str, map_search->partial))
2097 word=linguistics_next_word(word);
2100 if (match && !duplicate(map_search, it, attr_label)) {
2101 item_coord_rewind(it);
2107 case attr_house_number:
2108 //if (it->type == type_house_number)
2109 if ((it->type == type_house_number)||(type_house_number_interpolation_even)
2110 ||(type_house_number_interpolation_odd)
2111 ||(type_house_number_interpolation_all)
2114 // is it a housenumber?
2115 if (binfile_attr_get(it->priv_data, attr_house_number, &at))
2117 // match housenumber to our string
2118 if (!ascii_cmp(at.u.str, map_search->search->u.str, map_search->partial))
2120 //binfile_attr_get(it->priv_data, attr_street_name, &at);
2121 //dbg(0,"hnnn B1 street_name=%s",at.u.str);
2122 if (!duplicate(map_search, it, attr_house_number))
2124 binfile_attr_rewind(it->priv_data);
2136 if (!map_search->mr_item)
2138 map_rect_destroy_binfile(map_search->mr);
2139 if (!binmap_search_by_index(map_search->map, map_search->item, &map_search->mr))
2146 binmap_search_destroy(struct map_search_priv *ms)
2148 if (ms->search_results)
2149 g_hash_table_destroy(ms->search_results);
2151 map_rect_destroy_binfile(ms->mr_item);
2153 map_rect_destroy_binfile(ms->mr);
2158 binmap_get_attr(struct map_priv *m, enum attr_type type, struct attr *attr)
2162 case attr_map_release:
2163 if (m->map_release) {
2164 attr->u.str=m->map_release;
2170 attr->u.str=m->progress;
2180 binmap_set_attr(struct map_priv *map, struct attr *attr)
2182 switch (attr->type) {
2184 map->download_enabled = attr->u.num;
2192 static struct map_methods map_methods_binfile = {
2195 map_destroy_binfile,
2196 map_rect_new_binfile,
2197 map_rect_destroy_binfile,
2198 map_rect_get_item_binfile,
2199 map_rect_get_item_byid_binfile,
2201 binmap_search_destroy,
2202 binmap_search_get_item,
2209 binfile_get_index(struct map_priv *m)
2216 len = strlen("index");
2217 cde_index_size = sizeof(struct zip_cd)+len;
2219 offset = m->eoc64->zip64ecsz-cde_index_size;
2221 offset = m->eoc->zipecsz-cde_index_size;
2222 cd = binfile_read_cd(m, offset, len);
2225 cde_index_size+=sizeof(struct zip_cd_ext);
2227 offset = m->eoc64->zip64ecsz-cde_index_size;
2229 offset = m->eoc->zipecsz-cde_index_size;
2230 cd = binfile_read_cd(m, offset, len+sizeof(struct zip_cd_ext));
2233 if (cd->zipcfnl == len && !strncmp(cd->zipcfn, "index", len)) {
2234 m->index_offset=offset;
2239 offset=binfile_search_cd(m, 0, "index", 0, 0);
2242 cd=binfile_read_cd(m, offset, -1);
2245 m->index_offset=offset;
2251 map_binfile_zip_setup(struct map_priv *m, char *filename, int mmap)
2253 struct zip_cd *first_cd;
2255 if (!(m->eoc=binfile_read_eoc(m->fi)))
2257 dbg_assert(m->eoc->zipedsk == m->eoc->zipecen);
2258 if (m->eoc->zipedsk && strlen(filename) > 3) {
2259 char *tmpfilename=g_strdup(filename),*ext=tmpfilename+strlen(tmpfilename)-3;
2260 m->fis=g_new(struct file *,m->eoc->zipedsk);
2261 for (i = 0 ; i < m->eoc->zipedsk-1 ; i++) {
2262 sprintf(ext,"b%02d",i+1);
2263 m->fis[i]=file_create(tmpfilename, 0);
2265 file_mmap(m->fis[i]);
2267 m->fis[m->eoc->zipedsk-1]=m->fi;
2268 g_free(tmpfilename);
2270 dbg(1,"num_disk %d\n",m->eoc->zipedsk);
2271 m->eoc64=binfile_read_eoc64(m->fi);
2272 if (!binfile_get_index(m))
2274 if (!(first_cd=binfile_read_cd(m, 0, 0)))
2276 m->cde_size=sizeof(struct zip_cd)+first_cd->zipcfnl+first_cd->zipcxtl;
2277 m->zip_members=m->index_offset/m->cde_size+1;
2278 dbg(1,"cde_size %d\n", m->cde_size);
2279 dbg(1,"members %d\n",m->zip_members);
2280 file_data_free(m->fi, (unsigned char *)first_cd);
2289 map_binfile_download_initial(struct map_priv *m)
2291 struct attr readwrite={attr_readwrite,{(void *)1}};
2292 struct attr create={attr_create,{(void *)1}};
2293 struct attr *attrs[4];
2295 long long woffset=0,planet_size;
2297 int cd1size,cdisize;
2298 long long cd1offset,cdioffset;
2299 struct zip64_eoc *zip64_eoc;
2300 struct zip64_eocl *zip64_eocl;
2301 struct zip_eoc *zip_eoc;
2302 struct zip_cd *cd1,*cdn,*cdi;
2303 int count,chunk,cdoffset=0;
2305 struct map_download *download=g_new0(struct map_download, 1);
2307 attrs[0]=&readwrite;
2310 download->file=file_create(m->filename,attrs);
2312 download_planet_size(download);
2313 download_eoc(download);
2314 download_directory_start(download);
2315 while (download_directory_do(download));
2316 download_directory_finish(download);
2317 download_initial_finish(download);
2318 m->fi=download->file;
2323 cd1size=sizeof(*cd1);
2324 cd1offset=zip64_eoc->zip64eofst;
2325 cd1=(struct zip_cd *)map_binfile_download_range(m, cd1offset, cd1size);
2328 cd1size=sizeof(*cd1)+binfile_cd_extra(cd1);
2330 cd1=(struct zip_cd *)map_binfile_download_range(m, cd1offset, cd1size);
2334 cdisize=sizeof(*cdi)+strlen("index")+cd1->zipcxtl;
2335 cdioffset=zip64_eoc->zip64eofst+zip64_eoc->zip64ecsz-cdisize;
2336 cdi=(struct zip_cd *)map_binfile_download_range(m, cdioffset, cdisize);
2342 cdn=g_malloc0(cd1size*256);
2344 file_data_write(out, woffset, sizeof(*zip64_eoc), (unsigned char *)zip64_eoc);
2345 woffset+=sizeof(*zip64_eoc);
2348 file_data_write(out, woffset, cd1size, (unsigned char *)cd1);
2350 count=(cdioffset-cd1offset)/cd1size-1;
2356 file_data_write(out, woffset, cd1size*chunk, (unsigned char *)cdn);
2357 woffset+=cd1size*chunk;
2362 file_data_write(out, woffset, cdisize, (unsigned char *)cdi);
2369 map_binfile_open(struct map_priv *m)
2372 struct map_rect_priv *mr;
2375 struct attr readwrite={attr_readwrite, {(void *)1}};
2376 struct attr *attrs[]={&readwrite, NULL};
2378 dbg(1,"file_create %s\n", m->filename);
2379 m->fi=file_create(m->filename, m->url?attrs:NULL);
2380 if (! m->fi && m->url)
2383 dbg(0,"Failed to load '%s'\n", m->filename);
2386 if (m->check_version)
2387 m->version=file_version(m->fi, m->check_version);
2388 magic=(int *)file_data_read(m->fi, 0, 4);
2390 file_destroy(m->fi);
2394 *magic = le32_to_cpu(*magic);
2395 if (*magic == zip_lfh_sig || *magic == zip_split_sig || *magic == zip_cd_sig || *magic == zip64_eoc_sig) {
2396 if (!map_binfile_zip_setup(m, m->filename, m->flags & 1)) {
2397 dbg(0,"invalid file format for '%s'\n", m->filename);
2398 file_destroy(m->fi);
2402 } else if (*magic == zip_lfh_sig_rev || *magic == zip_split_sig_rev || *magic == zip_cd_sig_rev || *magic == zip64_eoc_sig_rev) {
2403 dbg(0,"endianness mismatch\n");
2404 file_destroy(m->fi);
2409 file_data_free(m->fi, (unsigned char *)magic);
2410 m->cachedir=g_strdup("/tmp/navit");
2412 mr=map_rect_new_binfile(m, NULL);
2414 while ((item=map_rect_get_item_binfile(mr)) == &busy_item);
2415 if (item && item->type == type_map_information) {
2416 if (binfile_attr_get(item->priv_data, attr_version, &attr))
2417 m->map_version=attr.u.num;
2418 if (binfile_attr_get(item->priv_data, attr_map_release, &attr))
2419 m->map_release=g_strdup(attr.u.str);
2420 if (m->url && binfile_attr_get(item->priv_data, attr_url, &attr)) {
2421 dbg(0,"url config %s map %s\n",m->url,attr.u.str);
2422 if (strcmp(m->url, attr.u.str))
2423 m->update_available=1;
2425 m->url=g_strdup(attr.u.str);
2428 map_rect_destroy_binfile(mr);
2429 if (m->map_version >= 16) {
2430 dbg(0,"Warning: This map is incompatible with your navit version. Please update navit.\n");
2438 map_binfile_close(struct map_priv *m)
2441 file_data_free(m->fi, (unsigned char *)m->index_cd);
2442 file_data_free(m->fi, (unsigned char *)m->eoc);
2443 file_data_free(m->fi, (unsigned char *)m->eoc64);
2444 g_free(m->cachedir);
2445 g_free(m->map_release);
2447 for (i = 0 ; i < m->eoc->zipedsk ; i++) {
2448 file_destroy(m->fis[i]);
2451 file_destroy(m->fi);
2455 map_binfile_destroy(struct map_priv *m)
2457 g_free(m->filename);
2459 g_free(m->progress);
2465 binfile_check_version(struct map_priv *m)
2468 if (!m->check_version)
2471 version=file_version(m->fi, m->check_version);
2472 if (version != m->version) {
2474 map_binfile_close(m);
2475 map_binfile_open(m);
2480 static struct map_priv *
2481 map_new_binfile(struct map_methods *meth, struct attr **attrs, struct callback_list *cbl)
2484 struct attr *data=attr_search(attrs, NULL, attr_data);
2485 struct attr *check_version,*map_pass,*flags,*url,*download_enabled;
2486 struct file_wordexp *wexp;
2491 wexp=file_wordexp_new(data->u.str);
2492 wexp_data=file_wordexp_get_array(wexp);
2493 dbg(1,"map_new_binfile %s\n", data->u.str);
2494 *meth=map_methods_binfile;
2496 m=g_new0(struct map_priv, 1);
2499 m->filename=g_strdup(wexp_data[0]);
2500 file_wordexp_destroy(wexp);
2501 check_version=attr_search(attrs, NULL, attr_check_version);
2503 m->check_version=check_version->u.num;
2504 map_pass=attr_search(attrs, NULL, attr_map_pass);
2506 m->passwd=g_strdup(map_pass->u.str);
2507 flags=attr_search(attrs, NULL, attr_flags);
2509 m->flags=flags->u.num;
2510 url=attr_search(attrs, NULL, attr_url);
2512 m->url=g_strdup(url->u.str);
2513 download_enabled = attr_search(attrs, NULL, attr_update);
2514 if (download_enabled)
2515 m->download_enabled=download_enabled->u.num;
2517 if (!map_binfile_open(m) && !m->check_version && !m->url) {
2518 map_binfile_destroy(m);
2529 dbg(1,"binfile: plugin_init\n");
2530 if (sizeof(struct zip_cd) != 46) {
2531 dbg(0,"error: sizeof(struct zip_cd)=%d\n",sizeof(struct zip_cd));
2533 plugin_register_map_type("binfile", map_new_binfile);