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