summaryrefslogtreecommitdiff
path: root/src/tet3/dtet2lib/ldst.c
blob: 17d093f34fb51531d108808ef6e66d8dc47200cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 *      SCCS:  @(#)ldst.c	1.7 (96/11/04) 
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1992 X/Open Company Limited
 *
 * All rights reserved.  No part of this source code may be reproduced,
 * stored in a retrieval system, or transmitted, in any form or by any
 * means, electronic, mechanical, photocopying, recording or otherwise,
 * except as stated in the end-user licence agreement, without the prior
 * permission of the copyright owners.
 *
 * X/Open and the 'X' symbol are trademarks of X/Open Company Limited in
 * the UK and other countries.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/************************************************************************

SCCS:   	@(#)ldst.c	1.7 96/11/04 TETware release 3.3
NAME:		ldst.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	April 1992

DESCRIPTION:
	low-level functions to convert data structures between internal and
	machine-independent format

MODIFICATIONS:

************************************************************************/

#ifndef TET_LITE	/* -START-LITE-CUT- */

#include <stdio.h>
#include "dtmac.h"
#include "ltoa.h"
#include "ldst.h"
#include "dtmsg.h"
#include "error.h"
#include "dtetlib.h"


/* static function declarations */
static int bs2char PROTOLIST((char *, char *, int));
static int bs2long PROTOLIST((char *, long *, int));
static int bs2short PROTOLIST((char *, short *, int));
static int bs2ushort PROTOLIST((char *,  unsigned short *, int));
static int char2bs PROTOLIST((char *, char *, int));
static int long2bs PROTOLIST((long *, char *, int));
static int short2bs PROTOLIST((short *, char *, int));


/*
**	tet_bs2st() - convert a structure from machine-independent to
**		internal format
**
**	return 0 if the expected number of bytes was received, -1 otherwise
**	(this is mainly a defence against inconsistencies between the
**	structures and their stdesc descriptions)
*/

int tet_bs2st(from, to, st, nst, len)
char *from, *to;
register struct stdesc *st;
register int nst, len;
{
	register char *fp, *tp;
	register int n, count;

	for (count = 0; nst > 0 && count < len; nst--, st++) {
		fp = from + st->st_bsoff;
		tp = to + st->st_stoff;
		n = st->st_type & ST_COUNTMASK;
		switch (st->st_type & ST_TYPEMASK) {
		case ST_CHARTYPE:
			count += bs2char(fp, tp, n);
			break;
		case ST_SHORTTYPE:
			count += bs2short(fp, (short *) tp, n);
			break;
		case ST_USHORTTYPE:
			count += bs2ushort(fp, (unsigned short *) tp, n);
			break;
		case ST_LONGTYPE:
			count += bs2long(fp, (long *) tp, n);
			break;
		default:
			error(0, "unexpected type", tet_i2o(st->st_type));
			break;
		}
	}

	if (len != count) {
		error(0, "internal error: tet_bs2st() wanted", tet_i2a(count));
		error(0, "tet_bs2st() received", tet_i2a(len));
		return(-1);
	}

	return(0);
}

/*
**	tet_st2bs() - convert a structure to machine-independent format
**
**	return the number of bytes occupied by the result
*/

int tet_st2bs(from, to, st, nst)
char *from, *to;
register struct stdesc *st;
register int nst;
{
	register char *fp, *tp;
	register int n, count;

	for (count = 0; nst > 0; nst--, st++) {
		fp = from + st->st_stoff;
		tp = to + st->st_bsoff;
		n = st->st_type & ST_COUNTMASK;
		switch (st->st_type & ST_TYPEMASK) {
		case ST_CHARTYPE:
			count += char2bs(fp, tp, n);
			break;
		case ST_SHORTTYPE:
		case ST_USHORTTYPE:
			count += short2bs((short *) fp, tp, n);
			break;
		case ST_LONGTYPE:
			count += long2bs((long *) fp, tp, n);
			break;
		default:
			error(0, "unexpected type", tet_i2o(st->st_type));
			break;
		}
	}

	return(count);
}

/*
**	bs2char(), bs2short(), bs2ushort, bs2long() - convert arrays of
**		objects from machine-independent to internal format
**
**	return the number of bytes of machine-independent data converted
*/

static int bs2char(from, to, n)
register char *from;
register char *to;
register int n;
{
	register int count = n;

	while (--n >= 0)
		*to++ = *from++;

	return(count);
}

static int bs2short(from, to, n)
register char *from;
register short *to;
register int n;
{
	register int count = n * SHORTSIZE;

	while (--n >= 0) {
		*to++ = ld16(from);
		from += SHORTSIZE;
	}

	return(count);
}

static int bs2ushort(from, to, n)
register char *from;
register unsigned short *to;
register int n;
{
	register int count = n * SHORTSIZE;

	while (--n >= 0) {
		*to++ = ld16u(from);
		from += SHORTSIZE;
	}

	return(count);
}

static int bs2long(from, to, n)
register char *from;
register long *to;
register int n;
{
	register int count = n * LONGSIZE;

	while (--n >= 0) {
		*to++ = ld32(from);
		from += LONGSIZE;
	}

	return(count);
}

/*
**	char2bs(), short2bs(), long2bs() - convert arrays of
**		objects from internal to machine-independent format
**
**	return the number of bytes occupied by the machine-independent data
*/

static int char2bs(from, to, n)
register char *from, *to;
register int n;
{
	register int count = n;

	while (--n >= 0)
		*to++ = *from++;

	return(count);
}

static int short2bs(from, to, n)
register short *from;
register char *to;
register int n;
{
	register int count = n * SHORTSIZE;

	while (--n >= 0) {
		st16(*from, to);
		from++;
		to += SHORTSIZE;
	}

	return(count);
}

static int long2bs(from, to, n)
register long *from;
register char *to;
register int n;
{
	register int count = n * LONGSIZE;

	while (--n >= 0) {
		st32(*from, to);
		from++;
		to += LONGSIZE;
	}

	return(count);
}

#else	/* -END-LITE-CUT- */

int tet_ldst_c_not_empty;

#endif /* !TET_LITE */	/* -LITE-CUT-LINE- */