Beispiel #1
0
static OSMPBF__Blob *
read_blob(OSMPBF__BlobHeader *header, FILE *f, unsigned char *buffer)
{
	int len=header->datasize;
	SANITY_CHECK_LENGTH(len, MAX_BLOB_LENGTH)
	if (fread(buffer, len, 1, f) != 1)
		return NULL;
	return osmpbf__blob__unpack(&protobuf_c_system_allocator, len, buffer);
}
Beispiel #2
0
OSMPBF__Blob* parse_blob_body(struct Parser* const  p, const OSMPBF__BlobHeader* const bh) {
  OSMPBF__Blob* b = 0;
  b = osmpbf__blob__unpack(0, bh->datasize, p->cur);
  if (b == 0) 
    fprintf(stderr, "%s:%d error parsing blob of type %s\n",
	    __FILE__, __LINE__, bh->type);
  else {
    fprintf(stderr, "osm_blob: has_raw %d, has_raw_size %d, has_zlib_data %d, has_lzma_data %d\n",
	    b->has_raw, b->has_raw_size, b->has_zlib_data, b->has_lzma_data);
    p->cur += bh->datasize;
  }
  return b;
}
static void *read_blob(FILE *input, size_t length, size_t *raw_length)
{
  VALUE exc = Qnil;
  void *buffer = NULL;
  OSMPBF__Blob *blob = NULL;

  if(length < 1 || length > MAX_BLOB_SIZE)
    rb_raise(rb_eIOError, "Invalid blob size");

  if(!(buffer = malloc(length)))
    rb_raise(rb_eNoMemError, "Unable to allocate memory for the blob");

  if(fread(buffer, length, 1, input))
    blob = osmpbf__blob__unpack(NULL, length, buffer);

  free(buffer);

  if(blob == NULL)
    rb_raise(rb_eIOError, "Unable to read the blob");

  void *data = NULL;

  if(blob->has_raw)
  {
    if(!(data = malloc(blob->raw.len)))
    {
      exc = rb_exc_new2(rb_eNoMemError, "Unable to allocate memory for the data");
      goto exit_nicely;
    }

    memcpy(data, blob->raw.data, blob->raw.len);
    *raw_length = blob->raw.len;
  }
  else if(blob->has_zlib_data)
  {
    if(!(data = malloc(MAX_BLOB_SIZE)))
    {
      exc = rb_exc_new2(rb_eNoMemError, "Unable to allocate memory for the data");
      goto exit_nicely;
    }

    int ret;
    z_stream strm;

    strm.zalloc = Z_NULL;
    strm.zfree = Z_NULL;
    strm.opaque = Z_NULL;
    strm.avail_in = (unsigned int)blob->zlib_data.len;
    strm.next_in = blob->zlib_data.data;
    strm.avail_out = blob->raw_size;
    strm.next_out = data;

    ret = inflateInit(&strm);

    if (ret != Z_OK)
    {
      exc = rb_exc_new2(rb_eRuntimeError, "Zlib init failed");
      goto exit_nicely;
    }

    ret = inflate(&strm, Z_NO_FLUSH);

    (void)inflateEnd(&strm);

    if (ret != Z_STREAM_END)
    {
      exc = rb_exc_new2(rb_eRuntimeError, "Zlib compression failed");
      goto exit_nicely;
    }

    *raw_length = blob->raw_size;
  }
  else if(blob->has_lzma_data)
  {
    exc = rb_exc_new2(rb_eNotImpError, "LZMA compression is not supported");
    goto exit_nicely;
  }
  else
  {
    exc = rb_exc_new2(rb_eNotImpError, "Unknown blob format");
    goto exit_nicely;
  }

  exit_nicely:
    if(blob) osmpbf__blob__free_unpacked(blob, NULL);
    if(!data) free(data);
    if(exc != Qnil) rb_exc_raise(exc);

  return data;
}