xref: /onnv-gate/usr/src/lib/libast/common/misc/reclen.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin 
244887Schin /*
254887Schin  * return the length of the current record at b, size n, according to f
264887Schin  * -1 returned on error
274887Schin  *  0 returned if more data is required
284887Schin  */
294887Schin 
304887Schin #include <recfmt.h>
314887Schin #include <ctype.h>
324887Schin 
334887Schin ssize_t
reclen(Recfmt_t f,const void * b,size_t n)344887Schin reclen(Recfmt_t f, const void* b, size_t n)
354887Schin {
364887Schin 	register unsigned char*	s = (unsigned char*)b;
374887Schin 	register unsigned char*	e;
384887Schin 	size_t			h;
394887Schin 	size_t			z;
404887Schin 
414887Schin 	switch (RECTYPE(f))
424887Schin 	{
434887Schin 	case REC_delimited:
444887Schin 		if (e = (unsigned char*)memchr(s, REC_D_DELIMITER(f), n))
454887Schin 			return e - s + 1;
464887Schin 		return 0;
474887Schin 	case REC_fixed:
484887Schin 		return REC_F_SIZE(f);
494887Schin 	case REC_variable:
504887Schin 		h = REC_V_HEADER(f);
514887Schin 		if (n < h)
524887Schin 			return 0;
534887Schin 		z = 0;
544887Schin 		s += REC_V_OFFSET(f);
554887Schin 		e = s + REC_V_LENGTH(f);
564887Schin 		if (REC_V_LITTLE(f))
574887Schin 			while (e > s)
584887Schin 				z = (z<<8)|*--e;
594887Schin 		else
604887Schin 			while (s < e)
614887Schin 				z = (z<<8)|*s++;
624887Schin 		if (!REC_V_INCLUSIVE(f))
634887Schin 			z += h;
644887Schin 		else if (z < h)
654887Schin 			z = h;
664887Schin 		return z;
674887Schin 	case REC_method:
684887Schin 		return -1;
694887Schin 	}
704887Schin 	return -1;
714887Schin }
72