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