No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / store_mem.c
blob5d6d706ced2bda85a710c0e63b1b810c1ffd7f12
1 /*
2 * Copyright (c) 1997 - 2000, 2002 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 "krb5_locl.h"
35 #include "store-int.h"
37 __RCSID("$Heimdal: store_mem.c 20307 2007-04-11 11:16:28Z lha $"
38 "$NetBSD$");
40 typedef struct mem_storage{
41 unsigned char *base;
42 size_t size;
43 unsigned char *ptr;
44 }mem_storage;
46 static ssize_t
47 mem_fetch(krb5_storage *sp, void *data, size_t size)
49 mem_storage *s = (mem_storage*)sp->data;
50 if(size > s->base + s->size - s->ptr)
51 size = s->base + s->size - s->ptr;
52 memmove(data, s->ptr, size);
53 sp->seek(sp, size, SEEK_CUR);
54 return size;
57 static ssize_t
58 mem_store(krb5_storage *sp, const void *data, size_t size)
60 mem_storage *s = (mem_storage*)sp->data;
61 if(size > s->base + s->size - s->ptr)
62 size = s->base + s->size - s->ptr;
63 memmove(s->ptr, data, size);
64 sp->seek(sp, size, SEEK_CUR);
65 return size;
68 static ssize_t
69 mem_no_store(krb5_storage *sp, const void *data, size_t size)
71 return -1;
74 static off_t
75 mem_seek(krb5_storage *sp, off_t offset, int whence)
77 mem_storage *s = (mem_storage*)sp->data;
78 switch(whence){
79 case SEEK_SET:
80 if(offset > s->size)
81 offset = s->size;
82 if(offset < 0)
83 offset = 0;
84 s->ptr = s->base + offset;
85 break;
86 case SEEK_CUR:
87 return sp->seek(sp, s->ptr - s->base + offset, SEEK_SET);
88 case SEEK_END:
89 return sp->seek(sp, s->size + offset, SEEK_SET);
90 default:
91 errno = EINVAL;
92 return -1;
94 return s->ptr - s->base;
97 krb5_storage * KRB5_LIB_FUNCTION
98 krb5_storage_from_mem(void *buf, size_t len)
100 krb5_storage *sp = malloc(sizeof(krb5_storage));
101 mem_storage *s;
102 if(sp == NULL)
103 return NULL;
104 s = malloc(sizeof(*s));
105 if(s == NULL) {
106 free(sp);
107 return NULL;
109 sp->data = s;
110 sp->flags = 0;
111 sp->eof_code = HEIM_ERR_EOF;
112 s->base = buf;
113 s->size = len;
114 s->ptr = buf;
115 sp->fetch = mem_fetch;
116 sp->store = mem_store;
117 sp->seek = mem_seek;
118 sp->free = NULL;
119 return sp;
122 krb5_storage * KRB5_LIB_FUNCTION
123 krb5_storage_from_data(krb5_data *data)
125 return krb5_storage_from_mem(data->data, data->length);
128 krb5_storage * KRB5_LIB_FUNCTION
129 krb5_storage_from_readonly_mem(const void *buf, size_t len)
131 krb5_storage *sp = malloc(sizeof(krb5_storage));
132 mem_storage *s;
133 if(sp == NULL)
134 return NULL;
135 s = malloc(sizeof(*s));
136 if(s == NULL) {
137 free(sp);
138 return NULL;
140 sp->data = s;
141 sp->flags = 0;
142 sp->eof_code = HEIM_ERR_EOF;
143 s->base = rk_UNCONST(buf);
144 s->size = len;
145 s->ptr = rk_UNCONST(buf);
146 sp->fetch = mem_fetch;
147 sp->store = mem_no_store;
148 sp->seek = mem_seek;
149 sp->free = NULL;
150 return sp;