xref: /onnv-gate/usr/src/lib/libast/common/string/swapop.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  * Glenn Fowler
254887Schin  * AT&T Research
264887Schin  *
274887Schin  * internal representation conversion support
284887Schin  */
294887Schin 
304887Schin #include <ast.h>
314887Schin #include <swap.h>
324887Schin 
334887Schin /*
344887Schin  * return the swap operation for external to internal conversion
354887Schin  * if size<0 then (-size) used and (-size==4)&&(op==3) => op=7
364887Schin  * this is a workaround for 4 byte magic predicting 8 byte swap
374887Schin  */
384887Schin 
394887Schin int
swapop(const void * internal,const void * external,int size)404887Schin swapop(const void* internal, const void* external, int size)
414887Schin {
424887Schin 	register int	op;
434887Schin 	register int	z;
444887Schin 	char		tmp[sizeof(intmax_t)];
454887Schin 
464887Schin 	if ((z = size) < 0)
474887Schin 		z = -z;
484887Schin 	if (z <= 1)
494887Schin 		return 0;
504887Schin 	if (z <= sizeof(intmax_t))
514887Schin 		for (op = 0; op < z; op++)
524887Schin 			if (!memcmp(internal, swapmem(op, external, tmp, z), z))
534887Schin 			{
544887Schin 				if (size < 0 && z == 4 && op == 3)
554887Schin 					op = 7;
564887Schin 				return op;
574887Schin 			}
584887Schin 	return -1;
594887Schin }
60