summaryrefslogtreecommitdiff
path: root/src/tet3/dtet2lib/valmsg.c
blob: db05ad01857de8d30499427650a058aa75a48759 (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
/*
 *      SCCS:  @(#)valmsg.c	1.8 (98/08/28) 
 *
 *	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:   	@(#)valmsg.c	1.8 98/08/28 TETware release 3.3
NAME:		valmsg.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	April 1992

DESCRIPTION:
	function to convert DTET interprocess numeric value message between
	internal and machine-independent format

MODIFICATIONS:
	Andrew Dingwall, UniSoft Ltd., October 1994
	added support for sync message data on the end of a numeric
	value message

	Andrew Dingwall, UniSoft Ltd., July 1998
	Added support for shared API libraries.
 

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

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

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

static struct stdesc st[] = {
	VALMSG_DESC
};

static short nst = -1;
static short fixed = -1;

/* static function declarations */
static int bs2vsmsg PROTOLIST((char *, int, struct valmsg **, int *, int));
static void stinit PROTOLIST((void));


static void stinit()
{
	register struct valmsg *sp = (struct valmsg *) 0;
	register int n = 0;

	VALMSG_INIT(st, sp, n, nst, fixed);
}

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

int tet_valmsg2bs(from, to)
register struct valmsg *from;
register char *to;
{
	register int count;
	struct stdesc tmp;

	if (nst < 0)
		stinit();

	/* convert the fixed part */
	count = tet_st2bs((char *) from, to, st, fixed);

	/* then convert the variable part */
	tmp = st[fixed];
	tmp.st_type = (tmp.st_type & ST_TYPEMASK) |
		(from->vm_nvalue & ST_COUNTMASK);
	count += tet_st2bs((char *) from, to + VM_VALUESTART, &tmp, 1);

	return(count);
}

/*
**	tet_bs2valmsg() - convert a valmsg message to internal format
**
**	return the number of bytes in the valmsg result, or -1 on error
*/

TET_IMPORT int tet_bs2valmsg(from, fromlen, to, tolen)
char *from;
int fromlen;
struct valmsg **to;
int *tolen;
{
	return(bs2vsmsg(from, fromlen, to, tolen, 0));
}

/*
**	bs2vsmsg() - common subroutine for tet_bs2valmsg() and tet_bs2synmsg()
*/

static int bs2vsmsg(from, fromlen, to, tolen, smproc)
register char *from;
register int fromlen;
register struct valmsg **to;
register int *tolen;
int smproc;
{
	struct stdesc tmp;
	register int bslen, vmlen;

	if (nst < 0)
		stinit();

	/* make sure that the buffer is big enough for a minimal message */
	if (BUFCHK((char **) to, tolen, valmsgsz(1)) < 0)
		return(-1);

	/* convert the fixed part */
	if (tet_bs2st(from, (char *) *to, st, fixed,
		TET_MIN(fromlen, VM_VALUESTART)) < 0)
			return(-1);

	/* make sure that the buffer is big enough for the actual message */
	if ((int) (*to)->vm_nvalue > 1 &&
		BUFCHK((char **) to, tolen, valmsgsz((*to)->vm_nvalue)) < 0)
			return(-1);

	/* then convert the variable part */
	tmp = st[fixed];
	tmp.st_type = (tmp.st_type & ST_TYPEMASK) |
		((*to)->vm_nvalue & ST_COUNTMASK);
	bslen = fromlen - VM_VALUESTART;
	if (smproc &&
		(vmlen = VM_VALMSGSZ((*to)->vm_nvalue) - VM_VALUESTART) < bslen)
			bslen = vmlen;
	if (tet_bs2st(from + VM_VALUESTART, (char *) *to, &tmp, 1, bslen) < 0)
		return(-1);

	/* return the number of bytes in the result */
	return(valmsgsz((*to)->vm_nvalue));
}

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

int tet_synmsg2bs(from, to)
register struct valmsg *from;
register char *to;
{
	register int count;
	register int dlen;
	struct stdesc tmp;

	/* convert the valmsg part */
	count = tet_valmsg2bs(from, to);

	/* convert the sync message data (if any) */
	if ((dlen = VM_MSDLEN(from)) > 0) {
		tmp.st_type = ST_CHAR(dlen);
		tmp.st_bsoff = count;
		tmp.st_stoff = VM_MSDATA(from) - (char *) from;
		count += tet_st2bs((char *) from, to, &tmp, 1);
	}

	return(count);
}

/*
**	tet_bs2synmsg() - convert a synmsg message to internal format
**
**	return the number of bytes in the valmsg result, or -1 on error
*/

TET_IMPORT int tet_bs2synmsg(from, fromlen, to, tolen)
register char *from;
register int fromlen;
register struct valmsg **to;
register int *tolen;
{
	register int count;
	register int dlen;
	struct stdesc tmp;

	/* convert the valmsg part */
	if ((count = bs2vsmsg(from, fromlen, to, tolen, 1)) < 0)
		return(-1);

	/* return now if there is no sync message data to convert */
	if (fromlen <= (int) VM_VALMSGSZ((*to)->vm_nvalue))
		return(count);

	/* grow the buffer if necessary to accomodate the sync message data */
	dlen = (int) VM_MSDLEN(*to);
	if (BUFCHK((char **) to, tolen, synmsgsz((*to)->vm_nvalue, dlen)) < 0)
		return(-1);

	/* convert the sync message data */
	tmp.st_type = ST_CHAR(dlen);
	tmp.st_bsoff = VM_VALMSGSZ((*to)->vm_nvalue);
	tmp.st_stoff = VM_MSDATA(*to) - (char *) *to;
	if (tet_bs2st(from, (char *) *to, &tmp, 1,
		fromlen - VM_VALMSGSZ((*to)->vm_nvalue)) < 0)
			return(-1);

	return(synmsgsz((*to)->vm_nvalue, dlen));
}

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

int tet_valmsg_c_not_empty;

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