/* ==========================================================================
* hexdump.c - hexdump.c
* --------------------------------------------------------------------------
* Copyright (c) 2013 William Ahern
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
* ==========================================================================
*/
#if __STDC__ && !_XOPEN_SOURCE
#define _XOPEN_SOURCE 600 /* _setjmp(3), _longjmp(3), getopt(3) */
#endif
#include <errno.h> /* ERANGE errno */
#include <limits.h> /* INT_MAX */
#include <setjmp.h> /* _setjmp(3) _longjmp(3) */
#include <stdint.h> /* int64_t */
#include <stdio.h> /* FILE fprintf(3) snprintf(3) */
#include <stdlib.h> /* malloc(3) realloc(3) free(3) abort(3) */
#include <string.h> /* memset(3) memmove(3) */
#include "hexdump.h"
#define SAY_(fmt, ...) fprintf(stderr, fmt "%s", __FILE__, __LINE__, __func__, __VA_ARGS__);
#define SAY(...) SAY_("@@ %s:%d:%s: " __VA_ARGS__, "\n");
#define HAI SAY("HAI")
#define OOPS(...) do { \
SAY(__VA_ARGS__); \
abort(); \
} while (0)
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif
#define countof(a) (sizeof (a) / sizeof *(a))
#ifndef NOTUSED
#if __GNUC__
#define NOTUSED __attribute__((unused))
#else
#define NOTUSED
#endif
#endif
#ifndef NORETURN
#if __GNUC__
#define NORETURN __attribute__((noreturn))
#else
#define NORETURN
#endif
#endif
#if _MSC_VER && _MSC_VER < 1900 && !defined inline
#define inline __inline
#endif
#if _MSC_VER
#define NARG_OUTER(x) x
#define NARG_INNER(a, b, c, d, e, f, g, h, N,...) N
#define NARG(...) NARG_OUTER(NARG_INNER(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0))
#else
#define NARG_(a, b, c, d, e, f, g, h, N,...) N
#define NARG(...) NARG_(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#endif
#define PASTE(x, y) x##y
#define XPASTE(x, y) PASTE(x, y)
#if _MSC_VER && _MSC_VER < 1900 && !defined snprintf
#include <stdarg.h> /* va_list va_start va_end */
#define snprintf(...) hxd_snprintf(__VA_ARGS__)
static int (hxd_snprintf)(char *dst, size_t lim, const char *fmt, ...) {
va_list ap;
int n;
va_start(ap, fmt);
n = _vsnprintf(dst, lim, fmt, ap);
va_end(ap);
if (lim)
dst[lim - 1] = '\0';
return n;
}
#endif
static unsigned char toprint(unsigned char chr) {
return (chr > 0x1f && chr < 0x7f)? chr : '.';
} /* toprint() */
static const char *tooctal(char buf[3], unsigned char chr) {
if (chr > 0x1f && chr < 0x7f) {
buf[0] = chr;
buf[1] = '\0';
} else {
switch (chr) {
case '\0':
buf[0] = '\\';
buf[1] = '0';
buf[2] = '\0';
break;
case '\a':
buf[0] = '\\';
buf[1] = 'a';
buf[2] = '\0';
break;
case '\b':
buf[0] = '\\';
buf[1] = 'b';
buf[2] = '\0';
break;
case '\f':
buf[0] = '\\';
buf[1] = 'f';
buf[2] = '\0';
break;
case '\n':
buf[0] = '\\';
buf[1] = 'n';
buf[2] = '\0';
break;
case '\r':
buf[0] = '\\';
buf[1] = 'r';
buf[2] = '\0';
break;
case '\t':
buf[0] = '\\';
buf[1] = 't';
buf[2] = '\0';
break;
case '\v':
buf[0] = '\\';
buf[1] = 'v';
buf[2] = '\0';
break;
default:
buf[0] = "01234567"[0x7 & (chr >> 6)];
buf[1] = "01234567"[0x7 & (chr >> 3)];
buf[2] = "01234567"[0x7 & (chr >> 0)];
break;
}
}
return buf;
} /* tooctal() */
static const char *toshort(char buf[3], unsigned char chr) {
static const char map[][3] = {
[0x00] = "nul", [0x01] = "soh", [0x02] = "stx", [0x03] = "etx",
[0x04] = "eot", [0x05] = "enq", [0x06] = "ack", [0x07] = "bel",
[0x08] = "bs", [0x09] = "ht", [0x0a] = "lf", [0x0b] = "vt",
[0x0c] = "ff", [0x0d] = "cr", [0x0e] = "so", [0x0f] = "si",
[0x10] = "dle", [0x11] = "dc1", [0x12] = "dc2", [0x13] = "dc3",
[0x14] = "dc4", [0x15] = "nak", [0x16] = "syn", [0x17] = "etb",
[0x18] = "can", [0x19] = "em", [0x1a] = "sub", [0x1b] = "esc",
[0x1c] = "fs", [0x1d] = "gs", [0x1e] = "rs", [0x1f] = "us",
[0x7f] = "del",
};
if (chr <= 0x1f || chr == 0x7f) {
memcpy(buf, map[chr], 3);
} else if (chr < 0x7f) {
buf[0] = chr;
buf[1] = '\0';
} else {
buf[0] = "0123456789abcdef"[0x0f & (chr >> 4)];
buf[1] = "0123456789abcdef"[0x0f & chr];
buf[2] = '\0';
}
return buf;
} /* toshort() */
static inline _Bool hxd_isspace(unsigned char chr, _Bool nlok) {
static const unsigned char space[] = {
['\t'] = 1, ['\n'] = 1, ['\v'] = 1, ['\r'] = 1, ['\f'] = 1, [' '] = 1,
};
return (chr < sizeof space && space[chr] && (nlok || chr != '\n'));
} /* hxd_isspace() */
static inline unsigned char skipws(const unsigned char **fmt, _Bool nlok) {
while (hxd_isspace(**fmt, nlok))
++*fmt;
return **fmt;
} /* skipws() */
static inline int getint(const unsigned char **fmt) {
static const int limit = ((INT_MAX - (INT_MAX % 10) - 1) / 10);
int i = -1;
if (**fmt >= '0' && **fmt <= '9') {
i = 0;
do {
i *= 10;
i += **fmt - '0';
++*fmt;
} while (**fmt >= '0' && **fmt <= '9' && i <= limit);
}
return i;
} /* getint() */
#define F_HASH 1
#define F_ZERO 2
#define F_MINUS 4
#define F_SPACE 8
#define F_PLUS 16
#define FC2(x, y) (((0xff & (y)) << 8) | (0xff & (x)))
#define FC1(x) (0xff & (x))
#define FC(...) XPASTE(FC, NARG(__VA_ARGS__))(__VA_ARGS__)
static inline int getcnv(int *flags, int *width, int *prec, int *bytes, const unsigned char **fmt) {
int ch;
*flags = 0;
for (; (ch = **fmt); ++*fmt) {
switch (ch) {
case '#':
*flags |= F_HASH;
break;
case '0':
*flags |= F_ZERO;
break;
case '-':
*flags |= F_MINUS;
break;
case ' ':
*flags |= F_SPACE;
break;
case '+':
*flags |= F_PLUS;
break;
default:
goto width;
} /* switch() */
}
width:
*width = getint(fmt);
*prec = (**fmt == '.')? (++*fmt, getint(fmt)) : -1;
*bytes = 0;
switch ((ch = **fmt)) {
case '%':
break;
case 'c':
*bytes = 1;
break;
case 'd': case 'i': case 'o': case 'u': case 'X': case 'x':
*bytes = 4;
break;
case 's':
if (*prec == -1)
return 0;
*bytes = *prec;
break;
case '_':
switch (*++*fmt) {
case 'a':
switch (*++*fmt) {
case 'd':
ch = FC('_', 'd');
break;
case 'o':
ch = FC('_', 'o');
break;
case 'x':
ch = FC('_', 'x');
break;
default:
return 0;
}
*bytes = 0;
break;
case 'A':
switch (*++*fmt) {
case 'd':
ch = FC('_', 'D');
break;
case 'o':
ch = FC('_', 'O');
break;
case 'x':
ch = FC('_', 'X');
break;
default:
return 0;
}
*bytes = 0;
/* XXX: Not supported yet. */
return 0;
break;
case 'c':
ch = FC('_', 'c');
*bytes = 1;
break;
case 'p':
ch = FC(
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
hexdump.rar (4个子文件)
hexdump
bin
hexdump 24KB
Android.mk 588B
hexdump.c 43KB
hexdump.h 7KB
共 4 条
- 1
Free飝Fly
- 粉丝: 4076
- 资源: 49
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0