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

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

   
Visit the ZANavi Wiki