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

Diff of /navit/navit/map/binfile/binfile.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.2  
changed lines
  Added in v.31

   
Visit the ZANavi Wiki