xref: /minix3/common/lib/libprop/prop_ingest.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: prop_ingest.c,v 1.5 2014/09/05 05:19:24 matt Exp $	*/
26b6d114aSBen Gras 
36b6d114aSBen Gras /*-
46b6d114aSBen Gras  * Copyright (c) 2006 The NetBSD Foundation, Inc.
56b6d114aSBen Gras  * All rights reserved.
66b6d114aSBen Gras  *
76b6d114aSBen Gras  * This code is derived from software contributed to The NetBSD Foundation
86b6d114aSBen Gras  * by Jason R. Thorpe.
96b6d114aSBen Gras  *
106b6d114aSBen Gras  * Redistribution and use in source and binary forms, with or without
116b6d114aSBen Gras  * modification, are permitted provided that the following conditions
126b6d114aSBen Gras  * are met:
136b6d114aSBen Gras  * 1. Redistributions of source code must retain the above copyright
146b6d114aSBen Gras  *    notice, this list of conditions and the following disclaimer.
156b6d114aSBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
166b6d114aSBen Gras  *    notice, this list of conditions and the following disclaimer in the
176b6d114aSBen Gras  *    documentation and/or other materials provided with the distribution.
186b6d114aSBen Gras  *
196b6d114aSBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206b6d114aSBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216b6d114aSBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
226b6d114aSBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
236b6d114aSBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246b6d114aSBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256b6d114aSBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266b6d114aSBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276b6d114aSBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286b6d114aSBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296b6d114aSBen Gras  * POSSIBILITY OF SUCH DAMAGE.
306b6d114aSBen Gras  */
316b6d114aSBen Gras 
326b6d114aSBen Gras #include "prop_object_impl.h"
33f14fb602SLionel Sambuc #include <prop/proplib.h>
346b6d114aSBen Gras 
356b6d114aSBen Gras struct _prop_ingest_context {
366b6d114aSBen Gras 	prop_ingest_error_t	pic_error;
376b6d114aSBen Gras 	prop_type_t		pic_type;
386b6d114aSBen Gras 	const char *		pic_key;
396b6d114aSBen Gras 	void *			pic_private;
406b6d114aSBen Gras };
416b6d114aSBen Gras 
426b6d114aSBen Gras /*
436b6d114aSBen Gras  * prop_ingest_context_alloc --
446b6d114aSBen Gras  *	Allocate and initialize an ingest context.
456b6d114aSBen Gras  */
466b6d114aSBen Gras prop_ingest_context_t
prop_ingest_context_alloc(void * xprivate)47*0a6a1f1dSLionel Sambuc prop_ingest_context_alloc(void *xprivate)
486b6d114aSBen Gras {
496b6d114aSBen Gras 	prop_ingest_context_t ctx;
506b6d114aSBen Gras 
516b6d114aSBen Gras 	ctx = _PROP_MALLOC(sizeof(*ctx), M_TEMP);
526b6d114aSBen Gras 	if (ctx != NULL) {
536b6d114aSBen Gras 		ctx->pic_error = PROP_INGEST_ERROR_NO_ERROR;
546b6d114aSBen Gras 		ctx->pic_type = PROP_TYPE_UNKNOWN;
556b6d114aSBen Gras 		ctx->pic_key = NULL;
56*0a6a1f1dSLionel Sambuc 		ctx->pic_private = xprivate;
576b6d114aSBen Gras 	}
586b6d114aSBen Gras 	return (ctx);
596b6d114aSBen Gras }
606b6d114aSBen Gras 
616b6d114aSBen Gras /*
626b6d114aSBen Gras  * prop_ingest_context_free --
636b6d114aSBen Gras  *	Free an ingest context.
646b6d114aSBen Gras  */
656b6d114aSBen Gras void
prop_ingest_context_free(prop_ingest_context_t ctx)666b6d114aSBen Gras prop_ingest_context_free(prop_ingest_context_t ctx)
676b6d114aSBen Gras {
686b6d114aSBen Gras 
696b6d114aSBen Gras 	_PROP_FREE(ctx, M_TEMP);
706b6d114aSBen Gras }
716b6d114aSBen Gras 
726b6d114aSBen Gras /*
736b6d114aSBen Gras  * prop_ingest_context_error --
746b6d114aSBen Gras  *	Get the error code from an ingest context.
756b6d114aSBen Gras  */
766b6d114aSBen Gras prop_ingest_error_t
prop_ingest_context_error(prop_ingest_context_t ctx)776b6d114aSBen Gras prop_ingest_context_error(prop_ingest_context_t ctx)
786b6d114aSBen Gras {
796b6d114aSBen Gras 
806b6d114aSBen Gras 	return (ctx->pic_error);
816b6d114aSBen Gras }
826b6d114aSBen Gras 
836b6d114aSBen Gras /*
846b6d114aSBen Gras  * prop_ingest_context_type --
856b6d114aSBen Gras  *	Return the type of last object visisted by an ingest context.
866b6d114aSBen Gras  */
876b6d114aSBen Gras prop_type_t
prop_ingest_context_type(prop_ingest_context_t ctx)886b6d114aSBen Gras prop_ingest_context_type(prop_ingest_context_t ctx)
896b6d114aSBen Gras {
906b6d114aSBen Gras 
916b6d114aSBen Gras 	return (ctx->pic_type);
926b6d114aSBen Gras }
936b6d114aSBen Gras 
946b6d114aSBen Gras /*
956b6d114aSBen Gras  * prop_ingest_context_key --
966b6d114aSBen Gras  *	Return the last key looked up by an ingest context.
976b6d114aSBen Gras  */
986b6d114aSBen Gras const char *
prop_ingest_context_key(prop_ingest_context_t ctx)996b6d114aSBen Gras prop_ingest_context_key(prop_ingest_context_t ctx)
1006b6d114aSBen Gras {
1016b6d114aSBen Gras 
1026b6d114aSBen Gras 	return (ctx->pic_key);
1036b6d114aSBen Gras }
1046b6d114aSBen Gras 
1056b6d114aSBen Gras /*
1066b6d114aSBen Gras  * prop_ingest_context_private --
1076b6d114aSBen Gras  *	Return the caller-private data associated with an ingest context.
1086b6d114aSBen Gras  */
1096b6d114aSBen Gras void *
prop_ingest_context_private(prop_ingest_context_t ctx)1106b6d114aSBen Gras prop_ingest_context_private(prop_ingest_context_t ctx)
1116b6d114aSBen Gras {
1126b6d114aSBen Gras 
1136b6d114aSBen Gras 	return (ctx->pic_private);
1146b6d114aSBen Gras }
1156b6d114aSBen Gras 
1166b6d114aSBen Gras /*
1176b6d114aSBen Gras  * prop_dictionary_ingest --
1186b6d114aSBen Gras  *	Ingest a dictionary using handlers for each object to translate
1196b6d114aSBen Gras  *	into an arbitrary binary format.
1206b6d114aSBen Gras  */
1216b6d114aSBen Gras bool
prop_dictionary_ingest(prop_dictionary_t dict,const prop_ingest_table_entry rules[],prop_ingest_context_t ctx)1226b6d114aSBen Gras prop_dictionary_ingest(prop_dictionary_t dict,
1236b6d114aSBen Gras 		       const prop_ingest_table_entry rules[],
1246b6d114aSBen Gras 		       prop_ingest_context_t ctx)
1256b6d114aSBen Gras {
1266b6d114aSBen Gras 	const prop_ingest_table_entry *pite;
1276b6d114aSBen Gras 	prop_object_t obj;
1286b6d114aSBen Gras 
1296b6d114aSBen Gras 	ctx->pic_error = PROP_INGEST_ERROR_NO_ERROR;
1306b6d114aSBen Gras 
1316b6d114aSBen Gras 	for (pite = rules; pite->pite_key != NULL; pite++) {
1326b6d114aSBen Gras 		ctx->pic_key = pite->pite_key;
1336b6d114aSBen Gras 		obj = prop_dictionary_get(dict, pite->pite_key);
1346b6d114aSBen Gras 		ctx->pic_type = prop_object_type(obj);
1356b6d114aSBen Gras 		if (obj == NULL) {
1366b6d114aSBen Gras 			if (pite->pite_flags & PROP_INGEST_FLAG_OPTIONAL) {
1376b6d114aSBen Gras 				if ((*pite->pite_handler)(ctx, NULL) == false) {
1386b6d114aSBen Gras 					ctx->pic_error =
1396b6d114aSBen Gras 					    PROP_INGEST_ERROR_HANDLER_FAILED;
1406b6d114aSBen Gras 					return (false);
1416b6d114aSBen Gras 				}
1426b6d114aSBen Gras 				continue;
1436b6d114aSBen Gras 			}
1446b6d114aSBen Gras 			ctx->pic_error = PROP_INGEST_ERROR_NO_KEY;
1456b6d114aSBen Gras 			return (false);
1466b6d114aSBen Gras 		}
1476b6d114aSBen Gras 		if (ctx->pic_type != pite->pite_type &&
1486b6d114aSBen Gras 		    pite->pite_type != PROP_TYPE_UNKNOWN) {
1496b6d114aSBen Gras 			ctx->pic_error = PROP_INGEST_ERROR_WRONG_TYPE;
1506b6d114aSBen Gras 			return (false);
1516b6d114aSBen Gras 		}
1526b6d114aSBen Gras 		if ((*pite->pite_handler)(ctx, obj) == false) {
1536b6d114aSBen Gras 			ctx->pic_error = PROP_INGEST_ERROR_HANDLER_FAILED;
1546b6d114aSBen Gras 			return (false);
1556b6d114aSBen Gras 		}
1566b6d114aSBen Gras 	}
1576b6d114aSBen Gras 
1586b6d114aSBen Gras 	return (true);
1596b6d114aSBen Gras }
160