summaryrefslogtreecommitdiff
path: root/source3/registry/reg_parse.h
blob: 0694225ef50ea07d16bda9bc23e2cd274c12327e (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Samba Unix/Linux SMB client library
 *
 * Copyright (C) Gregor Beck 2010
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @brief  Parser for registration entries (.reg) files.
 * A parser is a talloced incarnation of an opaque struct reg_parse.
 * It is fed with the (.reg) file line by line calling @ref reg_parse_line
 * and emits output by calling functions from its reg_parse_callback.
 * @file   reg_parse.h
 * @author Gregor Beck <gb@sernet.de>
 * @date   Jun 2010
 */

#ifndef REG_PARSE_H
#define REG_PARSE_H

#include <stdint.h>
#include <stdbool.h>

/**
 * Protoype for function called on key found.
 * The usual action to take is delete the key if del==true, open it if
 * already existing or create a new one.
 *
 * @param private_data
 * @param key
 * @param klen number of elements in key
 * @param del whether to delete the key
 *
 * @retval >=0 on success
 *
 * @see reg_format_key
 */
typedef int (*reg_parse_callback_key_t) (void* private_data,
					 const char* key[],
					 size_t klen,
					 bool del);

/**
 * Protoype for function called on value found.
 * The usual action to take is set the value of the last opened key.
 *
 * @param private_data
 * @param name the values name
 * @param type the values type
 * @param data the values value
 * @param len  the number of bytes of data
 *
 * @retval >=0 on success
 *
 * @see reg_format_value
 */
typedef int (*reg_parse_callback_val_t) (void*  private_data,
					 const char* name,
					 uint32_t type,
					 const uint8_t* data,
					 uint32_t len);

/**
 * Protoype for function called on value delete found.
 * Delete value from the last opened key. It is usually no error if
 * no such value exist.
 *
 * @param private_data
 * @param name
 *
 * @retval >=0 on success
 *
 * @see reg_format_value_delete
 */
typedef int (*reg_parse_callback_val_del_t) (void* private_data,
					     const char* name);


/**
 * Protoype for function called on comment found.
 *
 * @param private_data
 * @param line comment with marker removed.
 *
 * @retval  >=0 on success
 *
 * @see reg_format_comment
 */
typedef int (*reg_parse_callback_comment_t) (void* private_data,
					     const char* line);

/**
 * Type handling the output of a reg_parse object.
 * It containes the functions to call and an opaque data pointer.
 */
typedef struct reg_parse_callback {
	reg_parse_callback_key_t key; /**< Function called on key found */
	reg_parse_callback_val_t val; /**< Function called on value found */
        /** Function called on value delete found */
	reg_parse_callback_val_del_t val_del;
	/** Function called on comment found */
	reg_parse_callback_comment_t comment;
	void* data; /**< Private data passed to callback function */
} reg_parse_callback;

/**
 * A Parser for a registration entries (.reg) file.
 *
 * It may be used as a reg_format_callback, so the following is valid:
 * @code
 * reg_format* f = reg_format_new(mem_ctx,
 *                                (reg_format_callback)reg_parse_new(mem_ctx, cb, NULL, 0),
 *                                NULL, 0, "\\");
 * @endcode
 * @see reg_format
 */
typedef struct reg_parse reg_parse;

/**
 * Create a new reg_parse object.
 *
 * @param talloc_ctx the talloc parent
 * @param cb         the output handler
 * @param str_enc    the charset of hex encoded strings (REG_MULTI_SZ, REG_EXAND_SZ) if not UTF-16
 * @param flags
 *
 * @return a talloc'ed reg_parse object, NULL on error
 */
reg_parse* reg_parse_new(const void* talloc_ctx,
			 reg_parse_callback cb,
			 const char* str_enc,
			 unsigned flags);

/**
 * Feed one line to the parser.
 *
 * @param parser
 * @param line one line from a (.reg) file, in UNIX charset
 *
 * @return 0 on success
 *
 * @see reg_format_callback_writeline_t
 */
int reg_parse_line(struct reg_parse* parser, const char* line);


/**
 * Parse a (.reg) file, read from a file descriptor.
 *
 * @param fd the file descriptor
 * @param cb the output handler
 * @param opts
 *
 * @return 0 on success
 */
int reg_parse_fd(int fd,
		 const reg_parse_callback* cb,
		 const char* opts);

/**
 * Parse a (.reg) file
 *
 * @param filename the file to open
 * @param cb the output handler
 * @param opts
 *
 * @return 0 on success
 */
int reg_parse_file(const char* filename,
		   const reg_parse_callback* cb,
		   const char* opts);

int reg_parse_set_options(reg_parse* parser, const char* opt);

/******************************************************************************/


#endif /* REG_PARSE_H */