2 * FreeRDP: A Remote Desktop Protocol Client
3 * ASN.1 Packed Encoding Rules (BER)
5 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
25 * @param length length
29 boolean per_read_length(STREAM* s, uint16* length)
33 stream_read_uint8(s, byte);
38 *length = (byte << 8);
39 stream_read_uint8(s, byte);
53 * @param length length
56 void per_write_length(STREAM* s, int length)
59 stream_write_uint16_be(s, (length | 0x8000));
61 stream_write_uint8(s, length);
67 * @param choice choice
71 boolean per_read_choice(STREAM* s, uint8* choice)
73 stream_read_uint8(s, *choice);
80 * @param choice index of chosen field
83 void per_write_choice(STREAM* s, uint8 choice)
85 stream_write_uint8(s, choice);
91 * @param selection selection
95 boolean per_read_selection(STREAM* s, uint8* selection)
97 stream_read_uint8(s, *selection);
102 * Write PER selection for OPTIONAL fields.
104 * @param selection bit map of selected fields
107 void per_write_selection(STREAM* s, uint8 selection)
109 stream_write_uint8(s, selection);
113 * Read PER number of sets.
115 * @param number number of sets
119 boolean per_read_number_of_sets(STREAM* s, uint8* number)
121 stream_read_uint8(s, *number);
126 * Write PER number of sets for SET OF.
128 * @param number number of sets
131 void per_write_number_of_sets(STREAM* s, uint8 number)
133 stream_write_uint8(s, number);
137 * Read PER padding with zeros.
142 boolean per_read_padding(STREAM* s, int length)
144 stream_seek(s, length);
150 * Write PER padding with zeros.
155 void per_write_padding(STREAM* s, int length)
159 for (i = 0; i < length; i++)
160 stream_write_uint8(s, 0);
166 * @param integer integer
170 boolean per_read_integer(STREAM* s, uint32* integer)
174 per_read_length(s, &length);
177 stream_read_uint8(s, *integer);
178 else if (length == 2)
179 stream_read_uint16_be(s, *integer);
189 * @param integer integer
192 void per_write_integer(STREAM* s, uint32 integer)
196 per_write_length(s, 1);
197 stream_write_uint8(s, integer);
199 else if (integer <= 0xFFFF)
201 per_write_length(s, 2);
202 stream_write_uint16_be(s, integer);
204 else if (integer <= 0xFFFFFFFF)
206 per_write_length(s, 4);
207 stream_write_uint32_be(s, integer);
212 * Read PER INTEGER (uint16).
214 * @param integer integer
215 * @param min minimum value
219 boolean per_read_integer16(STREAM* s, uint16* integer, uint16 min)
221 stream_read_uint16_be(s, *integer);
223 if (*integer + min > 0xFFFF)
232 * Write PER INTEGER (uint16).
234 * @param integer integer
235 * @param min minimum value
238 void per_write_integer16(STREAM* s, uint16 integer, uint16 min)
240 stream_write_uint16_be(s, integer - min);
244 * Read PER ENUMERATED.
246 * @param enumerated enumerated
247 * @param count enumeration count
251 boolean per_read_enumerated(STREAM* s, uint8* enumerated, uint8 count)
253 stream_read_uint8(s, *enumerated);
255 /* check that enumerated value falls within expected range */
256 if (*enumerated + 1 > count)
263 * Write PER ENUMERATED.
265 * @param enumerated enumerated
266 * @param count enumeration count
270 void per_write_enumerated(STREAM* s, uint8 enumerated, uint8 count)
272 stream_write_uint8(s, enumerated);
276 * Read PER OBJECT_IDENTIFIER (OID).
278 * @param oid object identifier (OID)
282 boolean per_read_object_identifier(STREAM* s, uint8 oid[6])
289 per_read_length(s, &length); /* length */
294 stream_read_uint8(s, t12); /* first two tuples */
295 a_oid[0] = (t12 >> 4);
296 a_oid[1] = (t12 & 0x0F);
298 stream_read_uint8(s, a_oid[2]); /* tuple 3 */
299 stream_read_uint8(s, a_oid[3]); /* tuple 4 */
300 stream_read_uint8(s, a_oid[4]); /* tuple 5 */
301 stream_read_uint8(s, a_oid[5]); /* tuple 6 */
303 if ((a_oid[0] == oid[0]) && (a_oid[1] == oid[1]) &&
304 (a_oid[2] == oid[2]) && (a_oid[3] == oid[3]) &&
305 (a_oid[4] == oid[4]) && (a_oid[5] == oid[5]))
316 * Write PER OBJECT_IDENTIFIER (OID)
318 * @param oid object identifier (oid)
321 void per_write_object_identifier(STREAM* s, uint8 oid[6])
323 uint8 t12 = (oid[0] << 4) & (oid[1] & 0x0F);
324 stream_write_uint8(s, 5); /* length */
325 stream_write_uint8(s, t12); /* first two tuples */
326 stream_write_uint8(s, oid[2]); /* tuple 3 */
327 stream_write_uint8(s, oid[3]); /* tuple 4 */
328 stream_write_uint8(s, oid[4]); /* tuple 5 */
329 stream_write_uint8(s, oid[5]); /* tuple 6 */
336 * @param length string length
339 void per_write_string(STREAM* s, uint8* str, int length)
343 for (i = 0; i < length; i++)
344 stream_write_uint8(s, str[i]);
348 * Read PER OCTET_STRING.
350 * @param oct_str octet string
351 * @param length string length
352 * @param min minimum length
356 boolean per_read_octet_string(STREAM* s, uint8* oct_str, int length, int min)
362 per_read_length(s, &mlength);
364 if (mlength + min != length)
368 stream_seek(s, length);
370 for (i = 0; i < length; i++)
372 if (a_oct_str[i] != oct_str[i])
380 * Write PER OCTET_STRING
382 * @param oct_str octet string
383 * @param length string length
384 * @param min minimum string length
387 void per_write_octet_string(STREAM* s, uint8* oct_str, int length, int min)
392 mlength = (length - min >= 0) ? length - min : min;
394 per_write_length(s, mlength);
396 for (i = 0; i < length; i++)
397 stream_write_uint8(s, oct_str[i]);
401 * Read PER NumericString.
403 * @param num_str numeric string
404 * @param length string length
405 * @param min minimum string length
408 boolean per_read_numeric_string(STREAM* s, int min)
414 per_read_length(s, &mlength);
416 length = mlength + min;
418 for (i = 0; i < length; i += 2)
427 * Write PER NumericString.
429 * @param num_str numeric string
430 * @param length string length
431 * @param min minimum string length
434 void per_write_numeric_string(STREAM* s, uint8* num_str, int length, int min)
440 mlength = (length - min >= 0) ? length - min : min;
442 per_write_length(s, mlength);
444 for (i = 0; i < length; i += 2)
447 c2 = ((i + 1) < length) ? num_str[i + 1] : 0x30;
449 c1 = (c1 - 0x30) % 10;
450 c2 = (c2 - 0x30) % 10;
451 num = (c1 << 4) | c2;
453 stream_write_uint8(s, num); /* string */