Ejemplo n.º 1
0
MR_UInt8 *BMPRead(FILE * pFile, int &pXRes, int &pYRes)
{
	MR_Int32 lDataPos;
	MR_UInt8 *lReturnValue = NULL;

	// Read Header
	fseek(pFile, 10, SEEK_SET);
	sfread(&lDataPos, 4, 1, pFile);

	fseek(pFile, 18, SEEK_SET);
	sfread(&pXRes, 4, 1, pFile);
	sfread(&pYRes, 4, 1, pFile);

	fseek(pFile, lDataPos, SEEK_SET);

	ASSERT(pXRes > 0);
	ASSERT(pYRes > 0);

	size_t sz = static_cast<size_t>(pXRes * pYRes);
	lReturnValue = new MR_UInt8[sz];

	sfread(lReturnValue, sz, 1, pFile);

	return lReturnValue;

}
Ejemplo n.º 2
0
Archivo: history.c Proyecto: att/ast
//
// Position history file at size and find next command number.
//
static int hist_nearend(History_t *hp, Sfio_t *iop, off_t size) {
    unsigned char *cp, *endbuff, *buff, marker[4];
    int n;
    int incmd = 1;

    if (size <= 2) goto begin;
    if (sfseek(iop, size, SEEK_SET) < 0) goto begin;

    // Skip to marker command and return the number. Numbering commands occur after a null and begin
    // with HIST_CMDNO.
    while (true) {
        cp = buff = (unsigned char *)sfreserve(iop, SF_UNBOUND, SF_LOCKR | SF_WRITE);
        if (!cp) break;

        n = sfvalue(iop);
        endbuff = cp + n;
        while (true) {
            // Check for marker.
            if (!incmd && *cp++ == HIST_CMDNO && *cp == 0) {
                n = cp + 1 - buff;
                incmd = -1;
                break;
            }
            incmd = 0;
            cp += strnlen((char *)cp, endbuff - cp) + 1;  // point past the terminating null
            if (cp > endbuff) {
                incmd = 1;
                break;
            }
            if (*cp == 0 && ++cp > endbuff) {
                break;
            }
        }
        size += n;
        sfread(iop, (char *)buff, n);
        if (incmd < 0) {
            if ((n = sfread(iop, (char *)marker, 4)) == 4) {
                n = (marker[0] << 16) | (marker[1] << 8) | marker[2];
                if (n < size / 2) {
                    hp->histmarker = hp->histcnt = size + 4;
                    return n;
                }
                n = 4;
            }
            if (n > 0) size += n;
            incmd = 0;
        }
    }

begin:
    sfseek(iop, 2, SEEK_SET);
    hp->histmarker = hp->histcnt = 2;
    return 1;
}
Ejemplo n.º 3
0
static int
verify(char* path, char* old, char* processor, int must)
{
	char*	ns;
	char*	os;
	int	nz;
	int	oz;
	int	r;
	Sfio_t*	nf;
	Sfio_t*	of;

	r = 0;
	if (nf = sfopen(NiL, path, "r"))
	{
		if ((ns = sfgetr(nf, '\n', 1)) && (nz = sfvalue(nf) - 1) > 0)
		{
			ns += nz;
			if ((oz = strlen(processor)) <= nz && !strcmp(processor, ns - oz))
				r = 1;
			else
				error(2, "%s: %s clashes with %s", path, processor, ns - nz);
		}
		if (r && old && sfseek(nf, 0L, 0) == 0 && (of = sfopen(NiL, old, "r")))
		{
			for (;;)
			{
				ns = sfreserve(nf, 0, 0);
				nz = sfvalue(nf);
				os = sfreserve(of, 0, 0);
				oz = sfvalue(nf);
				if (nz <= 0 || oz <= 0)
					break;
				if (nz > oz)
					nz = oz;
				if (memcmp(ns, os, nz))
					break;
				nz = sfread(nf, ns, nz);
				oz = sfread(of, os, nz);
				if (!nz || !oz)
					break;
			}
			sfclose(of);
			if (!nz && !oz && !touch(old, (time_t)-1, (time_t)-1, 0))
				r = 0;
		}
		sfclose(nf);
	}
	else if (must)
		error(ERROR_SYSTEM|2, "%s: cannot read", path);
	return r;
}
Ejemplo n.º 4
0
main()
{
	Sfio_t*	null;
	Sfio_t*	f;
	char	buf[256*1024], b[256*1024];
	int	k, n;

	if(!(null = sfopen(NIL(Sfio_t*),"/dev/null","w")) )
		terror("Opening /dev/null");

	sfsetbuf(null,NIL(char*),(size_t)SF_UNBOUND);

	if(!SFISNULL(null) )
		terror("Not /dev/null?");

	if(!(f = sfopen(NIL(Sfio_t*), Kpv[0], "w+")) )
		terror("Creating %s", Kpv[0]);
	sfwrite(f,"1234",4);
	sfseek(f,(Sfoff_t)1,0);
	sfsync(f);

	sfsetfd(null,-1);
	sfsetfd(null,sffileno(f));
	sfsync(null);

	sfseek(f,(Sfoff_t)0,0);
	if(sfread(f,buf,4) != 4 || strncmp(buf,"1234",4) != 0)
		terror("Bad data");

	for(k = 0; k < sizeof(buf); ++k)
		buf[k] = 1;
	for(k = sizeof(buf)/4; k < sizeof(buf)/2; ++k) /* make a big hole */
		buf[k] = 0;

	if(!(f = sfopen(f, Kpv[0], "w+")) )
		terror("Creating %s", Kpv[0]);
	n = sizeof(buf)-127;
	if(sfwrite(f,buf,n) != n)
		terror("Writing large buffer");
	sfseek(f,(Sfoff_t)0,0);
	if(sfread(f,b,n) != n)
		terror("Reading large buffer");
	for(k = 0; k < n; ++k)
		if(b[k] != buf[k])
			terror("Bad data");

	rmkpv();
	return 0;
}
Ejemplo n.º 5
0
/*
 * With _Fcin.fcptr>_Fcin.fcbuff, the stream pointer is advanced and
 * If _Fcin.fclast!=0, performs an sfreserve() for the next buffer.
 * If a notify function has been set, it is called
 * If last is non-zero, and the stream is a file, 0 is returned when
 * the previous character is a 0 byte.
 */
int	fcfill(void)
{
	register int		n;
	register Sfio_t	*f;
	register unsigned char	*last=_Fcin.fclast, *ptr=_Fcin.fcptr;
	if(!(f=fcfile()))
	{
		/* see whether pointer has passed null byte */
		if(ptr>_Fcin.fcbuff && *--ptr==0)
			_Fcin.fcptr=ptr;
		else
			_Fcin.fcoff = 0;
		return(0);
	}
	if(last)
	{
		if( ptr<last && ptr>_Fcin.fcbuff && *(ptr-1)==0)
			return(0);
		if(_Fcin.fcchar)
			*last = _Fcin.fcchar;
		if(ptr > last)
			_Fcin.fcptr = ptr = last;
	}
	if((n = ptr-_Fcin.fcbuff) && _Fcin.fcfun)
		(*_Fcin.fcfun)(f,(const char*)_Fcin.fcbuff,n,_Fcin.context);
	sfread(f, (char*)_Fcin.fcbuff, n);
	_Fcin.fcoff +=n;
	_Fcin._fcfile = 0;
	if(!last)
		return(0);
	else if(fcfopen(f) < 0)
		return(EOF);
	return(*_Fcin.fcptr++);
}
Ejemplo n.º 6
0
tmain()
{
	int	i;
	char	wbuf[1023];
	char	rbuf[1023];
	Sfio_t	*fp;

	for(i = 0; i < sizeof(wbuf); ++i)
		wbuf[i] = (i%26)+'a';
	wbuf[sizeof(wbuf)-1] = '\0';

	if(!(fp = sftmp(0)))
		terror("Opening temp file");

	for(i = 0; i < 256; ++i)
		if(sfwrite(fp,wbuf,sizeof(wbuf)) != sizeof(wbuf))
			terror("Writing");

	sfseek(fp,(Sfoff_t)0,0);
	sfset(fp,SF_WRITE,0);
	sfsetbuf(fp,NIL(char*),0);
	sfsetbuf(fp,NIL(char*),(size_t)SF_UNBOUND);

	for(i = 0; i < 256; ++i)
	{	if(sfread(fp,rbuf,sizeof(rbuf)) != sizeof(rbuf))
			terror("Reading");

		if(strcmp(rbuf,wbuf) != 0)
			terror("Unmatched record");
	}

	texit(0);
}
Ejemplo n.º 7
0
MAIN()
{
	Sfio_t*	f;
	char	buf[1024], buf2[1024], *data;
	int	n, r;

	/* test to see if malloc() winds up calling mmap() */
	if(!(data = (char*)malloc(8*1024)) )
		terror("Malloc failed\n");
	free(data);
	Success = 0;

	/* our real work */
	if(!(f = sfopen(NIL(Sfio_t*), tstfile(0),"w")) )
		terror("Can't open to write\n");

	for(n = 0; n < sizeof(buf); ++n)
		buf[n] = '0' + (n%10);

	for(n = 0; n < 10; ++n)
		sfwrite(f,buf,sizeof(buf));

	if(!(f = sfopen(f, tstfile(0),"r")) )
		terror("Can't open to read\n");

	for(n = 0; n < 10; ++n)
	{	if((r = sfread(f,buf2,sizeof(buf))) != sizeof(buf))
			terror("Bad read size=%d\n",r);
		if(strncmp(buf,buf2,sizeof(buf)) != 0)
			terror("Get wrong data\n");
	}

	TSTEXIT(0);
}
Ejemplo n.º 8
0
Archivo: fcin.c Proyecto: att/ast
//
// Open stream <f> for fast character input.
//
int fcfopen(Sfio_t *f) {
    int n;
    char *buff;
    Fcin_t save;

    errno = 0;
    _Fcin.fcbuff = _Fcin.fcptr;
    _Fcin._fcfile = f;
    fcsave(&save);
    if (!(buff = (char *)sfreserve(f, SF_UNBOUND, SF_LOCKR))) {
        fcrestore(&save);
        _Fcin.fcchar = 0;
        _Fcin.fcptr = _Fcin.fcbuff = &_Fcin.fcchar;
        _Fcin.fclast = NULL;
        _Fcin._fcfile = NULL;
        return EOF;
    }
    n = sfvalue(f);
    fcrestore(&save);
    sfread(f, buff, 0);
    _Fcin.fcoff = sftell(f);
    buff = (char *)sfreserve(f, SF_UNBOUND, SF_LOCKR);
    _Fcin.fclast = (_Fcin.fcptr = _Fcin.fcbuff = (unsigned char *)buff) + n;
    if (sffileno(f) >= 0) *_Fcin.fclast = 0;
    return n;
}
Ejemplo n.º 9
0
Archivo: fcin.c Proyecto: att/ast
//
// With _Fcin.fcptr>_Fcin.fcbuff, the stream pointer is advanced.
// If _Fcin.fclast!=0, performs an sfreserve() for the next buffer.
// If a notify function has been set, it is called.
// If last is non-zero, and the stream is a file, 0 is returned when
// the previous character is a 0 byte.
//
int fcfill(void) {
    int n;
    Sfio_t *f;
    unsigned char *last = _Fcin.fclast, *ptr = _Fcin.fcptr;
    f = fcfile();
    if (!f) {
        // See whether pointer has passed null byte.
        if (ptr > _Fcin.fcbuff && *--ptr == 0) {
            _Fcin.fcptr = ptr;
        } else {
            _Fcin.fcoff = 0;
        }
        return 0;
    }
    if (last) {
        if (ptr < last && ptr > _Fcin.fcbuff && *(ptr - 1) == 0) return 0;
        if (_Fcin.fcchar) *last = _Fcin.fcchar;
        if (ptr > last) _Fcin.fcptr = ptr = last;
    }
    n = ptr - _Fcin.fcbuff;
    if (n && _Fcin.fcfun) (*_Fcin.fcfun)(f, (const char *)_Fcin.fcbuff, n, _Fcin.context);
    sfread(f, (char *)_Fcin.fcbuff, n);
    _Fcin.fcoff += n;
    _Fcin._fcfile = NULL;
    if (!last) {
        return 0;
    } else if (fcfopen(f) < 0) {
        return EOF;
    }
    return *_Fcin.fcptr++;
}
Ejemplo n.º 10
0
MAIN()
{
	Sfio_t*	f;
	char	buf[1024], *s;
	int	n;
#ifdef DEBUG
	Sfio_t*	logf = sfopen(0,"LOG","a"); sfsetbuf(logf,NIL(Void_t*),0);
#endif

	alarm(10);
	if(argc > 1)
	{	/* coprocess only */
		while((s = sfreserve(sfstdin,-1,0)) )
		{
#ifdef DEBUG
			sfwrite(logf, s, sfvalue(sfstdin));
#endif
			sfwrite(sfstdout, s, sfvalue(sfstdin));
		}
		return 0;
	}

	/* make coprocess */
	if(!(f = sfpopen(NIL(Sfio_t*), sfprints("%s -p",argv[0]), "r+")))
		terror("Opening for read/write\n");
	for(n = 0; n < 10; ++n)
	{	sfsprintf(buf,sizeof(buf),"Line %d",n);
		sfputr(f,buf,'\n');
		if(!(s = sfgetr(f,'\n',1)))
			terror("Did not read back line\n");
		if(strcmp(s,buf) != 0)
			terror("Input=%s, Expect=%s\n",s,buf);
	}

	if(sfputr(f,"123456789",'\n') != 10)
		terror("Bad write");

	if(sfread(f,buf,3) != 3)
		terror("Did not get data back\n");
	if(strncmp(s,"123",3) != 0)
		terror("Wrong data\n");

	if(sfwrite(f,"aaa",3) != 3 || sfputc(f,'\n') != '\n')
		terror("Fail on write\n");

	if(!(s = sfgetr(f,'\n',1)) )
		terror("Should have gotten 456789\n"); 
	if(strcmp(s,"456789") != 0)
		terror("Wrong data2\n");

	if(!(s = sfgetr(f,'\n',1)) )
		terror("Should have gotten aaa\n"); 
	if(strcmp(s,"aaa") != 0)
		terror("Wrong data3\n");

	sfclose(f);
	
	TSTEXIT(0);
}
Ejemplo n.º 11
0
size_t
fread(void* p, size_t s, size_t n, Sfio_t* f)
{
	ssize_t	v;

	STDIO_INT(f, "fread", ssize_t, (void*, size_t, size_t, Sfio_t*), (p, s, n, f))

	return ((v = sfread(f, p, s * n)) <= 0) ? 0 : (v / s);
}
Ejemplo n.º 12
0
static int init (int count) {
    int n;

    if ((n = sfread (sfstdin, buffer, count)) < 1)
        error (3, "cannot read initial buffer");
    start = &buffer[0];
    end = start + n;
    return 0;
}
Ejemplo n.º 13
0
wint_t
fgetwc(Sfio_t* f)
{
	wchar_t	c;

	STDIO_INT(f, "fgetwc", wint_t, (Sfio_t*), (f))

	FWIDE(f, WEOF);
	return (sfread(f, &c, sizeof(c)) == sizeof(c)) ? c : WEOF;
}
Ejemplo n.º 14
0
static struct argnod *r_arg(Shell_t *shp)
{
	register struct argnod *ap=0, *apold, *aptop=0;
	register long l;
	Stk_t		*stkp=shp->stk;
	while((l=sfgetu(infile))>0)
	{
		ap = (struct argnod*)stkseek(stkp,(unsigned)l+ARGVAL);
		if(!aptop)
			aptop = ap;
		else
			apold->argnxt.ap = ap;
		if(--l > 0)
		{
			sfread(infile,ap->argval,(size_t)l);
			ccmaps(ap->argval, l, CC_ASCII, CC_NATIVE);
		}
		ap->argval[l] = 0;
		ap->argchn.cp = 0;
		ap->argflag = sfgetc(infile);
#if 0
		if((ap->argflag&ARG_MESSAGE) && *ap->argval)
		{
			/* replace international messages */
			sh_endword(shp,1);
			ap->argflag &= ~ARG_MESSAGE;
			if(!(ap->argflag&(ARG_MAC|ARG_EXP)))
				ap = sh_endword(shp,0);
			else
			{
				ap = (struct argnod*)stkfreeze(stkp,0);
				if(ap->argflag==0)
					ap->argflag = ARG_RAW;
			}
		}
		else
#endif
			ap = (struct argnod*)stkfreeze(stkp,0);
		if(*ap->argval==0 && (ap->argflag&ARG_EXP))
			ap->argchn.ap = (struct argnod*)r_tree(shp);
		else if(*ap->argval==0 && (ap->argflag&~(ARG_APPEND|ARG_MESSAGE|ARG_QUOTED|ARG_ARRAY))==0)
		{
			struct fornod *fp = (struct fornod*)getnode(shp->stk,fornod);
			fp->fortyp = sfgetu(infile);
			fp->fortre = r_tree(shp);
			fp->fornam = ap->argval+1;
			ap->argchn.ap = (struct argnod*)fp;
		}
		apold = ap;
	}
	if(ap)
		ap->argnxt.ap = 0;
	return(aptop);
}
Ejemplo n.º 15
0
MR_UInt8 *TrackCompiler::LoadPalette(FILE * pFile)
{
	MR_UInt8 *lReturnValue = NULL;

	fseek(pFile, -769, SEEK_END);

	char lMagicNumber = 0;

	sfread(&lMagicNumber, 1, 1, pFile);

	if(lMagicNumber != 12) {
		throw TrackCompileExn(_("Bad palette format for background file"));
	}
	else {
		lReturnValue = new MR_UInt8[MR_BACK_COLORS * 3];
		sfread(lReturnValue, 1, MR_BACK_COLORS * 3, pFile);
	}

	return lReturnValue;
}
Ejemplo n.º 16
0
bool dbf_uncompress_init(search_context_t* sc, FILE* f) {
	compressed_node_t header;

	EW(sfread(&header, sizeof(compressed_node_t), f));
	EW(sfread(sc->buf, SC_BUF_SIZE, f));
	sc->bytes_total        = header.bytes_total;
	if (header.bytes_total <= 4) {
		sc->mask.s.record_number = header.record_number_mask;
		sc->mask.s.dup_count     = header.dup_count_mask << header.record_number_bits;
		sc->mask.s.trail_count   = header.trail_count_mask << (header.record_number_bits + header.dup_count_bits);
	} else {
		sc->mask.l.record_number = (uint64_t)header.record_number_mask;
		sc->mask.l.dup_count     = (uint64_t)header.dup_count_mask << header.record_number_bits;
		sc->mask.l.trail_count   = (uint64_t)header.trail_count_mask << (header.record_number_bits + header.dup_count_bits);
	}
	sc->dup_count_shr      = header.record_number_bits;
	sc->trail_count_shr	   = header.record_number_bits + header.dup_count_bits;
	sc->key_pos            = SC_BUF_SIZE;
	sc->idx_pos            = 0;
	return TRUE;
}
Ejemplo n.º 17
0
int dbf_seek(dbf_handle_t dbf, int pos) {
	if (pos < 0 || pos >= dbf->row_count) {
		set_error(ERR_DBF_INVALID_RECORD_ID, dbf, "row=%i", pos);
		return ERROR;
	}
	fseek(dbf->file, dbf->first_row + pos * dbf->row_size, SEEK_SET);
	EW(sfread(dbf->row, dbf->row_size, dbf->file));
	if (dbf->row[0] != FLAG_RECORD_OK && dbf->row[0] != FLAG_RECORD_DELETED) {
		set_error(ERR_DBF_BAD_FORMAT, dbf, "row=%i", pos);
		return ERROR;
	}
	dbf->pos = pos;
	return OK;
}
Ejemplo n.º 18
0
static char *r_string(Stk_t *stkp)
{
	register Sfio_t *in = infile;
	register unsigned long l = sfgetu(in);
	register char *ptr;
	if(l == 0)
		return(NIL(char*));
	ptr = stkalloc(stkp,(unsigned)l);
	if(--l > 0)
	{
		if(sfread(in,ptr,(size_t)l)!=(size_t)l)
			return(NIL(char*));
		ccmaps(ptr, l, CC_ASCII, CC_NATIVE);
	}
	ptr[l] = 0;
	return(ptr);
}
Ejemplo n.º 19
0
static UW16 get_font_type(stream *f)
{   char buf[20], mark_PS[]="%!";
    int i;

    if (sfread(buf, 1, sizeof(buf), f) != sizeof(buf))
        return 0;
    if (buf[0] == 0x15 || buf[0] == 0x00) /* fixme : don't know how to do correctly. */
        return FC_FCO_TYPE;
    for (i = 0; i < sizeof(buf) - sizeof(mark_PS); i++)
        if(!memcmp(buf + i, mark_PS, sizeof(mark_PS) - 1))
            return FC_PST1_TYPE;
    if (buf[0] == '\0' && buf[1] == '\1')
        return FC_TT_TYPE;
    if (buf[0] == 't' && buf[1] == 't')
        return FC_TT_TYPE;
    return 0; /* fixme : unknown type - actually an error. */
}
Ejemplo n.º 20
0
int HRSstreamNext(HRSstream_t s, char *data)
{
  int bytesread;

  if (s->filterOnly) {
    return getNextElement(s, data);    
  }
  else { /* sorted stream */
    bytesread = sfread(s->lfp, data, s->logicalSize);
    if (bytesread == 0)
      return 0;
    else if (bytesread != s->logicalSize) {
      HRSerrorExit1("stream read from %s failed.\n", s->streamName);
      /* won't get here */
      return 0;
    }
    else return 1;
  }
}
Ejemplo n.º 21
0
static int ensure (int count) {
    int n, i;

    if (count > 1000000)
        error (3, "cannot ensure more than 1000000 bytes");
    if (start + count - 1 < end)
        return 0;
    n = end - start;
    if (start != buffer)
        for (i = 0; i < n; i++)
            buffer[i] = start[i];
    start = buffer, end = buffer + n;
    if ((n = sfread (sfstdin, end, count - n)) == -1)
        error (3, "cannot fill buffer");
    end += n;
    if (end - start < count)
        return -1;
    return 0;
}
Ejemplo n.º 22
0
static int binaryStreamAux(HRSstream_t s, char *dataOUT)
{ 
  binaryStream_s *bs = s->bs;
  int bytes = 0;
  int val = 0;

  assert(bs->temp != NULL);
  assert(bs->translate != NULL);

  do {
    bytes = sfread(bs->pfp, bs->temp, bs->physicalSize);
    /* files are checked to make sure that they a 
       multiple of the physical record size */
    if (bytes != bs->physicalSize) {
      if (++bs->count < bs->numFilesinStream) {
	HRSclosefile(bs->pfp);
	bs->pfp = HRSopenfile(bs->fileNames[bs->count], "r");
	continue;
      }
      else 
	return HRS_STREAM_STOP;
    }
    if (bs->filter != NULL) 
      val = bs->filter(s->paramSet, s->paramTable, bs->temp, dataOUT);
    else 
      val = bs->translate(s->paramSet, s->paramTable, bs->temp, dataOUT);

    if (val == HRS_STREAM_STOP)
      return val;

    else if (val < 0) {
      HRSerrorExit1("%s: translation function failed.\n",
		    s->streamName);
    }
    else if (val == HRS_STREAM_DROP_REC)
      continue;
    else return val;
  }
  while (1);

  /* should never get here */
}
Ejemplo n.º 23
0
tmain()
{
	Sfio_t*	f;
	char		buf[1024];
	char*		s;
	int		fd[2];

	close(0);
	if(pipe(fd) < 0 || fd[0] != 0)
		terror("Making pipe");

	strcpy(buf,"1234567890");
	if(!(f = sfopen(NIL(Sfio_t*),buf,"s")))
		terror("Opening string stream");

	if(!sfstack(f,sfstdin))
		terror("Stacking");

	if(write(fd[1],"ab",2) != 2)
		terror("Writing ab to pipe");
	if(!(s = sfreserve(f,SF_UNBOUND,SF_LOCKR)) || sfvalue(f) != 2)
		terror("Peeking size1 = %d but should be 2", sfvalue(f));
	sfread(f,s,0);
	if(strncmp(s,"ab",2) != 0)
		terror("Wrong data1");

	if(write(fd[1],"cd",2) != 2)
		terror("Writing cd to pipe");
	close(fd[1]);
	if(!(s = sfreserve(f,4,0)) )
		terror("Peeking size2 = %d but should be 4", sfvalue(f));
	if(strncmp(s,"abcd",4) != 0)
		terror("Wrong data2");

	if(!(s = sfreserve(f,10,0)) )
		terror("Peeking size3 = %d but should be 10", sfvalue(f));
	if(strncmp(s,"1234567890",10) != 0)
		terror("Wrong data3");

	texit(0);
}
Ejemplo n.º 24
0
int journal_read_node(journal_t *journal, journal_node_t *n, char *dst)
{
	dbg_journal("journal: reading node with id=%"PRIu64", data=<%u, %u>, flags=0x%hx\n",
	            n->id, n->pos, n->pos + n->len, n->flags);

	/* Check valid flag. */
	if (!(n->flags & JOURNAL_VALID)) {
		dbg_journal("journal: node with id=%llu is invalid "
		            "(flags=0x%hx)\n", (unsigned long long)n->id, n->flags);
		return KNOT_EINVAL;
	}

	/* Seek journal node. */
	int seek_ret = lseek(journal->fd, n->pos, SEEK_SET);

	/* Read journal node content. */
	if (seek_ret < 0 || !sfread(dst, n->len, journal->fd)) {
		return KNOT_ERROR;
	}

	return KNOT_EOK;
}
Ejemplo n.º 25
0
tmain()
{
	Sfio_t		*fr;
	int		p[2];
	char		wbuf[1023], rbuf[RBUF*1023], *s;
	int		i, r, n;

	if(pipe(p) < 0 )
		terror("Making pipe for communication");

	if(!(fr = sfnew(0, 0, (size_t)SF_UNBOUND, p[0], SF_READ)) )
		terror("Making read stream");

	for(i = 0; i < sizeof(wbuf); ++i)
		wbuf[i] = (i%10) + '0';

	switch(fork())
	{
		case -1 :
			terror("fork() failed");
		case 0 :
			for(i = 0; i < RBUF*ITER; ++i)
				if(write(p[1], wbuf, sizeof(wbuf)) != sizeof(wbuf))
					terror("Write to pipe failed i=%d", i);
			break;
		default:
			for(i = 0; i < ITER; ++i)
			{	if(sfread(fr, rbuf, sizeof(rbuf)) != sizeof(rbuf))
					terror("Read from pipe failed i=%d", i);
				for(r = 0, s = rbuf; r < RBUF; r += 1, s += n)
				for(n = 0; n < sizeof(wbuf); ++n)
					if(s[n] != (n%10)+'0')
						terror("Bad data i=%d n=%d", i, n);
			}
			break;
	}

	texit(0);
}
Ejemplo n.º 26
0
static int
tp_getdata(Pax_t* pax, register Paxarchive_t* ap, register Paxfile_t* f, int fd)
{
	Sfio_t*		sp;
	off_t		skip;
	ssize_t		n;
	int		r;

	if (!(n = f->st->st_size))
		return 1;
	skip = roundof(f->st->st_size, TP_BLOCK);
	r = -1;
	if (fd < 0)
		r = 1;
	else if (sp = paxpart(pax, ap, f->st->st_size))
		for (;;)
		{
			if ((n = sfread(sp, pax->buf, sizeof(pax->buf))) < 0)
			{
				(*pax->errorf)(NiL, pax, 2, "%s: %s: unexpected EOF", ap->name, f->name);
				break;
			}
			else if (n == 0)
			{
				r = 1;
				break;
			}
			skip -= n;
			if (paxdata(pax, ap, f, fd, pax->buf, n))
				break;
		}
	if (skip && paxread(pax, ap, NiL, skip, 0, 0) != skip)
	{
		(*pax->errorf)(NiL, pax, 2, "%s: %s: cannot skip past %s format data", ap->name, f->name, ap->format->name);
		r = -1;
	}
	return r;
}
Ejemplo n.º 27
0
Dssfile_t*
dssfopen(Dss_t* dss, const char* path, Sfio_t* io, Dssflags_t flags, Dssformat_t* format)
{
	Dssfile_t*	file;
	Vmalloc_t*	vm;
	char*		s;
	size_t		n;
	int		i;
	struct stat	st;
	Sfdisc_t	top;
	char		buf[PATH_MAX];

	if (flags & DSS_FILE_WRITE)
	{
		if (io)
		{
			memset(&top, 0, sizeof(top));
			if (sfdisc(io, &top))
			{
				n = top.disc == &dss->state->compress_preferred;
				sfdisc(io, SF_POPDISC);
				if (n)
				{
					sfdisc(io, SF_POPDISC);
					sfdczip(io, path, dss->meth->compress ? dss->meth->compress : "gzip", dss->disc->errorf);
				}
			}
		}
		if (dss->flags & DSS_APPEND)
			flags |= DSS_FILE_APPEND;
	}
	if (!path || !*path || streq(path, "-"))
	{
		if (flags & DSS_FILE_WRITE)
		{
			if (io)
				path = "output-stream";
			else
			{
				path = "/dev/stdout";
				io = sfstdout;
			}
		}
		else if (io)
			path = "input-stream";
		else
		{
			path = "/dev/stdin";
			io = sfstdin;
		}
		flags |= DSS_FILE_KEEP;
	}
	else if (io)
		flags |= DSS_FILE_KEEP;
	else if (flags & DSS_FILE_WRITE)
	{
		if (!(io = sfopen(NiL, path, (flags & DSS_FILE_APPEND) ? "a" : "w")))
		{
			if (dss->disc->errorf)
				(*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "%s: cannot open", path);
			return 0;
		}
	}
	else if (!(io = dssfind(path, "", DSS_VERBOSE, buf, sizeof(buf), dss->disc)))
		return 0;
	else
		path = (const char*)buf;
	if (!(vm = vmopen(Vmdcheap, Vmbest, 0)))
	{
		if (dss->disc->errorf)
			(*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "out of space");
		return 0;
	}
	if (!(file = vmnewof(vm, 0, Dssfile_t, 1, strlen(path) + 1)))
	{
		if (dss->disc->errorf)
			(*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "out of space");
		if (!(flags & DSS_FILE_KEEP))
			sfclose(io);
		vmclose(vm);
		return 0;
	}
	strcpy(file->path = (char*)(file + 1), path);
	file->dss = dss;
	file->vm = vm;
	file->io = io;
	file->flags = flags;
	if (flags & DSS_FILE_WRITE)
	{
		if (!(file->format = format) && !(file->format = dss->format))
		{
			if (dss->disc->errorf)
				(*dss->disc->errorf)(NiL, dss->disc, 2, "output method format must be specified");
			if (!(flags & DSS_FILE_KEEP))
				sfclose(io);
			return 0;
		}
		file->readf = noreadf;
		file->writef = file->format->writef;
	}
	else
	{
		if (sfsize(file->io) || !fstat(sffileno(file->io), &st) && (S_ISFIFO(st.st_mode)
#ifdef S_ISSOCK
			|| S_ISSOCK(st.st_mode)
#endif
			))
		{
			if (sfdczip(file->io, file->path, NiL, dss->disc->errorf) < 0)
			{
				if (dss->disc->errorf)
					(*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "%s: inflate error", file->path);
				dssfclose(file);
				return 0;
			}
			s = sfreserve(file->io, SF_UNBOUND, SF_LOCKR);
			n = sfvalue(file->io);
			if (!s)
			{
				if (n && dss->disc->errorf)
					(*dss->disc->errorf)(NiL, dss->disc, ERROR_SYSTEM|2, "%s: cannot peek", file->path);
				dssfclose(file);
				return 0;
			}
			for (file->format = (Dssformat_t*)dtfirst(dss->meth->formats); file->format && !(i = (*file->format->identf)(file, s, n, dss->disc)); file->format = (Dssformat_t*)dtnext(dss->meth->formats, file->format));
			sfread(file->io, s, 0);
			if (!file->format)
			{
				if (dss->disc->errorf)
					(*dss->disc->errorf)(NiL, dss->disc, 2, "%s: unknown %s format", file->path, dss->meth->name);
				dssfclose(file);
				return 0;
			}
			if (i < 0)
				return 0;
			if (format && format != file->format)
			{
				if (dss->disc->errorf)
					(*dss->disc->errorf)(NiL, dss->disc, 2, "%s: %s file format %s incompatible with %s", file->path, dss->meth->name, file->format->name, format->name);
				dssfclose(file);
				return 0;
			}
			if ((dss->flags & DSS_VERBOSE) && dss->disc->errorf)
				(*dss->disc->errorf)(dss, dss->disc, 1, "%s: %s method %s format", file->path, dss->meth->name, file->format->name);
			file->readf = file->format->readf;
		}
		else
		{
			file->format = format ? format : dss->format ? dss->format : (Dssformat_t*)dtfirst(dss->meth->formats);
			file->readf = nullreadf;
		}
		file->writef = nowritef;
		if (!dss->format)
			dss->format = file->format;
	}
	if (!file->format)
	{
		if (dss->disc->errorf)
			(*dss->disc->errorf)(NiL, dss->disc, 2, "%s: %s method did not set file format", file->path, dss->meth->name);
		dssfclose(file);
		return 0;
	}
	file->record.file = file;
	if ((*file->format->openf)(file, dss->disc))
	{
		dssfclose(file);
		return 0;
	}
	return file;
}
Ejemplo n.º 28
0
MR_UInt8 *TrackCompiler::PCXRead(FILE * pFile, int &pXRes, int &pYRes)
{
	MR_UInt8 *lReturnValue = NULL;

	MR_Int16 lStartX;
	MR_Int16 lStartY;
	MR_Int16 lSizeX;
	MR_Int16 lSizeY;
	MR_Int16 lNbBytesPerLine;

	fseek(pFile, 4, SEEK_SET);
	sfread(&lStartX, 2, 1, pFile);
	sfread(&lStartY, 2, 1, pFile);
	sfread(&lSizeX, 2, 1, pFile);
	sfread(&lSizeY, 2, 1, pFile);

	fseek(pFile, 66, SEEK_SET);
	sfread(&lNbBytesPerLine, 2, 1, pFile);

	pXRes = lSizeX - lStartX + 1;
	pYRes = lSizeY - lStartY + 1;

	fseek(pFile, 128, SEEK_SET);

	ASSERT(pXRes > 0);
	ASSERT(pYRes > 0);

	lReturnValue = new MR_UInt8[pXRes * pYRes];

	int lOffset = 0;

	for(int lLineNb = 0; lLineNb < pYRes; lLineNb++) {
		for(int lColNb = 0; lColNb < lNbBytesPerLine;) {
			MR_UInt8 lBuffer;
			MR_UInt8 lBuffer2;

			sfread(&lBuffer, 1, 1, pFile);

			if((lBuffer & 0xc0) == 0xc0) {
				sfread(&lBuffer2, 1, 1, pFile);
				/*
				   lBuffer2++;
				   if( lBuffer2 == 255 )
				   {
				   lBuffer2 = 0;
				   }
				 */

				for(int lCounter = 0; lCounter < (lBuffer & 0x3f); lCounter++) {
					if(lColNb < pXRes) {
						lReturnValue[lOffset++] = lBuffer2;
					}
					lColNb++;
				}
			}
			else {
				if(lColNb < pXRes) {
					/*
					   lBuffer++;
					   if( lBuffer2 == 255 )
					   {
					   lBuffer2 = 0;
					   }
					 */
					lReturnValue[lOffset++] = lBuffer;
				}
				lColNb++;
			}
		}
	}

	return lReturnValue;
}
Ejemplo n.º 29
0
/*
 * copy the lines starting at offset <start> from in <in> to <out>
 * in reverse order
 */
int rev_line(Sfio_t *in, Sfio_t *out, off_t start)
{
	register char *cp, *cpold;
	register int n, nleft=0;
	char buff[BUFSIZE];
	off_t offset;
	if(sfseek(in,(off_t)0,SEEK_CUR) < 0)
	{
		Sfio_t *tmp = sftmp(4*SF_BUFSIZE);
		if(!tmp)
			return(-1);
		if(start>0 && sfmove(in, (Sfio_t*)0, start, -1) != start)
			return(-1);
		if(sfmove(in, tmp, SF_UNBOUND, -1) < 0 || !sfeof(in) || sferror(tmp))
			return(-1);
		in = tmp;
		start=0;
	}
	if((offset = sfseek(in,(off_t)0,SEEK_END)) <= start)
		return(0);
	offset = rounddown(offset,BUFSIZE);
	while(1)
	{
		n = BUFSIZE;
		if(offset < start)
		{
			n -= (start-offset);
			offset = start;
		}
		sfseek(in, offset, SEEK_SET);
		if((n=sfread(in, buff, n)) <=0)
			break;
		cp = buff+n;
		n = *buff;
		*buff = '\n';
		while(1)
		{
			cpold = cp;
			if(nleft==0)
				cp--;
			if(cp==buff)
			{
				nleft= 1;
				break;
			}
			while(*--cp != '\n');
			if(cp==buff && n!='\n')
			{
				*cp = n;
				nleft += cpold-cp;
				break;
			}
			else
				cp++;
			if(sfwrite(out,cp,cpold-cp) < 0)
				return(-1);
			if(nleft)
			{
				if(nleft==1)
					sfputc(out,'\n');
				else if(sfmove(in,out,nleft,-1) != nleft)
					return(-1);
				nleft = 0;
			}
		}
		if(offset <= start)
			break;
		offset -= BUFSIZE;
	}
	if(nleft)
	{
		sfseek(in, start, SEEK_SET);
		if(sfmove(in,out,nleft,-1) != nleft)
			return(-1);
	}
	return(0);
}
Ejemplo n.º 30
0
int
pzheadread(register Pz_t* pz)
{
	register int		i;
	register int		n;
	register unsigned char*	s;
	size_t			m;
	Pzpart_t*		pp;

	if (pz->flags & PZ_HEAD)
		return 0;

	/*
	 * check the header magic
	 */

	if (s = (unsigned char*)sfreserve(pz->io, 4, 1))
	{
		i = s[0];
		n = s[1];
	}
	else
		i = n = 0;
	if (pz->disc->errorf)
		(*pz->disc->errorf)(pz, pz->disc, -1, "%s: pzheadread: f=%08x i=%02x n=%02x partition=%s%s", pz->path, pz->flags, i, n, pz->disc->partition, s ? "" : " (nil)");
	if (i != PZ_MAGIC_1 || n != PZ_MAGIC_2 || s[2] == 0 || s[3] >= 10)
	{
		sfread(pz->io, s, 0);
		if (pz->flags & PZ_SPLIT)
			return 0;
		if (pz->flags & PZ_DISC)
		{
			pz->flags &= ~PZ_POP;
			return -1;
		}
		if (!(pz->flags & (PZ_READ|PZ_WRITE|PZ_STAT)) && (m = pz->prefix.count))
		{
			if (pz->prefix.terminator >= 0)
			{
				while (m-- > 0)
				{
					if (!sfgetr(pz->io, pz->prefix.terminator, 0))
					{
						if (pz->disc->errorf)
							(*pz->disc->errorf)(pz, pz->disc, 2, "%s: cannot read %I*u prefix record%s from data", pz->path, sizeof(pz->prefix.count), pz->prefix.count, pz->prefix.count == 1 ? "" : "s");
						return -1;
					}
				}
			}
			else if (!sfreserve(pz->io, m, 0))
			{
				if (pz->disc->errorf)
					(*pz->disc->errorf)(pz, pz->disc, 2, "%s: cannot read %I*u prefix byte%s from data", pz->path, sizeof(pz->prefix.count), pz->prefix.count, pz->prefix.count == 1 ? "" : "s");
				return -1;
			}
		}
		if (!(n = pz->row))
		{
			if ((pz->flags & PZ_ACCEPT) || !sfsize(pz->io))
				n = 1;
			else if ((n = pzfixed(pz, pz->io, NiL, 0)) <= 0 && pz->disc->partition)
			{
				pz->flags |= PZ_ROWONLY;
				if (!pzpartition(pz, pz->disc->partition))
					n = pz->row;
				pz->flags &= ~PZ_ROWONLY;
			}
		}
		if (n <= 0)
		{
			if (!(pz->flags & PZ_DELAY) && (pz->disc->partition || !(pz->flags & PZ_FORCE)))
			{
				if (pz->disc->errorf)
					(*pz->disc->errorf)(pz, pz->disc, 2, "%s: unknown input format", pz->path);
				return -1;
			}
			pz->flags |= PZ_UNKNOWN;
			n = 1;
		}
		if (!(pp = vmnewof(pz->vm, 0, Pzpart_t, 1, 0)))
			return -1;
		pz->major = PZ_MAJOR;
		pz->minor = PZ_MINOR;
		pp->name = "";
		pp->row = n;
		return pzpartinit(pz, pp, NiL);
	}
	sfread(pz->io, s, 2);
	pz->flags &= ~PZ_FORCE;
	pz->major = sfgetc(pz->io);
	pz->minor = sfgetc(pz->io);
	switch (pz->major)
	{
	case 1:
		if (pz->minor <= 2)
			goto noway;
		break;
	case 2:
		pz->win = sfgetu(pz->io);
		break;
	default:
		goto noway;
	}
	pz->flags |= PZ_HEAD;
	return pzpartread(pz);
 noway:
	if (pz->disc->errorf)
		(*pz->disc->errorf)(pz, pz->disc, 2, "%s: data %d.%d not supported by implementation %d.%d", pz->path, pz->major, pz->minor, PZ_MAJOR, PZ_MINOR);
	return -1;
}