diff options
Diffstat (limited to 'vendor/windows-metadata/src/writer')
-rw-r--r-- | vendor/windows-metadata/src/writer/imp/blobs.rs | 60 | ||||
-rw-r--r-- | vendor/windows-metadata/src/writer/imp/codes.rs | 94 | ||||
-rw-r--r-- | vendor/windows-metadata/src/writer/imp/definitions.rs | 43 | ||||
-rw-r--r-- | vendor/windows-metadata/src/writer/imp/file.rs | 138 | ||||
-rw-r--r-- | vendor/windows-metadata/src/writer/imp/mod.rs | 408 | ||||
-rw-r--r-- | vendor/windows-metadata/src/writer/imp/references.rs | 36 | ||||
-rw-r--r-- | vendor/windows-metadata/src/writer/imp/strings.rs | 46 | ||||
-rw-r--r-- | vendor/windows-metadata/src/writer/imp/tables.rs | 287 | ||||
-rw-r--r-- | vendor/windows-metadata/src/writer/mod.rs | 95 |
9 files changed, 0 insertions, 1207 deletions
diff --git a/vendor/windows-metadata/src/writer/imp/blobs.rs b/vendor/windows-metadata/src/writer/imp/blobs.rs deleted file mode 100644 index 08bad113d..000000000 --- a/vendor/windows-metadata/src/writer/imp/blobs.rs +++ /dev/null @@ -1,60 +0,0 @@ -use super::*; - -#[derive(Default)] -pub struct Blobs { - // Blobs don't need to be sorted. A map is used to collapse duplicates. A `BTreeMap` in particular is used to help with reproducible builds. - map: BTreeMap<Vec<u8>, u32>, - stream: Vec<u8>, -} - -pub struct StagedBlobs(Blobs); - -impl Blobs { - pub fn insert(&mut self, value: Vec<u8>) { - if !value.is_empty() { - self.map.entry(value).or_default(); - } - } - - pub fn stage(mut self) -> StagedBlobs { - self.stream = vec![0]; - - for (value, index) in self.map.iter_mut() { - *index = self.stream.len() as _; - - match value.len() { - 0..=0x7F => self.stream.push(value.len() as _), - 0x80..=0x3FFF => { - self.stream.push((0x80 | value.len() >> 8) as _); - self.stream.push((0xFF & value.len()) as _); - } - _ => { - self.stream.push((0xC0 | value.len() >> 24) as _); - self.stream.push((0xFF & value.len() >> 16) as _); - self.stream.push((0xFF & value.len() >> 8) as _); - self.stream.push((0xFF & value.len()) as _); - } - } - - self.stream.extend_from_slice(value); - } - - self.stream.resize(round(self.stream.len(), 4), 0); - StagedBlobs(self) - } -} - -impl StagedBlobs { - pub fn stream(self) -> Vec<u8> { - self.0.stream - } - - #[track_caller] - pub fn index(&self, value: &[u8]) -> u32 { - if value.is_empty() { - 0 - } else { - self.0.map[value] - } - } -} diff --git a/vendor/windows-metadata/src/writer/imp/codes.rs b/vendor/windows-metadata/src/writer/imp/codes.rs deleted file mode 100644 index 3c1b60048..000000000 --- a/vendor/windows-metadata/src/writer/imp/codes.rs +++ /dev/null @@ -1,94 +0,0 @@ -/// A `ResolutionScope` is an index into a certain table indicating the scope in which a TypeRef can be resolved. -#[derive(Default, Clone)] -pub enum ResolutionScope { - #[default] - None, - Module(u32), - ModuleRef(u32), - AssemblyRef(u32), - TypeRef(u32), -} - -impl ResolutionScope { - pub fn encode(&self) -> u32 { - match self { - Self::Module(row) => (row + 1) << 2, - Self::ModuleRef(row) => ((row + 1) << 2) + 1, - Self::AssemblyRef(row) => ((row + 1) << 2) + 2, - Self::TypeRef(row) => ((row + 1) << 2) + 3, - _ => 0, - } - } -} - -/// A `TypeDefOrRef` is an index into a certain table used to locate a type definition. -#[derive(Default, Clone)] -pub enum TypeDefOrRef { - #[default] - None, - TypeDef(u32), - TypeRef(u32), - TypeSpec(u32), -} - -impl TypeDefOrRef { - pub fn encode(&self) -> u32 { - match self { - Self::TypeDef(row) => (row + 1) << 2, - Self::TypeRef(row) => ((row + 1) << 2) + 1, - Self::TypeSpec(row) => ((row + 1) << 2) + 2, - _ => 0, - } - } -} - -/// A `HasConstant` is an index into a certain table used to identify the parent of a row in the `Constant` table. -#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord)] -pub enum HasConstant { - #[default] - None, - Field(u32), - Param(u32), - Property(u32), -} - -impl HasConstant { - pub fn encode(&self) -> u32 { - match self { - Self::Field(row) => (row + 1) << 2, - Self::Param(row) => ((row + 1) << 2) + 1, - Self::Property(row) => ((row + 1) << 2) + 2, - _ => 0, - } - } -} - -#[derive(Default, Clone)] -pub enum HasCustomAttribute { - #[default] - None, -} - -#[derive(Default, Clone)] -pub enum CustomAttributeType { - #[default] - None, -} - -#[derive(Default, Clone)] -pub enum TypeOrMethodDef { - #[default] - None, -} - -#[derive(Default, Clone)] -pub enum MemberForwarded { - #[default] - None, -} - -#[derive(Default, Clone)] -pub enum MemberRefParent { - #[default] - None, -} diff --git a/vendor/windows-metadata/src/writer/imp/definitions.rs b/vendor/windows-metadata/src/writer/imp/definitions.rs deleted file mode 100644 index 476e3ed52..000000000 --- a/vendor/windows-metadata/src/writer/imp/definitions.rs +++ /dev/null @@ -1,43 +0,0 @@ -use super::*; - -#[derive(Default)] -pub struct Definitions<'a> { - map: BTreeMap<(&'a str, &'a str), Definition<'a>>, -} - -pub struct Definition<'a> { - pub item: &'a Item, - pub index: u32, - pub value_type: bool, -} - -pub struct StagedDefinitions<'a>(Definitions<'a>); - -impl<'a> Definitions<'a> { - pub fn insert(&mut self, item: &'a Item) { - if self.map.insert(item_type_name(item), Definition { item, index: 0, value_type: item_value_type(item) }).is_some() { - panic!("Duplicate type found"); - } - } - - pub fn stage(mut self) -> StagedDefinitions<'a> { - for (index, value) in self.map.values_mut().enumerate() { - value.index = index as _; - } - StagedDefinitions(self) - } -} - -impl<'a> StagedDefinitions<'a> { - pub fn get(&self, namespace: &'a str, name: &'a str) -> Option<&'a Definition> { - self.0.map.get(&(namespace, name)) - } - - pub fn items(&self) -> impl Iterator<Item = &Item> { - self.0.map.values().map(|value| value.item) - } - - pub fn iter(&self) -> impl Iterator<Item = (u32, &Item)> { - self.0.map.values().map(|value| (value.index, value.item)) - } -} diff --git a/vendor/windows-metadata/src/writer/imp/file.rs b/vendor/windows-metadata/src/writer/imp/file.rs deleted file mode 100644 index 2f25f942b..000000000 --- a/vendor/windows-metadata/src/writer/imp/file.rs +++ /dev/null @@ -1,138 +0,0 @@ -use super::*; -use std::mem::*; - -pub fn write(tables: Tables, strings: StagedStrings, blobs: StagedBlobs) -> Vec<u8> { - unsafe { - let mut tables = tables.stream(); - let mut strings = strings.stream(); - let mut blobs = blobs.stream(); - let mut guids = vec![0; 16]; // zero guid - let size_of_streams = tables.len() + guids.len() + strings.len() + blobs.len(); - - let mut dos: IMAGE_DOS_HEADER = zeroed(); - dos.e_magic = IMAGE_DOS_SIGNATURE as _; - dos.e_lfarlc = 64; - dos.e_lfanew = size_of::<IMAGE_DOS_HEADER>() as _; - - let mut file: IMAGE_FILE_HEADER = zeroed(); - file.Machine = IMAGE_FILE_MACHINE_I386; - file.NumberOfSections = 1; - file.SizeOfOptionalHeader = size_of::<IMAGE_OPTIONAL_HEADER32>() as _; - file.Characteristics = IMAGE_FILE_DLL | IMAGE_FILE_32BIT_MACHINE | IMAGE_FILE_EXECUTABLE_IMAGE; - - let mut optional: IMAGE_OPTIONAL_HEADER32 = zeroed(); - optional.Magic = IMAGE_NT_OPTIONAL_HDR32_MAGIC; - optional.MajorLinkerVersion = 11; - optional.SizeOfInitializedData = 1024; - optional.ImageBase = 0x400000; - optional.SectionAlignment = SECTION_ALIGNMENT; - optional.FileAlignment = 512; - optional.MajorOperatingSystemVersion = 6; - optional.MinorOperatingSystemVersion = 2; - optional.MajorSubsystemVersion = 6; - optional.MinorSubsystemVersion = 2; - optional.SizeOfHeaders = 512; - optional.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI; - optional.DllCharacteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT | IMAGE_DLLCHARACTERISTICS_NO_SEH | IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE; - optional.SizeOfStackReserve = 0x100000; - optional.SizeOfHeapReserve = 4096; - optional.LoaderFlags = 0x100000; - optional.NumberOfRvaAndSizes = 16; - - let mut section: IMAGE_SECTION_HEADER = zeroed(); - section.Name = *b".text\0\0\0"; - section.Characteristics = 0x4000_0020; - section.VirtualAddress = SECTION_ALIGNMENT; - - let mut clr: IMAGE_COR20_HEADER = zeroed(); - clr.cb = size_of::<IMAGE_COR20_HEADER>() as _; - clr.MajorRuntimeVersion = 2; - clr.MinorRuntimeVersion = 5; - clr.Flags = 1; - - let metadata = METADATA_HEADER { - signature: METADATA_SIGNATURE, - major_version: 1, - minor_version: 1, - length: 20, - version: *b"WindowsRuntime\0\0\0\0\0\0", - streams: 4, - ..Default::default() - }; - - type TablesHeader = StreamHeader<4>; - type StringsHeader = StreamHeader<12>; - type GuidsHeader = StreamHeader<8>; - type BlobsHeader = StreamHeader<8>; - - let size_of_stream_headers = size_of::<TablesHeader>() + size_of::<StringsHeader>() + size_of::<GuidsHeader>() + size_of::<BlobsHeader>(); - let size_of_image = optional.FileAlignment as usize + size_of::<IMAGE_COR20_HEADER>() + size_of::<METADATA_HEADER>() + size_of_stream_headers + size_of_streams; - - optional.SizeOfImage = round(size_of_image, optional.SectionAlignment as _) as _; - section.Misc.VirtualSize = size_of_image as u32 - optional.FileAlignment; - section.SizeOfRawData = round(section.Misc.VirtualSize as _, optional.FileAlignment as _) as _; - - optional.DataDirectory[14] = IMAGE_DATA_DIRECTORY { VirtualAddress: SECTION_ALIGNMENT, Size: size_of::<IMAGE_COR20_HEADER>() as _ }; - section.PointerToRawData = optional.FileAlignment; - clr.MetaData.VirtualAddress = SECTION_ALIGNMENT + size_of::<IMAGE_COR20_HEADER>() as u32; - clr.MetaData.Size = section.Misc.VirtualSize - size_of::<IMAGE_COR20_HEADER>() as u32; - - let mut buffer = Vec::<u8>::new(); - - buffer.write_header(&dos); - buffer.write_u32(IMAGE_NT_SIGNATURE); - buffer.write_header(&file); - buffer.write_header(&optional); - buffer.write_header(§ion); - debug_assert!(buffer.len() < optional.FileAlignment as _); - buffer.resize(optional.FileAlignment as _, 0); - buffer.write_header(&clr); - let metadata_offset = buffer.len(); - buffer.write_header(&metadata); - - let stream_offset = buffer.len() - metadata_offset + size_of_stream_headers; - let tables_header = TablesHeader::new(stream_offset as _, tables.len() as _, b"#~\0\0"); - let strings_header = StringsHeader::new(tables_header.next_offset(), strings.len() as _, b"#Strings\0\0\0\0"); - let guids_header = GuidsHeader::new(strings_header.next_offset(), guids.len() as _, b"#GUID\0\0\0"); - let blobs_header = BlobsHeader::new(guids_header.next_offset(), blobs.len() as _, b"#Blob\0\0\0"); - - buffer.write_header(&tables_header); - buffer.write_header(&strings_header); - buffer.write_header(&guids_header); - buffer.write_header(&blobs_header); - - buffer.append(&mut tables); - buffer.append(&mut strings); - buffer.append(&mut guids); - buffer.append(&mut blobs); - - assert_eq!(clr.MetaData.Size as usize, buffer.len() - metadata_offset); - assert_eq!(size_of_image, buffer.len()); - - buffer - } -} - -const SECTION_ALIGNMENT: u32 = 4096; - -#[repr(C)] -struct StreamHeader<const LEN: usize> { - offset: u32, - size: u32, - name: [u8; LEN], -} - -impl<const LEN: usize> StreamHeader<LEN> { - fn new(offset: u32, size: u32, name: &[u8; LEN]) -> Self { - Self { offset, size, name: *name } - } - fn next_offset(&self) -> u32 { - self.offset + self.size - } -} - -fn guid_stream() -> Vec<u8> { - let mut buffer = Vec::new(); - buffer.resize(16, 0); // zero guid - buffer -} diff --git a/vendor/windows-metadata/src/writer/imp/mod.rs b/vendor/windows-metadata/src/writer/imp/mod.rs deleted file mode 100644 index adecde371..000000000 --- a/vendor/windows-metadata/src/writer/imp/mod.rs +++ /dev/null @@ -1,408 +0,0 @@ -mod blobs; -mod codes; -mod definitions; -mod file; -mod references; -mod strings; -mod tables; - -use super::*; -use blobs::*; -use codes::*; -use definitions::*; -use references::*; -use strings::*; -use tables::Tables; - -use std::collections::BTreeMap; - -pub fn round(size: usize, round: usize) -> usize { - let round = round - 1; - (size + round) & !round -} - -pub fn write(name: &str, winrt: bool, definitions: &[Item], assemblies: &[&str]) -> Vec<u8> { - // Index assemblies used to resolve references to existing winmd files. - let assemblies: Vec<reader::File> = assemblies.iter().map(|file| reader::File::new(file).expect("Assemblies could not be loaded")).collect(); - let assemblies = &reader::Reader::new(&assemblies); - - // Build sorted list of definitions. - let definitions = &{ - let mut index = Definitions::default(); - definitions.iter().for_each(|item| index.insert(item)); - index.stage() - }; - - // Build sorted list of references. - let references = &{ - let mut index = References::default(); - for item in definitions.items() { - match item { - Item::Struct(ty) => ty.fields.iter().for_each(|field| type_reference(&field.ty, definitions, assemblies, &mut index)), - Item::Interface(_ty) => {} - _ => {} - } - } - index.stage() - }; - - // Now that we have stable type indexes, build blobs and index strings. - let (blobs, strings) = { - let mut blobs = Blobs::default(); - let mut strings = Strings::default(); - strings.insert(name); - strings.insert("<Module>"); - strings.insert("mscorlib"); - strings.insert("System"); - strings.insert("ValueType"); - strings.insert("Enum"); - strings.insert("value__"); - - for item in definitions.items() { - match item { - Item::Struct(ty) => { - strings.insert(&ty.namespace); - strings.insert(&ty.name); - ty.fields.iter().for_each(|field| { - strings.insert(&field.name); - blobs.insert(field_blob(&field.ty, definitions, references)); - }); - } - Item::Enum(ty) => { - strings.insert(&ty.namespace); - strings.insert(&ty.name); - let enum_type = Type::Named((ty.namespace.clone(), ty.name.clone())); - blobs.insert(field_blob(&enum_type, definitions, references)); - blobs.insert(field_blob(&value_to_type(&ty.constants[0].value), definitions, references)); - ty.constants.iter().for_each(|constant| { - strings.insert(&constant.name); - blobs.insert(value_blob(&constant.value)); - }); - } - Item::Interface(ty) => { - strings.insert(&ty.namespace); - strings.insert(&ty.name); - ty.methods.iter().for_each(|method| { - strings.insert(&method.name); - blobs.insert(method_blob(method, definitions, references)); - method.params.iter().for_each(|param| { - strings.insert(¶m.name); - }); - }); - } - } - } - - (blobs.stage(), strings.stage()) - }; - - // Now that everything is indexed in various heaps, write out the table records. - let tables = { - let mut tables = Tables::default(); - tables.Module.push(tables::Module { Name: strings.index(name), Mvid: 1, ..Default::default() }); - tables.TypeDef.push(tables::TypeDef { TypeName: strings.index("<Module>"), ..Default::default() }); - let mscorlib = tables.AssemblyRef.push2(tables::AssemblyRef { MajorVersion: 4, Name: strings.index("mscorlib"), ..Default::default() }); - let value_type = tables.TypeRef.push2(tables::TypeRef { TypeName: strings.index("ValueType"), TypeNamespace: strings.index("System"), ResolutionScope: ResolutionScope::AssemblyRef(mscorlib).encode() }); - let enum_type = tables.TypeRef.push2(tables::TypeRef { TypeName: strings.index("Enum"), TypeNamespace: strings.index("System"), ResolutionScope: ResolutionScope::AssemblyRef(mscorlib).encode() }); - - for (_index, item) in definitions.iter() { - match item { - Item::Struct(ty) => { - let mut flags = TypeAttributes::PUBLIC | TypeAttributes::SEQUENTIAL_LAYOUT | TypeAttributes::SEALED; - if winrt { - flags |= TypeAttributes::WINRT; - } - tables.TypeDef.push(tables::TypeDef { - Flags: flags.0, - TypeName: strings.index(&ty.name), - TypeNamespace: strings.index(&ty.namespace), - Extends: TypeDefOrRef::TypeRef(value_type).encode(), - FieldList: tables.Field.len() as _, - MethodList: tables.MethodDef.len() as _, - }); - for field in &ty.fields { - let flags = FieldAttributes::PUBLIC; - tables.Field.push(tables::Field { Flags: flags.0, Name: strings.index(&field.name), Signature: blobs.index(&field_blob(&field.ty, definitions, references)) }); - } - } - Item::Enum(ty) => { - let mut flags = TypeAttributes::PUBLIC | TypeAttributes::SEALED; - if winrt { - flags |= TypeAttributes::WINRT; - } - tables.TypeDef.push(tables::TypeDef { - Flags: flags.0, - TypeName: strings.index(&ty.name), - TypeNamespace: strings.index(&ty.namespace), - Extends: TypeDefOrRef::TypeRef(enum_type).encode(), - FieldList: tables.Field.len() as _, - MethodList: tables.MethodDef.len() as _, - }); - let enum_type = Type::Named((ty.namespace.clone(), ty.name.clone())); - let flags = FieldAttributes::PRIVATE | FieldAttributes::SPECIAL | FieldAttributes::RUNTIME_SPECIAL; - tables.Field.push2(tables::Field { - Flags: flags.0, - Name: strings.index("value__"), - Signature: blobs.index(&field_blob(&value_to_type(&ty.constants[0].value), definitions, references)), - }); - for constant in &ty.constants { - let flags = FieldAttributes::PUBLIC | FieldAttributes::STATIC | FieldAttributes::LITERAL | FieldAttributes::HAS_DEFAULT; - let field = tables.Field.push2(tables::Field { Flags: flags.0, Name: strings.index(&constant.name), Signature: blobs.index(&field_blob(&enum_type, definitions, references)) }); - tables.Constant.push(tables::Constant { Type: value_type_code(&constant.value), Parent: HasConstant::Field(field).encode(), Value: blobs.index(&value_blob(&constant.value)) }); - } - } - Item::Interface(ty) => { - let mut flags = TypeAttributes::PUBLIC | TypeAttributes::INTERFACE | TypeAttributes::ABSTRACT; - if winrt { - flags |= TypeAttributes::WINRT; - } - tables.TypeDef.push(tables::TypeDef { - Flags: flags.0, - TypeName: strings.index(&ty.name), - TypeNamespace: strings.index(&ty.namespace), - Extends: 0, - FieldList: tables.Field.len() as _, - MethodList: tables.MethodDef.len() as _, - }); - for method in &ty.methods { - let flags = MethodAttributes::ABSTRACT | MethodAttributes::HIDE_BY_SIG | MethodAttributes::NEW_SLOT | MethodAttributes::PUBLIC | MethodAttributes::VIRTUAL; - tables.MethodDef.push(tables::MethodDef { - RVA: 0, - ImplFlags: 0, - Flags: flags.0, - Name: strings.index(&method.name), - Signature: blobs.index(&method_blob(method, definitions, references)), - ParamList: tables.Param.len() as _, - }); - for (sequence, param) in method.params.iter().enumerate() { - tables.Param.push(tables::Param { Flags: param_flags_to_attributes(param.flags).0, Sequence: (sequence + 1) as _, Name: strings.index(¶m.name) }); - } - } - } - } - } - - tables - }; - - // With all of the streams prepared, write out ECMA-335 file format. - file::write(tables, strings, blobs) -} - -fn type_reference<'a>(ty: &'a Type, definitions: &StagedDefinitions, assemblies: &reader::Reader, references: &mut References<'a>) { - // TODO: More matches to come... - #[allow(clippy::single_match)] - match ty { - Type::Named((namespace, name)) => { - if definitions.get(namespace, name).is_none() { - references.insert(namespace, name, assemblies); - } - } - _ => {} - } -} - -fn param_flags_to_attributes(flags: ParamFlags) -> ParamAttributes { - let mut attributes = ParamAttributes(0); - if flags.contains(ParamFlags::INPUT) { - attributes |= ParamAttributes::INPUT; - } - if flags.contains(ParamFlags::OUTPUT) { - attributes |= ParamAttributes::OUTPUT; - } - if flags.contains(ParamFlags::OPTIONAL) { - attributes |= ParamAttributes::OPTIONAL; - } - attributes -} - -fn item_type_name(item: &Item) -> (&str, &str) { - match item { - Item::Struct(ty) => (ty.namespace.as_str(), ty.name.as_str()), - Item::Enum(ty) => (ty.namespace.as_str(), ty.name.as_str()), - Item::Interface(ty) => (ty.namespace.as_str(), ty.name.as_str()), - } -} - -fn item_value_type(item: &Item) -> bool { - match item { - Item::Struct(_) | Item::Enum(_) => true, - Item::Interface(_) => false, - } -} - -fn method_blob(method: &Method, definitions: &StagedDefinitions, references: &StagedReferences) -> Vec<u8> { - let mut blob = vec![0x20]; // HASTHIS - u32_blob(method.params.len() as _, &mut blob); - for param in &method.params { - type_blob(¶m.ty, &mut blob, definitions, references); - } - type_blob(&method.return_type, &mut blob, definitions, references); - blob -} - -fn field_blob(ty: &Type, definitions: &StagedDefinitions, references: &StagedReferences) -> Vec<u8> { - let mut blob = vec![0x6]; - type_blob(ty, &mut blob, definitions, references); - blob -} - -fn value_blob(value: &Value) -> Vec<u8> { - match value { - Value::I8(value) => value.to_le_bytes().to_vec(), - Value::U8(value) => value.to_le_bytes().to_vec(), - Value::I16(value) => value.to_le_bytes().to_vec(), - Value::U16(value) => value.to_le_bytes().to_vec(), - Value::I32(value) => value.to_le_bytes().to_vec(), - Value::U32(value) => value.to_le_bytes().to_vec(), - Value::I64(value) => value.to_le_bytes().to_vec(), - Value::U64(value) => value.to_le_bytes().to_vec(), - _ => panic!("Unsupported value type"), - } -} - -fn value_to_type(value: &Value) -> Type { - match value { - Value::I8(_) => Type::I8, - Value::U8(_) => Type::U8, - Value::I16(_) => Type::I16, - Value::U16(_) => Type::U16, - Value::I32(_) => Type::I32, - Value::U32(_) => Type::U32, - Value::I64(_) => Type::I64, - Value::U64(_) => Type::U64, - _ => panic!("Unsupported value type"), - } -} - -fn value_type_code(value: &Value) -> u16 { - match value { - Value::I8(_) => 0x04, - Value::U8(_) => 0x05, - Value::I16(_) => 0x06, - Value::U16(_) => 0x07, - Value::I32(_) => 0x08, - Value::U32(_) => 0x09, - Value::I64(_) => 0x0a, - Value::U64(_) => 0x0b, - _ => panic!("Unsupported value type"), - } -} - -fn type_blob(ty: &Type, blob: &mut Vec<u8>, definitions: &StagedDefinitions, references: &StagedReferences) { - match ty { - Type::Void => blob.push(0x01), - Type::Bool => blob.push(0x02), - Type::Char => blob.push(0x03), - Type::I8 => blob.push(0x04), - Type::U8 => blob.push(0x05), - Type::I16 => blob.push(0x06), - Type::U16 => blob.push(0x07), - Type::I32 => blob.push(0x08), - Type::U32 => blob.push(0x09), - Type::I64 => blob.push(0x0a), - Type::U64 => blob.push(0x0b), - Type::F32 => blob.push(0x0c), - Type::F64 => blob.push(0x0d), - Type::ISize => blob.push(0x18), - Type::USize => blob.push(0x19), - Type::String => blob.push(0x0e), - //Type::IInspectable => blob.push(0x1c), - Type::Named((namespace, name)) => { - let (value_type, code) = type_name_encode(namespace, name, definitions, references); - value_type_blob(value_type, blob); - u32_blob(code, blob); - } - } -} - -fn value_type_blob(value_type: bool, blob: &mut Vec<u8>) { - if value_type { - blob.push(0x11); - } else { - blob.push(0x12); - } -} - -fn u32_blob(value: u32, blob: &mut Vec<u8>) { - if value < 0x80 { - blob.push(value as _); - } else if value < 0x4000 { - blob.push((0x40 | (value & 0xFF00)) as _); - blob.push((value | 0xFF) as _); - } else { - blob.push((0x60 | (value & 0xFF000000)) as _); - blob.push((value | 0xFF0000) as _); - blob.push((value | 0xFF00) as _); - blob.push((value | 0xFF) as _); - } -} - -/// Returns the TypeDefOrRef-encoded value for the type name as well as whether the type is a value type, needed -/// in some cases like when a TypeDefOrRef appears in a signature. -fn type_name_encode(namespace: &str, name: &str, definitions: &StagedDefinitions, references: &StagedReferences) -> (bool, u32) { - if let Some(definition) = definitions.get(namespace, name) { - return (definition.value_type, TypeDefOrRef::TypeDef(definition.index + 1).encode()); - } - let reference = references.get(namespace, name).expect("Type not found"); - (reference.value_type, TypeDefOrRef::TypeRef(reference.index + 1).encode()) -} - -pub trait Write { - unsafe fn write_header<T: Sized>(&mut self, value: &T); - fn write_u8(&mut self, value: u8); - fn write_u16(&mut self, value: u16); - fn write_u32(&mut self, value: u32); - fn write_u64(&mut self, value: u64); - fn write_code(&mut self, value: u32, size: usize); - fn write_index(&mut self, index: u32, len: usize); -} - -impl Write for Vec<u8> { - unsafe fn write_header<T: Sized>(&mut self, value: &T) { - self.extend_from_slice(std::slice::from_raw_parts(value as *const _ as _, std::mem::size_of::<T>())); - } - - fn write_u8(&mut self, value: u8) { - self.extend_from_slice(&value.to_le_bytes()); - } - - fn write_u16(&mut self, value: u16) { - self.extend_from_slice(&value.to_le_bytes()); - } - - fn write_u32(&mut self, value: u32) { - self.extend_from_slice(&value.to_le_bytes()); - } - - fn write_u64(&mut self, value: u64) { - self.extend_from_slice(&value.to_le_bytes()); - } - - fn write_code(&mut self, value: u32, size: usize) { - if size == 2 { - self.write_u16(value as _); - } else { - self.write_u32(value); - } - } - - fn write_index(&mut self, index: u32, len: usize) { - if len < (1 << 16) { - self.write_u16(index as u16 + 1); - } else { - self.write_u32(index + 1); - } - } -} - -trait Push2<T> { - fn push2(&mut self, value: T) -> u32; -} - -impl<T> Push2<T> for Vec<T> { - fn push2(&mut self, value: T) -> u32 { - self.push(value); - (self.len() - 1) as _ - } -} diff --git a/vendor/windows-metadata/src/writer/imp/references.rs b/vendor/windows-metadata/src/writer/imp/references.rs deleted file mode 100644 index b35a8a275..000000000 --- a/vendor/windows-metadata/src/writer/imp/references.rs +++ /dev/null @@ -1,36 +0,0 @@ -use super::*; - -#[derive(Default)] -pub struct References<'a> { - map: BTreeMap<(&'a str, &'a str), Reference>, -} - -pub struct Reference { - pub index: u32, - pub value_type: bool, -} - -pub struct StagedReferences<'a>(References<'a>); - -impl<'a> References<'a> { - pub fn insert(&mut self, namespace: &'a str, name: &'a str, assemblies: &reader::Reader) { - self.map.entry((namespace, name)).or_insert_with(|| { - let type_def = assemblies.get(reader::TypeName::new(namespace, name)).next().expect("Type not found"); - let value_type = matches!(assemblies.type_def_kind(type_def), reader::TypeKind::Struct | reader::TypeKind::Enum); - Reference { value_type, index: 0 } - }); - } - - pub fn stage(mut self) -> StagedReferences<'a> { - for (index, value) in self.map.values_mut().enumerate() { - value.index = index as _; - } - StagedReferences(self) - } -} - -impl<'a> StagedReferences<'a> { - pub fn get(&'a self, namespace: &'a str, name: &'a str) -> Option<&'a Reference> { - self.0.map.get(&(namespace, name)) - } -} diff --git a/vendor/windows-metadata/src/writer/imp/strings.rs b/vendor/windows-metadata/src/writer/imp/strings.rs deleted file mode 100644 index da20e8cde..000000000 --- a/vendor/windows-metadata/src/writer/imp/strings.rs +++ /dev/null @@ -1,46 +0,0 @@ -use super::*; - -#[derive(Default)] -pub struct Strings<'a> { - // Strings don't need to be sorted. A map is used to collapse duplicates. A `BTreeMap` in particular is used to help with reproducible builds. - map: BTreeMap<&'a str, u32>, - stream: Vec<u8>, -} - -pub struct StagedStrings<'a>(Strings<'a>); - -impl<'a> Strings<'a> { - pub fn insert(&mut self, value: &'a str) { - if !value.is_empty() { - self.map.entry(value).or_default(); - } - } - - pub fn stage(mut self) -> StagedStrings<'a> { - self.stream = vec![0]; - - for (value, index) in self.map.iter_mut() { - *index = self.stream.len() as _; - - self.stream.extend_from_slice(value.as_bytes()); - self.stream.push(0); // terminator - } - - self.stream.resize(round(self.stream.len(), 4), 0); - StagedStrings(self) - } -} - -impl<'a> StagedStrings<'a> { - pub fn stream(self) -> Vec<u8> { - self.0.stream - } - - pub fn index(&self, value: &str) -> u32 { - if value.is_empty() { - 0 - } else { - self.0.map[value] - } - } -} diff --git a/vendor/windows-metadata/src/writer/imp/tables.rs b/vendor/windows-metadata/src/writer/imp/tables.rs deleted file mode 100644 index 7b3450738..000000000 --- a/vendor/windows-metadata/src/writer/imp/tables.rs +++ /dev/null @@ -1,287 +0,0 @@ -#![allow(non_snake_case)] - -use super::*; - -#[derive(Default)] -pub struct Tables { - pub AssemblyRef: Vec<AssemblyRef>, - pub ClassLayout: Vec<ClassLayout>, - pub Constant: Vec<Constant>, - pub CustomAttribute: Vec<CustomAttribute>, - pub Field: Vec<Field>, - pub GenericParam: Vec<GenericParam>, - pub ImplMap: Vec<ImplMap>, - pub InterfaceImpl: Vec<InterfaceImpl>, - pub MemberRef: Vec<MemberRef>, - pub MethodDef: Vec<MethodDef>, - pub Module: Vec<Module>, - pub ModuleRef: Vec<ModuleRef>, - pub NestedClass: Vec<NestedClass>, - pub Param: Vec<Param>, - pub Property: Vec<Property>, - pub TypeDef: Vec<TypeDef>, - pub TypeRef: Vec<TypeRef>, - pub TypeSpec: Vec<TypeSpec>, -} - -#[derive(Default)] -pub struct AssemblyRef { - pub MajorVersion: u16, - pub MinorVersion: u16, - pub BuildNumber: u16, - pub RevisionNumber: u16, - pub Flags: u32, - pub PublicKeyOrToken: u32, - pub Name: u32, - pub Culture: u32, - pub HashValue: u32, -} - -#[derive(Default)] -pub struct ClassLayout { - pub PackingSize: u16, - pub ClassSize: u32, - pub Parent: u32, -} - -#[derive(Default)] -pub struct Constant { - pub Type: u16, - pub Parent: u32, - pub Value: u32, -} - -#[derive(Default)] -pub struct CustomAttribute { - pub Parent: u32, - pub Type: u32, - pub Value: u32, -} - -#[derive(Default)] -pub struct Field { - pub Flags: u16, - pub Name: u32, - pub Signature: u32, -} - -#[derive(Default)] -pub struct GenericParam { - pub Number: u16, - pub Flags: u16, - pub Owner: u32, - pub Name: u32, -} - -#[derive(Default)] -pub struct ImplMap { - pub MappingFlags: u16, - pub MemberForwarded: u32, - pub ImportName: u32, - pub ImportScope: u32, -} - -#[derive(Default)] -pub struct InterfaceImpl { - pub Class: u32, - pub Interface: u32, -} - -#[derive(Default)] -pub struct MemberRef { - pub Class: u32, - pub Name: u32, - pub Signature: u32, -} - -#[derive(Default)] -pub struct MethodDef { - pub RVA: u32, - pub ImplFlags: u16, - pub Flags: u16, - pub Name: u32, - pub Signature: u32, - pub ParamList: u32, -} - -#[derive(Default)] -pub struct Module { - pub Generation: u16, - pub Name: u32, - pub Mvid: u32, - pub EncId: u32, - pub EncBaseId: u32, -} - -#[derive(Default)] -pub struct ModuleRef { - pub Name: u32, -} - -#[derive(Default)] -pub struct NestedClass { - pub NestedClass: u32, - pub EnclosingClass: u32, -} - -#[derive(Default)] -pub struct Param { - pub Flags: u16, - pub Sequence: u16, - pub Name: u32, -} - -#[derive(Default)] -pub struct Property { - pub Flags: u16, - pub Name: u32, - pub Type: u32, -} - -#[derive(Default)] -pub struct TypeDef { - pub Flags: u32, - pub TypeName: u32, - pub TypeNamespace: u32, - pub Extends: u32, - pub FieldList: u32, - pub MethodList: u32, -} - -#[derive(Default)] -pub struct TypeRef { - pub ResolutionScope: u32, - pub TypeName: u32, - pub TypeNamespace: u32, -} - -#[derive(Default)] -pub struct TypeSpec { - pub Signature: u32, -} - -impl Tables { - pub fn stream(self) -> Vec<u8> { - let resolution_scope = coded_index_size(&[self.Module.len(), self.ModuleRef.len(), self.AssemblyRef.len(), self.TypeRef.len()]); - let type_def_or_ref = coded_index_size(&[self.TypeDef.len(), self.TypeRef.len(), self.TypeSpec.len()]); - let has_constant = coded_index_size(&[self.Field.len(), self.Param.len(), self.Property.len()]); - - let valid_tables: u64 = 1 << 0 | // Module - 1 << 0x01 | // TypeRef - 1 << 0x02 | // TypeDef - 1 << 0x04 | // Field - 1 << 0x06 | // MethodDef - 1 << 0x08 | // Param - 1 << 0x09 | // InterfaceImpl - 1 << 0x0A | // MemberRef - 1 << 0x0B | // Constant - 1 << 0x0C | // CustomAttribute - 1 << 0x0F | // ClassLayout - 1 << 0x17 | // Property - 1 << 0x1A | // ModuleRef - 1 << 0x1B | // TypeSpec - 1 << 0x1C | // ImplMap - 1 << 0x23 | // AssemblyRef - 1 << 0x29 | // NestedClass - 1 << 0x2A; // GenericParam - - // The table stream header... - - let mut buffer = Vec::new(); - buffer.write_u32(0); // Reserved - buffer.write_u8(2); // MajorVersion - buffer.write_u8(0); // MinorVersion - buffer.write_u8(0b111); // HeapSizes - buffer.write_u8(0); // Reserved - buffer.write_u64(valid_tables); - buffer.write_u64(0); // Sorted - - // Followed by the length of each of the valid tables... - - buffer.write_u32(self.Module.len() as u32); - buffer.write_u32(self.TypeRef.len() as u32); - buffer.write_u32(self.TypeDef.len() as u32); - buffer.write_u32(self.Field.len() as u32); - buffer.write_u32(self.MethodDef.len() as u32); - buffer.write_u32(self.Param.len() as u32); - buffer.write_u32(self.InterfaceImpl.len() as u32); - buffer.write_u32(self.MemberRef.len() as u32); - buffer.write_u32(self.Constant.len() as u32); - buffer.write_u32(self.CustomAttribute.len() as u32); - buffer.write_u32(self.ClassLayout.len() as u32); - buffer.write_u32(self.Property.len() as u32); - buffer.write_u32(self.ModuleRef.len() as u32); - buffer.write_u32(self.TypeSpec.len() as u32); - buffer.write_u32(self.ImplMap.len() as u32); - buffer.write_u32(self.AssemblyRef.len() as u32); - buffer.write_u32(self.NestedClass.len() as u32); - buffer.write_u32(self.GenericParam.len() as u32); - - // Followed by each table's rows... - - for x in self.Module { - buffer.write_u16(x.Generation); - buffer.write_u32(x.Name); - buffer.write_u32(x.Mvid); - buffer.write_u32(x.EncId); - buffer.write_u32(x.EncBaseId); - } - - for x in self.TypeRef { - buffer.write_code(x.ResolutionScope, resolution_scope); - buffer.write_u32(x.TypeName); - buffer.write_u32(x.TypeNamespace); - } - - for x in self.TypeDef { - buffer.write_u32(x.Flags); - buffer.write_u32(x.TypeName); - buffer.write_u32(x.TypeNamespace); - buffer.write_code(x.Extends, type_def_or_ref); - buffer.write_index(x.FieldList, self.Field.len()); - buffer.write_index(x.MethodList, self.MethodDef.len()); - } - - for x in self.Field { - buffer.write_u16(x.Flags); - buffer.write_u32(x.Name); - buffer.write_u32(x.Signature); - } - - for x in self.MethodDef { - buffer.write_u32(x.RVA); - buffer.write_u16(x.ImplFlags); - buffer.write_u16(x.Flags); - buffer.write_u32(x.Name); - buffer.write_u32(x.Signature); - buffer.write_index(x.ParamList, self.Param.len()); - } - - for x in self.Param { - buffer.write_u16(x.Flags); - buffer.write_u16(x.Sequence); - buffer.write_u32(x.Name); - } - - for x in self.Constant { - buffer.write_u16(x.Type); - buffer.write_code(x.Parent, has_constant); - buffer.write_u32(x.Value); - } - - for x in self.AssemblyRef { - buffer.write_u16(x.MajorVersion); - buffer.write_u16(x.MinorVersion); - buffer.write_u16(x.BuildNumber); - buffer.write_u16(x.RevisionNumber); - buffer.write_u32(x.Flags); - buffer.write_u32(x.PublicKeyOrToken); - buffer.write_u32(x.Name); - buffer.write_u32(x.Culture); - buffer.write_u32(x.HashValue); - } - - buffer.resize(round(buffer.len(), 4), 0); - buffer - } -} diff --git a/vendor/windows-metadata/src/writer/mod.rs b/vendor/windows-metadata/src/writer/mod.rs deleted file mode 100644 index 8dcf332b8..000000000 --- a/vendor/windows-metadata/src/writer/mod.rs +++ /dev/null @@ -1,95 +0,0 @@ -mod imp; -use super::*; - -// Generates an in-memory .winmd file. -pub fn write(name: &str, winrt: bool, items: &[Item], assemblies: &[&str]) -> Vec<u8> { - imp::write(name, winrt, items, assemblies) -} - -pub enum Item { - Struct(Struct), - Enum(Enum), - Interface(Interface), -} - -pub struct Struct { - pub namespace: String, - pub name: String, - pub fields: Vec<Field>, -} - -pub struct Enum { - pub namespace: String, - pub name: String, - pub constants: Vec<Constant>, -} - -pub struct Interface { - pub namespace: String, - pub name: String, - pub methods: Vec<Method>, -} - -pub struct Field { - pub name: String, - pub ty: Type, -} - -pub struct Constant { - pub name: String, - pub value: Value, -} - -pub struct Method { - pub name: String, - pub return_type: Type, - pub params: Vec<Param>, -} - -pub struct Param { - pub name: String, - pub ty: Type, - pub flags: ParamFlags, -} - -flags!(ParamFlags, u32); -impl ParamFlags { - pub const INPUT: Self = Self(0x1); - pub const OUTPUT: Self = Self(0x2); - pub const OPTIONAL: Self = Self(0x10); -} - -pub enum Type { - Void, - Bool, - Char, - I8, - U8, - I16, - U16, - I32, - U32, - I64, - U64, - F32, - F64, - ISize, - USize, - String, - Named((String, String)), -} - -pub enum Value { - Bool(bool), - U8(u8), - I8(i8), - U16(u16), - I16(i16), - U32(u32), - I32(i32), - U64(u64), - I64(i64), - F32(f32), - F64(f64), - String(String), -} |