LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/krb5 - store_emem.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 70 104 67.3 %
Date: 2024-06-13 04:01:37 Functions: 5 6 83.3 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 1997 - 2002 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             : #include "store-int.h"
      36             : #include <assert.h>
      37             : 
      38             : typedef struct emem_storage{
      39             :     unsigned char *base;
      40             :     size_t size;
      41             :     size_t len;
      42             :     unsigned char *ptr;
      43             : }emem_storage;
      44             : 
      45             : static ssize_t
      46      327974 : emem_fetch(krb5_storage *sp, void *data, size_t size)
      47             : {
      48      327974 :     emem_storage *s = (emem_storage*)sp->data;
      49             : 
      50      327974 :     assert(data != NULL && s->ptr != NULL);
      51             : 
      52      327974 :     if((size_t)(s->base + s->len - s->ptr) < size)
      53           0 :         size = s->base + s->len - s->ptr;
      54      327974 :     memmove(data, s->ptr, size);
      55      327974 :     sp->seek(sp, size, SEEK_CUR);
      56      327974 :     return size;
      57             : }
      58             : 
      59             : static ssize_t
      60     3002545 : emem_store(krb5_storage *sp, const void *data, size_t size)
      61             : {
      62             :     emem_storage *s;
      63             : 
      64     3002545 :     if (size == 0) {
      65       72874 :         sp->seek(sp, 0, SEEK_CUR);
      66       72874 :         return 0;
      67             :     }
      68             : 
      69     2929671 :     s = (emem_storage*)sp->data;
      70             : 
      71     2929671 :     assert(data != NULL);
      72             : 
      73     2929671 :     if(size > (size_t)(s->base + s->size - s->ptr)){
      74             :         void *base;
      75             :         size_t sz, off;
      76       29375 :         off = s->ptr - s->base;
      77       29375 :         sz = off + size;
      78       29375 :         if (sz < 4096)
      79       29367 :             sz *= 2;
      80       29375 :         base = realloc(s->base, sz);
      81       29375 :         if(base == NULL)
      82           0 :             return -1;
      83       29375 :         s->size = sz;
      84       29375 :         s->base = base;
      85       29375 :         s->ptr = (unsigned char*)base + off;
      86             :     }
      87     2929671 :     memmove(s->ptr, data, size);
      88     2929671 :     sp->seek(sp, size, SEEK_CUR);
      89     2929671 :     return size;
      90             : }
      91             : 
      92             : static off_t
      93     8628886 : emem_seek(krb5_storage *sp, off_t offset, int whence)
      94             : {
      95     8628886 :     emem_storage *s = (emem_storage*)sp->data;
      96     8628886 :     switch(whence){
      97     4642417 :     case SEEK_SET:
      98     4642417 :         if((size_t)offset > s->size)
      99           0 :             offset = s->size;
     100     4642417 :         if(offset < 0)
     101           0 :             offset = 0;
     102     4642417 :         s->ptr = s->base + offset;
     103     4642417 :         if((size_t)offset > s->len)
     104     2929671 :             s->len = offset;
     105     4642417 :         break;
     106     3658493 :     case SEEK_CUR:
     107     3658493 :         sp->seek(sp,s->ptr - s->base + offset, SEEK_SET);
     108     3658493 :         break;
     109      327976 :     case SEEK_END:
     110      327976 :         sp->seek(sp, s->len + offset, SEEK_SET);
     111      327976 :         break;
     112           0 :     default:
     113           0 :         errno = EINVAL;
     114           0 :         return -1;
     115             :     }
     116     8628886 :     return s->ptr - s->base;
     117             : }
     118             : 
     119             : static int
     120           0 : emem_trunc(krb5_storage *sp, off_t offset)
     121             : {
     122           0 :     emem_storage *s = (emem_storage*)sp->data;
     123             :     /*
     124             :      * If offset is larget then current size, or current size is
     125             :      * shrunk more then half of the current size, adjust buffer.
     126             :      */
     127           0 :     if (offset == 0) {
     128           0 :         free(s->base);
     129           0 :         s->size = 0;
     130           0 :         s->base = NULL;
     131           0 :         s->ptr = NULL;
     132           0 :     } else if ((size_t)offset > s->size || (s->size / 2) > (size_t)offset) {
     133             :         void *base;
     134             :         size_t off;
     135           0 :         off = s->ptr - s->base;
     136           0 :         base = realloc(s->base, offset);
     137           0 :         if(base == NULL)
     138           0 :             return ENOMEM;
     139           0 :         if ((size_t)offset > s->size)
     140           0 :             memset((char *)base + s->size, 0, offset - s->size);
     141           0 :         s->size = offset;
     142           0 :         s->base = base;
     143           0 :         s->ptr = (unsigned char *)base + off;
     144             :     }
     145           0 :     s->len = offset;
     146           0 :     if ((s->ptr - s->base) > offset)
     147           0 :         s->ptr = s->base + offset;
     148           0 :     return 0;
     149             : }
     150             : 
     151             : 
     152             : static void
     153      327974 : emem_free(krb5_storage *sp)
     154             : {
     155      327974 :     emem_storage *s = sp->data;
     156             : 
     157      327974 :     assert(s->base != NULL);
     158             : 
     159      327974 :     memset_s(s->base, s->len, 0, s->len);
     160      327974 :     free(s->base);
     161      327974 : }
     162             : 
     163             : /**
     164             :  * Create a elastic (allocating) memory storage backend. Memory is
     165             :  * allocated on demand. Free returned krb5_storage with
     166             :  * krb5_storage_free().
     167             :  *
     168             :  * @return A krb5_storage on success, or NULL on out of memory error.
     169             :  *
     170             :  * @ingroup krb5_storage
     171             :  *
     172             :  * @sa krb5_storage_from_mem()
     173             :  * @sa krb5_storage_from_readonly_mem()
     174             :  * @sa krb5_storage_from_fd()
     175             :  * @sa krb5_storage_from_data()
     176             :  * @sa krb5_storage_from_socket()
     177             :  */
     178             : 
     179             : KRB5_LIB_FUNCTION krb5_storage * KRB5_LIB_CALL
     180      327974 : krb5_storage_emem(void)
     181             : {
     182             :     krb5_storage *sp;
     183             :     emem_storage *s;
     184             : 
     185      327974 :     sp = malloc(sizeof(krb5_storage));
     186      327974 :     if (sp == NULL)
     187           0 :         return NULL;
     188             : 
     189      327974 :     s = malloc(sizeof(*s));
     190      327974 :     if (s == NULL) {
     191           0 :         free(sp);
     192           0 :         return NULL;
     193             :     }
     194      327974 :     sp->data = s;
     195      327974 :     sp->flags = 0;
     196      327974 :     sp->eof_code = HEIM_ERR_EOF;
     197      327974 :     s->size = 1024;
     198      327974 :     s->base = calloc(1, s->size);
     199      327974 :     if (s->base == NULL) {
     200           0 :         free(sp);
     201           0 :         free(s);
     202           0 :         return NULL;
     203             :     }
     204      327974 :     s->len = 0;
     205      327974 :     s->ptr = s->base;
     206      327974 :     sp->fetch = emem_fetch;
     207      327974 :     sp->store = emem_store;
     208      327974 :     sp->seek = emem_seek;
     209      327974 :     sp->trunc = emem_trunc;
     210      327974 :     sp->fsync = NULL;
     211      327974 :     sp->free = emem_free;
     212      327974 :     sp->max_alloc = UINT_MAX/8;
     213      327974 :     return sp;
     214             : }

Generated by: LCOV version 1.13