format.c

来自「WinCE 3.0 BSP, 包含Inter SA1110, Intel_815」· C语言 代码 · 共 502 行

C
502
字号
/*****************************************************************************
* 
*   THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
*   ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
*   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
*   PARTICULAR PURPOSE.
*   Copyright (c) 1998 Hitachi,Ltd.
*
*   @doc EXTERNAL BOOTLOAD
*
*   @module format.c | Boot loader format output support
*
*   @comm
*           This file contains simple formatted output string support for the
*           boot loader.
*
*           Note:
*
*           supported, with no frills: x d s u X, H, and B
*           backslash n is converted to backslash n backslash r.
*
*           X is equivalent to 08x; B, 02x; and H, 04x in printf format.
*
******************************************************************************/
#include <windows.h>
#include <stdarg.h>
#include <loader.h>
#include <oemfw.h>

//
// Functional Prototypes
//
void pOutputByte(unsigned char c);
void pOutputNumHex(unsigned long   n, long            depth);
void pOutputNumDecimal(unsigned long n);
void OEMWriteDebugByte(unsigned char);

char *szSprintf=0;

//
// Routine starts
//
/*****************************************************************************
*
*
*   @func   void    |   OutputFormatString | Simple formatted output string routine
*
*   @rdesc  none
*
*   @parm   const unsigned char * |   sz,... |
*               Format String:
*
*               @flag Format string | type
*               @flag u | unsigned
*               @flag d | int
*               @flag c | char
*               @flag s | string
*               @flag x | 4-bit hex number
*               @flag B | 8-bit hex number
*               @flag H | 16-bit hex number
*               @flag X | 32-bit hex number
*
*   @comm
*           Same as FormatString, but output to serial port instead of buffer.
*/
#if 0
void OutputDebugStringW(const unsigned char *sz, ...)
{
}
#endif

void OutputFormatString(const unsigned char *sz, ...)
{
    unsigned int    c;
    va_list         vl;
    
    va_start(vl, sz);
    
    while (*sz) {
        c = *sz++;
        switch (c) { 
            case (unsigned char)'%':
                c = *sz++;
                switch (c) { 
                    case 'x':
                        pOutputNumHex(va_arg(vl, unsigned long), 0);
                        break;
                    case 'B':
                        pOutputNumHex(va_arg(vl, unsigned long), 2);
                        break;
                    case 'H':
                        pOutputNumHex(va_arg(vl, unsigned long), 4);
                        break;
                    case 'X':
                        pOutputNumHex(va_arg(vl, unsigned long), 8);
                        break;
                    case 'd':
                        {
                            long    l;
                
                            l = va_arg(vl, long);
                            if (l < 0) { 
                                pOutputByte('-');
                                l = - l;
                            }
                            pOutputNumDecimal((unsigned long)l);
                        }
                        break;
                    case 'u':
                        pOutputNumDecimal(va_arg(vl, unsigned long));
                        break;
                    case 's':
                        OutputString(va_arg(vl, char *));
                        break;
                    case '%':
                        pOutputByte('%');
                        break;
                    case 'c':
                        c = va_arg(vl, unsigned char);
                        pOutputByte(c);
                        break;
                
                    default:
                        pOutputByte(' ');
                        break;
                }
                break;
            case '\n':
                pOutputByte('\r');
            // fall through
            default:
                pOutputByte(c);
        }
    }
    
    va_end(vl);
}

/*****************************************************************************
*
*
*   @func   void    |   FormatString | Simple formatted output string routine
*
*   @rdesc  Returns length of formatted string
*
*   @parm   unsigned char * |   pBuf |
*               Pointer to string to return formatted output.  User must ensure
*               that buffer is large enough.
*
*   @parm   const unsigned char * |   sz,... |
*               Format String:
*
*               @flag Format string | type
*               @flag u | unsigned
*               @flag d | int
*               @flag c | char
*               @flag s | string
*               @flag x | 4-bit hex number
*               @flag B | 8-bit hex number
*               @flag H | 16-bit hex number
*               @flag X | 32-bit hex number
*
*   @comm
*           Same as OutputFormatString, but output to buffer instead of serial port.
*/
unsigned int FormatString(unsigned char *pBuf, const unsigned char *sz, ...)
{
    unsigned int    c;
    va_list         vl;
    
    va_start(vl, sz);
    
    szSprintf = pBuf;
    while (*sz) {
        c = *sz++;
        switch (c) { 
            case (unsigned char)'%':
                c = *sz++;
                switch (c) { 
                    case 'x':
                        pOutputNumHex(va_arg(vl, unsigned long), 0);
                        break;
                    case 'B':
                        pOutputNumHex(va_arg(vl, unsigned long), 2);
                        break;
                    case 'H':
                        pOutputNumHex(va_arg(vl, unsigned long), 4);
                        break;
                    case 'X':
                        pOutputNumHex(va_arg(vl, unsigned long), 8);
                        break;
                    case 'd':
                        {
                            long    l;
                
                            l = va_arg(vl, long);
                            if (l < 0) { 
                                pOutputByte('-');
                                l = - l;
                            }
                            pOutputNumDecimal((unsigned long)l);
                        }
                        break;
                    case 'u':
                        pOutputNumDecimal(va_arg(vl, unsigned long));
                        break;
                    case 's':
                        OutputString(va_arg(vl, char *));
                        break;
                    case '%':
                        pOutputByte('%');
                        break;
                    case 'c':
                        c = va_arg(vl, unsigned char);
                        pOutputByte(c);
                        break;
                
                    default:
                        pOutputByte(' ');
                        break;
                }
                break;
            case '\n':
                pOutputByte('\r');
            // fall through
            default:
                pOutputByte(c);
        }
    }
    pOutputByte(0);
    c = szSprintf - pBuf;
    szSprintf = 0;
    va_end(vl);
    return (c);
}

/*****************************************************************************
*
*
*   @func   void    |   pOutputByte | Sends a byte out of the monitor port.
*
*   @rdesc  none
*
*   @parm   unsigned int |   c |
*               Byte to send.
*
*/
void pOutputByte(unsigned char c)
{
    if (szSprintf)
        *szSprintf++ = c;
    else
        OEMWriteDebugByte(c);
}


/*****************************************************************************
*
*
*   @func   void    |   pOutputNumHex | Print the hex representation of a number through the monitor port.
*
*   @rdesc  none
*
*   @parm   unsigned long |   n |
*               The number to print.
*
*   @parm   long | depth |
*               Minimum number of digits to print.
*
*/
void pOutputNumHex(unsigned long   n, long            depth)
{
    if (depth) {
        depth--;
    }
    
    if ((n & ~0xf) || depth) {
        pOutputNumHex(n >> 4, depth);
        n &= 0xf;
    }
    
    if (n < 10) {
        pOutputByte((unsigned char)n + '0');
    } else { 
        pOutputByte((unsigned char)n - 10 + 'A');
    }
}


/*****************************************************************************
*
*
*   @func   void    |   pOutputNumDecimal | Print the decimal representation of a number through the monitor port.
*
*   @rdesc  none
*
*   @parm   unsigned long |   n |
*               The number to print.
*
*/
void pOutputNumDecimal(unsigned long n)
{
    if (n >= 10) {
        pOutputNumDecimal(n / 10);
        n %= 10;
    }
    pOutputByte((unsigned char)n + '0');
}

/*****************************************************************************
*
*
*   @func   void    |   OutputString | Sends an unformatted string to the monitor port.
*
*   @rdesc  none
*
*   @parm   const unsigned char * |   s |
*               points to the string to be printed.
*
*   @comm
*           backslash n is converted to backslash r backslash n
*/
void OutputString(const unsigned char *s)
{
    while (*s) {		
        if (*s == '\n') {
            OEMWriteDebugByte('\r');
        }
        OEMWriteDebugByte(*s++);
    }
}


/*****************************************************************************
*
*
*   @func   int    |   atox | Converts a hex string to its 32-bit value.
*
*   @rdesc  Returns 1 - successful, in this case, *value is stored in the 32-bit value.
*           0 - fail (the string is not a valid hex number).
*
*   @parm   const unsigned char * |   pBuf |
*               points to the hex string
*
*   @parm   unsigned int * | value |
*               32-bit value of hex string
*/
int atox(const unsigned char *pBuf, unsigned int        *value)
{
    unsigned int    c;
    unsigned int    result;
    int             i;
    
    result = 0;
    i = 0;
    while (*pBuf) {
        i++;
        if (i > (2 * sizeof(unsigned int ))) {
            return 0;
        }
        c = *pBuf++;                   // get leftmost charachter
        c -= '0';                      // normalize to zero
        if (c > 9) {
            // account for discontinuity between '0'..'9' and 'A'..'Z'
            c -= 7;
            if (c > 15) {
                // account for lower case letters
                c -= 32;
            }
        }
        if (c > 15) {
            return 0;
        }
        result = (result << sizeof(unsigned int )) | c;
    }
    *value = result;
    return 1;
}

/*****************************************************************************
*
*
*   @func   unsigned char    |   toascii | Translate ASCII code
*
*   @rdesc  Return Value: The byte itself if it is a printable character.
*           Otherwise a '.'.
*
*   @parm   unsigned char | c |
*               The given byte
*
*/
unsigned char toascii(unsigned char   c)
{
    return (c >= 0x20 && c <= 0x7e) ? c : '.';
}

/* Copied this from eboot/format.c */
void
EdbgOutputDebugString(
    const unsigned char *sz, ...
)
{
    unsigned char    c;
    va_list         vl;
    
    va_start(vl, sz);
    
    while (*sz) {
        c = *sz++;
        switch (c) { 
            case (unsigned char)'%':
            c = *sz++;
            switch (c) { 
                case 'x':
                pOutputNumHex(va_arg(vl, unsigned long), 0);
                break;
                case 'B':
                pOutputNumHex(va_arg(vl, unsigned long), 2);
                break;
                case 'H':
                pOutputNumHex(va_arg(vl, unsigned long), 4);
                break;
                case 'X':
                pOutputNumHex(va_arg(vl, unsigned long), 8);
                break;
                case 'd': {
                long    l;
                
                l = va_arg(vl, long);
                if (l < 0) { 
                    pOutputByte('-');
                    l = - l;
                }
                pOutputNumDecimal((unsigned long)l);
            }
                break;
                case 'u':
                pOutputNumDecimal(va_arg(vl, unsigned long));
                break;
                case 's':
                OutputString(va_arg(vl, char *));
                break;
                case '%':
                pOutputByte('%');
                break;
                case 'c':
                c = va_arg(vl, unsigned char);
                pOutputByte(c);
                break;
                
                default:
                pOutputByte(' ');
                break;
            }
            break;
            case '\n':
            pOutputByte('\r');
            // fall through
            default:
            pOutputByte(c);
        }
    }
    
    va_end(vl);
}

// This routine will take a binary IP address as represent here and return a dotted decimal version of it
char *inet_ntoa( DWORD dwIP ) {

    static char szDottedD[16];

    FormatString( szDottedD, "%u.%u.%u.%u",
        (BYTE)dwIP, (BYTE)(dwIP >> 8), (BYTE)(dwIP >> 16), (BYTE)(dwIP >> 24) );

    return szDottedD;

} // inet_ntoa()

// This routine will take a dotted decimal IP address as represent here and return a binary version of it
DWORD inet_addr( char *pszDottedD ) {

    DWORD dwIP = 0;
    DWORD cBytes;
    char *pszLastNum;
    int atoi (const char *s);
    
    // Replace the dots with NULL terminators
    pszLastNum = pszDottedD;
    for( cBytes = 0; cBytes < 4; cBytes++ ) {
        while(*pszDottedD != '.' && *pszDottedD != '\0')
            pszDottedD++;
        if (pszDottedD == '\0' && cBytes != 3)
            return 0;
        *pszDottedD = '\0';
        dwIP |= (atoi(pszLastNum) & 0xFF) << (8*cBytes);
        pszLastNum = ++pszDottedD;
    }

    return dwIP;

} // inet_ntoa()

⌨️ 快捷键说明

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