aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ssl/src/ssl_debug.erl
blob: 625889c43bed9dc369f95f5f6175ce0615d767f9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2007-2009. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%

%%

%%% Purpose : some debug utilities

-module(ssl_debug).

-export([unhex/1, hexd/1, hex_data/2, term_data/2, hex_data/4, term_data/4, make_binary/1]).

%% external

hex_data(Name, Data) ->
    io:format("~s\n~s", [Name, hex(Data)]).

term_data(Name, Term) ->
    io:format("~s\n~p\n", [Name, Term]).

hex_data(Name, Data, Mod, Line) ->
    io:format("~w:~p ~s\n~s", [Mod, Line, Name, hex(Data)]).

term_data(Name, Term, Mod, Line) ->
    io:format("~w:~p ~s\n~p\n", [Mod, Line, Name, Term]).

unhex(S) ->
    Lines = string:tokens(S, "\n"),
    H = [unhex(L, []) || L <- Lines],
    list_to_binary(H).

make_binary(Size) ->
    crypto:rand_bytes(Size).

%% internal

is_hex_digit(C) when C >= $0, C =< $9 -> true;
is_hex_digit(C) when C >= $A, C =< $F -> true;
is_hex_digit(C) when C >= $a, C =< $f -> true;
is_hex_digit(_) -> false.

unhex([], Acc) ->
    list_to_binary(lists:reverse(Acc));
unhex([_], Acc) ->
    unhex([], Acc);
unhex([$  | Tl], Acc) ->
    unhex(Tl, Acc);
unhex([D1, D2 | Tl], Acc) ->
    case {is_hex_digit(D1), is_hex_digit(D2)} of
        {true, true} ->
            unhex(Tl, [erlang:list_to_integer([D1, D2], 16) | Acc]);
        _ ->
            unhex([], Acc)
    end.

hexd(B) ->
    io:format("~s\n", [hex(B)]).

hex(B) -> hex(erlang:iolist_to_binary(B), []).

hex_asc(B) ->
    L = binary_to_list(B),
    {hexify(L), asciify(L)}.

hex(<<B:16/binary, Rest/binary>>, Acc) ->
    {HS, AS} = hex_asc(B),
    hex(Rest, ["\n", AS, "  ", HS | Acc]);
hex(<<>>, Acc) ->
    lists:reverse(Acc);
hex(B, Acc) ->
    {HS, AS} = hex_asc(B),
    L = erlang:iolist_size(HS),
    lists:flatten(lists:reverse(Acc, [HS, lists:duplicate(3*16 - L, $ ), "  ", AS, "\n"])).

hexify(L) -> [[hex_byte(B), " "] || B <- L].

hex_byte(B) when B < 16#10 -> ["0", erlang:integer_to_list(B, 16)];
hex_byte(B) -> erlang:integer_to_list(B, 16).

asciify(L) -> [ascii_byte(C) || C <- L].

ascii_byte($") -> $.;
ascii_byte(C) when C < 32; C >= 127 -> $.;
ascii_byte(C) -> C.