?? util.c
字號:
/* * File: util.c * * An implementation of a utility which exposes the util.h interface. Please * see util.h for more info. * * See Also * util.h * * Copyright (C) 2002 RidgeRun, Inc. * Author: RidgeRun, Inc <skranz@ridgerun.com> * * This program 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 of the License, or (at your * option) any later version. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 675 Mass Ave, Cambridge, MA 02139, USA. * * Please report all bugs/problems to the author or <support@dsplinux.net> * * key: RRGPLCR (do not remove) * */#include "memconfig.h"#include "types.h"#include "util.h"#include "io.h"static const unsigned char hextable[256] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,};static char *do_sprintf(char *buf, const char *fmt, ...);/****************************** Routine: Description: ******************************/unsigned char util_ascii_to_bin(unsigned char c){ return hextable[c];}/****************************** Routine: similar to hexstrtobyte Description: Incoming string is assumed to be a hex numeric string (without the 0x prefix) whos converted value will fit within an integer. ******************************/int util_hexstrtoint(char *val, int *ret){ *ret = 0; while ((*val != '\0') && (*val != '\r')) { int d; if ((*val >= '0') && (*val <= '9')) d = *val - '0'; else if ((*val >= 'a') && (*val <= 'f')) d = *val - 'a' + 10; else if ((*val >= 'A') && (*val <= 'F')) d = *val - 'A' + 10; else { util_printf("Bad digit '%c'\n", *val); return -1; } *ret = *ret << 4; *ret += d; val++; } return 0;}/****************************** Routine: Description: Incoming string is assumed to be a hex numeric string (without the 0x prefix) whos converted value will fit within a single byte. example "1", "ff", "7d", "22", etc. ******************************/unsigned char util_hexstrtobyte(char *str){ unsigned short retval; int len; retval = 0; len = util_strlen(str); if (2 == len) { retval += (unsigned short)(util_ascii_to_bin(str[0])*0x10); retval += (unsigned short)(util_ascii_to_bin(str[1])); } else if (1 == len) { retval += (unsigned short)(util_ascii_to_bin(str[0])); } else { SYSTEM_FATAL("Logic Error"); } if (retval > 0x00FF) SYSTEM_FATAL("Logic Error"); return (unsigned char)retval;}/****************************** Routine: Description: Incoming string is assumed to be a numeric dec string whos converted value will fit withing a single int. example "1", "255", "125", "34", "65535", etc. ******************************/unsigned int util_decstrtoint(char *str){ unsigned int retval; int len,i, mul=1; retval = 0; len = util_strlen(str); for (i=1;i<=len;i++) { retval += (unsigned int)(util_ascii_to_bin(str[len - i]) * mul); mul *= 10; } if (retval > 0xFFFFFFFF) SYSTEM_FATAL("Logic Error"); return retval;}/****************************** Routine: Description: Incoming string is assumed to be a numeric dec string whos converted value will fit withing a single byte. example "1", "255", "125", "34", etc. ******************************/unsigned char util_decstrtobyte(char *str){ unsigned short retval; int len; retval = 0; len = util_strlen(str); if (3 == len) { retval += (unsigned short)(util_ascii_to_bin(str[0]) * 100); retval += (unsigned short)(util_ascii_to_bin(str[1]) * 10); retval += (unsigned short)(util_ascii_to_bin(str[2]) * 1); } else if (2 == len) { retval += (unsigned short)(util_ascii_to_bin(str[0]) * 10); retval += (unsigned short)(util_ascii_to_bin(str[1]) * 1); } else if (1 == len) { retval += (unsigned short)(util_ascii_to_bin(str[0]) * 1); } if (retval > 0x00FF) SYSTEM_FATAL("Logic Error"); return (unsigned char)retval;}/****************************** Routine: Description: Input string is expected to be standard IP format; x.x.x.x, where x can be up to three numeric decimal characters. example. "128.34.130.9" ******************************/unsigned long util_IPstr_to_num(char *IP){ // Example IP: 1.2.3.4 (7 chars) // Example IP: 100.200.300.400 (15 chars) #define MIN_IP 7 #define MAX_IP 15 char str[MAX_IP+1]; unsigned long retval; unsigned char bval; unsigned char *start; unsigned char *end; bval = retval = 0; if (util_strlen(IP) < MIN_IP) SYSTEM_FATAL("IP format error"); if (util_strlen(IP) > MAX_IP) SYSTEM_FATAL("IP format error"); util_strcpy(str,IP); // get a local writable copy. // Byte#1 MSB end = str; start = end; while (*end != '.') end++; *end = 0; // replace the '.' with a 0 to terminate the string. bval = util_decstrtobyte(start); retval = retval | bval<<24; // Byte#2 end++; start = end; while (*end != '.') end++; *end = 0; bval = util_decstrtobyte(start); retval = retval | bval<<16; // Byte#3 end++; start = end; while (*end != '.') end++; *end = 0; bval = util_decstrtobyte(start); retval = retval | bval<<8; // Byte#4 LSB end++; start = end; bval = util_decstrtobyte(start); retval = retval | bval; return retval;}/****************************** Routine: Description: Input string is expected to be in standard MAC format; x:x:x:x:x:x, where x can be up to two numeric hex characters. example. "ff:0:e0:10:c:22" This call will convert the incoming string into a series of numeric values which are loaded into the callers supplied array; mac_array[0] through mac_array[5]. ******************************/void util_fill_MAC(unsigned char *mac_array, char *MAC_str){ // example MAC: 1:2:3:4:5:6 (11 chars). // example MAC: 10:20:30:40:50:60 (17 chars). #define MIN_MAC 11 #define MAX_MAC 17 char str[MAX_MAC+1]; int i; unsigned char bval; unsigned char *start; unsigned char *end; bval = 0; if (util_strlen(MAC_str) < MIN_MAC) SYSTEM_FATAL("MAC str format Error"); if (util_strlen(MAC_str) > MAX_MAC) SYSTEM_FATAL("MAC str format Error"); util_strcpy(str,MAC_str); // get a local writable copy. end = str; start = end; for (i=0; i<6; i++) { if (i<5) { while (*end != ':') end++; *end = 0; // replace the ':' with a 0 to terminate the string. } bval = util_hexstrtobyte(start); mac_array[i] = bval; end++; start = end; }}/****************************** Routine: Description: Format codes %d - decimal %i - decimal %b - 1 byte val (2 ascii hex digits) %x - 2 byte val (4 ascii hex digits) %X - 4 byte val (8 ascii hex digits) %s - An ASCII string %c - An ASCII char %I - Internet address (x.x.x.x)******************************/static const char hex[]="0123456789ABCDEF";static char *do_printf(char *buf, const char *fmt, const int *dp){ register char *p; char tmp[16]; while (*fmt) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -