Skip to content

Instantly share code, notes, and snippets.

@yswallow
Created June 21, 2016 15:51
Show Gist options
  • Select an option

  • Save yswallow/6864092828fca15e01acb61ca9f06ae4 to your computer and use it in GitHub Desktop.

Select an option

Save yswallow/6864092828fca15e01acb61ca9f06ae4 to your computer and use it in GitHub Desktop.
可読性の低いC言語で書かれたHelloWorldを出力する可読性の低いRubyツール
require 'erb'
INT_SIZE = 4
str = ARGV[0].chomp
chars = str.chars
i = 0
chars16 = []
((chars.size / INT_SIZE)+1).times do |i|
chars16[i] = []
INT_SIZE.times { chars16[i] << "%02x" % (chars.shift || "\0" ).ord }
end
strs = chars16.map(&:reverse).map(&:join)
erb_str = <<EOF
#include <stdio.h>
int main(void)
{
int c[<%= strs.size %>];
char* ptc = &c;
<% strs.each_with_index { |s,i|%>
c[<%= i %>] = 0x<%= s %>;<% } %>
printf("%s\\n",ptc);
return 0;
}
EOF
puts ERB.new(erb_str).result(binding)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment