Plugin symbols can't have '-' in them... Also add example to krb5-plugin.7
[heimdal.git] / base / data.c
blobfc1c4422eec6f012d0347b738bfb6b73336d6f2a
1 /*
2 * Copyright (c) 2011 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "baselocl.h"
35 #include <string.h>
37 static void
38 data_dealloc(void *ptr)
42 static int
43 data_cmp(void *a, void *b)
45 heim_octet_string *osa = a, *osb = b;
46 if (osa->length != osb->length)
47 return osa->length - osb->length;
48 return memcmp(osa->data, osb->data, osa->length);
51 static unsigned long
52 data_hash(void *ptr)
54 heim_octet_string *os = ptr;
55 const unsigned char *s = os->data;
57 if (os->length < 4)
58 return os->length;
59 return s[0] | (s[1] << 8) |
60 (s[os->length - 2] << 16) | (s[os->length - 1] << 24);
63 struct heim_type_data _heim_data_object = {
64 HEIM_TID_DATA,
65 "data-object",
66 NULL,
67 data_dealloc,
68 NULL,
69 data_cmp,
70 data_hash
73 /**
74 * Create a data object
76 * @param string the string to create, must be an utf8 string
78 * @return string object
81 heim_data_t
82 heim_data_create(const void *data, size_t length)
84 heim_octet_string *os;
86 os = _heim_alloc_object(&_heim_data_object, sizeof(*os) + length);
87 if (os) {
88 os->data = (uint8_t *)os + sizeof(*os);
89 os->length = length;
90 memcpy(os->data, data, length);
92 return (heim_data_t)os;
95 /**
96 * Return the type ID of data objects
98 * @return type id of string objects
101 heim_tid_t
102 heim_data_get_type_id(void)
104 return HEIM_TID_DATA;
108 * Get the data value of the content.
110 * @param data the data object to get the value from
112 * @return a heim_octet_string
115 const heim_octet_string *
116 heim_data_get_data(heim_data_t data)
118 return (const heim_octet_string *)data;
121 const void *
122 heim_data_get_ptr(heim_data_t data)
124 return ((const heim_octet_string *)data)->data;
127 size_t heim_data_get_length(heim_data_t data)
129 return ((const heim_octet_string *)data)->length;