Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 924ee443774dcb7c214df344aafe006009b50315 0 parents
@aerosol aerosol authored
Showing with 125 additions and 0 deletions.
  1. +27 −0 README.md
  2. +98 −0 src/binpp.erl
27 README.md
@@ -0,0 +1,27 @@
+binpp - Erlang Binary Pretty Printer
+===================================
+
+Example usage:
+
+ 1> Bin.
+ <<12,242,207,49,82,69,45,130,212,69,80,88,8,81,23,36,86,7,
+ 68,19,133,97,51,216,56,145,88,8,81,...>>
+ 2> binpp:pprint(Bin).
+ 0C F2 CF 31 52 45 2D 82 D4 45 50 58 08 51 17 24 .òÏ1RE-‚ÔEPX.Q.$
+ 56 07 44 13 85 61 33 D8 38 91 58 08 51 17 24 56 V.D.…a3Ø8‘X.Q.$V
+ 0A 14 20 4E 24 16 09 60 F4 0A 15 11 01 30 13 89 .. N$..`ô....0.‰
+ 05 81 0F 09 15 C5 61 33 D8 54 91 52 5D 81 17 24 ....Åa3ØT‘R].$
+ 11 14 60 23 D1 3D 55 80 ..`#Ñ=U€
+ ok
+ 3> binpp:format(Bin, bin).
+ "00001100 11110010 11001111 00110001 01010010 01000101 00101101 10000010
+ 11010100 01000101 01010000 01011000 00001000 01010001 00010111 00100100
+ 01010110 00000111 01000100 00010011 10000101 01100001 00110011 11011000
+ 00111000 10010001 01011000 00001000 01010001 00010111 00100100 01010110
+ 00001010 00010100 00100000 01001110 00100100 00010110 00001001 01100000
+ 11110100 00001010 00010101 00010001 00000001 00110000 00010011 10001001
+ 00000101 10000001 00001111 00001001 00010101 11000101 01100001 00110011
+ 11011000 01010100 10010001 01010010 01011101 10000001 00010111 00100100
+ 00010001 00010100 01100000 00100011 11010001 00111101 01010101 10000000"
+
+That's all folks!
98 src/binpp.erl
@@ -0,0 +1,98 @@
+% DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+% Version 2, December 2004
+%
+% Copyright (C) 2011 Adam Rutkowski <adam@mtod.org>
+%
+% Everyone is permitted to copy and distribute verbatim or modified
+% copies of this license document, and changing it is allowed as long
+% as the name is changed.
+%
+% DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+% TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+%
+% 0. You just DO WHAT THE FUCK YOU WANT TO.
+
+-module(binpp).
+-author('Adam Rutkowski adam@mtod.org').
+-export([pprint/1]).
+-export([format/1, format/2]).
+-export([convert/1, convert/2]).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% API %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+-spec convert(binary(), atom()) -> {ok, list()}.
+
+convert(Bin, hex) when is_binary(Bin) ->
+ convert(Bin, [], fun hexstr/1);
+
+convert(Bin, bin) when is_binary(Bin) ->
+ convert(Bin, [], fun binstr/1).
+
+-spec convert(binary()) -> {ok, list()}.
+
+convert(Bin) when is_binary(Bin) ->
+ convert(Bin).
+
+-spec format(binary(), atom()) -> ok.
+
+format(Bin, Base) ->
+ {ok, Octets} = convert(Bin, Base),
+ io:format("~p~n", [string:join(Octets, " ")]),
+ ok.
+
+-spec format(binary()) -> ok.
+
+format(Bin) ->
+ format(Bin, hex),
+ ok.
+
+-spec pprint(binary()) -> ok.
+
+pprint(Bin) ->
+ {ok, Octets} = convert(Bin, hex),
+ Buckets = buckets(16, Octets),
+ lists:foreach(fun print_bucket/1, Buckets),
+ ok.
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Core :) %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+
+convert(<<>>, Acc, _) ->
+ {ok, lists:reverse(Acc)};
+
+convert(<<Bin:8/integer, Rest/binary>>, SoFar, FormatFun) ->
+ convert(Rest, [FormatFun(Bin)|SoFar], FormatFun).
+
+print_bucket(Bucket) ->
+ OctetLine = string:join(Bucket, " "),
+ OctetRepr = lists:map(
+ fun(B) ->
+ case list_to_integer(B, 16) of
+ Code when Code >= 32 -> Code;
+ _Else -> $.
+ end
+ end,
+ Bucket),
+ io:format("~s ~s~n", [string:left(OctetLine, 16*2 + 16, $ ), OctetRepr]).
+
+hexstr(B) -> string:right(integer_to_list(B, 16), 2, $0).
+binstr(B) -> string:right(integer_to_list(B, 2), 8, $0).
+
+%% Divide list L into X lists of size N
+%% courtesy of MononcQc
+buckets(N, L) ->
+ buckets(1, N, length(L) div N, L, [[]]).
+buckets(_, _, 0, [], [[]|Acc]) ->
+ lists:reverse(Acc);
+buckets(_, _, 0, Rest, [[]|Acc]) ->
+ lists:reverse([Rest|Acc]);
+buckets(N, N, M, [H|T], [A|Acc]) ->
+ buckets(1, N, M-1, T, [[], lists:reverse([H|A]) | Acc]);
+buckets(X, N, M, [H|T], [A|Acc]) ->
+ buckets(X+1, N, M, T, [[H|A]|Acc]).
+
Please sign in to comment.
Something went wrong with that request. Please try again.