<commit_before>require 'metasm/main'

module Metasm
class ExeFormat
	attr_accessor :cpu, :encoded

	# creates a new instance, populates self.encoded with the supplied string
	def self.load(str, *a)
		e = new(*a)
		e.encoded << str
		e
	end

	# same as +load+, but from a file
	# uses VirtualFile if available
	def self.load_file(path, *a)
		if defined? VirtualFile
			load(VirtualFile.read(path), *a)
		else
			File.open(path, 'rb') { |fd| load(fd.read, *a) }
		end
	end

	# +load_file+ then decode
	def self.decode_file(path, *a)
		e = load_file(path, *a)
		e.decode
		e
	end

	# +load_file+ then decode header
	def self.decode_file_header(path, *a)
		e = load_file(path, *a)
		e.decode_header
		e
	end

	# creates a new object using the specified cpu, parses the asm source, and assemble
	def self.assemble(cpu, source)
		ex = new(cpu)
		ex.parse(source)
		ex.assemble
		ex
	end

	# encodes the executable as a string, checks that all relocations are
	# resolved, and returns the raw string version
	def encode_string(*a)
		encode(*a)
		raise "Unresolved relocations #{@encoded.reloc.map { |o, r| r.target }.inspect}" if not @encoded.reloc.empty?
		@encoded.data
	end

	# saves the result of +encode_string+ in the specified file
	# fails if the file already exists
	def encode_file(path, *a)
		raise Errno::EEXIST, path if File.exist? path	# race, but cannot use O_EXCL, as O_BINARY is not defined in ruby
		encode_string(*a)
		File.open(path, 'wb') { |fd| fd.write(@encoded.data) }
	end

	# converts a constant name to its numeric value using the hash<commit_msg>patch backtrace in exe.assemble<commit_after>require 'metasm/main'

module Metasm
class ExeFormat
	attr_accessor :cpu, :encoded

	# creates a new instance, populates self.encoded with the supplied string
	def self.load(str, *a)
		e = new(*a)
		e.encoded << str
		e
	end

	# same as +load+, but from a file
	# uses VirtualFile if available
	def self.load_file(path, *a)
		if defined? VirtualFile
			load(VirtualFile.read(path), *a)
		else
			File.open(path, 'rb') { |fd| load(fd.read, *a) }
		end
	end

	# +load_file+ then decode
	def self.decode_file(path, *a)
		e = load_file(path, *a)
		e.decode
		e
	end

	# +load_file+ then decode header
	def self.decode_file_header(path, *a)
		e = load_file(path, *a)
		e.decode_header
		e
	end

	# creates a new object using the specified cpu, parses the asm source, and assemble
	def self.assemble(cpu, source)
		ex = new(cpu)
		caller.first =~ /^(.*?):(\d+)/
		ex.parse(source, $1, $2.to_i+1)
		ex.assemble
		ex
	end

	# encodes the executable as a string, checks that all relocations are
	# resolved, and returns the raw string version
	def encode_string(*a)
		encode(*a)
		raise "Unresolved relocations #{@encoded.reloc.map { |o, r| r.target }.inspect}" if not @encoded.reloc.empty?
		@encoded.data
	end

	# saves the result of +encode_string+ in the specified file
	# fails if the file already exists
	def encode_file(path, *a)
		raise Errno::EEXIST, path if File.exist? path	# race, but cannot use O_EXCL, as O_BINARY is not defined in ruby
		encode_string(*a)
		File.open(path, 'wb') { |fd| fd.write(@encoded.data) }
	end

	# converts a constant name to its numeric value using the hash
{'fraction_non_alphanumeric': 0.1103125, 'fraction_numerical': 0.0009375, 'mean_word_length': 3.8353474320241694, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7627273', 'n_tokens_mistral': 1232, 'n_tokens_neox': 1143, 'n_words': 388}