Ejemplo n.º 1
0
        }                                                           // 3C File address of new exe header

        public static IMAGE_DOS_HEADER Deserialize(MultiPartFile file)
        {
            IMAGE_DOS_HEADER idh = new IMAGE_DOS_HEADER();

            idh.Magic                  = file.ReadUInt16();
            idh.LastPageBytes          = file.ReadUInt16();
            idh.Pages                  = file.ReadUInt16();
            idh.Relocations            = file.ReadUInt16();
            idh.HeaderParagraphSize    = file.ReadUInt16();
            idh.MinimumExtraParagraphs = file.ReadUInt16();
            idh.MaximumExtraParagraphs = file.ReadUInt16();
            idh.InitialSSValue         = file.ReadUInt16();
            idh.InitialSPValue         = file.ReadUInt16();
            idh.Checksum               = file.ReadUInt16();
            idh.InitialIPValue         = file.ReadUInt16();
            idh.InitialCSValue         = file.ReadUInt16();
            idh.RelocationTableAddr    = file.ReadUInt16();
            idh.OverlayNumber          = file.ReadUInt16();
            idh.Reserved1              = new ushort[Constants.ERES1WDS];
            for (int i = 0; i < Constants.ERES1WDS; i++)
            {
                idh.Reserved1[i] = file.ReadUInt16();
            }
            idh.OEMIdentifier  = file.ReadUInt16();
            idh.OEMInformation = file.ReadUInt16();
            idh.Reserved2      = new ushort[Constants.ERES2WDS];
            for (int i = 0; i < Constants.ERES2WDS; i++)
            {
                idh.Reserved2[i] = file.ReadUInt16();
            }
            idh.NewExeHeaderAddr = file.ReadInt32();

            return(idh);
        }
Ejemplo n.º 2
0
        public static NewRlcInfo Deserialize(MultiPartFile file)
        {
            NewRlcInfo nri = new NewRlcInfo();

            nri.RelocationItemCount = file.ReadUInt16();

            return(nri);
        }
Ejemplo n.º 3
0
        public static RsrcString Deserialize(MultiPartFile file)
        {
            RsrcString rs = new RsrcString();

            rs.Length = file.ReadByte();
            rs.Text   = file.ReadChars(rs.Length);

            return(rs);
        }
Ejemplo n.º 4
0
        public static IMAGE_DATA_DIRECTORY Deserialize(MultiPartFile file)
        {
            IMAGE_DATA_DIRECTORY idd = new IMAGE_DATA_DIRECTORY();

            idd.VirtualAddress = file.ReadUInt32();
            idd.Size           = file.ReadUInt32();

            return(idd);
        }
Ejemplo n.º 5
0
        public static IMAGE_RESOURCE_DIRECTORY_ENTRY Deserialize(MultiPartFile file)
        {
            IMAGE_RESOURCE_DIRECTORY_ENTRY irde = new IMAGE_RESOURCE_DIRECTORY_ENTRY();

            irde.Name         = file.ReadUInt32();
            irde.OffsetToData = file.ReadUInt32();

            return(irde);
        }
Ejemplo n.º 6
0
        public static IMAGE_RESOURCE_DIR_STRING_U Deserialize(MultiPartFile file)
        {
            IMAGE_RESOURCE_DIR_STRING_U irdsu = new IMAGE_RESOURCE_DIR_STRING_U();

            irdsu.Length     = file.ReadUInt16();
            irdsu.NameString = file.ReadChars(irdsu.Length);

            return(irdsu);
        }
Ejemplo n.º 7
0
        public static NewRsrc Deserialize(MultiPartFile file)
        {
            NewRsrc nr = new NewRsrc();

            nr.AlignmentShiftCount = file.ReadUInt16();
            nr.TypeInfo            = RsrcTypeInfo.Deserialize(file);

            return(nr);
        }
Ejemplo n.º 8
0
        public When_Reading_Non_Partial_Files()
        {
            Given(() =>
            {
                UseThe(MultiPartFile.OpenRead(TheNamed <string>("file1"), TheNamed <string>("file2")));
                UseThe(new StreamReader(The <MultiPartFileStream>()));
            });

            When(() => The <StreamReader>().ReadToEnd());
        }
Ejemplo n.º 9
0
        public static RsrcTypeInfo Deserialize(MultiPartFile file)
        {
            RsrcTypeInfo rti = new RsrcTypeInfo();

            rti.ID      = file.ReadUInt16();
            rti.rt_nres = file.ReadUInt16();
            rti.rt_proc = file.ReadUInt32();

            return(rti);
        }
        public static IMAGE_RESOURCE_DATA_ENTRY Deserialize(MultiPartFile file)
        {
            IMAGE_RESOURCE_DATA_ENTRY irde = new IMAGE_RESOURCE_DATA_ENTRY();

            irde.OffsetToData = file.ReadUInt32();
            irde.Size         = file.ReadUInt32();
            irde.CodePage     = file.ReadUInt32();
            irde.Reserved     = file.ReadUInt32();

            return(irde);
        }
Ejemplo n.º 11
0
 public InflateImpl(MultiPartFile inf, string outf)
 {
     inputBuffer         = new byte[0x4000];
     inputFile           = inf;
     outputFile          = File.OpenWrite(outf);
     InputSize           = 0;
     OutputSize          = 0;
     inputBufferSize     = inputBuffer.Length;
     inputBufferPosition = inputBufferSize;
     crc = new CRC32();
 }
Ejemplo n.º 12
0
        public static TYPEINFO Deserialize(MultiPartFile file)
        {
            TYPEINFO ti = new TYPEINFO();

            ti.TypeID        = file.ReadUInt16();
            ti.ResourceCount = file.ReadUInt16();
            ti.Reserved      = file.ReadUInt32();
            ti.NameInfo      = NAMEINFO.Deserialize(file);

            return(ti);
        }
Ejemplo n.º 13
0
        public static NewSegdata Deserialize(MultiPartFile file)
        {
            NewSegdata nsd = new NewSegdata();

            nsd.Iterations        = file.ReadUInt16();
            nsd.TotalBytes        = file.ReadUInt16();
            nsd.IteratedDataBytes = file.ReadChar();
            nsd.DataBytes         = (char)BitConverter.GetBytes(nsd.Iterations)[0];

            return(nsd);
        }
Ejemplo n.º 14
0
        public static NewSeg Deserialize(MultiPartFile file)
        {
            NewSeg ns = new NewSeg();

            ns.StartFileSector   = file.ReadUInt16();
            ns.BytesInFile       = file.ReadUInt16();
            ns.Flags             = file.ReadUInt16();
            ns.MinimumAllocation = file.ReadUInt16();

            return(ns);
        }
Ejemplo n.º 15
0
        public static ResourceTable Deserialize(MultiPartFile file)
        {
            ResourceTable rt = new ResourceTable();

            rt.rscAlignShift    = file.ReadUInt16();
            rt.TypeInfo         = TYPEINFO.Deserialize(file);
            rt.rscEndTypes      = file.ReadUInt16();
            rt.rscResourceNames = null; // TODO: Figure out size
            rt.rscEndNames      = file.ReadByte();

            return(rt);
        }
Ejemplo n.º 16
0
        public static RsrcNameInfo Deserialize(MultiPartFile file)
        {
            RsrcNameInfo rni = new RsrcNameInfo();

            rni.Offset     = file.ReadUInt16();
            rni.Length     = file.ReadUInt16();
            rni.Flags      = file.ReadUInt16();
            rni.NameID     = file.ReadUInt16();
            rni.Handle     = file.ReadUInt16();
            rni.UsageCount = file.ReadUInt16();

            return(rni);
        }
Ejemplo n.º 17
0
        public static NAMEINFO Deserialize(MultiPartFile file)
        {
            NAMEINFO ni = new NAMEINFO();

            ni.Offset = file.ReadUInt16();
            ni.Length = file.ReadUInt16();
            ni.Flags  = file.ReadUInt16();
            ni.ID     = file.ReadUInt16();
            ni.Handle = file.ReadUInt16();
            ni.Usage  = file.ReadUInt16();

            return(ni);
        }
Ejemplo n.º 18
0
 public Given_Few_Partial_Files()
 {
     Given(() =>
     {
         using (MultiPartFileStream multiFileStream = MultiPartFile.Create(TheNamed <string>("mainFilePath"), 10))
             using (var writer = new StreamWriter(multiFileStream))
             {
                 writer.Write(TheNamed <string>("content"));
                 writer.Flush();
                 UseThe(multiFileStream.Files, "writtenFiles");
             }
     });
 }
        public static IMAGE_RESOURCE_DIRECTORY Deserialize(MultiPartFile file)
        {
            IMAGE_RESOURCE_DIRECTORY ird = new IMAGE_RESOURCE_DIRECTORY();

            ird.Characteristics      = file.ReadUInt32();
            ird.TimeDateStamp        = file.ReadUInt32();
            ird.MajorVersion         = file.ReadUInt16();
            ird.MinorVersion         = file.ReadUInt16();
            ird.NumberOfNamedEntries = file.ReadUInt16();
            ird.NumberOfIdEntries    = file.ReadUInt16();

            return(ird);
        }
Ejemplo n.º 20
0
        public static IMAGE_FILE_HEADER Deserialize(MultiPartFile file)
        {
            IMAGE_FILE_HEADER ifh = new IMAGE_FILE_HEADER();

            ifh.Machine              = file.ReadUInt16();
            ifh.NumberOfSections     = file.ReadUInt16();
            ifh.TimeDateStamp        = file.ReadUInt32();
            ifh.PointerToSymbolTable = file.ReadUInt32();
            ifh.NumberOfSymbols      = file.ReadUInt32();
            ifh.SizeOfOptionalHeader = file.ReadUInt16();
            ifh.Characteristics      = file.ReadUInt16();

            return(ifh);
        }
Ejemplo n.º 21
0
        public When_Write_To_File_With_Big_File_Limit()
        {
            Given(() =>
            {
                UseThe(MultiPartFile.Create(TheNamed <string>("mainFilePath"), perFileLength: fileSizeLimitInBytes));
                UseThe(new StreamWriter(The <MultiPartFileStream>()));
            });

            When(() =>
            {
                The <StreamWriter>().Write(TheNamed <string>("content"));
                The <StreamWriter>().Flush();
                The <StreamWriter>().Dispose();
                return(The <MultiPartFileStream>().Files);
            });
        }
Ejemplo n.º 22
0
        private byte[] GetMultipartFormData(Dictionary <string, object> postParameters, string boundary)
        {
            Stream formDataStream = new MemoryStream();

            foreach (var param in postParameters)
            {
                formDataStream.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n"));

                if (param.Value is MultiPartFile)
                {
                    MultiPartFile fileToUpload = (MultiPartFile)param.Value;

                    string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n",
                                                  boundary,
                                                  param.Key,
                                                  fileToUpload.FileName ?? param.Key,
                                                  fileToUpload.ContentType ?? "application/octet-stream");

                    formDataStream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header));

                    formDataStream.Write(fileToUpload.File, 0, fileToUpload.File.Length);

                    formDataStream.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n"));

                    string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
                                                    boundary,
                                                    "name",
                                                    fileToUpload.Guid);
                    formDataStream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));
                }
            }

            string footer = "\r\n--" + boundary + "--\r\n";

            formDataStream.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer));

            formDataStream.Position = 0;
            byte[] formData = new byte[formDataStream.Length];
            formDataStream.Read(formData, 0, formData.Length);

            formDataStream.Close();

            return(formData);
        }
        private Tuple <Stream, string> GetConvertedMedia(MultiPartFile data, EMediaType mediaType, int maxFileSize)
        {
            Stream stream;
            string extension;

            switch (mediaType)
            {
            case EMediaType.Video:

                var contentType = data.File.ContentType;
                switch (contentType)
                {
                case "video/ogg":
                case "video/mp4":
                case "video/webm":
                    stream    = data.File.OpenReadStream();
                    extension = GetExtensionFromContentType(contentType);
                    break;

                default:
                    if (!CanConvert)
                    {
                        throw new BadMediaException($"Videos with the type {contentType} are not allowed");
                    }
                    else
                    {
                        var video = new Video(data.File.OpenReadStream());
                        stream    = _videoConverter.ConvertToWebm(video).Stream;
                        extension = "webm";
                    }

                    break;
                }

                break;

            default:
                stream    = data.File.OpenReadStream();
                extension = GetExtensionFromContentType(data.File.ContentType);
                break;
            }

            return(new Tuple <Stream, string>(stream, extension));
        }
Ejemplo n.º 24
0
        public void Bind(T instance)
        {
            if (instance == null)
            {
                return;
            }
            formBinder.Bind(instance);

            var fileProperties = getFileProperties(instance);

            foreach (PropertyInfo property in fileProperties)
            {
                if (fileLookup.HasName(property.Name))
                {
                    MultiPartFile file = fileLookup.GetFiles(property.Name).First();
                    property.SetValue(instance, file.Contents);
                }
            }
        }
Ejemplo n.º 25
0
        public static IMAGE_OPTIONAL_HEADER Deserialize(MultiPartFile file)
        {
            IMAGE_OPTIONAL_HEADER ioh = new IMAGE_OPTIONAL_HEADER();

            ioh.Magic = file.ReadUInt16();
            ioh.MajorLinkerVersion      = file.ReadByte();
            ioh.MinorLinkerVersion      = file.ReadByte();
            ioh.SizeOfCode              = file.ReadUInt32();
            ioh.SizeOfInitializedData   = file.ReadUInt32();
            ioh.SizeOfUninitializedData = file.ReadUInt32();
            ioh.AddressOfEntryPoint     = file.ReadUInt32();
            ioh.BaseOfCode              = file.ReadUInt32();
            ioh.BaseOfData              = file.ReadUInt32();

            ioh.ImageBase                   = file.ReadUInt32();
            ioh.SectionAlignment            = file.ReadUInt32();
            ioh.FileAlignment               = file.ReadUInt32();
            ioh.MajorOperatingSystemVersion = file.ReadUInt16();
            ioh.MinorOperatingSystemVersion = file.ReadUInt16();
            ioh.MajorImageVersion           = file.ReadUInt16();
            ioh.MinorImageVersion           = file.ReadUInt16();
            ioh.MajorSubsystemVersion       = file.ReadUInt16();
            ioh.MinorSubsystemVersion       = file.ReadUInt16();
            ioh.Reserved1                   = file.ReadUInt32();
            ioh.SizeOfImage                 = file.ReadUInt32();
            ioh.SizeOfHeaders               = file.ReadUInt32();
            ioh.CheckSum            = file.ReadUInt32();
            ioh.Subsystem           = file.ReadUInt16();
            ioh.DllCharacteristics  = file.ReadUInt16();
            ioh.SizeOfStackReserve  = file.ReadUInt32();
            ioh.SizeOfStackCommit   = file.ReadUInt32();
            ioh.SizeOfHeapReserve   = file.ReadUInt32();
            ioh.SizeOfHeapCommit    = file.ReadUInt32();
            ioh.LoaderFlags         = file.ReadUInt32();
            ioh.NumberOfRvaAndSizes = file.ReadUInt32();
            ioh.DataDirectory       = new IMAGE_DATA_DIRECTORY[Constants.IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
            for (int i = 0; i < Constants.IMAGE_NUMBEROF_DIRECTORY_ENTRIES; i++)
            {
                ioh.DataDirectory[i] = IMAGE_DATA_DIRECTORY.Deserialize(file);
            }

            return(ioh);
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Open a potentially-multipart file for analysis and extraction
        /// </summary>
        /// <param name="file">Possible wise installer base</param>
        /// <returns>True if the file could be opened, false otherwise</returns>
        private bool Open(string file)
        {
            inputFile = MultiPartFile.Create(file);
            if (inputFile == null)
            {
                return(false);
            }

            file = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file));

            int fileno = 2;

            while (inputFile.Append($"{file}.w{fileno / 10 + 48}{fileno % 10 + 48}"))
            {
                fileno++;
            }

            return(true);
        }
        public async Task <StatusCodeResult> AddMediaAsync(string id, MultiPartFile data, EMediaType mediaType,
                                                           int maxFileSize = int.MaxValue)
        {
            if (data?.File == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var residentObjectId = await CanWriteDataToResidentAsync(id);

            var convertedMedia = GetConvertedMedia(data, mediaType, maxFileSize);
            var title          = data.File.FileName;

            using (var stream = convertedMedia.Item1)
            {
                await((IResidentsService)DataService)
                .AddMediaAsync(residentObjectId, title, stream, mediaType, convertedMedia.Item2);
            }

            return(StatusCode((int)HttpStatusCode.Created));
        }
Ejemplo n.º 28
0
 public void Add(string name, MultiPartFile file)
 {
     if (file == null)
     {
         throw new ArgumentNullException("file");
     }
     if (name == null)
     {
         nullFiles.Add(file);
     }
     else
     {
         List<MultiPartFile> files;
         if (!fileLookup.TryGetValue(name ?? String.Empty, out files))
         {
             files = new List<MultiPartFile>();
             fileLookup.Add(name ?? String.Empty, files);
         }
         files.Add(file);
     }
 }
Ejemplo n.º 29
0
        public static IMAGE_SECTION_HEADER Deserialize(MultiPartFile file)
        {
            IMAGE_SECTION_HEADER ish = new IMAGE_SECTION_HEADER();

            ish.Name = file.ReadBytes(Constants.IMAGE_SIZEOF_SHORT_NAME);

            // Misc
            ish.PhysicalAddress = file.ReadUInt32();
            ish.VirtualSize     = ish.PhysicalAddress;

            ish.VirtualAddress       = file.ReadUInt32();
            ish.SizeOfRawData        = file.ReadUInt32();
            ish.PointerToRawData     = file.ReadUInt32();
            ish.PointerToRelocations = file.ReadUInt32();
            ish.PointerToLinenumbers = file.ReadUInt32();
            ish.NumberOfRelocations  = file.ReadUInt16();
            ish.NumberOfLinenumbers  = file.ReadUInt16();
            ish.Characteristics      = (SectionCharacteristics)file.ReadUInt32();

            return(ish);
        }
Ejemplo n.º 30
0
        }                                                       // 3F Windows SDK version number

        public static IMAGE_OS2_HEADER Deserialize(MultiPartFile file)
        {
            IMAGE_OS2_HEADER ioh = new IMAGE_OS2_HEADER();

            ioh.Magic                       = file.ReadUInt16();
            ioh.LinkerVersion               = file.ReadByte();
            ioh.LinkerRevision              = file.ReadByte();
            ioh.EntryTableOffset            = file.ReadUInt16();
            ioh.EntryTableSize              = file.ReadUInt16();
            ioh.CrcChecksum                 = file.ReadUInt32();
            ioh.Flags                       = file.ReadUInt16();
            ioh.Autodata                    = file.ReadUInt16();
            ioh.InitialHeapAlloc            = file.ReadUInt16();
            ioh.InitialStackAlloc           = file.ReadUInt16();
            ioh.InitialCSIPSetting          = file.ReadUInt32();
            ioh.InitialSSSPSetting          = file.ReadUInt32();
            ioh.FileSegmentCount            = file.ReadUInt16();
            ioh.ModuleReferenceTableSize    = file.ReadUInt16();
            ioh.NonResidentNameTableSize    = file.ReadUInt16();
            ioh.SegmentTableOffset          = file.ReadUInt16();
            ioh.ResourceTableOffset         = file.ReadUInt16();
            ioh.ResidentNameTableOffset     = file.ReadUInt16();
            ioh.ModuleReferenceTableOffset  = file.ReadUInt16();
            ioh.ImportedNamesTableOffset    = file.ReadUInt16();
            ioh.NonResidentNamesTableOffset = file.ReadUInt32();
            ioh.MovableEntriesCount         = file.ReadUInt16();
            ioh.SegmentAlignmentShiftCount  = file.ReadUInt16();
            ioh.ResourceEntriesCount        = file.ReadUInt16();
            ioh.TargetOperatingSystem       = file.ReadByte();
            ioh.AdditionalFlags             = file.ReadByte();
            ioh.Reserved                    = new ushort[Constants.NERESWORDS];
            for (int i = 0; i < Constants.NERESWORDS; i++)
            {
                ioh.Reserved[i] = file.ReadUInt16();
            }
            ioh.WindowsSDKRevision = file.ReadByte();
            ioh.WindowsSDKVersion  = file.ReadByte();

            return(ioh);
        }
        public async Task UpdatePictureAsync(string id, [FromForm] MultiPartFile picture)
        {
            const int maxFileSize = (int)10e6;

            if (picture?.File == null)
            {
                throw new ArgumentNullException(nameof(picture));
            }

            var objectId = await CanWriteDataToResidentAsync(id);

            try
            {
                var bytes = picture.ConvertToBytes(maxFileSize);

                await DataService.UpdatePropertyAsync(objectId, x => x.Picture, bytes);
            }
            catch (FileToLargeException)
            {
                throw new FileToLargeException(maxFileSize);
            }
        }
        public void Extract(HttpListenerRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (!request.HasEntityBody)
            {
                return;
            }
            // Make sure it is a multi-part request
            string[] parts = request.ContentType.Split(';').Select(s => s.Trim()).ToArray();
            if (!parts[0].Equals("multipart/form-data", StringComparison.InvariantCultureIgnoreCase))
            {
                return;
            }
            // Parse the content type parameters
            var contentTypeParameters = parts
                .Skip(1)
                .Select(p => p.Split(new char[] { '=' }, 2))
                .Where(p => p.Length == 2)
                .ToLookup(p => p[0], p => p[1], StringComparer.InvariantCultureIgnoreCase);
            // Check the boundary is specified, and only once
            if (contentTypeParameters["boundary"].Count() != 1)
            {
                return;
            }
            string boundary = contentTypeParameters["boundary"].First();

            using (Stream responseStream = request.InputStream)
            {
                Encoding encoding = request.ContentEncoding;
                StreamingMultiPartParser parser = new StreamingMultiPartParser(responseStream, encoding, boundary);

                parser.SectionFound += (o, e) =>
                {
                    var data = getSectionData(e);
                    if (data == null)
                    {
                        return;
                    }
                    if (String.IsNullOrWhiteSpace(data.FileName))
                    {
                        string value = encoding.GetString(data.Contents);
                        this.parameters.Add(data.Name, value);
                    }
                    else
                    {
                        var file = new MultiPartFile()
                        {
                            Name = data.Name,
                            FileName = data.FileName,
                            ContentType = data.ContentType,
                            Contents = data.Contents
                        };
                        this.files.Add(file.Name, file);
                    }
                };

                parser.Parse().Wait();
            }
        }