2 * tfcrypt -- high security Threefish encryption tool.
4 * tfcrypt is copyrighted:
5 * Copyright (C) 2012-2018 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.
36 struct mhexdump_args {
48 #if SIZE_MAX == 0xffffffff
49 #define ADDRFMT "%08x: "
50 #define paddr (mha->addaddr == 2 ? (uint32_t)P+(x*mha->group) : (x*mha->group))
52 #define ADDRFMT "%016lx: "
53 #define paddr (mha->addaddr == 2 ? (uint64_t)P+(x*mha->group) : (x*mha->group))
56 #define BYTEOUT ((unsigned char)P[y+(x*mha->group)])
58 int fmhexdump(const struct mhexdump_args *mha)
60 const unsigned char *P = mha->data;
63 if (!mha->fp || !mha->data || mha->szdata == 0) return 0;
65 for (x = 0; x < mha->szdata/mha->group; x++) {
66 if (mha->addaddr) fprintf(mha->fp, ADDRFMT, paddr);
67 for (y = 0; y < mha->group; y++) {
68 fprintf(mha->fp, "%02hhx", BYTEOUT);
69 if (((y+1) % mha->hexgroup) == 0 && (y != (mha->group)-1)) fputc(' ', mha->fp);
71 if (mha->hexstr) fprintf(mha->fp, " ");
72 if (mha->hexstr) for (y = 0; y < mha->group; y++) {
73 if (isprint(BYTEOUT)) fprintf(mha->fp, "%c", BYTEOUT);
74 else fputc('.', mha->fp);
76 if (mha->szdata/mha->group == 1 && mha->szdata-mha->group == 0) {
77 if (mha->newline) fputc('\n', mha->fp);
79 else fputc('\n', mha->fp);
81 if (mha->szdata-(x*mha->group) == 0) goto _ret;
83 if (mha->addaddr) fprintf(mha->fp, ADDRFMT, paddr);
84 for (y = 0; y < mha->szdata-(x*mha->group); y++) {
85 fprintf(mha->fp, "%02hhx", BYTEOUT);
86 if (((y+1) % mha->hexgroup) == 0) fputc(' ', mha->fp);
88 if (mha->hexstr) for (; y < mha->group; y++) {
89 fprintf(mha->fp, " ");
90 if (((y+1) % mha->hexgroup) == 0 && (y != mha->group-1)) fputc(' ', mha->fp);
92 if (mha->hexstr) fprintf(mha->fp, " ");
93 if (mha->hexstr) for (y = 0; y < mha->szdata-(x*mha->group); y++) {
94 if (isprint(BYTEOUT)) fprintf(mha->fp, "%c", BYTEOUT);
95 else fputc('.', mha->fp);
98 if (mha->newline) fputc('\n', mha->fp);
100 _ret: fflush(mha->fp);
101 if (mha->closef) fclose(mha->fp);
107 int xmhexdump(int to, const void *data, size_t szdata, int hgroup, int hexstr, int newline)
109 struct mhexdump_args mha;
111 if (hgroup == 0) hgroup = 16;
113 memset(&mha, 0, sizeof(struct mhexdump_args));
114 mha.fp = (to == 2) ? stderr : stdout;
119 mha.hexgroup = hgroup;
122 mha.newline = newline;
124 return fmhexdump(&mha);