Esempio n. 1
0
static gboolean
_load_v4(PersistState *self, gboolean load_all_entries)
{
  gint fd;
  gint64 file_size;
  gpointer map;
  gpointer key_block;
  guint32 key_size;
  PersistFileHeader *header;
  gint key_count, i;

  fd = open(self->commited_filename, O_RDONLY);
  if (fd < 0)
    {
      /* no previous data found */
      return TRUE;
    }

  file_size = lseek(fd, 0, SEEK_END);
  if (file_size > ((1LL << 31) - 1))
    {
      msg_error("Persistent file too large",
                evt_tag_str("filename", self->commited_filename),
                evt_tag_printf("size", "%" G_GINT64_FORMAT, file_size),
                NULL);
      return FALSE;
    }
  map = mmap(NULL, file_size, PROT_READ, MAP_SHARED, fd, 0);
  close(fd);
  if (map == MAP_FAILED)
    {
      msg_error("Error mapping persistent file into memory",
                evt_tag_str("filename", self->commited_filename),
                evt_tag_errno("error", errno),
                NULL);
      return FALSE;
    }
  header = (PersistFileHeader *) map;

  key_block = ((gchar *) map) + offsetof(PersistFileHeader, initial_key_store);
  key_size = sizeof((((PersistFileHeader *) NULL))->initial_key_store);

  key_count = GUINT32_FROM_BE(header->key_count);
  i = 0;
  while (i < key_count)
    {
      gchar *name;
      guint32 entry_ofs, chain_ofs;
      SerializeArchive *sa;

      sa = serialize_buffer_archive_new(key_block, key_size);
      while (i < key_count)
        {
          if (!serialize_read_cstring(sa, &name, NULL))
            {
              serialize_archive_free(sa);
              msg_error("Persistent file format error, unable to fetch key name",
                        NULL);
              goto free_and_exit;
            }
          if (name[0])
            {
              if (serialize_read_uint32(sa, &entry_ofs))
                {
                  PersistValueHeader *value_header;
                  i++;

                  if (entry_ofs < sizeof(PersistFileHeader) || entry_ofs > file_size)
                    {
                      serialize_archive_free(sa);
                      g_free(name);
                      msg_error("Persistent file format error, entry offset is out of bounds",
                                NULL);
                      goto free_and_exit;
                    }

                  value_header = (PersistValueHeader *) ((gchar *) map + entry_ofs - sizeof(PersistValueHeader));
                  if ((value_header->in_use) || load_all_entries)
                    {
                      gpointer new_block;
                      PersistEntryHandle new_handle;

                      new_handle = _alloc_value(self, GUINT32_FROM_BE(value_header->size), FALSE, value_header->version);
                      new_block = persist_state_map_entry(self, new_handle);
                      memcpy(new_block, value_header + 1, GUINT32_FROM_BE(value_header->size));
                      persist_state_unmap_entry(self, new_handle);
                      /* add key to the current file */
                      _add_key(self, name, new_handle);
                    }
                  g_free(name);
                }
              else
                {
                  /* bad format */
                  serialize_archive_free(sa);
                  g_free(name);
                  msg_error("Persistent file format error, unable to fetch key name",
                            NULL);
                  goto free_and_exit;
                }
            }
          else
            {
              g_free(name);
              if (serialize_read_uint32(sa, &chain_ofs))
                {
                  /* end of block, chain to the next one */
                  if (chain_ofs == 0 || chain_ofs > file_size)
                    {
                      msg_error("Persistent file format error, key block chain offset is too large or zero",
                                evt_tag_printf("key_block", "%08lx", (gulong) ((gchar *) key_block - (gchar *) map)),
                                evt_tag_printf("key_size", "%d", key_size),
                                evt_tag_int("ofs", chain_ofs),
                                NULL);
                      serialize_archive_free(sa);
                      goto free_and_exit;
                    }
                  key_block = ((gchar *) map) + chain_ofs;
                  key_size = GUINT32_FROM_BE(*(guint32 *) (((gchar *) key_block) - sizeof(PersistValueHeader)));
                  if (chain_ofs + key_size > file_size)
                    {
                      msg_error("Persistent file format error, key block size is too large",
                                evt_tag_int("key_size", key_size),
                                NULL);
                      serialize_archive_free(sa);
                      goto free_and_exit;
                    }
                  break;
                }
              else
                {
                  serialize_archive_free(sa);
                  msg_error("Persistent file format error, unable to fetch chained key block offset",
                            NULL);
                  goto free_and_exit;
                }
            }
        }
      serialize_archive_free(sa);
    }
 free_and_exit:
  munmap(map, file_size);
  return TRUE;
}
Esempio n. 2
0
/* Incrementally load the next chunk of data. */
static gboolean gdk_pixbuf__qtif_image_load_increment (gpointer data,
                                                       const guchar *buf, guint size,
                                                       GError **error)
{
    QTIFContext *context = (QTIFContext *)data;
    GError *tmp = NULL;
    gboolean ret = TRUE; /* Return TRUE for insufficient data. */

    while(ret && (size != 0u))
    {
        switch(context->state)
        {
        case STATE_READY:
            /* Abort if we have seen too mant atoms. */
            if(context->atom_count == 0u)
            {
                g_set_error_literal (error, GDK_PIXBUF_ERROR,
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                                     _("Failed to find an image data atom."));
                return FALSE;
            }
            context->atom_count--;

            /* Copy to header buffer in context, in case supplied data is not enough. */
            while(context->run_length < sizeof(QtHeader))
            {
                context->header_buffer[context->run_length] = *buf;
                context->run_length++;
                buf++;
                size--;
            }

            /* Parse buffer as QT header. */
            if(context->run_length == sizeof(QtHeader))
            {
                QtHeader *hdr = (QtHeader *)context->header_buffer;
                context->run_length = GUINT32_FROM_BE(hdr->length) - sizeof(QtHeader);

                /* Atom max size check. */
                if(context->run_length > ATOM_SIZE_MAX)
                {
                    g_set_error(error, GDK_PIXBUF_ERROR,
                                       GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                                       ngettext (  "QTIF atom size too large (%d byte)",
                                                   "QTIF atom size too large (%d bytes)",
                                                    hdr->length),
                                       hdr->length);
                    return FALSE;
                }

                /* Set state according to atom type. */
                if(GUINT32_FROM_BE(hdr->tag) == QTIF_TAG_IDATA)
                {
                    GError *tmp = NULL;

                    context->state = STATE_DATA;

                    /* Create GdkPixbufLoader for this image data. */
                    ret = gdk_pixbuf__qtif_image_create_loader(context, &tmp);
                    if(!ret)
                    {
                        g_propagate_error (error, tmp);
                    }
                }
                else
                {
                    context->state = STATE_OTHER;
                }
            }
            break;

        default: /* Both STATE_DATA and STATE_OTHER will come here. */
            /* Check for atom boundary. */
            if(context->run_length > size)
            {
                /* Supply image data to GdkPixbufLoader if in STATE_DATA. */
                if(context->state == STATE_DATA)
                {
                    tmp = NULL;
                    ret = gdk_pixbuf_loader_write(context->loader, buf, size, &tmp);
                    if(!ret && (error != NULL) && (*error == NULL))
                    {
                        g_propagate_error (error, tmp);
                    }
                }
                context->run_length -= size;
                size = 0u;
            }
            else
            {
                /* Supply image data to GdkPixbufLoader if in STATE_DATA. */
                if(context->state == STATE_DATA)
                {
                    gboolean r;

                    /* Here we should have concluded a complete image atom. */
                    tmp = NULL;
                    ret = gdk_pixbuf_loader_write(context->loader, buf, context->run_length, &tmp);
                    if(!ret && (error != NULL) && (*error == NULL))
                    {
                        g_propagate_error (error, tmp);
                    }

                    /* Free GdkPixbufLoader and handle callback. */
                    tmp = NULL;
                    r = gdk_pixbuf__qtif_image_free_loader(context, &tmp);
                    if(!r)
                    {
                        if((error != NULL) && (*error == NULL))
                        {
                            g_propagate_error (error, tmp);
                        }
                        ret = FALSE;
                    }
                }
                buf = &buf[context->run_length];
                size -= context->run_length;
                context->run_length = 0u;
                context->state = STATE_READY;
            }
            break;
        }
    }

    return ret;
}
Esempio n. 3
0
/*
 * NOTE: can only be called from the main thread (e.g. log_pipe_init/deinit).
 */
static gboolean
_add_key(PersistState *self, const gchar *key, PersistEntryHandle handle)
{
  PersistEntry *entry;
  gpointer key_area;
  gboolean new_block_created = FALSE;
  SerializeArchive *sa;

  g_assert(key[0] != 0);

  entry = g_new(PersistEntry, 1);
  entry->ofs = handle;
  g_hash_table_insert(self->keys, g_strdup(key), entry);

  /* we try to insert the key into the current block first, then if it
     doesn't fit, we create a new block */

  while (1)
    {
      /* the size of the key block chain part, 4 byte for the empty string length, guint32 for the link to the next block */
      guint32 chain_size = sizeof(guint32) + sizeof(guint32);
      gboolean success;

      key_area = persist_state_map_entry(self, self->current_key_block);

      /* we reserve space for the next area pointer */
      sa = serialize_buffer_archive_new(key_area + self->current_key_ofs, self->current_key_size - self->current_key_ofs - chain_size);
      sa->silent = TRUE;
      success =
        serialize_write_cstring(sa, key, -1) &&
        serialize_write_uint32(sa, handle);

      if (!success)
        {
          serialize_archive_free(sa);
          if (!new_block_created)
            {
              PersistEntryHandle new_block;

              /* we unmap the key_area as otherwise we can't grow because of the pending maps */
              persist_state_unmap_entry(self, self->current_key_block);

              /* ah, we couldn't fit into the current block, create a new one and link it off the old one */
              new_block = _alloc_value(self, PERSIST_STATE_KEY_BLOCK_SIZE, TRUE, 0);
              if (!new_block)
                {
                  msg_error("Unable to allocate space in the persistent file for key store",
                            NULL);
                  return FALSE;
                }

              key_area = persist_state_map_entry(self, self->current_key_block);
              sa = serialize_buffer_archive_new(key_area + self->current_key_ofs, self->current_key_size - self->current_key_ofs);
              if (!serialize_write_cstring(sa, "", 0) ||
                  !serialize_write_uint32(sa, new_block))
                {
                  /* hmmm. now this is bad, we couldn't write the tail of the
                     block even though we always reserved space for it, this
                     is a programming error somewhere in this function. */
                  g_assert_not_reached();
                }
              serialize_archive_free(sa);
              persist_state_unmap_entry(self, self->current_key_block);
              self->current_key_block = new_block;
              self->current_key_size = PERSIST_STATE_KEY_BLOCK_SIZE;
              self->current_key_ofs = 0;
              new_block_created = TRUE;
            }
          else
            {
              /* if this happens, that means that the current key
               * entry won't fit even into a freshly initialized key
               * block, this means that the key is too large. */
              msg_error("Persistent key too large, it cannot be larger than somewhat less than 4k",
                        evt_tag_str("key", key),
                        NULL);
              persist_state_unmap_entry(self, self->current_key_block);
              return FALSE;
            }
        }
      else
        {
          self->header->key_count = GUINT32_TO_BE(GUINT32_FROM_BE(self->header->key_count) + 1);
          self->current_key_ofs += serialize_buffer_archive_get_pos(sa);
          serialize_archive_free(sa);
          persist_state_unmap_entry(self, self->current_key_block);
          return TRUE;
        }
    }
  g_assert_not_reached();
}
Esempio n. 4
0
WhsPattern *
whs_pattern_load (const gchar *filename)
{
  g_return_val_if_fail (filename != NULL && *filename != '\0', NULL);
  
  FILE *f = g_fopen (filename, "rb");
  size_t ret;

  if (!f) {
    g_warning ("Can't open file");
    return NULL;
  }

  // Header
  gchar header[4];
  if ((ret = fread (&header, 1, 4, f)) < 4) {
    if (ret >= 0)
      g_warning ("Read only %d of 4 bytes", ret);
    else
      g_warning ("Read failed: %s", strerror (ret));
    fclose (f);
    return NULL;
  }

  if (strncmp (header, "WHSP", 4) != 0) {
    g_warning ("Not a valid pattern file");
    fclose (f);
    return NULL;
  }
 
  // Min and max frequency
  guint32 min_freq, max_freq;
  if ((ret = fread (&min_freq, 1, 4, f)) < 4) {
    if (ret >= 0)
      g_warning ("Read only %d of 4 bytes", ret);
    else
      g_warning ("Read failed: %s", strerror (ret));
    fclose (f);
    return NULL;
  }
  min_freq = GUINT32_FROM_BE (min_freq);

  if ((ret = fread (&max_freq, 1, 4, f)) < 4) {
    if (ret >= 0)
      g_warning ("Read only %d of 4 bytes", ret);
    else
      g_warning ("Read failed: %s", strerror (ret));
    fclose (f);
    return NULL;
  }
  max_freq = GUINT32_FROM_BE (max_freq);

  // Sample rate
  guint32 sample_rate;
  if ((ret = fread (&sample_rate, 1, 4, f)) < 4) {
    if (ret >= 0)
      g_warning ("Read only %d of 4 bytes", ret);
    else
      g_warning ("Read failed: %s", strerror (ret));
    fclose (f);
    return NULL;
  }
  sample_rate = GUINT32_FROM_BE (sample_rate);

  // Classifier name size
  guint32 size;
  if ((ret = fread (&size, 1, 4, f)) < 4) {
    if (ret >= 0)
      g_warning ("Read only %d of 4 bytes", ret);
    else
      g_warning ("Read failed: %s", strerror (ret));
    fclose (f);
    return NULL;
  }
  size = GUINT32_FROM_BE (size);

  if (size <= 0) {
    g_warning ("No classifier name");
    fclose (f);
    return NULL;
  }

  // Classifier name
  gchar *classifier = g_new0 (gchar, size);
  if ((ret = fread (classifier, 1, size, f)) < size) {
    if (ret >= 0)
      g_warning ("Read only %d of %d bytes", ret, size);
    else
      g_warning ("Read failed: %s", strerror (ret));

    g_free (classifier);
    fclose (f);
    return NULL;
  }

  // Classifier data size
  if ((ret = fread (&size, 1, 4, f)) < 4) {
    if (ret >= 0)
      g_warning ("Read only %d of 4 bytes", ret);
    else
      g_warning ("Read failed: %s", strerror (ret));
    g_free (classifier);
    fclose (f);
    return NULL;
  }
  size = GUINT32_FROM_BE (size);

  if (size <= 0) {
    g_warning ("No classifier data");
    fclose (f);
    g_free (classifier);
    return NULL;
  }

  // Classifier data
  guint8 *data = g_new0 (guint8, size);
  if ((ret = fread (data, 1, size, f)) < size) {
    if (ret >= 0)
      g_warning ("Read only %d of %d bytes", ret, size);
    else
      g_warning ("Read failed: %s", strerror (ret));

    g_free (classifier);
    g_free (data);
    fclose (f);
    return NULL;
  }


  WhsPattern *self = WHS_PATTERN_CAST (g_type_create_instance (WHS_TYPE_PATTERN));

  self->priv->classifier = classifier;
  self->priv->classifier_data = data;
  self->priv->size = size;
  self->priv->min_freq = min_freq;
  self->priv->max_freq = max_freq;
  self->priv->sample_rate = sample_rate;

  fclose (f);

  return self;
}
Esempio n. 5
0
/* Load QTIF from a file handler. */
static GdkPixbuf *gdk_pixbuf__qtif_image_load (FILE *f, GError **error)
{
    guint count;

    if(f == NULL)
    {
        g_set_error_literal (error, GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_BAD_OPTION,
                             _("Input file descriptor is NULL."));
        return NULL;
    }

    for(count = QTIF_ATOM_COUNT_MAX; count != 0u; count--)
    {
        QtHeader hdr;
        size_t rd;

        /* Read QtHeader. */
        rd = fread(&hdr, 1, sizeof(QtHeader), f);
        if(rd != sizeof(QtHeader))
        {
            g_set_error_literal(error, GDK_PIXBUF_ERROR,
                                GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                                _("Failed to read QTIF header"));
            return NULL;
        }

        hdr.length = GUINT32_FROM_BE(hdr.length) - sizeof(QtHeader);
        if(hdr.length > ATOM_SIZE_MAX)
        {
            g_set_error(error, GDK_PIXBUF_ERROR,
                        GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                        ngettext (  "QTIF atom size too large (%d byte)",
                                    "QTIF atom size too large (%d bytes)",
                                    hdr.length),
                        hdr.length);
            return NULL;
        }

        switch(GUINT32_FROM_BE(hdr.tag))
        {
        case QTIF_TAG_IDATA: /* "idat" data atom. */
            {
                /* Load image using GdkPixbufLoader. */
                guchar *buf;
                GdkPixbufLoader *loader;
                GdkPixbuf *pixbuf = NULL;
                GError *tmp = NULL;

                /* Allocate read buffer. */
                buf = g_try_malloc(READ_BUFFER_SIZE);
                if(buf == NULL)
                {
                    g_set_error(error, GDK_PIXBUF_ERROR,
                                GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
                                ngettext ( "Failed to allocate %d byte for file read buffer",
                                           "Failed to allocate %d bytes for file read buffer",
                                           READ_BUFFER_SIZE
                                ),
                                READ_BUFFER_SIZE);
                    return NULL;
                }

                /* Create GdkPixbufLoader. */
                loader = gdk_pixbuf_loader_new();
                if(loader == NULL)
                {
                    g_set_error(error, GDK_PIXBUF_ERROR,
                                GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                                ngettext (  "QTIF atom size too large (%d byte)",
                                            "QTIF atom size too large (%d bytes)",
                                            hdr.length),
                                hdr.length);
                    goto clean_up;
                }

                /* Read atom data. */
                while(hdr.length != 0u)
                {
                    rd = (hdr.length > READ_BUFFER_SIZE) ? READ_BUFFER_SIZE : hdr.length;

                    rd = fread(buf, 1, rd, f);
                    if(rd < 0)
                    {
                        g_set_error(error, GDK_PIXBUF_ERROR,
                                    GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                                    _("File error when reading QTIF atom: %s"), g_strerror(errno));
                        break;
                    }

                    if(!gdk_pixbuf_loader_write(loader, buf, rd, &tmp))
                    {
                        g_propagate_error (error, tmp);
                        break;
                    }
                    hdr.length -= rd;
                }

clean_up:
                /* Release loader */
                if(loader != NULL)
                {
                    gdk_pixbuf_loader_close(loader, NULL);
                    pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
                    if(pixbuf != NULL)
                    {
                        g_object_ref(pixbuf);
                    }
                    g_object_unref(loader);
                }
                if(buf != NULL)
                {
                    g_free(buf);
                }
                return pixbuf;
            }

        default:
            /* Skip any other types of atom. */
            if(!fseek(f, hdr.length, SEEK_CUR))
            {
                g_set_error(error, GDK_PIXBUF_ERROR,
                            GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                            ngettext (  "Failed to skip the next %d byte with seek().",
                                        "Failed to skip the next %d bytes with seek().",
                                        hdr.length),
                            hdr.length);
                return NULL;
            }
            break;
        }
    }
    return NULL;
}
Esempio n. 6
0
static gboolean RAS2State(struct rasterfile *RAS,
			  struct ras_progressive_state *State,
			  GError **error)
{
	State->Header.width = GUINT32_FROM_BE(RAS->width);
	State->Header.height = GUINT32_FROM_BE(RAS->height);
	State->Header.depth = GUINT32_FROM_BE(RAS->depth);
	State->Header.type = GUINT32_FROM_BE(RAS->type);
	State->Header.maptype = GUINT32_FROM_BE(RAS->maptype);
	State->Header.maplength = GUINT32_FROM_BE(RAS->maplength);

	if ((gint)State->Header.width <= 0 ||
	    (gint)State->Header.height <= 0 || 
	    State->Header.maplength > 768) {
		g_set_error_literal (error,
                                     GDK_PIXBUF_ERROR,
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                                     _("RAS image has bogus header data"));
		return FALSE;
	}

	State->RasType = State->Header.depth;	/* This may be less trivial someday */
	State->HeaderSize = 32 + State->Header.maplength;

	if (State->RasType == 32)
		State->LineWidth = State->Header.width * 4;
	else if (State->RasType == 24)
		State->LineWidth = State->Header.width * 3;
	else if (State->RasType == 8)
		State->LineWidth = State->Header.width * 1;
	else if (State->RasType == 1) {
		State->LineWidth = State->Header.width / 8;
		if ((State->Header.width & 7) != 0)
			State->LineWidth++;
	}
	else {
		g_set_error_literal (error,
                                     GDK_PIXBUF_ERROR,
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                                     _("RAS image has unknown type"));
		return FALSE;
	}

	if (State->Header.type > 2 || State->Header.maptype > 1) {
		g_set_error_literal (error,
                                     GDK_PIXBUF_ERROR,
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                                     _("unsupported RAS image variation"));
		return FALSE;
	}

	/* Now pad the line to be a multiple of 16 bits */
	if ((State->LineWidth & 1) != 0)
		State->LineWidth++;

	if (!State->LineBuf) {
		State->LineBuf = g_try_malloc (State->LineWidth);

		if (!State->LineBuf) {
			g_set_error_literal (error,
                                             GDK_PIXBUF_ERROR,
                                             GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
                                             _("Not enough memory to load RAS image"));
			return FALSE;
		}
	}


	if (!State->pixbuf) {
		if (State->size_func) {
			gint width = State->Header.width;
			gint height = State->Header.height;
			
			(*State->size_func) (&width, &height, State->user_data);
			if (width == 0 || height == 0)
				return FALSE;
		}

		if (State->RasType == 32)
			State->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
							(gint) State->Header.width,
							(gint) State->Header.height);
		else
			State->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
							(gint) State->Header.width,
							(gint) State->Header.height);
		
                if (!State->pixbuf) {
                        g_set_error_literal (error,
                                             GDK_PIXBUF_ERROR,
                                             GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
                                             _("Not enough memory to load RAS image"));
                        return FALSE;
                }

		if (State->prepared_func != NULL)
			/* Notify the client that we are ready to go */
			(*State->prepared_func) (State->pixbuf,
                                                 NULL,
						 State->user_data);

	}
	
	if ((State->Header.maplength == 0) && (State->RasType == 1)) {
		State->HeaderBuf[32] = 255;
		State->HeaderBuf[33] = 0;
		State->HeaderBuf[34] = 255;
		State->HeaderBuf[35] = 0;
		State->HeaderBuf[36] = 255;
		State->HeaderBuf[37] = 0;
	}

	return TRUE;
}
Esempio n. 7
0
void
msn_message_parse_payload(MsnMessage *msg,
						  const char *payload, size_t payload_len)
{
	char *tmp_base, *tmp;
	const char *content_type;
	char *end;
	char **elems, **cur, **tokens;

	g_return_if_fail(payload != NULL);

	tmp_base = tmp = g_malloc0(payload_len + 1);
	memcpy(tmp_base, payload, payload_len);

	/* Parse the attributes. */
	end = strstr(tmp, "\r\n\r\n");
	/* TODO? some clients use \r delimiters instead of \r\n, the official client
	 * doesn't send such messages, but does handle receiving them. We'll just
	 * avoid crashing for now */
	if (end == NULL) {
		g_free(tmp_base);
		g_return_if_reached();
	}
	*end = '\0';

	elems = g_strsplit(tmp, "\r\n", 0);

	for (cur = elems; *cur != NULL; cur++)
	{
		const char *key, *value;

		tokens = g_strsplit(*cur, ": ", 2);

		key = tokens[0];
		value = tokens[1];

		if (!strcmp(key, "MIME-Version"))
		{
			g_strfreev(tokens);
			continue;
		}

		if (!strcmp(key, "Content-Type"))
		{
			char *charset, *c;

			if ((c = strchr(value, ';')) != NULL)
			{
				if ((charset = strchr(c, '=')) != NULL)
				{
					charset++;
					msn_message_set_charset(msg, charset);
				}

				*c = '\0';
			}

			msn_message_set_content_type(msg, value);
		}
		else
		{
			msn_message_set_attr(msg, key, value);
		}

		g_strfreev(tokens);
	}

	g_strfreev(elems);

	/* Proceed to the end of the "\r\n\r\n" */
	tmp = end + 4;

	/* Now we *should* be at the body. */
	content_type = msn_message_get_content_type(msg);

	if (content_type != NULL &&
		!strcmp(content_type, "application/x-msnmsgrp2p"))
	{
		MsnSlpHeader header;
		MsnSlpFooter footer;
		int body_len;

		if (payload_len - (tmp - tmp_base) < sizeof(header)) {
			g_free(tmp_base);
			g_return_if_reached();
		}

		msg->msnslp_message = TRUE;

		/* Import the header. */
		memcpy(&header, tmp, sizeof(header));
		tmp += sizeof(header);

		msg->msnslp_header.session_id = GUINT32_FROM_LE(header.session_id);
		msg->msnslp_header.id         = GUINT32_FROM_LE(header.id);
		msg->msnslp_header.offset     = GUINT64_FROM_LE(header.offset);
		msg->msnslp_header.total_size = GUINT64_FROM_LE(header.total_size);
		msg->msnslp_header.length     = GUINT32_FROM_LE(header.length);
		msg->msnslp_header.flags      = GUINT32_FROM_LE(header.flags);
		msg->msnslp_header.ack_id     = GUINT32_FROM_LE(header.ack_id);
		msg->msnslp_header.ack_sub_id = GUINT32_FROM_LE(header.ack_sub_id);
		msg->msnslp_header.ack_size   = GUINT64_FROM_LE(header.ack_size);

		body_len = payload_len - (tmp - tmp_base) - sizeof(footer);

		/* Import the body. */
		if (body_len > 0) {
			msg->body_len = body_len;
			msg->body = g_malloc0(msg->body_len + 1);
			memcpy(msg->body, tmp, msg->body_len);
			tmp += body_len;
		}

		/* Import the footer. */
		if (body_len >= 0) {
			memcpy(&footer, tmp, sizeof(footer));
			tmp += sizeof(footer);
			msg->msnslp_footer.value = GUINT32_FROM_BE(footer.value);
		}
	}
	else
	{
		if (payload_len - (tmp - tmp_base) > 0) {
			msg->body_len = payload_len - (tmp - tmp_base);
			msg->body = g_malloc0(msg->body_len + 1);
			memcpy(msg->body, tmp, msg->body_len);
		}
	}

	g_free(tmp_base);
}
Esempio n. 8
0
static PartitionTable *
part_table_parse_apple (int fd, guint64 offset, guint64 size)
{
	int n;
	PartitionTable *p;
	struct {
		guint16 signature;
		guint16 block_size;
		guint32 block_count;
		/* more stuff */
	} __attribute__ ((packed)) mac_header;
	struct {
		guint16 signature;
		guint16 res1;
		guint32 map_count;
		guint32 start_block;
		guint32 block_count;
		char name[32];
		char type[32];
		guint32 data_start;
		guint32 data_count;
		guint32 status;
		guint32 boot_start;
		guint32 boot_size;
		guint32 boot_load;
		guint32 boot_load2;
		guint32 boot_entry;
		guint32 boot_entry2;
		guint32 boot_cksum;
		char processor[16]; /* identifies ISA of boot */
		/* more stuff */
	} __attribute__ ((packed)) mac_part;
	int block_size;
	int block_count;
	int map_count;

	HAL_INFO (("Entering Apple parser"));

	p = NULL;

	/* Check Mac start of disk signature */
	if (lseek (fd, offset + 0, SEEK_SET) < 0) {
		HAL_INFO (("lseek failed (%s)", strerror (errno)));
		goto out;
	}
	if (read (fd, &mac_header, sizeof (mac_header)) != sizeof (mac_header)) {
		HAL_INFO (("read failed (%s)", strerror (errno)));
		goto out;
	}
	if (memcmp (&(mac_header.signature), MAC_MAGIC, 2) != 0) {
		HAL_INFO (("No MAC_MAGIC found"));
		goto out;
	}

	block_size = GUINT16_FROM_BE (mac_header.block_size);
	block_count = GUINT32_FROM_BE (mac_header.block_count); /* num blocks on whole disk */

	HAL_INFO (("Mac MAGIC found, block_size=%d", block_size));

	p = part_table_new_empty (PART_TYPE_APPLE);
	p->offset = offset;
	p->size = size;

	/* get number of entries from first entry   */
	if (lseek (fd, offset + block_size, SEEK_SET) < 0) {
		HAL_INFO (("lseek failed (%s)", strerror (errno)));
		goto out;
	}
	if (read (fd, &mac_part, sizeof (mac_part)) != sizeof (mac_part)) {
		HAL_INFO (("read failed (%s)", strerror (errno)));
		goto out;
	}
	map_count = GUINT32_FROM_BE (mac_part.map_count); /* num blocks in part map */

	HAL_INFO (("map_count = %d", map_count));

	for (n = 0; n < map_count; n++) {
		PartitionEntry *pe;

		if (memcmp (&(mac_part.signature), MAC_PART_MAGIC, 2) != 0) {
			HAL_INFO (("No MAC_PART_MAGIC found"));
			break;
		}

		if (lseek (fd, offset + (n + 1) * block_size, SEEK_SET) < 0) {
			HAL_INFO (("lseek failed (%s)", strerror (errno)));
			goto out;
		}
		if (read (fd, &mac_part, sizeof (mac_part)) != sizeof (mac_part)) {
			HAL_INFO (("read failed (%s)", strerror (errno)));
			goto out;
		}

		pe = part_entry_new (NULL,
				     (guint8*) &mac_part,
				     sizeof (mac_part), 
				     offset + (n + 1) * block_size);
		p->entries = g_slist_append (p->entries, pe);
		
	}

out:
	HAL_INFO (("Leaving Apple parser"));
	return p;
}
Esempio n. 9
0
static guint32
get_be32 (const void *buf)
{
	return GUINT32_FROM_BE ( * ((guint32 *) buf) );
}
Esempio n. 10
0
static inline guint32 guint32_from_be (const guint32_be be) {
    guint32 tmp;
    memcpy (&tmp, &be, sizeof tmp);
    return GUINT32_FROM_BE (tmp);
}
Esempio n. 11
0
uint32_t rd_long(uint8_t *p)
{
	uint32_t *p32 = (uint32_t *)p;
	return GUINT32_FROM_BE(*p32);
}
Esempio n. 12
0
static gpointer
open_thread_func (gpointer user_data)
{
  SyncData *data = user_data;
  GVfsAfpConnection *conn = data->conn;
  GVfsAfpConnectionPrivate *priv = conn->priv;

  GSocketClient *client;

  guint16 req_id;
  gboolean res = FALSE;
  char *reply;
  DSIHeader dsi_header;
  guint pos;

  client = g_socket_client_new ();
  priv->stream = G_IO_STREAM (g_socket_client_connect (client, priv->addr, data->cancellable,
                                                       data->error));
  g_object_unref (client);

  if (!priv->stream)
    goto out;

  req_id = get_request_id (conn);
  res = send_request_sync (g_io_stream_get_output_stream (priv->stream),
                           DSI_OPEN_SESSION, req_id, 0,  0, NULL,
                           data->cancellable, data->error);
  if (!res)
    goto out;

  res = read_reply_sync (g_io_stream_get_input_stream (priv->stream),
                         &dsi_header, &reply, data->cancellable, data->error);
  if (!res)
    goto out;

  pos = 0;
  while ((dsi_header.totalDataLength - pos) > 2)
  {
    guint8 optionType;
    guint8 optionLength;

    optionType = reply[pos++];
    optionLength = reply[pos++];

    switch (optionType)
    {
      
      case 0x00:
        if (optionLength == 4 && (dsi_header.totalDataLength - pos) >= 4)
          priv->kRequestQuanta = GUINT32_FROM_BE (*(guint32 *)(reply + pos));

        break;
        
      case 0x02:
        if (optionLength == 4 && (dsi_header.totalDataLength - pos) >= 4)
         priv->kServerReplayCacheSize = GUINT32_FROM_BE (*(guint32 *)(reply + pos));

        break;
      

      default:
        g_debug ("Unknown DSI option\n");
    }

    pos += optionLength;
  }
  g_free (reply);

out:
  if (res)
    g_atomic_int_set (&priv->atomic_state, STATE_CONNECTED);
  
  /* Signal sync call thread */
  data->res = res;
  sync_data_signal (data);

  /* Return from thread on failure */
  if (!res)
  {
    g_clear_object (&priv->stream);
    return NULL;
  }
  
  /* Create MainLoop */
  priv->worker_context = g_main_context_new ();
  priv->worker_loop = g_main_loop_new (priv->worker_context, TRUE);

  read_reply (conn);
  
  /* Run mainloop */
  g_main_loop_run (priv->worker_loop);

  return NULL;
}