tor  0.4.2.1-alpha-dev
structvar.c
Go to the documentation of this file.
1 /* Copyright (c) 2001 Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
20 #include "orconfig.h"
21 #include "lib/confmgt/structvar.h"
22 #include "lib/cc/compat_compiler.h"
23 #include "lib/conf/conftypes.h"
24 #include "lib/confmgt/type_defs.h"
25 #include "lib/confmgt/typedvar.h"
26 #include "lib/log/util_bug.h"
27 
29 
30 #include <stddef.h>
31 
35 void
36 struct_set_magic(void *object, const struct_magic_decl_t *decl)
37 {
38  tor_assert(object);
39  tor_assert(decl);
40  uint32_t *ptr = STRUCT_VAR_P(object, decl->magic_offset);
41  *ptr = decl->magic_val;
42 }
43 
47 void
48 struct_check_magic(const void *object, const struct_magic_decl_t *decl)
49 {
50  tor_assert(object);
51  tor_assert(decl);
52 
53  const uint32_t *ptr = STRUCT_VAR_P(object, decl->magic_offset);
54  tor_assertf(*ptr == decl->magic_val,
55  "Bad magic number on purported %s object. "
56  "Expected %"PRIu32"x but got "PRIu32"x.",
57  decl->magic_val, *ptr);
58 }
59 
64 void *
65 struct_get_mptr(void *object, const struct_member_t *member)
66 {
67  tor_assert(object);
68  return STRUCT_VAR_P(object, member->offset);
69 }
70 
75 const void *
76 struct_get_ptr(const void *object, const struct_member_t *member)
77 {
78  tor_assert(object);
79  return STRUCT_VAR_P(object, member->offset);
80 }
81 
86 static const var_type_def_t *
88 {
89  if (member->type_def)
90  return member->type_def;
91 
92  return lookup_type_def(member->type);
93 }
94 
99 void
100 struct_var_free(void *object, const struct_member_t *member)
101 {
102  void *p = struct_get_mptr(object, member);
103  const var_type_def_t *def = get_type_def(member);
104 
105  typed_var_free(p, def);
106 }
107 
112 int
113 struct_var_copy(void *dest, const void *src, const struct_member_t *member)
114 {
115  void *p_dest = struct_get_mptr(dest, member);
116  const void *p_src = struct_get_ptr(src, member);
117  const var_type_def_t *def = get_type_def(member);
118 
119  return typed_var_copy(p_dest, p_src, def);
120 }
121 
126 bool
127 struct_var_eq(const void *a, const void *b, const struct_member_t *member)
128 {
129  const void *p_a = struct_get_ptr(a, member);
130  const void *p_b = struct_get_ptr(b, member);
131  const var_type_def_t *def = get_type_def(member);
132 
133  return typed_var_eq(p_a, p_b, def);
134 }
135 
140 bool
141 struct_var_ok(const void *object, const struct_member_t *member)
142 {
143  const void *p = struct_get_ptr(object, member);
144  const var_type_def_t *def = get_type_def(member);
145 
146  return typed_var_ok(p, def);
147 }
148 
153 int
154 struct_var_kvassign(void *object, const struct config_line_t *line,
155  char **errmsg,
156  const struct_member_t *member)
157 {
158  void *p = struct_get_mptr(object, member);
159  const var_type_def_t *def = get_type_def(member);
160 
161  return typed_var_kvassign(p, line, errmsg, def);
162 }
163 
168 struct config_line_t *
169 struct_var_kvencode(const void *object, const struct_member_t *member)
170 {
171  const void *p = struct_get_ptr(object, member);
172  const var_type_def_t *def = get_type_def(member);
173 
174  return typed_var_kvencode(member->name, p, def);
175 }
176 
182 void
183 struct_var_mark_fragile(void *object, const struct_member_t *member)
184 {
185  void *p = struct_get_mptr(object, member);
186  const var_type_def_t *def = get_type_def(member);
187  return typed_var_mark_fragile(p, def);
188 }
189 
193 const char *
195 {
196  return member->name;
197 }
198 
206 const char *
208 {
209  const var_type_def_t *def = get_type_def(member);
210 
211  return def ? def->name : NULL;
212 }
213 
215 uint32_t
217 {
218  const var_type_def_t *def = get_type_def(member);
219 
220  return def ? def->flags : 0;
221 }
bool typed_var_ok(const void *value, const var_type_def_t *def)
Definition: typedvar.c:202
bool struct_var_ok(const void *object, const struct_member_t *member)
Definition: structvar.c:141
const void * struct_get_ptr(const void *object, const struct_member_t *member)
Definition: structvar.c:76
void typed_var_free(void *target, const var_type_def_t *def)
Definition: typedvar.c:86
const char * struct_var_get_name(const struct_member_t *member)
Definition: structvar.c:194
uint32_t struct_var_get_flags(const struct_member_t *member)
Definition: structvar.c:216
void typed_var_mark_fragile(void *value, const var_type_def_t *def)
Definition: typedvar.c:219
bool typed_var_eq(const void *a, const void *b, const var_type_def_t *def)
Definition: typedvar.c:178
Structure declarations for typedvar type definitions.
struct config_line_t * struct_var_kvencode(const void *object, const struct_member_t *member)
Definition: structvar.c:169
void * struct_get_mptr(void *object, const struct_member_t *member)
Definition: structvar.c:65
tor_assert(buffer)
const char * struct_var_get_typename(const struct_member_t *member)
Definition: structvar.c:207
int struct_var_kvassign(void *object, const struct config_line_t *line, char **errmsg, const struct_member_t *member)
Definition: structvar.c:154
#define STRUCT_VAR_P(st, off)
Header for lib/confmgt/structvar.c.
ptrdiff_t offset
Definition: conftypes.h:114
Utility macros to handle different features and behavior in different compilers.
Header for lib/confmgt/typedvar.c.
int struct_var_copy(void *dest, const void *src, const struct_member_t *member)
Definition: structvar.c:113
int typed_var_copy(void *dest, const void *src, const var_type_def_t *def)
Definition: typedvar.c:145
const char * name
Definition: conftypes.h:87
uint32_t magic_val
Definition: conftypes.h:128
void struct_var_mark_fragile(void *object, const struct_member_t *member)
Definition: structvar.c:183
Header for lib/confmgt/type_defs.c.
config_type_t type
Definition: conftypes.h:103
Types used to specify configurable options.
const var_type_def_t * lookup_type_def(config_type_t type)
Definition: type_defs.c:767
bool struct_var_eq(const void *a, const void *b, const struct_member_t *member)
Definition: structvar.c:127
int typed_var_kvassign(void *target, const config_line_t *line, char **errmsg, const var_type_def_t *def)
Definition: typedvar.c:66
void struct_var_free(void *object, const struct_member_t *member)
Definition: structvar.c:100
void struct_check_magic(const void *object, const struct_magic_decl_t *decl)
Definition: structvar.c:48
void struct_set_magic(void *object, const struct_magic_decl_t *decl)
Definition: structvar.c:36
static const var_type_def_t * get_type_def(const struct_member_t *member)
Definition: structvar.c:87
const char * name
const struct var_type_def_t * type_def
Definition: conftypes.h:109
ptrdiff_t magic_offset
Definition: conftypes.h:131
Macros to manage assertions, fatal and non-fatal.
config_line_t * typed_var_kvencode(const char *key, const void *value, const var_type_def_t *def)
Definition: typedvar.c:120