blob: 3058d095182ba29d3e4059150149a34d81ea521f [file] [log] [blame]
Harald Welte92c45f32010-06-12 18:59:38 +02001/*-
Harald Welteec0e2172010-07-20 00:03:44 +02002 * Copyright (c) 2003, 2004, 2006 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
Harald Welte92c45f32010-06-12 18:59:38 +02004 * Redistribution and modifications are permitted subject to BSD license.
5 */
6#include <asn_internal.h>
7#include <PrintableString.h>
8
9/*
Harald Welteec0e2172010-07-20 00:03:44 +020010 * ASN.1:1984 (X.409)
11 */
Harald Welte41b85d52015-08-31 08:56:53 +020012static const int _PrintableString_alphabet[256] = {
Harald Welteec0e2172010-07-20 00:03:44 +020013 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* */
14 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* */
15 1, 0, 0, 0, 0, 0, 0, 2, 3, 4, 0, 5, 6, 7, 8, 9, /* . '() +,-./ */
1610,11,12,13,14,15,16,17,18,19,20, 0, 0,21, 0,22, /* 0123456789: = ? */
17 0,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37, /* ABCDEFGHIJKLMNO */
1838,39,40,41,42,43,44,45,46,47,48, 0, 0, 0, 0, 0, /* PQRSTUVWXYZ */
19 0,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63, /* abcdefghijklmno */
2064,65,66,67,68,69,70,71,72,73,74, 0, 0, 0, 0, 0, /* pqrstuvwxyz */
21};
Harald Welte41b85d52015-08-31 08:56:53 +020022static const int _PrintableString_code2value[74] = {
Harald Welteec0e2172010-07-20 00:03:44 +02002332,39,40,41,43,44,45,46,47,48,49,50,51,52,53,54,
2455,56,57,58,61,63,65,66,67,68,69,70,71,72,73,74,
2575,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,
2697,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,
27113,114,115,116,117,118,119,120,121,122};
28
29/*
Harald Welte92c45f32010-06-12 18:59:38 +020030 * PrintableString basic type description.
31 */
Harald Welte41b85d52015-08-31 08:56:53 +020032static const ber_tlv_tag_t asn_DEF_PrintableString_tags[] = {
Harald Welte92c45f32010-06-12 18:59:38 +020033 (ASN_TAG_CLASS_UNIVERSAL | (19 << 2)), /* [UNIVERSAL 19] IMPLICIT ...*/
34 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
35};
Harald Welteec0e2172010-07-20 00:03:44 +020036static int asn_DEF_PrintableString_v2c(unsigned int value) {
37 return _PrintableString_alphabet[value > 255 ? 0 : value] - 1;
38}
39static int asn_DEF_PrintableString_c2v(unsigned int code) {
40 if(code < 74)
41 return _PrintableString_code2value[code];
42 return -1;
43}
Harald Welte41b85d52015-08-31 08:56:53 +020044static const asn_per_constraints_t asn_DEF_PrintableString_constraints = {
Harald Welteec0e2172010-07-20 00:03:44 +020045 { APC_CONSTRAINED, 4, 4, 0x20, 0x39 }, /* Value */
46 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
47 asn_DEF_PrintableString_v2c,
48 asn_DEF_PrintableString_c2v
49};
Harald Welte92c45f32010-06-12 18:59:38 +020050asn_TYPE_descriptor_t asn_DEF_PrintableString = {
51 "PrintableString",
52 "PrintableString",
53 OCTET_STRING_free,
54 OCTET_STRING_print_utf8, /* ASCII subset */
55 PrintableString_constraint,
56 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
57 OCTET_STRING_encode_der,
58 OCTET_STRING_decode_xer_utf8,
59 OCTET_STRING_encode_xer_utf8,
Harald Welteec0e2172010-07-20 00:03:44 +020060 OCTET_STRING_decode_uper,
61 OCTET_STRING_encode_uper,
Harald Welte41b85d52015-08-31 08:56:53 +020062 OCTET_STRING_decode_aper,
63 OCTET_STRING_encode_aper,
Harald Welte92c45f32010-06-12 18:59:38 +020064 0, /* Use generic outmost tag fetcher */
65 asn_DEF_PrintableString_tags,
66 sizeof(asn_DEF_PrintableString_tags)
67 / sizeof(asn_DEF_PrintableString_tags[0]) - 1,
68 asn_DEF_PrintableString_tags,
69 sizeof(asn_DEF_PrintableString_tags)
70 / sizeof(asn_DEF_PrintableString_tags[0]),
Harald Welteec0e2172010-07-20 00:03:44 +020071 &asn_DEF_PrintableString_constraints,
Harald Welte92c45f32010-06-12 18:59:38 +020072 0, 0, /* No members */
73 0 /* No specifics */
74};
75
76
Harald Welte92c45f32010-06-12 18:59:38 +020077int
78PrintableString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
79 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
80 const PrintableString_t *st = (const PrintableString_t *)sptr;
81
82 if(st && st->buf) {
83 uint8_t *buf = st->buf;
84 uint8_t *end = buf + st->size;
85
86 /*
87 * Check the alphabet of the PrintableString.
88 * ASN.1:1984 (X.409)
89 */
90 for(; buf < end; buf++) {
91 if(!_PrintableString_alphabet[*buf]) {
Harald Welteec0e2172010-07-20 00:03:44 +020092 _ASN_CTFAIL(app_key, td, sptr,
Harald Welte92c45f32010-06-12 18:59:38 +020093 "%s: value byte %ld (%d) "
94 "not in PrintableString alphabet "
95 "(%s:%d)",
96 td->name,
97 (long)((buf - st->buf) + 1),
98 *buf,
99 __FILE__, __LINE__);
100 return -1;
101 }
102 }
103 } else {
Harald Welteec0e2172010-07-20 00:03:44 +0200104 _ASN_CTFAIL(app_key, td, sptr,
Harald Welte92c45f32010-06-12 18:59:38 +0200105 "%s: value not given (%s:%d)",
106 td->name, __FILE__, __LINE__);
107 return -1;
108 }
109
110 return 0;
111}