📄 encode.c
字号:
/* * Copyright 2000-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//* * Copyright 1986-1997 Epilogue Technology Corporation. * Copyright 1998 Integrated Systems, Inc. * All rights reserved. *//* * $Log: encode.c,v $ * Revision 1.2 2001/11/06 21:20:09 josh * revised new path hacking * * Revision 1.1.1.1 2001/11/05 17:47:42 tneale * Tornado shuffle * * Revision 9.2 2001/01/19 22:22:20 paul * Update copyright. * * Revision 9.1 2000/03/17 00:19:04 meister * Update copyright message * * Revision 9.0 1998/10/16 22:11:23 sar * Update version stamp to match release * * Revision 8.4 1998/06/19 20:13:51 sar * make sure all files include asn1conf.h and snmp.h to pick up all of * the common code * * Revision 8.3 1998/06/05 18:53:12 sra * "#include <foo.h>" => "#include <envoy/h/foo.h>". * * Revision 8.2 1998/05/29 04:31:11 sar * Add and use defines for v2 types and v2 protocol pieces. This allows * us to include the types and pieces for other versions, such as v3, * without having to touch all of the seperate files that use the * types or pieces. * * Revision 8.1 1998/02/25 04:51:44 sra * Update copyrights. * * Revision 8.0 1997/11/18 00:56:48 sar * Updated revision to 8.0 * * Revision 7.2 1997/03/20 06:48:53 sra * DFARS-safe copyright text. Zap! * * Revision 7.1 1997/02/25 10:49:26 sra * Update copyright notice, dust under the bed. * * Revision 7.0 1996/03/18 20:01:11 sar * Updated revision to 7.0 and copyright to 96 * * Revision 6.1 1995/10/20 23:02:48 sar * removed no_pp stuff, casts of zero * changed memcpy to MEMCPY * * Revision 6.0 1995/05/31 21:47:22 sra * Release 6.0. * * Revision 5.1 1994/09/29 18:37:21 sar * If we aren't using the v2 types remove the code to handle them mostly * this means ifdef the code. In mibutils.c and snmp_d.c we need to * examine the version of the packet in some cases to reject v2 types for * a v1 packet when both v1 and v2 are installed. * * Revision 5.0 1994/05/16 15:42:42 sar * Updated revision to 5.0 and copyright to include 1994 * * Revision 4.3 1994/05/09 21:03:44 sar * Added L for long to some constants. * * Revision 4.2 1994/01/25 20:43:50 sar * Removed some macro code that allowed oids to be 16 or 32 bits long * we now only deal with 32 bit oids. * Made the temporary buffer for oid encoding be 5 bytes long instead * of 4 bytes, this was needed due to the asn1 encoding of a large >2g * oid. * * Revision 4.1 1993/09/30 20:40:15 sar * Corrected the computation of unused bits in the A_EncodeBitString * routine. * * Revision 4.0 1993/06/24 15:45:46 sar * Updated revision to 4.0 and copyright to 93 * * Revision 3.2 1993/04/26 20:37:09 sar * Added ifdefs to allow clean makes of version 1 and 2, added bit strings, * arrange for deletion of acls with the party or context they refer to * is deleted. * * Revision 3.1 1993/03/25 21:18:09 sar * Added routines to encode/decode 64 bit integers/counter64s * * Revision 3.0 1992/04/03 19:52:37 dab * Release 3.0 * * Revision 2.106 92/02/11 12:53:45 dab * Typo'd type of argument 'func' to routine A_EncodeSubId() as EHELPER_T * instead of EHELPER_T *. Only the Zortech compiler noticed. * * Revision 2.105 92/02/05 18:46:31 dab * More casting to keep MSC happy. * * Revision 2.103 91/10/30 20:41:58 dab * Directly include asn1conf.h, snmpdefs.h, and snmpstat.h (if needed). * * Revision 2.102 91/08/15 12:31:00 dab * Removed <libfuncs.h>. * * Revision 2.101 91/08/12 12:44:13 dab * Use EHELPER_T to declare all the various helper routines. This * typedef (from encode.h) handles the problem of declaring the routines * arguments or not as the compiler requires. * * Revision 2.100 91/08/09 14:08:20 dab * Update version. * * Revision 1.1 91/07/30 02:23:37 romkey * Initial revision * * * Rev 2.1 23 Nov 1990 13:45:48 * Added a cast to calls to A_EncodeType() on the flags parameter * to satisfy Microsoft C version 6. * * Rev 2.0 31 Mar 1990 15:06:46 * Release 2.00 * * Rev 1.5 05 Jun 1989 0:11:16 * Corrected: error in encoding unsigned integers which have the high-order * bit set (value >= 2,147,483,648). * * Rev 1.4 28 Apr 1989 16:51:44 * Added protection against encoding null object identifiers. * * Rev 1.3 17 Mar 1989 21:41:34 * Calls to memcpy/memset protected against zero lengths * * Rev 1.2 19 Sep 1988 17:26:52 * Made changes to make the Sun C compiler happy. * * Rev 1.1 14 Sep 1988 17:57:04 * Moved includes of system include files into libfuncs.h. * * Rev 1.0 12 Sep 1988 10:46:56 * Initial revision.*//* [clearcase]modification history-------------------01c,12may05,job fix apigen comments01b,18apr05,job update copyright notices01a,24nov03,job update copyright information*/#include <wrn/wm/snmp/engine/asn1conf.h>#include <wrn/wm/snmp/engine/asn1.h>#include <wrn/wm/snmp/engine/snmp.h>#include <wrn/wm/snmp/engine/buffer.h>#include <wrn/wm/snmp/engine/encode.h>/* Turn on the following define to make A_SizeOfSubId() into a macro */#define SUBID_MACRO#if !defined(SUBID_MACRO)static ALENGTH_T A_SizeOfSubId(OIDC_T);#endif /* SUBID_MACRO */static void A_EncodeSubId(OIDC_T, EHELPER_T *, OCTET_T *);/****************************************************************************A_SizeOfInt -- Return total size that an integer would occupy when ASN.1 encoded (tag and length fields are not included).PARAMETERS INT_32_T The integer (signed 32 bit)Returns: ALENGTH_T Number of octets the integer would occupy if in ASN.1 encoding****************************************************************************/ALENGTH_T A_SizeOfInt(INT_32_T i){if (i >= 0L) return (i <= 0x0000007FL ? 1 : /* <= 127 */ (i <= 0x00007FFFL ? 2 : /* <= 32767 */ (i <= 0x007FFFFFL ? 3 : /* <= 8388607 */ 4))); /* > 8388607 */else return (i >= (INT_32_T)0xFFFFFF80L ? 1 : /* >= -128 */ (i >= (INT_32_T)0XFFFF8000L ? 2 : /* >= -32768 */ (i >= (INT_32_T)0XFF800000L ? 3 : /* >= -8388608 */ 4))); /* < -8388608 */}/****************************************************************************A_SizeOfUnsignedInt -- Return total size that an unsigned integer would occupy when ASN.1 encoded (tag and length fields are not included).PARAMETERS UINT_32_T The integer (unsigned 32 bit)Returns: ALENGTH_T Number of octets the integer would occupy if in ASN.1 encoding****************************************************************************/ALENGTH_T A_SizeOfUnsignedInt(UINT_32_T i){return (i <= 0x0000007FL ? 1 : /* <= 127 */ (i <= 0x00007FFFL ? 2 : /* <= 32768 */ (i <= 0x007FFFFFL ? 3 : /* <= 8388607 */ (i <= 0x7FFFFFFFL ? 4 : /* <= 2147483647*/ 5)))); /* > 2147483647*/}/****************************************************************************A_SizeOfSubId -- Compute the number of bytes required to hold a subidentifier from an object id. ASN.1 encoded (tag and length fields are not included)PARAMETERS OIDC_TReturns: ALENGTH_T Number of octets needed in ASN.1 encoding****************************************************************************/#if !defined(SUBID_MACRO)static ALENGTH_T A_SizeOfSubId(OIDC_T i){return (i <= 0x7F ? 1 : (i <= 0x3FFF ? 2 : (i <= 0x1FFFFFL ? 3 : (i <= 0x0FFFFFFFL ? 4 : 5))));}#else /* SUBID_MACRO */#define A_SizeOfSubId(I) (ALENGTH_T)((OIDC_T)(I) <= 0x7F ? 1 : \ ((OIDC_T)(I) <= 0x3FFF ? 2 : \ ((OIDC_T)(I) <= 0x1FFFFFL ? 3 : \ ((OIDC_T)(I) <= 0x0FFFFFFFL ? 4 : 5))))#endif /* SUBID_MACRO *//****************************************************************************A_SizeOfObjectId -- Return total size that an object ID would occupy when ASN.1 encoded (tag and length fields are not included)PARAMETERS OBJ_ID_T * Pointer to the internal object Id structureReturns: ALENGTH_T Number of octets the object ID would occupy if in ASN.1 encodingNote: It is assumed by this routine that the object identifier has at leasttwo components.****************************************************************************/ALENGTH_T A_SizeOfObjectId(OBJ_ID_T *objp){ALENGTH_T leng;OIDC_T *cp = objp->component_list;int i;OIDC_T x;if (objp->num_components == 0) return 0;/* Compute the value of the first subidentifier from the values of the *//* first two components. */ { x = *cp++; x = x * 40 + *cp++; leng = A_SizeOfSubId(x); }for (i = 2; i < objp->num_components; i++) { x = *cp++; leng += A_SizeOfSubId(x); }return leng;}/* We only inlcude ui64 function if the type is installed */#if (ENVOY_USE_V2_TYPES)/****************************************************************************A_SizeOfUnsignedInt64 -- Return total size that a big unsigned integer would occupy when ASN.1 encoded (tag and length fields are not included).PARAMETERS UINT_64_T * The integer (unsigned 64 bit)Returns: ALENGTH_T Number of octets the integer would occupy if in ASN.1 encoding****************************************************************************/ALENGTH_T A_SizeOfUnsignedInt64(UINT_64_T *i){if (i->high == 0) return (i->low <= 0x0000007FL ? 1 : /* <= 127 */ (i->low <= 0x00007FFFL ? 2 : /* <= 32768 */ (i->low <= 0x007FFFFFL ? 3 : /* <= 8388607 */ (i->low <= 0x7FFFFFFFL ? 4 : /* <= 2147483647*/ 5)))); /* > 2147483647*/else return (i->high <= 0x0000007FL ? 5 : /* <= 127 */ (i->high <= 0x00007FFFL ? 6 : /* <= 32768 */ (i->high <= 0x007FFFFFL ? 7 : /* <= 8388607 */ (i->high <= 0x7FFFFFFFL ? 8 : /* <= 2147483647*/ 9)))); /* > 2147483647*/}#endif /* (ENVOY_USE_V2_TYPES) *//****************************************************************************A_EncodeType -- Encode an ASN.1 type field into buffer.PARAMETERS ATVALUE_T The type value OCTET_T A_IDCF_MASK flag values ALENGTH_T (*f()) Function to be called to take generated data OCTET_T * Parameter to be passed unchanged to the function.Notes: The function whose address is passed as a parameter is called zeroor more times to take away some accumulated data. The function is calledwith these parameters: OCTET_T * The parameter (funcparm) passed to this routine OCTET_T * The buffer where the data resides ALENGTH_T The number of octets in the buffer.The function should return the number of octets consumed, type ALENGTH_T.The function should return a zero if it has taken all the data it wants.Returns: nothing****************************************************************************/void A_EncodeType(ATVALUE_T id, OCTET_T flags, EHELPER_T *func, OCTET_T *funcparm){flags &= A_IDCF_MASK;if (id <= (ATVALUE_T)30) { OCTET_T c; c = flags | (OCTET_T) id; (void)(*func)(funcparm, &c, (ALENGTH_T)sizeof(OCTET_T)); }else { /* Build a partial reverse order version of the result and then */ /* reverse it again back to correct order */ OCTET_T buff[5], reverse[4]; /* Can't handle more than 4 octets */ OCTET_T *bp = buff; OCTET_T *rp = reverse; unsigned short int count = 0; /* Should never exceed 4 */ ALENGTH_T cnt; *bp++ = (OCTET_T)((flags & A_IDCF_MASK) | 0x1F); while (id > 0) { *rp++ = (OCTET_T) (id & 0x7F); id >>= 7; count++; } cnt = count + 1; while ((count--) > 1) { *bp++ = (OCTET_T)(*(--rp) | 0x80); } *bp++ = *(--rp); (void)(*func)(funcparm, buff, cnt); }}/****************************************************************************A_EncodeLength -- Encode an ASN.1 definite form length field into buffer.PARAMETERS ALENGTH_T Length to be encoded ALENGTH_T (*f()) Function to be called to take generated data OCTET_T * Parameter to be passed unchanged to the function.Notes: The function whose address is passed as a parameter is called zeroor more times to take away some accumulated data. The function is calledwith these parameters: OCTET_T * The parameter (funcparm) passed to this routine OCTET_T * The buffer where the data resides ALENGTH_T The number of octets in the buffer.The function should return the number of octets consumed, type ALENGTH_T.The function should return a zero if it has taken all the data it wants.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -