xref: /freebsd/crypto/heimdal/lib/krb5/store_mem.c (revision c19800e8)
1b528cefcSMark Murray /*
28373020dSJacques Vidrine  * Copyright (c) 1997 - 2000, 2002 Kungliga Tekniska Högskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include "krb5_locl.h"
358373020dSJacques Vidrine #include "store-int.h"
36b528cefcSMark Murray 
37c19800e8SDoug Rabson typedef struct mem_storage{
38b528cefcSMark Murray     unsigned char *base;
39b528cefcSMark Murray     size_t size;
40b528cefcSMark Murray     unsigned char *ptr;
41b528cefcSMark Murray }mem_storage;
42b528cefcSMark Murray 
43b528cefcSMark Murray static ssize_t
mem_fetch(krb5_storage * sp,void * data,size_t size)44b528cefcSMark Murray mem_fetch(krb5_storage *sp, void *data, size_t size)
45b528cefcSMark Murray {
46b528cefcSMark Murray     mem_storage *s = (mem_storage*)sp->data;
47b528cefcSMark Murray     if(size > (size_t)(s->base + s->size - s->ptr))
48b528cefcSMark Murray 	size = s->base + s->size - s->ptr;
49b528cefcSMark Murray     memmove(data, s->ptr, size);
50b528cefcSMark Murray     sp->seek(sp, size, SEEK_CUR);
51b528cefcSMark Murray     return size;
52b528cefcSMark Murray }
53b528cefcSMark Murray 
54b528cefcSMark Murray static ssize_t
mem_store(krb5_storage * sp,const void * data,size_t size)55b528cefcSMark Murray mem_store(krb5_storage *sp, const void *data, size_t size)
56b528cefcSMark Murray {
575e9cd1aeSAssar Westerlund     mem_storage *s = (mem_storage*)sp->data;
58b528cefcSMark Murray     if(size > (size_t)(s->base + s->size - s->ptr))
59b528cefcSMark Murray 	size = s->base + s->size - s->ptr;
60b528cefcSMark Murray     memmove(s->ptr, data, size);
61b528cefcSMark Murray     sp->seek(sp, size, SEEK_CUR);
62b528cefcSMark Murray     return size;
63b528cefcSMark Murray }
64b528cefcSMark Murray 
65b528cefcSMark Murray static ssize_t
mem_no_store(krb5_storage * sp,const void * data,size_t size)66b528cefcSMark Murray mem_no_store(krb5_storage *sp, const void *data, size_t size)
67c19800e8SDoug Rabson {
68c19800e8SDoug Rabson     return -1;
69c19800e8SDoug Rabson }
70c19800e8SDoug Rabson 
71c19800e8SDoug Rabson static off_t
mem_seek(krb5_storage * sp,off_t offset,int whence)72c19800e8SDoug Rabson mem_seek(krb5_storage *sp, off_t offset, int whence)
73b528cefcSMark Murray {
74b528cefcSMark Murray     mem_storage *s = (mem_storage*)sp->data;
75b528cefcSMark Murray     switch(whence){
76b528cefcSMark Murray     case SEEK_SET:
77b528cefcSMark Murray 	if((size_t)offset > s->size)
78b528cefcSMark Murray 	    offset = s->size;
79b528cefcSMark Murray 	if(offset < 0)
80b528cefcSMark Murray 	    offset = 0;
81b528cefcSMark Murray 	s->ptr = s->base + offset;
82b528cefcSMark Murray 	break;
83b528cefcSMark Murray     case SEEK_CUR:
84b528cefcSMark Murray 	return sp->seek(sp, s->ptr - s->base + offset, SEEK_SET);
85b528cefcSMark Murray     case SEEK_END:
86b528cefcSMark Murray 	return sp->seek(sp, s->size + offset, SEEK_SET);
87b528cefcSMark Murray     default:
88b528cefcSMark Murray 	errno = EINVAL;
89b528cefcSMark Murray 	return -1;
90b528cefcSMark Murray     }
91b528cefcSMark Murray     return s->ptr - s->base;
92b528cefcSMark Murray }
93b528cefcSMark Murray 
94b528cefcSMark Murray static int
mem_trunc(krb5_storage * sp,off_t offset)95b528cefcSMark Murray mem_trunc(krb5_storage *sp, off_t offset)
96c19800e8SDoug Rabson {
97b528cefcSMark Murray     mem_storage *s = (mem_storage*)sp->data;
98b528cefcSMark Murray     if((size_t)offset > s->size)
99b528cefcSMark Murray 	return ERANGE;
100b528cefcSMark Murray     s->size = offset;
101b528cefcSMark Murray     if ((s->ptr - s->base) > offset)
102b528cefcSMark Murray 	s->ptr = s->base + offset;
103b528cefcSMark Murray     return 0;
104b528cefcSMark Murray }
105b528cefcSMark Murray 
106b528cefcSMark Murray static int
mem_no_trunc(krb5_storage * sp,off_t offset)107b528cefcSMark Murray mem_no_trunc(krb5_storage *sp, off_t offset)
108b528cefcSMark Murray {
109b528cefcSMark Murray     return EINVAL;
1108373020dSJacques Vidrine }
111b528cefcSMark Murray 
112b528cefcSMark Murray /**
113b528cefcSMark Murray  * Create a fixed size memory storage block
114b528cefcSMark Murray  *
115b528cefcSMark Murray  * @return A krb5_storage on success, or NULL on out of memory error.
116b528cefcSMark Murray  *
117b528cefcSMark Murray  * @ingroup krb5_storage
118b528cefcSMark Murray  *
119b528cefcSMark Murray  * @sa krb5_storage_mem()
120b528cefcSMark Murray  * @sa krb5_storage_from_readonly_mem()
121c19800e8SDoug Rabson  * @sa krb5_storage_from_data()
122b528cefcSMark Murray  * @sa krb5_storage_from_fd()
123b528cefcSMark Murray  */
124b528cefcSMark Murray 
125b528cefcSMark Murray KRB5_LIB_FUNCTION krb5_storage * KRB5_LIB_CALL
krb5_storage_from_mem(void * buf,size_t len)126c19800e8SDoug Rabson krb5_storage_from_mem(void *buf, size_t len)
127c19800e8SDoug Rabson {
128c19800e8SDoug Rabson     krb5_storage *sp = malloc(sizeof(krb5_storage));
129c19800e8SDoug Rabson     mem_storage *s;
130c19800e8SDoug Rabson     if(sp == NULL)
131c19800e8SDoug Rabson 	return NULL;
132c19800e8SDoug Rabson     s = malloc(sizeof(*s));
133c19800e8SDoug Rabson     if(s == NULL) {
134c19800e8SDoug Rabson 	free(sp);
135c19800e8SDoug Rabson 	return NULL;
136c19800e8SDoug Rabson     }
137c19800e8SDoug Rabson     sp->data = s;
138c19800e8SDoug Rabson     sp->flags = 0;
139c19800e8SDoug Rabson     sp->eof_code = HEIM_ERR_EOF;
140c19800e8SDoug Rabson     s->base = buf;
141c19800e8SDoug Rabson     s->size = len;
142c19800e8SDoug Rabson     s->ptr = buf;
143c19800e8SDoug Rabson     sp->fetch = mem_fetch;
144c19800e8SDoug Rabson     sp->store = mem_store;
145c19800e8SDoug Rabson     sp->seek = mem_seek;
146c19800e8SDoug Rabson     sp->trunc = mem_trunc;
147c19800e8SDoug Rabson     sp->free = NULL;
148c19800e8SDoug Rabson     sp->max_alloc = UINT_MAX/8;
149c19800e8SDoug Rabson     return sp;
150c19800e8SDoug Rabson }
151 
152 /**
153  * Create a fixed size memory storage block
154  *
155  * @return A krb5_storage on success, or NULL on out of memory error.
156  *
157  * @ingroup krb5_storage
158  *
159  * @sa krb5_storage_mem()
160  * @sa krb5_storage_from_mem()
161  * @sa krb5_storage_from_readonly_mem()
162  * @sa krb5_storage_from_fd()
163  */
164 
165 KRB5_LIB_FUNCTION krb5_storage * KRB5_LIB_CALL
krb5_storage_from_data(krb5_data * data)166 krb5_storage_from_data(krb5_data *data)
167 {
168     return krb5_storage_from_mem(data->data, data->length);
169 }
170 
171 /**
172  * Create a fixed size memory storage block that is read only
173  *
174  * @return A krb5_storage on success, or NULL on out of memory error.
175  *
176  * @ingroup krb5_storage
177  *
178  * @sa krb5_storage_mem()
179  * @sa krb5_storage_from_mem()
180  * @sa krb5_storage_from_data()
181  * @sa krb5_storage_from_fd()
182  */
183 
184 KRB5_LIB_FUNCTION krb5_storage * KRB5_LIB_CALL
krb5_storage_from_readonly_mem(const void * buf,size_t len)185 krb5_storage_from_readonly_mem(const void *buf, size_t len)
186 {
187     krb5_storage *sp = malloc(sizeof(krb5_storage));
188     mem_storage *s;
189     if(sp == NULL)
190 	return NULL;
191     s = malloc(sizeof(*s));
192     if(s == NULL) {
193 	free(sp);
194 	return NULL;
195     }
196     sp->data = s;
197     sp->flags = 0;
198     sp->eof_code = HEIM_ERR_EOF;
199     s->base = rk_UNCONST(buf);
200     s->size = len;
201     s->ptr = rk_UNCONST(buf);
202     sp->fetch = mem_fetch;
203     sp->store = mem_no_store;
204     sp->seek = mem_seek;
205     sp->trunc = mem_no_trunc;
206     sp->free = NULL;
207     sp->max_alloc = UINT_MAX/8;
208     return sp;
209 }
210