--- ray/src/common/readoct.c 1991/02/07 11:00:42 1.11 +++ ray/src/common/readoct.c 1993/06/07 09:42:16 2.9 @@ -1,4 +1,4 @@ -/* Copyright (c) 1986 Regents of the University of California */ +/* Copyright (c) 1992 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -18,14 +18,15 @@ static char SCCSid[] = "$SunId$ LBL"; #include "otypes.h" -extern double atof(); -static double getflt(); -static long getint(); -static char *getstr(); +static double ogetflt(); +static long ogetint(); +static char *ogetstr(); +static int getobj(), octerror(); static OCTREE getfullnode(), gettree(); static char *infn; /* input file name */ static FILE *infp; /* input file stream */ +static int objsize; /* size of stored OBJECT's */ static OBJECT objorig; /* zeroeth object */ static short otypmap[NUMOTYPE+8]; /* object type map */ @@ -39,8 +40,9 @@ char *ofn[]; { char sbuf[512]; int nf; - OBJECT fnobjects; + OBJECT fnobjects; register int i; + long m; if (fname == NULL) { infn = "standard input"; @@ -53,26 +55,28 @@ char *ofn[]; error(SYSTEM, errmsg); } } +#ifdef MSDOS + setmode(fileno(infp), O_BINARY); +#endif /* get header */ - if (load & IO_INFO) - copyheader(infp, stdout); - else - getheader(infp, NULL); + if (checkheader(infp, OCTFMT, load&IO_INFO ? stdout : NULL) < 0) + octerror(USER, "not an octree"); /* check format */ - if (getint(2) != OCTMAGIC) - octerror(USER, "invalid octree format"); + if ((objsize = ogetint(2)-OCTMAGIC) <= 0 || + objsize > MAXOBJSIZ || objsize > sizeof(long)) + octerror(USER, "incompatible octree format"); /* get boundaries */ if (load & IO_BOUNDS) { for (i = 0; i < 3; i++) - scene->cuorg[i] = atof(getstr(sbuf)); - scene->cusize = atof(getstr(sbuf)); + scene->cuorg[i] = atof(ogetstr(sbuf)); + scene->cusize = atof(ogetstr(sbuf)); } else { for (i = 0; i < 4; i++) - getstr(sbuf); + ogetstr(sbuf); } objorig = nobjects; /* set object offset */ nf = 0; /* get object files */ - while (*getstr(sbuf)) { + while (*ogetstr(sbuf)) { if (load & IO_SCENE) readobj(sbuf); if (load & IO_FILES) @@ -82,105 +86,98 @@ char *ofn[]; if (load & IO_FILES) ofn[nf] = NULL; /* get number of objects */ - fnobjects = getint(sizeof(OBJECT)); + fnobjects = m = ogetint(objsize); + if (fnobjects != m) + octerror(USER, "too many objects"); - if (load & IO_TREE) { - /* get the octree */ + if (load & IO_TREE) /* get the octree */ scene->cutree = gettree(); - /* get the scene */ - if (nf == 0 && load & IO_SCENE) { - for (i = 0; *getstr(sbuf); i++) - if ((otypmap[i] = otype(sbuf)) < 0) { - sprintf(errmsg, "unknown type \"%s\"", - sbuf); - octerror(WARNING, errmsg); - } - while (getobj() != OVOID) - ; - } - } - fclose(infp); - /* consistency checks */ - if (load & IO_SCENE) { - /* check object count */ + else if (load & IO_SCENE && nf == 0) + skiptree(); + + if (load & IO_SCENE) /* get the scene */ + if (nf == 0) { + for (i = 0; *ogetstr(sbuf); i++) + if ((otypmap[i] = otype(sbuf)) < 0) { + sprintf(errmsg, "unknown type \"%s\"", sbuf); + octerror(WARNING, errmsg); + } + while (getobj() != OVOID) + ; + } else { /* consistency checks */ + /* check object count */ if (nobjects != objorig+fnobjects) octerror(USER, "bad object count; octree stale?"); - /* check for non-surfaces */ + /* check for non-surfaces */ if (nonsurfinset(objorig, fnobjects)) - octerror(USER, "non-surface in set; octree stale?"); - } + octerror(USER, "modifier in tree; octree stale?"); + } + fclose(infp); return(nf); } static char * -getstr(s) /* get null-terminated string */ +ogetstr(s) /* get null-terminated string */ char *s; { - register char *cp; - register int c; + extern char *getstr(); - cp = s; - while ((c = getc(infp)) != EOF) - if ((*cp++ = c) == '\0') - return(s); - - octerror(USER, "truncated octree"); + if (getstr(s, infp) == NULL) + octerror(USER, "truncated octree"); + return(s); } static OCTREE getfullnode() /* get a set, return fullnode */ { - OBJECT set[MAXSET+1]; + OBJECT set[MAXSET+1]; register int i; + register long m; - set[0] = getint(sizeof(OBJECT)); - if (set[0] > MAXSET) + if ((set[0] = ogetint(objsize)) > MAXSET) octerror(USER, "bad set in getfullnode"); - for (i = 1; i <= set[0]; i++) - set[i] = getint(sizeof(OBJECT)) + objorig; + for (i = 1; i <= set[0]; i++) { + m = ogetint(objsize) + objorig; + if ((set[i] = m) != m) + octerror(USER, "too many objects"); + } return(fullnode(set)); } static long -getint(siz) /* get a siz-byte integer */ -register int siz; +ogetint(siz) /* get a siz-byte integer */ +int siz; { - register int c; + extern long getint(); register long r; - if ((c = getc(infp)) == EOF) - goto end_file; - r = 0x80&c ? -1<<8|c : c; /* sign extend */ - while (--siz > 0) { - if ((c = getc(infp)) == EOF) - goto end_file; - r <<= 8; - r |= c; - } + r = getint(siz, infp); + if (feof(infp)) + octerror(USER, "truncated octree"); return(r); -end_file: - octerror(USER, "truncated octree"); } static double -getflt() /* get a floating point number */ +ogetflt() /* get a floating point number */ { - extern double ldexp(); - double d; + extern double getflt(); + double r; - d = (double)getint(4)/0x7fffffff; - return(ldexp(d, (int)getint(1))); + r = getflt(infp); + if (feof(infp)) + octerror(USER, "truncated octree"); + return(r); } static OCTREE gettree() /* get a pre-ordered octree */ { - register OCTREE ot; + register OCTREE ot; register int i; switch (getc(infp)) { @@ -203,14 +200,40 @@ gettree() /* get a pre-ordered octree */ static +skiptree() /* skip octree on input */ +{ + register int i; + + switch (getc(infp)) { + case OT_EMPTY: + return; + case OT_FULL: + for (i = ogetint(objsize)*objsize; i-- > 0; ) + if (getc(infp) == EOF) + octerror(USER, "truncated octree"); + return; + case OT_TREE: + for (i = 0; i < 8; i++) + skiptree(); + return; + case EOF: + octerror(USER, "truncated octree"); + default: + octerror(USER, "damaged octree"); + } +} + + +static getobj() /* get next object */ { char sbuf[MAXSTR]; int obj; register int i; - register OBJREC *objp; + register long m; + register OBJREC *objp; - i = getint(1); + i = ogetint(1); if (i == -1) return(OVOID); /* terminator */ if ((obj = newobject()) == OVOID) @@ -218,41 +241,44 @@ getobj() /* get next object */ objp = objptr(obj); if ((objp->otype = otypmap[i]) < 0) octerror(USER, "reference to unknown type"); - if ((objp->omod = getint(sizeof(OBJECT))) != OVOID) - objp->omod += objorig; - objp->oname = savqstr(getstr(sbuf)); - if (objp->oargs.nsargs = getint(2)) { + if ((m = ogetint(objsize)) != OVOID) { + m += objorig; + if ((OBJECT)m != m) + octerror(USER, "too many objects"); + } + objp->omod = m; + objp->oname = savqstr(ogetstr(sbuf)); + if (objp->oargs.nsargs = ogetint(2)) { objp->oargs.sarg = (char **)bmalloc (objp->oargs.nsargs*sizeof(char *)); if (objp->oargs.sarg == NULL) goto memerr; for (i = 0; i < objp->oargs.nsargs; i++) - objp->oargs.sarg[i] = savestr(getstr(sbuf)); + objp->oargs.sarg[i] = savestr(ogetstr(sbuf)); } else objp->oargs.sarg = NULL; -#ifdef IARGS - if (objp->oargs.niargs = getint(2)) { +#ifdef IARGS + if (objp->oargs.niargs = ogetint(2)) { objp->oargs.iarg = (long *)bmalloc (objp->oargs.niargs*sizeof(long)); if (objp->oargs.iarg == NULL) goto memerr; for (i = 0; i < objp->oargs.niargs; i++) - objp->oargs.iarg[i] = getint(4); + objp->oargs.iarg[i] = ogetint(4); } else objp->oargs.iarg = NULL; #endif - if (objp->oargs.nfargs = getint(2)) { - objp->oargs.farg = (double *)bmalloc - (objp->oargs.nfargs*sizeof(double)); + if (objp->oargs.nfargs = ogetint(2)) { + objp->oargs.farg = (FLOAT *)bmalloc + (objp->oargs.nfargs*sizeof(FLOAT)); if (objp->oargs.farg == NULL) goto memerr; for (i = 0; i < objp->oargs.nfargs; i++) - objp->oargs.farg[i] = getflt(); + objp->oargs.farg[i] = ogetflt(); } else objp->oargs.farg = NULL; /* initialize */ objp->os = NULL; - objp->lastrno = -1; /* insert */ insertobject(obj); return(obj);