annotate dlib/dlib.h @ 2291:2af950507af5

password shredding Justus had written a patch for this, but the list archive didn't want to disgorge it, so I wrote one.
author corvid <corvid@lavabit.com>
date Thu, 29 Sep 2011 15:59:46 +0000
parents f34b803b8639
children 29c53b9ebe92
rev   line source
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
1 #ifndef __DLIB_H__
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
2 #define __DLIB_H__
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
3
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
4 #include <stdio.h> /* for FILE* */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
5 #include <stddef.h> /* for size_t */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
6 #include <stdarg.h> /* for va_list */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
7 #include <string.h> /* for strerror */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
8 #include <strings.h> /* for strcasecmp, strncasecmp (POSIX 2001) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
9
1104
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
10 #include "d_size.h"
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
11
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12 #ifdef __cplusplus
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 extern "C" {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14 #endif /* __cplusplus */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17 *-- Common macros -----------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
18 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
19 #ifndef FALSE
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
20 #define FALSE (0)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23 #ifndef TRUE
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24 #define TRUE (!FALSE)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25 #endif
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
26
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27 #undef MAX
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30 #undef MIN
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32
1221
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1106
diff changeset
33 /* Handle signed char */
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1106
diff changeset
34 #define dIsspace(c) isspace((uchar_t)(c))
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1106
diff changeset
35 #define dIsalnum(c) isalnum((uchar_t)(c))
f34b803b8639 Handle signed chars. Aadded dIsspace() and dIsalnum() to dlib
Jorge Arellano Cid <jcid@dillo.org>
parents: 1106
diff changeset
36
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38 *-- Casts -------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40 /* TODO: include a void* size test in configure.in */
4
9a9338f78888 +- Connected signals to <li> elements (fixes links within lists).
jcid
parents: 0
diff changeset
41 /* (long) works for both 32bit and 64bit */
9a9338f78888 +- Connected signals to <li> elements (fixes links within lists).
jcid
parents: 0
diff changeset
42 #define VOIDP2INT(p) ((long)(p))
306
7a76f872ce73 - Eliminated gcc 4.2.3 warnings on 64bit OS.
jcid
parents: 167
diff changeset
43 #define INT2VOIDP(i) ((void*)((long)(i)))
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 *-- Memory -------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 #define dNew(type, count) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 ((type *) dMalloc ((unsigned) sizeof (type) * (count)))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 #define dNew0(type, count) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 ((type *) dMalloc0 ((unsigned) sizeof (type) * (count)))
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53 void *dMalloc (size_t size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54 void *dRealloc (void *mem, size_t size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 void *dMalloc0 (size_t size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56 void dFree (void *mem);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59 *- Debug macros --------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
60 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61 #define D_STMT_START do
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 #define D_STMT_END while (0)
786
17d77a1950d8 Added: dReturn_if() and dReturn_val_if() macros to Dlib.
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
63 #define dReturn_if(expr) \
17d77a1950d8 Added: dReturn_if() and dReturn_val_if() macros to Dlib.
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
64 D_STMT_START{ \
17d77a1950d8 Added: dReturn_if() and dReturn_val_if() macros to Dlib.
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
65 if (expr) { return; }; \
17d77a1950d8 Added: dReturn_if() and dReturn_val_if() macros to Dlib.
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
66 }D_STMT_END
1106
5bab6f4dc3cc dReturn_val_if arg
corvid <corvid@lavabit.com>
parents: 1104
diff changeset
67 #define dReturn_val_if(expr,val) \
786
17d77a1950d8 Added: dReturn_if() and dReturn_val_if() macros to Dlib.
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
68 D_STMT_START{ \
17d77a1950d8 Added: dReturn_if() and dReturn_val_if() macros to Dlib.
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
69 if (expr) { return val; }; \
17d77a1950d8 Added: dReturn_if() and dReturn_val_if() macros to Dlib.
Jorge Arellano Cid <jcid@dillo.org>
parents: 306
diff changeset
70 }D_STMT_END
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71 #define dReturn_if_fail(expr) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 D_STMT_START{ \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 if (!(expr)) { return; }; \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74 }D_STMT_END
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 #define dReturn_val_if_fail(expr,val) \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 D_STMT_START{ \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 if (!(expr)) { return val; }; \
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 }D_STMT_END
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 *- C strings -----------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83 char *dStrdup(const char *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 char *dStrndup(const char *s, size_t sz);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 char *dStrconcat(const char *s1, ...);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 char *dStrstrip(char *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 char *dStrnfill(size_t len, char c);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 char *dStrsep(char **orig, const char *delim);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 char *dStristr(const char *haystack, const char *needle);
2291
2af950507af5 password shredding
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
90 void dStrshred(char *s);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 /* these are in POSIX 2001. Could be implemented if a port requires it */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93 #define dStrcasecmp strcasecmp
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 #define dStrncasecmp strncasecmp
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 #define dStrerror strerror
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98 *-- dStr ---------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 #define Dstr_char_t char
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 typedef struct _dstr {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 int sz; /* allocated size (private) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 int len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105 Dstr_char_t *str;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 } Dstr;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108 Dstr *dStr_new (const char *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 Dstr *dStr_sized_new (int sz);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 void dStr_fit (Dstr *ds);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 void dStr_free (Dstr *ds, int all);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 void dStr_append_c (Dstr *ds, int c);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 void dStr_append (Dstr *ds, const char *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 void dStr_append_l (Dstr *ds, const char *s, int l);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 void dStr_insert (Dstr *ds, int pos_0, const char *s);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 void dStr_insert_l (Dstr *ds, int pos_0, const char *s, int l);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117 void dStr_truncate (Dstr *ds, int len);
2291
2af950507af5 password shredding
corvid <corvid@lavabit.com>
parents: 1221
diff changeset
118 void dStr_shred (Dstr *ds);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119 void dStr_erase (Dstr *ds, int pos_0, int len);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120 void dStr_vsprintfa (Dstr *ds, const char *format, va_list argp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 void dStr_vsprintf (Dstr *ds, const char *format, va_list argp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122 void dStr_sprintf (Dstr *ds, const char *format, ...);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123 void dStr_sprintfa (Dstr *ds, const char *format, ...);
157
49d4a18c4928 - Switched URL_DATA type from char* to a dStr.
jcid
parents: 67
diff changeset
124 int dStr_cmp(Dstr *ds1, Dstr *ds2);
163
9b036ecc40a4 - Added dStr_memmem() to dlib.
jcid
parents: 162
diff changeset
125 char *dStr_memmem(Dstr *haystack, Dstr *needle);
167
1540ba94d145 - Added dStr_printable() to dlib.
jcid
parents: 163
diff changeset
126 const char *dStr_printable(Dstr *in, int maxlen);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 *-- dList --------------------------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 struct Dlist_ {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132 int sz; /* allocated size (private) */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 int len;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134 void **list;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135 };
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137 typedef struct Dlist_ Dlist;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 /* dCompareFunc:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140 * Return: 0 if parameters are equal (for dList_find_custom).
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 * Return: 0 if equal, < 0 if (a < b), > 0 if (b < a) --for insert sorted.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142 *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143 * For finding a data node with an external key, the comparison function
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 * parameters are: first the data node, and then the key.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 typedef int (*dCompareFunc) (const void *a, const void *b);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 Dlist *dList_new(int size);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150 void dList_free (Dlist *lp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 void dList_append (Dlist *lp, void *data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 void dList_prepend (Dlist *lp, void *data);
162
7a0dad1cffe0 - Fixed a SEGFAULT bug in http.c (handling of web->url).
jcid
parents: 157
diff changeset
153 void dList_insert_pos (Dlist *lp, void *data, int pos0);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154 int dList_length (Dlist *lp);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
155 void dList_remove (Dlist *lp, const void *data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
156 void dList_remove_fast (Dlist *lp, const void *data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
157 void *dList_nth_data (Dlist *lp, int n0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
158 void *dList_find (Dlist *lp, const void *data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
159 int dList_find_idx (Dlist *lp, const void *data);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
160 void *dList_find_custom (Dlist *lp, const void *data, dCompareFunc func);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
161 void dList_sort (Dlist *lp, dCompareFunc func);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
162 void dList_insert_sorted (Dlist *lp, void *data, dCompareFunc func);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
163 void *dList_find_sorted (Dlist *lp, const void *data, dCompareFunc func);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
164
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
165 /*
67
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 4
diff changeset
166 *- Parse function ------------------------------------------------------------
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 4
diff changeset
167 */
1053
64e078b7a711 Refactor the preferences parser
Tim Nieradzik, Jorge Arellano Cid
parents: 786
diff changeset
168 int dParser_parse_rc_line(char **line, char **name, char **value);
67
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 4
diff changeset
169
ec671a7ea6e2 - * Improved the dpi framework. Now dpi-programs can be specified in dpidrc,
jcid
parents: 4
diff changeset
170 /*
1104
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
171 *- Dlib messages -------------------------------------------------------------
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
172 */
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
173 void dLib_show_messages(bool_t show);
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
174
a450688d276e Added DLIB_MSG() macro for dlib's messages
corvid <corvid@lavabit.com>
parents: 1053
diff changeset
175 /*
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
176 *- Misc utility functions ----------------------------------------------------
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
177 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
178 char *dGetcwd ();
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
179 char *dGethomedir ();
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
180 char *dGetline (FILE *stream);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
181
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
182
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
183 #ifdef __cplusplus
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
184 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
185 #endif /* __cplusplus */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
186
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
187 #endif /* __DLIB_H__ */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
188