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