/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-â
âvi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8                                :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â Copyright 2021 Justine Alexandra Roberts Tunney                              â
â                                                                              â
â Permission to use, copy, modify, and/or distribute this software for         â
â any purpose with or without fee is hereby granted, provided that the         â
â above copyright notice and this permission notice appear in all copies.      â
â                                                                              â
â THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                â
â WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                â
â WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             â
â AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         â
â DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        â
â PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               â
â TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             â
â PERFORMANCE OF THIS SOFTWARE.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "libc/stdio/stdio.h"

/**
 * @fileoverview Braille Dump
 *
 * Written by Justine Tunney <jtunney@gmail.com>
 * https://twitter.com/justinetunney
 *
 * Designed by Ange Albertini (corkami)
 * https://twitter.com/angealbertini
 *
 * See also `hexdump -C`.
 */

const wchar_t K[256] = L"ââºâ»â¥â¦â£â â¢ââââââªâ«â¼"  /* 0x00 */
                       L"âºâââ¼Â¶Â§â¬â¨âââââââ²â¼"  /* 0x10 */
                       L" !\"#$%&'()*+,-./" /* 0x20 */
                       L"0123456789:;<=>?"  /* 0x30 */
                       L"@ABCDEFGHIJKLMNO"  /* 0x40 */
                       L"PQRSTUVWXYZ[\\]^_" /* 0x50 */
                       L"`abcdefghijklmno"  /* 0x60 */
                       L"pqrstuvwxyz{|}~â"  /* 0x70 */
                       L"ââ¡â¢â£â  â¡ â¢ â£ â â¡â¢â£â ¤â¡¤â¢¤â£¤"  /* 0x80 */
                       L"â â¡â¢â£â ¡â¡¡â¢¡â£¡â â¡â¢â£â ¥â¡¥â¢¥â£¥"  /* 0x90 */
                       L"â â¡â¢â£â £â¡£â¢£â££â â¡â¢â£â §â¡§â¢§â£§"  /* 0xa0 */
                       L"â â¡â¢â£â ©â¡©â¢©â£©â â¡â¢â£â ­â¡­â¢­â£­"  /* 0xb0 */
                       L"â â¡â¢â£â ªâ¡ªâ¢ªâ£ªâ â¡â¢â£â ®â¡®â¢®â£®"  /* 0xc0 */
                       L"â â¡â¢â£â ±â¡±â¢±â£±â â¡â¢â£â µâ¡µâ¢µâ£µ"  /* 0xd0 */
                       L"â â¡â¢â£â ºâ¡ºâ¢ºâ£ºâ â¡â¢â£â ¾â¡¾â¢¾â£¾"  /* 0xe0 */
                       L"â â¡â¢â£â »â¡»â¢»â£»â â¡â¢â£â ¿â¡¿â¢¿â£¿"; /* 0xf0 */

int main(int argc, char *argv[]) {
  long o;
  FILE *f;
  int b, c, i, n;
  wchar_t B[17], *q;
  char R[16], A[50], *p;
  if (!(f = argc > 1 ? fopen(argv[1], "r") : stdin)) {
    fprintf(stderr, "ERROR: COULD NOT OPEN: %s\n", argv[1]);
    return 1;
  }
  for (b = o = 0;;) {
    if (!(n = fread(R, 1, 16, f))) break;
    p = A, q = B;
    for (c = i = 0; i < n; ++i) {
      if (i == 8) *p++ = ' ';
      *p++ = "0123456789abcdef"[(R[i] & 0xF0) >> 4];
      *p++ = "0123456789abcdef"[(R[i] & 0x0F) >> 0];
      *p++ = ' ';
      *q++ = K[R[i] & 255];
      c |= R[i];
    }
    if (c || !o) {
      *p = *q = b = 0;
      printf("%08x  %-49s â%lsâ\n", o, A, B);
    } else if (!b) {
      b = 1;
      printf("*\n");
    }
    o += n;
  }
  if (o) printf("%08x\n", o);
  return !feof(f);
}
