sfdcseekable.c revision 3e14f97f673e8a630f076077de35afdd43dc1587
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/***********************************************************************
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* This software is part of the ast package *
3e14f97f673e8a630f076077de35afdd43dc1587Roger A. Faulkner* Copyright (c) 1985-2010 AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* and is licensed under the *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Common Public License, Version 1.0 *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* by AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* A copy of the License is available at *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* http://www.opensource.org/licenses/cpl1.0.txt *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Information and Software Systems Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* AT&T Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Florham Park NJ *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Glenn Fowler <gsf@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* David Korn <dgk@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Phong Vo <kpv@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin***********************************************************************/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#include "sfdchdr.h"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* Discipline to make an unseekable read stream seekable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin**
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** sfraise(f,SFSK_DISCARD,0) discards previous seek data
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** but seeks from current offset on still allowed
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin**
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** Written by Kiem-Phong Vo, kpv@research.att.com, 03/18/1998.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin*/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintypedef struct _skable_s
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{ Sfdisc_t disc; /* sfio discipline */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Sfio_t* shadow; /* to shadow data */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Sfoff_t discard;/* sfseek(f,-1,SEEK_SET) discarded data */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Sfoff_t extent; /* shadow extent */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin int eof; /* if eof has been reached */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin} Seek_t;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if __STD_C
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic ssize_t skwrite(Sfio_t* f, const Void_t* buf, size_t n, Sfdisc_t* disc)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic ssize_t skwrite(f, buf, n, disc)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfio_t* f; /* stream involved */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinVoid_t* buf; /* buffer to read into */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsize_t n; /* number of bytes to read */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfdisc_t* disc; /* discipline */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (ssize_t)(-1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if __STD_C
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic ssize_t skread(Sfio_t* f, Void_t* buf, size_t n, Sfdisc_t* disc)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic ssize_t skread(f, buf, n, disc)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfio_t* f; /* stream involved */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinVoid_t* buf; /* buffer to read into */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsize_t n; /* number of bytes to read */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfdisc_t* disc; /* discipline */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Seek_t* sk;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Sfio_t* sf;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Sfoff_t addr;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin ssize_t r, w, p;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk = (Seek_t*)disc;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sf = sk->shadow;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(sk->eof)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return sfread(sf,buf,n);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin addr = sfseek(sf,(Sfoff_t)0,SEEK_CUR);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(addr+n <= sk->extent)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return sfread(sf,buf,n);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((r = (ssize_t)(sk->extent-addr)) > 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if((w = sfread(sf,buf,r)) != r)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return w;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin buf = (char*)buf + r;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin n -= r;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* do a raw read */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((w = sfrd(f,buf,n,disc)) <= 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { sk->eof = 1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin w = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((p = sfwrite(sf,buf,w)) != w)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk->eof = 1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(p > 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk->extent += p;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return r+w;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if __STD_C
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic Sfoff_t skseek(Sfio_t* f, Sfoff_t addr, int type, Sfdisc_t* disc)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic Sfoff_t skseek(f, addr, type, disc)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfio_t* f;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfoff_t addr;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint type;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfdisc_t* disc;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Seek_t* sk;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Sfio_t* sf;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin char buf[SF_BUFSIZE];
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin ssize_t r, w;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk = (Seek_t*)disc;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sf = sk->shadow;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin switch (type)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case SEEK_SET:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin addr -= sk->discard;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case SEEK_CUR:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin addr += sftell(sf);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case SEEK_END:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin addr += sk->extent;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin default:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(addr < 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (Sfoff_t)(-1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(addr > sk->extent)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(sk->eof)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (Sfoff_t)(-1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* read enough to reach the seek point */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin while(addr > sk->extent)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(addr > sk->extent+sizeof(buf) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin w = sizeof(buf);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else w = (int)(addr-sk->extent);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((r = sfrd(f,buf,w,disc)) <= 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin w = r-1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if((w = sfwrite(sf,buf,r)) > 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk->extent += w;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(w != r)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { sk->eof = 1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(addr > sk->extent)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return (Sfoff_t)(-1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return sfseek(sf,addr,SEEK_SET) + sk->discard;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* on close, remove the discipline */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if __STD_C
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int skexcept(Sfio_t* f, int type, Void_t* data, Sfdisc_t* disc)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int skexcept(f,type,data,disc)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfio_t* f;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint type;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinVoid_t* data;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfdisc_t* disc;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin Seek_t* sk;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk = (Seek_t*)disc;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin switch (type)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case SF_FINAL:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case SF_DPOP:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sfclose(sk->shadow);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin free(disc);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case SFSK_DISCARD:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk->eof = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk->discard += sk->extent;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk->extent = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sfseek(sk->shadow,(Sfoff_t)0,SEEK_SET);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if __STD_C
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint sfdcseekable(Sfio_t* f)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinint sfdcseekable(f)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSfio_t* f;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin reg Seek_t* sk;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* see if already seekable */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(sfseek(f,(Sfoff_t)0,SEEK_CUR) >= 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(!(sk = (Seek_t*)malloc(sizeof(Seek_t))) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin memset(sk, 0, sizeof(*sk));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk->disc.readf = skread;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk->disc.writef = skwrite;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk->disc.seekf = skseek;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk->disc.exceptf = skexcept;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk->shadow = sftmp(SF_BUFSIZE);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk->discard = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk->extent = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin sk->eof = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(sfdisc(f, (Sfdisc_t*)sk) != (Sfdisc_t*)sk)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { sfclose(sk->shadow);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin free(sk);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}