summaryrefslogtreecommitdiff
path: root/gtk/gtksecurememoryprivate.h
blob: bf9c5f3803176d0070cf174fb5219b893cb6db9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* gtksecurememoryprivate.h - Allocator for non-pageable memory

   Copyright 2007  Stefan Walter
   Copyright 2020  GNOME Foundation

   SPDX-License-Identifier: LGPL-2.0-or-later

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   see <http://www.gnu.org/licenses/>.

   Author: Stef Walter <stef@memberwebs.com>
*/

#pragma once

#include <stdlib.h>
#include <glib.h>

/*
 * Main functionality
 *
 * Allocations return NULL on failure.
 */

#define GTK_SECURE_USE_FALLBACK     0x0001

void *          gtk_secure_alloc_full   (const char *tag,
                                         size_t length,
                                         int options);

void *          gtk_secure_realloc_full (const char *tag,
                                         void *p,
                                         size_t length,
                                         int options);

void            gtk_secure_free         (void *p);

void            gtk_secure_free_full    (void *p,
                                         int fallback);

void            gtk_secure_clear        (void *p,
                                         size_t length);

int             gtk_secure_check        (const void *p);

void            gtk_secure_validate     (void);

char *          gtk_secure_strdup_full  (const char *tag,
                                         const char *str,
                                         int options);

char *          gtk_secure_strndup_full (const char *tag,
                                         const char *str,
                                         size_t length,
                                         int options);

void            gtk_secure_strclear     (char *str);

void            gtk_secure_strfree      (char *str);

/* Simple wrappers */

static inline void *gtk_secure_alloc (size_t length) {
  return gtk_secure_alloc_full ("gtk", length, GTK_SECURE_USE_FALLBACK);
}

static inline void *gtk_secure_realloc (void *p, size_t length) {
  return gtk_secure_realloc_full ("gtk", p, length, GTK_SECURE_USE_FALLBACK);
}

static inline void *gtk_secure_strdup (const char *str) {
  return gtk_secure_strdup_full ("gtk", str, GTK_SECURE_USE_FALLBACK);
}

static inline void *gtk_secure_strndup (const char *str, size_t length) {
  return gtk_secure_strndup_full ("gtk", str, length, GTK_SECURE_USE_FALLBACK);
}

typedef struct {
  const char *tag;
  size_t request_length;
  size_t block_length;
} gtk_secure_rec;

gtk_secure_rec *   gtk_secure_records    (unsigned int *count);