internal void Save() { if (Validate()) { CompoundDocument doc = new CompoundDocument(); doc.Storage = new CompoundDocument.StoragePart(); var store = new CompoundDocument.StoragePart(); doc.Storage.SubStorage.Add("VBA", store); store.DataStreams.Add("_VBA_PROJECT", CreateVBAProjectStream()); store.DataStreams.Add("dir", CreateDirStream()); foreach (var module in Modules) { store.DataStreams.Add(module.Name, CompoundDocument.CompressPart(Encoding.GetEncoding(CodePage).GetBytes(module.Attributes.GetAttributeText() + module.Code))); } //Copy streams from the template, if used. if (Document != null) { foreach (var ss in Document.Storage.SubStorage) { if (ss.Key != "VBA") { doc.Storage.SubStorage.Add(ss.Key, ss.Value); } } foreach (var s in Document.Storage.DataStreams) { if (s.Key != "dir" && s.Key != "PROJECT" && s.Key != "PROJECTwm") { doc.Storage.DataStreams.Add(s.Key, s.Value); } } } doc.Storage.DataStreams.Add("PROJECT", CreateProjectStream()); doc.Storage.DataStreams.Add("PROJECTwm", CreateProjectwmStream()); if (Part == null) { Uri = new Uri(PartUri, UriKind.Relative); Part = _pck.CreatePart(Uri, ExcelPackage.schemaVBA); var rel = _wb.Part.CreateRelationship(Uri, TargetMode.Internal, schemaRelVba); } var vbaBuffer = doc.Save(); var st = Part.GetStream(FileMode.Create); st.Write(vbaBuffer, 0, vbaBuffer.Length); st.Flush(); st.Close(); //Save the digital signture Signature.Save(this); } }
private byte[] CreateProjectwmStream() { BinaryWriter bw = new BinaryWriter(new MemoryStream()); foreach (var module in Modules) { bw.Write(Encoding.GetEncoding(CodePage).GetBytes(module.Name)); //Name bw.Write((byte)0); //Null bw.Write(Encoding.Unicode.GetBytes(module.Name)); //Name bw.Write((ushort)0); //Null } bw.Write((ushort)0); //Null return(CompoundDocument.CompressPart(((MemoryStream)bw.BaseStream).ToArray())); }
public void Compression() { //Compression/Decompression string value = "#aaabcdefaaaaghijaaaaaklaaamnopqaaaaaaaaaaaarstuvwxyzaaa"; byte[] compValue = CompoundDocument.CompressPart(Encoding.GetEncoding(1252).GetBytes(value)); string decompValue = Encoding.GetEncoding(1252).GetString(CompoundDocument.DecompressPart(compValue)); Assert.AreEqual(value, decompValue); value = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; compValue = CompoundDocument.CompressPart(Encoding.GetEncoding(1252).GetBytes(value)); decompValue = Encoding.GetEncoding(1252).GetString(CompoundDocument.DecompressPart(compValue)); Assert.AreEqual(value, decompValue); }
/// <summary> /// MS-OVBA 2.3.4.1 /// </summary> /// <returns></returns> private byte[] CreateDirStream() { BinaryWriter bw = new BinaryWriter(new MemoryStream()); /****** PROJECTINFORMATION Record ******/ bw.Write((ushort)1); //ID bw.Write((uint)4); //Size bw.Write((uint)SystemKind); //SysKind bw.Write((ushort)2); //ID bw.Write((uint)4); //Size bw.Write((uint)Lcid); //Lcid bw.Write((ushort)0x14); //ID bw.Write((uint)4); //Size bw.Write((uint)LcidInvoke); //Lcid Invoke bw.Write((ushort)3); //ID bw.Write((uint)2); //Size bw.Write((ushort)CodePage); //Codepage //ProjectName bw.Write((ushort)4); //ID bw.Write((uint)Name.Length); //Size bw.Write(Encoding.GetEncoding(CodePage).GetBytes(Name)); //Project Name //Description bw.Write((ushort)5); //ID bw.Write((uint)Description.Length); //Size bw.Write(Encoding.GetEncoding(CodePage).GetBytes(Description)); //Project Name bw.Write((ushort)0x40); //ID bw.Write((uint)Description.Length * 2); //Size bw.Write(Encoding.Unicode.GetBytes(Description)); //Project Description //Helpfiles bw.Write((ushort)6); //ID bw.Write((uint)HelpFile1.Length); //Size bw.Write(Encoding.GetEncoding(CodePage).GetBytes(HelpFile1)); //HelpFile1 bw.Write((ushort)0x3D); //ID bw.Write((uint)HelpFile2.Length); //Size bw.Write(Encoding.GetEncoding(CodePage).GetBytes(HelpFile2)); //HelpFile2 //Help context id bw.Write((ushort)7); //ID bw.Write((uint)4); //Size bw.Write((uint)HelpContextID); //Help context id //Libflags bw.Write((ushort)8); //ID bw.Write((uint)4); //Size bw.Write((uint)0); //Help context id //Vba Version bw.Write((ushort)9); //ID bw.Write((uint)4); //Reserved bw.Write((uint)MajorVersion); //Reserved bw.Write((ushort)MinorVersion); //Help context id //Constants bw.Write((ushort)0x0C); //ID bw.Write((uint)Constants.Length); //Size bw.Write(Encoding.GetEncoding(CodePage).GetBytes(Constants)); //Help context id bw.Write((ushort)0x3C); //ID bw.Write((uint)Constants.Length / 2); //Size bw.Write(Encoding.Unicode.GetBytes(Constants)); //HelpFile2 /****** PROJECTREFERENCES Record ******/ foreach (var reference in References) { WriteNameReference(bw, reference); if (reference.ReferenceRecordID == 0x2F) { WriteControlReference(bw, reference); } else if (reference.ReferenceRecordID == 0x33) { WriteOrginalReference(bw, reference); } else if (reference.ReferenceRecordID == 0x0D) { WriteRegisteredReference(bw, reference); } else if (reference.ReferenceRecordID == 0x0E) { WriteProjectReference(bw, reference); } } bw.Write((ushort)0x0F); bw.Write((uint)0x02); bw.Write((ushort)Modules.Count); bw.Write((ushort)0x13); bw.Write((uint)0x02); bw.Write((ushort)0xFFFF); foreach (var module in Modules) { WriteModuleRecord(bw, module); } bw.Write((ushort)0x10); //Terminator bw.Write((uint)0); return(CompoundDocument.CompressPart(((MemoryStream)bw.BaseStream).ToArray())); }