static SeahorseOperation* seahorse_hkp_source_import (SeahorseSource *sksrc, GInputStream *input) { SeahorseHKPOperation *hop; SeahorseHKPSource *hsrc; SoupMessage *message; GSList *keydata = NULL; GString *buf = NULL; GHashTable *form; gchar *key, *t; SoupURI *uri; GSList *l; guint len; g_return_val_if_fail (SEAHORSE_IS_HKP_SOURCE (sksrc), NULL); hsrc = SEAHORSE_HKP_SOURCE (sksrc); g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL); g_object_ref (input); for (;;) { buf = g_string_sized_new (2048); len = seahorse_util_read_data_block (buf, input, "-----BEGIN PGP PUBLIC KEY BLOCK-----", "-----END PGP PUBLIC KEY BLOCK-----"); if (len > 0) { keydata = g_slist_prepend (keydata, g_string_free (buf, FALSE)); } else { g_string_free (buf, TRUE); break; } } if (g_slist_length (keydata) == 0) { g_object_unref (input); return seahorse_operation_new_complete (NULL); } /* Figure out the URI we're sending to */ uri = get_http_server_uri (sksrc, "/pks/add"); g_return_val_if_fail (uri, FALSE); /* New operation and away we go */ keydata = g_slist_reverse (keydata); hop = setup_hkp_operation (hsrc); form = g_hash_table_new (g_str_hash, g_str_equal); for (l = keydata; l; l = g_slist_next (l)) { g_assert (l->data != NULL); g_hash_table_insert (form, "keytext", l->data); key = soup_form_encode_urlencoded (form); message = soup_message_new_from_uri ("POST", uri); soup_message_set_request (message, "application/x-www-form-urlencoded", SOUP_MEMORY_TAKE, key, strlen (key)); soup_session_queue_message (hop->session, message, (SoupSessionCallback)send_callback, hop); hop->requests++; } g_hash_table_destroy (form); hop->total = hop->requests; t = g_strdup_printf (_("Connecting to: %s"), uri->host); seahorse_operation_mark_progress (SEAHORSE_OPERATION (hop), t, -1); g_free (t); soup_uri_free (uri); seahorse_util_string_slist_free (keydata); g_object_unref (input); return SEAHORSE_OPERATION (hop); }
static GstFlowReturn gst_gio_base_src_create (GstBaseSrc * base_src, guint64 offset, guint size, GstBuffer ** buf_return) { GstGioBaseSrc *src = GST_GIO_BASE_SRC (base_src); GstBuffer *buf; GstFlowReturn ret = GST_FLOW_OK; g_return_val_if_fail (G_IS_INPUT_STREAM (src->stream), GST_FLOW_ERROR); /* If we have the requested part in our cache take a subbuffer of that, * otherwise fill the cache again with at least 4096 bytes from the * requested offset and return a subbuffer of that. * * We need caching because every read/seek operation will need to go * over DBus if our backend is GVfs and this is painfully slow. */ if (src->cache && offset >= GST_BUFFER_OFFSET (src->cache) && offset + size <= GST_BUFFER_OFFSET_END (src->cache)) { GST_DEBUG_OBJECT (src, "Creating subbuffer from cached buffer: offset %" G_GUINT64_FORMAT " length %u", offset, size); buf = gst_buffer_create_sub (src->cache, offset - GST_BUFFER_OFFSET (src->cache), size); GST_BUFFER_OFFSET (buf) = offset; GST_BUFFER_OFFSET_END (buf) = offset + size; GST_BUFFER_SIZE (buf) = size; } else { guint cachesize = MAX (4096, size); gssize read, res; gboolean success, eos; GError *err = NULL; if (src->cache) { gst_buffer_unref (src->cache); src->cache = NULL; } if (G_UNLIKELY (offset != src->position)) { if (!GST_GIO_STREAM_IS_SEEKABLE (src->stream)) return GST_FLOW_NOT_SUPPORTED; GST_DEBUG_OBJECT (src, "Seeking to position %" G_GUINT64_FORMAT, offset); ret = gst_gio_seek (src, G_SEEKABLE (src->stream), offset, src->cancel); if (ret == GST_FLOW_OK) src->position = offset; else return ret; } src->cache = gst_buffer_try_new_and_alloc (cachesize); if (G_UNLIKELY (src->cache == NULL)) { GST_ERROR_OBJECT (src, "Failed to allocate %u bytes", cachesize); return GST_FLOW_ERROR; } GST_LOG_OBJECT (src, "Reading %u bytes from offset %" G_GUINT64_FORMAT, cachesize, offset); /* GIO sometimes gives less bytes than requested although * it's not at the end of file. SMB for example only * supports reads up to 64k. So we loop here until we get at * at least the requested amount of bytes or a read returns * nothing. */ read = 0; while (size - read > 0 && (res = g_input_stream_read (G_INPUT_STREAM (src->stream), GST_BUFFER_DATA (src->cache) + read, cachesize - read, src->cancel, &err)) > 0) { read += res; } success = (read >= 0); eos = (cachesize > 0 && read == 0); if (!success && !gst_gio_error (src, "g_input_stream_read", &err, &ret)) { GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL), ("Could not read from stream: %s", err->message)); g_clear_error (&err); } if (success && !eos) { src->position += read; GST_BUFFER_SIZE (src->cache) = read; GST_BUFFER_OFFSET (src->cache) = offset; GST_BUFFER_OFFSET_END (src->cache) = offset + read; GST_DEBUG_OBJECT (src, "Read successful"); GST_DEBUG_OBJECT (src, "Creating subbuffer from new " "cached buffer: offset %" G_GUINT64_FORMAT " length %u", offset, size); buf = gst_buffer_create_sub (src->cache, 0, MIN (size, read)); GST_BUFFER_OFFSET (buf) = offset; GST_BUFFER_OFFSET_END (buf) = offset + MIN (size, read); GST_BUFFER_SIZE (buf) = MIN (size, read); } else { GST_DEBUG_OBJECT (src, "Read not successful"); gst_buffer_unref (src->cache); src->cache = NULL; buf = NULL; } if (eos) ret = GST_FLOW_UNEXPECTED; } *buf_return = buf; return ret; }
gboolean gimp_levels_config_load_cruft (GimpLevelsConfig *config, GInputStream *input, GError **error) { GDataInputStream *data_input; gint low_input[5]; gint high_input[5]; gint low_output[5]; gint high_output[5]; gdouble gamma[5]; gchar *line; gsize line_len; gint i; g_return_val_if_fail (GIMP_IS_LEVELS_CONFIG (config), FALSE); g_return_val_if_fail (G_IS_INPUT_STREAM (input), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); data_input = g_data_input_stream_new (input); line_len = 64; line = g_data_input_stream_read_line (data_input, &line_len, NULL, error); if (! line) return FALSE; if (strcmp (line, "# GIMP Levels File") != 0) { g_set_error_literal (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE, _("not a GIMP Levels file")); g_object_unref (data_input); g_free (line); return FALSE; } g_free (line); for (i = 0; i < 5; i++) { gchar float_buf[32]; gchar *endp; gint fields; line_len = 64; line = g_data_input_stream_read_line (data_input, &line_len, NULL, error); if (! line) { g_object_unref (data_input); return FALSE; } fields = sscanf (line, "%d %d %d %d %31s", &low_input[i], &high_input[i], &low_output[i], &high_output[i], float_buf); g_free (line); if (fields != 5) goto error; gamma[i] = g_ascii_strtod (float_buf, &endp); if (endp == float_buf || errno == ERANGE) goto error; } g_object_unref (data_input); g_object_freeze_notify (G_OBJECT (config)); for (i = 0; i < 5; i++) { config->low_input[i] = low_input[i] / 255.0; config->high_input[i] = high_input[i] / 255.0; config->low_output[i] = low_output[i] / 255.0; config->high_output[i] = high_output[i] / 255.0; config->gamma[i] = gamma[i]; } g_object_notify (G_OBJECT (config), "gamma"); g_object_notify (G_OBJECT (config), "low-input"); g_object_notify (G_OBJECT (config), "high-input"); g_object_notify (G_OBJECT (config), "low-output"); g_object_notify (G_OBJECT (config), "high-output"); g_object_thaw_notify (G_OBJECT (config)); return TRUE; error: g_object_unref (data_input); g_set_error_literal (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_PARSE, _("parse error")); return FALSE; }
GList * gimp_brush_generated_load (GimpContext *context, GFile *file, GInputStream *input, GError **error) { GimpBrush *brush; GDataInputStream *data_input; gchar *string; gsize string_len; gint linenum; gchar *name = NULL; GimpBrushGeneratedShape shape = GIMP_BRUSH_GENERATED_CIRCLE; gboolean have_shape = FALSE; gint spikes = 2; gdouble spacing; gdouble radius; gdouble hardness; gdouble aspect_ratio; gdouble angle; g_return_val_if_fail (G_IS_FILE (file), NULL); g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); data_input = g_data_input_stream_new (input); /* make sure the file we are reading is the right type */ linenum = 1; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; if (! g_str_has_prefix (string, "GIMP-VBR")) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ, _("Not a GIMP brush file.")); g_free (string); goto failed; } g_free (string); /* make sure we are reading a compatible version */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; if (! g_str_has_prefix (string, "1.0")) { if (! g_str_has_prefix (string, "1.5")) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ, _("Unknown GIMP brush version.")); g_free (string); goto failed; } else { have_shape = TRUE; } } g_free (string); /* read name */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; g_strstrip (string); /* the empty string is not an allowed name */ if (strlen (string) < 1) { name = g_strdup (_("Untitled")); } else { name = gimp_any_to_utf8 (string, -1, _("Invalid UTF-8 string in brush file '%s'."), gimp_file_get_utf8_name (file)); } g_free (string); if (have_shape) { GEnumClass *enum_class; GEnumValue *shape_val; enum_class = g_type_class_peek (GIMP_TYPE_BRUSH_GENERATED_SHAPE); /* read shape */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; g_strstrip (string); shape_val = g_enum_get_value_by_nick (enum_class, string); if (! shape_val) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ, _("Unknown GIMP brush shape.")); g_free (string); goto failed; } g_free (string); shape = shape_val->value; } /* read brush spacing */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; spacing = g_ascii_strtod (string, NULL); g_free (string); /* read brush radius */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; radius = g_ascii_strtod (string, NULL); g_free (string); if (have_shape) { /* read number of spikes */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; spikes = CLAMP (atoi (string), 2, 20); g_free (string); } /* read brush hardness */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; hardness = g_ascii_strtod (string, NULL); g_free (string); /* read brush aspect_ratio */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; aspect_ratio = g_ascii_strtod (string, NULL); g_free (string); /* read brush angle */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; angle = g_ascii_strtod (string, NULL); g_free (string); g_object_unref (data_input); brush = GIMP_BRUSH (gimp_brush_generated_new (name, shape, radius, spikes, hardness, aspect_ratio, angle)); g_free (name); brush->spacing = spacing; return g_list_prepend (NULL, brush); failed: g_object_unref (data_input); if (name) g_free (name); g_prefix_error (error, _("In line %d of brush file: "), linenum); return NULL; }
GList * gimp_pattern_load (GimpContext *context, GFile *file, GInputStream *input, GError **error) { GimpPattern *pattern = NULL; const Babl *format = NULL; PatternHeader header; gsize size; gsize bytes_read; gint bn_size; gchar *name = NULL; g_return_val_if_fail (G_IS_FILE (file), NULL); g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); /* read the size */ if (! g_input_stream_read_all (input, &header, sizeof (header), &bytes_read, NULL, error) || bytes_read != sizeof (header)) { g_prefix_error (error, _("File appears truncated: ")); goto error; } /* rearrange the bytes in each unsigned int */ header.header_size = g_ntohl (header.header_size); header.version = g_ntohl (header.version); header.width = g_ntohl (header.width); header.height = g_ntohl (header.height); header.bytes = g_ntohl (header.bytes); header.magic_number = g_ntohl (header.magic_number); /* Check for correct file format */ if (header.magic_number != GPATTERN_MAGIC || header.version != 1 || header.header_size <= sizeof (header)) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ, _("Unknown pattern format version %d."), header.version); goto error; } /* Check for supported bit depths */ if (header.bytes < 1 || header.bytes > 4) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ, _("Unsupported pattern depth %d.\n" "GIMP Patterns must be GRAY or RGB."), header.bytes); goto error; } /* Read in the pattern name */ if ((bn_size = (header.header_size - sizeof (header)))) { gchar *utf8; name = g_new (gchar, bn_size); if (! g_input_stream_read_all (input, name, bn_size, &bytes_read, NULL, error) || bytes_read != bn_size) { g_prefix_error (error, _("File appears truncated.")); g_free (name); goto error; } utf8 = gimp_any_to_utf8 (name, -1, _("Invalid UTF-8 string in pattern file '%s'."), gimp_file_get_utf8_name (file)); g_free (name); name = utf8; } if (! name) name = g_strdup (_("Unnamed")); pattern = g_object_new (GIMP_TYPE_PATTERN, "name", name, "mime-type", "image/x-gimp-pat", NULL); g_free (name); switch (header.bytes) { case 1: format = babl_format ("Y' u8"); break; case 2: format = babl_format ("Y'A u8"); break; case 3: format = babl_format ("R'G'B' u8"); break; case 4: format = babl_format ("R'G'B'A u8"); break; } pattern->mask = gimp_temp_buf_new (header.width, header.height, format); size = header.width * header.height * header.bytes; if (! g_input_stream_read_all (input, gimp_temp_buf_get_data (pattern->mask), size, &bytes_read, NULL, error) || bytes_read != size) { g_prefix_error (error, _("File appears truncated.")); goto error; } return g_list_prepend (NULL, pattern); error: if (pattern) g_object_unref (pattern); g_prefix_error (error, _("Fatal parse error in pattern file: ")); return NULL; }