Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #include <inttypes.h>
- #include <ctype.h>
- // MARK: - Definitions.
- typedef uint8_t octet;
- int snap(FILE * fp, void const * opvar, size_t op_l);
- /*
- * MARK: main()
- */
- int main(int argc, char const * argv[]) {
- char bfr[1024] = { '\0', };
- size_t bfr_sz = sizeof bfr / sizeof *bfr;
- int onek = 1024;
- double G333 = 6.333;
- char const * alan_kana = "アラン";
- char const * alan_kanaU = "\u30a2\u30e9\u30f3"; // UTF-8: 0xE3 0x82 0xA2 - 0xE3 0x83 0xA9 - 0xE3 0x83 0xB3
- char const * alan_hira = "あらん";
- char const * alan_hiraU = "\u3042\u3089\u3093"; // UTF-8: 0xE3 0x81 0x82 - 0xE3 0x82 0x89 - 0xE3 0x82 0x93
- sprintf(bfr, "%5d %7.3f %08x %s %s", onek, G333, onek, alan_kana, alan_hira);
- printf("%4zu: %s\n", strlen(bfr), bfr);
- snprintf(bfr, bfr_sz, "%5d %7.3f %08x %s %s", onek, G333, onek, alan_kanaU, alan_hiraU);
- printf("%4zu: %s\n", strlen(bfr), bfr);
- snap(stdout, bfr, strlen(bfr) + 1);
- return 0;
- }
- /*
- * MARK: snap()
- * SVC 51 (0A33)
- */
- int snap(FILE * fp, void const * opvar, size_t op_l) {
- octet const * op = (octet const *) opvar;
- int chr_ct = 0;
- size_t const LL = 0x20; // snap line length (32)
- size_t const GP = 0x08; // distance between print gaps
- octet const * op_end = op + op_l;
- octet const * op_now = op;
- chr_ct += fprintf(fp, "%p:\n", (void *) op);
- while (op_now < op_end) {
- octet const * hex_p = op_now;
- octet const * chr_p = op_now;
- chr_ct += fprintf(fp, "%04" PRIxPTR ": ", hex_p - op);
- for (size_t xc = 0, sp = 1; xc < LL; ++xc) {
- octet const * loc = hex_p++;
- if (loc < op_end) {
- octet x_ = *loc;
- chr_ct += fprintf(fp, "%02" PRIx8 "%s", x_, (sp++ % GP == 0) ? " " : "");
- }
- else {
- chr_ct += fprintf(fp, " %s", (sp++ % GP == 0) ? " " : "");
- }
- }
- chr_ct += fprintf(fp, " ");
- for (size_t cc = 0, sp = 1; cc < LL; ++cc) {
- octet const * loc = chr_p++;
- octet c_ = loc < op_end ? *loc : ' ';
- chr_ct += fprintf(fp, "%c%s", isprint(c_) ? c_ : '.', (sp++ % GP == 0) ? " " : "");
- }
- chr_ct += fprintf(fp, "\n");
- op_now += LL;
- }
- chr_ct += fprintf(fp, "\n");
- return chr_ct;
- }
- /*
- $ clang -Wall -Wpedantic -Werror=vla -fsanitize=address -std=gnu2x -o ../bin/sprintf_dep sprintf_dep.c && ../bin/sprintf_dep
- sprintf_dep.c:26:3: warning: 'sprintf' is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]
- sprintf(bfr, "%5d %7.3f %08x %s %s", onek, G333, onek, alan_kana, alan_hira);
- ^
- /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdio.h:188:1: note: 'sprintf' has been explicitly marked deprecated here
- __deprecated_msg("This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead.")
- ^
- /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h:215:48: note: expanded from macro '__deprecated_msg'
- #define __deprecated_msg(_msg) __attribute__((__deprecated__(_msg)))
- ^
- 1 warning generated.
- sprintf_dep(12209,0x114b9d600) malloc: nano zone abandoned due to inability to preallocate reserved vm space.
- 42: 1024 6.333 00000400 アラン あらん
- 42: 1024 6.333 00000400 アラン あらん
- 0x7ff7b7269ec0:
- 0000: 2031303234202020 362e333333203030 30303034303020e3 82a2e383a9e383b3 1024 6.333 00 000400 . ........
- 0020: 20e38182e38289e3 829300 ....... ...
- $
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement