Lines Matching +full:elm +full:- +full:id
50 static ssize_t ber_read_element(struct ber *ber, struct ber_element *elm);
64 struct ber_element *elm; in ber_get_element() local
66 if ((elm = calloc(1, sizeof(*elm))) == NULL) in ber_get_element()
69 elm->be_encoding = encoding; in ber_get_element()
70 ber_set_header(elm, BER_CLASS_UNIVERSAL, BER_TYPE_DEFAULT); in ber_get_element()
72 return elm; in ber_get_element()
76 ber_set_header(struct ber_element *elm, int class, unsigned long type) in ber_set_header() argument
78 elm->be_class = class & BER_CLASS_MASK; in ber_set_header()
80 type = elm->be_encoding; in ber_set_header()
81 elm->be_type = type; in ber_set_header()
85 ber_link_elements(struct ber_element *prev, struct ber_element *elm) in ber_link_elements() argument
88 if ((prev->be_encoding == BER_TYPE_SEQUENCE || in ber_link_elements()
89 prev->be_encoding == BER_TYPE_SET) && in ber_link_elements()
90 prev->be_sub == NULL) in ber_link_elements()
91 prev->be_sub = elm; in ber_link_elements()
93 prev->be_next = elm; in ber_link_elements()
100 struct ber_element *elm; in ber_unlink_elements() local
102 if ((prev->be_encoding == BER_TYPE_SEQUENCE || in ber_unlink_elements()
103 prev->be_encoding == BER_TYPE_SET) && in ber_unlink_elements()
104 prev->be_sub != NULL) { in ber_unlink_elements()
105 elm = prev->be_sub; in ber_unlink_elements()
106 prev->be_sub = NULL; in ber_unlink_elements()
108 elm = prev->be_next; in ber_unlink_elements()
109 prev->be_next = NULL; in ber_unlink_elements()
112 return (elm); in ber_unlink_elements()
132 struct ber_element *elm; in ber_add_sequence() local
134 if ((elm = ber_get_element(BER_TYPE_SEQUENCE)) == NULL) in ber_add_sequence()
137 ber_link_elements(prev, elm); in ber_add_sequence()
139 return elm; in ber_add_sequence()
145 struct ber_element *elm; in ber_add_set() local
147 if ((elm = ber_get_element(BER_TYPE_SET)) == NULL) in ber_add_set()
150 ber_link_elements(prev, elm); in ber_add_set()
152 return elm; in ber_add_set()
158 struct ber_element *elm; in ber_add_enumerated() local
162 if ((elm = ber_get_element(BER_TYPE_ENUMERATED)) == NULL) in ber_add_enumerated()
165 elm->be_numeric = val; in ber_add_enumerated()
177 elm->be_len = len + 1; in ber_add_enumerated()
179 ber_link_elements(prev, elm); in ber_add_enumerated()
181 return elm; in ber_add_enumerated()
187 struct ber_element *elm; in ber_add_integer() local
191 if ((elm = ber_get_element(BER_TYPE_INTEGER)) == NULL) in ber_add_integer()
194 elm->be_numeric = val; in ber_add_integer()
206 elm->be_len = len + 1; in ber_add_integer()
208 ber_link_elements(prev, elm); in ber_add_integer()
210 return elm; in ber_add_integer()
214 ber_get_integer(struct ber_element *elm, long long *n) in ber_get_integer() argument
216 if (elm->be_encoding != BER_TYPE_INTEGER) in ber_get_integer()
217 return -1; in ber_get_integer()
219 *n = elm->be_numeric; in ber_get_integer()
224 ber_get_enumerated(struct ber_element *elm, long long *n) in ber_get_enumerated() argument
226 if (elm->be_encoding != BER_TYPE_ENUMERATED) in ber_get_enumerated()
227 return -1; in ber_get_enumerated()
229 *n = elm->be_numeric; in ber_get_enumerated()
237 struct ber_element *elm; in ber_add_boolean() local
239 if ((elm = ber_get_element(BER_TYPE_BOOLEAN)) == NULL) in ber_add_boolean()
242 elm->be_numeric = bool ? 0xff : 0; in ber_add_boolean()
243 elm->be_len = 1; in ber_add_boolean()
245 ber_link_elements(prev, elm); in ber_add_boolean()
247 return elm; in ber_add_boolean()
251 ber_get_boolean(struct ber_element *elm, int *b) in ber_get_boolean() argument
253 if (elm->be_encoding != BER_TYPE_BOOLEAN) in ber_get_boolean()
254 return -1; in ber_get_boolean()
256 *b = !(elm->be_numeric == 0); in ber_get_boolean()
269 struct ber_element *elm; in ber_add_nstring() local
274 if ((elm = ber_get_element(BER_TYPE_OCTETSTRING)) == NULL) { in ber_add_nstring()
280 elm->be_val = string; in ber_add_nstring()
281 elm->be_len = len; in ber_add_nstring()
282 elm->be_free = 1; /* free string on cleanup */ in ber_add_nstring()
284 ber_link_elements(prev, elm); in ber_add_nstring()
286 return elm; in ber_add_nstring()
290 ber_get_string(struct ber_element *elm, char **s) in ber_get_string() argument
292 if (elm->be_encoding != BER_TYPE_OCTETSTRING) in ber_get_string()
293 return -1; in ber_get_string()
295 *s = elm->be_val; in ber_get_string()
300 ber_get_nstring(struct ber_element *elm, void **p, size_t *len) in ber_get_nstring() argument
302 if (elm->be_encoding != BER_TYPE_OCTETSTRING) in ber_get_nstring()
303 return -1; in ber_get_nstring()
305 *p = elm->be_val; in ber_get_nstring()
306 *len = elm->be_len; in ber_get_nstring()
313 struct ber_element *elm; in ber_add_bitstring() local
318 if ((elm = ber_get_element(BER_TYPE_BITSTRING)) == NULL) { in ber_add_bitstring()
324 elm->be_val = v; in ber_add_bitstring()
325 elm->be_len = len; in ber_add_bitstring()
326 elm->be_free = 1; /* free string on cleanup */ in ber_add_bitstring()
328 ber_link_elements(prev, elm); in ber_add_bitstring()
330 return elm; in ber_add_bitstring()
334 ber_get_bitstring(struct ber_element *elm, void **v, size_t *len) in ber_get_bitstring() argument
336 if (elm->be_encoding != BER_TYPE_BITSTRING) in ber_get_bitstring()
337 return -1; in ber_get_bitstring()
339 *v = elm->be_val; in ber_get_bitstring()
340 *len = elm->be_len; in ber_get_bitstring()
347 struct ber_element *elm; in ber_add_null() local
349 if ((elm = ber_get_element(BER_TYPE_NULL)) == NULL) in ber_add_null()
352 ber_link_elements(prev, elm); in ber_add_null()
354 return elm; in ber_add_null()
358 ber_get_null(struct ber_element *elm) in ber_get_null() argument
360 if (elm->be_encoding != BER_TYPE_NULL) in ber_get_null()
361 return -1; in ber_get_null()
369 struct ber_element *elm; in ber_add_eoc() local
371 if ((elm = ber_get_element(BER_TYPE_EOC)) == NULL) in ber_add_eoc()
374 ber_link_elements(prev, elm); in ber_add_eoc()
376 return elm; in ber_add_eoc()
380 ber_get_eoc(struct ber_element *elm) in ber_get_eoc() argument
382 if (elm->be_encoding != BER_TYPE_EOC) in ber_get_eoc()
383 return -1; in ber_get_eoc()
394 if (o->bo_n < BER_MIN_OID_LEN || o->bo_n > BER_MAX_OID_LEN || in ber_oid2ber()
395 o->bo_id[0] > 2 || o->bo_id[1] > 40) in ber_oid2ber()
398 v = (o->bo_id[0] * 40) + o->bo_id[1]; in ber_oid2ber()
399 for (i = 2, j = 0; i <= o->bo_n; v = o->bo_id[i], i++) { in ber_oid2ber()
400 for (k = 28; k >= 7; k -= 7) { in ber_oid2ber()
422 return (-1); in ber_string2oid()
425 /* Parse OID strings in the common forms n.n.n, n_n_n_n, or n-n-n */ in ber_string2oid()
427 if ((p = strpbrk(p, "._-")) != NULL) in ber_string2oid()
429 o->bo_id[o->bo_n++] = strtonum(sp, 0, UINT_MAX, &errstr); in ber_string2oid()
430 if (errstr || o->bo_n > BER_MAX_OID_LEN) in ber_string2oid()
431 return (-1); in ber_string2oid()
440 struct ber_element *elm; in ber_add_oid() local
444 if ((elm = ber_get_element(BER_TYPE_OBJECT)) == NULL) in ber_add_oid()
453 elm->be_val = buf; in ber_add_oid()
454 elm->be_len = len; in ber_add_oid()
455 elm->be_free = 1; in ber_add_oid()
460 ber_link_elements(prev, elm); in ber_add_oid()
462 return (elm); in ber_add_oid()
465 ber_free_elements(elm); in ber_add_oid()
477 bcopy(&o->bo_id, &no.bo_id, sizeof(no.bo_id)); in ber_add_noid()
487 if (ber_string2oid(oidstr, &o) == -1) in ber_add_oidstring()
494 ber_get_oid(struct ber_element *elm, struct ber_oid *o) in ber_get_oid() argument
499 if (elm->be_encoding != BER_TYPE_OBJECT) in ber_get_oid()
500 return (-1); in ber_get_oid()
502 buf = elm->be_val; in ber_get_oid()
503 len = elm->be_len; in ber_get_oid()
506 return (-1); in ber_get_oid()
509 o->bo_id[j++] = buf[i] / 40; in ber_get_oid()
510 o->bo_id[j++] = buf[i++] % 40; in ber_get_oid()
512 o->bo_id[j] = (o->bo_id[j] << 7) + (buf[i] & ~0x80); in ber_get_oid()
517 o->bo_n = j; in ber_get_oid()
632 int *d, level = -1; in ber_scanf_elements()
649 if (ber_get_bitstring(ber, ptr, len) == -1) in ber_scanf_elements()
655 if (ber_get_boolean(ber, d) == -1) in ber_scanf_elements()
666 if (ber_get_enumerated(ber, i) == -1) in ber_scanf_elements()
672 if (ber_get_integer(ber, i) == -1) in ber_scanf_elements()
678 if (ber_get_oid(ber, o) == -1) in ber_scanf_elements()
687 if (ber_get_string(ber, s) == -1) in ber_scanf_elements()
694 *d = ber->be_class; in ber_scanf_elements()
695 *t = ber->be_type; in ber_scanf_elements()
701 if (ber_get_nstring(ber, ptr, len) == -1) in ber_scanf_elements()
706 if (ber->be_encoding != BER_TYPE_NULL) in ber_scanf_elements()
711 if (ber->be_encoding != BER_TYPE_EOC) in ber_scanf_elements()
717 if (ber->be_encoding != BER_TYPE_SEQUENCE && in ber_scanf_elements()
718 ber->be_encoding != BER_TYPE_SET) in ber_scanf_elements()
720 if (ber->be_sub == NULL || level >= _MAX_SEQ-1) in ber_scanf_elements()
723 ber = ber->be_sub; in ber_scanf_elements()
730 ber = parent[level--]; in ber_scanf_elements()
737 if (ber->be_next == NULL) in ber_scanf_elements()
739 ber = ber->be_next; in ber_scanf_elements()
742 return (ret == n ? 0 : -1); in ber_scanf_elements()
746 return (-1); in ber_scanf_elements()
759 * -1 on failure and sets errno
770 if (ber->br_wbuf != NULL && ber->br_wbuf + len > ber->br_wend) { in ber_write_elements()
771 free(ber->br_wbuf); in ber_write_elements()
772 ber->br_wbuf = NULL; in ber_write_elements()
774 if (ber->br_wbuf == NULL) { in ber_write_elements()
775 if ((ber->br_wbuf = malloc(len)) == NULL) in ber_write_elements()
776 return -1; in ber_write_elements()
777 ber->br_wend = ber->br_wbuf + len; in ber_write_elements()
781 ber->br_wptr = ber->br_wbuf; in ber_write_elements()
783 if (ber_dump_element(ber, root) == -1) in ber_write_elements()
784 return -1; in ber_write_elements()
787 if (ber->fd != -1) in ber_write_elements()
788 return write(ber->fd, ber->br_wbuf, len); in ber_write_elements()
807 ber_read_elements(struct ber *ber, struct ber_element *elm) in ber_read_elements() argument
809 struct ber_element *root = elm; in ber_read_elements()
818 if (ber_read_element(ber, root) == -1) { in ber_read_elements()
820 if (elm == NULL) in ber_read_elements()
831 if (root->be_sub && (root->be_encoding == BER_TYPE_SEQUENCE || in ber_free_elements()
832 root->be_encoding == BER_TYPE_SET)) in ber_free_elements()
833 ber_free_elements(root->be_sub); in ber_free_elements()
834 if (root->be_next) in ber_free_elements()
835 ber_free_elements(root->be_next); in ber_free_elements()
836 if (root->be_free && (root->be_encoding == BER_TYPE_OCTETSTRING || in ber_free_elements()
837 root->be_encoding == BER_TYPE_BITSTRING || in ber_free_elements()
838 root->be_encoding == BER_TYPE_OBJECT)) in ber_free_elements()
839 free(root->be_val); in ber_free_elements()
851 if (root->be_sub && (root->be_encoding == BER_TYPE_SEQUENCE || in ber_calc_len()
852 root->be_encoding == BER_TYPE_SET)) in ber_calc_len()
853 root->be_len = ber_calc_len(root->be_sub); in ber_calc_len()
856 if (root->be_type > BER_TYPE_SINGLE_MAX) in ber_calc_len()
857 for (t = root->be_type; t > 0; t >>= 7) in ber_calc_len()
859 if (root->be_len >= BER_TAG_MORE) in ber_calc_len()
860 for (s = root->be_len; s > 0; s >>= 8) in ber_calc_len()
864 if (root->be_next) in ber_calc_len()
865 size += ber_calc_len(root->be_next); in ber_calc_len()
868 if (root->be_type == BER_TYPE_EOC && root->be_len == 0) in ber_calc_len()
871 return (root->be_len + size); in ber_calc_len()
887 switch (root->be_encoding) { in ber_dump_element()
891 l = (unsigned long long)root->be_numeric; in ber_dump_element()
892 for (i = root->be_len; i > 0; i--) { in ber_dump_element()
893 u = (l >> ((i - 1) * 8)) & 0xff; in ber_dump_element()
898 return -1; in ber_dump_element()
901 ber_write(ber, root->be_val, root->be_len); in ber_dump_element()
908 if (root->be_sub && ber_dump_element(ber, root->be_sub) == -1) in ber_dump_element()
909 return -1; in ber_dump_element()
913 if (root->be_next == NULL) in ber_dump_element()
915 return ber_dump_element(ber, root->be_next); in ber_dump_element()
921 u_char id = 0, t, buf[8]; in ber_dump_header() local
927 if (root->be_type <= BER_TYPE_SINGLE_MAX) { in ber_dump_header()
928 id = root->be_type | (root->be_class << BER_CLASS_SHIFT); in ber_dump_header()
929 if (root->be_encoding == BER_TYPE_SEQUENCE || in ber_dump_header()
930 root->be_encoding == BER_TYPE_SET) in ber_dump_header()
931 id |= BER_TYPE_CONSTRUCTED; in ber_dump_header()
933 ber_putc(ber, id); in ber_dump_header()
935 id = BER_TAG_MASK | (root->be_class << BER_CLASS_SHIFT); in ber_dump_header()
936 if (root->be_encoding == BER_TYPE_SEQUENCE || in ber_dump_header()
937 root->be_encoding == BER_TYPE_SET) in ber_dump_header()
938 id |= BER_TYPE_CONSTRUCTED; in ber_dump_header()
940 ber_putc(ber, id); in ber_dump_header()
942 for (t = 0, type = root->be_type; type > 0; type >>= 7) in ber_dump_header()
945 while (t-- > 0) { in ber_dump_header()
952 if (root->be_len < BER_TAG_MORE) { in ber_dump_header()
954 ber_putc(ber, root->be_len); in ber_dump_header()
956 for (t = 0, size = root->be_len; size > 0; size >>= 8) in ber_dump_header()
962 ber_putc(ber, buf[--t]); in ber_dump_header()
969 if (ber->br_wptr + 1 <= ber->br_wend) in ber_putc()
970 *ber->br_wptr = c; in ber_putc()
971 ber->br_wptr++; in ber_putc()
977 if (ber->br_wptr + len <= ber->br_wend) in ber_write()
978 bcopy(buf, ber->br_wptr, len); in ber_write()
979 ber->br_wptr += len; in ber_write()
992 if (ber_getc(b, &u) == -1) in get_id()
993 return -1; in get_id()
1004 if (ber_getc(b, &u) == -1) in get_id()
1005 return -1; in get_id()
1012 return -1; in get_id()
1020 * extract length of a ber object -- if length is unknown an error is returned.
1028 if (ber_getc(b, &u) == -1) in get_len()
1029 return -1; in get_len()
1039 return -1; in get_len()
1043 for (s = 0; n > 0; n--) { in get_len()
1044 if (ber_getc(b, &u) == -1) in get_len()
1045 return -1; in get_len()
1052 return -1; in get_len()
1058 return -1; in get_len()
1066 ber_read_element(struct ber *ber, struct ber_element *elm) in ber_read_element() argument
1075 if ((r = get_id(ber, &type, &class, &cstruct)) == -1) in ber_read_element()
1076 return -1; in ber_read_element()
1080 if ((r = get_len(ber, &len)) == -1) in ber_read_element()
1081 return -1; in ber_read_element()
1089 if (ber->fd == -1 && len > ber->br_rend - ber->br_rptr) { in ber_read_element()
1091 return -1; in ber_read_element()
1094 elm->be_type = type; in ber_read_element()
1095 elm->be_len = len; in ber_read_element()
1096 elm->be_class = class; in ber_read_element()
1098 if (elm->be_encoding == 0) { in ber_read_element()
1101 elm->be_encoding = BER_TYPE_SEQUENCE; in ber_read_element()
1103 elm->be_encoding = type; in ber_read_element()
1104 else if (ber->br_application != NULL) { in ber_read_element()
1110 elm->be_encoding = (*ber->br_application)(elm); in ber_read_element()
1113 elm->be_encoding = BER_TYPE_NULL; in ber_read_element()
1116 switch (elm->be_encoding) { in ber_read_element()
1117 case BER_TYPE_EOC: /* End-Of-Content */ in ber_read_element()
1123 return -1; in ber_read_element()
1126 return -1; in ber_read_element()
1132 if (val >> ((i - 1) * 8) & 0x80) in ber_read_element()
1134 elm->be_numeric = val; in ber_read_element()
1137 elm->be_val = malloc(len); in ber_read_element()
1138 if (elm->be_val == NULL) in ber_read_element()
1139 return -1; in ber_read_element()
1140 elm->be_free = 1; in ber_read_element()
1141 elm->be_len = len; in ber_read_element()
1142 ber_read(ber, elm->be_val, len); in ber_read_element()
1146 elm->be_val = malloc(len + 1); in ber_read_element()
1147 if (elm->be_val == NULL) in ber_read_element()
1148 return -1; in ber_read_element()
1149 elm->be_free = 1; in ber_read_element()
1150 elm->be_len = len; in ber_read_element()
1151 ber_read(ber, elm->be_val, len); in ber_read_element()
1152 ((u_char *)elm->be_val)[len] = '\0'; in ber_read_element()
1156 return -1; in ber_read_element()
1160 if (elm->be_sub == NULL) { in ber_read_element()
1161 if ((elm->be_sub = ber_get_element(0)) == NULL) in ber_read_element()
1162 return -1; in ber_read_element()
1164 next = elm->be_sub; in ber_read_element()
1167 if (r == -1) in ber_read_element()
1168 return -1; in ber_read_element()
1169 len -= r; in ber_read_element()
1170 if (len > 0 && next->be_next == NULL) { in ber_read_element()
1171 if ((next->be_next = ber_get_element(0)) == in ber_read_element()
1173 return -1; in ber_read_element()
1175 next = next->be_next; in ber_read_element()
1188 if (b->br_rbuf == NULL) in ber_readbuf()
1189 return -1; in ber_readbuf()
1191 sz = b->br_rend - b->br_rptr; in ber_readbuf()
1195 return (-1); /* end of buffer and parser wants more data */ in ber_readbuf()
1198 bcopy(b->br_rptr, buf, len); in ber_readbuf()
1199 b->br_rptr += len; in ber_readbuf()
1207 b->br_rbuf = b->br_rptr = buf; in ber_set_readbuf()
1208 b->br_rend = (u_int8_t *)buf + len; in ber_set_readbuf()
1214 if (b->br_wbuf == NULL) in ber_get_writebuf()
1215 return -1; in ber_get_writebuf()
1216 *buf = b->br_wbuf; in ber_get_writebuf()
1217 return (b->br_wend - b->br_wbuf); in ber_get_writebuf()
1223 b->br_application = cb; in ber_set_application()
1229 free(b->br_wbuf); in ber_free()
1241 if (b->fd == -1) in ber_getc()
1244 r = read(b->fd, c, 1); in ber_getc()
1261 if (ber->fd == -1) in ber_read()
1264 r = read(ber->fd, b, remain); in ber_read()
1265 if (r == -1) { in ber_read()
1268 return -1; in ber_read()
1271 return (b - (u_char *)buf); in ber_read()
1273 remain -= r; in ber_read()
1275 return (b - (u_char *)buf); in ber_read()