node/lib/metasm/metasm/parse_c.rb
# returns the size of a type in bytes
returns the size of a type in bytes
returns the size of a type in bytes
def sizeof(var, type=nil)
			var, type = nil, var if var.kind_of? Type and not type
			type ||= var.type
			case type
			when Array
				case type.length
				when nil
					if var.kind_of? CExpression and not var.lexpr and not var.op and var.rexpr.kind_of? Variable
						var = var.rexpr
					end
					raise self, 'unknown array size' if not var.kind_of? Variable or not var.initializer
					init = var.initializer
					init = init.rexpr if init.kind_of? C::CExpression and not init.op and init.rexpr.kind_of? ::String
					case init
					when ::String; sizeof(nil, type.type) * (init.length + 1)
					when ::Array
						v = init.compact.first
						v ? (sizeof(nil, type.type) * init.length) : 0
					else sizeof(init)
					end
				when ::Integer; type.length * sizeof(type.type)
				when CExpression
					len = type.length.reduce(self)
					raise self, 'unknown array size' if not len.kind_of? ::Integer
					len * sizeof(type)
				else raise self, 'unknown array size'
				end
			when Pointer
				if var.kind_of? CExpression and not var.op and var.rexpr.kind_of? ::String
					sizeof(nil, type.type) * (var.rexpr.length + 1)
				else
					@typesize[:ptr]
				end
			when Function
				1	
			when BaseType
				@typesize[type.name]
			when Enum
				@typesize[:int]
			when Struct
				raise self, "unknown structure size #{type.name}" if not type.members
				al = type.align(self)
				al = 1 if (var.kind_of?(Attributes) and var.has_attribute('sizeof_packed')) or type.has_attribute('sizeof_packed')
				lm = type.members.last
				lm ? (type.offsetof(self, lm) + sizeof(lm) + al - 1) / al * al : 0
			when Union
				raise self, "unknown structure size #{type.name}" if not type.members
				type.members.map { |m| sizeof(m) }.max || 0
			when TypeDef
				sizeof(var, type.type)
			end
		end
def sizeof ( var , type = nil ) var , type = nil , var if var . kind_of? Type and not type type ||= var . type case type when Array case type . length when nil if var . kind_of? CExpression and not var . lexpr and not var . op and var . rexpr . kind_of? Variable var = var . rexpr end raise self , 'unknown array size' if not var . kind_of? Variable or not var . initializer init = var . initializer init = init . rexpr if init . kind_of? C :: CExpression and not init . op and init . rexpr . kind_of? :: String case init when :: String ; sizeof ( nil , type . type ) * ( init . length + 1 ) when :: Array v = init . compact . first v ? ( sizeof ( nil , type . type ) * init . length ) : 0 else sizeof ( init ) end when :: Integer ; type . length * sizeof ( type . type ) when CExpression len = type . length . reduce ( self ) raise self , 'unknown array size' if not len . kind_of? :: Integer len * sizeof ( type ) else raise self , 'unknown array size' end when Pointer if var . kind_of? CExpression and not var . op and var . rexpr . kind_of? :: String sizeof ( nil , type . type ) * ( var . rexpr . length + 1 ) else @typesize [ :ptr ] end when Function 1 when BaseType @typesize [ type . name ] when Enum @typesize [ :int ] when Struct raise self , "unknown structure size #{type.name}" if not type . members al = type . align ( self ) al = 1 if ( var . kind_of? ( Attributes ) and var . has_attribute ( 'sizeof_packed' ) ) or type . has_attribute ( 'sizeof_packed' ) lm = type . members . last lm ? ( type . offsetof ( self , lm ) + sizeof ( lm ) + al - 1 ) / al * al : 0 when Union raise self , "unknown structure size #{type.name}" if not type . members type . members . map { | m | sizeof ( m ) } . max || 0 when TypeDef sizeof ( var , type . type ) end end
returns the size of a type in bytes
returns the size of a type in bytes
# XXX double-check class apparition order ('when' checks inheritance) # sizeof("lolz") => 5 # raise # gcc
var
type