Codebase list readosm / lintian-fixes/main src / osmxml.c
lintian-fixes/main

Tree @lintian-fixes/main (Download .tar.gz)

osmxml.c @lintian-fixes/mainraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/* 
/ osmxml.c
/
/ OSM-XML (.osm) implementation
/
/ version  1.1.0, 2017 September 25
/
/ Author: Sandro Furieri a.furieri@lqt.it
/
/ ------------------------------------------------------------------------------
/ 
/ Version: MPL 1.1/GPL 2.0/LGPL 2.1
/ 
/ The contents of this file are subject to the Mozilla Public License Version
/ 1.1 (the "License"); you may not use this file except in compliance with
/ the License. You may obtain a copy of the License at
/ http://www.mozilla.org/MPL/
/ 
/ Software distributed under the License is distributed on an "AS IS" basis,
/ WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
/ for the specific language governing rights and limitations under the
/ License.
/
/ The Original Code is the ReadOSM library
/
/ The Initial Developer of the Original Code is Alessandro Furieri
/ 
/ Portions created by the Initial Developer are Copyright (C) 2012-2017
/ the Initial Developer. All Rights Reserved.
/ 
/ Contributor(s):
/ 
/ Alternatively, the contents of this file may be used under the terms of
/ either the GNU General Public License Version 2 or later (the "GPL"), or
/ the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
/ in which case the provisions of the GPL or the LGPL are applicable instead
/ of those above. If you wish to allow use of your version of this file only
/ under the terms of either the GPL or the LGPL, and not to allow others to
/ use your version of this file under the terms of the MPL, indicate your
/ decision by deleting the provisions above and replace them with the notice
/ and other provisions required by the GPL or the LGPL. If you do not delete
/ the provisions above, a recipient may use your version of this file under
/ the terms of any one of the MPL, the GPL or the LGPL.
/ 
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <expat.h>

#if defined(_WIN32) && !defined(__MINGW32__)
/* MSVC: avoiding to include at all config.h */
#else
#include "config.h"
#endif

#include "readosm.h"
#include "readosm_internals.h"

#if defined(_WIN32)
#define atol_64		_atoi64
#else
#define atol_64		atoll
#endif

#define BUFFSIZE	8192

struct xml_params
{
/* an helper struct supporting XML parsing */
    int current_tag;
    const void *user_data;
    readosm_node_callback node_callback;
    readosm_way_callback way_callback;
    readosm_relation_callback relation_callback;
    readosm_internal_node node;
    readosm_internal_way way;
    readosm_internal_relation relation;
    int stop;
};

static void
xml_init_params (struct xml_params *params, const void *user_data,
		 readosm_node_callback node_fnct, readosm_way_callback way_fnct,
		 readosm_relation_callback relation_fnct, int stop)
{
/* initializing an empty XML helper structure */
    params->current_tag = READOSM_CURRENT_TAG_UNKNOWN;
    params->user_data = user_data;
    params->node_callback = node_fnct;
    params->way_callback = way_fnct;
    params->relation_callback = relation_fnct;

    params->node.id = READOSM_UNDEFINED;
    params->node.latitude = READOSM_UNDEFINED;
    params->node.longitude = READOSM_UNDEFINED;
    params->node.version = READOSM_UNDEFINED;
    params->node.changeset = READOSM_UNDEFINED;
    params->node.user = NULL;
    params->node.uid = READOSM_UNDEFINED;
    params->node.timestamp = NULL;
    params->node.tag_count = 0;
    params->node.first_tag.next_item = 0;
    params->node.first_tag.next = NULL;
    params->node.last_tag = &(params->node.first_tag);

    params->way.id = READOSM_UNDEFINED;
    params->way.version = READOSM_UNDEFINED;
    params->way.changeset = READOSM_UNDEFINED;
    params->way.user = NULL;
    params->way.uid = READOSM_UNDEFINED;
    params->way.timestamp = NULL;
    params->way.ref_count = 0;
    params->way.first_ref.next_item = 0;
    params->way.first_ref.next = NULL;
    params->way.last_ref = &(params->way.first_ref);
    params->way.tag_count = 0;
    params->way.first_tag.next_item = 0;
    params->way.first_tag.next = NULL;
    params->way.last_tag = &(params->way.first_tag);

    params->relation.id = READOSM_UNDEFINED;
    params->relation.version = READOSM_UNDEFINED;
    params->relation.changeset = READOSM_UNDEFINED;
    params->relation.user = NULL;
    params->relation.uid = READOSM_UNDEFINED;
    params->relation.timestamp = NULL;
    params->relation.member_count = 0;
    params->relation.first_member.next_item = 0;
    params->relation.first_member.next = NULL;
    params->relation.last_member = &(params->relation.first_member);
    params->relation.tag_count = 0;
    params->relation.first_tag.next_item = 0;
    params->relation.first_tag.next = NULL;
    params->relation.last_tag = &(params->relation.first_tag);

    params->stop = stop;
}

static void
xml_reset_params (struct xml_params *params)
{
/* resetting the XML helper structure to initial empty state */
    readosm_internal_tag_block *tag_blk;
    readosm_internal_tag_block *tag_blk_n;
    readosm_internal_ref *ref;
    readosm_internal_ref *ref_n;
    readosm_internal_member_block *mbr_blk;
    readosm_internal_member_block *mbr_blk_n;

    if (params->node.user)
	free (params->node.user);
    if (params->node.timestamp)

	free (params->node.timestamp);
    tag_blk = &(params->node.first_tag);
    while (tag_blk)
      {
	  tag_blk_n = tag_blk->next;
	  if (tag_blk == &(params->node.first_tag))
	      release_internal_tag_block (tag_blk, 0);
	  else
	      release_internal_tag_block (tag_blk, 1);
	  tag_blk = tag_blk_n;
      }
    params->node.first_tag.next_item = 0;
    params->node.first_tag.next = NULL;
    params->node.last_tag = &(params->node.first_tag);

    if (params->way.user)
	free (params->way.user);
    if (params->way.timestamp)
	free (params->way.timestamp);
    ref = params->way.first_ref.next;
    while (ref)
      {
	  ref_n = ref->next;
	  free (ref);
	  ref = ref_n;
      }
    params->way.first_ref.next_item = 0;
    params->way.first_ref.next = NULL;
    params->way.last_ref = &(params->way.first_ref);
    tag_blk = &(params->way.first_tag);
    while (tag_blk)
      {
	  tag_blk_n = tag_blk->next;
	  if (tag_blk == &(params->way.first_tag))
	      release_internal_tag_block (tag_blk, 0);
	  else
	      release_internal_tag_block (tag_blk, 1);
	  tag_blk = tag_blk_n;
      }
    params->way.first_tag.next_item = 0;
    params->way.first_tag.next = NULL;
    params->way.last_tag = &(params->way.first_tag);

    if (params->relation.user)
	free (params->relation.user);
    if (params->relation.timestamp)
	free (params->relation.timestamp);
    mbr_blk = &(params->relation.first_member);
    while (mbr_blk)
      {
	  mbr_blk_n = mbr_blk->next;
	  if (mbr_blk == &(params->relation.first_member))
	      release_internal_member_block (mbr_blk, 0);
	  else
	      release_internal_member_block (mbr_blk, 1);
	  mbr_blk = mbr_blk_n;
      }
    params->relation.first_member.next_item = 0;
    params->relation.first_member.next = NULL;
    params->relation.last_member = &(params->relation.first_member);
    tag_blk = &(params->relation.first_tag);
    while (tag_blk)
      {
	  tag_blk_n = tag_blk->next;
	  if (tag_blk == &(params->relation.first_tag))
	      release_internal_tag_block (tag_blk, 0);
	  else
	      release_internal_tag_block (tag_blk, 1);
	  tag_blk = tag_blk_n;
      }
    params->relation.first_tag.next_item = 0;
    params->relation.first_tag.next = NULL;
    params->relation.last_tag = &(params->relation.first_tag);

    xml_init_params (params, params->user_data, params->node_callback,
		     params->way_callback, params->relation_callback,
		     params->stop);
}

static void
xml_start_node (struct xml_params *params, const char **attr)
{
/* an XML Node starts here */
    int i;
    int len;
    xml_reset_params (params);
    for (i = 0; attr[i]; i += 2)
      {
	  if (strcmp (attr[i], "id") == 0)
	      params->node.id = atol_64 (attr[i + 1]);
	  if (strcmp (attr[i], "lat") == 0)
	      params->node.latitude = atof (attr[i + 1]);
	  if (strcmp (attr[i], "lon") == 0)
	      params->node.longitude = atof (attr[i + 1]);
	  if (strcmp (attr[i], "version") == 0)
	      params->node.version = atoi (attr[i + 1]);
	  if (strcmp (attr[i], "changeset") == 0)
	      params->node.changeset = atol_64 (attr[i + 1]);
	  if (strcmp (attr[i], "user") == 0)
	    {
		len = strlen (attr[i + 1]);
		params->node.user = malloc (len + 1);
		strcpy (params->node.user, attr[i + 1]);
	    }
	  if (strcmp (attr[i], "uid") == 0)
	      params->node.uid = atoi (attr[i + 1]);
	  if (strcmp (attr[i], "timestamp") == 0)
	    {
		len = strlen (attr[i + 1]);
		params->node.timestamp = malloc (len + 1);
		strcpy (params->node.timestamp, attr[i + 1]);
	    }
      }
    params->current_tag = READOSM_CURRENT_TAG_IS_NODE;
}

static void
xml_end_node (struct xml_params *params)
{
/* an XML Node ends here */
    if (params->node_callback != NULL && params->stop == 0)
      {
	  int ret =
	      call_node_callback (params->node_callback, params->user_data,
				  &(params->node));
	  if (ret != READOSM_OK)
	      params->stop = 1;
      }
    xml_reset_params (params);
}

static void
xml_start_way (struct xml_params *params, const char **attr)
{
/* an XML Way starts here */
    int i;
    int len;
    xml_reset_params (params);
    for (i = 0; attr[i]; i += 2)
      {
	  if (strcmp (attr[i], "id") == 0)
	      params->way.id = atol_64 (attr[i + 1]);
	  if (strcmp (attr[i], "version") == 0)
	      params->way.version = atoi (attr[i + 1]);
	  if (strcmp (attr[i], "changeset") == 0)
	      params->way.changeset = atol_64 (attr[i + 1]);
	  if (strcmp (attr[i], "user") == 0)
	    {
		len = strlen (attr[i + 1]);
		params->way.user = malloc (len + 1);
		strcpy (params->way.user, attr[i + 1]);
	    }
	  if (strcmp (attr[i], "uid") == 0)
	      params->way.uid = atoi (attr[i + 1]);
	  if (strcmp (attr[i], "timestamp") == 0)
	    {
		len = strlen (attr[i + 1]);
		params->way.timestamp = malloc (len + 1);
		strcpy (params->way.timestamp, attr[i + 1]);
	    }
      }
    params->current_tag = READOSM_CURRENT_TAG_IS_WAY;
}

static void
xml_end_way (struct xml_params *params)
{
/* an XML Way ends here */
    if (params->way_callback != NULL && params->stop == 0)
      {
	  int ret = call_way_callback (params->way_callback, params->user_data,
				       &(params->way));
	  if (ret != READOSM_OK)
	      params->stop = 1;
      }
    xml_reset_params (params);
}

static void
xml_start_relation (struct xml_params *params, const char **attr)
{
/* an XML Relation starts here */
    int i;
    int len;
    xml_reset_params (params);
    for (i = 0; attr[i]; i += 2)
      {
	  if (strcmp (attr[i], "id") == 0)
	      params->relation.id = atol_64 (attr[i + 1]);
	  if (strcmp (attr[i], "version") == 0)
	      params->relation.version = atoi (attr[i + 1]);
	  if (strcmp (attr[i], "changeset") == 0)
	      params->relation.changeset = atol_64 (attr[i + 1]);
	  if (strcmp (attr[i], "user") == 0)
	    {
		len = strlen (attr[i + 1]);
		params->relation.user = malloc (len + 1);
		strcpy (params->relation.user, attr[i + 1]);
	    }
	  if (strcmp (attr[i], "uid") == 0)
	      params->relation.uid = atoi (attr[i + 1]);
	  if (strcmp (attr[i], "timestamp") == 0)
	    {
		len = strlen (attr[i + 1]);
		params->relation.timestamp = malloc (len + 1);
		strcpy (params->relation.timestamp, attr[i + 1]);
	    }
      }
    params->current_tag = READOSM_CURRENT_TAG_IS_RELATION;
}

static void
xml_end_relation (struct xml_params *params)
{
/* an XML Relation ends here */
    if (params->relation_callback != NULL && params->stop == 0)
      {
	  int ret = call_relation_callback (params->relation_callback,
					    params->user_data,
					    &(params->relation));
	  if (ret != READOSM_OK)
	      params->stop = 1;
      }
    xml_reset_params (params);
}

static void
xml_start_xtag (struct xml_params *params, const char **attr)
{
/* an XML Tag starts here */
    const char *key = NULL;
    const char *value = NULL;
    int i;

    if (params->current_tag == READOSM_CURRENT_TAG_IS_NODE
	|| params->current_tag == READOSM_CURRENT_TAG_IS_WAY
	|| params->current_tag == READOSM_CURRENT_TAG_IS_RELATION)
      {
	  for (i = 0; attr[i]; i += 2)
	    {
		if (strcmp (attr[i], "k") == 0)
		    key = attr[i + 1];
		if (strcmp (attr[i], "v") == 0)
		    value = attr[i + 1];
	    }
	  if (params->current_tag == READOSM_CURRENT_TAG_IS_NODE)
	      append_tag_to_node (&(params->node), key, value);
	  if (params->current_tag == READOSM_CURRENT_TAG_IS_WAY)
	      append_tag_to_way (&(params->way), key, value);
	  if (params->current_tag == READOSM_CURRENT_TAG_IS_RELATION)
	      append_tag_to_relation (&(params->relation), key, value);
      }
}

static void
xml_start_nd (struct xml_params *params, const char **attr)
{
/* an XML Nd starts here */
    int i;

    if (params->current_tag == READOSM_CURRENT_TAG_IS_WAY)
      {
	  for (i = 0; attr[i]; i += 2)
	    {
		if (strcmp (attr[i], "ref") == 0)
		    append_reference_to_way (&(params->way),
					     atol_64 (attr[i + 1]));
	    }
      }
}

static void
xml_start_member (struct xml_params *params, const char **attr)
{
/* an XML Member starts here */
    long long id = 0;
    int type = READOSM_UNDEFINED;
    const char *role = NULL;
    int i;

    if (params->current_tag == READOSM_CURRENT_TAG_IS_RELATION)
      {
	  for (i = 0; attr[i]; i += 2)
	    {
		if (strcmp (attr[i], "ref") == 0)
		    id = atol_64 (attr[i + 1]);
		if (strcmp (attr[i], "type") == 0)
		  {
		      if (strcmp (attr[i + 1], "node") == 0)
			  type = READOSM_MEMBER_NODE;
		      if (strcmp (attr[i + 1], "way") == 0)
			  type = READOSM_MEMBER_WAY;
		      if (strcmp (attr[i + 1], "relation") == 0)
			  type = READOSM_MEMBER_RELATION;
		  }
		if (strcmp (attr[i], "role") == 0)
		    role = attr[i + 1];
	    }
	  append_member_to_relation (&(params->relation), type, id, role);
      }
}

static void
xml_start_tag (void *data, const char *el, const char **attr)
{
/* some generic XML tag starts here */
    struct xml_params *params = (struct xml_params *) data;
    if (strcmp (el, "node") == 0)
	xml_start_node (params, attr);
    if (strcmp (el, "tag") == 0)
	xml_start_xtag (params, attr);
    if (strcmp (el, "way") == 0)
	xml_start_way (params, attr);
    if (strcmp (el, "nd") == 0)
	xml_start_nd (params, attr);
    if (strcmp (el, "relation") == 0)
	xml_start_relation (params, attr);
    if (strcmp (el, "member") == 0)
	xml_start_member (params, attr);
}

static void
xml_end_tag (void *data, const char *el)
{
/* some generic XML tag ends here */
    struct xml_params *params = (struct xml_params *) data;
    if (strcmp (el, "node") == 0)
	xml_end_node (params);
    if (strcmp (el, "way") == 0)
	xml_end_way (params);
    if (strcmp (el, "relation") == 0)
	xml_end_relation (params);
}

READOSM_PRIVATE int
parse_osm_xml (readosm_file * input, const void *user_data,
	       readosm_node_callback node_fnct, readosm_way_callback way_fnct,
	       readosm_relation_callback relation_fnct)
{
/* parsing the input file [OSM XML format] */
    XML_Parser parser;
    char xml_buff[BUFFSIZE];
    int done = 0;
    int len;
    struct xml_params params;

    xml_init_params (&params, user_data, node_fnct, way_fnct, relation_fnct, 0);

    parser = XML_ParserCreate (NULL);
    if (!parser)
	return READOSM_CREATE_XML_PARSER_ERROR;

    XML_SetUserData (parser, &params);
    XML_SetElementHandler (parser, xml_start_tag, xml_end_tag);
    while (!done)
      {
	  len = fread (xml_buff, 1, BUFFSIZE, input->in);
	  if (ferror (input->in))
	      return READOSM_READ_ERROR;
	  done = feof (input->in);
	  if (!XML_Parse (parser, xml_buff, len, done))
	      return READOSM_XML_ERROR;
	  if (params.stop)
	      return READOSM_ABORT;
      }
    XML_ParserFree (parser);

    return READOSM_OK;
}

READOSM_DECLARE const char *
readosm_expat_version (void)
{
/* returning the current Expat version string */
    XML_Expat_Version expat;
    static char version[64];
    expat = XML_ExpatVersionInfo ();
    sprintf (version, "%d.%d.%d", expat.major, expat.minor, expat.micro);
    return version;
}