BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file NCDFastNames.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 <string.h> |
||
31 | |||
32 | #include <misc/balloc.h> |
||
33 | |||
34 | #include "NCDFastNames.h" |
||
35 | |||
36 | static size_t count_names (const char *str, size_t str_len) |
||
37 | { |
||
38 | size_t count = 1; |
||
39 | |||
40 | while (str_len > 0) { |
||
41 | if (*str == '.') { |
||
42 | count++; |
||
43 | } |
||
44 | str++; |
||
45 | str_len--; |
||
46 | } |
||
47 | |||
48 | return count; |
||
49 | } |
||
50 | |||
51 | static int add_name (NCDFastNames *o, NCDStringIndex *string_index, const char *str, size_t str_len, const char *remain, size_t remain_len) |
||
52 | { |
||
53 | ASSERT(str) |
||
54 | ASSERT(!!o->dynamic_names == (o->num_names > NCD_NUM_FAST_NAMES)) |
||
55 | |||
56 | NCD_string_id_t id = NCDStringIndex_GetBin(string_index, str, str_len); |
||
57 | if (id < 0) { |
||
58 | return 0; |
||
59 | } |
||
60 | |||
61 | if (o->num_names < NCD_NUM_FAST_NAMES) { |
||
62 | o->static_names[o->num_names++] = id; |
||
63 | return 1; |
||
64 | } |
||
65 | |||
66 | if (o->num_names == NCD_NUM_FAST_NAMES) { |
||
67 | size_t num_more = (!remain ? 0 : count_names(remain, remain_len)); |
||
68 | size_t num_all = o->num_names + 1 + num_more; |
||
69 | |||
70 | if (!(o->dynamic_names = BAllocArray(num_all, sizeof(o->dynamic_names[0])))) { |
||
71 | return 0; |
||
72 | } |
||
73 | |||
74 | memcpy(o->dynamic_names, o->static_names, NCD_NUM_FAST_NAMES * sizeof(o->dynamic_names[0])); |
||
75 | } |
||
76 | |||
77 | o->dynamic_names[o->num_names++] = id; |
||
78 | |||
79 | return 1; |
||
80 | } |
||
81 | |||
82 | int NCDFastNames_Init (NCDFastNames *o, NCDStringIndex *string_index, MemRef str) |
||
83 | { |
||
84 | ASSERT(str.ptr) |
||
85 | |||
86 | o->num_names = 0; |
||
87 | o->dynamic_names = NULL; |
||
88 | |||
89 | size_t i = 0; |
||
90 | while (i < str.len) { |
||
91 | if (str.ptr[i] == '.') { |
||
92 | if (!add_name(o, string_index, str.ptr, i, str.ptr + (i + 1), str.len - (i + 1))) { |
||
93 | goto fail; |
||
94 | } |
||
95 | str = MemRef_SubFrom(str, i + 1); |
||
96 | i = 0; |
||
97 | continue; |
||
98 | } |
||
99 | i++; |
||
100 | } |
||
101 | |||
102 | if (!add_name(o, string_index, str.ptr, i, NULL, 0)) { |
||
103 | goto fail; |
||
104 | } |
||
105 | |||
106 | return 1; |
||
107 | |||
108 | fail: |
||
109 | BFree(o->dynamic_names); |
||
110 | return 0; |
||
111 | } |
||
112 | |||
113 | void NCDFastNames_Free (NCDFastNames *o) |
||
114 | { |
||
115 | if (o->dynamic_names) { |
||
116 | BFree(o->dynamic_names); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | size_t NCDFastNames_GetNumNames (NCDFastNames *o) |
||
121 | { |
||
122 | ASSERT(o->num_names > 0) |
||
123 | |||
124 | return o->num_names; |
||
125 | } |
||
126 | |||
127 | NCD_string_id_t * NCDFastNames_GetNames (NCDFastNames *o) |
||
128 | { |
||
129 | return o->dynamic_names ? o->dynamic_names : o->static_names; |
||
130 | } |