?? asn1.c
字號:
//==========================================================================//// ./lib/current/src/asn1.c//////==========================================================================//####ECOSGPLCOPYRIGHTBEGIN####// -------------------------------------------// This file is part of eCos, the Embedded Configurable Operating System.// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.//// eCos is free software; you can redistribute it and/or modify it under// the terms of the GNU General Public License as published by the Free// Software Foundation; either version 2 or (at your option) any later version.//// eCos is distributed in the hope that it will be useful, but WITHOUT ANY// WARRANTY; without even the implied warranty of MERCHANTABILITY or// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License// for more details.//// You should have received a copy of the GNU General Public License along// with eCos; if not, write to the Free Software Foundation, Inc.,// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.//// As a special exception, if other files instantiate templates or use macros// or inline functions from this file, or you compile this file and link it// with other works to produce a work based on this file, this file does not// by itself cause the resulting work to be covered by the GNU General Public// License. However the source code for this file must still be made available// in accordance with section (3) of the GNU General Public License.//// This exception does not invalidate any other reasons why a work based on// this file might be covered by the GNU General Public License.//// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.// at http://sources.redhat.com/ecos/ecos-license/// -------------------------------------------//####ECOSGPLCOPYRIGHTEND####//####UCDSNMPCOPYRIGHTBEGIN####//// -------------------------------------------//// Portions of this software may have been derived from the UCD-SNMP// project, <http://ucd-snmp.ucdavis.edu/> from the University of// California at Davis, which was originally based on the Carnegie Mellon// University SNMP implementation. Portions of this software are therefore// covered by the appropriate copyright disclaimers included herein.//// The release used was version 4.1.2 of May 2000. "ucd-snmp-4.1.2"// -------------------------------------------////####UCDSNMPCOPYRIGHTEND####//==========================================================================//#####DESCRIPTIONBEGIN####//// Author(s): hmt// Contributors: hmt// Date: 2000-05-30// Purpose: Port of UCD-SNMP distribution to eCos.// Description: // ////####DESCRIPTIONEND####////==========================================================================/******************************************************************** Copyright 1989, 1991, 1992 by Carnegie Mellon University Derivative Work -Copyright 1996, 1998, 1999, 2000 The Regents of the University of California All Rights ReservedPermission to use, copy, modify and distribute this software and itsdocumentation for any purpose and without fee is hereby granted,provided that the above copyright notice appears in all copies andthat both that copyright notice and this permission notice appear insupporting documentation, and that the name of CMU and The Regents ofthe University of California not be used in advertising or publicitypertaining to distribution of the software without specific writtenpermission.CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA DISCLAIM ALLWARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL CMU ORTHE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY SPECIAL,INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTINGFROM THE LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OFCONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR INCONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.*********************************************************************//* * Abstract Syntax Notation One, ASN.1 * As defined in ISO/IS 8824 and ISO/IS 8825 * This implements a subset of the above International Standards that * is sufficient to implement SNMP. * * Encodes abstract data types into a machine independent stream of bytes. * *//********************************************************************** Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University All Rights ReservedPermission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear in supporting documentation, and that the name of CMU not beused in advertising or publicity pertaining to distribution of thesoftware without specific, written prior permission. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDINGALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALLCMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES ORANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THISSOFTWARE.******************************************************************/#include <config.h>#ifdef KINETICS#include "gw.h"#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#include <sys/types.h>#include <stdio.h>#ifdef HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_WINSOCK_H#include <winsock.h>#endif#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#ifdef vms#include <in.h>#endif#if HAVE_DMALLOC_H#include <dmalloc.h>#endif#include "asn1.h"#include "int64.h"#include "snmp_debug.h"#include "mib.h"#ifndef NULL#define NULL 0#endif#include "snmp_api.h"#include "snmp_impl.h" /* to define ERROR_MSG */staticvoid _asn_size_err(const char *str, size_t wrongsize, size_t rightsize){ char ebuf[128]; sprintf(ebuf,"%s size %d: s/b %d",str, wrongsize, rightsize); ERROR_MSG(ebuf);}staticvoid _asn_length_err(const char *str, size_t wrongsize, size_t rightsize){ char ebuf[128]; sprintf(ebuf,"%s length %d too large: exceeds %d",str, wrongsize, rightsize); ERROR_MSG(ebuf);}/* * call after asn_parse_length to verify result. */staticint _asn_parse_length_check(const char *str, u_char *bufp, u_char *data, u_long plen, size_t dlen){ char ebuf[128]; size_t header_len; if (bufp == NULL){ /* error message is set */ return 1; } header_len = bufp - data; if (((size_t)plen + header_len) > dlen){ sprintf(ebuf, "%s: message overflow: %d len + %d delta > %d len", str, (int)plen, (int)header_len, (int)dlen); ERROR_MSG(ebuf); return 1; } return 0;}/* * call after asn_build_header to verify result. */staticint _asn_build_header_check(const char *str, u_char *data, size_t datalen, size_t typedlen){ char ebuf[128]; if (data == NULL){ /* error message is set */ return 1; } if (datalen < typedlen){ sprintf(ebuf, "%s: bad header, length too short: %d < %d", str, datalen, typedlen); ERROR_MSG(ebuf); return 1; } return 0;}/* checks the incoming packet for validity and returns its size or 0 */intasn_check_packet (u_char *pkt, size_t len){ u_long asn_length; if (len < 2) return 0; /* always too short */ if (*pkt != (u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR)) return -1; /* wrong type */ if (*(pkt+1) & 0x80) { /* long length */ if ((int)len < (int)(*(pkt+1) & ~0x80)+2) return 0; /* still to short, incomplete length */ asn_parse_length(pkt+1, &asn_length); return (asn_length + 2 + (*(pkt+1) & ~0x80)); } else { /* short length */ return (*(pkt+1) + 2); }}staticint _asn_bitstring_check(const char * str, u_long asn_length, u_char datum){ char ebuf[128]; if (asn_length < 1){ sprintf(ebuf,"%s: length %d too small", str, (int)asn_length); ERROR_MSG(ebuf); return 1; } if (datum > 7){ sprintf(ebuf,"%s: datum %d >7: too large", str, (int)(datum)); ERROR_MSG(ebuf); return 1; } return 0;}/* * asn_parse_int - pulls a long out of an ASN int 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 end of this 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_parse_int( u_char *data IN - pointer to start of object int *datalength IN/OUT - number of valid bytes left in buffer u_char *type OUT - asn type of object long *intp IN/OUT - pointer to start of output buffer int intsize IN - size of output buffer*/u_char *asn_parse_int(u_char *data, size_t *datalength, u_char *type, long *intp, size_t intsize){/* * ASN.1 integer ::= 0x02 asnlength byte {byte}* */ static const char *errpre = "parse int"; register u_char *bufp = data; u_long asn_length; register long value = 0; if (intsize != sizeof (long)){ _asn_size_err(errpre, intsize, sizeof(long)); return NULL; } *type = *bufp++; bufp = asn_parse_length(bufp, &asn_length); if (_asn_parse_length_check(errpre, bufp, data, asn_length, *datalength)) return NULL; if ((size_t)asn_length > intsize){ _asn_length_err(errpre, (size_t)asn_length, intsize); return NULL; } *datalength -= (int)asn_length + (bufp - data); if (*bufp & 0x80) value = -1; /* integer is negative */ DEBUGDUMPSETUP("dump_recv", data, bufp - data + asn_length); while(asn_length--) value = (value << 8) | *bufp++; DEBUGMSG(("dump_recv", " ASN Integer:\t%ld (0x%.2X)\n", value, value)); *intp = value; return bufp;}/* * asn_parse_unsigned_int - pulls an unsigned long out of an ASN int 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 end of this 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_parse_unsigned_int( u_char *data IN - pointer to start of object int *datalength IN/OUT - number of valid bytes left in buffer u_char *type OUT - asn type of object u_long *intp IN/OUT - pointer to start of output buffer int intsize IN - size of output buffer */u_char *asn_parse_unsigned_int(u_char *data, size_t *datalength, u_char *type, u_long *intp, size_t intsize){/* * ASN.1 integer ::= 0x02 asnlength byte {byte}* */ static const char *errpre = "parse uint"; register u_char *bufp = data; u_long asn_length; register u_long value = 0; if (intsize != sizeof (long)){ _asn_size_err(errpre, intsize, sizeof(long)); return NULL; } *type = *bufp++; bufp = asn_parse_length(bufp, &asn_length); if (_asn_parse_length_check(errpre, bufp, data, asn_length, *datalength)) return NULL; if (((int)asn_length > (intsize + 1)) || (((int)asn_length == intsize + 1) && *bufp != 0x00)){ _asn_length_err(errpre, (size_t)asn_length, intsize); return NULL; } *datalength -= (int)asn_length + (bufp - data); if (*bufp & 0x80) value = ~value; /* integer is negative */ DEBUGDUMPSETUP("dump_recv", data, bufp - data + asn_length); while(asn_length--) value = (value << 8) | *bufp++; DEBUGMSG(("dump_recv", " ASN UInteger:\t%ld (0x%.2X)\n", value, value)); *intp = value; return bufp;}/* * asn_build_int - builds an ASN object containing an integer. * 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 end of this 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_int( u_char *data IN - pointer to start of output buffer int *datalength IN/OUT - number of valid bytes left in buffer int type IN - asn type of object long *intp IN - pointer to start of long integer int intsize IN - size of input buffer */u_char *asn_build_int(u_char *data, size_t *datalength, u_char type, long *intp, size_t intsize){/* * ASN.1 integer ::= 0x02 asnlength byte {byte}* */ static const char *errpre = "build int"; register long integer; register u_long mask; if (intsize != sizeof (long)){ _asn_size_err(errpre, intsize, sizeof(long)); return NULL; } integer = *intp; /* * Truncate "unnecessary" bytes off of the most significant end of this * 2's complement integer. There should be no sequence of 9 * consecutive 1's or 0's at the most significant end of the * integer. */ mask = ((u_long) 0x1FF) << ((8 * (sizeof(long) - 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(data, datalength, type, intsize); if (_asn_build_header_check(errpre,data,*datalength,intsize)) return NULL; *datalength -= intsize; mask = ((u_long) 0xFF) << (8 * (sizeof(long) - 1)); /* mask is 0xFF000000 on a big-endian machine */ while(intsize--){ *data++ = (u_char)((integer & mask) >> (8 * (sizeof(long) - 1))); integer <<= 8; } return data;}/* * asn_build_unsigned_int - builds an ASN object containing an integer. * 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 end of this 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_unsigned_int( u_char *data IN - pointer to start of output buffer int *datalength IN/OUT - number of valid bytes left in buffer u_char type IN - asn type of object u_long *intp IN - pointer to start of long integer int intsize IN - size of input buffer */u_char *asn_build_unsigned_int(u_char *data, size_t *datalength, u_char type, u_long *intp, size_t intsize){/* * ASN.1 integer ::= 0x02 asnlength byte {byte}* */ static const char *errpre = "build uint"; register u_long integer; register u_long mask; int add_null_byte = 0;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -