Exemple #1
0
static void
printdata(size_t level, const void *v, size_t x, size_t l)
{
	const uint8_t *p = v, *ep = p + l;
	size_t ox;
	char buf[128];

	while (p + x < ep) {
		const uint8_t *q;
		uint8_t c = getclass(p[x]);
		uint8_t t = gettype(p[x]);
		ox = x;
		if (x != 0)
		printf("%.2x %.2x %.2x\n", p[x - 1], p[x], p[x + 1]);
		uint32_t tag = gettag(p, &x, ep - p + x);
		if (p + x >= ep)
			break;
		uint32_t len = getlength(p, &x, ep - p + x);
		
		printf("%zu %zu-%zu %c,%c,%s,%u:", level, ox, x,
		    der_class[c], der_type[t],
		    der_tag(buf, sizeof(buf), tag), len);
		q = p + x;
		if (p + len > ep)
			errx(EXIT_FAILURE, "corrupt der");
		printtag(tag, q, len);
		if (t != DER_TYPE_PRIMITIVE)
			printdata(level + 1, p, x, len + x);
		x += len;
	}
}
Exemple #2
0
int mm_init(void)
{

    t_block prolog_header, prolog_footer, epilog;

    //printf("sizeof struct:%d\n",sizeof(struct s_block));

    //try to allocated 3* Struct(8bytes) = 24 bytes

    if ((heap_listp = mem_sbrk(3*sizeof(struct s_block))) == NULL)
        return -1;


     prolog_header = heap_listp;
     prolog_header->size = PACK(0,USED);

     prolog_footer = heap_listp + 1;
     epilog = heap_listp + 2;

     prolog_header->size = PACK(0,USED);

     prolog_footer->size = PACK(0,USED);


     epilog->size = PACK(0,USED);
     printtag(epilog);

     heap_listp += 1;

     //printblock(heap_listp);

     return 0;

}
Exemple #3
0
int main(int argc, char **argv) {
    uint32_t length;            // length of the BlockHeader message
    BlockHeader *bhmsg = NULL;  // serialized BlockHeader message
    Blob *bmsg = NULL;          // serialized Blob message (size is given in the header)

    FILE * fd;

    char lenbuf[4];
    unsigned char *buf = NULL;

    enum {
        osmheader,
        osmdata
    } state = osmheader;

    if (argc == 2) {
        fd = fopen(argv[1], "r");
	if (fd == NULL) {
            fprintf(stderr, "Can't open %s", argv[1]);
            return -1;
        }
    } else {
        fd = stdin;
    }

    fputs_unlocked("<?xml version='1.0' encoding='UTF-8'?>\n<osm version=\"0.6\" generator=\"" PACKAGE_STRING "\">""\n", stdout);

    do {
    if (!fread(lenbuf, 4, 1, fd)) break;

    length = ntohl(*((uint32_t *) lenbuf));  // convert the buffer to a value

    if (verbose) fprintf(stderr, "Length BlockHeader: %d\n", length);

    if (length == 0 || length > MAX_BLOCK_HEADER_SIZE) {
        if (feof(fd)) {
	  break;
        }

        fprintf(stderr, "Block Header isn't present or exceeds minimum/maximum size\n");
        return 1;
    }

    
    /* Since we know the length of the BlockHeader now, we can allocate it */
    buf = (unsigned char *) malloc(length * sizeof(unsigned char));
    if (buf == NULL) {
        fprintf (stderr, "Error allocating BlockHeader buffer\n");
        return 1;
    }

    /* We are reading the BlockHeader */
    if(! fread(buf, length, 1, fd)) break;

    bhmsg = block_header__unpack (NULL, length, buf);
    free(buf);
    if (bhmsg == NULL) {
        fprintf(stderr, "Error unpacking BlockHeader message\n");
        return 1;
    }

    length = bhmsg->datasize;
    if (verbose) fprintf(stderr, "Type: %s\nLength: %u\n", bhmsg->type, length);
    if (length <= 0 || length > MAX_BLOB_SIZE) {
        fprintf(stderr, "Blob isn't present or exceeds minimum/maximum size\n");
        return 1;
    }

    if (strcmp(bhmsg->type, "OSMHeader") == 0) {
        state = osmheader;
    } else if (strcmp(bhmsg->type, "OSMData") == 0) {
        state = osmdata;
    }

    block_header__free_unpacked (bhmsg, &protobuf_c_system_allocator);

    /* We are now reading the 'Blob' */
    buf = (unsigned char *) malloc(length * sizeof(unsigned char *));
    if(! fread(buf, length, 1, fd)) break;

    bmsg = blob__unpack (NULL, length, buf);
    if (bmsg == NULL) {
        fprintf(stderr, "Error unpacking Blob message\n");
        return 1;
    }
    free(buf);
    
    unsigned char *uncompressed;
    if (bmsg->has_raw) {
        uncompressed = bmsg->raw.data;
	bmsg->raw_size = bmsg->raw.len; // I wonder if we should do this.
    } else {
        uncompressed = handleCompressedBlob(bmsg);
        if (uncompressed == NULL) {
            fprintf(stderr, "Uncompression failed\n");
            return 1;
        }
    }

    if (state == osmheader) {
        HeaderBlock *hmsg = header_block__unpack (NULL, bmsg->raw_size, uncompressed);
        if (hmsg == NULL) {
            fprintf(stderr, "Error unpacking HeaderBlock message\n");
            return 1;
        }

        if (verbose) fprintf(stderr, "%s\n", hmsg->required_features[0]);

        header_block__free_unpacked (hmsg, &protobuf_c_system_allocator);
    } else if (state == osmdata) {
        /*
         * Unpack Block and check if all went well
         */
        PrimitiveBlock *pmsg = primitive_block__unpack (NULL, bmsg->raw_size, uncompressed);
        if (pmsg == NULL) {
            fprintf(stderr, "Error unpacking PrimitiveBlock message\n");
            return 1;
        }

        double lat_offset = NANO_DEGREE * pmsg->lat_offset;
        double lon_offset = NANO_DEGREE * pmsg->lon_offset;
        double granularity = NANO_DEGREE * pmsg->granularity;

        if (verbose) fprintf(stderr, "\t""Granularity: %d""\n", pmsg->granularity);
        if (verbose) fprintf(stderr, "\t""Primitive groups: %li""\n", pmsg->n_primitivegroup);
        for (unsigned int j = 0; j < pmsg->n_primitivegroup; j++) {
            if (verbose) fprintf(stderr,"\t\t""Nodes: %li""\n"\
                   "\t\t""Ways: %li""\n"\
                   "\t\t""Relations: %li""\n",
                   (pmsg->primitivegroup[j]->dense ?
                    pmsg->primitivegroup[j]->dense->n_id :
                           pmsg->primitivegroup[j]->n_nodes),
                   pmsg->primitivegroup[j]->n_ways,
                   pmsg->primitivegroup[j]->n_relations);

            /* TODO: Nodes is *untested* */
            if (pmsg->primitivegroup[j]->n_nodes > 0) {
                for (unsigned k = 0; k < pmsg->primitivegroup[j]->n_nodes; k++) {
                    Node *node = pmsg->primitivegroup[j]->nodes[k];

                    printf("\t""<node id=\"%li\" lat=\"%.07f\" lon=\"%.07f\"",
                        node->id,
                        lat_offset + (node->lat * granularity),
                        lon_offset + (node->lon * granularity));
                    if (node->info) {
                        Info *info = node->info;
                        if (info->has_version) {
                            printnumericattribute("version", info->version);
                        }
                        if (info->has_changeset) {
                            printnumericattribute("changeset", info->changeset);
                        }
                        if (info->has_user_sid) {
                            ProtobufCBinaryData user = pmsg->stringtable->s[info->user_sid];
                            printuser(user);
                        }
                        if (info->has_uid) {
                            printnumericattribute("uid", info->uid);
                        }
                        if (info->has_timestamp) {
                            printtimestamp("timestamp", info->timestamp);
                        }
                    }

                    if (node->n_keys == 0 || node->n_vals == 0) {
                        fputs_unlocked("/>""\n", stdout);
                    } else {
                        fputs_unlocked(">""\n", stdout);

                        for (unsigned l = 0; l < node->n_keys; l++) {
                            ProtobufCBinaryData key = pmsg->stringtable->s[node->keys[l]];
                            ProtobufCBinaryData val = pmsg->stringtable->s[node->vals[l]];

                            printtag(key, val);
                        }

                        fputs_unlocked("\t""</node>""\n", stdout);
                    }
                }
            }
            else
            if (pmsg->primitivegroup[j]->n_ways > 0) {
                for (unsigned k = 0; k < pmsg->primitivegroup[j]->n_ways; k++) {
                    Way *way = pmsg->primitivegroup[j]->ways[k];
                    printsotid("way", way->id);
                    if (way->info) {
                        Info *info = way->info;
                        if (info->has_version) {
                            printnumericattribute("version", info->version);
                        }
                        if (info->has_changeset) {
                            printnumericattribute("changeset", info->changeset);
                        }
                        if (info->has_user_sid) {
                            ProtobufCBinaryData user = pmsg->stringtable->s[info->user_sid];
                            printuser(user);
                        }
                        if (info->has_uid) {
                            printnumericattribute("uid", info->uid);
                        }
                        if (info->has_timestamp) {
                            printtimestamp("timestamp", info->timestamp);
                        }
                    }

                    if ((way->n_keys == 0 || way->n_vals == 0) && way->n_refs == 0) {
                        fputs_unlocked("/>""\n", stdout);
                    } else {
                        long int deltaref = 0;
                        
                        fputs_unlocked(">""\n", stdout);
                        
                        for (unsigned l = 0; l < way->n_refs; l++) {
                            deltaref += way->refs[l];
                            printnd(deltaref);
                        }

                        for (unsigned l = 0; l < way->n_keys; l++) {
                            ProtobufCBinaryData key = pmsg->stringtable->s[way->keys[l]];
                            ProtobufCBinaryData val = pmsg->stringtable->s[way->vals[l]];

                            printtag(key, val);
                        }

                        fputs_unlocked("\t""</way>""\n", stdout);
                    }
                }
            }
            else
            if (pmsg->primitivegroup[j]->n_relations > 0) {
                for (unsigned k = 0; k < pmsg->primitivegroup[j]->n_relations; k++) {
                    Relation *relation = pmsg->primitivegroup[j]->relations[k];
                    printsotid("relation", relation->id);
                    if (relation->info) {
                        Info *info = relation->info;
                        if (info->has_version) {
                            printnumericattribute("version", info->version);
                        }
                        if (info->has_changeset) {
                            printnumericattribute("changeset", info->changeset);
                        }
                        if (info->has_user_sid) {
                            ProtobufCBinaryData user = pmsg->stringtable->s[info->user_sid];
                            printuser(user);
                        }
                        if (info->has_uid) {
                            printnumericattribute("uid", info->uid);
                        }
                        if (info->has_timestamp) {
                            printtimestamp("timestamp", info->timestamp);
                        }
                    }

                    if ((relation->n_keys == 0 || relation->n_vals == 0) && relation->n_memids == 0) {
                        fputs_unlocked("/>""\n", stdout);
                    } else {
                        long int deltamemids = 0;
                        
                        fputs_unlocked(">""\n", stdout);
                        
                        for (unsigned l = 0; l < relation->n_memids; l++) {
                            char *type;
                            ProtobufCBinaryData role =  pmsg->stringtable->s[relation->roles_sid[l]];
                            deltamemids += relation->memids[l];

                            switch (relation->types[l]) {
                                case RELATION__MEMBER_TYPE__NODE:
                                    type = "node";
                                    break;
                                case RELATION__MEMBER_TYPE__WAY:
                                    type = "way";
                                    break;
                                case RELATION__MEMBER_TYPE__RELATION:
                                    type = "relation";
                                    break;
                                default:
                                    fprintf(stderr, "Unsupported type: %u""\n", relation->types[l]);
                                    return 1;
                            }

                            printmember(type, deltamemids, role);
                        }

                        for (unsigned l = 0; l < relation->n_keys; l++) {
                            ProtobufCBinaryData key = pmsg->stringtable->s[relation->keys[l]];
                            ProtobufCBinaryData val = pmsg->stringtable->s[relation->vals[l]];

                            printtag(key, val);
                        }

                        fputs_unlocked("\t""</relation>""\n", stdout);
                    }
                }
            }
            else
            if (pmsg->primitivegroup[j]->n_changesets > 0) {
                for (unsigned k = 0; k < pmsg->primitivegroup[j]->n_changesets; k++) {
                    ChangeSet *changeset = pmsg->primitivegroup[j]->changesets[k];

                    printsotid("changeset", changeset->id);
                    if (changeset->info) {
                        Info *info = changeset->info;
                        /* Not in Changeset                    
                        if (info->has_version) {
                            printnumericattribute("version", info->version);
                        }
                        if (info->has_changeset) {
                            printnumericattribute("changeset", info->changeset);
                        }*/
                        if (info->has_user_sid) {
                            ProtobufCBinaryData user = pmsg->stringtable->s[info->user_sid];
                            printuser(user);
                        }
                        if (info->has_uid) {
                            printnumericattribute("uid", info->uid);
                        }
                        if (info->has_timestamp) {
                            printtimestamp("created_at", info->timestamp);

                            if (changeset->has_closetime_delta) {
                                printtimestamp("closed_at", (info->timestamp + changeset->closetime_delta));
                            }
                        }
                    }

                    printf(" open=\"%s\" min_lon=\"%.07f\" min_lat=\"%.07f\" max_lon=\"%.07f\" max_lat=\"%.07f\"",
                        (changeset->open ? "true" : "false"),
                        lat_offset + (changeset->bbox->left * granularity),
                        lon_offset + (changeset->bbox->bottom * granularity),
                        lat_offset + (changeset->bbox->right * granularity),
                        lon_offset + (changeset->bbox->top * granularity));

                    if (changeset->n_keys == 0 || changeset->n_vals == 0) {
                        fputs_unlocked("/>""\n", stdout);
                    } else {
                        fputs_unlocked(">""\n", stdout);

                        for (unsigned l = 0; l < changeset->n_keys; l++) {
                            ProtobufCBinaryData key = pmsg->stringtable->s[changeset->keys[l]];
                            ProtobufCBinaryData val = pmsg->stringtable->s[changeset->vals[l]];

                            printtag(key, val);
                        }

                        fputs_unlocked("\t""</changeset>""\n", stdout);
                    }
                }
            }
            else
            if (pmsg->primitivegroup[j]->dense) {
                unsigned l = 0;
                unsigned long int deltaid = 0;
                long int deltalat = 0;
                long int deltalon = 0;
                unsigned long int deltatimestamp = 0;
                unsigned long int deltachangeset = 0;
                long int deltauid = 0;
                unsigned long int deltauser_sid = 0;

                DenseNodes *dense = pmsg->primitivegroup[j]->dense;
                

                for (unsigned k = 0; k < dense->n_id; k++) {
                    unsigned char has_tags = 0;
                    deltaid += dense->id[k];
                    deltalat += dense->lat[k];
                    deltalon += dense->lon[k];
                    
                    printf("\t""<node id=\"%li\" lat=\"%.07f\" lon=\"%.07f\"", deltaid,
                            lat_offset + (deltalat * granularity),
                            lon_offset + (deltalon * granularity));

                    if (dense->denseinfo) {
                        DenseInfo *denseinfo = dense->denseinfo;

                        deltatimestamp += denseinfo->timestamp[k];
                        deltachangeset += denseinfo->changeset[k];
                        deltauid += denseinfo->uid[k];
                        deltauser_sid += denseinfo->user_sid[k];

                        printnumericattribute("version", denseinfo->version[k]);
                        printnumericattribute("changeset", deltachangeset);
			if (deltauid != -1) { // osmosis devs failed to read the specs
	                        printuser(pmsg->stringtable->s[deltauser_sid]);
	                        printnumericattribute("uid", deltauid);
			}
                        printtimestamp("timestamp", deltatimestamp);
                    }

                    if (l < dense->n_keys_vals) {
                        while (dense->keys_vals[l] != 0 && l < dense->n_keys_vals) {
                            if (has_tags < 1) {
                                has_tags++;
                                fputs_unlocked(">\n", stdout);
                            }
                            
                            ProtobufCBinaryData key = pmsg->stringtable->s[dense->keys_vals[l]];
                            ProtobufCBinaryData val = pmsg->stringtable->s[dense->keys_vals[l+1]];

                            printtag(key, val);

                            l += 2;
                        }
                        l += 1;
                    }

                    if (has_tags < 1) {
                        fputs_unlocked("/>""\n", stdout);
                    } else {
                        fputs_unlocked("\t""</node>""\n", stdout);
                    } 
                }
            }
        }

        primitive_block__free_unpacked (pmsg, &protobuf_c_system_allocator);
    }
    if (!bmsg->has_raw) free(uncompressed);
    blob__free_unpacked (bmsg, &protobuf_c_system_allocator);

    } while (!feof(fd));

    fputs_unlocked("</osm>""\n", stdout);

    if (!feof(fd)) {
      fprintf(stderr, "Input processing terminated early\n");
      return 1;
    }

    return 0;
}