/**
 * mate_keyring_memory_try_realloc:
 * @p: The pointer to reallocate or NULL to allocate a new block.
 * @sz: The new desired size of the memory block.
 *
 * Reallocate a block of mate-keyring non-pageable memory.
 *
 * Glib memory is also reallocated correctly when passed to this function.
 * If called with a null pointer, then a new block of memory is allocated.
 * If called with a zero size, then the block of memory is freed.
 *
 * If memory cannot be allocated, NULL is returned and the original block
 * of memory remains intact.
 *
 * Return value: The new block, or NULL if memory cannot be allocated.
 * The memory block should be freed with mate_keyring_memory_free()
 */
gpointer
mate_keyring_memory_try_realloc (gpointer p, gulong sz)
{
	gpointer n;

	if (!p) {
		return mate_keyring_memory_try_alloc (sz);
	} else if (!sz) {
		 mate_keyring_memory_free (p);
		 return NULL;
	} else if (!egg_secure_check (p)) {
		return g_try_realloc (p, sz);
	}

	/* First try and ask secure memory to reallocate */
	n = egg_secure_realloc_full (p, sz, 0);

	g_assert (n);

	return n;
}
static gboolean
pem_parse_block (const gchar *data, gsize n_data, guchar **decoded, gsize *n_decoded,
                 GHashTable **headers)
{
	const gchar *x, *hbeg, *hend;
	const gchar *p, *end;
	gint state = 0;
	guint save = 0;
	
	g_assert (data);
	g_assert (n_data);
	
	g_assert (decoded);
	g_assert (n_decoded);
	
	p = data;
	end = p + n_data;
	
	hbeg = hend = NULL;
	
	/* Try and find a pair of blank lines with only white space between */
	while (hend == NULL) {
		x = memchr (p, '\n', end - p);
		if (!x)
			break;
		++x;
		while (isspace (*x)) {
			/* Found a second line, with only spaces between */
			if (*x == '\n') {
				hbeg = data;
				hend = x;
				break;
			/* Found a space between two lines */
			} else {
				++x;
			}
		}
		
		/* Try next line */
		p = x;
	}

	/* Headers found? */	
	if (hbeg && hend) {
		data = hend;
		n_data = end - data;
	}
	
	*n_decoded = (n_data * 3) / 4 + 1;
	if (egg_secure_check (data))
		*decoded = egg_secure_alloc (*n_decoded);
	else
		*decoded = g_malloc0 (*n_decoded);
	g_return_val_if_fail (*decoded, FALSE);
	
	*n_decoded = g_base64_decode_step (data, n_data, *decoded, &state, &save);
	if (!*n_decoded) {
		egg_secure_free (*decoded);
		return FALSE;
	}
	
	if (headers && hbeg && hend) 
		parse_header_lines (hbeg, hend, headers);
	
	return TRUE;
}
/**
 * mate_keyring_memory_is_secure:
 * @p: The pointer to check
 *
 * Check if a pointer is in non-pageable memory allocated by mate-keyring.
 *
 * Return value: Whether the memory is non-pageable or not
 */
gboolean
mate_keyring_memory_is_secure (gpointer p)
{
	return egg_secure_check (p) ? TRUE : FALSE;
}