xref: /netbsd-src/common/lib/libprop/prop_ingest.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: prop_ingest.c,v 1.2 2007/08/16 16:28:18 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the NetBSD
21  *      Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <prop/proplib.h>
40 #include "prop_object_impl.h"
41 
42 struct _prop_ingest_context {
43 	prop_ingest_error_t	pic_error;
44 	prop_type_t		pic_type;
45 	const char *		pic_key;
46 	void *			pic_private;
47 };
48 
49 /*
50  * prop_ingest_context_alloc --
51  *	Allocate and initialize an ingest context.
52  */
53 prop_ingest_context_t
54 prop_ingest_context_alloc(void *private)
55 {
56 	prop_ingest_context_t ctx;
57 
58 	ctx = _PROP_MALLOC(sizeof(*ctx), M_TEMP);
59 	if (ctx != NULL) {
60 		ctx->pic_error = PROP_INGEST_ERROR_NO_ERROR;
61 		ctx->pic_type = PROP_TYPE_UNKNOWN;
62 		ctx->pic_key = NULL;
63 		ctx->pic_private = private;
64 	}
65 	return (ctx);
66 }
67 
68 /*
69  * prop_ingest_context_free --
70  *	Free an ingest context.
71  */
72 void
73 prop_ingest_context_free(prop_ingest_context_t ctx)
74 {
75 
76 	_PROP_FREE(ctx, M_TEMP);
77 }
78 
79 /*
80  * prop_ingest_context_error --
81  *	Get the error code from an ingest context.
82  */
83 prop_ingest_error_t
84 prop_ingest_context_error(prop_ingest_context_t ctx)
85 {
86 
87 	return (ctx->pic_error);
88 }
89 
90 /*
91  * prop_ingest_context_type --
92  *	Return the type of last object visisted by an ingest context.
93  */
94 prop_type_t
95 prop_ingest_context_type(prop_ingest_context_t ctx)
96 {
97 
98 	return (ctx->pic_type);
99 }
100 
101 /*
102  * prop_ingest_context_key --
103  *	Return the last key looked up by an ingest context.
104  */
105 const char *
106 prop_ingest_context_key(prop_ingest_context_t ctx)
107 {
108 
109 	return (ctx->pic_key);
110 }
111 
112 /*
113  * prop_ingest_context_private --
114  *	Return the caller-private data associated with an ingest context.
115  */
116 void *
117 prop_ingest_context_private(prop_ingest_context_t ctx)
118 {
119 
120 	return (ctx->pic_private);
121 }
122 
123 /*
124  * prop_dictionary_ingest --
125  *	Ingest a dictionary using handlers for each object to translate
126  *	into an arbitrary binary format.
127  */
128 bool
129 prop_dictionary_ingest(prop_dictionary_t dict,
130 		       const prop_ingest_table_entry rules[],
131 		       prop_ingest_context_t ctx)
132 {
133 	const prop_ingest_table_entry *pite;
134 	prop_object_t obj;
135 
136 	ctx->pic_error = PROP_INGEST_ERROR_NO_ERROR;
137 
138 	for (pite = rules; pite->pite_key != NULL; pite++) {
139 		ctx->pic_key = pite->pite_key;
140 		obj = prop_dictionary_get(dict, pite->pite_key);
141 		ctx->pic_type = prop_object_type(obj);
142 		if (obj == NULL) {
143 			if (pite->pite_flags & PROP_INGEST_FLAG_OPTIONAL) {
144 				if ((*pite->pite_handler)(ctx, NULL) == false) {
145 					ctx->pic_error =
146 					    PROP_INGEST_ERROR_HANDLER_FAILED;
147 					return (false);
148 				}
149 				continue;
150 			}
151 			ctx->pic_error = PROP_INGEST_ERROR_NO_KEY;
152 			return (false);
153 		}
154 		if (ctx->pic_type != pite->pite_type &&
155 		    pite->pite_type != PROP_TYPE_UNKNOWN) {
156 			ctx->pic_error = PROP_INGEST_ERROR_WRONG_TYPE;
157 			return (false);
158 		}
159 		if ((*pite->pite_handler)(ctx, obj) == false) {
160 			ctx->pic_error = PROP_INGEST_ERROR_HANDLER_FAILED;
161 			return (false);
162 		}
163 	}
164 
165 	return (true);
166 }
167