blob: c8ee3ae3ce1fff2d26d7ef6c014f6eebe0557252 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin725883b2006-10-09 12:07:58 +00002 * Copyright (c) 2003, 2004, 2006 Lev Walkin <vlm@lionet.info>.
3 * All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00004 * Redistribution and modifications are permitted subject to BSD license.
5 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00006#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <PrintableString.h>
8
9/*
Lev Walkin725883b2006-10-09 12:07:58 +000010 * ASN.1:1984 (X.409)
11 */
12static int _PrintableString_alphabet[256] = {
13 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};
22static int _PrintableString_code2value[74] = {
2332,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/*
Lev Walkinf15320b2004-06-03 03:38:44 +000030 * PrintableString basic type description.
31 */
Lev Walkin5e033762004-09-29 13:26:15 +000032static ber_tlv_tag_t asn_DEF_PrintableString_tags[] = {
Lev Walkin188ed2c2004-09-13 08:31:01 +000033 (ASN_TAG_CLASS_UNIVERSAL | (19 << 2)), /* [UNIVERSAL 19] IMPLICIT ...*/
34 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
Lev Walkinf15320b2004-06-03 03:38:44 +000035};
Lev Walkin725883b2006-10-09 12:07:58 +000036static 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}
44static asn_per_constraints_t asn_DEF_PrintableString_constraints = {
45 { 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};
Lev Walkin5e033762004-09-29 13:26:15 +000050asn_TYPE_descriptor_t asn_DEF_PrintableString = {
Lev Walkinf15320b2004-06-03 03:38:44 +000051 "PrintableString",
Lev Walkindc06f6b2004-10-20 15:50:55 +000052 "PrintableString",
Lev Walkina9cc46e2004-09-22 16:06:28 +000053 OCTET_STRING_free,
Lev Walkindc06f6b2004-10-20 15:50:55 +000054 OCTET_STRING_print_utf8, /* ASCII subset */
Lev Walkinf15320b2004-06-03 03:38:44 +000055 PrintableString_constraint,
Lev Walkindc06f6b2004-10-20 15:50:55 +000056 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,
Lev Walkin725883b2006-10-09 12:07:58 +000060 OCTET_STRING_decode_uper,
61 OCTET_STRING_encode_uper,
Lev Walkinf15320b2004-06-03 03:38:44 +000062 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000063 asn_DEF_PrintableString_tags,
64 sizeof(asn_DEF_PrintableString_tags)
65 / sizeof(asn_DEF_PrintableString_tags[0]) - 1,
66 asn_DEF_PrintableString_tags,
67 sizeof(asn_DEF_PrintableString_tags)
68 / sizeof(asn_DEF_PrintableString_tags[0]),
Lev Walkin725883b2006-10-09 12:07:58 +000069 &asn_DEF_PrintableString_constraints,
Lev Walkin449f8322004-08-20 13:23:42 +000070 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000071 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000072};
73
74
Lev Walkinf15320b2004-06-03 03:38:44 +000075int
Lev Walkin5e033762004-09-29 13:26:15 +000076PrintableString_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkin1eded352006-07-13 11:19:01 +000077 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +000078 const PrintableString_t *st = (const PrintableString_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +000079
80 if(st && st->buf) {
81 uint8_t *buf = st->buf;
82 uint8_t *end = buf + st->size;
83
84 /*
85 * Check the alphabet of the PrintableString.
86 * ASN.1:1984 (X.409)
87 */
88 for(; buf < end; buf++) {
89 if(!_PrintableString_alphabet[*buf]) {
Lev Walkined9019a2006-10-16 12:18:41 +000090 _ASN_CTFAIL(app_key, td, sptr,
Lev Walkin426b0982004-10-26 09:59:42 +000091 "%s: value byte %ld (%d) "
Lev Walkin16835b62004-08-22 13:47:59 +000092 "not in PrintableString alphabet "
93 "(%s:%d)",
Lev Walkinf15320b2004-06-03 03:38:44 +000094 td->name,
Lev Walkin426b0982004-10-26 09:59:42 +000095 (long)((buf - st->buf) + 1),
Lev Walkin5a56c7c2004-08-23 08:34:14 +000096 *buf,
Lev Walkin16835b62004-08-22 13:47:59 +000097 __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +000098 return -1;
99 }
100 }
101 } else {
Lev Walkined9019a2006-10-16 12:18:41 +0000102 _ASN_CTFAIL(app_key, td, sptr,
Lev Walkin16835b62004-08-22 13:47:59 +0000103 "%s: value not given (%s:%d)",
104 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 return -1;
106 }
107
108 return 0;
109}