Exemplo n.º 1
0
/**
 * dfu_image_from_dfuse: (skip)
 * @data: data buffer
 * @length: length of @data we can access
 * @consumed: (out): the number of bytes we consued
 * @error: a #GError, or %NULL
 *
 * Unpacks an image from DfuSe data.
 *
 * Returns: a #DfuImage, or %NULL for error
 **/
static DfuImage *
dfu_image_from_dfuse (const guint8 *data,
		      guint32 length,
		      guint32 *consumed,
		      GError **error)
{
	DfuSeImagePrefix *im;
	guint32 elements;
	guint32 offset = sizeof(DfuSeImagePrefix);
	g_autoptr(DfuImage) image = NULL;

	g_assert_cmpint(sizeof(DfuSeImagePrefix), ==, 274);

	/* check input buffer size */
	if (length < sizeof(DfuSeImagePrefix)) {
		g_set_error (error,
			     FWUPD_ERROR,
			     FWUPD_ERROR_INTERNAL,
			     "invalid image data size %u",
			     (guint32) length);
		return NULL;
	}

	/* verify image signature */
	im = (DfuSeImagePrefix *) data;
	if (memcmp (im->sig, "Target", 6) != 0) {
		g_set_error_literal (error,
				     FWUPD_ERROR,
				     FWUPD_ERROR_INVALID_FILE,
				     "invalid DfuSe target signature");
		return NULL;
	}

	/* create new image */
	image = dfu_image_new ();
	dfu_image_set_alt_setting (image, im->alt_setting);
	if (GUINT32_FROM_LE (im->target_named) == 0x01)
		dfu_image_set_name (image, im->target_name);

	/* parse elements */
	length -= offset;
	elements = GUINT32_FROM_LE (im->elements);
	for (guint j = 0; j < elements; j++) {
		guint32 consumed_local;
		g_autoptr(DfuElement) element = NULL;
		element = dfu_element_from_dfuse (data + offset, length,
						  &consumed_local, error);
		if (element == NULL)
			return NULL;
		dfu_image_add_element (image, element);
		offset += consumed_local;
		length -= consumed_local;
	}

	/* return size */
	if (consumed != NULL)
		*consumed = offset;

	return g_object_ref (image);
}
Exemplo n.º 2
0
static gint
ico_write_int32 (FILE     *fp,
                 guint32  *data,
                 gint      count)
{
  gint total;

  total = count;
  if (count > 0)
    {
#if (G_BYTE_ORDER == G_BIG_ENDIAN)
      gint i;

      for (i = 0; i < count; i++)
        data[i] = GUINT32_FROM_LE (data[i]);
#endif

      ico_write_int8 (fp, (guint8 *) data, count * 4);

#if (G_BYTE_ORDER == G_BIG_ENDIAN)
      /* Put it back like we found it */
      for (i = 0; i < count; i++)
        data[i] = GUINT32_FROM_LE (data[i]);
#endif
    }

  return total * 4;
}
Exemplo n.º 3
0
/**
 * dfu_firmware_from_dfuse: (skip)
 * @firmware: a #DfuFirmware
 * @bytes: data to parse
 * @flags: some #DfuFirmwareParseFlags
 * @error: a #GError, or %NULL
 *
 * Unpacks into a firmware object from DfuSe data.
 *
 * Returns: %TRUE for success
 **/
gboolean
dfu_firmware_from_dfuse (DfuFirmware *firmware,
			 GBytes *bytes,
			 DfuFirmwareParseFlags flags,
			 GError **error)
{
	DfuSePrefix *prefix;
	gsize len;
	guint32 offset = sizeof(DfuSePrefix);
	guint8 *data;

	/* check the prefix (BE) */
	data = (guint8 *) g_bytes_get_data (bytes, &len);
	prefix = (DfuSePrefix *) data;
	if (memcmp (prefix->sig, "DfuSe", 5) != 0) {
		g_set_error_literal (error,
				     FWUPD_ERROR,
				     FWUPD_ERROR_INTERNAL,
				     "invalid DfuSe prefix");
		return FALSE;
	}

	/* check the version */
	if (prefix->ver != 0x01) {
		g_set_error (error,
			     FWUPD_ERROR,
			     FWUPD_ERROR_INTERNAL,
			     "invalid DfuSe version, got %02x",
			     prefix->ver);
		return FALSE;
	}

	/* check image size */
	if (GUINT32_FROM_LE (prefix->image_size) != len) {
		g_set_error (error,
			     FWUPD_ERROR,
			     FWUPD_ERROR_INTERNAL,
			     "invalid DfuSe image size, "
			     "got %" G_GUINT32_FORMAT ", "
			     "expected %" G_GSIZE_FORMAT,
			     GUINT32_FROM_LE (prefix->image_size),
			     len);
		return FALSE;
	}

	/* parse the image targets */
	len -= sizeof(DfuSePrefix);
	for (guint i = 0; i < prefix->targets; i++) {
		guint consumed;
		g_autoptr(DfuImage) image = NULL;
		image = dfu_image_from_dfuse (data + offset, (guint32) len,
					      &consumed, error);
		if (image == NULL)
			return FALSE;
		dfu_firmware_add_image (firmware, image);
		offset += consumed;
		len -= consumed;
	}
	return TRUE;
}
Exemplo n.º 4
0
static gboolean hcidump_process_packet(FILE_T fh, struct wtap_pkthdr *phdr,
    Buffer *buf, int *err, gchar **err_info)
{
	struct dump_hdr dh;
	int packet_size;

	if (!wtap_read_bytes_or_eof(fh, &dh, DUMP_HDR_SIZE, err, err_info))
		return FALSE;

	packet_size = GUINT16_FROM_LE(dh.len);
	if (packet_size > WTAP_MAX_PACKET_SIZE) {
		/*
		 * Probably a corrupt capture file; don't blow up trying
		 * to allocate space for an immensely-large packet.
		 */
		*err = WTAP_ERR_BAD_FILE;
		*err_info = g_strdup_printf("hcidump: File has %u-byte packet, bigger than maximum of %u",
			packet_size, WTAP_MAX_PACKET_SIZE);
		return FALSE;
	}

	phdr->rec_type = REC_TYPE_PACKET;
	phdr->presence_flags = WTAP_HAS_TS;
	phdr->ts.secs = GUINT32_FROM_LE(dh.ts_sec);
	phdr->ts.nsecs = GUINT32_FROM_LE(dh.ts_usec) * 1000;
	phdr->caplen = packet_size;
	phdr->len = packet_size;

	phdr->pseudo_header.p2p.sent = (dh.in ? FALSE : TRUE);

	return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
}
Exemplo n.º 5
0
/**
 * e2k_security_descriptor_new:
 * @xml_form: the XML form of the folder's security descriptor
 * (The "http://schemas.microsoft.com/exchange/security/descriptor"
 * property, aka %E2K_PR_EXCHANGE_SD_XML)
 * @binary_form: the binary form of the folder's security descriptor
 * (The "http://schemas.microsoft.com/exchange/ntsecuritydescriptor"
 * property, aka %E2K_PR_EXCHANGE_SD_BINARY)
 *
 * Constructs an #E2kSecurityDescriptor from the data in @xml_form and
 * @binary_form.
 *
 * Return value: the security descriptor, or %NULL if the data could
 * not be parsed.
 **/
E2kSecurityDescriptor *
e2k_security_descriptor_new (xmlNodePtr xml_form, GByteArray *binary_form)
{
	E2kSecurityDescriptor *sd;
	E2k_SECURITY_DESCRIPTOR_RELATIVE sdbuf;
	guint16 off, header_len;

	g_return_val_if_fail (xml_form != NULL, NULL);
	g_return_val_if_fail (binary_form != NULL, NULL);

	if (binary_form->len < 2)
		return NULL;

	memcpy (&header_len, binary_form->data, 2);
	header_len = GUINT16_FROM_LE (header_len);
	if (header_len + sizeof (sdbuf) > binary_form->len)
		return NULL;

	memcpy (&sdbuf, binary_form->data + header_len, sizeof (sdbuf));
	if (sdbuf.Revision != E2K_SECURITY_DESCRIPTOR_REVISION)
		return NULL;
	if ((sdbuf.Control & (E2K_SE_DACL_PRESENT | E2K_SE_SACL_PRESENT)) !=
	    E2K_SE_DACL_PRESENT)
		return NULL;

	sd = g_object_new (E2K_TYPE_SECURITY_DESCRIPTOR, NULL);
	sd->priv->header = g_byte_array_new ();
	g_byte_array_append (sd->priv->header, binary_form->data, header_len);
	sd->priv->control_flags = sdbuf.Control;

	/* Create a SID for "Default" then extract remaining SIDs from
	 * the XML form since they have display names associated with
	 * them.
	 */
	sd->priv->default_sid =
		e2k_sid_new_from_string_sid (E2K_SID_TYPE_WELL_KNOWN_GROUP,
					     E2K_SID_WKS_EVERYONE, NULL);
	g_hash_table_insert (sd->priv->sids,
			     (char *)e2k_sid_get_binary_sid (sd->priv->default_sid),
			     sd->priv->default_sid);
	extract_sids (sd, xml_form);

	off = GUINT32_FROM_LE (sdbuf.Owner) + sd->priv->header->len;
	if (!parse_sid (sd, binary_form, &off, &sd->priv->owner))
		goto lose;
	off = GUINT32_FROM_LE (sdbuf.Group) + sd->priv->header->len;
	if (!parse_sid (sd, binary_form, &off, &sd->priv->group))
		goto lose;

	off = GUINT32_FROM_LE (sdbuf.Dacl) + sd->priv->header->len;
	if (!parse_acl (sd, binary_form, &off))
		goto lose;

	return sd;

 lose:
	g_object_unref (sd);
	return NULL;
}
Exemplo n.º 6
0
size_t
riff_seek_id3(FILE *file)
{
	int ret;
	struct stat st;
	struct riff_header header;
	struct riff_chunk_header chunk;
	size_t size;

	/* determine the file size */

	ret = fstat(fileno(file), &st);
	if (ret < 0) {
		g_warning("Failed to stat file descriptor: %s",
			  strerror(errno));
		return 0;
	}

	/* seek to the beginning and read the RIFF header */

	ret = fseek(file, 0, SEEK_SET);
	if (ret != 0) {
		g_warning("Failed to seek: %s", g_strerror(errno));
		return 0;
	}

	size = fread(&header, sizeof(header), 1, file);
	if (size != 1 ||
	    memcmp(header.id, "RIFF", 4) != 0 ||
	    GUINT32_FROM_LE(header.size) > (uint32_t)st.st_size)
		/* not a RIFF file */
		return 0;

	while (true) {
		/* read the chunk header */

		size = fread(&chunk, sizeof(chunk), 1, file);
		if (size != 1)
			return 0;

		size = GUINT32_FROM_LE(chunk.size);
		if (size > G_MAXINT32)
			/* too dangerous, bail out: possible integer
			   underflow when casting to off_t */
			return 0;

		if (size % 2 != 0)
			/* pad byte */
			++size;

		if (memcmp(chunk.id, "id3 ", 4) == 0)
			/* found it! */
			return size;

		ret = fseek(file, size, SEEK_CUR);
		if (ret != 0)
			return 0;
	}
}
Exemplo n.º 7
0
static void print_obj(struct db_obj_ent *obj)
{
	uint32_t n_str = GUINT32_FROM_LE(obj->n_str);
	int i;
	void *p;
	uint16_t *slenp;
	char *dbstr;

	if (GUINT32_FROM_LE(obj->flags) & DB_OBJ_INLINE) {
		printf("%s\t%s\t%s\t[%u]\t%u\n",
			obj->bucket,
			obj->owner,
			obj->md5,
			(unsigned) GUINT64_FROM_LE(obj->size),
			n_str);
	} else {
		printf("%s\t%s\t%s\t%llX",
			obj->bucket,
			obj->owner,
			obj->md5,
			(long long) GUINT64_FROM_LE(obj->d.a.oid));
		for (i = 0; i < MAXWAY; i++) {
			if (i == 0) {
				printf("\t");
			} else {
				printf(",");
			}
			printf("%d", GUINT32_FROM_LE(obj->d.a.nidv[i]));
		}
		printf(" %u\n", n_str);
	}

	p = obj;
	p += sizeof(*obj);
	slenp = p;

	p += n_str * sizeof(uint16_t);

	for (i = 0; i < n_str; i++) {
		char pfx[16];

		dbstr = p;
		p += GUINT16_FROM_LE(*slenp);
		slenp++;

		if (i == 0)
			strcpy(pfx, "key: ");
		else
			sprintf(pfx, "str%d: ", i);

		printf("%s%s\n", pfx, dbstr);
	}

	printf("====\n");
}
Exemplo n.º 8
0
static gboolean hcidump_read(wtap *wth, int *err, gchar **err_info,
    gint64 *data_offset)
{
	struct dump_hdr dh;
	guint8 *buf;
	int bytes_read, packet_size;

	*data_offset = file_tell(wth->fh);

	bytes_read = file_read(&dh, DUMP_HDR_SIZE, wth->fh);
	if (bytes_read != DUMP_HDR_SIZE) {
		*err = file_error(wth->fh, err_info);
		if (*err == 0 && bytes_read != 0)
			*err = WTAP_ERR_SHORT_READ;
		return FALSE;
	}

	packet_size = GUINT16_FROM_LE(dh.len);
	if (packet_size > WTAP_MAX_PACKET_SIZE) {
		/*
		 * Probably a corrupt capture file; don't blow up trying
		 * to allocate space for an immensely-large packet.
		 */
		*err = WTAP_ERR_BAD_FILE;
		*err_info = g_strdup_printf("hcidump: File has %u-byte packet, bigger than maximum of %u",
			packet_size, WTAP_MAX_PACKET_SIZE);
		return FALSE;
	}

	buffer_assure_space(wth->frame_buffer, packet_size);
	buf = buffer_start_ptr(wth->frame_buffer);

	bytes_read = file_read(buf, packet_size, wth->fh);
	if (bytes_read != packet_size) {
		*err = file_error(wth->fh, err_info);
		if (*err == 0)
			*err = WTAP_ERR_SHORT_READ;
		return FALSE;
	}

	wth->phdr.presence_flags = WTAP_HAS_TS;
	wth->phdr.ts.secs = GUINT32_FROM_LE(dh.ts_sec);
	wth->phdr.ts.nsecs = GUINT32_FROM_LE(dh.ts_usec) * 1000;
	wth->phdr.caplen = packet_size;
	wth->phdr.len = packet_size;

	wth->phdr.pseudo_header.p2p.sent = (dh.in ? FALSE : TRUE);

	return TRUE;
}
Exemplo n.º 9
0
static BlueSkyCleanerItem *bluesky_cleaner_deserialize(BlueSkyRCStr *raw)
{
    const char *data = raw->data;
    size_t len = raw->len;
    const char *data1, *data2, *data3;
    size_t len1, len2, len3;

    g_assert(len > 4);
    if (len < sizeof(struct cloudlog_header)
        || memcmp(data, CLOUDLOG_MAGIC, 4) != 0)
    {
        g_warning("Deserializing garbage cloud log item from cleaner!");
        return NULL;
    };

    struct cloudlog_header *header = (struct cloudlog_header *)data;
    len1 = GUINT32_FROM_LE(header->size1);
    len2 = GUINT32_FROM_LE(header->size2);
    len3 = GUINT32_FROM_LE(header->size3);
    data1 = data + sizeof(struct cloudlog_header);
    data2 = data1 + len1;
    data3 = data2 + len2;
    g_assert(data3 + len3 - data <= len);

    BlueSkyCleanerItem *item = g_new0(BlueSkyCleanerItem, 1);
    item->type = header->type - '0';
    item->inum = GUINT64_FROM_LE(header->inum);
    memcpy(&item->id, &header->id, sizeof(BlueSkyCloudID));

    int link_count = len2 / sizeof(BlueSkyCloudID);
    g_print("Outgoing links: %d\n", link_count);
    item->links = g_array_new(FALSE, TRUE, sizeof(BlueSkyCleanerLink));
    for (int i = 0; i < link_count; i++) {
        BlueSkyCleanerLink link;

        g_assert(len2 >= sizeof(link.id));
        memcpy(&link.id, data2, sizeof(link.id));
        data2 += sizeof(link.id); len2 -= sizeof(link.id);

        g_assert(len3 >= sizeof(link.location));
        memcpy(&link.location, data3, sizeof(link.location));
        data3 += sizeof(link.location); len3 -= sizeof(link.location);

        g_array_append_val(item->links, link);
    }

    return item;
}
Exemplo n.º 10
0
static gboolean
fu_smbios_parse_ep64 (FuSmbios *self, const gchar *buf, gsize sz, GError **error)
{
	FuSmbiosStructureEntryPoint64 *ep;
	guint8 csum = 0;

	/* verify size */
	if (sz != sizeof(FuSmbiosStructureEntryPoint64)) {
		g_set_error (error,
			     FWUPD_ERROR,
			     FWUPD_ERROR_INVALID_FILE,
			     "invalid smbios3 entry point got %" G_GSIZE_FORMAT
			     " bytes, expected %" G_GSIZE_FORMAT,
			     sz, sizeof(FuSmbiosStructureEntryPoint32));
		return FALSE;
	}

	/* verify checksum */
	for (guint i = 0; i < sz; i++)
		csum += buf[i];
	if (csum != 0x00) {
		g_set_error_literal (error,
				     FWUPD_ERROR,
				     FWUPD_ERROR_INVALID_FILE,
				     "entry point checksum invalid");
		return FALSE;
	}
	ep = (FuSmbiosStructureEntryPoint64 *) buf;
	self->structure_table_len = GUINT32_FROM_LE (ep->structure_table_len);
	self->smbios_ver = g_strdup_printf ("%u.%u",
					    ep->smbios_major_ver,
					    ep->smbios_minor_ver);
	return TRUE;
}
Exemplo n.º 11
0
float mdb_get_single(unsigned char *buf, int offset)
{
	union {guint32 g; float f;} f;
	memcpy(&f, &buf[offset], 4);
	f.g = GUINT32_FROM_LE(f.g);
	return f.f;
}
Exemplo n.º 12
0
/**
 * mongo_bson_new_from_data:
 * @buffer: (in): The buffer to create a #MongoBson.
 * @length: (in): The length of @buffer.
 *
 * Creates a new #MongoBson instance using the buffer and the length.
 *
 * Returns: A new #MongoBson that should be freed with mongo_bson_unref().
 */
MongoBson *
mongo_bson_new_from_data (const guint8 *buffer,
                          gsize         length)
{
   MongoBson *bson;
   guint32 bson_len;

   g_return_val_if_fail(buffer != NULL, NULL);

   /*
    * The first 4 bytes of a BSON are the length, including the 4 bytes
    * containing said length.
    */
   memcpy(&bson_len, buffer, sizeof bson_len);
   bson_len = GUINT32_FROM_LE(bson_len);
   if (bson_len != length) {
      return NULL;
   }

   bson = g_slice_new0(MongoBson);
   bson->ref_count = 1;
   bson->buf = g_byte_array_sized_new(length);
   g_byte_array_append(bson->buf, buffer, length);

   return bson;
}
Exemplo n.º 13
0
static gboolean
commview_read_header(commview_header_t *cv_hdr, FILE_T fh, int *err,
    gchar **err_info)
{
	wtap_file_read_expected_bytes(&cv_hdr->data_len, 2, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->source_data_len, 2, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->version, 1, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->year, 2, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->month, 1, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->day, 1, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->hours, 1, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->minutes, 1, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->seconds, 1, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->usecs, 4, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->flags, 1, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->signal_level_percent, 1, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->rate, 1, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->band, 1, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->channel, 1, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->direction, 1, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->signal_level_dbm, 1, fh, err, err_info);
	wtap_file_read_expected_bytes(&cv_hdr->noise_level, 1, fh, err, err_info);

	/* Convert multi-byte values from little endian to host endian format */
	cv_hdr->data_len = GUINT16_FROM_LE(cv_hdr->data_len);
	cv_hdr->source_data_len = GUINT16_FROM_LE(cv_hdr->source_data_len);
	cv_hdr->year = GUINT16_FROM_LE(cv_hdr->year);
	cv_hdr->usecs = GUINT32_FROM_LE(cv_hdr->usecs);

	return TRUE;
}
Exemplo n.º 14
0
float mdb_get_single(void *buf, int offset)
{
	union {guint32 g; float f;} f;
	memcpy(&f, buf + offset, 4);
	f.g = GUINT32_FROM_LE(f.g);
	return f.f;
}
Exemplo n.º 15
0
/**
 * fwupd_guid_to_string:
 * @guid: a #fwupd_guid_t to read
 * @flags: some %FwupdGuidFlags, e.g. %FWUPD_GUID_FLAG_MIXED_ENDIAN
 *
 * Returns a text GUID of mixed or BE endian for a packed buffer.
 *
 * Returns: A new GUID
 *
 * Since: 1.2.5
 **/
gchar *
fwupd_guid_to_string (const fwupd_guid_t *guid, FwupdGuidFlags flags)
{
	fwupd_guid_native_t gnat;

	g_return_val_if_fail (guid != NULL, NULL);

	/* copy to avoid issues with aligning */
	memcpy (&gnat, guid, sizeof(gnat));

	/* mixed is bizaar, but specified as the DCE encoding */
	if (flags & FWUPD_GUID_FLAG_MIXED_ENDIAN) {
		return g_strdup_printf ("%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
					GUINT32_FROM_LE(gnat.a),
					GUINT16_FROM_LE(gnat.b),
					GUINT16_FROM_LE(gnat.c),
					GUINT16_FROM_BE(gnat.d),
					gnat.e[0], gnat.e[1],
					gnat.e[2], gnat.e[3],
					gnat.e[4], gnat.e[5]);
	}
	return g_strdup_printf ("%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
				GUINT32_FROM_BE(gnat.a),
				GUINT16_FROM_BE(gnat.b),
				GUINT16_FROM_BE(gnat.c),
				GUINT16_FROM_BE(gnat.d),
				gnat.e[0], gnat.e[1],
				gnat.e[2], gnat.e[3],
				gnat.e[4], gnat.e[5]);
}
Exemplo n.º 16
0
guint32
msn_read32le(const char *buf)
{
    guint32 val;
    memcpy(&val, buf, sizeof(val));
    return GUINT32_FROM_LE(val);
}
Exemplo n.º 17
0
/**
 * mongo_bson_new_from_static_data:
 * @buffer: (in) (transfer full): The static buffer to use.
 * @length: (in): The number of bytes in @buffer.
 * @notify: (in): A #GDestroyNotify to free @buffer.
 *
 * Creates a new #MongoBson structure using @buffer. This does not copy
 * the content of the buffer and uses it directly. Therefore, you MUST make
 * sure that the structure outlives the buffer beneath it.
 *
 * If the structure is referenced with mongo_bson_ref(), the contents of
 * the buffer will be copied.
 *
 * When the structure is released, @notify will be called to release
 * @buffer.
 *
 * You MAY NOT modify the contents of @buffer using any of the
 * mongo_bson_append methods.
 *
 * Returns: A newly created #MongoBson structure.
 */
MongoBson *
mongo_bson_new_from_static_data (guint8         *buffer,
                                 gsize           length,
                                 GDestroyNotify  notify)
{
   MongoBson *bson;
   guint32 bson_len;

   g_return_val_if_fail(buffer != NULL, NULL);

   /*
    * The first 4 bytes of a BSON are the length, including the 4 bytes
    * containing said length.
    */
   memcpy(&bson_len, buffer, sizeof bson_len);
   bson_len = GUINT32_FROM_LE(bson_len);
   if (bson_len != length) {
      return NULL;
   }

   bson = g_slice_new0(MongoBson);
   bson->ref_count = 1;
   bson->static_data = buffer;
   bson->static_len = length;
   bson->static_notify = notify;

   return bson;
}
Exemplo n.º 18
0
/**
 * dfu_element_from_dfuse: (skip)
 * @data: data buffer
 * @length: length of @data we can access
 * @consumed: (out): the number of bytes we consued
 * @error: a #GError, or %NULL
 *
 * Unpacks an element from DfuSe data.
 *
 * Returns: a #DfuElement, or %NULL for error
 **/
static DfuElement *
dfu_element_from_dfuse (const guint8 *data,
			guint32 length,
			guint32 *consumed,
			GError **error)
{
	DfuElement *element = NULL;
	DfuSeElementPrefix *el = (DfuSeElementPrefix *) data;
	guint32 size;
	g_autoptr(GBytes) contents = NULL;

	g_assert_cmpint(sizeof(DfuSeElementPrefix), ==, 8);

	/* check input buffer size */
	if (length < sizeof(DfuSeElementPrefix)) {
		g_set_error (error,
			     FWUPD_ERROR,
			     FWUPD_ERROR_INTERNAL,
			     "invalid element data size %u",
			     (guint32) length);
		return NULL;
	}

	/* check size */
	size = GUINT32_FROM_LE (el->size);
	if (size + sizeof(DfuSeElementPrefix) > length) {
		g_set_error (error,
			     FWUPD_ERROR,
			     FWUPD_ERROR_INTERNAL,
			     "invalid element size %u, only %u bytes left",
			     size,
			     (guint32) (length - sizeof(DfuSeElementPrefix)));
		return NULL;
	}

	/* create new element */
	element = dfu_element_new ();
	dfu_element_set_address (element, GUINT32_FROM_LE (el->address));
	contents = g_bytes_new (data + sizeof(DfuSeElementPrefix), size);
	dfu_element_set_contents (element, contents);

	/* return size */
	if (consumed != NULL)
		*consumed = (guint32) sizeof(DfuSeElementPrefix) + size;

	return element;
}
Exemplo n.º 19
0
// Processes messages having this format:
// https://en.bitcoin.it/wiki/Protocol_specification#Message_structure
void incoming_node_data(const int fd, struct bitcoin_storage *const st)
{
	static struct msg_wire *buf = NULL; // For storing the message payload
	static int buf_allocated = 0;
	static int buf_pos = 0;
	static int buf_left = sizeof(struct msg_wire);
	static enum msg_type buf_type = UNDEFINED;

	// Reallocate buffer only if it is too small.
	if (buf_allocated < buf_pos+buf_left) {
		buf_allocated = buf_pos+buf_left;
		buf = g_realloc(buf,buf_allocated);
	}

	const int got = read(fd,(void*)buf+buf_pos,buf_left);
	if (got == 0) {
		errx(3,"Unexpected end of bitcoind stream");
	} else if (got == -1) {
		err(3,"Error reading bitcoind stream");
	}
	buf_pos += got;
	buf_left -= got;

	// Header received. To continue reading in next pass, update
	// the message length and possibly compact the header.
	if (buf_type == UNDEFINED && buf_pos == sizeof(struct msg_wire)) {
		// Check message header
		if (GUINT32_FROM_LE(buf->magic) != 0xD9B4BEF9) {
			errx(3,"Magic error. Probably we got out of sync.");
		}

		// Gather message type and length
		buf_type = bitcoin_find_type(buf);
		const guint32 payload_len = GUINT32_FROM_LE(buf->length_le);
		
		// Do header compaction if type is not INV. Doing
		// dangerous in-place rewrite.
		if (buf_type != INV) {
			COMPACT->type = buf_type;
			COMPACT->length = payload_len;
			COMPACT->sent = false;
			// Rewind to compacted payload starting pos
			buf_pos = offsetof(struct msg,payload);
		}
		
		buf_left = payload_len;
	}
Exemplo n.º 20
0
/**
 * Reads an unsigned 32-bit Little Endian value from the stream into native endian format.
 *
 * @param value Pointer to the variable to read the value into.
 * @param stream A #VFSFile object representing the stream.
 * @return TRUE if read was succesful, FALSE if there was an error.
 */
EXPORT bool_t vfs_fget_le32(uint32_t *value, VFSFile *stream)
{
    uint32_t tmp;
    if (vfs_fread(&tmp, sizeof(tmp), 1, stream) != 1)
        return FALSE;
    *value = GUINT32_FROM_LE(tmp);
    return TRUE;
}
Exemplo n.º 21
0
/**
 * unzip_file:
 * @zip_file:   pointer to start of compressed data
 * @unzip_size: the size of the compressed data block
 *
 * Returns a pointer to uncompressed data (maybe NULL)
 */
void *unzip_file(gchar *zip_file, gulong *unzip_size)
{
	void *unzip_data = NULL;
#ifndef HAVE_LIBZ
	goto end;
#else
	gchar *zip_data;
	struct _lfh {
		guint32 sig;
		guint16 extract_version;
		guint16 flags;
		guint16 comp_method;
		guint16 time;
		guint16 date;
		guint32 crc_32;
		guint32 compressed_size;
		guint32 uncompressed_size;
		guint16 filename_len;
		guint16 extra_field_len;
	}  __attribute__ ((__packed__)) *local_file_header = NULL;


	local_file_header = (struct _lfh *) zip_file;
	if (GUINT32_FROM_LE(local_file_header->sig) != 0x04034b50) {
		g_warning("%s(): wrong format", __PRETTY_FUNCTION__);
		g_free(unzip_data);
		goto end;
	}

	zip_data = zip_file + sizeof(struct _lfh)
		+ GUINT16_FROM_LE(local_file_header->filename_len)
		+ GUINT16_FROM_LE(local_file_header->extra_field_len);
	gulong uncompressed_size = GUINT32_FROM_LE(local_file_header->uncompressed_size);
	unzip_data = g_malloc(uncompressed_size);

	if (!(*unzip_size = uncompress_data(unzip_data, uncompressed_size, zip_data, GUINT32_FROM_LE(local_file_header->compressed_size)))) {
		g_free(unzip_data);
		unzip_data = NULL;
		goto end;
	}

#endif
end:
	return(unzip_data);
}
Exemplo n.º 22
0
static struct fp_print_data *fpi_print_data_from_fp2_data(unsigned char *buf,
	size_t buflen)
{
	size_t total_data_len, item_len;
	struct fp_print_data *data;
	struct fp_print_data_item *item;
	struct fpi_print_data_fp2 *raw = (struct fpi_print_data_fp2 *) buf;
	unsigned char *raw_buf;
	struct fpi_print_data_item_fp2 *raw_item;

	total_data_len = buflen - sizeof(*raw);
	data = print_data_new(GUINT16_FROM_LE(raw->driver_id),
		GUINT32_FROM_LE(raw->devtype), raw->data_type);
	raw_buf = raw->data;
	while (total_data_len) {
		if (total_data_len < sizeof(*raw_item))
			break;
		total_data_len -= sizeof(*raw_item);

		raw_item = (struct fpi_print_data_item_fp2 *)raw_buf;
		item_len = GUINT32_FROM_LE(raw_item->length);
		fp_dbg("item len %d, total_data_len %d", item_len, total_data_len);
		if (total_data_len < item_len) {
			fp_err("corrupted fingerprint data");
			break;
		}
		total_data_len -= item_len;

		item = fpi_print_data_item_new(item_len);
		/* FIXME: fp_print_data->data content is not endianess agnostic */
		memcpy(item->data, raw_item->data, item_len);
		data->prints = g_slist_prepend(data->prints, item);

		raw_buf += sizeof(*raw_item);
		raw_buf += item_len;
	}

	if (g_slist_length(data->prints) == 0) {
		fp_print_data_free(data);
		data = NULL;
	}

	return data;

}
Exemplo n.º 23
0
KoneplusRmpMacroKeyInfo *koneplus_rmp_macro_key_info_v1_to_koneplus_rmp_macro_key_info(KoneplusRmpMacroKeyInfoV1 const *v1) {
	KoneplusRmpMacroKeyInfo *result;
	result = koneplus_rmp_macro_key_info_new();

	result->button_number = v1->button_number;
	result->type = v1->type;
	koneplus_rmp_macro_key_info_set_talk_device(result, GUINT16_FROM_LE(v1->talk_device));
	koneplus_rmp_macro_key_info_set_macroset_name(result, (gchar const *)v1->macroset_name);
	koneplus_rmp_macro_key_info_set_macro_name(result, (gchar const *)v1->macro_name);
	koneplus_rmp_macro_key_info_set_loop(result, GUINT32_FROM_LE(v1->loop));
	koneplus_rmp_macro_key_info_set_count(result, GUINT16_FROM_LE(v1->count));
	memcpy(&result->keystrokes[0], &v1->keystrokes[0], sizeof(result->keystrokes));
	koneplus_rmp_macro_key_info_set_timer_length(result, GUINT32_FROM_LE(v1->timer_length));
	koneplus_rmp_macro_key_info_set_timer_name(result, (gchar const *)v1->timer_name);
	koneplus_rmp_macro_key_info_set_filename(result, (gchar const *)v1->filename);

	return result;
}
Exemplo n.º 24
0
gboolean
gst_riff_parse_strf_iavs (GstElement * element,
    GstBuffer * buf, gst_riff_strf_iavs ** _strf, GstBuffer ** data)
{
  gst_riff_strf_iavs *strf;

  g_return_val_if_fail (buf != NULL, FALSE);
  g_return_val_if_fail (_strf != NULL, FALSE);
  g_return_val_if_fail (data != NULL, FALSE);

  if (GST_BUFFER_SIZE (buf) < sizeof (gst_riff_strf_iavs))
    goto too_small;

  strf = g_memdup (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
  gst_buffer_unref (buf);

#if (G_BYTE_ORDER == G_BIG_ENDIAN)
  strf->DVAAuxSrc = GUINT32_FROM_LE (strf->DVAAuxSrc);
  strf->DVAAuxCtl = GUINT32_FROM_LE (strf->DVAAuxCtl);
  strf->DVAAuxSrc1 = GUINT32_FROM_LE (strf->DVAAuxSrc1);
  strf->DVAAuxCtl1 = GUINT32_FROM_LE (strf->DVAAuxCtl1);
  strf->DVVAuxSrc = GUINT32_FROM_LE (strf->DVVAuxSrc);
  strf->DVVAuxCtl = GUINT32_FROM_LE (strf->DVVAuxCtl);
  strf->DVReserved1 = GUINT32_FROM_LE (strf->DVReserved1);
  strf->DVReserved2 = GUINT32_FROM_LE (strf->DVReserved2);
#endif

  /* debug */
  GST_INFO_OBJECT (element, "strf tag found in context iavs:");
  GST_INFO_OBJECT (element, " DVAAuxSrc   %08x", strf->DVAAuxSrc);
  GST_INFO_OBJECT (element, " DVAAuxCtl   %08x", strf->DVAAuxCtl);
  GST_INFO_OBJECT (element, " DVAAuxSrc1  %08x", strf->DVAAuxSrc1);
  GST_INFO_OBJECT (element, " DVAAuxCtl1  %08x", strf->DVAAuxCtl1);
  GST_INFO_OBJECT (element, " DVVAuxSrc   %08x", strf->DVVAuxSrc);
  GST_INFO_OBJECT (element, " DVVAuxCtl   %08x", strf->DVVAuxCtl);
  GST_INFO_OBJECT (element, " DVReserved1 %08x", strf->DVReserved1);
  GST_INFO_OBJECT (element, " DVReserved2 %08x", strf->DVReserved2);

  *_strf = strf;
  *data = NULL;

  return TRUE;

  /* ERRORS */
too_small:
  {
    GST_ERROR_OBJECT (element,
        "Too small strf_iavs (%d available, %" G_GSSIZE_FORMAT " needed)",
        GST_BUFFER_SIZE (buf), sizeof (gst_riff_strf_iavs));
    gst_buffer_unref (buf);
    return FALSE;
  }
}
Exemplo n.º 25
0
void
purple_pn_xfer_got_invite(struct pn_peer_call *call,
                          const char *branch,
                          const char *context)
{
    PurpleAccount *account;
    PurpleXfer *xfer;
    char *bin;
    gsize bin_len;
    guint32 file_size;
    char *file_name;
    gunichar2 *uni_name;

    account = msn_session_get_user_data (pn_peer_link_get_session (call->link));

    call->cb = xfer_completed_cb;
    call->end_cb = xfer_end_cb;
    call->progress_cb = xfer_progress_cb;
    call->branch = g_strdup(branch);

    call->pending = TRUE;

    xfer = purple_xfer_new(account, PURPLE_XFER_RECEIVE,
                           pn_peer_link_get_passport (call->link));
    if (xfer)
    {
        bin = (char *)purple_base64_decode(context, &bin_len);
        file_size = GUINT32_FROM_LE(*(gsize *)(bin + 8));

        uni_name = (gunichar2 *)(bin + 20);
        while(*uni_name != 0 && ((char *)uni_name - (bin + 20)) < MAX_FILE_NAME_LEN) {
            *uni_name = GUINT16_FROM_LE(*uni_name);
            uni_name++;
        }

        file_name = g_utf16_to_utf8((const gunichar2 *)(bin + 20), -1,
                                    NULL, NULL, NULL);

        g_free(bin);

        purple_xfer_set_filename(xfer, file_name);
        purple_xfer_set_size(xfer, file_size);
        purple_xfer_set_init_fnc(xfer, xfer_init);
        purple_xfer_set_request_denied_fnc(xfer, xfer_cancel);
        purple_xfer_set_cancel_recv_fnc(xfer, xfer_cancel);

        call->xfer = xfer;
        purple_xfer_ref(call->xfer);

        xfer->data = call;

        purple_xfer_request(xfer);
    }
}
Exemplo n.º 26
0
void bu256_hex(char *hexstr, const bu256_t *v)
{
	*hexstr = 0;

	int i;
	for (i = 7; i >= 0; i--) {		/* endian: high to low */
		char tmp[8 + 1];

		sprintf(tmp, "%08x", GUINT32_FROM_LE(v->dword[i]));
		strcat(hexstr, tmp);
	}
}
Exemplo n.º 27
0
/**
 * g_bytes_vector_read_uint32:
 * @vector: (in): A #GBytesVector.
 * @value: (out): A location for a #guint32.
 *
 * Reads a #guint64 from the vector of buffers in @vector. If successful
 * then @value is set and %TRUE is returned.
 *
 * Returns: %TRUE if successful; otherwise %FALSE.
 */
gboolean
g_bytes_vector_read_uint32 (GBytesVector *vector,
                            guint32      *value)
{
   gboolean ret;

   if ((ret = g_bytes_vector_read(vector, (guint8 *)value, sizeof *value))) {
      *value = GUINT32_FROM_LE(*value);
   }

   return ret;
}
Exemplo n.º 28
0
guint32
fu_wac_calculate_checksum32le (const guint8 *data, gsize len)
{
	guint32 csum = 0x0;
	g_return_val_if_fail (len % 4 == 0, 0xff);
	for (guint i = 0; i < len; i += 4) {
		guint32 tmp;
		memcpy (&tmp, &data[i], sizeof(guint32));
		csum += GUINT32_FROM_LE (tmp);
	}
	return GUINT32_TO_LE (csum);
}
Exemplo n.º 29
0
static gboolean dbx_load_indices (DbxImporter *m)
{
	guint indexptr, itemcount;
	guint32 index_ofs = 0;

	if (dbx_pread (m->dbx_fd, &indexptr, 4, INDEX_POINTER) != 4) {
		g_set_error (
			&m->base.error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
			"Failed to read first index pointer from DBX file");
		return FALSE;
	}

	if (dbx_pread (m->dbx_fd, &itemcount, 4, ITEM_COUNT) != 4) {
		g_set_error (
			&m->base.error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
			"Failed to read item count from DBX file");
		return FALSE;
	}

	indexptr = GUINT32_FROM_LE (indexptr);
	m->index_count = itemcount = GUINT32_FROM_LE (itemcount);
	m->indices = g_malloc (itemcount * 4);

	d (printf ("indexptr %x, itemcount %d\n", indexptr, itemcount));

	if (indexptr && !dbx_load_index_table (m, indexptr, &index_ofs))
		return FALSE;

	d (printf ("Loaded %d of %d indices\n", index_ofs, m->index_count));

	if (index_ofs < m->index_count) {
		g_set_error (
			&m->base.error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
			"Corrupt DBX file: Seems to contain fewer than %d "
			"entries claimed in its header", m->index_count);
		return FALSE;
	}
	return TRUE;
}
Exemplo n.º 30
0
/**
 * mongo_bson_stream_next:
 * @stream: (in): A #MongoBsonStream.
 *
 * Gets the next #MongoBson document found in the stream.
 *
 * Returns: (transfer full): A #MongoBson if successful; otherwise %NULL.
 */
MongoBson *
mongo_bson_stream_next (MongoBsonStream *stream)
{
   MongoBson *bson;
   guint32 doc_len_le;
   guint32 doc_len;
   guint8 *buffer;

   g_return_val_if_fail(MONGO_IS_BSON_STREAM(stream), NULL);
   g_return_val_if_fail(stream->priv->stream ||
                        stream->priv->channel,
                        NULL);

   if (!mongo_bson_stream_read(stream,
                               (guint8 *)&doc_len_le,
                               sizeof doc_len_le,
                               sizeof doc_len_le)) {
      return NULL;
   }

   doc_len = GUINT32_FROM_LE(doc_len_le);

   /*
    * Sanity check to make sure it is less than 8mB and
    * greater than 5 bytes (minimum required).
    */
   if (doc_len > (1024 * 1024 * 8) || doc_len <= 5) {
      return NULL;
   }

   buffer = g_malloc(doc_len);
   memcpy(buffer, &doc_len_le, sizeof doc_len_le);

   /*
    * Read the rest of the BSON document into our buffer.
    */
   if (!mongo_bson_stream_read(stream,
                               buffer + sizeof doc_len_le,
                               doc_len,
                               doc_len - sizeof doc_len_le)) {
      return NULL;
   }

   if (!(bson = mongo_bson_new_from_data(buffer, doc_len))) {
      g_free(buffer);
      return NULL;
   }

   return bson;
}