helpers.c
来自「本附件为嵌入式Web的相关资料」· C语言 代码 · 共 370 行
C
370 行
/*********************************************************************
*
* Helper Functions for Microchip TCP/IP Stack
*
*********************************************************************
* FileName: Helpers.C
* Dependencies: Compiler.h
* Helpers.h
* Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F
* Complier: Microchip C18 v3.02 or higher
* Microchip C30 v2.01 or higher
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* This software is owned by Microchip Technology Inc. ("Microchip")
* and is supplied to you for use exclusively as described in the
* associated software agreement. This software is protected by
* software and other intellectual property laws. Any use in
* violation of the software license may subject the user to criminal
* sanctions as well as civil liability. Copyright 2006 Microchip
* Technology Inc. All rights reserved.
*
* This software is provided "AS IS." MICROCHIP DISCLAIMS ALL
* WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED
* TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
* INFRINGEMENT. Microchip shall in no event be liable for special,
* incidental, or consequential damages.
*
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Nilesh Rajbharti 5/17/01 Original (Rev 1.0)
* Nilesh Rajbharti 2/9/02 Cleanup
* Nilesh Rajbharti 6/25/02 Rewritten CalcIPChecksum() to avoid
* multi-byte shift operation.
* Howard Schlunder 2/9/05 Added hexatob(), btohexa_high(), and
* btohexa_low()
* Howard Schlunder 10/10/06 Optimized swapl()
********************************************************************/
#define __HELPERS_C
#include "mchp_tcp_ip\tcpip.h"
#if defined(STACK_USE_HTTP_SERVER)
// TODO: Test this function, it may have bugs
void UnencodeURL(BYTE *URL)
{
BYTE *Right, *Copy;
WORD_VAL Number;
while(Right = strchr(URL, '%'))
{
// Make sure the string is long enough
if(Right[2] == '\0' || Right[1] == '\0')
break;
// Update the string in place
Number.v[0] = Right[2];
Number.v[1] = Right[1];
*URL = hexatob(Number);
URL += 3;
// Remove two blank spots by shifting all remaining characters right two
Copy = URL;
while(*Right)
{
*Right++ = *Copy++;
}
}
}
#endif
#if defined(__C30__) || defined(HI_TECH_C) || defined (__PIC32MX__)
/*********************************************************************
* Function: void itoa(unsigned int Value, char *Buffer)
*
* PreCondition: None
*
* Input: Value: Unsigned integer to be converted
* Buffer: Pointer to a location to write the string
*
* Output: *Buffer: Receives the resulting string
*
* Side Effects: None
*
* Overview: The function converts an unsigned integer (16 bits)
* into a null terminated decimal string.
*
* Note: None
********************************************************************/
void itoa(unsigned int Value, char *Buffer)
{
unsigned char i;
unsigned int Digit;
unsigned int Divisor;
enum {FALSE = 0, TRUE} Printed = FALSE;
if(Value)
{
for(i = 0, Divisor = 10000; i < 5; i++)
{
Digit = Value/Divisor;
if(Digit || Printed)
{
*Buffer++ = '0' + Digit;
Value -= Digit*Divisor;
Printed = TRUE;
}
Divisor /= 10;
}
}
else
{
*Buffer++ = '0';
}
*Buffer = '\0';
}
#endif
/*********************************************************************
* Function: BYTE hexatob(WORD_VAL AsciiChars)
*
* PreCondition: None
*
* Input: Two ascii bytes; each ranged '0'-'9', 'A'-'F', or
* 'a'-'f'
*
* Output: The resulting packed byte: 0x00-0xFF
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
BYTE hexatob(WORD_VAL AsciiChars)
{
// Convert lowercase to uppercase
if(AsciiChars.v[1] > 'F')
AsciiChars.v[1] -= 'a'-'A';
if(AsciiChars.v[0] > 'F')
AsciiChars.v[0] -= 'a'-'A';
// Convert 0-9, A-F to 0x0-0xF
if(AsciiChars.v[1] > '9')
AsciiChars.v[1] -= 'A' - 10;
else
AsciiChars.v[1] -= '0';
if(AsciiChars.v[0] > '9')
AsciiChars.v[0] -= 'A' - 10;
else
AsciiChars.v[0] -= '0';
// Concatenate
return (AsciiChars.v[1]<<4) | AsciiChars.v[0];
}
/*********************************************************************
* Function: BYTE btohexa_high(BYTE b)
*
* PreCondition: None
*
* Input: One byte ranged 0x00-0xFF
*
* Output: An ascii byte (always uppercase) between '0'-'9'
* or 'A'-'F' that corresponds to the upper 4 bits of
* the input byte.
* ex: b = 0xAE, btohexa_high() returns 'A'
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
BYTE btohexa_high(BYTE b)
{
b >>= 4;
return (b>0x9u) ? b+'A'-10:b+'0';
}
/*********************************************************************
* Function: BYTE btohexa_low(BYTE b)
*
* PreCondition: None
*
* Input: One byte ranged 0x00-0xFF
*
* Output: An ascii byte (always uppercase) between '0'-'9'
* or 'A'-'F' that corresponds to the lower 4 bits of
* the input byte.
* ex: b = 0xAE, btohexa_low() returns 'E'
*
* Side Effects: None
*
* Overview: None
*
* Note: None
********************************************************************/
BYTE btohexa_low(BYTE b)
{
b &= 0x0F;
return (b>9u) ? b+'A'-10:b+'0';
}
WORD swaps(WORD v)
{
WORD_VAL t;
BYTE b;
t.Val = v;
b = t.v[1];
t.v[1] = t.v[0];
t.v[0] = b;
return t.Val;
}
DWORD swapl(DWORD v)
{
// Swap bytes 0 and 3
((DWORD_VAL*)&v)->v[0] ^= ((DWORD_VAL*)&v)->v[3];
((DWORD_VAL*)&v)->v[3] ^= ((DWORD_VAL*)&v)->v[0];
((DWORD_VAL*)&v)->v[0] ^= ((DWORD_VAL*)&v)->v[3];
// Swap bytes 1 and 2
((DWORD_VAL*)&v)->v[1] ^= ((DWORD_VAL*)&v)->v[2];
((DWORD_VAL*)&v)->v[2] ^= ((DWORD_VAL*)&v)->v[1];
((DWORD_VAL*)&v)->v[1] ^= ((DWORD_VAL*)&v)->v[2];
return v;
}
WORD CalcIPChecksum(BYTE* buffer, WORD count)
{
WORD i;
WORD *val;
DWORD_VAL sum = {0x00000000ul};
i = count >> 1;
val = (WORD*)buffer;
// Calculate the sum of all words
while(i--)
sum.Val += (DWORD)*val++;
// Add in the sum of the remaining byte, if present
if(((WORD_VAL*)&count)->bits.b0)
sum.Val += (DWORD)*(BYTE*)val;
// Do an end-around carry (one's complement arrithmatic)
sum.Val = (DWORD)sum.w[0] + (DWORD)sum.w[1];
// Do another end-around carry in case if the prior add
// caused a carry out
sum.w[0] += sum.w[1];
// Return the resulting checksum
return ~sum.w[0];
}
/*********************************************************************
* Function: WORD CalcIPBufferChecksum(WORD len)
*
* PreCondition: TCPInit() is already called AND
* MAC buffer pointer set to starting of buffer
*
* Input: len - Total number of bytes to calculate
* checksum for.
*
* Output: 16-bit checksum as defined by rfc 793.
*
* Side Effects: None
*
* Overview: This function performs checksum calculation in
* MAC buffer itself.
*
* Note: None
********************************************************************/
#if defined(NON_MCHP_MAC)
WORD CalcIPBufferChecksum(WORD len)
{
DWORD_VAL Checksum = {0x00000000ul};
WORD ChunkLen;
BYTE DataBuffer[20]; // Must be an even size
WORD *DataPtr;
while(len)
{
// Obtain a chunk of data (less SPI overhead compared
// to requesting one byte at a time)
ChunkLen = len > sizeof(DataBuffer) ? sizeof(DataBuffer) : len;
MACGetArray(DataBuffer, ChunkLen);
len -= ChunkLen;
// Take care of a last odd numbered data byte
if(((WORD_VAL*)&ChunkLen)->bits.b0)
{
DataBuffer[ChunkLen] = 0x00;
ChunkLen++;
}
// Calculate the checksum over this chunk
DataPtr = (WORD*)&DataBuffer[0];
while(ChunkLen)
{
Checksum.Val += *DataPtr++;
ChunkLen -= 2;
}
}
// Do an end-around carry (one's complement arrithmatic)
Checksum.Val = (DWORD)Checksum.w[0] + (DWORD)Checksum.w[1];
// Do another end-around carry in case if the prior add
// caused a carry out
Checksum.w[0] += Checksum.w[1];
// Return the resulting checksum
return ~Checksum.w[0];
}
#endif
/*********************************************************************
* Function: char *strupr(char *s)
*
* PreCondition: None
*
* Input: s: Pointer to a null terminated string to convert.
*
* Output: char* return: Pointer to the initial string
* *s: String is updated in to have all upper case
* characters
*
* Side Effects: None
*
* Overview: The function sequentially converts all lower case
* characters in the input s string to upper case
* characters. Non a-z characters are skipped.
*
* Note: None
********************************************************************/
#if defined(__C30__) || defined(HI_TECH_C) || defined (__PIC32MX__)
char *strupr(char *s)
{
char c;
char *t;
t = s;
while( (c = *t) )
{
if(c >= 'a' && c <= 'z')
{
*t -= ('a' - 'A');
}
t++;
}
return s;
}
#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?