xref: /freebsd/crypto/heimdal/lib/krb5/store_mem.c (revision 17d6c636720d00f77e5d098daf4c278f89d84f7b)
1 /*
2  * Copyright (c) 1997 - 2000 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 "krb5_locl.h"
35 
36 RCSID("$Id: store_mem.c,v 1.10 2000/05/19 14:39:02 assar Exp $");
37 
38 typedef struct mem_storage{
39     unsigned char *base;
40     size_t size;
41     unsigned char *ptr;
42 }mem_storage;
43 
44 static ssize_t
45 mem_fetch(krb5_storage *sp, void *data, size_t size)
46 {
47     mem_storage *s = (mem_storage*)sp->data;
48     if(size > s->base + s->size - s->ptr)
49 	size = s->base + s->size - s->ptr;
50     memmove(data, s->ptr, size);
51     sp->seek(sp, size, SEEK_CUR);
52     return size;
53 }
54 
55 static ssize_t
56 mem_store(krb5_storage *sp, const void *data, size_t size)
57 {
58     mem_storage *s = (mem_storage*)sp->data;
59     if(size > s->base + s->size - s->ptr)
60 	size = s->base + s->size - s->ptr;
61     memmove(s->ptr, data, size);
62     sp->seek(sp, size, SEEK_CUR);
63     return size;
64 }
65 
66 static off_t
67 mem_seek(krb5_storage *sp, off_t offset, int whence)
68 {
69     mem_storage *s = (mem_storage*)sp->data;
70     switch(whence){
71     case SEEK_SET:
72 	if(offset > s->size)
73 	    offset = s->size;
74 	if(offset < 0)
75 	    offset = 0;
76 	s->ptr = s->base + offset;
77 	break;
78     case SEEK_CUR:
79 	return sp->seek(sp, s->ptr - s->base + offset, SEEK_SET);
80     case SEEK_END:
81 	return sp->seek(sp, s->size + offset, SEEK_SET);
82     default:
83 	errno = EINVAL;
84 	return -1;
85     }
86     return s->ptr - s->base;
87 }
88 
89 krb5_storage *
90 krb5_storage_from_mem(void *buf, size_t len)
91 {
92     krb5_storage *sp = malloc(sizeof(krb5_storage));
93     mem_storage *s;
94     if(sp == NULL)
95 	return NULL;
96     s = malloc(sizeof(*s));
97     if(s == NULL) {
98 	free(sp);
99 	return NULL;
100     }
101     sp->data = s;
102     sp->flags = 0;
103     s->base = buf;
104     s->size = len;
105     s->ptr = buf;
106     sp->fetch = mem_fetch;
107     sp->store = mem_store;
108     sp->seek = mem_seek;
109     sp->free = NULL;
110     return sp;
111 }
112 
113 krb5_storage *
114 krb5_storage_from_data(krb5_data *data)
115 {
116 	return krb5_storage_from_mem(data->data, data->length);
117 }
118