diff options
Diffstat (limited to 'src/lib/krb5/asn.1/asn1buf.c')
-rw-r--r-- | src/lib/krb5/asn.1/asn1buf.c | 177 |
1 files changed, 89 insertions, 88 deletions
diff --git a/src/lib/krb5/asn.1/asn1buf.c b/src/lib/krb5/asn.1/asn1buf.c index 5320e184e..49ef84fbb 100644 --- a/src/lib/krb5/asn.1/asn1buf.c +++ b/src/lib/krb5/asn.1/asn1buf.c @@ -1,3 +1,4 @@ +/* -*- mode: c; indent-tabs-mode: nil -*- */ /* Coding Buffer Implementation */ /* @@ -47,7 +48,7 @@ next >= base next <= bound+2 (i.e. next should be able to step just past the bound, but no further. (The bound should move out in response - to being crossed by next.)) */ + to being crossed by next.)) */ #define ASN1BUF_OMIT_INLINE_FUNCS #include "asn1buf.h" @@ -55,7 +56,7 @@ #include <stdio.h> #include "asn1_get.h" -#define asn1_is_eoc(class, num, indef) \ +#define asn1_is_eoc(class, num, indef) \ ((class) == UNIVERSAL && !(num) && !(indef)) asn1_error_code asn1buf_create(asn1buf **buf) @@ -80,30 +81,30 @@ asn1_error_code asn1buf_imbed(asn1buf *subbuf, const asn1buf *buf, const unsigne { subbuf->base = subbuf->next = buf->next; if (!indef) { - subbuf->bound = subbuf->base + length - 1; - if (subbuf->bound > buf->bound) - return ASN1_OVERRUN; + subbuf->bound = subbuf->base + length - 1; + if (subbuf->bound > buf->bound) + return ASN1_OVERRUN; } else /* constructed indefinite */ - subbuf->bound = buf->bound; + subbuf->bound = buf->bound; return 0; } asn1_error_code asn1buf_sync(asn1buf *buf, asn1buf *subbuf, - asn1_class asn1class, asn1_tagnum lasttag, - unsigned int length, int indef, int seqindef) + asn1_class asn1class, asn1_tagnum lasttag, + unsigned int length, int indef, int seqindef) { asn1_error_code retval; if (!seqindef) { - /* sequence was encoded as definite length */ - buf->next = subbuf->bound + 1; + /* sequence was encoded as definite length */ + buf->next = subbuf->bound + 1; } else if (!asn1_is_eoc(asn1class, lasttag, indef)) { - retval = asn1buf_skiptail(subbuf, length, indef); - if (retval) - return retval; + retval = asn1buf_skiptail(subbuf, length, indef); + if (retval) + return retval; } else { - /* We have just read the EOC octets. */ - buf->next = subbuf->next; + /* We have just read the EOC octets. */ + buf->next = subbuf->next; } return 0; } @@ -116,26 +117,26 @@ asn1_error_code asn1buf_skiptail(asn1buf *buf, const unsigned int length, const nestlevel = 1 + indef; if (!indef) { - if (length <= buf->bound - buf->next + 1) - buf->next += length; - else - return ASN1_OVERRUN; + if (length <= buf->bound - buf->next + 1) + buf->next += length; + else + return ASN1_OVERRUN; } while (nestlevel > 0) { - if (buf->bound - buf->next + 1 <= 0) - return ASN1_OVERRUN; - retval = asn1_get_tag_2(buf, &t); - if (retval) return retval; - if (!t.indef) { - if (t.length <= buf->bound - buf->next + 1) - buf->next += t.length; - else - return ASN1_OVERRUN; - } - if (t.indef) - nestlevel++; - if (asn1_is_eoc(t.asn1class, t.tagnum, t.indef)) - nestlevel--; /* got an EOC encoding */ + if (buf->bound - buf->next + 1 <= 0) + return ASN1_OVERRUN; + retval = asn1_get_tag_2(buf, &t); + if (retval) return retval; + if (!t.indef) { + if (t.length <= buf->bound - buf->next + 1) + buf->next += t.length; + else + return ASN1_OVERRUN; + } + if (t.indef) + nestlevel++; + if (asn1_is_eoc(t.asn1class, t.tagnum, t.indef)) + nestlevel--; /* got an EOC encoding */ } return 0; } @@ -143,9 +144,9 @@ asn1_error_code asn1buf_skiptail(asn1buf *buf, const unsigned int length, const asn1_error_code asn1buf_destroy(asn1buf **buf) { if (*buf != NULL) { - free((*buf)->base); - free(*buf); - *buf = NULL; + free((*buf)->base); + free(*buf); + *buf = NULL; } return 0; } @@ -172,7 +173,7 @@ asn1_error_code asn1buf_insert_octetstring(asn1buf *buf, const unsigned int len, retval = asn1buf_ensure_space(buf,len); if (retval) return retval; for (length=1; length<=len; length++,(buf->next)++) - *(buf->next) = (char)(s[len-length]); + *(buf->next) = (char)(s[len-length]); return 0; } @@ -184,7 +185,7 @@ asn1_error_code asn1buf_insert_charstring(asn1buf *buf, const unsigned int len, retval = asn1buf_ensure_space(buf,len); if (retval) return retval; for (length=1; length<=len; length++,(buf->next)++) - *(buf->next) = (char)(s[len-length]); + *(buf->next) = (char)(s[len-length]); return 0; } @@ -202,14 +203,14 @@ asn1_error_code asn1buf_remove_octetstring(asn1buf *buf, const unsigned int len, if (len > buf->bound + 1 - buf->next) return ASN1_OVERRUN; if (len == 0) { - *s = 0; - return 0; + *s = 0; + return 0; } *s = (asn1_octet*)malloc(len*sizeof(asn1_octet)); if (*s == NULL) - return ENOMEM; + return ENOMEM; for (i=0; i<len; i++) - (*s)[i] = (asn1_octet)(buf->next)[i]; + (*s)[i] = (asn1_octet)(buf->next)[i]; buf->next += len; return 0; } @@ -220,13 +221,13 @@ asn1_error_code asn1buf_remove_charstring(asn1buf *buf, const unsigned int len, if (len > buf->bound + 1 - buf->next) return ASN1_OVERRUN; if (len == 0) { - *s = 0; - return 0; + *s = 0; + return 0; } *s = (char*)malloc(len*sizeof(char)); if (*s == NULL) return ENOMEM; for (i=0; i<len; i++) - (*s)[i] = (char)(buf->next)[i]; + (*s)[i] = (char)(buf->next)[i]; buf->next += len; return 0; } @@ -241,7 +242,7 @@ int asn1buf_remains(asn1buf *buf, int indef) * Two 0 octets means the end of an indefinite encoding. */ if (indef && remain >= 2 && !*(buf->next) && !*(buf->next + 1)) - return 0; + return 0; else return remain; } @@ -256,12 +257,12 @@ asn1_error_code asn12krb5_buf(const asn1buf *buf, krb5_data **code) (*code)->length = asn1buf_len(buf); (*code)->data = (char*)malloc((((*code)->length)+1)*sizeof(char)); if ((*code)->data == NULL) { - free(*code); - *code = NULL; - return ENOMEM; + free(*code); + *code = NULL; + return ENOMEM; } for (i=0; i < (*code)->length; i++) - ((*code)->data)[i] = (buf->base)[((*code)->length)-i-1]; + ((*code)->data)[i] = (buf->base)[((*code)->length)-i-1]; ((*code)->data)[(*code)->length] = '\0'; return 0; } @@ -275,21 +276,21 @@ asn1_error_code asn1buf_unparse(const asn1buf *buf, char **s) { free(*s); if (buf == NULL) { - *s = malloc(sizeof("<NULL>")); - if (*s == NULL) return ENOMEM; - strcpy(*s,"<NULL>"); + *s = malloc(sizeof("<NULL>")); + if (*s == NULL) return ENOMEM; + strcpy(*s,"<NULL>"); } else if (buf->base == NULL) { - *s = malloc(sizeof("<EMPTY>")); - if (*s == NULL) return ENOMEM; - strcpy(*s,"<EMPTY>"); + *s = malloc(sizeof("<EMPTY>")); + if (*s == NULL) return ENOMEM; + strcpy(*s,"<EMPTY>"); } else { - unsigned int length = asn1buf_len(buf); - unsigned int i; + unsigned int length = asn1buf_len(buf); + unsigned int i; - *s = calloc(length+1, sizeof(char)); - if (*s == NULL) return ENOMEM; - (*s)[length] = '\0'; - for (i=0; i<length; i++) ; + *s = calloc(length+1, sizeof(char)); + if (*s == NULL) return ENOMEM; + (*s)[length] = '\0'; + for (i=0; i<length; i++) ; /* OLDDECLARG( (*s)[i] = , (buf->base)[length-i-1]) */ } return 0; @@ -297,32 +298,32 @@ asn1_error_code asn1buf_unparse(const asn1buf *buf, char **s) asn1_error_code asn1buf_hex_unparse(const asn1buf *buf, char **s) { -#define hexchar(d) ((d)<=9 ? ('0'+(d)) : \ - ((d)<=15 ? ('A'+(d)-10) : \ - 'X')) +#define hexchar(d) ((d)<=9 ? ('0'+(d)) : \ + ((d)<=15 ? ('A'+(d)-10) : \ + 'X')) free(*s); if (buf == NULL) { - *s = malloc(sizeof("<NULL>")); - if (*s == NULL) return ENOMEM; - strcpy(*s,"<NULL>"); + *s = malloc(sizeof("<NULL>")); + if (*s == NULL) return ENOMEM; + strcpy(*s,"<NULL>"); } else if (buf->base == NULL) { - *s = malloc(sizeof("<EMPTY>")); - if (*s == NULL) return ENOMEM; - strcpy(*s,"<EMPTY>"); + *s = malloc(sizeof("<EMPTY>")); + if (*s == NULL) return ENOMEM; + strcpy(*s,"<EMPTY>"); } else { - unsigned int length = asn1buf_len(buf); - int i; - - *s = malloc(3*length); - if (*s == NULL) return ENOMEM; - for (i = length-1; i >= 0; i--) { - (*s)[3*(length-i-1)] = hexchar(((buf->base)[i]&0xF0)>>4); - (*s)[3*(length-i-1)+1] = hexchar((buf->base)[i]&0x0F); - (*s)[3*(length-i-1)+2] = ' '; - } - (*s)[3*length-1] = '\0'; + unsigned int length = asn1buf_len(buf); + int i; + + *s = malloc(3*length); + if (*s == NULL) return ENOMEM; + for (i = length-1; i >= 0; i--) { + (*s)[3*(length-i-1)] = hexchar(((buf->base)[i]&0xF0)>>4); + (*s)[3*(length-i-1)+1] = hexchar((buf->base)[i]&0x0F); + (*s)[3*(length-i-1)+2] = ' '; + } + (*s)[3*length-1] = '\0'; } return 0; } @@ -349,8 +350,8 @@ asn1_error_code asn1buf_ensure_space(asn1buf *buf, const unsigned int amount) { int avail = asn1buf_free(buf); if (avail < amount) { - asn1_error_code retval = asn1buf_expand(buf, amount-avail); - if (retval) return retval; + asn1_error_code retval = asn1buf_expand(buf, amount-avail); + if (retval) return retval; } return 0; } @@ -364,13 +365,13 @@ asn1_error_code asn1buf_expand(asn1buf *buf, unsigned int inc) else bound_offset = buf->bound - buf->base; if (inc < STANDARD_INCREMENT) - inc = STANDARD_INCREMENT; + inc = STANDARD_INCREMENT; if (buf->base == NULL) - buf->base = malloc((asn1buf_size(buf)+inc) * sizeof(asn1_octet)); + buf->base = malloc((asn1buf_size(buf)+inc) * sizeof(asn1_octet)); else - buf->base = realloc(buf->base, - (asn1buf_size(buf)+inc) * sizeof(asn1_octet)); + buf->base = realloc(buf->base, + (asn1buf_size(buf)+inc) * sizeof(asn1_octet)); if (buf->base == NULL) return ENOMEM; buf->bound = (buf->base) + bound_offset + inc; buf->next = (buf->base) + next_offset; |