/*
- Copyleft (c) 1999, 2016
-
- Author : scz [email protected]
- Compile : For *nix
-
: gcc -Wall -pipe -O3 -s -o hexout hexout.c
-
:
-
: strip hexout or gcc -s
-
: mcs -d hexout (only for Solaris)
-
:
- Compile : For x86/EWindows XP SP1 & VC 7.1
-
: cl hexout.c /Fehexout.exe /nologo /Os /GB /Gz /Gs65536 /W3 /WX /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /link /RELEASE
-
:
- Create : 2005-03-31 16:04
- Modify : 2006-11-30 11:56
-
- The only thing they can't take from us are our minds. !H */
#if 0
这个程序是为了配合nc快速测试一些协议相关的东西,没有更多其它意义,因为看到 TK在用类似工具,下手写一个备用。
Windows
hexout "\xd\xa\x0\0\x9\x7scz\x" | xxd -g 1 0000000: 0d 0a 00 5c 30 09 07 73 63 7a 5c 78 ...\0..scz\x
Linux
$ ./hexout "\xd\xa\x0\0\x9\x7scz\x" | xxd -g 1 0000000: 0d 0a 00 5c 30 09 07 73 63 7a 5c 78 ...\0..scz\x
Solaris
$ ./hexout "\xd\xa\x0\0\x9\x7scz\x" | od -Ax -tx1 0000000 0d 0a 00 5c 30 09 07 73 63 7a 5c 78
#endif
/************************************************************************
-
*
-
Head File *
-
*
************************************************************************/
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h>
#if !defined(WIN32) #include <unistd.h> #else #include <io.h> #endif
/************************************************************************
-
*
-
Macro *
-
*
************************************************************************/
#if defined(WIN32)
#pragma comment( linker, "/INCREMENTAL:NO" ) #pragma comment( linker, "/merge:.rdata=.text" ) #pragma comment( linker, "/subsystem:console" )
#define write _write #define fileno _fileno
#endif
/************************************************************************
-
*
-
Function Prototype *
-
*
************************************************************************/
static unsigned int dosomething ( unsigned char *in, unsigned char *out ); static void reverse ( unsigned char *in, unsigned int insize, unsigned char *out );
/************************************************************************
-
*
-
Static Global Var *
-
*
************************************************************************/
/************************************************************************/
/*
-
2005-05-11 09:42
-
处理\、\xH、\xHH、\r、\n、\t、\0这几种情形 */ static unsigned int dosomething ( unsigned char *in, unsigned char *out ) { unsigned int i, j; unsigned char str[3];
for ( i = 0, j = 0; '\0' != in[i]; i++, j++ ) { if ( '\' != in[i] ) { out[j] = in[i]; } else { switch ( in[i+1] ) { case '\': /* * 出现了两个
/ out[j] = '\'; i++; break; case 'r': / * 出现了\r / out[j] = '\r'; i++; break; case 'n': / * 出现了\n / out[j] = '\n'; i++; break; case 't': / * 出现了\t / out[j] = '\t'; i++; break; case '0': / * 出现了\0 / out[j] = '\0'; i++; break; case 'x': / * 出现了\x / str[1] = str[2] = '\0'; if ( ( in[i+2] >= '0' && in[i+2] <= '9' ) || ( in[i+2] >= 'a' && in[i+2] <= 'f' ) || ( in[i+2] >= 'A' && in[i+2] <= 'F' ) ) { i += 2; str[0] = in[i]; if ( ( in[i+1] >= '0' && in[i+1] <= '9' ) || ( in[i+1] >= 'a' && in[i+1] <= 'f' ) || ( in[i+1] >= 'A' && in[i+1] <= 'F' ) ) { i++; str[1] = in[i]; } / * 按16进制串处理 / out[j] = ( unsigned char )strtoul( str, NULL, 16 ); } else { out[j] = in[i]; } break; case '\0': / * 最后一个
/ default: / * 无需转义 / out[j] = in[i]; break; } / end of switch / } } / end of for / return( j ); } / end of dosomething */
/*
-
2006-11-30 13:47 scz
-
in、out可以重叠,insize必须大于0 */ static void reverse ( unsigned char *in, unsigned int insize, unsigned char *out ) { unsigned int i, j;
memmove( out, in, insize ); for ( i = 0, j = insize - 1; i < j; i++, j-- ) { out[i] ^= out[j]; out[j] ^= out[i]; out[i] ^= out[j]; } /* end of for / return; } / end of reverse */
#if !defined(WIN32) int main ( int argc, char * argv[] ) #else int __cdecl main ( int argc, char * argv[] ) #endif { int ret = EXIT_FAILURE; unsigned int i;
if ( argc <= 1 )
{
fprintf
(
stderr,
"Usage: %s <string> [r]\n",
argv[0]
);
goto main_exit;
}
#if defined(WIN32) /* * 对于Windows平台有这个问题,不这样设置的话,向标准输出输出0x0A时会被 * 转换成0x0D 0x0A。 */ _setmode( fileno( stdout ), _O_BINARY ); #endif i = dosomething( argv[1], argv[1] ); if ( argc >= 3 ) { if ( 'r' == argv[2][0] || 'R' == argv[2][0] ) { reverse( argv[1], i, argv[1] ); } } write( fileno( stdout ), argv[1], i ); ret = EXIT_SUCCESS;
main_exit:
return( ret );
} /* end of main */
/************************************************************************/