Home | History | Annotate | Download | only in stdio
      1 /*-
      2  * Copyright (C) 2013 Pietro Cerutti <gahr (at) FreeBSD.org>
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     16  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
     17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     23  * SUCH DAMAGE.
     24  */
     25 
     26 #include <sys/cdefs.h>
     27 
     28 #include <errno.h>
     29 #include <fcntl.h>
     30 #include <stdio.h>
     31 #include <stdlib.h>
     32 #include <string.h>
     33 
     34 #include "local.h"
     35 
     36 // See http://pubs.opengroup.org/onlinepubs/9699919799/functions/fmemopen.html
     37 // and http://man7.org/linux/man-pages/man3/fmemopen.3.html for documentation.
     38 
     39 struct fmemopen_cookie {
     40   char* buf;
     41   char* allocation;
     42   size_t capacity;
     43   size_t size;
     44   size_t offset;
     45   bool append;
     46 };
     47 
     48 static int fmemopen_read(void* cookie, char* buf, int n) {
     49   fmemopen_cookie* ck = static_cast<fmemopen_cookie*>(cookie);
     50 
     51   if (static_cast<size_t>(n) > ck->size - ck->offset) n = ck->size - ck->offset;
     52 
     53   if (n > 0) {
     54     memmove(buf, ck->buf + ck->offset, n);
     55     ck->offset += n;
     56   }
     57   return n;
     58 }
     59 
     60 static int fmemopen_write(void* cookie, const char* buf, int n) {
     61   fmemopen_cookie* ck = static_cast<fmemopen_cookie*>(cookie);
     62 
     63   // We don't need to add the trailing NUL if there's already a trailing NUL
     64   // in the data we're writing.
     65   size_t space_for_null = (n > 0 && buf[n - 1] != '\0') ? 1 : 0;
     66 
     67   // Undo any seeking/reading on an append-only stream.
     68   if (ck->append) ck->offset = ck->size;
     69 
     70   // How much can we actually fit?
     71   if (static_cast<size_t>(n) + space_for_null > ck->capacity - ck->offset) {
     72     n = ck->capacity - ck->offset - space_for_null;
     73     // Give up if we don't even have room for one byte of userdata.
     74     if (n <= 0) {
     75       errno = ENOSPC;
     76       return -1;
     77     }
     78   }
     79 
     80   if (n > 0) {
     81     memmove(ck->buf + ck->offset, buf, n);
     82     ck->offset += n;
     83     // Is this the furthest we've ever been?
     84     if (ck->offset >= ck->size) {
     85       if (buf[n - 1] != '\0') ck->buf[ck->offset] = '\0';
     86       ck->size = ck->offset;
     87     }
     88   }
     89   return n;
     90 }
     91 
     92 static fpos_t fmemopen_seek(void* cookie, fpos_t offset, int whence) {
     93   fmemopen_cookie* ck = static_cast<fmemopen_cookie*>(cookie);
     94 
     95   if (whence == SEEK_SET && (offset >= 0 && static_cast<size_t>(offset) <= ck->capacity)) {
     96     return (ck->offset = offset);
     97   } else if (whence == SEEK_CUR && (ck->offset + offset <= ck->capacity)) {
     98     return (ck->offset += offset);
     99   } else if (whence == SEEK_END && (offset <= 0 && static_cast<size_t>(-offset) <= ck->size)) {
    100     return (ck->offset = ck->size + offset);
    101   }
    102   errno = EINVAL;
    103   return -1;
    104 }
    105 
    106 static int fmemopen_close(void* cookie) {
    107   fmemopen_cookie* ck = static_cast<fmemopen_cookie*>(cookie);
    108   free(ck->allocation);
    109   free(ck);
    110   return 0;
    111 }
    112 
    113 FILE* fmemopen(void* buf, size_t capacity, const char* mode) {
    114   int flags;
    115   if (__sflags(mode, &flags) == 0) {
    116     errno = EINVAL;
    117     return nullptr;
    118   }
    119 
    120   fmemopen_cookie* ck = static_cast<fmemopen_cookie*>(calloc(sizeof(fmemopen_cookie), 1));
    121   if (ck == nullptr) return nullptr;
    122 
    123   ck->buf = static_cast<char*>(buf);
    124   ck->capacity = capacity;
    125 
    126   if (ck->buf == nullptr) ck->buf = ck->allocation = static_cast<char*>(calloc(capacity, 1));
    127   if (ck->buf == nullptr) {
    128     free(ck);
    129     return nullptr;
    130   }
    131 
    132   FILE* fp = funopen(ck,
    133                      (flags & O_WRONLY) ? nullptr : fmemopen_read,
    134                      (flags & O_RDONLY) ? nullptr : fmemopen_write,
    135                      fmemopen_seek,
    136                      fmemopen_close);
    137   if (fp == nullptr) {
    138     fmemopen_close(ck);
    139     return nullptr;
    140   }
    141 
    142   if (mode[0] == 'a') {
    143     ck->size = strnlen(ck->buf, ck->capacity);
    144     ck->offset = ck->size;
    145     ck->append = true;
    146   } else if (mode[0] == 'r') {
    147     ck->size = capacity;
    148     ck->offset = 0;
    149   } else if (mode[0] == 'w') {
    150     ck->size = 0;
    151     ck->offset = 0;
    152     ck->buf[0] = '\0';
    153   }
    154 
    155   return fp;
    156 }
    157