Skip to content

Instantly share code, notes, and snippets.

@alansam
Created October 9, 2020 20:55
Show Gist options
  • Save alansam/1986e27be077209faaa94e375aecc33a to your computer and use it in GitHub Desktop.
Save alansam/1986e27be077209faaa94e375aecc33a to your computer and use it in GitHub Desktop.
Demonstrate what lies beneath: clang & gcc
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include "../snap.h"
int main(int argc, char const * argv[]) {
char carry[9] = { 'c', 'a', 'f', 'e', 'b', 'a', 'b', 'e', '\0', };
size_t carry_sz = sizeof(carry);
short sarry[2] = { SHRT_MIN, SHRT_MAX, };
size_t sarry_sz = sizeof(sarry);
void * cap_b = &carry;
void * cap_e = cap_b + carry_sz;
void * sap_b = &sarry;
void * sap_e = sap_b + sarry_sz;
void * sb;
void * se;
size_t s_sz;
if (sap_b > cap_b) {
sb = cap_b;
se = sap_e;
}
else {
sb = sap_b;
se = cap_e;
}
s_sz = se - sb;
printf("cap - %p, %p, %zu\n", cap_b, cap_e, carry_sz);
printf("sap - %p, %p, %zu\n", sap_b, sap_e, sarry_sz);
printf("snap - %p, %p, %zu\n", sb, se, s_sz);
snap(stdout, sb, s_sz);
return 0;
}
$ # Compiled with clang:
$ clang -Wall -std=gnu18 -o ./hw077 hw077.c ./snap.c && ./hw077
cap - 0x7ffedfee344f, 0x7ffedfee3458, 9
sap - 0x7ffedfee342c, 0x7ffedfee3430, 4
snap - 0x7ffedfee342c, 0x7ffedfee3458, 44
0x7ffedfee342c:
0000: 0080ff7f09000000 000000008834eedf fe7f000000000000 0100000000000000 ........ .....4.. ........ ........
0020: 0000006361666562 61626500 ...cafeb abe.
$ # Compiled with GNU gcc:
$ gcc-10 -Wall -std=gnu18 -o ./hw077 hw077.c ./snap.c && ./hw077
cap - 0x7ffee007a40f, 0x7ffee007a418, 9
sap - 0x7ffee007a40a, 0x7ffee007a40e, 4
snap - 0x7ffee007a40a, 0x7ffee007a418, 14
0x7ffee007a40a:
0000: 0080ff7f00636166 656261626500 .....caf ebabe.
#include "snap.h"
// MARK: - Implementation.
/*
* 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", 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;
}
//
// snap.h
// CF.snap
//
// Created by Alan Sampson on 6/22/20.
// Copyright © 2020 Alan @ FreeShell. All rights reserved.
//
#ifndef snap_h
#define snap_h
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <ctype.h>
// MARK: - Definitions.
typedef uint8_t octet;
int snap(FILE * fp, void const * op, size_t op_l);
#endif /* snap_h */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment