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