summaryrefslogtreecommitdiffstats
path: root/lib/libaccess/attrec.cpp
blob: 8911e896b0b3f272638a0edbd0693f39b36489e0 (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
/** BEGIN COPYRIGHT BLOCK
 * Copyright 2001 Sun Microsystems, Inc.
 * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

/*
 * Description (attrec.c)
 *
 *	This module contains routines for encoding and decoding
 *	attribute records.  See attrec.h for a description of attribute
 *	records.
 */

#include "base/systems.h"
#include "netsite.h"
#include "assert.h"
#define __PRIVATE_ATTREC
#include "libaccess/attrec.h"

/*
 * Description (NTS_Length)
 *
 *	This function returns the length of a null-terminated string.
 *	The length includes the terminating null octet.
 *
 *	Use of the NTSLENGTH() macro is recommended (see attrec.h).
 *
 * Arguments:
 *
 *	nts			- a pointer to the null-terminate string
 *				  (may be null)
 *
 * Returns:
 *
 *	The length of the string.  If 'nts' is null, the value is one,
 *	since there is always a null octet.
 */

int NTS_Length(NTS_t nts)
{
    return ((nts) ? strlen((const char *)nts) + 1 : 1);
}

/*
 * Description (NTS_Decode)
 *
 *	This function decodes a null-terminated string from a specified
 *	attribute record buffer.  It copies the string into a dynamically
 *	allocated buffer, if 'pnts' is not null, and returns a pointer
 *	to it.  The return value of the function is a pointer to the
 *	octet following the NTS in the attribute record buffer.
 *
 *	Use of the NTSDECODE() macro is recommended (see attrec.h).
 *
 * Arguments:
 *
 *	cp			- pointer into the attribute record buffer
 *	pnts			- pointer to returned reference to decoded
 *				  NTS, or null, if the decoded NTS is not
 *				  to be copied to a dynamic buffer
 *
 * Returns:
 *
 *	The function return value is a pointer to the octet following
 *	the NTS in the attribute record buffer.  A pointer to a
 *	dynamically allocated buffer containing the decoded NTS will
 *	be returned through 'pnts', if it is non-null.  This returned
 *	pointer will be null if the NTS contains only a terminating
 *	octet.
 */

ATR_t NTS_Decode(ATR_t cp, NTS_t * pnts)
{
    NTS_t nts = 0;
    int len = NTSLENGTH(cp);		/* length of the string */

    /* Are we going to return a copy of the string? */
    if (pnts) {

	/* Yes, is it more than just a null octet? */
	if (len > 1) {

	    /* Yes, allocate a buffer and copy the string to it */
	    nts = (NTS_t)MALLOC(len);
	    if (nts) {
		memcpy((void *)nts, (void *)cp, len);
	    }
	}

	/* Return a pointer to the copied string, or null */
	*pnts = nts;
    }

    /* Return pointer to octet after string */
    return cp + len;
}

/*
 * Description (NTS_Encode)
 *
 *	This function encodes a null-terminated string into a specified
 *	attribute record buffer.  It returns a pointer to the octet
 *	following the encoding.
 *
 *	Use of the NTSENCODE() macro is recommended (see attrec.h).
 *
 * Arguments:
 *
 *	cp			- pointer into the attribute record buffer
 *	nts			- pointer to the string to be encoded
 *
 * Returns:
 *
 *	A pointer to the octet following the encoding in the attribute
 *	record buffer is returned.
 */

ATR_t NTS_Encode(ATR_t cp, NTS_t nts)
{

    /* Is the string pointer null? */
    if (nts) {
	int len = NTSLENGTH(nts);

	/* No, copy the string to the attribute record buffer */
	memcpy((void *)cp, (void *)nts, len);

	/* Get pointer to octet after it */
	cp += len;
    }
    else {

	/* A null pointer indicates an empty NTS, i.e. just a null octet */
	*cp++ = 0;
    }

    /* Return a pointer to the octet after the encoding */
    return cp;
}

/*
 * Description (USI_Decode)
 *
 *	This function decodes an unsigned integer value from a specified
 *	attribute record buffer.
 *
 *	Use of the USIDECODE() macro is recommended (see attrec.h).
 *
 * Arguments:
 *
 *	cp			- pointer into the attribute record buffer
 *	pval			- pointer to returned integer value
 *
 * Returns:
 *
 *	If 'pval' is not null, the decoded integer value is returned
 *	in the referenced location.  The function return value is a
 *	pointer to the octet following the USI encoding in the attribute
 *	record buffer.
 */

ATR_t USI_Decode(ATR_t cp, USI_t * pval)
{
    int val;

    /* Is this a length value? */
    if (*(cp) & 0x80) {
	int i;
	int len;

	/* Yes, build the value from the indicated number of octets */
	len = *cp++ & 0x7;
	val = 0;
	for (i = 0; i < len; ++i) {
	    val <<= 8;
	    val |= (cp[i] & 0xff);
	}
	cp += len;
    }
    else {

	/* This octet is the value */
	val = *cp++;
    }

    /* Return the value if there's a place to put it */
    if (pval) *pval = val;

    /* Return a pointer to the next item in the attribute record */
    return cp;
}

/*
 * Description (USI_Encode)
 *
 *	This function encodes an unsigned integer value into a specified
 *	attribute record buffer.
 *
 *	Use of the USIENCODE() macro is recommended (see attrec.h).
 *
 * Arguments:
 *
 *	cp			- pointer into the attribute record buffer
 *	val			- the value to be encoded
 *
 * Returns:
 *
 *	A pointer to the octet following the generated encoding in the
 *	attribute record buffer is returned.
 */

ATR_t USI_Encode(ATR_t cp, USI_t val)
{
    /* Check size of value to be encoded */
    if (val <= 0x7f) *cp++ = val;
    else if (val <= 0xff) {
	/* Length plus 8-bit value */
	*cp++ = 0x81;
	*cp++ = val;
    }
    else if (val <= 0xffff) {
	/* Length plus 16-bit value */
	*cp++ = 0x82;
	cp[1] = val & 0xff;
	val >>= 8;
	cp[0] = val & 0xff;
	cp += 2;
    }
    else if (val <= 0xffffff) {
	/* Length plus 24-bit value */
	*cp++ = 0x83;
	cp[2] = val & 0xff;
	val >>= 8;
	cp[1] = val & 0xff;
	val >>= 8;
	cp[0] = val & 0xff;
	cp += 3;
    }
    else {
	/* Length plus 32-bit value */
	*cp++ = 0x84;
	cp[3] = val & 0xff;
	val >>= 8;
	cp[2] = val & 0xff;
	val >>= 8;
	cp[1] = val & 0xff;
	val >>= 8;
	cp[0] = val & 0xff;
	cp += 4;
    }

    /* Return a pointer to the next position in the attribute record */
    return cp;
}

/*
 * Description (USI_Insert)
 *
 *	This function is a variation of USI_Encode() that always generates
 *	the maximum-length encoding for USI value, regardless of the
 *	actual specified value.  For arguments, returns, see USI_Encode().
 *
 *	Use of the USIINSERT() macro is recommended.  The USIALLOC() macro
 *	returns the number of octets that USIINSERT() will generate.
 */

ATR_t USI_Insert(ATR_t cp, USI_t val)
{
    int i;

    assert(USIALLOC() == 5);

    *cp++ = 0x84;
    for (i = 3; i >= 0; --i) {
	cp[i] = val & 0xff;
	val >>= 8;
    }

    return cp + 5;
}

/*
 * Description (USI_Length)
 *
 *	This function returns the number of octets required to encode
 *	an unsigned integer value.
 *
 *	Use of the USILENGTH() macro is recommended (see attrec.h).
 *
 * Arguments:
 *
 *	val			- the unsigned integer value
 *
 * Returns:
 *
 *	The number of octets required to encode the specified value is
 *	returned.
 */

int USI_Length(USI_t val)
{
    return (((USI_t)(val) <= 0x7f) ? 1
				   : (((USI_t)(val) <= 0xff) ? 2
				   : (((USI_t)(val) <= 0xffff) ? 3
				   : (((USI_t)(val) <= 0xffffff) ? 4
				   : 5))));
}