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