예제 #1
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();
}
예제 #2
0
파일: data.c 프로젝트: seehuhn/jvqplot
static double *
parse_data_file(GDataInputStream *in,
                int *rows_ret, int *cols_ret, GError **err_ret)
{
  GError *err = NULL;
  int result_used = 0;
  int result_allocated = 256;
  double *result = g_new(double, result_allocated);

  gchar *line;
  int rows = 0;
  int cols = 0;

  while ((line = g_data_input_stream_read_line(in, NULL, NULL, &err))) {
    gchar **words = g_strsplit_set(line, " \t", 0);
    gboolean is_empty = ! words[0];
    if (is_empty || words[0][0] == '#') goto next_line;

    int n = 0;
    while (words[n]) ++n;
    if (cols == 0) {
      cols = n;
    } else if (n < cols
               && g_buffered_input_stream_get_available(G_BUFFERED_INPUT_STREAM(in)) == 0) {
      g_set_error(&err, JVQPLOT_ERROR, JVQPLOT_ERROR_INCOMPLETE,
                  "incomplete input");
      goto next_line;
    } else if (cols != n) {
      g_set_error(&err, JVQPLOT_ERROR, JVQPLOT_ERROR_CORRUPTED,
                  "invalid data (malformed matrix)");
      goto next_line;
    }

    /* if there is only one column, prepend the index */
    if (cols == 1) {
      if (result_used >= result_allocated) {
        result_allocated *= 2;
        result = g_renew(double, result, result_allocated);
      }
      result[result_used++] = rows+1;
    }

    int i;
    for (i=0; i<cols; ++i) {
      char *endptr;
      double x = strtod(words[i], &endptr);
      if (*endptr) {
        g_set_error(&err, JVQPLOT_ERROR, JVQPLOT_ERROR_CORRUPTED,
                    "invalid data (malformed number)");
        goto next_line;
      }
      if (result_used >= result_allocated) {
        result_allocated *= 2;
        result = g_renew(double, result, result_allocated);
      }
      result[result_used++] = x;
    }
예제 #3
0
/**
 * g_vfs_ftp_connection_enable_tls:
 * @conn: a connection without an active data connection
 * @server_identity: address of the server used to verify the certificate
 * @cb: callback called if there's a verification error
 * @user_data: user data passed to @cb
 * @cancellable: cancellable to interrupt wait
 * @error: %NULL or location to take a potential error
 *
 * Tries to enable TLS on the given @connection. If setting up TLS fails,
 * %FALSE will be returned and @error will be set. When this function fails,
 * you need to check if the connection is still usable. It might have been
 * closed.
 *
 * Returns: %TRUE on success, %FALSE otherwise.
 **/
gboolean
g_vfs_ftp_connection_enable_tls (GVfsFtpConnection * conn,
                                 GSocketConnectable *server_identity,
                                 CertificateCallback cb,
                                 gpointer            user_data,
                                 GCancellable *      cancellable,
                                 GError **           error)
{
  GIOStream *secure;

  g_return_val_if_fail (conn != NULL, FALSE);
  g_return_val_if_fail (conn->data == NULL, FALSE);
  g_return_val_if_fail (!conn->waiting_for_reply, FALSE);
  g_return_val_if_fail (g_buffered_input_stream_get_available (G_BUFFERED_INPUT_STREAM (conn->commands_in)) == 0, FALSE);

  secure = g_tls_client_connection_new (conn->commands,
                                        server_identity,
                                        error);
  if (secure == NULL)
    return FALSE;

  g_object_unref (conn->commands);
  conn->commands = secure;
  create_input_stream (conn);

  g_signal_connect (secure, "accept-certificate", G_CALLBACK (cb), user_data);

  if (!g_tls_connection_handshake (G_TLS_CONNECTION (secure),
                                   cancellable,
                                   error))
    {
      /* Close here to be sure it won't get used anymore */
      g_io_stream_close (secure, cancellable, NULL);
      return FALSE;
    }

  return TRUE;
}
예제 #4
0
static void done_async_read(GObject *obj, GAsyncResult *res, gpointer user_data) {
	struct ekg_connection *c = user_data;
	GError *err = NULL;
	gssize rsize;
	GBufferedInputStream *instr = G_BUFFERED_INPUT_STREAM(obj);

	rsize = g_buffered_input_stream_fill_finish(instr, res, &err);

	if (rsize <= 0) {
		if (rsize == -1) /* error */
			debug_error("done_async_read(), read failed: %s\n", err ? err->message : NULL);
		else { /* EOF */
#if NEED_SLAVERY
			if (c->master) /* let the master handle it */
				return;
#endif
			debug_function("done_async_read(), EOF\n");
			if (g_buffered_input_stream_get_available(instr) > 0)
				c->callback(c->instream, c->priv_data);

			err = g_error_new_literal(
					EKG_CONNECTION_ERROR,
					EKG_CONNECTION_ERROR_EOF,
					"Connection terminated");
		}

		c->failure_callback(c->instream, err, c->priv_data);
		ekg_connection_remove(c);
		g_error_free(err);
		return;
	}

	debug_function("done_async_read(): read %d bytes\n", rsize);

	c->callback(c->instream, c->priv_data);
	setup_async_read(c);
}
예제 #5
0
static VALUE
rg_available(VALUE self)
{
        return GSIZE2RVAL(g_buffered_input_stream_get_available(_SELF(self)));
}
예제 #6
0
char *
g_data_input_stream_read_upto (GDataInputStream  *stream,
                               const gchar       *stop_chars,
                               gssize             stop_chars_len,
                               gsize             *length,
                               GCancellable      *cancellable,
                               GError           **error)
{
  GBufferedInputStream *bstream;
  gsize checked;
  gssize found_pos;
  gssize res;
  char *data_until;

  g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);

  if (stop_chars_len < 0)
    stop_chars_len = strlen (stop_chars);

  bstream = G_BUFFERED_INPUT_STREAM (stream);

  checked = 0;

  while ((found_pos = scan_for_chars (stream, &checked, stop_chars, stop_chars_len)) == -1)
    {
      if (g_buffered_input_stream_get_available (bstream) ==
          g_buffered_input_stream_get_buffer_size (bstream))
        g_buffered_input_stream_set_buffer_size (bstream,
                                                 2 * g_buffered_input_stream_get_buffer_size (bstream));

      res = g_buffered_input_stream_fill (bstream, -1, cancellable, error);
      if (res < 0)
        return NULL;
      if (res == 0)
        {
          /* End of stream */
          if (g_buffered_input_stream_get_available (bstream) == 0)
            {
              if (length)
                *length = 0;
              return NULL;
            }
          else
            {
              found_pos = checked;
              break;
            }
        }
    }

  data_until = g_malloc (found_pos + 1);

  res = g_input_stream_read (G_INPUT_STREAM (stream),
                             data_until,
                             found_pos,
                             NULL, NULL);
  if (length)
    *length = (gsize)found_pos;
  g_warn_if_fail (res == found_pos);
  data_until[found_pos] = 0;

  return data_until;
}