BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file NCDInterpProg.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29  
30 #include <stdint.h>
31 #include <limits.h>
32  
33 #include <misc/balloc.h>
34 #include <misc/hashfun.h>
35 #include <base/BLog.h>
36  
37 #include "NCDInterpProg.h"
38  
39 #include <generated/blog_channel_ncd.h>
40  
41 #include "NCDInterpProg_hash.h"
42 #include <structure/CHash_impl.h>
43  
44 int NCDInterpProg_Init (NCDInterpProg *o, NCDProgram *prog, NCDStringIndex *string_index, NCDEvaluator *eval, NCDModuleIndex *module_index)
45 {
46 ASSERT(prog)
47 ASSERT(!NCDProgram_ContainsElemType(prog, NCDPROGRAMELEM_INCLUDE))
48 ASSERT(!NCDProgram_ContainsElemType(prog, NCDPROGRAMELEM_INCLUDE_GUARD))
49 ASSERT(string_index)
50 ASSERT(eval)
51 ASSERT(module_index)
52  
53 if (NCDProgram_NumElems(prog) > INT_MAX) {
54 BLog(BLOG_ERROR, "too many processes");
55 goto fail0;
56 }
57 int num_procs = NCDProgram_NumElems(prog);
58  
59 if (!(o->procs = BAllocArray(num_procs, sizeof(o->procs[0])))) {
60 BLog(BLOG_ERROR, "BAllocArray failed");
61 goto fail0;
62 }
63  
64 if (!NCDInterpProg__Hash_Init(&o->hash, num_procs)) {
65 BLog(BLOG_ERROR, "NCDInterpProg__Hash_Init failed");
66 goto fail1;
67 }
68  
69 o->num_procs = 0;
70  
71 for (NCDProgramElem *elem = NCDProgram_FirstElem(prog); elem; elem = NCDProgram_NextElem(prog, elem)) {
72 ASSERT(NCDProgramElem_Type(elem) == NCDPROGRAMELEM_PROCESS)
73 NCDProcess *p = NCDProgramElem_Process(elem);
74  
75 struct NCDInterpProg__process *e = &o->procs[o->num_procs];
76  
77 e->name = NCDStringIndex_Get(string_index, NCDProcess_Name(p));
78 if (e->name < 0) {
79 BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
80 goto fail2;
81 }
82  
83 if (!NCDInterpProcess_Init(&e->iprocess, p, string_index, eval, module_index)) {
84 BLog(BLOG_ERROR, "NCDInterpProcess_Init failed");
85 goto fail2;
86 }
87  
88 NCDInterpProg__HashRef ref = {e, o->num_procs};
89 if (!NCDInterpProg__Hash_Insert(&o->hash, o->procs, ref, NULL)) {
90 BLog(BLOG_ERROR, "duplicate process or template name: %s", NCDProcess_Name(p));
91 NCDInterpProcess_Free(&e->iprocess);
92 goto fail2;
93 }
94  
95 o->num_procs++;
96 }
97  
98 ASSERT(o->num_procs == num_procs)
99  
100 DebugObject_Init(&o->d_obj);
101 return 1;
102  
103 fail2:
104 while (o->num_procs-- > 0) {
105 NCDInterpProcess_Free(&o->procs[o->num_procs].iprocess);
106 }
107 NCDInterpProg__Hash_Free(&o->hash);
108 fail1:
109 BFree(o->procs);
110 fail0:
111 return 0;
112 }
113  
114 void NCDInterpProg_Free (NCDInterpProg *o)
115 {
116 DebugObject_Free(&o->d_obj);
117  
118 while (o->num_procs-- > 0) {
119 NCDInterpProcess_Free(&o->procs[o->num_procs].iprocess);
120 }
121  
122 NCDInterpProg__Hash_Free(&o->hash);
123  
124 BFree(o->procs);
125 }
126  
127 NCDInterpProcess * NCDInterpProg_FindProcess (NCDInterpProg *o, NCD_string_id_t name)
128 {
129 DebugObject_Access(&o->d_obj);
130 ASSERT(name >= 0)
131  
132 NCDInterpProg__HashRef ref = NCDInterpProg__Hash_Lookup(&o->hash, o->procs, name);
133 if (ref.link == NCDInterpProg__HashNullLink()) {
134 return NULL;
135 }
136  
137 ASSERT(ref.ptr->name == name)
138  
139 return &ref.ptr->iprocess;
140 }