2 * tfcrypt -- high security Threefish encryption tool.
4 * tfcrypt is copyrighted:
5 * Copyright (C) 2012-2019 Andrey Rys. All rights reserved.
7 * tfcrypt is licensed to you under the terms of std. MIT/X11 license:
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #if UINTPTR_MAX == UINT32_MAX
42 #elif UINTPTR_MAX == UINT64_MAX
44 #elif UINTPTR_MAX == UINT16_MAX
48 struct mhexdump_args {
60 #if defined(MACHINE_32BIT)
61 #define ADDRFMT "%08x: "
62 #define paddr (mha->addaddr == 2 ? (uint32_t)P+(x*mha->group) : (x*mha->group))
63 #elif defined(MACHINE_64BIT)
64 #define ADDRFMT "%016lx: "
65 #define paddr (mha->addaddr == 2 ? (uint64_t)P+(x*mha->group) : (x*mha->group))
66 #elif defined(MACHINE_16BIT)
67 #define ADDRFMT "%04x: "
68 #define paddr (mha->addaddr == 2 ? (uint16_t)P+(x*mha->group) : (x*mha->group))
70 #error No machine word size detected!
73 #define BYTEOUT ((unsigned char)P[y+(x*mha->group)])
75 int fmhexdump(const struct mhexdump_args *mha)
77 const unsigned char *P = mha->data;
80 if (!mha->fp || !mha->data || mha->szdata == 0) return 0;
82 for (x = 0; x < mha->szdata/mha->group; x++) {
83 if (mha->addaddr) fprintf(mha->fp, ADDRFMT, paddr);
84 for (y = 0; y < mha->group; y++) {
85 fprintf(mha->fp, "%02hhx", BYTEOUT);
86 if (((y+1) % mha->hexgroup) == 0 && (y != (mha->group)-1)) fputc(' ', mha->fp);
88 if (mha->hexstr) fprintf(mha->fp, " ");
89 if (mha->hexstr) for (y = 0; y < mha->group; y++) {
90 if (isprint(BYTEOUT)) fprintf(mha->fp, "%c", BYTEOUT);
91 else fputc('.', mha->fp);
93 if (mha->szdata/mha->group == 1 && mha->szdata-mha->group == 0) {
94 if (mha->newline) fputc('\n', mha->fp);
96 else fputc('\n', mha->fp);
98 if (mha->szdata-(x*mha->group) == 0) goto _ret;
100 if (mha->addaddr) fprintf(mha->fp, ADDRFMT, paddr);
101 for (y = 0; y < mha->szdata-(x*mha->group); y++) {
102 fprintf(mha->fp, "%02hhx", BYTEOUT);
103 if (((y+1) % mha->hexgroup) == 0) fputc(' ', mha->fp);
105 if (mha->hexstr) for (; y < mha->group; y++) {
106 fprintf(mha->fp, " ");
107 if (((y+1) % mha->hexgroup) == 0 && (y != mha->group-1)) fputc(' ', mha->fp);
109 if (mha->hexstr) fprintf(mha->fp, " ");
110 if (mha->hexstr) for (y = 0; y < mha->szdata-(x*mha->group); y++) {
111 if (isprint(BYTEOUT)) fprintf(mha->fp, "%c", BYTEOUT);
112 else fputc('.', mha->fp);
115 if (mha->newline) fputc('\n', mha->fp);
117 _ret: fflush(mha->fp);
118 if (mha->closef) fclose(mha->fp);
124 int xmhexdump(int to, const void *data, size_t szdata, int hgroup, int hexstr, int newline)
126 struct mhexdump_args mha;
128 if (hgroup == 0) hgroup = 16;
130 memset(&mha, 0, sizeof(struct mhexdump_args));
131 mha.fp = (to == 2) ? stderr : stdout;
136 mha.hexgroup = hgroup;
139 mha.newline = newline;
141 return fmhexdump(&mha);