1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov * Copyright (c) 1997 - 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
37b528cefcSMark Murray typedef struct emem_storage{
38b528cefcSMark Murray unsigned char *base;
39b528cefcSMark Murray size_t size;
40b528cefcSMark Murray size_t len;
41b528cefcSMark Murray unsigned char *ptr;
42b528cefcSMark Murray }emem_storage;
43b528cefcSMark Murray
44b528cefcSMark Murray static ssize_t
emem_fetch(krb5_storage * sp,void * data,size_t size)45b528cefcSMark Murray emem_fetch(krb5_storage *sp, void *data, size_t size)
46b528cefcSMark Murray {
47b528cefcSMark Murray emem_storage *s = (emem_storage*)sp->data;
48*ae771770SStanislav Sedov if((size_t)(s->base + s->len - s->ptr) < size)
49b528cefcSMark Murray size = s->base + s->len - s->ptr;
50b528cefcSMark Murray memmove(data, s->ptr, size);
51b528cefcSMark Murray sp->seek(sp, size, SEEK_CUR);
52b528cefcSMark Murray return size;
53b528cefcSMark Murray }
54b528cefcSMark Murray
55b528cefcSMark Murray static ssize_t
emem_store(krb5_storage * sp,const void * data,size_t size)565e9cd1aeSAssar Westerlund emem_store(krb5_storage *sp, const void *data, size_t size)
57b528cefcSMark Murray {
58b528cefcSMark Murray emem_storage *s = (emem_storage*)sp->data;
59*ae771770SStanislav Sedov if(size > (size_t)(s->base + s->size - s->ptr)){
60b528cefcSMark Murray void *base;
61b528cefcSMark Murray size_t sz, off;
62b528cefcSMark Murray off = s->ptr - s->base;
635bda878eSJacques Vidrine sz = off + size;
645bda878eSJacques Vidrine if (sz < 4096)
655bda878eSJacques Vidrine sz *= 2;
66b528cefcSMark Murray base = realloc(s->base, sz);
67b528cefcSMark Murray if(base == NULL)
68*ae771770SStanislav Sedov return -1;
69b528cefcSMark Murray s->size = sz;
70b528cefcSMark Murray s->base = base;
71b528cefcSMark Murray s->ptr = (unsigned char*)base + off;
72b528cefcSMark Murray }
73b528cefcSMark Murray memmove(s->ptr, data, size);
74b528cefcSMark Murray sp->seek(sp, size, SEEK_CUR);
75b528cefcSMark Murray return size;
76b528cefcSMark Murray }
77b528cefcSMark Murray
78b528cefcSMark Murray static off_t
emem_seek(krb5_storage * sp,off_t offset,int whence)79b528cefcSMark Murray emem_seek(krb5_storage *sp, off_t offset, int whence)
80b528cefcSMark Murray {
81b528cefcSMark Murray emem_storage *s = (emem_storage*)sp->data;
82b528cefcSMark Murray switch(whence){
83b528cefcSMark Murray case SEEK_SET:
84*ae771770SStanislav Sedov if((size_t)offset > s->size)
85b528cefcSMark Murray offset = s->size;
86b528cefcSMark Murray if(offset < 0)
87b528cefcSMark Murray offset = 0;
88b528cefcSMark Murray s->ptr = s->base + offset;
89*ae771770SStanislav Sedov if((size_t)offset > s->len)
90b528cefcSMark Murray s->len = offset;
91b528cefcSMark Murray break;
92b528cefcSMark Murray case SEEK_CUR:
93b528cefcSMark Murray sp->seek(sp,s->ptr - s->base + offset, SEEK_SET);
94b528cefcSMark Murray break;
95b528cefcSMark Murray case SEEK_END:
96b528cefcSMark Murray sp->seek(sp, s->len + offset, SEEK_SET);
97b528cefcSMark Murray break;
98b528cefcSMark Murray default:
99b528cefcSMark Murray errno = EINVAL;
100b528cefcSMark Murray return -1;
101b528cefcSMark Murray }
102b528cefcSMark Murray return s->ptr - s->base;
103b528cefcSMark Murray }
104b528cefcSMark Murray
105*ae771770SStanislav Sedov static int
emem_trunc(krb5_storage * sp,off_t offset)106*ae771770SStanislav Sedov emem_trunc(krb5_storage *sp, off_t offset)
107*ae771770SStanislav Sedov {
108*ae771770SStanislav Sedov emem_storage *s = (emem_storage*)sp->data;
109*ae771770SStanislav Sedov /*
110*ae771770SStanislav Sedov * If offset is larget then current size, or current size is
111*ae771770SStanislav Sedov * shrunk more then half of the current size, adjust buffer.
112*ae771770SStanislav Sedov */
113*ae771770SStanislav Sedov if (offset == 0) {
114*ae771770SStanislav Sedov free(s->base);
115*ae771770SStanislav Sedov s->size = 0;
116*ae771770SStanislav Sedov s->base = NULL;
117*ae771770SStanislav Sedov s->ptr = NULL;
118*ae771770SStanislav Sedov } else if ((size_t)offset > s->size || (s->size / 2) > (size_t)offset) {
119*ae771770SStanislav Sedov void *base;
120*ae771770SStanislav Sedov size_t off;
121*ae771770SStanislav Sedov off = s->ptr - s->base;
122*ae771770SStanislav Sedov base = realloc(s->base, offset);
123*ae771770SStanislav Sedov if(base == NULL)
124*ae771770SStanislav Sedov return ENOMEM;
125*ae771770SStanislav Sedov if ((size_t)offset > s->size)
126*ae771770SStanislav Sedov memset((char *)base + s->size, 0, offset - s->size);
127*ae771770SStanislav Sedov s->size = offset;
128*ae771770SStanislav Sedov s->base = base;
129*ae771770SStanislav Sedov s->ptr = (unsigned char *)base + off;
130*ae771770SStanislav Sedov }
131*ae771770SStanislav Sedov s->len = offset;
132*ae771770SStanislav Sedov if ((s->ptr - s->base) > offset)
133*ae771770SStanislav Sedov s->ptr = s->base + offset;
134*ae771770SStanislav Sedov return 0;
135*ae771770SStanislav Sedov }
136*ae771770SStanislav Sedov
137*ae771770SStanislav Sedov
138b528cefcSMark Murray static void
emem_free(krb5_storage * sp)139b528cefcSMark Murray emem_free(krb5_storage *sp)
140b528cefcSMark Murray {
1414137ff4cSJacques Vidrine emem_storage *s = sp->data;
1424137ff4cSJacques Vidrine memset(s->base, 0, s->len);
1434137ff4cSJacques Vidrine free(s->base);
144b528cefcSMark Murray }
145b528cefcSMark Murray
146*ae771770SStanislav Sedov /**
147*ae771770SStanislav Sedov * Create a elastic (allocating) memory storage backend. Memory is
148*ae771770SStanislav Sedov * allocated on demand. Free returned krb5_storage with
149*ae771770SStanislav Sedov * krb5_storage_free().
150*ae771770SStanislav Sedov *
151*ae771770SStanislav Sedov * @return A krb5_storage on success, or NULL on out of memory error.
152*ae771770SStanislav Sedov *
153*ae771770SStanislav Sedov * @ingroup krb5_storage
154*ae771770SStanislav Sedov *
155*ae771770SStanislav Sedov * @sa krb5_storage_from_mem()
156*ae771770SStanislav Sedov * @sa krb5_storage_from_readonly_mem()
157*ae771770SStanislav Sedov * @sa krb5_storage_from_fd()
158*ae771770SStanislav Sedov * @sa krb5_storage_from_data()
159*ae771770SStanislav Sedov */
160*ae771770SStanislav Sedov
161*ae771770SStanislav Sedov KRB5_LIB_FUNCTION krb5_storage * KRB5_LIB_CALL
krb5_storage_emem(void)162b528cefcSMark Murray krb5_storage_emem(void)
163b528cefcSMark Murray {
164*ae771770SStanislav Sedov krb5_storage *sp;
165*ae771770SStanislav Sedov emem_storage *s;
166*ae771770SStanislav Sedov
167*ae771770SStanislav Sedov sp = malloc(sizeof(krb5_storage));
168c19800e8SDoug Rabson if (sp == NULL)
169c19800e8SDoug Rabson return NULL;
170*ae771770SStanislav Sedov
171*ae771770SStanislav Sedov s = malloc(sizeof(*s));
172c19800e8SDoug Rabson if (s == NULL) {
173c19800e8SDoug Rabson free(sp);
174c19800e8SDoug Rabson return NULL;
175c19800e8SDoug Rabson }
176b528cefcSMark Murray sp->data = s;
177b528cefcSMark Murray sp->flags = 0;
1788373020dSJacques Vidrine sp->eof_code = HEIM_ERR_EOF;
179b528cefcSMark Murray s->size = 1024;
180b528cefcSMark Murray s->base = malloc(s->size);
181c19800e8SDoug Rabson if (s->base == NULL) {
182c19800e8SDoug Rabson free(sp);
183c19800e8SDoug Rabson free(s);
184c19800e8SDoug Rabson return NULL;
185c19800e8SDoug Rabson }
186b528cefcSMark Murray s->len = 0;
187b528cefcSMark Murray s->ptr = s->base;
188b528cefcSMark Murray sp->fetch = emem_fetch;
189b528cefcSMark Murray sp->store = emem_store;
190b528cefcSMark Murray sp->seek = emem_seek;
191*ae771770SStanislav Sedov sp->trunc = emem_trunc;
192b528cefcSMark Murray sp->free = emem_free;
193*ae771770SStanislav Sedov sp->max_alloc = UINT_MAX/8;
194b528cefcSMark Murray return sp;
195b528cefcSMark Murray }
196