xref: /original-bsd/usr.bin/pascal/libpc/PACK.c (revision ae45e3e8)
1 /*-
2  * Copyright (c) 1979, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)PACK.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 /*
13  * pack(a,i,z)
14  *
15  * with:	a: array[m..n] of t
16  *	z: packed array[u..v] of t
17  *
18  * semantics:	for j := u to v do
19  *			z[j] := a[j-u+i];
20  *
21  * need to check:
22  *	1. i >= m
23  *	2. i+(v-u) <= n		(i.e. i-m <= (n-m)-(v-u))
24  *
25  * on stack:	lv(z), lv(a), rv(i) (len 4)
26  *
27  * move w(t)*(v-u+1) bytes from lv(a)+w(t)*(i-m) to lv(z)
28  */
29 
30 PACK(i, a, z, size_a, lb_a, ub_a, size_z)
31 
32 	long	i;	/* subscript into a to begin packing */
33 	char	*a;	/* pointer to structure a */
34 	char	*z;	/* pointer to structure z */
35 	long	size_a;	/* sizeof(a_type) */
36 	long	lb_a;	/* lower bound of structure a */
37 	long	ub_a;	/* (upper bound of a) - (lb_a + sizeof(z_type)) */
38 	long	size_z;	/* sizeof(z_type) */
39 {
40 	int		subscr;
41 	register char	*cp;
42 	register char	*zp = z;
43 	register char	*limit;
44 
45 	subscr = i - lb_a;
46 	if (subscr < 0 || subscr > ub_a) {
47 		ERROR("i = %D: Bad i to pack(a,i,z)\n", i);
48 		return;
49 	}
50 	cp = &a[subscr * size_a];
51 	limit = cp + size_z;
52 	do	{
53 		*zp++ = *cp++;
54 	} while (cp < limit);
55 }
56