2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
10 * Encode INTEGER type using DER.
13 INTEGER_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
14 int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb,
16 const INTEGER_t *st = (const INTEGER_t *)sptr;
18 INTEGER_t effective_integer;
20 ASN_DEBUG("%s %s as INTEGER (tm=%d)",
21 cb?"Encoding":"Estimating", td->name, tag_mode);
24 * Canonicalize integer in the buffer.
25 * (Remove too long sign extension, remove some first 0x00 bytes)
28 uint8_t *buf = st->buf;
29 uint8_t *end1 = buf + st->size - 1;
32 /* Compute the number of superfluous leading bytes */
33 for(; buf < end1; buf++) {
35 * If the contents octets of an integer value encoding
36 * consist of more than one octet, then the bits of the
37 * first octet and bit 8 of the second octet:
38 * a) shall not all be ones; and
39 * b) shall not all be zero.
43 if((buf[1] & 0x80) == 0) continue;
46 if((buf[1] & 0x80)) continue;
52 /* Remove leading superfluous bytes from the integer */
53 shift = buf - st->buf;
59 unconst.c_buf = st->buf;
60 effective_integer.buf = unconst.nc_buf + shift;
61 effective_integer.size = st->size - shift;
63 st = &effective_integer;
67 rval = der_encode_primitive(td, st, tag_mode, tag, cb, app_key);
68 if(rval.structure_ptr == &effective_integer) {
69 rval.structure_ptr = sptr;