|
|
@ -5,6 +5,8 @@ |
|
|
|
, genSrcs/2 |
|
|
|
, notCan/2 |
|
|
|
, reToDir/2 |
|
|
|
, delFile/2 |
|
|
|
, isHasUnicode/1 |
|
|
|
]). |
|
|
|
|
|
|
|
%% 通过beam生成erl文件,生成的beam编译选项必要带debug_info才能反编译生成代码 |
|
|
@ -46,6 +48,59 @@ genSrcs(BeamDir, SrcDir) -> |
|
|
|
end, |
|
|
|
filelib:fold_files(BeamDir, "\\.beam$", true, FunDeal, []). |
|
|
|
|
|
|
|
%% 将反编译出来的源文件 -file 属性删除掉 |
|
|
|
delFile(SrcDir, NewDir) -> |
|
|
|
FunDeal = |
|
|
|
fun(File, ProAcc) -> |
|
|
|
ModName = filename:basename(File, ".erl"), |
|
|
|
Module = list_to_atom(ModName), |
|
|
|
{ok, CodeBin} = file:read_file(File), |
|
|
|
SrcBin = doDelFile(CodeBin, <<>>), |
|
|
|
file:write_file(lists:concat([NewDir, Module, ".erl"]), SrcBin), |
|
|
|
io:format("build beam:~p to erl:~p success.~n", [Module, Module]), |
|
|
|
ProAcc |
|
|
|
end, |
|
|
|
filelib:fold_files(SrcDir, "\\.erl$", true, FunDeal, []). |
|
|
|
|
|
|
|
doDelFile(CodeBin, SrcBin) -> |
|
|
|
case binary:split(CodeBin, <<"-file(">>) of |
|
|
|
[Part1] -> |
|
|
|
<<SrcBin/binary, Part1/binary>>; |
|
|
|
[Part1, Part2] -> |
|
|
|
[_, LeftPart] = binary:split(Part2, <<").">>), |
|
|
|
delFile(LeftPart, <<SrcBin/binary, Part1/binary>>) |
|
|
|
end. |
|
|
|
|
|
|
|
%% 检查源码中是否存在unicode字符 主要是为了检查是否存在中文 |
|
|
|
isHasUnicode(SrcDir) -> |
|
|
|
FunDeal = |
|
|
|
fun(File, ProAcc) -> |
|
|
|
ModName = filename:basename(File, ".erl"), |
|
|
|
Module = list_to_atom(ModName), |
|
|
|
{ok, CodeBin} = file:read_file(File), |
|
|
|
IsHas = checkUnicode(CodeBin), |
|
|
|
|
|
|
|
case IsHas of |
|
|
|
true -> |
|
|
|
[Module | ProAcc]; |
|
|
|
_ -> |
|
|
|
ProAcc |
|
|
|
end |
|
|
|
end, |
|
|
|
AllMods = filelib:fold_files(SrcDir, "\\.erl$", true, FunDeal, []), |
|
|
|
ModStr = <<<<(atom_to_binary(OneMod))/binary, "\n">> || OneMod <- AllMods>>, |
|
|
|
file:write_file("hasUnicodeMod.txt", ModStr). |
|
|
|
|
|
|
|
checkUnicode(<<>>) -> |
|
|
|
false; |
|
|
|
checkUnicode(<<Word/utf8, Left/binary>>) -> |
|
|
|
case Word > 256 of |
|
|
|
true -> |
|
|
|
true; |
|
|
|
_ -> |
|
|
|
checkUnicode(Left) |
|
|
|
end. |
|
|
|
|
|
|
|
%% 将不能反编译的beam文件复制到指定的目录 |
|
|
|
notCan(BeamDir, SrcDir) -> |
|
|
|
FunDeal = |
|
|
|