コード例 #1
0
ファイル: mux-message-part.c プロジェクト: djcb/mux
static GMimeStream*
get_mime_stream (GMimeObject *obj, GError **err)
{
	GMimeStream		*mstream;
	GMimeDataWrapper	*wrapper;

	if (!GMIME_IS_PART(obj)) {
		char		*str;
		GByteArray	*ar;

		str = g_mime_object_to_string (obj);
		ar  = g_byte_array_new_take ((guchar*)str, strlen(str));
		
		mstream = g_mime_stream_mem_new_with_byte_array (ar);
		g_mime_stream_mem_set_owner (
			GMIME_STREAM_MEM(mstream), TRUE);

		return mstream;
	}

	wrapper = get_data_wrapper (obj, err);
	if (!wrapper)
		return NULL;

	mstream = g_mime_data_wrapper_get_stream (wrapper);
	if (!mstream || g_mime_stream_reset (mstream) != 0) {
		g_set_error (err, G_IO_ERROR, G_IO_ERROR_FAILED,
			     "invalid mime-stream");
		return NULL;
	}

	return mstream;
}
コード例 #2
0
ファイル: mongo-bson.c プロジェクト: codebutler/mongo-glib
MongoBson *
mongo_bson_new_take_data (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_new_take(buffer, length);

   return bson;
}
コード例 #3
0
ファイル: gbytes.c プロジェクト: 492852238/SourceLearning
/**
 * g_bytes_unref_to_array:
 * @bytes: (transfer full): a #GBytes
 *
 * Unreferences the bytes, and returns a new mutable #GByteArray containing
 * the same byte data.
 *
 * As an optimization, the byte data is transferred to the array without copying
 * if this was the last reference to bytes and bytes was created with
 * g_bytes_new(), g_bytes_new_take() or g_byte_array_free_to_bytes(). In all
 * other cases the data is copied.
 *
 * Returns: (transfer full): a new mutable #GByteArray containing the same byte data
 *
 * Since: 2.32
 */
GByteArray *
g_bytes_unref_to_array (GBytes *bytes)
{
  gpointer data;
  gsize size;

  g_return_val_if_fail (bytes != NULL, NULL);

  data = g_bytes_unref_to_data (bytes, &size);
  return g_byte_array_new_take (data, size);
}
コード例 #4
0
static void ovirt_proxy_set_mock_ca(OvirtProxy *proxy)
{
    gchar *data;
    gsize size;
    GByteArray *cacert;

    g_file_get_contents(abs_srcdir "/https-cert/ca-cert.pem", &data, &size, NULL);
    cacert = g_byte_array_new_take((guint8 *)data, size);
    g_object_set(proxy, "ca-cert", cacert, NULL);
    g_byte_array_unref(cacert);
}
コード例 #5
0
static MMPortSerialResponseType
parse_response (MMPortSerial *port,
                GByteArray *response,
                GByteArray **parsed_response,
                GError **error)
{
    MMPortSerialAt *self = MM_PORT_SERIAL_AT (port);
    GString *string;
    gsize parsed_len;
    GError *inner_error = NULL;

    g_return_val_if_fail (self->priv->response_parser_fn != NULL, FALSE);

    /* Remove echo */
    if (self->priv->remove_echo)
        mm_port_serial_at_remove_echo (response);

    /* If there's no response to receive, we're done; e.g. if we only got
     * unsolicited messages */
    if (!response->len)
        return MM_PORT_SERIAL_RESPONSE_NONE;

    /* Construct the string that AT-parsing functions expect */
    string = g_string_sized_new (response->len + 1);
    g_string_append_len (string, (const char *) response->data, response->len);

    /* Fully cleanup the response array, we'll consider the contents we got
     * as the full reply that the command may expect. */
    g_byte_array_remove_range (response, 0, response->len);

    /* Parse it; returns FALSE if there is nothing we can do with this
     * response yet. */
    if (!self->priv->response_parser_fn (self->priv->response_parser_user_data, string, &inner_error)) {
        /* Copy what we got back in the response buffer. */
        g_byte_array_append (response, (const guint8 *) string->str, string->len);
        g_string_free (string, TRUE);
        return MM_PORT_SERIAL_RESPONSE_NONE;
    }

    /* If we got an error, propagate it without any further response string */
    if (inner_error) {
        g_propagate_error (error, inner_error);
        return MM_PORT_SERIAL_RESPONSE_ERROR;
    }

    /* Otherwise, build a new GByteArray considered as parsed response */
    parsed_len = string->len;
    *parsed_response = g_byte_array_new_take ((guint8 *) g_string_free (string, FALSE), parsed_len);
    return MM_PORT_SERIAL_RESPONSE_BUFFER;
}
コード例 #6
0
ファイル: askpass.c プロジェクト: andreasn/cockpit
static JsonObject *
read_control_message (int fd)
{
  JsonObject *options = NULL;
  GBytes *payload = NULL;
  GBytes *bytes = NULL;
  gchar *channel = NULL;
  guchar *data = NULL;
  gssize length = 0;

  length = cockpit_frame_read (fd, &data);
  if (length < 0)
    {
      g_message ("couldn't read askpass authorize message: %s", g_strerror (errno));
      length = 0;
    }
  else if (length > 0)
    {
      /* This could have a password, so clear it when freeing */
      bytes = g_bytes_new_with_free_func (data, length, byte_array_clear_and_free,
                                          g_byte_array_new_take (data, length));
      payload = cockpit_transport_parse_frame (bytes, &channel);
      data = NULL;
    }

  if (payload == NULL)
    {
      if (length > 0)
        g_message ("askpass did not receive valid message");
    }
  else if (channel != NULL)
    {
      g_message ("askpass did not receive a control message");
    }
  else if (!cockpit_transport_parse_command (payload, NULL, NULL, &options))
    {
      g_message ("askpass did not receive a valid control message");
    }

  g_free (channel);

  if (bytes)
    g_bytes_unref (bytes);
  if (payload)
    g_bytes_unref (payload);
  free (data);
  return options;
}
コード例 #7
0
ファイル: msg.c プロジェクト: spk121/jozabad
JzMsg *
jz_msg_new_data (guint16 lcn, guint8 q, guint16 pr, guint16 ps, guint8 *data, gsize len)
{
  JzMsg *self = g_new0(JzMsg, 1);

  MSG_HEADER(self);
  self->id = JZ_MSG_DATA;
  self->lcn = lcn;
  self->q = q;
  self->pr = pr;
  self->ps = ps;
  self->data = g_byte_array_new_take (data, len);
  MSG_ERROR_CHECK(self);
  g_byte_array_ref (self->data);
  return self;
}
コード例 #8
0
ファイル: msg.c プロジェクト: spk121/jozabad
JzMsg *
jz_msg_new_call_accepted (guint16 lcn, gchar *calling_address, gchar *called_address, packet_t packet,
						  guint16 window, tput_t throughput, guint8 *data, gsize len)
{
  JzMsg *self = g_new0(JzMsg, 1);

  MSG_HEADER (self);
  self->id = JZ_MSG_CALL_ACCEPTED;
  self->lcn = lcn;
  self->calling_address = g_strdup (calling_address);
  self->called_address = g_strdup (called_address);
  self->packet = packet;
  self->window = window;
  self->throughput = throughput;
  self->data = g_byte_array_new_take (data, len);
  MSG_ERROR_CHECK(self);
  return self;
}