1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2024-11-17 23:34:56 +00:00

Update per_support files

This commit is contained in:
Carles Fernandez 2017-08-25 22:44:49 +02:00 committed by Damian Miralles
parent fe336fef20
commit ddee13cfa6
2 changed files with 426 additions and 326 deletions

View File

@ -12,12 +12,12 @@ per_data_string(asn_per_data_t *pd) {
static char buf[2][32]; static char buf[2][32];
static int n; static int n;
n = (n+1) % 2; n = (n+1) % 2;
snprintf(buf[n], sizeof(buf), snprintf(buf[n], sizeof(buf[n]),
"{m=%d span %+d[%d..%d] (%d)}", "{m=%ld span %+ld[%d..%d] (%d)}",
pd->moved, (long)pd->moved,
(((int)pd->buffer) & 0xf), (((long)pd->buffer) & 0xf),
pd->nboff, pd->nbits, (int)pd->nboff, (int)pd->nbits,
pd->nbits - pd->nboff); (int)(pd->nbits - pd->nboff));
return buf[n]; return buf[n];
} }
@ -49,7 +49,8 @@ per_get_few_bits(asn_per_data_t *pd, int nbits) {
int32_t tailv, vhead; int32_t tailv, vhead;
if(!pd->refill || nbits > 31) return -1; if(!pd->refill || nbits > 31) return -1;
/* Accumulate unused bytes before refill */ /* Accumulate unused bytes before refill */
ASN_DEBUG("Obtain the rest %d bits (want %d)", nleft, nbits); ASN_DEBUG("Obtain the rest %d bits (want %d)",
(int)nleft, (int)nbits);
tailv = per_get_few_bits(pd, nleft); tailv = per_get_few_bits(pd, nleft);
if(tailv < 0) return -1; if(tailv < 0) return -1;
/* Refill (replace pd contents with new data) */ /* Refill (replace pd contents with new data) */
@ -103,13 +104,13 @@ per_get_few_bits(asn_per_data_t *pd, int nbits) {
accum &= (((uint32_t)1 << nbits) - 1); accum &= (((uint32_t)1 << nbits) - 1);
ASN_DEBUG(" [PER got %2d<=%2d bits => span %d %+d[%d..%d]:%02x (%d) => 0x%x]", ASN_DEBUG(" [PER got %2d<=%2d bits => span %d %+ld[%d..%d]:%02x (%d) => 0x%x]",
nbits, nleft, (int)nbits, (int)nleft,
pd->moved, (int)pd->moved,
(((int)pd->buffer) & 0xf), (((long)pd->buffer) & 0xf),
pd->nboff, pd->nbits, (int)pd->nboff, (int)pd->nbits,
pd->buffer[0], ((pd->buffer != NULL)?pd->buffer[0]:0),
pd->nbits - pd->nboff, (int)(pd->nbits - pd->nboff),
(int)accum); (int)accum);
return accum; return accum;
@ -160,7 +161,8 @@ per_get_many_bits(asn_per_data_t *pd, uint8_t *dst, int alright, int nbits) {
} }
/* /*
* Get the length "n" from the stream. * X.691-201508 #10.9 General rules for encoding a length determinant.
* Get the optionally constrained length "n" from the stream.
*/ */
ssize_t ssize_t
uper_get_length(asn_per_data_t *pd, int ebits, int *repeat) { uper_get_length(asn_per_data_t *pd, int ebits, int *repeat) {
@ -168,20 +170,25 @@ uper_get_length(asn_per_data_t *pd, int ebits, int *repeat) {
*repeat = 0; *repeat = 0;
if(ebits >= 0) return per_get_few_bits(pd, ebits); /* #11.9.4.1 Encoding if constrained (according to effective bits) */
if(ebits >= 0 && ebits <= 16) {
return per_get_few_bits(pd, ebits);
}
value = per_get_few_bits(pd, 8); value = per_get_few_bits(pd, 8);
if(value < 0) return -1; if((value & 0x80) == 0) { /* #11.9.3.6 */
if((value & 128) == 0) /* #10.9.3.6 */
return (value & 0x7F); return (value & 0x7F);
if((value & 64) == 0) { /* #10.9.3.7 */ } else if((value & 0x40) == 0) { /* #11.9.3.7 */
value = ((value & 63) << 8) | per_get_few_bits(pd, 8); /* bit 8 ... set to 1 and bit 7 ... set to zero */
if(value < 0) return -1; value = ((value & 0x3f) << 8) | per_get_few_bits(pd, 8);
return value; return value; /* potential -1 from per_get_few_bits passes through. */
} } else if(value < 0) {
value &= 63; /* this is "m" from X.691, #10.9.3.8 */
if(value < 1 || value > 4)
return -1; return -1;
}
value &= 0x3f; /* this is "m" from X.691, #11.9.3.8 */
if(value < 1 || value > 4) {
return -1; /* Prohibited by #11.9.3.8 */
}
*repeat = 1; *repeat = 1;
return (16384 * value); return (16384 * value);
} }
@ -200,7 +207,7 @@ uper_get_nslength(asn_per_data_t *pd) {
if(per_get_few_bits(pd, 1) == 0) { if(per_get_few_bits(pd, 1) == 0) {
length = per_get_few_bits(pd, 6) + 1; length = per_get_few_bits(pd, 6) + 1;
if(length <= 0) return -1; if(length <= 0) return -1;
ASN_DEBUG("l=%d", length); ASN_DEBUG("l=%d", (int)length);
return length; return length;
} else { } else {
int repeat; int repeat;
@ -237,8 +244,8 @@ uper_get_nsnnwn(asn_per_data_t *pd) {
} }
/* /*
* Put the normally small non-negative whole number. * X.691-11/2008, #11.6
* X.691, #10.6 * Encoding of a normally small non-negative whole number
*/ */
int int
uper_put_nsnnwn(asn_per_outp_t *po, int n) { uper_put_nsnnwn(asn_per_outp_t *po, int n) {
@ -263,6 +270,79 @@ uper_put_nsnnwn(asn_per_outp_t *po, int n) {
} }
/* X.691-2008/11, #11.5.6 -> #11.3 */
int uper_get_constrained_whole_number(asn_per_data_t *pd, unsigned long *out_value, int nbits) {
unsigned long lhalf; /* Lower half of the number*/
long half;
if(nbits <= 31) {
half = per_get_few_bits(pd, nbits);
if(half < 0) return -1;
*out_value = half;
return 0;
}
if((size_t)nbits > 8 * sizeof(*out_value))
return -1; /* RANGE */
half = per_get_few_bits(pd, 31);
if(half < 0) return -1;
if(uper_get_constrained_whole_number(pd, &lhalf, nbits - 31))
return -1;
*out_value = ((unsigned long)half << (nbits - 31)) | lhalf;
return 0;
}
/* X.691-2008/11, #11.5.6 -> #11.3 */
int uper_put_constrained_whole_number_s(asn_per_outp_t *po, long v, int nbits) {
/*
* Assume signed number can be safely coerced into
* unsigned of the same range.
* The following testing code will likely be optimized out
* by compiler if it is true.
*/
unsigned long uvalue1 = ULONG_MAX;
long svalue = uvalue1;
unsigned long uvalue2 = svalue;
assert(uvalue1 == uvalue2);
return uper_put_constrained_whole_number_u(po, v, nbits);
}
int uper_put_constrained_whole_number_u(asn_per_outp_t *po, unsigned long v, int nbits) {
if(nbits <= 31) {
return per_put_few_bits(po, v, nbits);
} else {
/* Put higher portion first, followed by lower 31-bit */
if(uper_put_constrained_whole_number_u(po, v >> 31, nbits - 31))
return -1;
return per_put_few_bits(po, v, 31);
}
}
int
per_put_aligned_flush(asn_per_outp_t *po) {
uint32_t unused_bits = (0x7 & (8 - (po->nboff & 0x07)));
size_t complete_bytes =
(po->buffer ? po->buffer - po->tmpspace : 0) + ((po->nboff + 7) >> 3);
if(unused_bits) {
po->buffer[po->nboff >> 3] &= ~0 << unused_bits;
}
if(po->outper(po->tmpspace, complete_bytes, po->op_key) < 0) {
return -1;
} else {
po->buffer = po->tmpspace;
po->nboff = 0;
po->nbits = 8 * sizeof(po->tmpspace);
po->flushed_bytes += complete_bytes;
return 0;
}
}
/* /*
* Put a small number of bits (<= 31). * Put a small number of bits (<= 31).
*/ */
@ -275,7 +355,7 @@ per_put_few_bits(asn_per_outp_t *po, uint32_t bits, int obits) {
if(obits <= 0 || obits >= 32) return obits ? -1 : 0; if(obits <= 0 || obits >= 32) return obits ? -1 : 0;
ASN_DEBUG("[PER put %d bits %x to %p+%d bits]", ASN_DEBUG("[PER put %d bits %x to %p+%d bits]",
obits, (int)bits, po->buffer, po->nboff); obits, (int)bits, po->buffer, (int)po->nboff);
/* /*
* Normalize position indicator. * Normalize position indicator.
@ -290,9 +370,11 @@ per_put_few_bits(asn_per_outp_t *po, uint32_t bits, int obits) {
* Flush whole-bytes output, if necessary. * Flush whole-bytes output, if necessary.
*/ */
if(po->nboff + obits > po->nbits) { if(po->nboff + obits > po->nbits) {
int complete_bytes = (po->buffer - po->tmpspace); size_t complete_bytes;
ASN_DEBUG("[PER output %d complete + %d]", if(!po->buffer) po->buffer = po->tmpspace;
complete_bytes, po->flushed_bytes); complete_bytes = (po->buffer - po->tmpspace);
ASN_DEBUG("[PER output %ld complete + %ld]",
(long)complete_bytes, (long)po->flushed_bytes);
if(po->outper(po->tmpspace, complete_bytes, po->op_key) < 0) if(po->outper(po->tmpspace, complete_bytes, po->op_key) < 0)
return -1; return -1;
if(po->nboff) if(po->nboff)
@ -307,42 +389,47 @@ per_put_few_bits(asn_per_outp_t *po, uint32_t bits, int obits) {
*/ */
buf = po->buffer; buf = po->buffer;
omsk = ~((1 << (8 - po->nboff)) - 1); omsk = ~((1 << (8 - po->nboff)) - 1);
off = (po->nboff += obits); off = (po->nboff + obits);
/* Clear data of debris before meaningful bits */ /* Clear data of debris before meaningful bits */
bits &= (((uint32_t)1 << obits) - 1); bits &= (((uint32_t)1 << obits) - 1);
ASN_DEBUG("[PER out %d %u/%x (t=%d,o=%d) %x&%x=%x]", obits, ASN_DEBUG("[PER out %d %u/%x (t=%d,o=%d) %x&%x=%x]", obits,
(int)bits, (int)bits, (int)bits, (int)bits,
po->nboff - obits, off, buf[0], omsk&0xff, buf[0] & omsk); (int)po->nboff, (int)off,
buf[0], (int)(omsk&0xff),
(int)(buf[0] & omsk));
if(off <= 8) /* Completely within 1 byte */ if(off <= 8) /* Completely within 1 byte */
po->nboff = off,
bits <<= (8 - off), bits <<= (8 - off),
buf[0] = (buf[0] & omsk) | bits; buf[0] = (buf[0] & omsk) | bits;
else if(off <= 16) else if(off <= 16)
po->nboff = off,
bits <<= (16 - off), bits <<= (16 - off),
buf[0] = (buf[0] & omsk) | (bits >> 8), buf[0] = (buf[0] & omsk) | (bits >> 8),
buf[1] = bits; buf[1] = bits;
else if(off <= 24) else if(off <= 24)
po->nboff = off,
bits <<= (24 - off), bits <<= (24 - off),
buf[0] = (buf[0] & omsk) | (bits >> 16), buf[0] = (buf[0] & omsk) | (bits >> 16),
buf[1] = bits >> 8, buf[1] = bits >> 8,
buf[2] = bits; buf[2] = bits;
else if(off <= 31) else if(off <= 31)
po->nboff = off,
bits <<= (32 - off), bits <<= (32 - off),
buf[0] = (buf[0] & omsk) | (bits >> 24), buf[0] = (buf[0] & omsk) | (bits >> 24),
buf[1] = bits >> 16, buf[1] = bits >> 16,
buf[2] = bits >> 8, buf[2] = bits >> 8,
buf[3] = bits; buf[3] = bits;
else { else {
ASN_DEBUG("->[PER out split %d]", obits); if(per_put_few_bits(po, bits >> (obits - 24), 24)) return -1;
per_put_few_bits(po, bits >> 8, 24); if(per_put_few_bits(po, bits, obits - 24)) return -1;
per_put_few_bits(po, bits, obits - 24);
ASN_DEBUG("<-[PER out split %d]", obits);
} }
ASN_DEBUG("[PER out %u/%x => %02x buf+%d]", ASN_DEBUG("[PER out %u/%x => %02x buf+%ld]",
(int)bits, (int)bits, buf[0], po->buffer - po->tmpspace); (int)bits, (int)bits, buf[0],
(long)(po->buffer - po->tmpspace));
return 0; return 0;
} }
@ -422,4 +509,3 @@ uper_put_nslength(asn_per_outp_t *po, size_t length) {
return 0; return 0;
} }

View File

@ -15,7 +15,7 @@ extern "C" {
/* /*
* Pre-computed PER constraints. * Pre-computed PER constraints.
*/ */
typedef struct asn_per_constraint_s { typedef const struct asn_per_constraint_s {
enum asn_per_constraint_flags { enum asn_per_constraint_flags {
APC_UNCONSTRAINED = 0x0, /* No PER visible constraints */ APC_UNCONSTRAINED = 0x0, /* No PER visible constraints */
APC_SEMI_CONSTRAINED = 0x1, /* Constrained at "lb" */ APC_SEMI_CONSTRAINED = 0x1, /* Constrained at "lb" */
@ -27,9 +27,9 @@ typedef struct asn_per_constraint_s {
long lower_bound; /* "lb" value */ long lower_bound; /* "lb" value */
long upper_bound; /* "ub" value */ long upper_bound; /* "ub" value */
} asn_per_constraint_t; } asn_per_constraint_t;
typedef struct asn_per_constraints_s { typedef const struct asn_per_constraints_s {
asn_per_constraint_t value; struct asn_per_constraint_s value;
asn_per_constraint_t size; struct asn_per_constraint_s size;
int (*value2code)(unsigned int value); int (*value2code)(unsigned int value);
int (*code2value)(unsigned int code); int (*code2value)(unsigned int code);
} asn_per_constraints_t; } asn_per_constraints_t;
@ -81,6 +81,9 @@ ssize_t uper_get_nslength(asn_per_data_t *pd);
*/ */
ssize_t uper_get_nsnnwn(asn_per_data_t *pd); ssize_t uper_get_nsnnwn(asn_per_data_t *pd);
/* X.691-2008/11, #11.5.6 */
int uper_get_constrained_whole_number(asn_per_data_t *pd, unsigned long *v, int nbits);
/* Non-thread-safe debugging function, don't use it */ /* Non-thread-safe debugging function, don't use it */
char *per_data_string(asn_per_data_t *pd); char *per_data_string(asn_per_data_t *pd);
@ -103,6 +106,17 @@ int per_put_few_bits(asn_per_outp_t *per_data, uint32_t bits, int obits);
/* Output a large number of bits */ /* Output a large number of bits */
int per_put_many_bits(asn_per_outp_t *po, const uint8_t *src, int put_nbits); int per_put_many_bits(asn_per_outp_t *po, const uint8_t *src, int put_nbits);
/*
* Flush whole bytes (0 or more) through (outper) member.
* The least significant bits which are not used are guaranteed to be set to 0.
* Returns -1 if callback returns -1. Otherwise, 0.
*/
int per_put_aligned_flush(asn_per_outp_t *po);
/* X.691-2008/11, #11.5 */
int uper_put_constrained_whole_number_s(asn_per_outp_t *po, long v, int nbits);
int uper_put_constrained_whole_number_u(asn_per_outp_t *po, unsigned long v, int nbits);
/* /*
* Put the length "n" to the Unaligned PER stream. * Put the length "n" to the Unaligned PER stream.
* This function returns the number of units which may be flushed * This function returns the number of units which may be flushed