Blame libio/oldfmemopen.c

Packit 6c4009
/* Fmemopen implementation.
Packit 6c4009
   Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Hanno Mueller, kontakt@hanno.de, 2000.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * fmemopen() - "my" version of a string stream
Packit 6c4009
 * Hanno Mueller, kontakt@hanno.de
Packit 6c4009
 *
Packit 6c4009
 *
Packit 6c4009
 * I needed fmemopen() for an application that I currently work on,
Packit 6c4009
 * but couldn't find it in libio. The following snippet of code is an
Packit 6c4009
 * attempt to implement what glibc's documentation describes.
Packit 6c4009
 *
Packit 6c4009
 *
Packit 6c4009
 *
Packit 6c4009
 * I already see some potential problems:
Packit 6c4009
 *
Packit 6c4009
 * - I never used the "original" fmemopen(). I am sure that "my"
Packit 6c4009
 *   fmemopen() behaves differently than the original version.
Packit 6c4009
 *
Packit 6c4009
 * - The documentation doesn't say wether a string stream allows
Packit 6c4009
 *   seeks. I checked the old fmemopen implementation in glibc's stdio
Packit 6c4009
 *   directory, wasn't quite able to see what is going on in that
Packit 6c4009
 *   source, but as far as I understand there was no seek there. For
Packit 6c4009
 *   my application, I needed fseek() and ftell(), so it's here.
Packit 6c4009
 *
Packit 6c4009
 * - "append" mode and fseek(p, SEEK_END) have two different ideas
Packit 6c4009
 *   about the "end" of the stream.
Packit 6c4009
 *
Packit 6c4009
 *   As described in the documentation, when opening the file in
Packit 6c4009
 *   "append" mode, the position pointer will be set to the first null
Packit 6c4009
 *   character of the string buffer (yet the buffer may already
Packit 6c4009
 *   contain more data). For fseek(), the last byte of the buffer is
Packit 6c4009
 *   used as the end of the stream.
Packit 6c4009
 *
Packit 6c4009
 * - It is unclear to me what the documentation tries to say when it
Packit 6c4009
 *   explains what happens when you use fmemopen with a NULL
Packit 6c4009
 *   buffer.
Packit 6c4009
 *
Packit 6c4009
 *   Quote: "fmemopen [then] allocates an array SIZE bytes long. This
Packit 6c4009
 *   is really only useful if you are going to write things to the
Packit 6c4009
 *   buffer and then read them back in again."
Packit 6c4009
 *
Packit 6c4009
 *   What does that mean if the original fmemopen() did not allow
Packit 6c4009
 *   seeking? How do you read what you just wrote without seeking back
Packit 6c4009
 *   to the beginning of the stream?
Packit 6c4009
 *
Packit 6c4009
 * - I think there should be a second version of fmemopen() that does
Packit 6c4009
 *   not add null characters for each write. (At least in my
Packit 6c4009
 *   application, I am not actually using strings but binary data and
Packit 6c4009
 *   so I don't need the stream to add null characters on its own.)
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include "libioP.h"
Packit 6c4009
Packit 6c4009
#if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_22)
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
typedef struct fmemopen_cookie_struct fmemopen_cookie_t;
Packit 6c4009
struct fmemopen_cookie_struct
Packit 6c4009
{
Packit 6c4009
  char *buffer;
Packit 6c4009
  int mybuffer;
Packit 6c4009
  int binmode;
Packit 6c4009
  size_t size;
Packit 6c4009
  off64_t pos;
Packit 6c4009
  size_t maxpos;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
static ssize_t
Packit 6c4009
fmemopen_read (void *cookie, char *b, size_t s)
Packit 6c4009
{
Packit 6c4009
  fmemopen_cookie_t *c;
Packit 6c4009
Packit 6c4009
  c = (fmemopen_cookie_t *) cookie;
Packit 6c4009
Packit 6c4009
  if (c->pos + s > c->size)
Packit 6c4009
    {
Packit 6c4009
      if ((size_t) c->pos == c->size)
Packit 6c4009
	return 0;
Packit 6c4009
      s = c->size - c->pos;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  memcpy (b, &(c->buffer[c->pos]), s);
Packit 6c4009
Packit 6c4009
  c->pos += s;
Packit 6c4009
  if ((size_t) c->pos > c->maxpos)
Packit 6c4009
    c->maxpos = c->pos;
Packit 6c4009
Packit 6c4009
  return s;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static ssize_t
Packit 6c4009
fmemopen_write (void *cookie, const char *b, size_t s)
Packit 6c4009
{
Packit 6c4009
  fmemopen_cookie_t *c;
Packit 6c4009
  int addnullc;
Packit 6c4009
Packit 6c4009
  c = (fmemopen_cookie_t *) cookie;
Packit 6c4009
Packit 6c4009
  addnullc = c->binmode == 0 && (s == 0 || b[s - 1] != '\0');
Packit 6c4009
Packit 6c4009
  if (c->pos + s + addnullc > c->size)
Packit 6c4009
    {
Packit 6c4009
      if ((size_t) (c->pos + addnullc) >= c->size)
Packit 6c4009
	{
Packit 6c4009
	  __set_errno (ENOSPC);
Packit 6c4009
	  return 0;
Packit 6c4009
	}
Packit 6c4009
      s = c->size - c->pos - addnullc;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  memcpy (&(c->buffer[c->pos]), b, s);
Packit 6c4009
Packit 6c4009
  c->pos += s;
Packit 6c4009
  if ((size_t) c->pos > c->maxpos)
Packit 6c4009
    {
Packit 6c4009
      c->maxpos = c->pos;
Packit 6c4009
      if (addnullc)
Packit 6c4009
	c->buffer[c->maxpos] = '\0';
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return s;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
fmemopen_seek (void *cookie, off64_t *p, int w)
Packit 6c4009
{
Packit 6c4009
  off64_t np;
Packit 6c4009
  fmemopen_cookie_t *c;
Packit 6c4009
Packit 6c4009
  c = (fmemopen_cookie_t *) cookie;
Packit 6c4009
Packit 6c4009
  switch (w)
Packit 6c4009
    {
Packit 6c4009
    case SEEK_SET:
Packit 6c4009
      np = *p;
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    case SEEK_CUR:
Packit 6c4009
      np = c->pos + *p;
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    case SEEK_END:
Packit 6c4009
      np = (c->binmode ? c->size : c->maxpos) - *p;
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
    default:
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (np < 0 || (size_t) np > c->size)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  *p = c->pos = np;
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
fmemopen_close (void *cookie)
Packit 6c4009
{
Packit 6c4009
  fmemopen_cookie_t *c;
Packit 6c4009
Packit 6c4009
  c = (fmemopen_cookie_t *) cookie;
Packit 6c4009
Packit 6c4009
  if (c->mybuffer)
Packit 6c4009
    free (c->buffer);
Packit 6c4009
  free (c);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
FILE *
Packit 6c4009
__old_fmemopen (void *buf, size_t len, const char *mode)
Packit 6c4009
{
Packit 6c4009
  cookie_io_functions_t iof;
Packit 6c4009
  fmemopen_cookie_t *c;
Packit 6c4009
  FILE *result;
Packit 6c4009
Packit 6c4009
  if (__glibc_unlikely (len == 0))
Packit 6c4009
    {
Packit 6c4009
    einval:
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t));
Packit 6c4009
  if (c == NULL)
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  c->mybuffer = (buf == NULL);
Packit 6c4009
Packit 6c4009
  if (c->mybuffer)
Packit 6c4009
    {
Packit 6c4009
      c->buffer = (char *) malloc (len);
Packit 6c4009
      if (c->buffer == NULL)
Packit 6c4009
	{
Packit 6c4009
	  free (c);
Packit 6c4009
	  return NULL;
Packit 6c4009
	}
Packit 6c4009
      c->buffer[0] = '\0';
Packit 6c4009
      c->maxpos = 0;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (__glibc_unlikely ((uintptr_t) len > -(uintptr_t) buf))
Packit 6c4009
	{
Packit 6c4009
	  free (c);
Packit 6c4009
	  goto einval;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      c->buffer = buf;
Packit 6c4009
Packit 6c4009
      if (mode[0] == 'w')
Packit 6c4009
	c->buffer[0] = '\0';
Packit 6c4009
Packit 6c4009
      c->maxpos = strnlen (c->buffer, len);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  c->size = len;
Packit 6c4009
Packit 6c4009
  if (mode[0] == 'a')
Packit 6c4009
    c->pos = c->maxpos;
Packit 6c4009
  else
Packit 6c4009
    c->pos = 0;
Packit 6c4009
Packit 6c4009
  c->binmode = mode[0] != '\0' && mode[1] == 'b';
Packit 6c4009
Packit 6c4009
  iof.read = fmemopen_read;
Packit 6c4009
  iof.write = fmemopen_write;
Packit 6c4009
  iof.seek = fmemopen_seek;
Packit 6c4009
  iof.close = fmemopen_close;
Packit 6c4009
Packit 6c4009
  result = _IO_fopencookie (c, mode, iof);
Packit 6c4009
  if (__glibc_unlikely (result == NULL))
Packit 6c4009
    {
Packit 6c4009
      if (c->mybuffer)
Packit 6c4009
	free (c->buffer);
Packit 6c4009
Packit 6c4009
      free (c);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
compat_symbol (libc, __old_fmemopen, fmemopen, GLIBC_2_2);
Packit 6c4009
#endif