<commit_before>require 'metasm/exe_format/main'
require 'metasm/encode'
require 'metasm/decode'

module Metasm
class MZ < ExeFormat
	class Header
		Fields = [:magic, :cblp, :cp, :crlc, :cparhdr, :minalloc, :maxalloc,
			:ss, :sp, :csum, :ip, :cs, :lfarlc, :ovno]
		attr_accessor(*Fields)

		def encode(mz, relocs)
			h = EncodedData.new
			set_default_values mz, h, relocs
			h << @magic
			Fields[1..-1].each { |m| h << Expression[send(m)].encode(:u16, mz.endianness) }
			h.align_size 16
			h
		end

		def set_default_values mz, h, relocs
			@magic    ||= 'MZ'
			@cblp     ||= Expression[[mz.label_at(mz.body, mz.body.virtsize), :-, mz.label_at(h, 0)], :%, 512]	# number of bytes used in last page
			@cp       ||= Expression[[[mz.label_at(mz.body, mz.body.virtsize), :-, mz.label_at(h, 0)], :+, 511], :/, 512]	# number of pages used
			@crlc     ||= relocs.virtsize/4
			@cparhdr  ||= Expression[[mz.label_at(relocs, 0), :-, mz.label_at(h, 0)], :/, 16]	# header size in paragraphs (16o)
			@minalloc ||= ((mz.body.virtsize - mz.body.rawsize) + 15) / 16
			@maxalloc ||= @minalloc
			@ss       ||= 0
			@sp       ||= 0		# ss:sp points at 1st byte of body => works if body does not reach end of segment (or maybe the overflow make the stack go to header space)
			@csum     ||= 0
			@ip       ||= Expression[mz.body.export['start'] || 0]	# when empty relocs, cs:ip looks like an offset from end of header
			@cs       ||= 0
			@lfarlc   ||= Expression[mz.label_at(relocs, 0), :-, mz.label_at(h, 0)]<commit_msg>MZ: changed cp/cblp calculation mode<commit_after>require 'metasm/exe_format/main'
require 'metasm/encode'
require 'metasm/decode'

module Metasm
class MZ < ExeFormat
	class Header
		Fields = [:magic, :cblp, :cp, :crlc, :cparhdr, :minalloc, :maxalloc,
			:ss, :sp, :csum, :ip, :cs, :lfarlc, :ovno]
		attr_accessor(*Fields)

		def encode(mz, relocs)
			h = EncodedData.new
			set_default_values mz, h, relocs
			h << @magic
			Fields[1..-1].each { |m| h << Expression[send(m)].encode(:u16, mz.endianness) }
			h.align_size 16
			h
		end

		def set_default_values mz, h, relocs
			@magic    ||= 'MZ'
			@cblp     ||= Expression[[mz.label_at(mz.body, mz.body.virtsize), :-, mz.label_at(h, 0)], :%, 512]	# number of bytes used in last page
			@cp       ||= Expression[[mz.label_at(mz.body, mz.body.virtsize), :-, mz.label_at(h, 0)], :/, 512]	# number of pages used
			@crlc     ||= relocs.virtsize/4
			@cparhdr  ||= Expression[[mz.label_at(relocs, 0), :-, mz.label_at(h, 0)], :/, 16]	# header size in paragraphs (16o)
			@minalloc ||= ((mz.body.virtsize - mz.body.rawsize) + 15) / 16
			@maxalloc ||= @minalloc
			@ss       ||= 0
			@sp       ||= 0		# ss:sp points at 1st byte of body => works if body does not reach end of segment (or maybe the overflow make the stack go to header space)
			@csum     ||= 0
			@ip       ||= Expression[mz.body.export['start'] || 0]	# when empty relocs, cs:ip looks like an offset from end of header
			@cs       ||= 0
			@lfarlc   ||= Expression[mz.label_at(relocs, 0), :-, mz.label_at(h, 0)]
{'fraction_non_alphanumeric': 0.16996047430830039, 'fraction_numerical': 0.022727272727272728, 'mean_word_length': 3.2298050139275767, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, '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': '626055', 'n_tokens_mistral': 1404, 'n_tokens_neox': 1209, 'n_words': 312}