From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/12000 Path: news.gmane.org!.POSTED!not-for-mail From: William Pitcock Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH v5] stdio: implement fopencookie(3) Date: Wed, 11 Oct 2017 04:52:54 +0000 Message-ID: <20171011045254.15544-1-nenolod@dereferenced.org> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1507697829 5942 195.159.176.226 (11 Oct 2017 04:57:09 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 11 Oct 2017 04:57:09 +0000 (UTC) Cc: William Pitcock To: musl@lists.openwall.com Original-X-From: musl-return-12013-gllmg-musl=m.gmane.org@lists.openwall.com Wed Oct 11 06:57:00 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1e294k-0000AA-1U for gllmg-musl@m.gmane.org; Wed, 11 Oct 2017 06:56:58 +0200 Original-Received: (qmail 1734 invoked by uid 550); 11 Oct 2017 04:57:02 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 1692 invoked from network); 11 Oct 2017 04:57:01 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dereferenced-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=X4nOk00D9yopLcmYr8UVCNkDb2SzZ0amg7U8FURZX7s=; b=QWEf0Q7PwGSuiqJTzxqgvZwcp439xKIpqxTLf6U2NTTVnnyzpZ3TnADG2eUBRW8AMr oWJo/yng12hK+Uq5saU0HoAbG5+rC66Q6FhYFE0PocDN+tpESVgepcZ7Ts8inqN2cWIg LSdjao44QG4kVLABW6TYBEBMjfh8EZoL3mPqoTxDWkhs1KpQWxFnZ7mzmqe08QHrtyOD BPFG0Aj/rlsF0bStC5aPOTPP+3DZm7Q5CL92asAecWTWk6hP6YzwzPJ0GH6MEfqxaX2B M3G5ozbUpBiIu6lQvZzAPCjlG8aZfGmX+OzC+0+4XvP1lxR+TJS85WKeU058FaYDM29Y qrGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=X4nOk00D9yopLcmYr8UVCNkDb2SzZ0amg7U8FURZX7s=; b=uPIh7sgJ6v1R85PCplOt2kgSGGSKMU+TmmmfcWOpvGfECUBjWhh6TI5bHPYy0WCADo MjCRfkNldz8GB/k2lx44K0hCw55tRJSX59wDfUWhJn1h0xLBPBvylnuwXcgU20lR7L8x LdJvg5DJ4AofNKSyIyLWnM2waBGqgXNzDm0mY1VCav1eFbzHxGVnn6wCNlpJPVou0Faw obwKaSm2mRA9Z1p/WQ93yFgtiyXSflS2t2xao/698UuHh987V24KXeXKNMTHFrmjjHem V4nPepy4wISkobaPA4b75u5fY2aAU7fQFNp6fGbxFlDxU9I+Kfy5YsjAMOGOQFekq2vT 9Oaw== X-Gm-Message-State: AMCzsaWqauARE2tZQEsIAOmyeWJX6Si5OSJqHjVtMQNoX3F9063hm2LP C+IkoyVcpCLFsaWlB1Xf5s3+HSPq X-Google-Smtp-Source: AOwi7QDgdsULRVAkWVXBshB0Q/FFZOijDYDkdpoN7DLxZOE0O3v56QDk7fo/v44TqeEpL+TNviUV0w== X-Received: by 10.202.80.213 with SMTP id e204mr8164328oib.0.1507697809612; Tue, 10 Oct 2017 21:56:49 -0700 (PDT) X-Mailer: git-send-email 2.13.3 Xref: news.gmane.org gmane.linux.lib.musl.general:12000 Archived-At: The fopencookie(3) function allows the programmer to create a custom stdio implementation, using four hook functions which operate on a "cookie" data type. Changelog: v5: - implement stdio readahead buffering support v4: - remove parameter names from header function declarations v3: - remove spurious `struct winsize` - make f->lock unconditionally 0 v2: - properly implement stdio buffering v1: - initial proof of concept --- include/stdio.h | 9 ++++ src/stdio/fopencookie.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/stdio/fopencookie.c diff --git a/include/stdio.h b/include/stdio.h index 884d2e6a..da0563f6 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -182,6 +182,15 @@ int vasprintf(char **, const char *, __isoc_va_list); #ifdef _GNU_SOURCE char *fgets_unlocked(char *, int, FILE *); int fputs_unlocked(const char *, FILE *); + +typedef struct { + ssize_t (*read)(void *, char *, size_t); + ssize_t (*write)(void *, const char *, size_t); + int (*seek)(void *, off_t *, int); + int (*close)(void *); +} cookie_io_functions_t; + +FILE *fopencookie(void *, const char *, cookie_io_functions_t); #endif #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE) diff --git a/src/stdio/fopencookie.c b/src/stdio/fopencookie.c new file mode 100644 index 00000000..fa15be00 --- /dev/null +++ b/src/stdio/fopencookie.c @@ -0,0 +1,115 @@ +#define _GNU_SOURCE +#include "stdio_impl.h" +#include +#include +#include +#include +#include + +struct fcookie { + void *cookie; + cookie_io_functions_t iofuncs; +}; + +static size_t cookieread(FILE *f, unsigned char *buf, size_t len) +{ + struct fcookie *fc = f->cookie; + size_t ret, remain = len, readlen = 0; + + if (fc->iofuncs.read == NULL) return 0; + + ret = fc->iofuncs.read(fc->cookie, (char *) buf, len - 1); + if (ret == 0) goto bail_eof; + + readlen += ret; + remain -= ret; + + f->rpos = f->buf; + ret = fc->iofuncs.read(fc->cookie, (char *) f->rpos, f->buf_size); + if (ret == 0) goto bail_eof; + f->rend = f->rpos + ret; + + if (remain > f->buf_size) remain = f->buf_size; + memcpy(buf + readlen, f->rpos, remain); + readlen += remain; + f->rpos += remain; + + return readlen; + +bail_eof: + f->flags |= F_EOF; + f->rpos = f->rend = f->buf; + return readlen; +} + +static size_t cookiewrite(FILE *f, const unsigned char *buf, size_t len) +{ + struct fcookie *fc = f->cookie; + size_t ret; + size_t len2 = f->wpos - f->wbase; + if (fc->iofuncs.write == NULL) return 0; + if (len2) { + f->wpos = f->wbase; + if (cookiewrite(f, f->wpos, len2) < len2) return 0; + } + return fc->iofuncs.write(fc->cookie, (const char *) buf, len); +} + +static off_t cookieseek(FILE *f, off_t off, int whence) +{ + struct fcookie *fc = f->cookie; + if (fc->iofuncs.seek) return fc->iofuncs.seek(fc->cookie, &off, whence); + return -1; +} + +static int cookieclose(FILE *f) +{ + struct fcookie *fc = f->cookie; + if (fc->iofuncs.close) return fc->iofuncs.close(fc->cookie); + return 0; +} + +FILE *fopencookie(void *cookie, const char *mode, cookie_io_functions_t iofuncs) +{ + FILE *f; + struct fcookie *fc; + + /* Check for valid initial mode character */ + if (!strchr("rwa", *mode)) { + errno = EINVAL; + return 0; + } + + /* Allocate FILE+fcookie+buffer or fail */ + if (!(f=malloc(sizeof *f + sizeof *fc + UNGET + BUFSIZ))) return 0; + + /* Zero-fill only the struct, not the buffer */ + memset(f, 0, sizeof *f); + + /* Impose mode restrictions */ + if (!strchr(mode, '+')) f->flags = (*mode == 'r') ? F_NOWR : F_NORD; + + /* Set up our fcookie */ + fc = (void *)(f + 1); + fc->cookie = cookie; + fc->iofuncs.read = iofuncs.read; + fc->iofuncs.write = iofuncs.write; + fc->iofuncs.seek = iofuncs.seek; + fc->iofuncs.close = iofuncs.close; + + f->fd = -1; + f->cookie = fc; + f->buf = (unsigned char *)f + sizeof *f + sizeof *fc + UNGET; + f->buf_size = BUFSIZ; + f->lbf = EOF; + f->lock = 0; + + /* Initialize op ptrs. No problem if some are unneeded. */ + f->read = cookieread; + f->write = cookiewrite; + f->seek = cookieseek; + f->close = cookieclose; + + /* Add new FILE to open file list */ + return __ofl_add(f); +} -- 2.13.3