74#include <sys/socket.h>
77#include <netinet/in.h>
115 u_char * type,
int *intp,
int intsize)
130 if (intsize !=
sizeof(
int)) {
143 if (asn_length + (bufp - data) > *datalength) {
148 if (asn_length > intsize) {
153 *datalength -= (
int) asn_length + (bufp - data);
161 value = (value << 8) | *bufp++;
180 u_char * type, u_int * intp,
int intsize)
195 if (intsize !=
sizeof(
int)) {
208 if (asn_length + (bufp - data) > *datalength) {
213 if ((asn_length > (intsize + 1)) ||
214 ((asn_length == intsize + 1) && *bufp != 0x00)) {
219 *datalength -= (
int) asn_length + (bufp - data);
227 value = (value << 8) | *bufp++;
246 u_char type,
int *intp,
int intsize)
259 if (intsize !=
sizeof(
int)) {
271 mask = (u_int) 0x1FF << ((8 * (
sizeof(
int) - 1)) - 1);
274 while ((((integer & mask) == 0) || ((integer & mask) == mask))
285 if (*datalength < intsize) {
290 *datalength -= intsize;
291 mask = (u_int) 0xFF << (8 * (
sizeof(
int) - 1));
294 *data++ = (u_char) ((integer & mask) >> (8 * (
sizeof(
int) - 1)));
312 u_char type, u_int * intp,
int intsize)
324 int add_null_byte = 0;
326 if (intsize !=
sizeof(
int)) {
331 mask = (u_int) 0x80 << (8 * (
sizeof(
int) - 1));
333 if ((integer & mask) != 0) {
345 mask = (u_int) 0x1FF << ((8 * (
sizeof(
int) - 1)) - 1);
347 while (((integer & mask) == 0) && intsize > 1) {
356 if (*datalength < intsize) {
360 *datalength -= intsize;
361 if (add_null_byte == 1) {
365 mask = (u_int) 0xFF << (8 * (
sizeof(
int) - 1));
368 *data++ = (u_char) ((integer & mask) >> (8 * (
sizeof(
int) - 1)));
388 u_char * type, u_char *
string,
int *strlength)
408 if (asn_length + (bufp - data) > *datalength) {
412 if (asn_length > *strlength) {
416 memcpy((
char *)
string, (
char *) bufp, (
int) asn_length);
417 *strlength = (
int) asn_length;
418 *datalength -= (
int) asn_length + (bufp - data);
419 return (bufp + asn_length);
434 u_char type, u_char *
string,
int strlength)
451 if (*datalength < strlength) {
455 memcpy((
char *) data, (
char *)
string, strlength);
456 *datalength -= strlength;
457 return (data + strlength);
489 header_len = bufp - data;
490 if (header_len + asn_length > *datalength || asn_length > (u_int)(2 << 18) ) {
494 *datalength = (
int) asn_length;
514 u_char type,
int length,
int truth)
521 if (*datalength < 1) {
543 u_char lengthbyte = *data;
546 lengthbyte &= ~ASN_LONG_LEN;
548 if (lengthbyte == 0) {
552 if (lengthbyte >
sizeof(
int)) {
557 memcpy((
char *) (length), (
char *) data + 1, (
int) lengthbyte);
558 *length = ntohl(*length);
559 *length >>= (8 * ((
sizeof *length) - lengthbyte));
560 return (data + lengthbyte + 1);
565 *length = (
int) lengthbyte;
571 int length,
int truth)
577 u_char *start_data = data;
583 if (*datalength < 1) {
587 *data++ = (u_char) length;
589 }
else if (length <= 0xFF) {
590 if (*datalength < 2) {
595 *data++ = (u_char) length;
597 if (*datalength < 3) {
602 *data++ = (u_char) ((length >> 8) & 0xFF);
603 *data++ = (u_char) (length & 0xFF);
611 if (*datalength < 3) {
616 *data++ = (u_char) ((length >> 8) & 0xFF);
617 *data++ = (u_char) (length & 0xFF);
620 *datalength -= (data - start_data);
639 u_char * type,
oid * objid,
int *objidlength)
653 oid *oidp = objid + 1;
663 if (asn_length + (bufp - data) > *datalength) {
667 *datalength -= (
int) asn_length + (bufp - data);
671 objid[0] = objid[1] = 0;
675 while (length > 0 && (*objidlength)-- > 0) {
679 subidentifier = (subidentifier << 7)
680 + (*(u_char *) bufp & ~ASN_BIT8);
682 }
while (*(u_char *) bufp++ &
ASN_BIT8);
689 *oidp++ = (
oid) subidentifier;
698 subidentifier = (u_int) objid[1];
699 if (subidentifier == 0x2B) {
703 objid[1] = (u_char) (subidentifier % 40);
704 objid[0] = (u_char) ((subidentifier - objid[1]) / 40);
707 *objidlength = (
int) (oidp - objid);
724 u_char type,
oid * objid,
int objidlength)
741 u_int
subid, mask, testmask;
744 if (objidlength < 2) {
748 *bp++ = op[1] + (op[0] * 40);
753 while (objidlength-- > 0) {
761 for (testmask = 0x7F, testbits = 0; testmask != 0;
762 testmask <<= 7, testbits += 7) {
763 if (
subid & testmask) {
769 for (; mask != 0x7F; mask >>= 7, bits -= 7) {
771 if (mask == 0x1E00000)
775 *bp++ = (u_char) (
subid & mask);
779 asnlength = bp - buf;
783 if (*datalength < asnlength) {
787 memcpy((
char *) data, (
char *) buf, asnlength);
788 *datalength -= asnlength;
789 return (data + asnlength);
u_char * asn_parse_objid(u_char *data, int *datalength, u_char *type, oid *objid, int *objidlength)
u_char * asn_parse_int(u_char *data, int *datalength, u_char *type, int *intp, int intsize)
u_char * asn_parse_length(u_char *data, u_int *length)
u_char * asn_build_unsigned_int(u_char *data, int *datalength, u_char type, u_int *intp, int intsize)
u_char * asn_build_string(u_char *data, int *datalength, u_char type, u_char *string, int strlength)
u_char * asn_parse_string(u_char *data, int *datalength, u_char *type, u_char *string, int *strlength)
u_char * asn_parse_header(u_char *data, int *datalength, u_char *type)
u_char * asn_build_length(u_char *data, int *datalength, int length, int truth)
u_char * asn_build_header_with_truth(u_char *data, int *datalength, u_char type, int length, int truth)
u_char * asn_build_objid(u_char *data, int *datalength, u_char type, oid *objid, int objidlength)
u_char * asn_build_int(u_char *data, int *datalength, u_char type, int *intp, int intsize)
u_char * asn_build_header(u_char *data, int *datalength, u_char type, int length)
u_char * asn_build_exception(u_char *data, int *datalength, u_char type)
u_char * asn_build_null(u_char *data, int *datalength, u_char type)
u_char * asn_parse_unsigned_int(u_char *data, int *datalength, u_char *type, u_int *intp, int intsize)
#define IS_EXTENSION_ID(byte)
#define SNMPERR_ASN_ENCODE
void snmp_set_api_error(int)
#define SNMPERR_ASN_DECODE