summaryrefslogtreecommitdiff
path: root/source3/lib/util_array.c
blob: 5a21053cc6b56ebe4d6246d00fa64caf33cd8694 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   Samba utility functions
   Copyright (C) Andrew Tridgell              1992-1999
   Copyright (C) Luke Kenneth Casson Leighton 1996-1999
   
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

void free_void_array(uint32 num_entries, void **entries,
		void(free_item)(void*))
{
	uint32 i;
	if (entries != NULL)
	{
		for (i = 0; i < num_entries; i++)
		{
			if (entries[i] != NULL)
			{
				free_item(entries[i]);
			}
		}
		free(entries);
	}
}

void* add_copy_to_array(uint32 *len, void ***array, const void *item,
	void*(item_dup)(const void*), BOOL alloc_anyway)
{
	if (len == NULL || array == NULL)
	{
		return NULL;
	}

	if (item != NULL || alloc_anyway)
	{
		void* copy = NULL;
		if (item != NULL || alloc_anyway)
		{
			copy = item_dup(item);
		}
		add_item_to_array(len, array, copy);
	}
	return NULL;
}

void* add_item_to_array(uint32 *len, void ***array, void *item)
{
	if (len == NULL || array == NULL)
	{
		return NULL;
	}

	(*array) = (void**)Realloc((*array), ((*len)+1)*sizeof((*array)[0]));

	if ((*array) != NULL)
	{
		(*array)[(*len)] = item;
		(*len)++;
		return item;
	}
	return NULL;
}

static void use_info_free(struct use_info *item)
{
	if (item != NULL)
	{
		if (item->srv_name != NULL)
		{
			free(item->srv_name);
		}
		if (item->user_name != NULL)
		{
			free(item->user_name);
		}
		if (item->domain != NULL)
		{
			free(item->domain);
		}
		free(item);
	}
}

static struct use_info *use_info_dup(const struct use_info *from)
{
	if (from != NULL)
	{
		struct use_info *copy = (struct use_info *)
		                        malloc(sizeof(struct use_info));
		if (copy != NULL)
		{
			ZERO_STRUCTP(copy);
			copy->connected = from->connected;
			if (from->srv_name != NULL)
			{
				copy->srv_name  = strdup(from->srv_name );
			}
			if (from->user_name != NULL)
			{
				copy->user_name = strdup(from->user_name);
			}
			if (from->domain != NULL)
			{
				copy->domain    = strdup(from->domain   );
			}
		}
		return copy;
	}
	return NULL;
}

void free_use_info_array(uint32 num_entries, struct use_info **entries)
{
	void(*fn)(void*) = (void(*)(void*))&use_info_free;
	free_void_array(num_entries, (void**)entries, *fn);
}

struct use_info* add_use_info_to_array(uint32 *len, struct use_info ***array,
				const struct use_info *name)
{
	void*(*fn)(const void*) = (void*(*)(const void*))&use_info_dup;
	return (struct use_info*)add_copy_to_array(len,
	                     (void***)array, (const void*)name, *fn, False);
				
}

void free_char_array(uint32 num_entries, char **entries)
{
	void(*fn)(void*) = (void(*)(void*))&free;
	free_void_array(num_entries, (void**)entries, *fn);
}

char* add_chars_to_array(uint32 *len, char ***array, const char *name)
{
	void*(*fn)(const void*) = (void*(*)(const void*))&strdup;
	return (char*)add_copy_to_array(len,
	                     (void***)array, (const void*)name, *fn, False);
				
}

static uint32 *uint32_dup(const uint32* from)
{
	if (from != NULL)
	{
		uint32 *copy = (uint32 *)malloc(sizeof(uint32));
		if (copy != NULL)
		{
			memcpy(copy, from, sizeof(*copy));
		}
		return copy;
	}
	return NULL;
}

void free_uint32_array(uint32 num_entries, uint32 **entries)
{
	void(*fn)(void*) = (void(*)(void*))&free;
	free_void_array(num_entries, (void**)entries, *fn);
}

uint32* add_uint32s_to_array(uint32 *len, uint32 ***array, const uint32 *name)
{
	void*(*fn)(const void*) = (void*(*)(const void*))&uint32_dup;
	return (uint32*)add_copy_to_array(len,
	                     (void***)array, (const void*)name, *fn, False);
				
}

void free_unistr_array(uint32 num_entries, UNISTR2 **entries)
{
	void(*fn)(void*) = (void(*)(void*))&unistr2_free;
	free_void_array(num_entries, (void**)entries, *fn);
}

UNISTR2* add_unistr_to_array(uint32 *len, UNISTR2 ***array, UNISTR2 *name)
{
	void*(*fn)(const void*) = (void*(*)(const void*))&unistr2_dup;
	return (UNISTR2*)add_copy_to_array(len,
	                   (void***)array, (const void*)name, *fn, False);
}

void free_sid_array(uint32 num_entries, DOM_SID **entries)
{
	void(*fn)(void*) = (void(*)(void*))&free;
	free_void_array(num_entries, (void**)entries, *fn);
}

DOM_SID* add_sid_to_array(uint32 *len, DOM_SID ***array, const DOM_SID *sid)
{
	void*(*fn)(const void*) = (void*(*)(const void*))&sid_dup;
	return (DOM_SID*)add_copy_to_array(len,
	                  (void***)array, (const void*)sid, *fn, False);
}

void free_devmode(DEVICEMODE *devmode)
{
	if (devmode!=NULL)
	{
		if (devmode->private!=NULL)
			free(devmode->private);
		free(devmode);
	}
}

void free_printer_info_2(PRINTER_INFO_2 *printer)
{
	if (printer!=NULL)
	{
		free_devmode(printer->devmode);
		free(printer);
	}
}

static PRINTER_INFO_2 *prt2_dup(const PRINTER_INFO_2* from)
{
	PRINTER_INFO_2 *copy = (PRINTER_INFO_2 *)malloc(sizeof(PRINTER_INFO_2));
	if (copy != NULL)
	{
		if (from != NULL)
		{
			memcpy(copy, from, sizeof(*copy));
		}
		else
		{
			ZERO_STRUCTP(copy);
		}
	}
	return copy;
}

void free_print2_array(uint32 num_entries, PRINTER_INFO_2 **entries)
{
	void(*fn)(void*) = (void(*)(void*))&free_printer_info_2;
	free_void_array(num_entries, (void**)entries, *fn);
}

PRINTER_INFO_2 *add_print2_to_array(uint32 *len, PRINTER_INFO_2 ***array,
				const PRINTER_INFO_2 *prt)
{
	void*(*fn)(const void*) = (void*(*)(const void*))&prt2_dup;
	return (PRINTER_INFO_2*)add_copy_to_array(len,
	           (void***)array, (const void*)prt, *fn, True);
}

static PRINTER_INFO_1 *prt1_dup(const PRINTER_INFO_1* from)
{
	PRINTER_INFO_1 *copy = (PRINTER_INFO_1 *)malloc(sizeof(PRINTER_INFO_1));
	if (copy != NULL)
	{
		if (from != NULL)
		{
			memcpy(copy, from, sizeof(*copy));
		}
		else
		{
			ZERO_STRUCTP(copy);
		}
	}
	return copy;
}

void free_print1_array(uint32 num_entries, PRINTER_INFO_1 **entries)
{
	void(*fn)(void*) = (void(*)(void*))&free;
	free_void_array(num_entries, (void**)entries, *fn);
}

PRINTER_INFO_1 *add_print1_to_array(uint32 *len, PRINTER_INFO_1 ***array,
				const PRINTER_INFO_1 *prt)
{
	void*(*fn)(const void*) = (void*(*)(const void*))&prt1_dup;
	return (PRINTER_INFO_1*)add_copy_to_array(len,
	                   (void***)array, (const void*)prt, *fn, True);
}

static JOB_INFO_1 *job1_dup(const JOB_INFO_1* from)
{
	JOB_INFO_1 *copy = (JOB_INFO_1 *)malloc(sizeof(JOB_INFO_1));
	if (copy != NULL)
	{
		if (from != NULL)
		{
			memcpy(copy, from, sizeof(*copy));
		}
		else
		{
			ZERO_STRUCTP(copy);
		}
	}
	return copy;
}

void free_job1_array(uint32 num_entries, JOB_INFO_1 **entries)
{
	void(*fn)(void*) = (void(*)(void*))&free;
	free_void_array(num_entries, (void**)entries, *fn);
}

JOB_INFO_1 *add_job1_to_array(uint32 *len, JOB_INFO_1 ***array,
				const JOB_INFO_1 *job)
{
	void*(*fn)(const void*) = (void*(*)(const void*))&job1_dup;
	return (JOB_INFO_1*)add_copy_to_array(len,
	                   (void***)array, (const void*)job, *fn, True);
}

static JOB_INFO_2 *job2_dup(const JOB_INFO_2* from)
{
	JOB_INFO_2 *copy = (JOB_INFO_2 *)malloc(sizeof(JOB_INFO_2));
	if (copy != NULL)
	{
		if (from != NULL)
		{
			memcpy(copy, from, sizeof(*copy));
		}
		else
		{
			ZERO_STRUCTP(copy);
		}
	}
	return copy;
}

void free_job2_array(uint32 num_entries, JOB_INFO_2 **entries)
{
	void(*fn)(void*) = (void(*)(void*))&free;
	free_void_array(num_entries, (void**)entries, *fn);
}

JOB_INFO_2 *add_job2_to_array(uint32 *len, JOB_INFO_2 ***array,
				const JOB_INFO_2 *job)
{
	void*(*fn)(const void*) = (void*(*)(const void*))&job2_dup;
	return (JOB_INFO_2*)add_copy_to_array(len,
	                   (void***)array, (const void*)job, *fn, True);
}