static GdkPixbuf*
dn_data_url_to_pixbuf(const NPVariant *data_url)
{
    GdkPixbuf *pixbuf = NULL;
    GInputStream *stream;
    gsize len;
    gchar *buffer, *data;
    GError *err = NULL;

    buffer = variant_to_string (data_url);
    if (buffer == NULL)
        return NULL;

    g_debug ("%s(\"%s\")", G_STRFUNC, buffer);

    if (!g_str_has_prefix (buffer, "data:image/png;base64,"))
        goto out;

    /* skip the mime type prefix */
    data = buffer + 22;

    g_base64_decode_inplace (data, &len);
    stream = g_memory_input_stream_new_from_data (data, len, NULL);
    pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, &err);
    if (err != NULL)
      {
        g_debug ("%s error: %s", G_STRFUNC, err->message);
        g_error_free (err);
      }
out:
    g_free (buffer);
    return pixbuf;
}
示例#2
0
static char *
rsvg_acquire_data_data (const char *uri,
                        const char *base_uri, 
                        char **out_mime_type,
                        gsize *out_len,
                        GError **error)
{
    const char *comma, *start, *end;
    char *mime_type;
    char *data;
    gsize data_len;
    gboolean base64 = FALSE;

    g_assert (out_len != NULL);
    g_assert (strncmp (uri, "data:", 5) == 0);

    mime_type = NULL;
    start = uri + 5;
    comma = strchr (start, ',');

    if (comma && comma != start) {
        /* Deal with MIME type / params */
        if (comma > start + BASE64_INDICATOR_LEN && 
            !g_ascii_strncasecmp (comma - BASE64_INDICATOR_LEN, BASE64_INDICATOR, BASE64_INDICATOR_LEN)) {
            end = comma - BASE64_INDICATOR_LEN;
            base64 = TRUE;
        } else {
            end = comma;
        }

        if (end != start) {
            mime_type = uri_decoded_copy (start, end - start);
        }
    }

    if (comma)
        start = comma + 1;

    if (*start) {
	data = uri_decoded_copy (start, strlen (start));

        if (base64)
            data = (char *) g_base64_decode_inplace (data, &data_len);
        else
            data_len = strlen (data);
    } else {
        data = NULL;
        data_len = 0;
    }

    if (out_mime_type)
        *out_mime_type = mime_type;
    else
        g_free (mime_type);

    *out_len = data_len;
    return data;
}
示例#3
0
static char *
base64_decode_string (const char *enc)
{
  if (enc == NULL)
    return NULL;

  char *dec = g_strdup (enc);
  gsize len;
  g_base64_decode_inplace (dec, &len);
  dec[len] = '\0';
  return dec;
}
示例#4
0
文件: http.c 项目: Amelos/moloch
void
moloch_http_parse_authorization(MolochSession_t *session, char *str)
{
    gsize olen;

    while (isspace(*str)) str++;

    char *space = strchr(str, ' ');

    if (!space)
        return;

    char *lower = g_ascii_strdown(str, space-str);
    if (!moloch_field_string_add(atField, session, lower, space-str, FALSE)) {
        g_free(lower);
    }

    if (strncasecmp("basic", str, 5) == 0) {
        str += 5;
        while (isspace(*str)) str++;

        // Yahoo reused Basic
        if (memcmp("token=", str, 6) != 0) {
            g_base64_decode_inplace(str, &olen);
            char *colon = strchr(str, ':');
            if (colon)
                *colon = 0;
            moloch_field_string_add(userField, session, str, -1, TRUE);
        }
    } else if (strncasecmp("digest", str, 6) == 0) {
        str += 5;
        while (isspace(*str)) str++;

        char *username = strstr(str, "username");
        if (!username) return;
        str = username + 8;
        while (isspace(*str)) str++;
        if (*str != '=') return;
        str++; // equal
        while (isspace(*str)) str++;

        int quote = 0;
        if (*str == '"') {
            quote = 1;
            str++;
        }
        char *end = str;
        while (*end && (*end != '"' || !quote) && (*end != ',' || quote)) {
            end++;
        }
        moloch_field_string_add(userField, session, str, end - str, TRUE);
    }
}
示例#5
0
static GInputStream *
soup_request_data_send (SoupRequest   *request,
			GCancellable  *cancellable,
			GError       **error)
{
	SoupRequestData *data = SOUP_REQUEST_DATA (request);
	SoupURI *uri = soup_request_get_uri (request);
	GInputStream *memstream;
	const char *comma, *start, *end;
	gboolean base64 = FALSE;
	char *uristr;

	uristr = soup_uri_to_string (uri, FALSE);
	start = uristr + 5;
	comma = strchr (start, ',');
	if (comma && comma != start) {
		/* Deal with MIME type / params */
		if (comma >= start + BASE64_INDICATOR_LEN && !g_ascii_strncasecmp (comma - BASE64_INDICATOR_LEN, BASE64_INDICATOR, BASE64_INDICATOR_LEN)) {
			end = comma - BASE64_INDICATOR_LEN;
			base64 = TRUE;
		} else
			end = comma;

		if (end != start)
			data->priv->content_type = soup_uri_decoded_copy (start, end - start, NULL);
	}

	memstream = g_memory_input_stream_new ();

	if (comma)
		start = comma + 1;

	if (*start) {
		int decoded_length = 0;
		guchar *buf = (guchar *) soup_uri_decoded_copy (start, strlen (start),
								&decoded_length);

		if (base64)
			buf = g_base64_decode_inplace ((gchar*) buf, &data->priv->content_length);
		else
			data->priv->content_length = decoded_length;

		g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (memstream),
						buf, data->priv->content_length,
						g_free);
	}
	g_free (uristr);

	return memstream;
}
示例#6
0
GBytes *
cockpit_auth_parse_authorization (GHashTable *headers,
                                  gchar **type)
{
  gchar *line;
  gchar *next;
  gchar *contents;
  gsize length;
  gpointer key;
  gsize i;

  /* Avoid copying as it can contain passwords */
  if (!g_hash_table_lookup_extended (headers, "Authorization", &key, (gpointer *)&line))
    return NULL;

  g_hash_table_steal (headers, "Authorization");
  g_free (key);

  line = str_skip (line, ' ');
  next = strchr (line, ' ');
  if (!next)
    {
      g_free (line);
      return NULL;
    }

  contents = str_skip (next, ' ');
  if (g_base64_decode_inplace (contents, &length) == NULL)
    {
      g_free (line);
      return NULL;
    }

  /* Null terminate for convenience, but null count not included in GBytes */
  contents[length] = '\0';

  if (type)
    {
      *type = g_strndup (line, next - line);
      for (i = 0; (*type)[i] != '\0'; i++)
        (*type)[i] = g_ascii_tolower ((*type)[i]);
    }

  /* Avoid copying by using the line directly */
  return g_bytes_new_with_free_func (contents, length, clear_free_authorization, line);
}
示例#7
0
static void
cockpit_auth_resume_async (CockpitAuth *self,
                           const gchar *application,
                           const gchar *type,
                           GHashTable *headers,
                           const gchar *remote_peer,
                           GAsyncReadyCallback callback,
                           gpointer user_data)
{
  AuthData *ad = NULL;
  GSimpleAsyncResult *task;
  GBytes *input = NULL;
  gchar **parts = NULL;
  gchar *header = NULL;
  gsize length;

  header = g_hash_table_lookup (headers, "Authorization");
  if (header)
    parts = g_strsplit (header, " ", 3);

  if (parts && g_strv_length (parts) == 3)
      ad = g_hash_table_lookup (self->authentication_pending, parts[1]);

  if (!ad)
    {
      task = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
                                        cockpit_auth_none_login_async);

      g_simple_async_result_set_error (task, COCKPIT_ERROR,
                                       COCKPIT_ERROR_AUTHENTICATION_FAILED,
                                       "Invalid resume token");
      g_simple_async_result_complete_in_idle (task);
    }
  else
    {

      task = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
                                        ad->tag);
      g_simple_async_result_set_op_res_gpointer (task, auth_data_ref (ad), auth_data_unref);
      g_hash_table_remove (self->authentication_pending, parts[1]);

      if (!g_base64_decode_inplace (parts[2], &length) || length < 1)
        {
          g_simple_async_result_set_error (task, COCKPIT_ERROR,
                                           COCKPIT_ERROR_AUTHENTICATION_FAILED,
                                           "Invalid resume token");
          g_simple_async_result_complete_in_idle (task);
        }
      else
        {
          input = g_bytes_new (parts[2], length);
          auth_data_add_pending_result (ad, task);
          cockpit_auth_process_write_auth_bytes (ad->auth_process, input);
          g_bytes_unref (input);
        }
    }

  if (parts)
    g_strfreev (parts);
  g_object_unref (task);
}