vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | #include <UTF8String.h> |
| 6 | |
| 7 | /* |
| 8 | * UTF8String basic type description. |
| 9 | */ |
| 10 | static ber_tlv_tag_t asn1_DEF_UTF8String_tags[] = { |
| 11 | (ASN_TAG_CLASS_UNIVERSAL | (12 << 2)) |
| 12 | }; |
| 13 | asn1_TYPE_descriptor_t asn1_DEF_UTF8String = { |
| 14 | "UTF8String", |
| 15 | UTF8String_constraint, /* Check for invalid codes, etc. */ |
| 16 | OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */ |
| 17 | OCTET_STRING_encode_der, /* Implemented in terms of OCTET STRING */ |
| 18 | UTF8String_print, |
| 19 | OCTET_STRING_free, |
| 20 | 0, /* Use generic outmost tag fetcher */ |
| 21 | asn1_DEF_UTF8String_tags, |
| 22 | sizeof(asn1_DEF_UTF8String_tags) |
| 23 | / sizeof(asn1_DEF_UTF8String_tags[0]), |
| 24 | 1, /* Single UNIVERSAL tag may be implicitly overriden */ |
| 25 | -1, /* Both ways are fine */ |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 26 | 0, 0, /* No members */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 27 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | static int _UTF8String_h1[16] = { |
| 31 | 1, 1, 1, 1, 1, 1, 1, 1, /* 0x0 ... 0x7 */ |
| 32 | 0, 0, 0, 0, 2, 2, 3, -1 |
| 33 | }; |
| 34 | static int _UTF8String_h2[16] = { |
| 35 | 4, 4, 4, 4, 4, 4, 4, 4, /* 0xF0 .. 0xF7 */ |
| 36 | 5, 5, 5, 5, 6, 6, -1, -1 |
| 37 | }; |
| 38 | |
| 39 | int |
| 40 | UTF8String_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 41 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
| 42 | ssize_t len; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 43 | len = UTF8String_length((const UTF8String_t *)sptr, td->name, |
| 44 | app_errlog, app_key); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 45 | if(len > 0) len = 0; |
| 46 | return len; |
| 47 | } |
| 48 | |
| 49 | ssize_t |
| 50 | UTF8String_length(const UTF8String_t *st, const char *opt_type_name, |
| 51 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
| 52 | |
| 53 | if(st && st->buf) { |
| 54 | size_t length = 0; |
| 55 | uint8_t *buf = st->buf; |
| 56 | uint8_t *end = buf + st->size; |
| 57 | int want; /* Number of bytes wanted */ |
| 58 | |
| 59 | for(want = 0; buf < end; buf++) { |
| 60 | uint8_t ch = *buf; |
| 61 | int w = _UTF8String_h1[ch >> 4]; |
| 62 | if(want) { /* Continuation expected */ |
| 63 | if(w) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 64 | _ASN_ERRLOG(app_errlog, app_key, |
| 65 | "%s: UTF-8 expectation " |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 66 | "failed at byte %d (%s:%d)", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 67 | opt_type_name, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 68 | (buf - st->buf) + 1, |
| 69 | __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 70 | return -1; |
| 71 | } |
| 72 | want--; |
| 73 | } else { |
| 74 | switch(w) { |
| 75 | case -1: /* Long UTF-8 */ |
| 76 | w = _UTF8String_h2[ch & 0xF0]; |
| 77 | if(w != -1) |
| 78 | break; |
| 79 | /* Fall through */ |
| 80 | case 0: |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 81 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 82 | "%s: UTF-8 expectation" |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 83 | "failed at byte %d (%s:%d)", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 84 | opt_type_name, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 85 | (buf - st->buf) + 1, |
| 86 | __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 87 | return -1; |
| 88 | } |
| 89 | want = w - 1; /* Expect this much */ |
| 90 | } |
| 91 | if(!want) length++; |
| 92 | } |
| 93 | |
| 94 | /* If still want something, then something is wrong */ |
| 95 | if(want) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 96 | _ASN_ERRLOG(app_errlog, app_key, |
vlm | 758530a | 2004-08-22 13:47:59 +0000 | [diff] [blame] | 97 | "%s: truncated UTF-8 sequence (%s:%d)", |
| 98 | opt_type_name, __FILE__, __LINE__); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | return length; |
| 103 | } else { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 104 | _ASN_ERRLOG(app_errlog, app_key, |
| 105 | "%s: value not given", opt_type_name); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 106 | return -1; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | int |
| 111 | UTF8String_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 112 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 113 | const UTF8String_t *st = (const UTF8String_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 114 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 115 | (void)td; /* Unused argument */ |
| 116 | (void)ilevel; /* Unused argument */ |
| 117 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 118 | if(st && st->buf) { |
| 119 | return cb(st->buf, st->size, app_key); |
| 120 | } else { |
| 121 | return cb("<absent>", 8, app_key); |
| 122 | } |
| 123 | } |