static int
mail_transaction_log_file_insert_read(struct mail_transaction_log_file *file,
				      uoff_t offset)
{
	void *data;
	size_t size;
	ssize_t ret;

	size = file->buffer_offset - offset;
	buffer_copy(file->buffer, size, file->buffer, 0, (size_t)-1);

	data = buffer_get_space_unsafe(file->buffer, 0, size);
	ret = pread_full(file->fd, data, size, offset);
	if (ret > 0) {
		/* success */
		file->buffer_offset -= size;
		return 1;
	}

	/* failure. don't leave ourself to inconsistent state */
	buffer_copy(file->buffer, 0, file->buffer, size, (size_t)-1);
	buffer_set_used_size(file->buffer, file->buffer->used - size);

	if (ret == 0) {
		mail_transaction_log_file_set_corrupted(file, "file shrank");
		return 0;
	} else if (errno == ESTALE) {
		/* log file was deleted in NFS server, fail silently */
		return 0;
	} else {
		log_file_set_syscall_error(file, "pread()");
		return -1;
	}
}
Example #2
0
off_t
buffer_copy_IntInt(int Iin, int Tin,
                   void *Pfilter, int Tfilter,
                   int Iout, int Tout,
                   off_t limit, struct dpkg_error *err)
{
	struct buffer_data read_data = { .type = Tin, .arg.i = Iin };
	struct buffer_data filter = { .type = Tfilter, .arg.ptr = Pfilter };
	struct buffer_data write_data = { .type = Tout, .arg.i = Iout };

	return buffer_copy(&read_data, &filter, &write_data, limit, err);
}

off_t
buffer_copy_IntPtr(int Iin, int Tin,
                   void *Pfilter, int Tfilter,
                   void *Pout, int Tout,
                   off_t limit, struct dpkg_error *err)
{
	struct buffer_data read_data = { .type = Tin, .arg.i = Iin };
	struct buffer_data filter = { .type = Tfilter, .arg.ptr = Pfilter };
	struct buffer_data write_data = { .type = Tout, .arg.ptr = Pout };

	return buffer_copy(&read_data, &filter, &write_data, limit, err);
}

static off_t
buffer_skip(struct buffer_data *input, off_t limit, struct dpkg_error *err)
{
	struct buffer_data output;
	struct buffer_data filter;

	switch (input->type) {
	case BUFFER_READ_FD:
		if (lseek(input->arg.i, limit, SEEK_CUR) != -1)
			return limit;
		if (errno != ESPIPE)
			return dpkg_put_errno(err, _("failed to seek"));
		break;
	default:
		internerr("unknown data type %i", input->type);
	}

	output.type = BUFFER_WRITE_NULL;
	output.arg.ptr = NULL;
	filter.type = BUFFER_FILTER_NULL;
	filter.arg.ptr = NULL;

	return buffer_copy(input, &filter, &output, limit, err);
}

off_t
buffer_skip_Int(int I, int T, off_t limit, struct dpkg_error *err)
{
	struct buffer_data input = { .type = T, .arg.i = I };

	return buffer_skip(&input, limit, err);
}
Example #3
0
/* Merge tag and secondary buffer back to primary buffer.  */
static void merge_buffers(void)
{
    buffer_copy(&primary_buf, &tag_buf);
    buffer_copy(&primary_buf, &secondary_buf);

    buffer_clear(&tag_buf);
    buffer_clear(&secondary_buf);

    current_buf = &primary_buf;
    /* We just processed a start-tag so bump up indent_delta.  */
    indent_delta++;
}
Example #4
0
/*
 * List all of the users.
 *
 * Returns a buffer containing a list of all the public
 * signing keys of the user.
 *
 * The buffer is heap allocated, so don't forget to free it!
 */
return_status user_store_list(buffer_t ** const list, user_store * const store) {
	return_status status = return_status_init();

	if ((list == NULL) || (store == NULL)) {
		throw(INVALID_INPUT, "Invalid input to user_store_list.");
	}

	*list = buffer_create_on_heap(PUBLIC_MASTER_KEY_SIZE * store->length, PUBLIC_MASTER_KEY_SIZE * store->length);
	throw_on_failed_alloc(*list);

	user_store_node *current_node = store->head;
	for (size_t i = 0; (i < store->length) && (current_node != NULL); i++) {
		int status_int = buffer_copy(
				*list,
				i * PUBLIC_MASTER_KEY_SIZE,
				current_node->public_signing_key,
				0,
				current_node->public_signing_key->content_length);
		if (status_int != 0) { //copying went wrong
			throw(BUFFER_ERROR, "Failed to copy public master key to user list.");
		}

		user_store_node *next_node = current_node->next;
		current_node = next_node;
	}

cleanup:
	on_error {
		if (list != NULL) {
				buffer_destroy_from_heap_and_null_if_valid(*list);
		}
	}

	return status;
}
Example #5
0
void pbkdf(buffer password, buffer salt, word block_index, int rounds, buffer *key) {
	assert(key->length == 32);

	hmac base_mac = hmac_init(password), mac;
	buffer temp = buffer_calloc(32);

	// First iteration
	// U1 = HMAC(P, S || (i+1))
	mac = hmac_clone(base_mac);
	hmac_update(&mac, salt);
	buffer index = buffer_calloc(4); // 1 word
	index.words[0] = block_index+1;
	hmac_update(&mac, index);
	buffer_free(&index);
	hmac_end(&mac, key);
	buffer_copy(&temp, *key);

	// Next iterations
	// U_(n+1) = U_n ^ HMAC(P, U_n)
	for (int i=1; i<rounds; i++) {
		mac = hmac_clone(base_mac);
		hmac_update(&mac, temp);
		hmac_end(&mac, &temp);
		buffer_xor(key, temp);
	}

	buffer_free(&temp);
	hmac_free(&base_mac);
}
Example #6
0
/*
 * Add a given buffer by copy to the end of a list
 * Careful buffer is passed by reference,
 * and must be free with list_free()
 */
void list_add_by_copy(list * l, buffer * value)
{
    list_node *ln;
    buffer *tmp;

    assert(l);
    assert(value);
    assert(l->size < UINT_MAX);

    ln = list_node_init();
    tmp = buffer_init();

    buffer_copy(tmp, value);
    ln->value = tmp;

    if (!l->first) {
        ln->prev = NULL;
        l->first = ln;
    } else {
        ln->prev = l->last;
        l->last->next = ln;
    }

    l->last = ln;
    l->last->next = NULL;
    l->size++;
}
Example #7
0
/*
 * Return the column's name matching the specified number from table
 * (Only use in specific FE position function, so not directly inside
 *  storage handle mechanism)
 */
buffer *ows_psql_column_name(ows * o, buffer * layer_name, int number)
{
    buffer *sql;
    PGresult *res;
    buffer *column;

    assert(o);
    assert(layer_name);

    sql = buffer_init();
    column = buffer_init();

    buffer_add_str(sql, "SELECT a.attname FROM pg_class c, pg_attribute a, pg_type t WHERE c.relname ='");
    buffer_copy(sql, layer_name);
    buffer_add_str(sql, "' AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid AND a.attnum = ");
    buffer_add_int(sql, number);

    res = ows_psql_exec(o, sql->buf);
    buffer_free(sql);

    if (PQresultStatus(res) != PGRES_TUPLES_OK || PQntuples(res) != 1) {
        PQclear(res);
        return column;
    }

    buffer_add_str(column, PQgetvalue(res, 0, 0));
    PQclear(res);

    return column;
}
Example #8
0
/*
 * Add a given list to the end of a list
 * Careful list is passed by reference,
 * and must be free with list_free()
 */
void list_add_list(list * l, list * l_to_add)
{
    list_node *ln, *ln_parse;

    assert(l);
    assert(l_to_add);

    for (ln_parse = l_to_add->first ; ln_parse ; ln_parse = ln_parse->next) {
        if (!in_list(l, ln_parse->value)) {
            ln = list_node_init();

            ln->value = buffer_init();
            buffer_copy(ln->value, ln_parse->value);

            if (!l->first) {
                ln->prev = NULL;
                l->first = ln;
            } else {
                ln->prev = l->last;
                l->last->next = ln;
            }

            l->last = ln;
            l->last->next = NULL;
            l->size++;
        }
    }
}
Example #9
0
/**
 * Parses a Content-Length header value.
 */
int parse_header_content_length(struct parser *p) {
    char c, *buffer;
    int i, m, r;

    if (ready(p) < 3)
        return PARSING_WAIT;
    c = buffer_get(&(p->buffer), p->mark);

    for (m = 1; isdigit(c) || c == ' ' || c == '\t'; m++) {
        if (ready(p) < 3 + m)
            return PARSING_WAIT;
        c = buffer_get(&(p->buffer), p->mark + m);
    }

    buffer = buffer_copy(&(p->buffer), p->mark, m - 1);
    if (buffer == NULL) {
        p->error = E_MEMORY;
        return PARSING_ERROR;
    }
    advance_mark(p, m - 1);

    r = parse_constant(p, CRLF, 2);
    if (r != PARSING_DONE) {
        free(buffer);
        return r;
    }

    errno = 0;
    p->request.content_length = strtol(buffer, NULL, 10);
    free(buffer);
    return (errno == 0) ? PARSING_DONE : PARSING_ERROR;
}
Example #10
0
/*
 * Set projection value into srs structure
 */
bool ows_srs_set(ows * o, ows_srs * c, const buffer * auth_name, int auth_srid)
{
  PGresult *res;
  buffer *sql;

  assert(o);
  assert(c);
  assert(o->pg);
  assert(auth_name);

  sql = buffer_init();
  buffer_add_str(sql, "SELECT srid, position('+units=m ' in proj4text)");
  buffer_add_str(sql, ", (position('AXIS[\"X\",NORTH]]' in srtext) + position('AXIS[\"Northing\",NORTH]]' in srtext))");
  buffer_add_str(sql, " FROM spatial_ref_sys WHERE auth_name='");
  buffer_copy(sql, auth_name);
  buffer_add_str(sql, "' AND auth_srid=");
  buffer_add_int(sql, auth_srid);

  res = ows_psql_exec(o, sql->buf);
  buffer_free(sql);

  /* If query dont return exactly 1 result, it means projection is not handled */
  if (PQresultStatus(res) != PGRES_TUPLES_OK || PQntuples(res) != 1) {
    PQclear(res);
    return false;
  }

  buffer_empty(c->auth_name);
  buffer_copy(c->auth_name, auth_name);
  c->auth_srid = auth_srid;

  c->srid = atoi(PQgetvalue(res, 0, 0));

  /* Such a way to know if units is meter or degree */
  if (atoi(PQgetvalue(res, 0, 1)) == 0)
    c->is_degree = true;
  else
    c->is_degree = false;

  /* Is easting-northing SRID ? */
  if (atoi(PQgetvalue(res, 0, 2)) != 0)
    c->is_eastern_axis = true;

  PQclear(res);
  return true;
}
Example #11
0
static void stack_insert(parr *stack, const struct buffer *buf, int index_)
{
	int index = stack->len + index_;
	parr_add(stack, NULL);
	memmove(&stack->data[index + 1], &stack->data[index],
		sizeof(void *) * (stack->len - index - 1));
	stack->data[index] = buffer_copy(buf->p, buf->len);
}
Example #12
0
void
octstr_copy(octstr_t *dst, const octstr_t *src)
{
	assert(src != NULL && dst != NULL
	    &&  src->buf != NULL && dst->buf != NULL);

	buffer_copy(dst->buf, src->buf, buffer_size(src->buf));
}
Example #13
0
/* Merge tag and secondary buffer to primary buffer. Force newlines if
   necessary. This routine is used with start-tags.  */
static void merge_buffers_start_tag(void)
{
    if (force_newline_before_start_tag) {
        force_newline_before_tag(&primary_buf);
    }
    buffer_copy(&primary_buf, &tag_buf);
    indent_delta++;
    if (force_newline_after_start_tag) {
        force_newline_after_tag(&primary_buf);
    }
    buffer_copy(&primary_buf, &secondary_buf);

    buffer_clear(&tag_buf);
    buffer_clear(&secondary_buf);

    current_buf = &primary_buf;
}
Example #14
0
static void stack_insert(GPtrArray *stack, const struct buffer *buf, int index_)
{
	int index = stack->len + index_;
	g_ptr_array_add(stack, NULL);
	memmove(&stack->pdata[index + 1], &stack->pdata[index],
		sizeof(gpointer) * (stack->len - index - 1));
	stack->pdata[index] = buffer_copy(buf->p, buf->len);
}
Example #15
0
int
main()
{
	struct buffer *a, *b;

	a = buffer_new();
	b = buffer_new();
	
	buffer_putc(a, 'a');
	buffer_putc(a, 'g');
	buffer_copy(b, a, buffer_size(a));
	buffer_copy(b, a, buffer_size(a));

	buffer_zero(b);
	
	printf("buf sz = %d\n", buffer_size(b));
	return 0;
}
Example #16
0
/*
 * Add a double to a given buffer
 */
void buffer_add_double(buffer * buf, double f)
{
  buffer *b;

  assert(buf);

  b = buffer_ftoa(f);
  buffer_copy(buf, b);
  buffer_free(b);
}
Example #17
0
/*
 * Add an int to a given buffer
 */
void buffer_add_int(buffer * buf, int i)
{
  buffer *b;

  assert(buf);

  b = buffer_itoa(i);
  buffer_copy(buf, b);
  buffer_free(b);
}
Example #18
0
void test_buffer()
{
    struct const_buffer buf0 = {"data", 4};
    struct const_buffer buf1 = {"data", 4};

    assert(buffer_equal(&buf0.p, &buf1.p) == true);

    struct buffer* buf2 = buffer_copy(&buf0.p, buf0.len);
    buffer_free(buf2);
}
Example #19
0
/*
 * Concatenate a buffer to the first.
 *
 * Return 0 on success.
 */
int buffer_concat(
		buffer_t * const destination,
		const buffer_t * const source) {
	return buffer_copy(
			destination,
			destination->content_length,
			source,
			0,
			source->content_length);
}
Example #20
0
/**
 *  call-seq:
 *    Rev::Buffer#to_str -> String
 * 
 * Convert the Buffer to a String.  The original buffer is unmodified.
 */
static VALUE Rev_Buffer_to_str(VALUE self) {
	VALUE str;
	struct buffer *buf;
	
	Data_Get_Struct(self, struct buffer, buf);
	
	str = rb_str_new(0, buf->size);
	buffer_copy(buf, RSTRING_PTR(str), buf->size);
  
  return str;
}
Example #21
0
void buffer_insert_zero(buffer_t *_buf, size_t pos, size_t data_size)
{
	struct real_buffer *buf = (struct real_buffer *)_buf;

	if (pos >= buf->used)
		buffer_write_zero(_buf, pos, data_size);
	else {
		buffer_copy(_buf, pos + data_size, _buf, pos, (size_t)-1);
		memset(buf->w_buffer + pos, 0, data_size);
	}
}
Example #22
0
void buffer_insert(buffer_t *_buf, size_t pos,
		   const void *data, size_t data_size)
{
	struct real_buffer *buf = (struct real_buffer *)_buf;

	if (pos >= buf->used)
		buffer_write(_buf, pos, data, data_size);
	else {
		buffer_copy(_buf, pos + data_size, _buf, pos, (size_t)-1);
		memcpy(buf->w_buffer + pos, data, data_size);
	}
}
Example #23
0
Buffer* buffer_copy_ts (Buffer* buffer, int32_t start, int32_t length) {
    assert(NULL != buffer);
    assert(NULL != buffer->mutex);

    pthread_mutex_lock(buffer->mutex);

    Buffer* ret = buffer_copy(buffer, start, length);

    pthread_mutex_unlock(buffer->mutex);

    return ret;
}
Example #24
0
/*
 * Copy the whole buffer struct to an another struct
 */
buffer *buffer_clone(buffer * buf)
{
  buffer *b;

  assert(buf);
  assert(buf->buf);

  b = buffer_init();
  buffer_copy(b, buf);
  
  return b;
}
Example #25
0
/*
 * Return the number of rows returned by the specified requests
 */
int ows_psql_number_features(ows * o, list * from, list * where)
{
    buffer *sql;
    PGresult *res;
    list_node *ln_from, *ln_where;
    int nb;

    assert(o);
    assert(from);
    assert(where);

    nb = 0;

    /* checks if from list and where list have the same size */
    if (from->size != where->size) return nb;


    for (ln_from = from->first, ln_where = where->first;
            ln_from;
            ln_from = ln_from->next, ln_where = ln_where->next) {
        sql = buffer_init();

        /* execute the request */
        buffer_add_str(sql, "SELECT count(*) FROM \"");
        buffer_copy(sql, ln_from->value);
        buffer_add_str(sql, "\" ");
        buffer_copy(sql, ln_where->value);
        res = ows_psql_exec(o, sql->buf);
        buffer_free(sql);

        if (PQresultStatus(res) != PGRES_TUPLES_OK) {
            PQclear(res);
            return -1;
        }
        nb = nb + atoi(PQgetvalue(res, 0, 0));
        PQclear(res);
    }

    return nb;
}
Example #26
0
/*
 * Return an array of prefixes and associated NS URI of a layer's list
 */
array *ows_layer_list_namespaces(ows_layer_list * ll)
{
    buffer *ns_prefix, *ns_uri;
    ows_layer_node *ln;
    array *namespaces = array_init();

    assert(ll);

    for (ln = ll->first; ln ; ln = ln->next) {
        if (    ln->layer->ns_prefix && ln->layer->ns_prefix->use 
             && !array_is_key(namespaces, ln->layer->ns_prefix->buf)) {

            ns_prefix = buffer_init();
            ns_uri = buffer_init();
            buffer_copy(ns_prefix, ln->layer->ns_prefix);
            buffer_copy(ns_uri, ln->layer->ns_uri);
            array_add(namespaces, ns_prefix, ns_uri);
        }
    }

    return namespaces;
}
Example #27
0
void *array_insert_space_i(struct array *array, unsigned int idx)
{
	void *data;
	size_t pos;

	pos = idx * array->element_size;
	buffer_copy(array->buffer, pos + array->element_size,
		    array->buffer, pos, (size_t)-1);

	data = buffer_get_space_unsafe(array->buffer, pos, array->element_size);
	memset(data, 0, array->element_size);
	return data;
}
Example #28
0
/*
 * Replace all occurences of string 'before' inside the buffer by string 'after'
 */
buffer *buffer_replace(buffer * buf, char *before, char *after)
{
  buffer *new_buf, *rest;
  size_t length;
  char *pos;

  assert(before);
  assert(after);
  assert(buf);

  if (!strcmp(before, after)) return buf; /* To prevent infinite loop */

  new_buf = buffer_init();
  buffer_copy(new_buf, buf);

  pos = strstr(new_buf->buf, before); /* Look for first occurence */
  while (pos) {
    length = strlen(pos);

    buffer_pop(new_buf, length);    /* Copy the first part without occurence */
    buffer_add_str(new_buf, after); /* Add the string after */

    /* Add the remaining string */
    rest = buffer_init();
    buffer_copy(rest, buf);
    buffer_shift(rest, buf->use - length + strlen(before));
    buffer_copy(new_buf, rest);
    buffer_free(rest);

    pos = strstr(new_buf->buf, before); /* Search the next occurence */
  }

  buffer_empty(buf);
  buffer_copy(buf, new_buf);
  buffer_free(new_buf);

  return buf;
}
Example #29
0
/*
 * Retrieve not_null columns of a table related a given layer
 */
static void ows_storage_fill_not_null(ows * o, ows_layer * l)
{
  int i, nb_result;
  buffer *sql, *b;
  PGresult *res;

  assert(o);
  assert(l);
  assert(l->storage);

  sql = buffer_init();
  buffer_add_str(sql, "SELECT a.attname AS field FROM pg_class c, pg_attribute a, pg_type t, pg_namespace n ");
  buffer_add_str(sql, "WHERE n.nspname = '");
  buffer_copy(sql, l->storage->schema);
  buffer_add_str(sql, "' AND c.relname = '");
  buffer_copy(sql, l->storage->table);
  buffer_add_str(sql, "' AND c.relnamespace = n.oid AND a.attnum > 0 AND a.attrelid = c.oid ");
  buffer_add_str(sql, "AND a.atttypid = t.oid AND a.attnotnull = 't'");

  res = ows_psql_exec(o, sql->buf);
  buffer_free(sql);

  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
    PQclear(res);
    ows_error(o, OWS_ERROR_REQUEST_SQL_FAILED, "Unable to access pg_* tables.", "not_null columns");
    return;
  }

  nb_result = PQntuples(res);
  if (nb_result) l->storage->not_null_columns = list_init();
  for (i = 0 ; i < nb_result ; i++) {
    b = buffer_init();
    buffer_add_str(b, PQgetvalue(res, i, 0));
    list_add(l->storage->not_null_columns, b);
  }

  PQclear(res);
}
Example #30
0
static void
push_readfile_response (aid_t to,
			fs_readfile_error_t error,
			size_t size,
			bd_t bd)
{
  readfile_response_t* res = malloc (sizeof (readfile_response_t));
  memset (res, 0, sizeof (readfile_response_t));
  res->to = to;
  fs_readfile_response_init (&res->response, error, size);
  res->bd = (bd != -1) ? buffer_copy (bd) : -1;
  *readfile_response_tail = res;
  readfile_response_tail = &res->next;
}