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

Legend:
Removed from v.15  
changed lines
  Added in v.30

   
Visit the ZANavi Wiki