/src/samba/third_party/heimdal/lib/base/data.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2011 Kungliga Tekniska Högskolan |
3 | | * (Royal Institute of Technology, Stockholm, Sweden). |
4 | | * All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions |
8 | | * are met: |
9 | | * |
10 | | * 1. Redistributions of source code must retain the above copyright |
11 | | * notice, this list of conditions and the following disclaimer. |
12 | | * |
13 | | * 2. Redistributions in binary form must reproduce the above copyright |
14 | | * notice, this list of conditions and the following disclaimer in the |
15 | | * documentation and/or other materials provided with the distribution. |
16 | | * |
17 | | * 3. Neither the name of the Institute nor the names of its contributors |
18 | | * may be used to endorse or promote products derived from this software |
19 | | * without specific prior written permission. |
20 | | * |
21 | | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
22 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
23 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
25 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
27 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
28 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
29 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
30 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
31 | | * SUCH DAMAGE. |
32 | | */ |
33 | | |
34 | | #include "baselocl.h" |
35 | | #include <string.h> |
36 | | |
37 | | static void HEIM_CALLCONV |
38 | | data_dealloc(void *ptr) |
39 | 0 | { |
40 | 0 | heim_data_t d = ptr; |
41 | 0 | heim_octet_string *os = (heim_octet_string *)d; |
42 | 0 | heim_data_free_f_t *deallocp; |
43 | 0 | heim_data_free_f_t dealloc; |
44 | |
|
45 | 0 | if (os->data == NULL) |
46 | 0 | return; |
47 | | |
48 | | /* Possible string ref */ |
49 | 0 | deallocp = _heim_get_isaextra(os, 0); |
50 | 0 | dealloc = *deallocp; |
51 | 0 | if (dealloc != NULL) |
52 | 0 | dealloc(os->data); |
53 | 0 | } |
54 | | |
55 | | static int |
56 | | data_cmp(void *a, void *b) |
57 | 0 | { |
58 | 0 | heim_octet_string *osa = a, *osb = b; |
59 | 0 | if (osa->length != osb->length) |
60 | 0 | return osa->length - osb->length; |
61 | 0 | return memcmp(osa->data, osb->data, osa->length); |
62 | 0 | } |
63 | | |
64 | | static uintptr_t |
65 | | data_hash(void *ptr) |
66 | 0 | { |
67 | 0 | heim_octet_string *os = ptr; |
68 | 0 | const unsigned char *s = os->data; |
69 | |
|
70 | 0 | if (os->length < 4) |
71 | 0 | return os->length; |
72 | | |
73 | 0 | return ((unsigned long)s[os->length - 1] << 24) |
74 | 0 | | (s[os->length - 2] << 16) | (s[1] << 8) | s[0]; |
75 | 0 | } |
76 | | |
77 | | struct heim_type_data _heim_data_object = { |
78 | | HEIM_TID_DATA, |
79 | | "data-object", |
80 | | NULL, |
81 | | data_dealloc, |
82 | | NULL, |
83 | | data_cmp, |
84 | | data_hash, |
85 | | NULL |
86 | | }; |
87 | | |
88 | | /** |
89 | | * Create a data object |
90 | | * |
91 | | * @param string the string to create, must be an utf8 string |
92 | | * |
93 | | * @return string object |
94 | | */ |
95 | | |
96 | | heim_data_t |
97 | | heim_data_create(const void *data, size_t length) |
98 | 0 | { |
99 | 0 | heim_octet_string *os; |
100 | |
|
101 | 0 | os = _heim_alloc_object(&_heim_data_object, sizeof(*os) + length); |
102 | 0 | if (os) { |
103 | 0 | os->data = (uint8_t *)os + sizeof(*os); |
104 | 0 | os->length = length; |
105 | 0 | memcpy(os->data, data, length); |
106 | 0 | } |
107 | 0 | return (heim_data_t)os; |
108 | 0 | } |
109 | | |
110 | | heim_data_t |
111 | | heim_data_ref_create(const void *data, size_t length, |
112 | | heim_data_free_f_t dealloc) |
113 | 0 | { |
114 | 0 | heim_octet_string *os; |
115 | 0 | heim_data_free_f_t *deallocp; |
116 | |
|
117 | 0 | os = _heim_alloc_object(&_heim_data_object, sizeof(*os) + length); |
118 | 0 | if (os) { |
119 | 0 | os->data = (void *)data; |
120 | 0 | os->length = length; |
121 | 0 | deallocp = _heim_get_isaextra(os, 0); |
122 | 0 | *deallocp = dealloc; |
123 | 0 | } |
124 | 0 | return (heim_data_t)os; |
125 | 0 | } |
126 | | |
127 | | |
128 | | /** |
129 | | * Return the type ID of data objects |
130 | | * |
131 | | * @return type id of data objects |
132 | | */ |
133 | | |
134 | | heim_tid_t |
135 | | heim_data_get_type_id(void) |
136 | 0 | { |
137 | 0 | return HEIM_TID_DATA; |
138 | 0 | } |
139 | | |
140 | | /** |
141 | | * Get the data value of the content. |
142 | | * |
143 | | * @param data the data object to get the value from |
144 | | * |
145 | | * @return a heim_octet_string |
146 | | */ |
147 | | |
148 | | const heim_octet_string * |
149 | | heim_data_get_data(heim_data_t data) |
150 | 0 | { |
151 | | /* Note that this works for data and data_ref objects */ |
152 | 0 | return (const heim_octet_string *)data; |
153 | 0 | } |
154 | | |
155 | | const void * |
156 | | heim_data_get_ptr(heim_data_t data) |
157 | 0 | { |
158 | | /* Note that this works for data and data_ref objects */ |
159 | 0 | return ((const heim_octet_string *)data)->data; |
160 | 0 | } |
161 | | |
162 | | size_t heim_data_get_length(heim_data_t data) |
163 | 0 | { |
164 | | /* Note that this works for data and data_ref objects */ |
165 | 0 | return ((const heim_octet_string *)data)->length; |
166 | 0 | } |