⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asn1.c

📁 -
💻 C
📖 第 1 页 / 共 3 页
字号:
     */    mask = (u_int) 0x1FF << ((8 * (sizeof(int) - 1)) - 1);    /* mask is 0xFF800000 on a big-endian machine */    while ((((integer & mask) == 0)	    || ((integer & mask) == mask)) && intsize > 1) {	intsize--;	integer <<= 8;    }    data = asn_build_header_with_truth(data, datalength, type, intsize, 1);    if (data == NULL)	return (NULL);    if (*datalength < intsize) {	snmp_set_api_error(SNMPERR_ASN_ENCODE);	return (NULL);    }    *datalength -= intsize;    if (add_null_byte == 1) {	*data++ = '\0';	intsize--;    }    mask = (u_int) 0xFF << (8 * (sizeof(int) - 1));    /* mask is 0xFF000000 on a big-endian machine */    while (intsize--) {	*data++ = (u_char) ((integer & mask) >> (8 * (sizeof(int) - 1)));	integer <<= 8;    }    return (data);}/* * asn_parse_string - pulls an octet string out of an ASN octet string type. *  On entry, datalength is input as the number of valid bytes following *   "data".  On exit, it is returned as the number of valid bytes *   following the beginning of the next object. * *  "string" is filled with the octet string. * *  Returns a pointer to the first byte past the end *   of this object (i.e. the start of the next object). *  Returns NULL on any error. */u_char *asn_parse_string(u_char * data, int *datalength,    u_char * type, u_char * string, int *strlength)  /*    u_char *data;       IN - pointer to start of object */  /*    int    *datalength; IN/OUT - # of valid bytes left in buffer */  /*    u_char *type;       OUT - asn type of object */  /*    u_char *string;     IN/OUT - pointer to start of output buffer */  /*    int    *strlength;  IN/OUT - size of output buffer */{    /*     * ASN.1 octet string ::= primstring | cmpdstring     * primstring ::= 0x04 asnlength byte {byte}*     * cmpdstring ::= 0x24 asnlength string {string}*     */    u_char *bufp = data;    u_int asn_length;    *type = *bufp++;    bufp = asn_parse_length(bufp, &asn_length);    if (bufp == NULL)	return (NULL);    if (asn_length + (bufp - data) > *datalength) {	snmp_set_api_error(SNMPERR_ASN_DECODE);	return (NULL);    }    if (asn_length > *strlength) {	snmp_set_api_error(SNMPERR_ASN_DECODE);	return (NULL);    }    xmemcpy((char *) string, (char *) bufp, (int) asn_length);    *strlength = (int) asn_length;    *datalength -= (int) asn_length + (bufp - data);    return (bufp + asn_length);}/* * asn_build_string - Builds an ASN octet string object containing the input *   string.  On entry, datalength is input as the number of valid bytes  *   following "data".  On exit, it is returned as the number of valid bytes *   following the beginning of the next object. * *  Returns a pointer to the first byte past the end *   of this object (i.e. the start of the next object). *  Returns NULL on any error. */u_char *asn_build_string(u_char * data, int *datalength,    u_char type, u_char * string, int strlength)  /*    u_char *data;       IN - pointer to start of object */  /*    int    *datalength; IN/OUT - # of valid bytes left in buf */  /*    u_char  type;       IN - ASN type of string */  /*    u_char *string;     IN - pointer to start of input buffer */  /*    int     strlength;  IN - size of input buffer */{    /*     * ASN.1 octet string ::= primstring | cmpdstring     * primstring ::= 0x04 asnlength byte {byte}*     * cmpdstring ::= 0x24 asnlength string {string}*     * This code will never send a compound string.     */    data = asn_build_header_with_truth(data, datalength, type, strlength, 1);    if (data == NULL)	return (NULL);    if (*datalength < strlength) {	snmp_set_api_error(SNMPERR_ASN_DECODE);	return (NULL);    }    xmemcpy((char *) data, (char *) string, strlength);    *datalength -= strlength;    return (data + strlength);}/* * asn_parse_header - interprets the ID and length of the current object. *  On entry, datalength is input as the number of valid bytes following *   "data".  On exit, it is returned as the number of valid bytes *   in this object following the id and length. * *  Returns a pointer to the first byte of the contents of this object. *  Returns NULL on any error. */u_char *asn_parse_header(u_char * data, int *datalength, u_char * type)  /*    u_char  *data;       IN - pointer to start of object */  /*    int     *datalength; IN/OUT - # of valid bytes left in buffer */  /*    u_char  *type;       OUT - ASN type of object */{    u_char *bufp = data;    int header_len;    u_int asn_length;    /* this only works on data types < 30, i.e. no extension octets */    if (IS_EXTENSION_ID(*bufp)) {	snmp_set_api_error(SNMPERR_ASN_DECODE);	return (NULL);    }    *type = *bufp;    bufp = asn_parse_length(bufp + 1, &asn_length);    if (bufp == NULL)	return (NULL);    header_len = bufp - data;    if (header_len + asn_length > *datalength) {	snmp_set_api_error(SNMPERR_ASN_DECODE);	return (NULL);    }    *datalength = (int) asn_length;    return (bufp);}/* * asn_build_header - builds an ASN header for an object with the ID and * length specified. *  On entry, datalength is input as the number of valid bytes following *   "data".  On exit, it is returned as the number of valid bytes *   in this object following the id and length. * *  This only works on data types < 30, i.e. no extension octets. *  The maximum length is 0xFFFF; * *  Returns a pointer to the first byte of the contents of this object. *  Returns NULL on any error. */u_char *asn_build_header_with_truth(u_char * data, int *datalength,    u_char type, int length, int truth)  /*    u_char *data;       IN - pointer to start of object */  /*    int    *datalength; IN/OUT - # of valid bytes left in buffer */  /*    u_char  type;       IN - ASN type of object */  /*    int     length;     IN - length of object */  /*    int     truth;      IN - Whether length is truth */{    if (*datalength < 1) {	snmp_set_api_error(SNMPERR_ASN_ENCODE);	return (NULL);    }    *data++ = type;    (*datalength)--;    return (asn_build_length(data, datalength, length, truth));}#if 0/* * asn_build_sequence - builds an ASN header for a sequence with the ID and * length specified. *  On entry, datalength is input as the number of valid bytes following *   "data".  On exit, it is returned as the number of valid bytes *   in this object following the id and length. * *  This only works on data types < 30, i.e. no extension octets. *  The maximum length is 0xFFFF; * *  Returns a pointer to the first byte of the contents of this object. *  Returns NULL on any error. */u_char *asn_build_sequence(u_char * data, int *datalength,    u_char type, int length)  /*    u_char *data;       IN - pointer to start of object */  /*    int    *datalength; IN/OUT - # of valid bytes left in buffer */  /*    u_char  type;       IN - ASN type of object */  /*    int     length;     IN - length of object */{    *datalength -= 4;    if (*datalength < 0) {	*datalength += 4;	/* fix up before punting */	snmp_set_api_error(SNMPERR_ASN_ENCODE);	return (NULL);    }    *data++ = type;    *data++ = (u_char) (0x02 | ASN_LONG_LEN);    *data++ = (u_char) ((length >> 8) & 0xFF);    *data++ = (u_char) (length & 0xFF);    return (data);}#endif/* * asn_parse_length - interprets the length of the current object. *  On exit, length contains the value of this length field. * *  Returns a pointer to the first byte after this length *  field (aka: the start of the data field). *  Returns NULL on any error. */u_char *asn_parse_length(u_char * data, u_int * length)  /*    u_char  *data;   IN - pointer to start of length field */  /*    u_int  *length; OUT - value of length field */{    u_char lengthbyte = *data;    if (lengthbyte & ASN_LONG_LEN) {	lengthbyte &= ~ASN_LONG_LEN;	/* turn MSb off */	if (lengthbyte == 0) {	    snmp_set_api_error(SNMPERR_ASN_DECODE);	    return (NULL);	}	if (lengthbyte > sizeof(int)) {	    snmp_set_api_error(SNMPERR_ASN_DECODE);	    return (NULL);	}	*length = (u_int) 0;	xmemcpy((char *) (length), (char *) data + 1, (int) lengthbyte);	*length = ntohl(*length);	*length >>= (8 * ((sizeof *length) - lengthbyte));	return (data + lengthbyte + 1);    }    /* short asnlength */    *length = (int) lengthbyte;    return (data + 1);}u_char *asn_build_length(u_char * data, int *datalength,    int length, int truth)  /*   u_char *data;       IN - pointer to start of object */  /*   int    *datalength; IN/OUT - # of valid bytes left in buf */  /*   int     length;     IN - length of object */  /*   int     truth;      IN - If 1, this is the true len. */{    u_char *start_data = data;    if (truth) {	/* no indefinite lengths sent */	if (length < 0x80) {	    if (*datalength < 1) {		snmp_set_api_error(SNMPERR_ASN_ENCODE);		return (NULL);	    }	    *data++ = (u_char) length;	} else if (length <= 0xFF) {	    if (*datalength < 2) {		snmp_set_api_error(SNMPERR_ASN_ENCODE);		return (NULL);	    }	    *data++ = (u_char) (0x01 | ASN_LONG_LEN);	    *data++ = (u_char) length;	} else {		/* 0xFF < length <= 0xFFFF */	    if (*datalength < 3) {		snmp_set_api_error(SNMPERR_ASN_ENCODE);		return (NULL);	    }	    *data++ = (u_char) (0x02 | ASN_LONG_LEN);	    *data++ = (u_char) ((length >> 8) & 0xFF);	    *data++ = (u_char) (length & 0xFF);	}    } else {	/* Don't know if this is the true length.  Make sure it's large	 * enough for later.	 */	if (*datalength < 3) {	    snmp_set_api_error(SNMPERR_ASN_ENCODE);	    return (NULL);	}	*data++ = (u_char) (0x02 | ASN_LONG_LEN);	*data++ = (u_char) ((length >> 8) & 0xFF);	*data++ = (u_char) (length & 0xFF);    }    *datalength -= (data - start_data);    return (data);}/* * asn_parse_objid - pulls an object indentifier out of an ASN object * identifier type. *  On entry, datalength is input as the number of valid bytes following *   "data".  On exit, it is returned as the number of valid bytes *   following the beginning of the next object. * *  "objid" is filled with the object identifier. * *  Returns a pointer to the first byte past the end *   of this object (i.e. the start of the next object). *  Returns NULL on any error. */u_char *asn_parse_objid(u_char * data, int *datalength,    u_char * type, oid * objid, int *objidlength)  /*    u_char  *data;        IN - pointer to start of object */  /*    int     *datalength;  IN/OUT - # of valid bytes left in buf */  /*    u_char  *type;        OUT - ASN type of object */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -