static void
extract_gibest_hash (GTask        *task,
                     gpointer      source_object,
                     gpointer      task_data,
                     GCancellable *cancellable)
{
  GFile *file = source_object;
  guint64 buffer[2][CHUNK_N_BYTES/8];
  GInputStream *stream = NULL;
  gssize n_bytes, file_size;
  GError *error = NULL;
  guint64 hash = 0;
  gint i;
  char *str;
  ResolveData *resolve_data = task_data;
  GrlLocalMetadataSourcePriv *priv;

  priv = GRL_LOCAL_METADATA_SOURCE_GET_PRIVATE (resolve_data->source);

  stream = G_INPUT_STREAM (g_file_read (file, cancellable, &error));
  if (stream == NULL)
    goto fail;

  /* Extract start/end chunks of the file */
  n_bytes = g_input_stream_read (stream, buffer[0], CHUNK_N_BYTES, cancellable, &error);
  if (n_bytes == -1)
    goto fail;

  if (!g_seekable_seek (G_SEEKABLE (stream), -CHUNK_N_BYTES, G_SEEK_END, cancellable, &error))
    goto fail;

  n_bytes = g_input_stream_read (stream, buffer[1], CHUNK_N_BYTES, cancellable, &error);
  if (n_bytes == -1)
    goto fail;

  for (i = 0; i < G_N_ELEMENTS (buffer[0]); i++)
    hash += buffer[0][i] + buffer[1][i];

  file_size = g_seekable_tell (G_SEEKABLE (stream));

  if (file_size < CHUNK_N_BYTES)
    goto fail;

  /* Include file size */
  hash += file_size;
  g_object_unref (stream);

  str = g_strdup_printf ("%" G_GINT64_FORMAT, hash);
  grl_data_set_string (GRL_DATA (resolve_data->rs->media), priv->hash_keyid, str);
  g_free (str);

  g_task_return_boolean (task, TRUE);
  return;

fail:
  GRL_DEBUG ("Could not get file hash: %s\n", error ? error->message : "Unknown error");
  g_task_return_error (task, error);
  g_clear_object (&stream);
}
static gboolean send_files (NstPlugin *plugin, GtkWidget *contact_widget, GList *file_list)
{
	GList *l;
	for (l = file_list; l != NULL; l = l->next)
	{
		// Open file
		GError *error = NULL;
		GFile *source = g_file_new_for_commandline_arg (l->data);
		GFileInputStream *in = g_file_read (source, NULL, &error);
		if (!in)
		{
			handle_error(error, "could not get input stream for file");
			g_object_unref (source);
			return FALSE;
		}

		// Read file contents
		GByteArray *data = g_byte_array_new ();
		guint8 buffer[1024];
		gssize len = g_input_stream_read (G_INPUT_STREAM(in), buffer, 1024, NULL, &error);
		while (len > 0)
		{
			g_byte_array_append (data, buffer, len);
			len = g_input_stream_read (G_INPUT_STREAM(in), buffer, 1024, NULL, &error);
		}
		if (len < 0)
		{
			handle_error(error, "could not read file");
			g_byte_array_free (data, TRUE);
			g_object_unref (in);
			g_object_unref (source);
			return FALSE;
		}

		// XMLRPC request to attach the file to the ticket
		GValue result;
		gchar *err = send_xmlrpc(gtk_entry_get_text(GTK_ENTRY(url_field)), "ticket.putAttachment", &result,
				G_TYPE_INT, strtol(gtk_entry_get_text(GTK_ENTRY(ticket_field)), NULL, 10),
				G_TYPE_STRING, g_file_get_basename(source),
				G_TYPE_STRING, gtk_entry_get_text(GTK_ENTRY(description_field)),
				SOUP_TYPE_BYTE_ARRAY, data,
				G_TYPE_BOOLEAN, TRUE,
				G_TYPE_INVALID);

		g_byte_array_free (data, TRUE);
		g_object_unref (in);

		if (err)
		{
			handle_error(NULL, err);
			g_free (err);
			g_object_unref (source);
			return FALSE;
		}
		g_object_unref (source);
	}

	return TRUE;
}
Exemplo n.º 3
0
static GFile *
get_g_file_with_encrypted_data (GFileInputStream *in_stream, goffset file_size)
{
    GError *err = NULL;
    GFileIOStream *ostream;
    gssize read_len;
    guchar *buf;
    gsize len_file_data = file_size - SHA512_DIGEST_SIZE;
    gsize done_size = 0;

    GFile *tmp_encrypted_file = g_file_new_tmp (NULL, &ostream, &err);
    if (tmp_encrypted_file == NULL) {
        g_printerr ("%s\n", err->message);
        // TODO
        return NULL;
    }

    GFileOutputStream *out_enc_stream = g_file_append_to (tmp_encrypted_file, G_FILE_CREATE_NONE, NULL, &err);
    if (out_enc_stream == NULL) {
        g_printerr ("%s\n", err->message);
        // TODO
        return NULL;
    }

    if (len_file_data < FILE_BUFFER) {
        buf = g_malloc (len_file_data);
        g_input_stream_read (G_INPUT_STREAM (in_stream), buf, len_file_data, NULL, &err);
        g_output_stream_write (G_OUTPUT_STREAM (out_enc_stream), buf, len_file_data, NULL, &err);
    }
    else {
        buf = g_malloc (FILE_BUFFER);
        while (done_size < len_file_data) {
            if ((len_file_data - done_size) > FILE_BUFFER) {
                read_len = g_input_stream_read (G_INPUT_STREAM (in_stream), buf, FILE_BUFFER, NULL, &err);
            }
            else {
                read_len = g_input_stream_read (G_INPUT_STREAM (in_stream), buf, len_file_data - done_size, NULL, &err);
            }
            if (read_len == -1) {
                g_printerr ("%s\n", err->message);
                // TODO
                return NULL;
            }
            g_output_stream_write (G_OUTPUT_STREAM (out_enc_stream), buf, read_len, NULL, &err);
            done_size += read_len;
            memset (buf, 0, FILE_BUFFER);
        }
    }

    g_input_stream_close (G_INPUT_STREAM (in_stream), NULL, NULL);
    g_output_stream_close (G_OUTPUT_STREAM (out_enc_stream), NULL, NULL);
    g_object_unref (out_enc_stream);
    g_free (buf);

    return tmp_encrypted_file;
}
static char *
get_uncompressed_name_from_archive (FrCommand  *comm,
				    const char *archive)
{
	GFile        *file;
	GInputStream *stream;
	char         *filename = NULL;

	if (! is_mime_type (comm->mime_type, "application/x-gzip"))
		return NULL;

	file = g_file_new_for_path (archive);

	stream = (GInputStream *) g_file_read (file, NULL, NULL);
	if (stream != NULL) {
		gboolean filename_present = TRUE;
		char     buffer[10];

		if (g_input_stream_read (stream, buffer, 10, NULL, NULL) >= 0) {
			/* Check whether the FLG.FNAME is set */
			if (((unsigned char)(buffer[3]) & 0x08) != 0x08)
				filename_present = FALSE;

			/* Check whether the FLG.FEXTRA is set */
			if (((unsigned char)(buffer[3]) & 0x04) == 0x04)
				filename_present = FALSE;
		}

		if (filename_present) {
			GString *str = NULL;

			str = g_string_new ("");
			while (g_input_stream_read (stream, buffer, 1, NULL, NULL) > 0) {
				if (buffer[0] == '\0') {
					filename = g_strdup (file_name_from_path (str->str));
#ifdef DEBUG
					g_message ("filename is: %s", filename);
#endif
					break;
				}
				g_string_append_c (str, buffer[0]);
			}
			g_string_free (str, TRUE);
		}
		g_object_unref (stream);
	}
	g_object_unref (file);

	return filename;
}
Exemplo n.º 5
0
static gchar *
encrypt_using_ctr_mode (Metadata *header_metadata, gcry_cipher_hd_t *hd, goffset file_size,
                        GFileInputStream *in_stream, GFileOutputStream *out_stream)
{
    GError *err = NULL;

    if (g_output_stream_write (G_OUTPUT_STREAM (out_stream), header_metadata, sizeof (Metadata), NULL, &err) == -1) {
        return g_strdup (err->message);
    }

    guchar *buffer = g_try_malloc0 (FILE_BUFFER);
    guchar *enc_buffer = g_try_malloc0 (FILE_BUFFER);

    if (buffer == NULL || enc_buffer == NULL) {
        return g_strdup ("Couldn't allocate memory");
    }

    goffset done_size = 0;
    gssize read_len;

    while (done_size < file_size) {
        if ((file_size - done_size) > FILE_BUFFER) {
            read_len = g_input_stream_read (G_INPUT_STREAM (in_stream), buffer, FILE_BUFFER, NULL, &err);
        }
        else {
            read_len = g_input_stream_read (G_INPUT_STREAM (in_stream), buffer, file_size - done_size, NULL, &err);
        }
        if (read_len == -1) {
            multiple_free (2, (gpointer) &buffer, (gpointer) &enc_buffer);
            return g_strdup (err->message);
        }

        gcry_cipher_encrypt (*hd, enc_buffer, read_len, buffer, read_len);
        if (g_output_stream_write (G_OUTPUT_STREAM (out_stream), enc_buffer, read_len, NULL, &err) == -1) {
            multiple_free (2, (gpointer) &buffer, (gpointer) &enc_buffer);
            return g_strdup (err->message);
        }

        memset (buffer, 0, FILE_BUFFER);
        memset (enc_buffer, 0, FILE_BUFFER);

        done_size += read_len;
    }

    multiple_free (2, (gpointer) &buffer, (gpointer) &enc_buffer);

    return NULL;
}
Exemplo n.º 6
0
static gboolean
fill_buffer (GZHandle *gz,
             gsize num_bytes)
{
  gsize count;

  z_stream * zstream = &gz->zstream;

  if (zstream->avail_in > 0) {
    return TRUE;
  }

  count = g_input_stream_read (gz->parent_str,
                               gz->buffer,
                               Z_BUFSIZE,
                               NULL, NULL);
  if (count == -1) {
    if (zstream->avail_out == num_bytes) {
      return FALSE;
    }
    gz->last_str_result = FALSE;
  } else {
    zstream->next_in = gz->buffer;
    zstream->avail_in = count;
  }

  return TRUE;
}
Exemplo n.º 7
0
static VALUE
rg_read(int argc, VALUE *argv, VALUE self)
{
        VALUE rbcount, cancellable, result;
        gsize count;
        GError *error = NULL;
        gssize bytes_read;

        rb_scan_args(argc, argv, "11", &rbcount, &cancellable);
        count = RVAL2GSIZE(rbcount);
        result = rb_str_new(NULL, count);
        bytes_read = g_input_stream_read(_SELF(self),
                                         RSTRING_PTR(result),
                                         count,
                                         RVAL2GCANCELLABLE(cancellable),
                                         &error);
        if (bytes_read == -1)
                rbgio_raise_error(error);

        rb_str_set_len(result, bytes_read);
        rb_str_resize(result, bytes_read);
        OBJ_TAINT(result);

        return result;
}
Exemplo n.º 8
0
static gssize ekg_gnutls_pull(gnutls_transport_ptr_t connptr, gpointer buf, gsize len) {
	struct ekg_gnutls_connection *conn = connptr;
	GBufferedInputStream *s = G_BUFFERED_INPUT_STREAM(conn->connection->instream);
	gsize avail_bytes = g_buffered_input_stream_get_available(s);

	/* XXX: EOF? */

	g_assert(len > 0);

	if (avail_bytes == 0) {
		if (conn->connection_error)
			return 0; /* EOF */

		gnutls_transport_set_errno(conn->session, EAGAIN);
		return -1;
	} else {
		GError *err = NULL;
		gssize ret = g_input_stream_read(
				G_INPUT_STREAM(s),
				buf,
				MIN(avail_bytes, len),
				NULL,
				&err);
		
		if (ret == -1) {
			debug_error("ekg_gnutls_pull() failed: %s\n", err->message);
			g_error_free(err);
		}

		return ret;
	}

	g_assert_not_reached();
}
Exemplo n.º 9
0
static gboolean
check_valid_png_header(GInputStream *stream, GError **err)
{
  const size_t hdr_size=8;
  gssize hdr_read_size;
  unsigned char header[hdr_size];

  hdr_read_size = g_input_stream_read(G_INPUT_STREAM(stream), header, hdr_size, NULL, err);
  if (hdr_read_size == -1)
    {
      // err should be set by _read()
      return FALSE;
    }
  else if (hdr_read_size < hdr_size)
    {
      g_set_error(err, error_quark(), LOAD_PNG_TOO_SHORT,
                 "too short for a png file, only %lu bytes.",
                 (unsigned long)hdr_read_size);

      return FALSE;
    }
  else if (hdr_read_size > hdr_size)
    {
        const gboolean reached = TRUE;
        g_assert(!reached);
    }

  if (png_sig_cmp (header, 0, hdr_size))
    {
      g_set_error(err, error_quark(), LOAD_PNG_WRONG_HEADER, "wrong png header");
      return FALSE;
    }
  return TRUE;
}
Exemplo n.º 10
0
static boolean
gio_source_fill(j_decompress_ptr cinfo)
{
    struct jpeg_source_mgr* src = cinfo->src;
    GioSource *self = (GioSource *)cinfo->client_data;
    GError *err = NULL;

    const gssize bytes_read = g_input_stream_read(self->stream, self->buffer,
                                                  self->buffer_size, NULL, &err);
    if (!err)
      {
        src->next_input_byte = (JOCTET*)self->buffer;
        src->bytes_in_buffer = bytes_read;
      }
    else
      {
        g_print("%s: %s\n", __PRETTY_FUNCTION__, err->message);
      }
    

    /* FIXME: needed for EOF?
    static const JOCTET EOI_BUFFER[ 2 ] = { (JOCTET)0xFF, (JOCTET)JPEG_EOI };
    src->next_input_byte = EOI_BUFFER;
    src->bytes_in_buffer = sizeof( EOI_BUFFER );
    */

    return TRUE;
}
Exemplo n.º 11
0
int
main (int argc, char **argv)
{
    GConverter *converter;
    GFile *file;
    GFileInputStream *file_stream;
    GInputStream *stream;
    gchar buf[1024];
    gssize bytes;
        
    if (argc < 2) {
        g_printerr ("Usage: test-bz2 FILE\n");
        return 1;
    }

    file = g_file_new_for_path (argv[1]);
    file_stream = g_file_read (file, NULL, NULL);
    converter = G_CONVERTER (yelp_bz2_decompressor_new ());
    stream = g_converter_input_stream_new (G_INPUT_STREAM (file_stream),
                                           converter);

    while ((bytes = g_input_stream_read (stream, buf, 1024, NULL, NULL)) > 0) {
        gchar *out = g_strndup (buf, bytes);
        puts (out);
        g_free (out);
    }

    return 0;
}
Exemplo n.º 12
0
static gboolean
trash_backend_read (GVfsBackend       *backend,
                    GVfsJobRead       *job,
                    GVfsBackendHandle  handle,
                    char              *buffer,
                    gsize              bytes_requested)
{
  GError *error = NULL;
  gssize bytes;

  bytes = g_input_stream_read (handle, buffer, bytes_requested,
                               G_VFS_JOB (job)->cancellable, &error);

  if (bytes >= 0)
    {
      g_vfs_job_read_set_size (job, bytes);
      g_vfs_job_succeeded (G_VFS_JOB (job));

      return TRUE;
    }

  g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
  g_error_free (error);

  return TRUE;
}
// FIXME: It would be better to do without this.
void IOChannel::readSync(size_t offset, size_t size, WorkQueue*, std::function<void (Data&, int error)> completionHandler)
{
    ASSERT(m_inputStream);
    size_t bufferSize = std::min(size, gDefaultReadBufferSize);
    uint8_t* bufferData = static_cast<uint8_t*>(fastMalloc(bufferSize));
    GRefPtr<SoupBuffer> readBuffer = adoptGRef(soup_buffer_new_with_owner(bufferData, bufferSize, bufferData, fastFree));
    Data data;
    size_t pendingBytesToRead = size;
    size_t bytesToRead = bufferSize;
    do {
        // FIXME: implement offset.
        gssize bytesRead = g_input_stream_read(m_inputStream.get(), const_cast<char*>(readBuffer->data), bytesToRead, nullptr, nullptr);
        if (bytesRead == -1) {
            completionHandler(data, -1);
            return;
        }

        if (!bytesRead)
            break;

        ASSERT(bytesRead > 0);
        fillDataFromReadBuffer(readBuffer.get(), static_cast<size_t>(bytesRead), data);

        pendingBytesToRead = size - data.size();
        bytesToRead = std::min(pendingBytesToRead, readBuffer->length);
    } while (pendingBytesToRead);

    completionHandler(data, 0);
}
Exemplo n.º 14
0
static gboolean
parse (GMarkupParseContext *pcontext,
       GInputStream        *input,
       GError             **error)
{
    gchar buffer[BUFSIZE];

    while (1) {
        gssize nread = g_input_stream_read (input,
                                            buffer,
                                            sizeof (buffer),
                                            NULL,
                                            error);
        if (nread < 0)
            return FALSE;

        if (nread == 0)
            break;

        if (!g_markup_parse_context_parse (pcontext, buffer, nread, error))
            return FALSE;
    }

    return g_markup_parse_context_end_parse (pcontext, error);
}
Exemplo n.º 15
0
static void
read_from_stream (GTask *task,
                  gpointer source_obj,
                  gpointer task_data,
                  GCancellable *cancellable)
{
  LoadData *data = task_data;
  GError *error = NULL;
  gssize res;

  data->pos = 0;
  g_byte_array_set_size (data->content, data->pos + GET_DATA_BLOCK_SIZE + 1);
  while ((res = g_input_stream_read (data->stream,
                                     data->content->data + data->pos,
                                     GET_DATA_BLOCK_SIZE,
                                     cancellable, &error)) > 0)
    {
      data->pos += res;
      g_byte_array_set_size (data->content, data->pos + GET_DATA_BLOCK_SIZE + 1);
    }

  if (res < 0)
    {
      g_task_return_error (task, error);
      return;
    }

  /* zero-terminate the content; we allocated an extra byte for this */
  data->content->data[data->pos] = 0;
  g_task_return_boolean (task, TRUE);
}
Exemplo n.º 16
0
static unsigned long
vfs_stream_read (FT_Stream stream,
		 unsigned long offset,
		 unsigned char *buffer,
		 unsigned long count)
{
    GFileInputStream *handle = stream->descriptor.pointer;
    gssize bytes_read = 0;

    if (!count && offset > stream->size)
        return 1;

    if (!g_seekable_seek (G_SEEKABLE (handle), offset, G_SEEK_SET, NULL, NULL))
        return (count ? 0 : 1);

    if (count > 0) {
        bytes_read = g_input_stream_read (G_INPUT_STREAM (handle), buffer,
					  count, NULL, NULL);

        if (bytes_read == -1)
            return 0;
    }

    return bytes_read;
}
static void 
state_read_buffer (GcrImporter *self, gboolean async)
{
	GError *error = NULL;
	gssize count;
	gsize at;
	
	g_assert (GCR_IS_IMPORTER (self));
	g_assert (G_IS_INPUT_STREAM (self->pv->input));
	
	if (!self->pv->buffer)
		self->pv->buffer = g_byte_array_sized_new (BLOCK);

	at = self->pv->buffer->len;
	g_byte_array_set_size (self->pv->buffer, at + BLOCK);
	
	if (async) {
		g_input_stream_read_async (self->pv->input, self->pv->buffer->data + at, 
		                           BLOCK, G_PRIORITY_DEFAULT, self->pv->cancel,
		                           on_read_buffer, self);
	} else {
		count = g_input_stream_read (self->pv->input, self->pv->buffer->data + at, 
		                             BLOCK, self->pv->cancel, &error);
		complete_read_buffer (self, count, error);
	}
}
Exemplo n.º 18
0
const gchar *
chupa_data_get_raw_data(ChupaData *data, gsize *length, GError **error)
{
    ChupaDataPrivate *priv;

    priv = CHUPA_DATA_GET_PRIVATE(data);
    if (!priv->finished || priv->error) {
        if (length)
            *length = 0;
        return NULL;
    }

    if (!priv->raw_data) {
        gssize count;
        gchar buffer[1024];
        gsize buffer_size;

        priv->raw_data = g_string_new(NULL);
        buffer_size = sizeof(buffer);
        while ((count = g_input_stream_read(priv->stream, buffer, buffer_size,
                                            NULL, error)) > 0) {
            g_string_append_len(priv->raw_data, buffer, count);
            if (count < buffer_size)
                break;
        }
    }

    if (length)
        *length = priv->raw_data->len;
    return priv->raw_data->str;
}
Exemplo n.º 19
0
static gboolean
dump_data (GFile *file,
           struct archive *archive,
           GCancellable               *cancellable,
           GError                    **error)
{
  char buffer[32*1024];
  g_autoptr(GFileInputStream) in = NULL;
  gssize in_buffer;

  in = g_file_read (file, cancellable, error);
  if (in == NULL)
    return FALSE;

  while (TRUE)
    {
      in_buffer = g_input_stream_read (G_INPUT_STREAM (in), buffer, sizeof (buffer), cancellable, error);
      if (in_buffer == -1)
        return FALSE;

      if (in_buffer == 0)
        break;

      if (archive_write_data (archive, buffer, in_buffer) < ARCHIVE_OK)
        return xdg_app_fail (error, "Can't write tar data");
    }

  return TRUE;
}
Exemplo n.º 20
0
static gboolean
on_gnupg_source_input (GcrGnupgProcess *self,
                       GnupgSource *gnupg_source,
                       gint fd)
{
	gssize read;

	if (gnupg_source->input_buf == NULL ||
	    gnupg_source->input_buf->len == 0) {
		if (self->pv->input == NULL)
			return FALSE;
		if (!gnupg_source->input_buf)
			gnupg_source->input_buf = g_byte_array_new ();
		g_byte_array_set_size (gnupg_source->input_buf, 4096);
		read = g_input_stream_read (self->pv->input,
		                            gnupg_source->input_buf->data,
		                            gnupg_source->input_buf->len,
		                            gnupg_source->cancellable, NULL);
		g_byte_array_set_size (gnupg_source->input_buf, read < 0 ? 0 : read);
		if (read < 0)
			return FALSE;
		if (read == 0)
			return FALSE;
	}

	if (!write_input (fd, gnupg_source->input_buf)) {
		g_warning ("couldn't write output data to gnupg process");
		return FALSE;
	}

	return TRUE;
}
Exemplo n.º 21
0
void
dmap_write_next_chunk (SoupMessage * message, ChunkData * cd)
{
	gssize read_size;
	GError *error = NULL;
	gchar *chunk = g_malloc (DMAP_SHARE_CHUNK_SIZE);

	g_debug ("Trying to read %d bytes.", DMAP_SHARE_CHUNK_SIZE);
	read_size = g_input_stream_read (cd->stream,
					 chunk,
					 DMAP_SHARE_CHUNK_SIZE, NULL, &error);
	if (read_size > 0) {
		soup_message_body_append (message->response_body,
					  SOUP_MEMORY_TAKE, chunk, read_size);
		g_debug ("Read/wrote %d bytes.", read_size);
	} else {
		if (error != NULL) {
			g_warning ("Error reading from input stream: %s",
				   error->message);
			g_error_free (error);
		}
		g_free (chunk);
		g_debug ("Wrote 0 bytes, sending message complete.");
		soup_message_body_complete (message->response_body);
	}
	soup_server_unpause_message (cd->server, message);
}
Exemplo n.º 22
0
static gssize
ostree_chain_input_stream_read (GInputStream  *stream,
                                void          *buffer,
                                gsize          count,
                                GCancellable  *cancellable,
                                GError       **error)
{
  OstreeChainInputStream *self = (OstreeChainInputStream*) stream;
  GInputStream *child;
  gssize res = -1;

  if (g_cancellable_set_error_if_cancelled (cancellable, error))
    return -1;
  
  if (self->priv->index >= self->priv->streams->len)
    return 0;

  res = 0;
  while (res == 0 && self->priv->index < self->priv->streams->len)
    {
      child = self->priv->streams->pdata[self->priv->index];
      res = g_input_stream_read (child,
                                 buffer,
                                 count,
                                 cancellable,
                                 error);
      if (res == 0)
        self->priv->index++;
    }

  return res;
}
Exemplo n.º 23
0
static gssize
byzanz_queue_input_stream_read (GInputStream *input_stream,
				void *	      buffer,
				gsize         count,
				GCancellable *cancellable,
				GError **     error)
{
  ByzanzQueueInputStream *stream = BYZANZ_QUEUE_INPUT_STREAM (input_stream);
  gssize result;

retry:
  if (!byzanz_queue_input_stream_ensure_input (stream, cancellable, error))
    return -1;

  /* No more data to read from the queue */
  if (stream->input == NULL)
    return 0;

  result = g_input_stream_read (stream->input, buffer, count, cancellable, error);
  if (result == -1)
    return -1;

  /* no data in file. Let's wait for more. */
  if (result == 0) {
    if (!byzanz_queue_input_stream_wait (stream, cancellable, error))
      return -1;
    goto retry;
  }

  stream->input_bytes += result;
  return result;
}
Exemplo n.º 24
0
/*
  net_read ()
  
  Attempts to read datalen bytes from the network connection

  Returns number of bytes read or <=0 if error is encountered.
 */
static gint32
net_read (MCACnxn *cnxn, void *data, guint32 datalen)
{
  gint32     retsize;

	retsize = read_data(cnxn->conn_priv, data, datalen);

#if 0
  GError    *error;
  assert(cnxn != NULL);
#ifdef USE_IO_STREAM
  assert(cnxn->net_in_stream != NULL);

  retsize = g_input_stream_read(cnxn->net_in_stream, data, datalen, NULL, &error);
#else
  assert(cnxn->gsocket != NULL);

	retsize = g_socket_receive(cnxn->gsocket, data, datalen, NULL, &error);
#endif
  if (retsize < 0){
    DEBUG_PRINT("net_read(): Error [%s]\n", error->message);
    g_error_free(error);
  }
  else{
    DEBUG_PRINT("net_read(): read [%d bytes]\n", retsize);
  }  
#endif  
  return retsize;
}
Exemplo n.º 25
0
GdkPixbufAnimation* load_animation_from_stream(GInputStream* input_stream, GCancellable* generator_cancellable)
{
    GError** error = NULL;
    gboolean res = TRUE;
    gssize n_read = 0;
    guchar buffer[65535];

    GdkPixbufAnimation* animation = NULL;
    GdkPixbufLoader*    loader    = NULL;

    loader = gdk_pixbuf_loader_new();

    while (1)
    {
        n_read = g_input_stream_read (input_stream, buffer, sizeof (buffer), generator_cancellable, error);

        if (n_read < 0)
        {
            res = FALSE;
            error = NULL;
            break;
            return NULL;
        }

        if (n_read == 0)
        {
            break;
        }

        if (!gdk_pixbuf_loader_write (loader, buffer, n_read, error))
        {
            res = FALSE;
            error = NULL;
            break;
        }
    }

    animation = NULL;

    if (res)
    {
        animation = gdk_pixbuf_loader_get_animation(loader);

        if (animation)
        {
            g_object_ref (animation);
        }
    }

    if (!gdk_pixbuf_loader_close (loader, error))
    {
        res = FALSE;
        error = NULL;
        g_object_unref (loader);
        return;
    }

    return animation;
}
Exemplo n.º 26
0
/* This function is to avoid situations like this
 *
 * BEGIN\r\nl\0\0\1...
 *
 * e.g. where we read into the first D-Bus message while waiting for
 * the final line from the client (TODO: file bug against gio for
 * this)
 */
static gchar *
_my_g_input_stream_read_line_safe (GInputStream  *i,
                                   gsize         *out_line_length,
                                   GCancellable  *cancellable,
                                   GError       **error)
{
  GString *str;
  gchar c;
  gssize num_read;
  gboolean last_was_cr;

  str = g_string_new (NULL);

  last_was_cr = FALSE;
  while (TRUE)
    {
      num_read = g_input_stream_read (i,
                                      &c,
                                      1,
                                      cancellable,
                                      error);
      if (num_read == -1)
        goto fail;
      if (num_read == 0)
        {
          if (error != NULL && *error == NULL)
            {
              g_set_error_literal (error,
                                   G_IO_ERROR,
                                   G_IO_ERROR_FAILED,
                                   _("Unexpected lack of content trying to (safely) read a line"));
            }
          goto fail;
        }

      g_string_append_c (str, (gint) c);
      if (last_was_cr)
        {
          if (c == 0x0a)
            {
              g_assert (str->len >= 2);
              g_string_set_size (str, str->len - 2);
              goto out;
            }
        }
      last_was_cr = (c == 0x0d);
    }

 out:
  if (out_line_length != NULL)
    *out_line_length = str->len;
  return g_string_free (str, FALSE);

 fail:
  g_assert (error == NULL || *error != NULL);
  g_string_free (str, TRUE);
  return NULL;
}
Exemplo n.º 27
0
static gpointer
stlink_gui_populate_filemem_view (STlinkGUI *gui)
{
	guchar        buffer[MEM_READ_SIZE];
	GFile        *file;
	GFileInfo    *file_info;
	GInputStream *input_stream;
	gint          off;
	GError       *err = NULL;

	g_return_val_if_fail (gui != NULL, NULL);
	g_return_val_if_fail (gui->filename != NULL, NULL);

	file = g_file_new_for_path (gui->filename);
	input_stream = G_INPUT_STREAM (g_file_read (file, NULL, &err));
	if (err) {
		stlink_gui_set_info_error_message (gui, err->message);
		g_error_free (err);
		goto out;
	}

	file_info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (input_stream),
	                                            G_FILE_ATTRIBUTE_STANDARD_SIZE, NULL, &err);
	if (err) {
		stlink_gui_set_info_error_message (gui, err->message);
		g_error_free (err);
		goto out_input;
	}
	if (gui->file_mem.memory) {
		g_free (gui->file_mem.memory);
	}
	gui->file_mem.size   = g_file_info_get_size (file_info);
	gui->file_mem.memory = g_malloc (gui->file_mem.size);

	for (off = 0; off < gui->file_mem.size; off += MEM_READ_SIZE) {
		guint   n_read = MEM_READ_SIZE;

		if (off + MEM_READ_SIZE > gui->file_mem.size) {
			n_read = gui->file_mem.size - off;
		}

		if (g_input_stream_read (G_INPUT_STREAM (input_stream),
		                         &buffer, n_read, NULL, &err) == -1) {
			stlink_gui_set_info_error_message (gui, err->message);
			g_error_free (err);
			goto out_input;
		}
		memcpy (gui->file_mem.memory + off, buffer, n_read);
		gui->progress.fraction = (gdouble) (off + n_read) / gui->file_mem.size;
	}
	g_idle_add ((GSourceFunc) stlink_gui_update_filemem_view, gui);

 out_input:
	g_object_unref (input_stream);
 out:
	g_object_unref (file);
	return NULL;
}
Exemplo n.º 28
0
static int
xml_reader_io_read_cb (void *context,
                       char *buffer,
                       int   len)
{
  GInputStream *stream = (GInputStream *)context;
  g_return_val_if_fail (G_IS_INPUT_STREAM(stream), -1);
  return g_input_stream_read (stream, buffer, len, NULL, NULL);
}
GdkPixbuf *
eel_gdk_pixbuf_load_from_stream_at_size (GInputStream  *stream,
					 int            size)
{
	char buffer[LOAD_BUFFER_SIZE];
	gssize bytes_read;
	GdkPixbufLoader *loader;
	GdkPixbuf *pixbuf;
	gboolean got_eos;
	

	g_return_val_if_fail (stream != NULL, NULL);

	got_eos = FALSE;
	loader = gdk_pixbuf_loader_new ();

	if (size > 0) {
		g_signal_connect (loader, "size-prepared",
				  G_CALLBACK (pixbuf_loader_size_prepared),
				  GINT_TO_POINTER (size));
	}

	while (1) {
		bytes_read = g_input_stream_read (stream, buffer, sizeof (buffer),
						  NULL, NULL);
		
		if (bytes_read < 0) {
			break;
		}
		if (bytes_read == 0) {
			got_eos = TRUE;
			break;
		}
		if (!gdk_pixbuf_loader_write (loader,
					      buffer,
					      bytes_read,
					      NULL)) {
			break;
		}
	}

	g_input_stream_close (stream, NULL, NULL);
	gdk_pixbuf_loader_close (loader, NULL);

	pixbuf = NULL;
	if (got_eos) {
		pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
		if (pixbuf != NULL) {
			g_object_ref (pixbuf);
		}
	}

	g_object_unref (loader);

	return pixbuf;
}
Exemplo n.º 30
0
/*
 * resize_cache:
 * @stream: A #CtplInputStream
 * @new_size: the requested new size of the stream's cache
 * @error: Return location for errors, or %NULL to ignore them
 * 
 * Tries to resize the cache of a #CtplInputStream to @new_size. This may grow
 * or shrink the cache; but it will never loose unread content.
 * Note that the new cache size might not be the requested one even on success:
 * there can be too much still useful cached data or the underlying stream can
 * be too close to the end to fill the full size. In both cases, the cache will
 * be resized to the closest size possible to the request.
 * 
 * Returns: %TRUE on success, %FALSE otherwise
 */
static gboolean
resize_cache (CtplInputStream *stream,
              gsize            new_size,
              GError         **error)
{
  gboolean success = TRUE;
  
  g_return_val_if_fail (new_size > 0, FALSE);
  
  if (new_size > stream->buf_size) {
    gssize read_size;
    gchar *new_buffer;
    
    new_buffer = g_try_realloc (stream->buffer, new_size);
    if (G_UNLIKELY (! new_buffer)) {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
                   "Not enough memory to cache %"G_GSIZE_FORMAT" bytes "
                   "from input", new_size);
      success = FALSE;
    } else {
      stream->buffer = new_buffer;
      read_size = g_input_stream_read (stream->stream,
                                       &stream->buffer[stream->buf_size],
                                       new_size - stream->buf_size,
                                       NULL, error);
      if (read_size < 0) {
        success = FALSE;
      } else {
        stream->buf_size += (gsize)read_size;
      }
    }
  } else if (new_size < stream->buf_size) {
    if (stream->buf_pos >= stream->buf_size) {
      /* we are at the end of the buffer, no need to care about its content,
       * just retrieve next data */
      stream->buf_size = new_size;
      stream->buffer = g_realloc (stream->buffer, stream->buf_size);
      success = ensure_cache_filled (stream, error);
    } else {
      gsize new_start = stream->buf_size - new_size;
      
      if (new_start > stream->buf_pos) {
        /* we have too much data in the buffer, cannot shrink to the requested
         * size; shrink as much as we can */
        new_start = stream->buf_size - stream->buf_pos;
        new_size = stream->buf_size - new_start;
      }
      /* OK, move the data and resize */
      memmove (stream->buffer, &stream->buffer[new_start], new_size);
      stream->buf_size = new_size;
      stream->buffer = g_realloc (stream->buffer, stream->buf_size);
    }
  }
  
  return success;
}