📄 tools.c
字号:
//==========================================================================
//
// ./lib/current/src/tools.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 Reserved
Permission 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 appears in all copies and
that both that copyright notice and this permission notice appear in
supporting documentation, and that the name of CMU and The Regents of
the University of California not be used in advertising or publicity
pertaining to distribution of the software without specific written
permission.
CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA DISCLAIM ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL CMU OR
THE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY SPECIAL,
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
FROM THE 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 THIS SOFTWARE.
*********************************************************************/
/*
* tools.c
*/
#include <config.h>
#include <ctype.h>
#include <stdio.h>
#include <sys/types.h>
#if TIME_WITH_SYS_TIME
# ifdef WIN32
# include <sys/timeb.h>
# else
# include <sys/time.h>
# endif
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#if HAVE_WINSOCK_H
#include <winsock.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#if HAVE_DMALLOC_H
#include <dmalloc.h>
#endif
#include "asn1.h"
#include "system.h"
#include "snmp_api.h"
#include "snmp_debug.h"
#include "snmp_debug.h"
#include "tools.h"
#include "mib.h"
#include "scapi.h"
/*******************************************************************-o-******
* free_zero
*
* Parameters:
* *buf Pointer at bytes to free.
* size Number of bytes in buf.
*/
void
free_zero(void *buf, size_t size)
{
if (buf) {
memset(buf, 0, size);
free(buf);
}
} /* end free_zero() */
/*******************************************************************-o-******
* malloc_random
*
* Parameters:
* size Number of bytes to malloc() and fill with random bytes.
*
* Returns pointer to allocaed & set buffer on success, size contains
* number of random bytes filled.
*
* buf is NULL and *size set to KMT error value upon failure.
*
*/
u_char *
malloc_random(size_t *size)
{
int rval = SNMPERR_SUCCESS;
u_char *buf = (u_char *)calloc (1, *size);
if (buf) {
rval = sc_random(buf, size);
if (rval < 0) {
free_zero(buf, *size);
buf = NULL;
} else {
*size = rval;
}
}
return buf;
} /* end malloc_random() */
/*******************************************************************-o-******
* memdup
*
* Parameters:
* to Pointer to allocate and copy memory to.
* from Pointer to copy memory from.
* size Size of the data to be copied.
*
* Returns
* SNMPERR_SUCCESS On success.
* SNMPERR_GENERR On failure.
*/
int
memdup(u_char **to, const u_char *from, size_t size)
{
if (to == NULL)
return SNMPERR_GENERR;
if (from == NULL) {
*to = NULL;
return SNMPERR_SUCCESS;
}
if ((*to = (u_char *)malloc(size)) == NULL)
return SNMPERR_GENERR;
memcpy(*to, from, size);
return SNMPERR_SUCCESS;
} /* end memdup() */
/*******************************************************************-o-******
* binary_to_hex
*
* Parameters:
* *input Binary data.
* len Length of binary data.
* **output NULL terminated string equivalent in hex.
*
* Returns:
* olen Length of output string not including NULL terminator.
*
* FIX Is there already one of these in the UCD SNMP codebase?
* The old one should be used, or this one should be moved to
* snmplib/snmp_api.c.
*/
u_int
binary_to_hex(const u_char *input, size_t len, char **output)
{
u_int olen = (len * 2) + 1;
char *s = (char *) calloc(1,olen),
*op = s;
const u_char *ip = input;
while (ip-input < (int)len) {
*op++ = VAL2HEX( (*ip >> 4) & 0xf );
*op++ = VAL2HEX( *ip & 0xf );
ip++;
}
*op = '\0';
*output = s;
return olen;
} /* end binary_to_hex() */
/*******************************************************************-o-******
* hex_to_binary2
*
* Parameters:
* *input Printable data in base16.
* len Length in bytes of data.
* **output Binary data equivalent to input.
*
* Returns:
* SNMPERR_GENERR Failure.
* <len> Otherwise, Length of allocated string.
*
*
* Input of an odd length is right aligned.
*
* FIX Another version of "hex-to-binary" which takes odd length input
* strings. It also allocates the memory to hold the binary data.
* Should be integrated with the official hex_to_binary() function.
*/
int
hex_to_binary2(const u_char *input, size_t len, char **output)
{
u_int olen = (len/2) + (len%2);
char *s = (char *)calloc (1,olen),
*op = s;
const u_char *ip = input;
*output = NULL;
*op = 0;
if (len%2) {
if(!isxdigit(*ip)) goto hex_to_binary2_quit;
*op++ = HEX2VAL( *ip ); ip++;
}
while (ip-input < (int)len) {
if(!isxdigit(*ip)) goto hex_to_binary2_quit;
*op = HEX2VAL( *ip ) << 4; ip++;
if(!isxdigit(*ip)) goto hex_to_binary2_quit;
*op++ += HEX2VAL( *ip ); ip++;
}
*output = s;
return olen;
hex_to_binary2_quit:
free_zero(s, olen);
return -1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -