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