a1rwlen.c,v

来自「TCP-IP红宝书源代码」· C,V 代码 · 共 90 行

C,V
90
字号
head	1.1;
access;
symbols;
locks
	dls:1.1; strict;
comment	@ * @;


1.1
date	97.09.21.19.29.17;	author dls;	state Dist;
branches;
next	;


desc
@@


1.1
log
@pre-3e code
@
text
@/* a1rwlen.c - a1readlen, a1writelen */

#include <conf.h>
#include <kernel.h>
#include <network.h>

#ifdef	SNMP

#include <snmp.h>
#include <asn1.h>

/*------------------------------------------------------------------------
 * a1readlen - read and return the length of an ASN.1 encoded object
 *------------------------------------------------------------------------
 */
int a1readlen(pack, lenlen)
unsigned char	*pack;
int		*lenlen;	/* length of length specification */
{
	int	totlen;
	int	i;

	/* if the high bit is NOT set, then len is in short form */
	if (!((*pack) & CHAR_HIBIT)) {
		*lenlen = 1;
		return (*pack) & ~CHAR_HIBIT;	/* use only low bits */
	}
	/*
	 * else, using long form where bit 7 = 1, and bits 6 - 0 encode
	 * the number of subsequent octets that specify the length
	 */
	*lenlen = (*pack++ & ~CHAR_HIBIT) + 1;

	for (i = 0, totlen = 0; i < (*lenlen) - 1; i++)
		totlen = (totlen << CHAR_BITS) | (int) *pack++;
	return totlen;
}

/*------------------------------------------------------------------------
 * a1writelen - write the length of an object in ASN.1 encoded form
 *------------------------------------------------------------------------
 */
int a1writelen(pp, len)		/* return number of bytes required */
u_char	*pp;
int	len;
{
	/* if len < 128 then use short form */
	if (len < CHAR_HIBIT) {
		*pp = len;
		return 1;
	}
	/* use long form, where bit 7 = 1, and bits 6 - 0 encode the
		number of subsequent octets that specify the length */
	if (len <= 255) {
		*pp++ = CHAR_HIBIT | 1;
		*pp = len & 0xff;
		return 2;
	}
	/* else, assume len <= 65535 (2^16 - 1) */
	*pp++ = CHAR_HIBIT | 2;
	*pp++ = len >> CHAR_BITS;
	*pp = len & 0xff;
	return 3;
}
#endif	/* SNMP */
@

⌨️ 快捷键说明

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