summaryrefslogtreecommitdiffstats
path: root/src/isode/psap/qbuf2pe_f.c
blob: acebccfac5764fb5e20615333cae3540e6e23d8e (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* qbuf2pe.c - build PE(s) from an SSDU assumed to be in qbuf(s) */

/* 
 * isode/psap/qbuf2pe_f.c
 */

/*
 *				  NOTICE
 *
 * Acquisition, use, and distribution of this module and related
 * materials are subject to the restrictions of a license agreement.
 * Consult the Preface in the User's Manual for the full terms of
 * this agreement.
 *
 */


/* LINTLIBRARY */

#include "stdio.h"
#include "psap.h"
#include "tailor.h"

/*  */

static	int  qb_read_cons ();
static	qbuf2data();

/*
 * The following macro takes one byte from a qbuf, stuffs it in c,
 * and adjusts the qbuf.
 */

#define qbuf2char(c)	{ \
				qp = Qb; \
				if (qp->qb_len > 0 && qp != Hqb) { \
					(c) = *qp->qb_data++; \
					Byteno++; \
					if(--qp->qb_len == 0) { \
						if(!(Qb = qp->qb_forw)) \
							abort(); \
					} \
				} \
				else { \
					SLOG (psap_log, LLOG_EXCEPTIONS, \
					  NULLCP, \
					  ("bad qbuf length=%d", \
					  ((qp == Hqb) ? 0: qp->qb_len))); \
					*result = PS_ERR_EOF; \
					if (pe) pe_free(pe); \
					return(NULLPE); \
				} \
			}

int		Byteno;
int		Qbrefs;
struct qbuf    *Hqb;
struct qbuf    *Fqb;
struct qbuf    *Qb;

static int pe_id_overshift = PE_ID_MASK << (PE_ID_BITS - PE_ID_SHIFT);

/*  */

PE
qbuf2pe_f (result)
int	*result;
{
	register PE	pe;
	register struct qbuf *qp;
	byte c, d;
	register PElementClass class;
	register PElementForm form;
	register PElementID id;
	register PElementLen   len;
	register int    i;
	register PElementLen j;

	pe = NULLPE;

	/*
	 * Just take the qbuf directly and build PEs.
	 * First, decode the id.
	 */

	qbuf2char(c);

	class = ((int)(c & PE_CLASS_MASK)) >> PE_CLASS_SHIFT;
	form = ((int)(c & PE_FORM_MASK)) >> PE_FORM_SHIFT;
	j = (c & PE_CODE_MASK);

	if (j == PE_ID_XTND)
		for (j = 0;; j <<= PE_ID_SHIFT) {
			qbuf2char(d);
			j |= d & PE_ID_MASK;
			if (!(d & PE_ID_MORE))
				break;
			if (j & pe_id_overshift) {
#ifdef DEBUG
			        DLOG (psap_log, LLOG_DEBUG,
				      ("j is %x d is %x %x %x %x\n",
				       j, d, *(Qb->qb_data-1),
				       *(Qb->qb_data-2),
				       *(Qb->qb_data-3)));
#endif
				*result = PS_ERR_OVERID;
				return (NULLPE);
			}
		}

	id = j;

	DLOG (psap_log, LLOG_DEBUG,
		("class=%d form=%d id=%d", class, form, id));

	if ((pe = pe_alloc (class, form, id)) == NULLPE) {
		*result = PS_ERR_NMEM;
		return (NULLPE);
	}

	qbuf2char(c);

	if ((i = c) & PE_LEN_XTND) {
		if ((i &= PE_LEN_MASK) > sizeof (PElementLen)) {
#ifdef DEBUG
		        DLOG (psap_log, LLOG_DEBUG,
			      ("c (%x) i (%x) %d is %x %x %x %x\n",
			       c, i, sizeof(PElementLen),
			       *(Qb->qb_data-1),
			       *(Qb->qb_data-2),
			       *(Qb->qb_data-3),
			       *(Qb->qb_data-4)));
			qbprintf();
#endif
			*result = PS_ERR_OVERLEN;
			return (NULLPE);
		}

		if (i) {
			for (j = 0; i-- > 0;) {
				qbuf2char(c);
				j = (j << 8) | (c & 0xff);
			}
			len = j;
		}
		else
			len = PE_LEN_INDF;
	}
	else
		len = i;

	SLOG (psap_log, LLOG_DEBUG, NULLCP, ("len=%d", len));
	pe -> pe_len = len;

	/* Now get the value.  */

	switch (pe -> pe_form) {
	case PE_FORM_PRIM: 
		if (len == PE_LEN_INDF) {
			*result = PS_ERR_INDF;
			goto you_lose;
		}
		if (len > 0) {
			qp = Qb;
			if (qp->qb_len >= len) {
				pe->pe_prim = (PElementData) qp->qb_data;
				pe->pe_inline = 1;
				Qbrefs++;
				Byteno += len;
				if (qp->qb_len == len) {
					if(!(Qb = qp->qb_forw))
						abort();
				}
				else {
					qp->qb_len -= len;
					qp->qb_data += len;
				}
			}
			else {
				if ((pe->pe_prim = PEDalloc (len)) == NULLPED) {
					*result = PS_ERR_NMEM;
					goto you_lose;
				}
				if (qbuf2data(pe->pe_prim, len) != len) {
					SLOG (psap_log, LLOG_EXCEPTIONS, NULLCP,
						    ("bad qbuf lenght=%d",len));
					*result = PS_ERR_EOF;
					goto you_lose;
				}
			}
		}
		break;

	case PE_FORM_CONS: 
		if ((len == PE_LEN_INDF || len > 0) && 
			qb_read_cons (&pe -> pe_cons, len, result) == NOTOK)
				goto you_lose;
		break;
	}

	return pe;

you_lose: ;
	if (psap_log -> ll_events & LLOG_PDUS) {
	    LLOG (psap_log, LLOG_PDUS, ("PE read thus far"));
	    pe2text (psap_log, pe, 1, *result);
	}
	
	pe_free (pe);
	return NULLPE;
}

/*  */

static	int  qb_read_cons (pe, len, cresult)
register PE	*pe;
register PElementLen len;
register int *cresult;
{
	register int    cc;
	register PE    p, q;
	int result;

	*pe = NULLPE;
	cc = Byteno + len;

	if ((p = qbuf2pe_f (&result)) == NULLPE) {
no_cons: ;
#ifdef	DEBUG
		if (len == PE_LEN_INDF)
			LLOG (psap_log, LLOG_DEBUG,
			  ("error building indefinite cons, %s",
				   ps_error (result)));
		else
			LLOG (psap_log, LLOG_DEBUG,
			  ("error building cons, stream at %d, wanted %d: %s",
				   Byteno, cc, ps_error (result)));
#endif

		*cresult = result;
		return NOTOK;
	}
	*pe = p;

	if (len == PE_LEN_INDF) {
		if (p->pe_class == PE_CLASS_UNIV && p->pe_id == PE_UNIV_EOC) {
			pe_free (p);
			*pe = NULLPE;
			return OK;
		}

		for(q = p; p = qbuf2pe_f(&result); q = q->pe_next = p) {
			if (p->pe_class == PE_CLASS_UNIV &&
						p->pe_id == PE_UNIV_EOC) {
				pe_free (p);
				return OK;
			}
		}
		goto no_cons;
	}

	for (q = p;; q = q -> pe_next = p) {
		if (cc < Byteno) {
#ifdef DEBUG
			DLOG (psap_log, LLOG_DEBUG,
			      ("cc %d Byteno %d last length was %d\n",
			       cc, Byteno));
			qbprintf();
#endif
			*cresult = PS_ERR_LEN;
			return (NOTOK);
		}
		if (cc == Byteno) {
			return OK;
		}
		if ((p = qbuf2pe_f (&result)) == NULLPE)
			goto no_cons;
	}
}

/*  */

static	qbuf2data(data, len)
PElementData data;
PElementLen len;
{
	register struct qbuf *qp;
	register int i, cc;

	for (qp = Qb, cc = 0; len > 0; data += i, cc += i, len -= i) {
		if (qp == Hqb)
			goto leave;

		i = min (qp -> qb_len, len);
		memcpy ((char *) data, qp -> qb_data, i);

		qp -> qb_len -= i;
		if (qp -> qb_len <= 0) {
			if(!(Qb = (qp = qp->qb_forw)))
				abort();
		}
		else
			qp->qb_data += i;
	}

leave:
	Byteno += cc;
	return cc;
}

/*  */

#ifdef DEBUG
qbprintf()
{
	int len;
	struct qbuf *qb;
	char *cp;

	for(qb = Fqb, cp = qb->qb_data;qb != Hqb;qb = qb->qb_forw) {
		for(len = 0;len < qb->qb_len;cp++, len++) {
		    (void) ll_printf (psap_log, "%x ", *cp);
		    if((len % 15) == 0) (void) ll_printf(psap_log, "\n");
		}
	}
	(void) ll_sync (psap_log);
}
#endif