Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial commit

  • Loading branch information...
commit 924ee443774dcb7c214df344aafe006009b50315 0 parents
Adam Rutkowski aerosol authored

Showing 2 changed files with 125 additions and 0 deletions. Show diff stats Hide diff stats

  1. +27 0 README.md
  2. +98 0 src/binpp.erl
27 README.md
Source Rendered
... ... @@ -0,0 +1,27 @@
  1 +binpp - Erlang Binary Pretty Printer
  2 +===================================
  3 +
  4 +Example usage:
  5 +
  6 + 1> Bin.
  7 + <<12,242,207,49,82,69,45,130,212,69,80,88,8,81,23,36,86,7,
  8 + 68,19,133,97,51,216,56,145,88,8,81,...>>
  9 + 2> binpp:pprint(Bin).
  10 + 0C F2 CF 31 52 45 2D 82 D4 45 50 58 08 51 17 24 .òÏ1RE-‚ÔEPX.Q.$
  11 + 56 07 44 13 85 61 33 D8 38 91 58 08 51 17 24 56 V.D.…a3Ø8‘X.Q.$V
  12 + 0A 14 20 4E 24 16 09 60 F4 0A 15 11 01 30 13 89 .. N$..`ô....0.‰
  13 + 05 81 0F 09 15 C5 61 33 D8 54 91 52 5D 81 17 24 ....Åa3ØT‘R].$
  14 + 11 14 60 23 D1 3D 55 80 ..`#Ñ=U€
  15 + ok
  16 + 3> binpp:format(Bin, bin).
  17 + "00001100 11110010 11001111 00110001 01010010 01000101 00101101 10000010
  18 + 11010100 01000101 01010000 01011000 00001000 01010001 00010111 00100100
  19 + 01010110 00000111 01000100 00010011 10000101 01100001 00110011 11011000
  20 + 00111000 10010001 01011000 00001000 01010001 00010111 00100100 01010110
  21 + 00001010 00010100 00100000 01001110 00100100 00010110 00001001 01100000
  22 + 11110100 00001010 00010101 00010001 00000001 00110000 00010011 10001001
  23 + 00000101 10000001 00001111 00001001 00010101 11000101 01100001 00110011
  24 + 11011000 01010100 10010001 01010010 01011101 10000001 00010111 00100100
  25 + 00010001 00010100 01100000 00100011 11010001 00111101 01010101 10000000"
  26 +
  27 +That's all folks!
98 src/binpp.erl
... ... @@ -0,0 +1,98 @@
  1 +% DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  2 +% Version 2, December 2004
  3 +%
  4 +% Copyright (C) 2011 Adam Rutkowski <adam@mtod.org>
  5 +%
  6 +% Everyone is permitted to copy and distribute verbatim or modified
  7 +% copies of this license document, and changing it is allowed as long
  8 +% as the name is changed.
  9 +%
  10 +% DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  11 +% TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  12 +%
  13 +% 0. You just DO WHAT THE FUCK YOU WANT TO.
  14 +
  15 +-module(binpp).
  16 +-author('Adam Rutkowski adam@mtod.org').
  17 +-export([pprint/1]).
  18 +-export([format/1, format/2]).
  19 +-export([convert/1, convert/2]).
  20 +
  21 +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  22 +% API %
  23 +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  24 +
  25 +-spec convert(binary(), atom()) -> {ok, list()}.
  26 +
  27 +convert(Bin, hex) when is_binary(Bin) ->
  28 + convert(Bin, [], fun hexstr/1);
  29 +
  30 +convert(Bin, bin) when is_binary(Bin) ->
  31 + convert(Bin, [], fun binstr/1).
  32 +
  33 +-spec convert(binary()) -> {ok, list()}.
  34 +
  35 +convert(Bin) when is_binary(Bin) ->
  36 + convert(Bin).
  37 +
  38 +-spec format(binary(), atom()) -> ok.
  39 +
  40 +format(Bin, Base) ->
  41 + {ok, Octets} = convert(Bin, Base),
  42 + io:format("~p~n", [string:join(Octets, " ")]),
  43 + ok.
  44 +
  45 +-spec format(binary()) -> ok.
  46 +
  47 +format(Bin) ->
  48 + format(Bin, hex),
  49 + ok.
  50 +
  51 +-spec pprint(binary()) -> ok.
  52 +
  53 +pprint(Bin) ->
  54 + {ok, Octets} = convert(Bin, hex),
  55 + Buckets = buckets(16, Octets),
  56 + lists:foreach(fun print_bucket/1, Buckets),
  57 + ok.
  58 +
  59 +
  60 +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  61 +% Core :) %
  62 +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  63 +
  64 +
  65 +convert(<<>>, Acc, _) ->
  66 + {ok, lists:reverse(Acc)};
  67 +
  68 +convert(<<Bin:8/integer, Rest/binary>>, SoFar, FormatFun) ->
  69 + convert(Rest, [FormatFun(Bin)|SoFar], FormatFun).
  70 +
  71 +print_bucket(Bucket) ->
  72 + OctetLine = string:join(Bucket, " "),
  73 + OctetRepr = lists:map(
  74 + fun(B) ->
  75 + case list_to_integer(B, 16) of
  76 + Code when Code >= 32 -> Code;
  77 + _Else -> $.
  78 + end
  79 + end,
  80 + Bucket),
  81 + io:format("~s ~s~n", [string:left(OctetLine, 16*2 + 16, $ ), OctetRepr]).
  82 +
  83 +hexstr(B) -> string:right(integer_to_list(B, 16), 2, $0).
  84 +binstr(B) -> string:right(integer_to_list(B, 2), 8, $0).
  85 +
  86 +%% Divide list L into X lists of size N
  87 +%% courtesy of MononcQc
  88 +buckets(N, L) ->
  89 + buckets(1, N, length(L) div N, L, [[]]).
  90 +buckets(_, _, 0, [], [[]|Acc]) ->
  91 + lists:reverse(Acc);
  92 +buckets(_, _, 0, Rest, [[]|Acc]) ->
  93 + lists:reverse([Rest|Acc]);
  94 +buckets(N, N, M, [H|T], [A|Acc]) ->
  95 + buckets(1, N, M-1, T, [[], lists:reverse([H|A]) | Acc]);
  96 +buckets(X, N, M, [H|T], [A|Acc]) ->
  97 + buckets(X+1, N, M, T, [[H|A]|Acc]).
  98 +

0 comments on commit 924ee44

Please sign in to comment.
Something went wrong with that request. Please try again.