Example #1
0
        protected void DoTests(FileHeader header, FileType expectedFileType, string expectedExtension)
        {
            string fileTypeErrorMessage;
               switch (expectedFileType)
               {
              default:
              case FileType.Other:
                 fileTypeErrorMessage = "Unknown file type";
                 break;
              case FileType.Image:
                 fileTypeErrorMessage = "Should be an image file";
                 break;
              case FileType.Video:
                 fileTypeErrorMessage = "Should be a video file";
                 break;
              case FileType.Audio:
                 fileTypeErrorMessage = "Should be an audio file";
                 break;
              case FileType.Swf:
                 fileTypeErrorMessage = "Should be a flash file";
                 break;
               }

               var extensionErrorMessage = "File format should be " + expectedExtension;

               Assert.NotNull(header, "File header returned null");
               Assert.AreEqual(header.Type, expectedFileType, fileTypeErrorMessage);
               Assert.AreEqual(header.Extension, expectedExtension, extensionErrorMessage);
        }
Example #2
0
		protected StorageStream(ITransactionalStorage transactionalStorage, string fileName,
								StorageStreamAccess storageStreamAccess,
								RavenJObject metadata, IndexStorage indexStorage, StorageOperationsTask operations)
		{
			TransactionalStorage = transactionalStorage;
			StorageStreamAccess = storageStreamAccess;
			Name = fileName;

			switch (storageStreamAccess)
			{
				case StorageStreamAccess.Read:
					TransactionalStorage.Batch(accessor => fileHeader = accessor.ReadFile(fileName));
					if (fileHeader.TotalSize == null)
					{
						throw new FileNotFoundException("File is not uploaded yet");
					}
					Metadata = fileHeader.Metadata;
					Seek(0, SeekOrigin.Begin);
					break;
				case StorageStreamAccess.CreateAndWrite:
					TransactionalStorage.Batch(accessor =>
					{
						operations.IndicateFileToDelete(fileName);
						accessor.PutFile(fileName, null, metadata);
						indexStorage.Index(fileName, metadata);
					});
					Metadata = metadata;
					break;
				default:
					throw new ArgumentOutOfRangeException("storageStreamAccess", storageStreamAccess, "Unknown value");
			}
		}
Example #3
0
			public ConflictResolutionStrategy ConflictDetected(FileHeader local, FileHeader remote, string sourceServerUri)
			{
				if (local.LastModified.CompareTo(remote.LastModified) >= 0)
					return ConflictResolutionStrategy.CurrentVersion;
				else
					return ConflictResolutionStrategy.RemoteVersion;
			}
Example #4
0
    public PakElement(PakFile Owner, FileHeader Header, ExtendedFileStream Stream)
    {
        this.Owner = Owner;
        this.Id = Owner.GetId();
        this.Stream = Stream;
        this.Header = Header;

        GetExtention();
    }
 public DownloadFile(
     FileHeader fileHeader, 
     IServerConnection serverConnection, 
     IEventAggregator eventAggregator)
 {
     _fileHeader = fileHeader;
     _serverConnection = serverConnection;
     _eventAggregator = eventAggregator;
 }
Example #6
0
    public void DeList()
    {
        if (_Elements.Count > 0)
            return;

        Decoded = false;
        Extractor.Instance.Progress(0);

        if (Headers == null)
        {
            Headers = new PakHeaders();
            Headers.FileName = FileName;
            Headers.One = Stream.GetInt();
            Headers.FileSize = Stream.GetInt();
            Headers.Padding = Stream.GetInt();

            Headers.Hsize = Stream.GetInt();

            Headers.HeaderSize = Headers.Hsize / 60;

            for (int i = 0; i < Headers.HeaderSize; i++)
            {
                FileHeader Header = new FileHeader();

                string Unk1 = Stream.GetString(20);

                Header.ZSize = Stream.GetInt();
                Header.Size = Stream.GetInt();
                Header.Unk2 = Stream.GetInt();
                Header.Start = Stream.GetInt();

                string Unk3 = Stream.GetString(24);

                Headers.Files.Add(Header);

                Extractor.Instance.Tool("Reading header :" + i + "/" + Headers.HeaderSize + ": " + Headers.FileName);
                Extractor.Instance.Progress((i * 100) / Headers.HeaderSize);
            }
        }

        int Total = Headers.Files.Count;
        for (int i = 0; i < Total; ++i)
        {
            Extractor.Instance.Progress((i * 100) / Total);
            FileHeader Header = Headers.Files[i];
            PakElement Ep = new PakElement(this, Header, Stream);
            Ep.GetExtention();
            _Elements.Add(Ep);
        }

        ExtractorMgr.SaveHeader(Headers);

        Extractor.Instance.Progress(100);
        Decoded = true;
    }
Example #7
0
        public void Constructor_Successful()
        {
            byte[] headerInfo = new byte[]
            {
                0x00, 0x00,
                0xE7, 0x03, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0xE7, 0x03, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00
            };

            FileHeader fileHeader = new FileHeader(headerInfo);

            Assert.AreEqual(999, fileHeader.Size);
            Assert.AreEqual(999, fileHeader.PixelArrayOffset);
        }
Example #8
0
			public bool BeforeChange(FileHeader instance, RavenJObject metadata, RavenJObject original)
			{
				var tags = metadata.Value<RavenJArray>("Tags");

				if (tags == null)
					return true;

				foreach (var banned in bannedTags)
				{
					tags.Remove(banned);
				}

				metadata["Tags"] = tags; 

				return true;
			}
		public bool Filter(FileHeader file, Guid destinationId, IEnumerable<FileHeader> candidatesToSynchronization)
		{
			// prevent synchronization back to source
			if (file.Metadata[SynchronizationConstants.RavenSynchronizationSource] == destinationId.ToString())
				return false;

			if (file.Name.EndsWith(RavenFileNameHelper.DownloadingFileSuffix))
				return false;

			if (file.Name.EndsWith(RavenFileNameHelper.DeletingFileSuffix))
				return false;

			if (file.IsFileBeingUploadedOrUploadHasBeenBroken())
				return false;

			if (ExistsRenameTombstone(file.Name, candidatesToSynchronization))
				return false;

			return true;
		}
Example #10
0
        public void Load(byte[] DataArray)
        {
            Data = DataArray;

            Header = new FileHeader(Data);

            UInt32 ReadOffset = Header.VertexOffset;
            for (int i = 0; i < Header.VertexCount; i++)
                Vertices.Add(new Vertex(Data, ref ReadOffset));

            ReadOffset = Header.TriangleOffset;
            for (int i = 0; i < Header.TriangleCount; i++)
                Triangles.Add(new Triangle(Data, ref ReadOffset));

            ReadOffset = Header.TypeOffset;
            for (int i = 0; i < Header.TypeCount; i++)
                Types.Add(new Type(Data, ref ReadOffset));

            Root.Nodes.Add(Helpers.CreateTreeNode(Name, this, string.Format("Size: {0:X6}", Data.Length)));

            Prepare();
        }
        public HttpResponseMessage Delete(string fileSystemName, string fileName)
        {
            var canonicalFilename = FileHeader.Canonize(fileName);

            var sourceServerInfo = InnerHeaders.Value <ServerInfo>(SyncingMultipartConstants.SourceServerInfo);
            var sourceFileETag   = Guid.Parse(InnerHeaders.GetValues(Constants.MetadataEtagField).First().Trim('\"'));

            Log.Debug("Starting to delete a file '{0}' with ETag {1} from {2} because of synchronization", fileName, sourceFileETag, sourceServerInfo);

            var report = new SynchronizationReport(canonicalFilename, sourceFileETag, SynchronizationType.Delete);

            try
            {
                Storage.Batch(accessor =>
                {
                    AssertFileIsNotBeingSynced(canonicalFilename, accessor);
                    FileLockManager.LockByCreatingSyncConfiguration(canonicalFilename, sourceServerInfo, accessor);
                });


                SynchronizationTask.IncomingSynchronizationStarted(canonicalFilename, sourceServerInfo, sourceFileETag, SynchronizationType.Delete);

                PublishSynchronizationNotification(fileSystemName, canonicalFilename, sourceServerInfo, report.Type, SynchronizationAction.Start);

                Storage.Batch(accessor => StartupProceed(canonicalFilename, accessor));

                var localMetadata = GetLocalMetadata(canonicalFilename);

                if (localMetadata != null)
                {
                    // REVIEW: Use InnerHeaders for consistency?
                    var sourceMetadata = GetFilteredMetadataFromHeaders(Request.Headers); // Request.Headers.FilterHeadersToObject();

                    bool isConflictResolved;

                    AssertConflictDetection(canonicalFilename, localMetadata, sourceMetadata, sourceServerInfo, out isConflictResolved);

                    Storage.Batch(accessor =>
                    {
                        StorageOperationsTask.IndicateFileToDelete(canonicalFilename);

                        var tombstoneMetadata = new RavenJObject
                        {
                            {
                                SynchronizationConstants.RavenSynchronizationHistory,
                                localMetadata[SynchronizationConstants.RavenSynchronizationHistory]
                            },
                            {
                                SynchronizationConstants.RavenSynchronizationVersion,
                                localMetadata[SynchronizationConstants.RavenSynchronizationVersion]
                            },
                            {
                                SynchronizationConstants.RavenSynchronizationSource,
                                localMetadata[SynchronizationConstants.RavenSynchronizationSource]
                            }
                        }.WithDeleteMarker();

                        Historian.UpdateLastModified(tombstoneMetadata);
                        accessor.PutFile(canonicalFilename, 0, tombstoneMetadata, true);
                    });

                    PublishFileNotification(fileName, FileChangeAction.Delete);
                }
            }
            catch (Exception ex)
            {
                if (ShouldAddExceptionToReport(ex))
                {
                    report.Exception = ex;

                    Log.WarnException(string.Format("Error was occurred during deletion synchronization of file '{0}' from {1}", fileName, sourceServerInfo), ex);
                }
            }
            finally
            {
                FinishSynchronization(canonicalFilename, report, sourceServerInfo, sourceFileETag);
            }

            PublishSynchronizationNotification(fileSystemName, fileName, sourceServerInfo, report.Type, SynchronizationAction.Finish);

            if (report.Exception == null)
            {
                Log.Debug("File '{0}' was deleted during synchronization from {1}", fileName, sourceServerInfo);
            }

            return(this.GetMessageWithObject(report, HttpStatusCode.OK));
        }
Example #12
0
        /// <summary>
        /// Decompress ZIP file
        /// </summary>
        /// <param name="fileHeader">The fh.</param>
        /// <param name="rootPathName">Name of the root path, save the decompressed file in this directory.</param>
        /// <param name="newFileName">New name of the file, if this file name is given it overwrite the name in the FH structure</param>
        /// <param name="createPath">if set to <c>true</c> [create path].</param>
        /// <param name="overWrite">if set to <c>true</c> [over write].</param>
        /// <exception cref="UZipDotNet.Exception">
        /// Extract file failed. Invalid directory path
        /// or
        /// Unsupported compression method
        /// or
        /// ZIP file CRC test failed
        /// </exception>
        public void Decompress(FileHeader fileHeader, string rootPathName, string newFileName, bool createPath, bool overWrite)
        {
            try
            {
                // save file header
                _fileHeaderInfo = fileHeader;

                // read file header for this file and compare it to the directory information
                ReadFileHeader(_fileHeaderInfo.FilePos);

                // compressed length
                _readRemain = _fileHeaderInfo.CompSize;
                ReadTotal = _readRemain;

                // build write file name
                // Root name (optional) plus either original name or a new name
                _writeFileName = (string.IsNullOrEmpty(rootPathName) ? string.Empty :
                    (rootPathName.EndsWith("\\") ? rootPathName : rootPathName + "\\")) +
                    (string.IsNullOrEmpty(newFileName) ? fileHeader.FileName : newFileName);

                // test if write file name has a path component
                int ptr = _writeFileName.LastIndexOf('\\');
                if (ptr >= 0)
                {
                    // make sure directory exists
                    if (!Directory.Exists(_writeFileName.Substring(0, ptr)))
                    {
                        // make a new folder
                        if (createPath) Directory.CreateDirectory(_writeFileName.Substring(0, ptr));

                        // error
                        else throw new Exception("Extract file failed. Invalid directory path");
                    }
                }

                // create destination file
                _writeStream = new FileStream(_writeFileName, overWrite ? FileMode.Create : FileMode.CreateNew, FileAccess.Write, FileShare.None);

                // convert stream to binary writer
                _writeFile = new BinaryWriter(_writeStream, Encoding.UTF8);

                // reset crc32 checksum
                _writeCrc32 = 0;

                // switch based on compression method
                switch (_fileHeaderInfo.CompMethod)
                {
                    // no compression
                    case 0:
                        NoCompression();
                        break;

                    // deflate compress method
                    case 8:
                        // decompress file
                        Decompress();
                        break;

                    // not supported
                    default:
                        throw new Exception("Unsupported compression method");
                }

                // Zip file checksum is CRC32
                if (_fileHeaderInfo.FileCRC32 != _writeCrc32) throw new Exception("ZIP file CRC test failed");

                // save file length
                WriteTotal = (uint)_writeStream.Length;

                // close write file
                _writeFile.Close();
                _writeFile = null;

                // if file times are available set the file time
                if (_fileTimeAvailable)
                {
                    File.SetCreationTime(_writeFileName, _fileCreateTime);
                    File.SetLastWriteTime(_writeFileName, _fileModifyTime);
                    File.SetLastAccessTime(_writeFileName, _fileAccessTime);
                }
                else
                {
                    // convert dos file date and time to DateTime format
                    var fileDosTime = new DateTime(1980 + ((_fileHeaderInfo.FileDate >> 9) & 0x7f),
                        (_fileHeaderInfo.FileDate >> 5) & 0xf, _fileHeaderInfo.FileDate & 0x1f,
                        (_fileHeaderInfo.FileTime >> 11) & 0x1f, (_fileHeaderInfo.FileTime >> 5) & 0x3f, 2 * (_fileHeaderInfo.FileTime & 0x1f));
                    File.SetCreationTime(_writeFileName, fileDosTime);
                    File.SetLastWriteTime(_writeFileName, fileDosTime);
                    File.SetLastAccessTime(_writeFileName, fileDosTime);
                }

                // set file attribute attributes
                if (_fileHeaderInfo.FileAttr != 0) File.SetAttributes(_writeFileName, _fileHeaderInfo.FileAttr);
            }
            catch (Exception)
            {
                // close the write file if it is open
                if (_writeFile != null)
                {
                    _writeFile.Close();
                    _writeFile = null;
                }

                throw;
            }
        }
 internal override RarFilePart CreateFilePart(FileHeader fileHeader, MarkHeader markHeader)
 {
     return(new FileInfoRarFilePart(this, markHeader, fileHeader, FileInfo));
 }
        public async void Should_synchronize_to_all_destinations()
        {
            var canonicalFilename = FileHeader.Canonize("test.bin");

            var sourceContent = SyncTestUtils.PrepareSourceStream(10000);

            sourceContent.Position = 0;

            var sourceClient = NewAsyncClient(0);

            var destination1Client = NewAsyncClient(1);
            var destination2Client = NewAsyncClient(2);

            var destination1Content = new RandomlyModifiedStream(sourceContent, 0.01);

            sourceContent.Position = 0;
            var destination2Content = new RandomlyModifiedStream(sourceContent, 0.01);

            sourceContent.Position = 0;

            await destination1Client.UploadAsync("test.bin", destination1Content);

            await destination2Client.UploadAsync("test.bin", destination2Content);

            sourceContent.Position = 0;
            await sourceClient.UploadAsync("test.bin", sourceContent);

            sourceContent.Position = 0;

            sourceClient.Synchronization.SetDestinationsAsync(destination1Client.ToSynchronizationDestination(), destination2Client.ToSynchronizationDestination()).Wait();

            var destinationSyncResults = sourceClient.Synchronization.SynchronizeAsync().Result;

            // we expect conflicts after first attempt of synchronization
            Assert.Equal(2, destinationSyncResults.Length);
            Assert.Equal(string.Format("File {0} is conflicted", canonicalFilename), destinationSyncResults[0].Reports.ToArray()[0].Exception.Message);
            Assert.Equal(string.Format("File {0} is conflicted", canonicalFilename), destinationSyncResults[1].Reports.ToArray()[0].Exception.Message);

            await destination1Client.Synchronization.ResolveConflictAsync("test.bin", ConflictResolutionStrategy.RemoteVersion);

            await destination2Client.Synchronization.ResolveConflictAsync("test.bin", ConflictResolutionStrategy.RemoteVersion);

            destinationSyncResults = await sourceClient.Synchronization.SynchronizeAsync();

            var conflictItem = await destination1Client.Configuration.GetKeyAsync <ConflictItem>(RavenFileNameHelper.ConflictConfigNameForFile("test.bin"));

            Assert.Null(conflictItem);

            conflictItem = await destination2Client.Configuration.GetKeyAsync <ConflictItem>(RavenFileNameHelper.ConflictConfigNameForFile("test.bin"));

            Assert.Null(conflictItem);

            // check if reports match
            Assert.Equal(2, destinationSyncResults.Length);
            var result1 = destinationSyncResults[0].Reports.ToArray()[0];

            Assert.Equal(sourceContent.Length, result1.BytesCopied + result1.BytesTransfered);
            Assert.Equal(SynchronizationType.ContentUpdate, result1.Type);

            var result2 = destinationSyncResults[1].Reports.ToArray()[0];

            Assert.Equal(sourceContent.Length, result2.BytesCopied + result2.BytesTransfered);
            Assert.Equal(SynchronizationType.ContentUpdate, result2.Type);

            // check content of files
            string destination1Md5;

            using (var resultFileContent = await destination1Client.DownloadAsync("test.bin"))
            {
                destination1Md5 = resultFileContent.GetMD5Hash();
            }

            string destination2Md5;

            using (var resultFileContent = await destination2Client.DownloadAsync("test.bin"))
            {
                destination2Md5 = resultFileContent.GetMD5Hash();
            }

            sourceContent.Position = 0;
            var sourceMd5 = sourceContent.GetMD5Hash();

            Assert.Equal(sourceMd5, destination1Md5);
            Assert.Equal(sourceMd5, destination2Md5);
            Assert.Equal(destination1Md5, destination2Md5);
        }
Example #15
0
 public bool importFile(int fnum,bool isid,byte[] data)
 {
     DinnerEntry e = fileInfo(fnum, isid);
     FileHeader hdr = readHeader(fnum, isid);
     byte[] fl;
     if (e.compressed && hdr.compressed && hdr.tot_size != e.size)
     {
         UInt32 decompsz = (uint)data.Length;
         if (e.header)
         {
             hdr = new FileHeader(data);
             fl = data;
             data = new byte[fl.Length - FileHeader.size];
             Array.Copy(fl, FileHeader.size,data,0, data.Length);
             hdr.setSize(decompsz);
         }
         else
             hdr.setSize(decompsz);
         byte[] cmpr = RNC.compress(data);
         data=new byte[cmpr.Length+FileHeader.size];
         Array.Copy(hdr.buf, 0, data, 0, FileHeader.size);
         Array.Copy(cmpr, 0, data, FileHeader.size, cmpr.Length);
     }
     int resize = data.Length - (int)e.size;
     if (resize != 0)
         resizeDisk(resize,ref e);
     Array.Copy(data, 0, diskbuf, e.offset, data.Length);
     return true;
 }
Example #16
0
        public async Task NotificationsAreReceivedOnSourceWhenSynchronizationsAreStartedAndFinished()
        {
            // content update
            await sourceClient.UploadAsync("test.bin", new MemoryStream(new byte[] { 1, 2, 3 }));

            var notificationTask = sourceStore.Changes().ForSynchronization()
                                   .Where(s => s.Direction == SynchronizationDirection.Outgoing)
                                   .Timeout(TimeSpan.FromSeconds(20)).Take(2).ToArray().
                                   ToTask();

            var report = await sourceClient.Synchronization.StartAsync("test.bin", destinationClient);

            Assert.Null(report.Exception);

            var synchronizationUpdates = await notificationTask;

            Assert.Equal(SynchronizationAction.Start, synchronizationUpdates[0].Action);
            Assert.Equal(FileHeader.Canonize("test.bin"), synchronizationUpdates[0].FileName);
            Assert.Equal(SynchronizationType.ContentUpdate, synchronizationUpdates[0].Type);
            Assert.Equal(SynchronizationAction.Finish, synchronizationUpdates[1].Action);
            Assert.Equal(FileHeader.Canonize("test.bin"), synchronizationUpdates[1].FileName);
            Assert.Equal(SynchronizationType.ContentUpdate, synchronizationUpdates[1].Type);

            // metadata update
            await sourceClient.UpdateMetadataAsync("test.bin", new RavenJObject { { "key", "value" } });

            notificationTask = sourceStore.Changes().ForSynchronization()
                               .Where(s => s.Direction == SynchronizationDirection.Outgoing)
                               .Timeout(TimeSpan.FromSeconds(20))
                               .Take(2).ToArray()
                               .ToTask();

            report = await sourceClient.Synchronization.StartAsync("test.bin", destinationClient);

            Assert.Null(report.Exception);

            synchronizationUpdates = await notificationTask;

            Assert.Equal(SynchronizationAction.Start, synchronizationUpdates[0].Action);
            Assert.Equal(FileHeader.Canonize("test.bin"), synchronizationUpdates[0].FileName);
            Assert.Equal(SynchronizationType.MetadataUpdate, synchronizationUpdates[0].Type);
            Assert.Equal(SynchronizationAction.Finish, synchronizationUpdates[1].Action);
            Assert.Equal(FileHeader.Canonize("test.bin"), synchronizationUpdates[1].FileName);
            Assert.Equal(SynchronizationType.MetadataUpdate, synchronizationUpdates[1].Type);

            // rename update
            await sourceClient.RenameAsync("test.bin", "rename.bin");

            notificationTask = sourceStore.Changes().ForSynchronization()
                               .Where(s => s.Direction == SynchronizationDirection.Outgoing)
                               .Timeout(TimeSpan.FromSeconds(20))
                               .Take(2).ToArray()
                               .ToTask();

            report = await sourceClient.Synchronization.StartAsync("test.bin", destinationClient);

            Assert.Null(report.Exception);

            synchronizationUpdates = await notificationTask;

            Assert.Equal(SynchronizationAction.Start, synchronizationUpdates[0].Action);
            Assert.Equal(FileHeader.Canonize("test.bin"), synchronizationUpdates[0].FileName);
            Assert.Equal(SynchronizationType.Rename, synchronizationUpdates[0].Type);
            Assert.Equal(SynchronizationAction.Finish, synchronizationUpdates[1].Action);
            Assert.Equal(FileHeader.Canonize("test.bin"), synchronizationUpdates[1].FileName);
            Assert.Equal(SynchronizationType.Rename, synchronizationUpdates[1].Type);

            // delete update
            await sourceClient.DeleteAsync("rename.bin");

            notificationTask = sourceStore.Changes().ForSynchronization()
                               .Where(s => s.Direction == SynchronizationDirection.Outgoing)
                               .Timeout(TimeSpan.FromSeconds(20))
                               .Take(2).ToArray()
                               .ToTask();

            report = await sourceClient.Synchronization.StartAsync("rename.bin", destinationClient);

            Assert.Null(report.Exception);

            synchronizationUpdates = await notificationTask;

            Assert.Equal(SynchronizationAction.Start, synchronizationUpdates[0].Action);
            Assert.Equal(FileHeader.Canonize("rename.bin"), synchronizationUpdates[0].FileName);
            Assert.Equal(SynchronizationType.Delete, synchronizationUpdates[0].Type);
            Assert.Equal(SynchronizationAction.Finish, synchronizationUpdates[1].Action);
            Assert.Equal(FileHeader.Canonize("rename.bin"), synchronizationUpdates[1].FileName);
            Assert.Equal(SynchronizationType.Delete, synchronizationUpdates[1].Type);
        }
Example #17
0
        public bool Read(string filename)
        {
            //try
            {
                m_Symbols   = new List <SymbolEntry>();
                m_Filenames = new List <FilenameSection>();
                byte[] data = System.IO.File.ReadAllBytes(filename);

                if (data.Length > 0)
                {
                    GCHandle pinnedData = GCHandle.Alloc(data, GCHandleType.Pinned);
                    IntPtr   stream     = pinnedData.AddrOfPinnedObject();

                    FilenameHeader  filenameHeader  = new FilenameHeader();
                    ChunkHeader     chunkHeader     = new ChunkHeader();
                    FilenameSection filenameSection = new FilenameSection();
                    AddressEntry    addressEntry    = new AddressEntry();
                    SymbolChunk     symbolChunk     = new SymbolChunk();
                    SymbolEntry     symbolEntry     = new SymbolEntry();
                    string          readString;

                    int bytesRead = 0;

                    // Symbol lines are 1-based, text editor lines are 0-based
                    int currentLine = 1;

                    //Read file header
                    FileHeader fileHeader = new FileHeader();
                    bytesRead += Serialise(ref stream, out fileHeader);

                    //Iterate over chunks
                    while (bytesRead < data.Length)
                    {
                        //Read chunk header
                        bytesRead += Serialise(ref stream, out chunkHeader);

                        //What is it?
                        switch (chunkHeader.chunkId)
                        {
                        case ChunkId.Filename:
                        {
                            //Read filename header
                            bytesRead            += Serialise(ref stream, out filenameHeader);
                            filenameHeader.length = Endian.Swap(filenameHeader.length);

                            //Read string
                            bytesRead += Serialise(ref stream, filenameHeader.length, out readString);

                            if (filenameHeader.flags == 0x1)
                            {
                                //This is the filename passed for assembly
                                m_AssembledFile = readString;
                            }
                            else
                            {
                                //If filename already exists, continue adding data to it
                                int sectionIdx = m_Filenames.FindIndex(element => element.filename == readString);
                                if (sectionIdx >= 0)
                                {
                                    //Continue
                                    filenameSection = m_Filenames[sectionIdx];

                                    //Fetch line counter
                                    currentLine = filenameSection.addresses[filenameSection.addresses.Count - 1].lineTo;
                                }
                                else
                                {
                                    //This is the first address in a filename chunk
                                    filenameSection           = new FilenameSection();
                                    filenameSection.addresses = new List <AddressEntry>();
                                    filenameSection.filename  = readString;

                                    //Reset line counter
                                    currentLine = 1;
                                }

                                //Chunk payload contains address
                                addressEntry.address  = chunkHeader.payload;
                                addressEntry.lineFrom = currentLine - 1;
                                addressEntry.lineTo   = filenameHeader.firstLine - 1;
                                currentLine           = filenameHeader.firstLine;
                                filenameSection.addresses.Add(addressEntry);

                                //Next
                                currentLine++;

                                //Add to filename list
                                m_Filenames.Add(filenameSection);
                            }

                            break;
                        }

                        case ChunkId.Address:
                        {
                            //Chunk payload contains address for a single line
                            addressEntry.address = chunkHeader.payload;

                            //Set line range
                            addressEntry.lineFrom = currentLine - 1;
                            addressEntry.lineTo   = currentLine - 1;

                            //Next
                            currentLine++;

                            //Add
                            filenameSection.addresses.Add(addressEntry);

                            break;
                        }

                        case ChunkId.AddressWithCount:
                        {
                            //Chunk payload contains address for a rage of lines
                            addressEntry.address = chunkHeader.payload;

                            //Read line count
                            byte lineCount = 0;
                            bytesRead += Serialise(ref stream, out lineCount);

                            //Set line range
                            addressEntry.lineFrom = currentLine - 1;
                            addressEntry.lineTo   = currentLine + (lineCount - 1) - 1;

                            //Next
                            currentLine += lineCount;

                            //Add
                            filenameSection.addresses.Add(addressEntry);

                            break;
                        }

                        case ChunkId.Symbol:
                        {
                            //Read symbol string length
                            byte stringLength = 0;
                            bytesRead += Serialise(ref stream, out stringLength);

                            //Read string
                            bytesRead += Serialise(ref stream, stringLength, out symbolEntry.name);

                            //Payload contains address
                            symbolEntry.address = chunkHeader.payload;

                            m_Symbols.Add(symbolEntry);

                            break;
                        }

                        case ChunkId.EndOfSection:
                            //Payload contains section size
                            break;

                        default:
                            short mysteryWord = 0;
                            bytesRead += Serialise(ref stream, out mysteryWord);
                            break;
                        }
                    }

                    pinnedData.Free();

                    //Build address to file/line map
                    m_Addr2FileLine = new Dictionary <uint, Tuple <string, int, int> >();

                    foreach (FilenameSection section in m_Filenames)
                    {
                        foreach (AddressEntry address in section.addresses)
                        {
                            if (!m_Addr2FileLine.ContainsKey(address.address))
                            {
                                m_Addr2FileLine[address.address] = new Tuple <string, int, int>(section.filename, address.lineFrom, address.lineTo);
                            }
                        }
                    }

                    return(true);
                }
            }
            //catch (Exception e)
            //{
            //    Console.WriteLine(e.Message);
            //}

            return(false);
        }
Example #18
0
        bool TryReadHeader(Stream stream, ref FileHeader fhdr, ref long dataStartOffset)
        {
            int numTracks = 82;

            stream.Seek(0, SeekOrigin.Begin);

            if (stream.Length < 16 + (2 * 82))
            {
                return(false);
            }

            FileHeader fheader;

            /* assume it's a regular HD-Copy file without the disk name */
            dataStartOffset         = 2 + 2 * numTracks;
            fheader.lastCylinder    = (byte)stream.ReadByte();
            fheader.sectorsPerTrack = (byte)stream.ReadByte();
            if (fheader.lastCylinder == 0xff && fheader.sectorsPerTrack == 0x18)
            {
                /* This is an "extended" HD-Copy file with filename information and 84 tracks */
                stream.Seek(0x0e, SeekOrigin.Begin);
                fheader.lastCylinder    = (byte)stream.ReadByte();
                fheader.sectorsPerTrack = (byte)stream.ReadByte();
                numTracks       = 84;
                dataStartOffset = 16 + 2 * numTracks;
            }
            fheader.trackMap = new byte[2 * numTracks];
            stream.Read(fheader.trackMap, 0, 2 * numTracks);

            /* Some sanity checks on the values we just read.
             * We know the image is from a DOS floppy disk, so assume
             * some sane cylinder and sectors-per-track count.
             */
            if (fheader.sectorsPerTrack < 8 ||
                fheader.sectorsPerTrack > 40)
            {
                return(false);
            }

            if (fheader.lastCylinder < 37 ||
                fheader.lastCylinder >= 84)
            {
                return(false);
            }

            // Validate the trackmap. First two tracks need to be present
            if (fheader.trackMap[0] != 1 ||
                fheader.trackMap[1] != 1)
            {
                return(false);
            }

            // all other tracks must be either present (=1) or absent (=0)
            for (int i = 0; i < 2 * numTracks; i++)
            {
                if (fheader.trackMap[i] > 1)
                {
                    return(false);
                }
            }

            /* return success */
            fhdr = fheader;
            return(true);
        }
Example #19
0
        void LoadDataFromDisk(PCFResourceType loadFlags, bool streamResources, bool assemblyMode)
        {
            Stream file = this.path.GetFileStream(FileMode.Open);

            //Load header first.
            this.fileHeader = LoadHeader(file);

            int bytesRead  = HeaderByteLength;
            int fileLength = (int)file.Length;

            while (bytesRead < fileLength)
            {
                int             chunkLength  = GetNextSegmentLength(file);
                PCFBlockTypes   blockType    = GetNextBlockType(file);
                DataBlockBase   block        = null;
                PCFResourceType resourceType = PCFResourceType.NONE;

                if (blockType == PCFBlockTypes.INDEXBLOCK)
                {
                    block = new IndexBlock();

                    this.blockData.Add(PCFResourceType.INDEX, block);
                }
                else if (blockType == PCFBlockTypes.NODEBLOCK)
                {
                    block = new NodeBlock(loadFlags);

                    this.blockData.Add(PCFResourceType.NODE, block);
                }
                else if (blockType == PCFBlockTypes.RESOURCEBLOCK)
                {
                    //Read 4 bytes to determine resourcetype.
                    file.Read(INT_BUFFER, 0, ChunkByteLength);
                    int rawResourceTypeValue = BitConverter.ToInt32(INT_BUFFER, 0);
                    resourceType = (PCFResourceType)Enum.ToObject(typeof(PCFResourceType), rawResourceTypeValue);

                    //Allows us to mask what blocks to read in from file.
                    if ((resourceType & loadFlags) != 0)
                    {
                        block = new ResourceBlock(resourceType, streamResources);
                        this.blockData.Add(resourceType, block);
                    }
                }
                else
                {
                    Debug.LogError("Unknown block type");
                }

                //Increment file position, make sure to count the chunk size bytes.
                bytesRead += chunkLength + ChunkByteLength;

                if (block != null)
                {
                    if (memStream)
                    {
                        int bytsOffset = ChunkByteLength;
                        if (blockType == PCFBlockTypes.RESOURCEBLOCK)
                        {
                            bytsOffset += ChunkByteLength;
                        }

                        long   fileStreamPos = file.Position;
                        byte[] streamBytes   = new byte[chunkLength - bytsOffset];
                        file.Read(streamBytes, 0, chunkLength - bytsOffset);

                        Stream stream = new MemoryStream(streamBytes);

                        block.SetBytes(stream, chunkLength, fileStreamPos, assemblyMode);
                    }
                    else
                    {
                        //Will internally read from file and increment its position.
                        block.SetBytes(file, chunkLength, 0, assemblyMode);
                    }
                }
                else
                {
                    //Manually seek to next chunk incase SetBytes was never called.
                    file.Seek(bytesRead, SeekOrigin.Begin);
                }
            }

            if (file != null)
            {
                //Stop reading pcf file.
                file.Close();
            }
        }
Example #20
0
        void SaveToDisk()
        {
            //Open file for reading.
            FileStream file   = new FileStream(this.path.FullName, FileMode.Create, FileAccess.Write);
            FileHeader header = GetHeaderPrototype();

            if (this.indexBlock == null)
            {
                Debug.LogError("No Index block specified!");
                return;
            }

            int indexBlockSize = (this.blockData.Count * IndexBlock.BYTES_PER_ELEMENT) + ChunkByteLength + BlockTypeLength;

            //Calculate number of bytes to offset from start, make room for header and index block.
            int fileOffset = indexBlockSize + header.GetLength();

            file.Seek((long)fileOffset, SeekOrigin.Begin);

            foreach (KeyValuePair <PCFResourceType, DataBlockBase> pair in this.blockData)
            {
                //Add index for each block.
                IndexElement indexElement = new IndexElement(pair.Key, fileOffset);
                this.indexBlock.AddIndex(indexElement);

                //All byte data for a block.
                byte[] blockData = pair.Value.GetBytes();

                //Make sure we dont end up with empty blocks.
                if (blockData == null || blockData.Length == 0)
                {
                    blockData = nullElement;
                }

                //Cannot use GetLength() here because we need to factor in all bytes including chunklength bytes.
                int blockLength = blockData.Length;

                file.Write(blockData, 0, blockLength);

                //Move file position to next block.
                fileOffset += blockLength;
            }

            header.SetFileLength(fileOffset - header.GetLength());

            //Seek back to file header (aka index 0) and write header + index block to disk.
            file.Seek(0, SeekOrigin.Begin);

            byte[] headerBytes = header.GetBytes();
            byte[] indexBytes  = this.indexBlock.GetBytes();

            file.Write(headerBytes, 0, headerBytes.Length);
            file.Write(indexBytes, 0, indexBytes.Length);

            if (file != null)
            {
                //Stop reading pcf file.
                file.Dispose();
                file.Close();
            }
        }
        public async Task <HttpResponseMessage> MultipartProceed(string fileSystemName)
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var fileName          = Request.Headers.GetValues(SyncingMultipartConstants.FileName).FirstOrDefault();
            var canonicalFilename = FileHeader.Canonize(fileName);

            var tempFileName = RavenFileNameHelper.DownloadingFileName(canonicalFilename);

            var sourceServerInfo = InnerHeaders.Value <ServerInfo>(SyncingMultipartConstants.SourceServerInfo);
            var sourceFileETag   = Guid.Parse(InnerHeaders.GetValues(Constants.MetadataEtagField).First().Trim('\"'));

            var report = new SynchronizationReport(canonicalFilename, sourceFileETag, SynchronizationType.ContentUpdate);

            Log.Debug("Starting to process multipart synchronization request of a file '{0}' with ETag {1} from {2}", fileName, sourceFileETag, sourceServerInfo);

            StorageStream localFile          = null;
            var           isNewFile          = false;
            var           isConflictResolved = false;

            try
            {
                Storage.Batch(accessor =>
                {
                    AssertFileIsNotBeingSynced(canonicalFilename, accessor);
                    FileLockManager.LockByCreatingSyncConfiguration(canonicalFilename, sourceServerInfo, accessor);
                });

                SynchronizationTask.IncomingSynchronizationStarted(canonicalFilename, sourceServerInfo, sourceFileETag, SynchronizationType.ContentUpdate);

                PublishSynchronizationNotification(fileSystemName, canonicalFilename, sourceServerInfo, report.Type, SynchronizationAction.Start);

                Storage.Batch(accessor => StartupProceed(canonicalFilename, accessor));

                RavenJObject sourceMetadata = GetFilteredMetadataFromHeaders(InnerHeaders);

                var localMetadata = GetLocalMetadata(canonicalFilename);
                if (localMetadata != null)
                {
                    AssertConflictDetection(canonicalFilename, localMetadata, sourceMetadata, sourceServerInfo, out isConflictResolved);
                    localFile = StorageStream.Reading(Storage, canonicalFilename);
                }
                else
                {
                    isNewFile = true;
                }

                Historian.UpdateLastModified(sourceMetadata);

                var synchronizingFile = SynchronizingFileStream.CreatingOrOpeningAndWriting(Storage, Search, StorageOperationsTask, tempFileName, sourceMetadata);

                var provider = new MultipartSyncStreamProvider(synchronizingFile, localFile);

                Log.Debug("Starting to process/read multipart content of a file '{0}'", fileName);

                await Request.Content.ReadAsMultipartAsync(provider);

                Log.Debug("Multipart content of a file '{0}' was processed/read", fileName);

                report.BytesCopied     = provider.BytesCopied;
                report.BytesTransfered = provider.BytesTransfered;
                report.NeedListLength  = provider.NumberOfFileParts;

                synchronizingFile.PreventUploadComplete = false;
                synchronizingFile.Flush();
                synchronizingFile.Dispose();
                sourceMetadata["Content-MD5"] = synchronizingFile.FileHash;

                Storage.Batch(accessor => accessor.UpdateFileMetadata(tempFileName, sourceMetadata));

                Storage.Batch(accessor =>
                {
                    StorageOperationsTask.IndicateFileToDelete(canonicalFilename);
                    accessor.RenameFile(tempFileName, canonicalFilename);

                    Search.Delete(tempFileName);
                    Search.Index(canonicalFilename, sourceMetadata);
                });

                if (isNewFile)
                {
                    Log.Debug("Temporary downloading file '{0}' was renamed to '{1}'. Indexes were updated.", tempFileName, fileName);
                }
                else
                {
                    Log.Debug("Old file '{0}' was deleted. Indexes were updated.", fileName);
                }

                if (isConflictResolved)
                {
                    ConflictArtifactManager.Delete(canonicalFilename);
                }
            }
            catch (Exception ex)
            {
                if (ShouldAddExceptionToReport(ex))
                {
                    report.Exception = ex;
                }
            }
            finally
            {
                if (localFile != null)
                {
                    localFile.Dispose();
                }
            }

            if (report.Exception == null)
            {
                Log.Debug(
                    "File '{0}' was synchronized successfully from {1}. {2} bytes were transfered and {3} bytes copied. Need list length was {4}",
                    fileName, sourceServerInfo, report.BytesTransfered, report.BytesCopied, report.NeedListLength);
            }
            else
            {
                Log.WarnException(
                    string.Format("Error has occurred during synchronization of a file '{0}' from {1}", fileName, sourceServerInfo),
                    report.Exception);
            }

            FinishSynchronization(canonicalFilename, report, sourceServerInfo, sourceFileETag);

            PublishFileNotification(fileName, isNewFile ? FileChangeAction.Add : FileChangeAction.Update);
            PublishSynchronizationNotification(fileSystemName, fileName, sourceServerInfo, report.Type, SynchronizationAction.Finish);

            if (isConflictResolved)
            {
                Publisher.Publish(new ConflictNotification
                {
                    FileName = fileName,
                    Status   = ConflictStatus.Resolved
                });
            }

            return(GetMessageWithObject(report));
        }
Example #22
0
 internal NonSeekableStreamFilePart(MarkHeader mh, FileHeader fh)
     : base(mh, fh)
 {
 }
Example #23
0
        /// <summary>
        ///  Read ZIP file directory
        /// </summary>
        /// <exception cref="UZipDotNet.Exception">
        /// ZIP file is too short
        /// or
        /// Invalid ZIP file (No central directory)
        /// or
        /// No support for multi-disk ZIP file
        /// or
        /// Central directory is empty or in error
        /// or
        /// Central directory empty
        /// or
        /// File directory signature error
        /// or
        /// Directory name must have a slash
        /// </exception>
        private void ReadZipFileDirectory()
        {
            //	End of central directory record:
            //	Pos		Len
            //	0		4		End of central directory signature = 0x06054b50
            //	4		2		Number of this disk
            //	6		2		Disk where central directory starts
            //	8		2		Number of central directory records on this disk
            //	10		2		Total number of central directory records
            //	12		4		Size of central directory (bytes)
            //	16		4		Offset of start of central directory, relative to start of archive
            //	20		2		ZIP file comment length (n)
            //	22		n		ZIP file comment
            //
            //	Central directory file header
            //
            //	Pos		Len
            //	0		4		Central directory file header signature = 0x02014b50
            //	4		2		Version made by
            //	6		2		Version needed to extract (minimum)
            //	8		2		General purpose bit flag
            //	10		2		Compression method
            //	12		2		File last modification time
            //	14		2		File last modification date
            //	16		4		CRC-32
            //	20		4		Compressed size
            //	24		4		Uncompressed size
            //	28		2		File name length (n)
            //	30		2		Extra field length (m)
            //	32		2		File comment length (k)
            //	34		2		Disk number where file starts
            //	36		2		Internal file attributes
            //	38		4		External file attributes
            //	42		4		Offset of local file header
            //	46		n		File name
            //	46+n	m		Extra field
            //	46+n+m	k		File comment

            // file length
            long fileLen = _writeStream.Length;
            if (fileLen < 98) throw new Exception("ZIP file is too short");

            // read last 512 byte block at the end of the file
            if (fileLen > 512) _writeStream.Position = fileLen - 512;
            byte[] dirSig = _writeFileReader.ReadBytes(512);

            // look for signature
            int ptr;
            for (ptr = dirSig.Length - 20; ptr >= 0 && BitConverter.ToInt32(dirSig, ptr) != 0x06054b50; ptr--)
            {
            }
            if (ptr < 0) throw new Exception("Invalid ZIP file (No central directory)");
            ptr += 4;

            // number of this disk should be zero
            short diskNo = BitConverter.ToInt16(dirSig, ptr);
            ptr += 2;
            if (diskNo != 0) throw new Exception("No support for multi-disk ZIP file");

            // disk where central directory starts should be zero
            short dirDiskNo = BitConverter.ToInt16(dirSig, ptr);
            ptr += 2;
            if (dirDiskNo != 0) throw new Exception("No support for multi-disk ZIP file");

            // Number of central directory records on this disk
            short dirEntries = BitConverter.ToInt16(dirSig, ptr);
            ptr += 2;

            // Total number of central directory records
            short dirEntriesTotal = BitConverter.ToInt16(dirSig, ptr);
            ptr += 2;
            if (dirEntriesTotal == 0 || dirEntriesTotal != dirEntries) throw new Exception("Central directory is empty or in error");

            // Size of central directory (bytes)
            int dirSize = BitConverter.ToInt32(dirSig, ptr);
            ptr += 4;
            if (dirSize == 0) throw new Exception("Central directory empty");

            // Offset of start of central directory, relative to start of archive
            _zipDirPosition = BitConverter.ToInt32(dirSig, ptr);
            if (_zipDirPosition == 0) throw new Exception("Central directory empty");

            // create new zip directory array
            ZipDir = new List<FileHeader>(dirEntriesTotal);

            // position file to central directory
            _writeStream.Position = _zipDirPosition;

            // read central directory
            while (dirEntriesTotal-- > 0)
            {
                // file header
                var fh = new FileHeader();

                // Central directory file header signature = 0x02014b50
                int fileDirSig = _writeFileReader.ReadInt32();
                if (fileDirSig != 0x02014b50) throw new Exception("File directory signature error");

                // ReSharper disable once UnusedVariable : Version made by (ignored)
                //int versionMadeBy = _writeFileReader.ReadInt16();

                // Low byte is version needed to extract (the low byte should be 20 for version 2.0).
                // High byte is a computer system code that define the extrenal file attribute.
                // If high byte is zero it is DOS compatible.
                fh.Version = _writeFileReader.ReadUInt16();

                //	General purpose bit flag (bit 0 encripted)
                fh.BitFlags = _writeFileReader.ReadUInt16();

                //	Compression method must be deflate or no compression
                fh.CompMethod = _writeFileReader.ReadUInt16();

                //	File last modification time
                fh.FileTime = _writeFileReader.ReadUInt16();

                //	File last modification date
                fh.FileDate = _writeFileReader.ReadUInt16();

                // CRC-32
                fh.FileCRC32 = _writeFileReader.ReadUInt32();

                // Compressed size
                fh.CompSize = _writeFileReader.ReadUInt32();

                // Uncompressed size
                fh.FileSize = _writeFileReader.ReadUInt32();

                // File name length
                int fileNameLen = _writeFileReader.ReadInt16();

                // Extra field length
                int extraFieldLen = _writeFileReader.ReadInt16();

                // File comment length
                int commentLen = _writeFileReader.ReadInt16();

                // Disk number where file starts
                int fileDiskNo = _writeFileReader.ReadInt16();
                if (fileDiskNo != 0) throw new Exception("No support for multi-disk ZIP file");

                // ReSharper disable once UnusedVariable : internal file attributes (ignored)
                //int fileIntAttr = _writeFileReader.ReadInt16();

                // external file attributes
                fh.FileAttr = (FileAttributes)_writeFileReader.ReadUInt32();

                // if file system is not FAT or equivalent, erase the attributes
                if ((fh.Version & 0xff00) != 0) fh.FileAttr = 0;

                // file position
                fh.FilePos = _writeFileReader.ReadUInt32();

                // file name
                // read all the bytes of the file name into a byte array
                // extract a string from the byte array using DOS (IBM OEM code page 437)
                // replace the unix forward slash with microsoft back slash
                fh.FileName = Utils.DecodeFilename(_writeFileReader.ReadBytes(fileNameLen));

                // if file attribute is a directory make sure we have terminating backslash
                //			if((FH.FileAttr & FileAttributes.Directory) != 0 && !FH.FileName.EndsWith("\\")) FH.FileName += "\\";

                // find if file name contains a path
                fh.Path = fh.FileName.Contains("\\");

                // if we have a directory, we must have a terminating slash
                if ((fh.FileAttr & FileAttributes.Directory) != 0 && !fh.Path) throw new Exception("Directory name must have a slash");

                // Skip Extra field and File comment
                _writeStream.Position += extraFieldLen + commentLen;

                // add file header to zip directory
                ZipDir.Add(fh);
            }

            // position file to central directory
            _writeStream.Position = _zipDirPosition;

            // remove central directory from this file
            _writeStream.SetLength(_zipDirPosition);

            // sort array
            ZipDir.Sort();
        }
Example #24
0
 private static void ShapeFileHeaderWriter(ref System.IO.BinaryWriter bWriter, FileHeader SHPFileHeader)
 {
     bWriter.Write(SHPFileHeader.FileCode);
     bWriter.Write(SHPFileHeader.u1);
     bWriter.Write(SHPFileHeader.u2);
     bWriter.Write(SHPFileHeader.u3);
     bWriter.Write(SHPFileHeader.u4);
     bWriter.Write(SHPFileHeader.u5);
     bWriter.Write(SHPFileHeader.FileLength);
     bWriter.Write(SHPFileHeader.Version);
     bWriter.Write(SHPFileHeader.ShapeType);
     bWriter.Write(SHPFileHeader.BoundingBoxXMin);
     bWriter.Write(SHPFileHeader.BoundingBoxYMin);
     bWriter.Write(SHPFileHeader.BoundingBoxXMax);
     bWriter.Write(SHPFileHeader.BoundingBoxYMax);
     bWriter.Write(SHPFileHeader.BoundingBoxZMin);
     bWriter.Write(SHPFileHeader.BoundingBoxZMax);
     bWriter.Write(SHPFileHeader.BoundingBoxMMin);
     bWriter.Write(SHPFileHeader.BoundingBoxMMax);
 }
Example #25
0
        public HttpResponseMessage Get([FromUri] string[] fileNames)
        {
            var list = new List <FileHeader>();

            var startsWith = GetQueryStringValue("startsWith");

            if (string.IsNullOrEmpty(startsWith) == false)
            {
                var matches = GetQueryStringValue("matches");

                var endsWithSlash = startsWith.EndsWith("/") || startsWith.EndsWith("\\");
                startsWith = FileHeader.Canonize(startsWith);
                if (endsWithSlash)
                {
                    startsWith += "/";
                }

                Storage.Batch(accessor =>
                {
                    var actualStart             = 0;
                    var filesToSkip             = Paging.Start;
                    int fileCount, matchedFiles = 0, addedFiles = 0;

                    do
                    {
                        fileCount = 0;

                        foreach (var file in accessor.GetFilesStartingWith(startsWith, actualStart, Paging.PageSize))
                        {
                            fileCount++;

                            var keyTest = file.FullPath.Substring(startsWith.Length);

                            if (WildcardMatcher.Matches(matches, keyTest) == false)
                            {
                                continue;
                            }

                            if (FileSystem.ReadTriggers.CanReadFile(FileHeader.Canonize(file.FullPath), file.Metadata, ReadOperation.Load) == false)
                            {
                                continue;
                            }

                            if (file.Metadata.Keys.Contains(SynchronizationConstants.RavenDeleteMarker))
                            {
                                continue;
                            }

                            matchedFiles++;

                            if (matchedFiles <= filesToSkip)
                            {
                                continue;
                            }

                            list.Add(file);
                            addedFiles++;
                        }

                        actualStart += Paging.PageSize;
                    }while (fileCount > 0 && addedFiles < Paging.PageSize && actualStart > 0 && actualStart < int.MaxValue);
                });
            }
            else
            {
                if (fileNames != null && fileNames.Length > 0)
                {
                    Storage.Batch(accessor =>
                    {
                        foreach (var path in fileNames.Where(x => x != null).Select(FileHeader.Canonize))
                        {
                            var file = accessor.ReadFile(path);

                            if (file == null || file.Metadata.Keys.Contains(SynchronizationConstants.RavenDeleteMarker))
                            {
                                list.Add(null);
                                continue;
                            }

                            list.Add(file);
                        }
                    });
                }
                else
                {
                    int results;
                    var keys = Search.Query(null, null, Paging.Start, Paging.PageSize, out results);

                    Storage.Batch(accessor => list.AddRange(keys.Select(accessor.ReadFile).Where(x => x != null)));
                }
            }

            return(GetMessageWithObject(list)
                   .WithNoCache());
        }
Example #26
0
        //public static void CreateBlockFile(string sBlockId, string sBlockName, Hashtable Area_Shp, string DestFilePath, string DestFileName, ShapeType ShapeType)
        //{
        //    Hashtable Shp_Area = new Hashtable();
        //    //- Get Encoding used in Current CultureInfo of OS.
        //    System.Text.Encoding FileEncoding = System.Text.Encoding.Default;
        //    //*** Build ShapeFile - AreaId Collection with unique Shape and comma delimited Areaids
        //    IDictionaryEnumerator dicEnumerator = Area_Shp.GetEnumerator();
        //    while (dicEnumerator.MoveNext())
        //    {
        //        if (Shp_Area.ContainsKey(dicEnumerator.Value))
        //        {
        //            Shp_Area[dicEnumerator.Value] = Shp_Area[dicEnumerator.Value].ToString() + "," + dicEnumerator.Key.ToString();
        //        }
        //        else
        //        {
        //            Shp_Area.Add(dicEnumerator.Value, dicEnumerator.Key);
        //        }
        //    }
        //    FileHeader SHPFileHeader = new FileHeader();
        //    FileStream oShpStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shp", FileMode.Create);
        //    BinaryWriter oShpBinaryWriter = new BinaryWriter(oShpStream);
        //    FileStream oShxStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shx", FileMode.Create);
        //    BinaryWriter oShxBinaryWriter = new BinaryWriter(oShxStream);
        //    FileStream oDbfStream = new FileStream(DestFilePath + "\\" + DestFileName + ".dbf", FileMode.Create);
        //    BinaryWriter oDbfBinaryWriter = new BinaryWriter(oDbfStream, FileEncoding);
        //    SHPFileHeader.FileCode = BigToLittleEndian(9994);
        //    SHPFileHeader.Version = 1000;
        //    SHPFileHeader.ShapeType = (int)ShapeType;
        //    ShapeFileHeaderWriter(ref oShpBinaryWriter, SHPFileHeader);
        //    //*** Write .Shp File Header
        //    ShapeFileHeaderWriter(ref oShxBinaryWriter, SHPFileHeader);
        //    //*** Write .Shx File Header
        //    DbaseFileHeaderWriter(ref oDbfBinaryWriter, 1, FileEncoding);
        //    //Area_Shp.Count '*** Write .dbf File Header
        //    int i;
        //    int j;
        //    string[] AreaId;
        //    string SrcFile;
        //    string SrcFilePath;
        //    string SrcFileName;
        //    ShapeInfo ShpInfo;
        //    Shape _Shape = new Shape();
        //    Shape _Block = new Shape();
        //    RectangleF _Extent = new RectangleF();
        //    PointF[] Pts;
        //    int Offset = 50;
        //    int RecordNumber;
        //    int ContentLength;
        //    int Points;
        //    int BlockPointCount = 0;
        //    int[] BlockPartOffset = null;
        //    dicEnumerator = Shp_Area.GetEnumerator();
        //    while (dicEnumerator.MoveNext())
        //    {
        //        SrcFile = dicEnumerator.Key.ToString();
        //        AreaId = dicEnumerator.Value.ToString().Split(","[0]);
        //        if (File.Exists(SrcFile))
        //        {
        //            SrcFilePath = Path.GetDirectoryName(SrcFile);
        //            SrcFileName = Path.GetFileNameWithoutExtension(SrcFile);
        //            ShpInfo = GetShapeInfo(SrcFilePath, SrcFileName);
        //            switch (ShapeType)
        //            {
        //                case ShapeType.Point:
        //                    break;
        //                case ShapeType.Polygon:
        //                case ShapeType.PolyLine:
        //                    for (j = 0; j <= AreaId.Length - 1; j++)
        //                    {
        //                        if (ShpInfo.Records.ContainsKey(AreaId[j]))
        //                        {
        //                            _Shape = (Shape)ShpInfo.Records[AreaId[j]];
        //                            if (_Extent.IsEmpty)
        //                            {
        //                                _Extent = _Shape.Extent;
        //                            }
        //                            else
        //                            {
        //                                _Extent = RectangleF.Union(_Extent, _Shape.Extent);
        //                            }
        //                            int iPrevPartsCount = _Block.Parts.Count;
        //                            //ReDim Preserve BlockPartOffset(iPrevPartsCount + _Shape.Parts.Count - 1)
        //                            //int[] YourArray;
        //                            //...
        //                            //int[] temp = new int[i + 1];
        //                            //if (YourArray != null)
        //                            //    Array.Copy(YourArray, temp, Math.Min(YourArray.Length, temp.Length));
        //                            //YourArray = temp;
        //                            int[] temp = new int[iPrevPartsCount + _Shape.Parts.Count];
        //                            if (BlockPartOffset != null)
        //                                Array.Copy(BlockPartOffset, temp, Math.Min(BlockPartOffset.Length, temp.Length));
        //                            BlockPartOffset = temp;
        //                            Points = 0;
        //                            for (i = 0; i <= _Shape.Parts.Count - 1; i++)
        //                            {
        //                                Pts = (PointF[])_Shape.Parts[i];
        //                                _Block.Parts.Add(Pts);
        //                                BlockPartOffset[iPrevPartsCount + i] = Pts.Length;
        //                                Points += Pts.Length;
        //                                BlockPointCount += Pts.Length;
        //                            }
        //                        }
        //                    }
        //                    break;
        //            }
        //        }
        //    }
        //    _Block.Extent = _Extent;
        //    RecordNumber = 1;
        //    //*** All Shape will be added as parts of a single block
        //    ContentLength = ((11 + _Block.Parts.Count) * 2) + (BlockPointCount * 8);
        //    PolygonRecordWriter(ref oShpBinaryWriter, RecordNumber, ContentLength, BlockPointCount, BlockPartOffset, _Block, SHPFileHeader.ShapeType);
        //    IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength);
        //    //*** Write SHX Record
        //    DbaseRecordWriter(ref oDbfBinaryWriter, sBlockId, sBlockName);
        //    //*** Write DBF Record
        //    if (ShapeType == ShapeType.Polygon | ShapeType == ShapeType.PolyLine)
        //    {
        //        SHPFileHeader.BoundingBoxXMin = _Extent.X;
        //        SHPFileHeader.BoundingBoxYMin = _Extent.Y;
        //        SHPFileHeader.BoundingBoxXMax = _Extent.X + _Extent.Width;
        //        SHPFileHeader.BoundingBoxYMax = _Extent.Y + _Extent.Height;
        //    }
        //    //*** Reset FileLength and Extent in Shp File Header
        //    oShpStream.Position = 24;
        //    oShpBinaryWriter.Write(BigToLittleEndian((int)oShpStream.Length / 2));
        //    oShpStream.Position = 36;
        //    oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin);
        //    oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin);
        //    oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax);
        //    oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax);
        //    //*** Reset FileLength and Extent in Shx File Header
        //    oShxStream.Position = 24;
        //    oShxBinaryWriter.Write(BigToLittleEndian((int)oShxStream.Length / 2));
        //    oShxStream.Position = 36;
        //    oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin);
        //    oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin);
        //    oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax);
        //    oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax);
        //    //*** Dispose
        //    oShpBinaryWriter.Close();
        //    oShxBinaryWriter.Close();
        //    oDbfBinaryWriter.Close();
        //    oShpStream.Close();
        //    oShxStream.Close();
        //    oDbfStream.Close();
        //    oShpBinaryWriter = null;
        //    oShxBinaryWriter = null;
        //    oDbfBinaryWriter = null;
        //    // _Extent = null;
        //    ShpInfo = null;
        //}
        ////*** Create Shape File for selected polygon from multiple shape file
        //[Obsolete]
        //public static void CreateShapeFile(Hashtable Area_Shp, string DestFilePath, string DestFileName, ShapeType ShapeType)
        //{
        //    Hashtable Shp_Area = new Hashtable();
        //    System.Text.Encoding FileEncoding = System.Text.Encoding.Default;
        //    //*** Build ShapeFile - AreaId Collection with unique Shape and comma delimited Areaids
        //    IDictionaryEnumerator dicEnumerator = Area_Shp.GetEnumerator();
        //    while (dicEnumerator.MoveNext())
        //    {
        //        if (Shp_Area.ContainsKey(dicEnumerator.Value))
        //        {
        //            Shp_Area[dicEnumerator.Value] = Shp_Area[dicEnumerator.Value].ToString() + "," + dicEnumerator.Key.ToString();
        //        }
        //        else
        //        {
        //            Shp_Area.Add(dicEnumerator.Value, dicEnumerator.Key);
        //        }
        //    }
        //    FileHeader SHPFileHeader = new FileHeader();
        //    FileStream oShpStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shp", FileMode.Create);
        //    BinaryWriter oShpBinaryWriter = new BinaryWriter(oShpStream);
        //    FileStream oShxStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shx", FileMode.Create);
        //    BinaryWriter oShxBinaryWriter = new BinaryWriter(oShxStream);
        //    FileStream oDbfStream = new FileStream(DestFilePath + "\\" + DestFileName + ".dbf", FileMode.Create);
        //    BinaryWriter oDbfBinaryWriter = new BinaryWriter(oDbfStream, FileEncoding);
        //    SHPFileHeader.FileCode = BigToLittleEndian(9994);
        //    SHPFileHeader.Version = 1000;
        //    SHPFileHeader.ShapeType = (int)ShapeType;
        //    ShapeFileHeaderWriter(ref oShpBinaryWriter, SHPFileHeader);
        //    //*** Write .Shp File Header
        //    ShapeFileHeaderWriter(ref oShxBinaryWriter, SHPFileHeader);
        //    //*** Write .Shx File Header
        //    DbaseFileHeaderWriter(ref oDbfBinaryWriter, Area_Shp.Count, FileEncoding);
        //    //*** Write .dbf File Header
        //    int i;
        //    int j;
        //    string[] AreaId;
        //    string SrcFile;
        //    string SrcFilePath;
        //    string SrcFileName;
        //    ShapeInfo ShpInfo;
        //    Shape _Shape = new Shape();
        //    RectangleF _Extent = new RectangleF();
        //    PointF[] Pts;
        //    int Offset = 50;
        //    int RecordNumber = 0;
        //    int ContentLength;
        //    int Points;
        //    dicEnumerator = Shp_Area.GetEnumerator();
        //    while (dicEnumerator.MoveNext())
        //    {
        //        SrcFile = dicEnumerator.Key.ToString();
        //        AreaId = dicEnumerator.Value.ToString().Split(","[0]);
        //        if (File.Exists(SrcFile))
        //        {
        //            SrcFilePath = Path.GetDirectoryName(SrcFile);
        //            SrcFileName = Path.GetFileNameWithoutExtension(SrcFile);
        //            ShpInfo = GetShapeInfo(SrcFilePath, SrcFileName);
        //            switch (ShapeType)
        //            {
        //                case ShapeType.Point:
        //                    PointF Pt;
        //                    ContentLength = 10;
        //                    for (j = 0; j <= AreaId.Length - 1; j++)
        //                    {
        //                        if (ShpInfo.Records.ContainsKey(AreaId[j]))
        //                        {
        //                            _Shape = (Shape)ShpInfo.Records[AreaId[j]];
        //                            Pt = (PointF)_Shape.Parts[0];
        //                            if (RecordNumber == 0)
        //                            {
        //                                SHPFileHeader.BoundingBoxXMin = Pt.X;
        //                                SHPFileHeader.BoundingBoxYMin = Pt.Y;
        //                                SHPFileHeader.BoundingBoxXMax = Pt.X;
        //                                SHPFileHeader.BoundingBoxYMax = Pt.Y;
        //                            }
        //                            else
        //                            {
        //                                SHPFileHeader.BoundingBoxXMin = Math.Min(SHPFileHeader.BoundingBoxXMin, Pt.X);
        //                                SHPFileHeader.BoundingBoxYMin = Math.Min(SHPFileHeader.BoundingBoxYMin, Pt.Y);
        //                                SHPFileHeader.BoundingBoxXMax = Math.Max(SHPFileHeader.BoundingBoxXMax, Pt.X);
        //                                SHPFileHeader.BoundingBoxYMax = Math.Max(SHPFileHeader.BoundingBoxYMax, Pt.Y);
        //                            }
        //                            RecordNumber += 1;
        //                            oShpBinaryWriter.Write(BigToLittleEndian(RecordNumber));
        //                            oShpBinaryWriter.Write(BigToLittleEndian(ContentLength));
        //                            oShpBinaryWriter.Write((int)1);
        //                            //Shape Type
        //                            oShpBinaryWriter.Write((double)Pt.X);
        //                            oShpBinaryWriter.Write((double)Pt.Y);
        //                            IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength);
        //                            //*** Write SHX Record
        //                            Offset += (4 + ContentLength);
        //                            //*** Add 4 for record header '*** Set Offset for next SHX record
        //                            DbaseRecordWriter(ref oDbfBinaryWriter, _Shape.AreaId, _Shape.AreaName);
        //                            //*** Write DBF Record
        //                        }
        //                    }
        //                    // Pt = null;
        //                    break;
        //                case ShapeType.Polygon:
        //                case ShapeType.PolyLine:
        //                    for (j = 0; j <= AreaId.Length - 1; j++)
        //                    {
        //                        if (ShpInfo.Records.ContainsKey(AreaId[j]))
        //                        {
        //                            _Shape = (Shape)ShpInfo.Records[AreaId[j]];
        //                            if (_Extent.IsEmpty)
        //                            {
        //                                _Extent = _Shape.Extent;
        //                            }
        //                            else
        //                            {
        //                                _Extent = RectangleF.Union(_Extent, _Shape.Extent);
        //                            }
        //                            int[] NumPoints = new int[_Shape.Parts.Count];
        //                            Points = 0;
        //                            for (i = 0; i <= _Shape.Parts.Count - 1; i++)
        //                            {
        //                                Pts = (PointF[])_Shape.Parts[i];
        //                                NumPoints[i] = Pts.Length;
        //                                Points += Pts.Length;
        //                            }
        //                            ContentLength = ((11 + _Shape.Parts.Count) * 2) + (Points * 8);
        //                            RecordNumber += 1;
        //                            PolygonRecordWriter(ref oShpBinaryWriter, RecordNumber, ContentLength, Points, NumPoints, _Shape, SHPFileHeader.ShapeType);
        //                            IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength);
        //                            //*** Write SHX Record
        //                            Offset += (4 + ContentLength);
        //                            //*** Add 4 for record header '*** Set Offset for next SHX record
        //                            DbaseRecordWriter(ref oDbfBinaryWriter, _Shape.AreaId, _Shape.AreaName);
        //                            //*** Write DBF Record
        //                        }
        //                    }
        //                    break;
        //            }
        //        }
        //    }
        //    if (ShapeType == ShapeType.Polygon | ShapeType == ShapeType.PolyLine)
        //    {
        //        SHPFileHeader.BoundingBoxXMin = _Extent.X;
        //        SHPFileHeader.BoundingBoxYMin = _Extent.Y;
        //        SHPFileHeader.BoundingBoxXMax = _Extent.X + _Extent.Width;
        //        SHPFileHeader.BoundingBoxYMax = _Extent.Y + _Extent.Height;
        //    }
        //    //*** Reset FileLength and Extent in Shp File Header
        //    oShpStream.Position = 24;
        //    oShpBinaryWriter.Write(BigToLittleEndian((int)oShpStream.Length / 2));
        //    oShpStream.Position = 36;
        //    oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin);
        //    oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin);
        //    oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax);
        //    oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax);
        //    //*** Reset FileLength and Extent in Shx File Header
        //    oShxStream.Position = 24;
        //    oShxBinaryWriter.Write(BigToLittleEndian((int)oShxStream.Length / 2));
        //    oShxStream.Position = 36;
        //    oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin);
        //    oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin);
        //    oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax);
        //    oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax);
        //    //*** Dispose
        //    oShpBinaryWriter.Close();
        //    oShxBinaryWriter.Close();
        //    oDbfBinaryWriter.Close();
        //    oShpStream.Close();
        //    oShxStream.Close();
        //    oDbfStream.Close();
        //    oShpBinaryWriter = null;
        //    oShxBinaryWriter = null;
        //    oDbfBinaryWriter = null;
        //    // _Extent = null;
        //    ShpInfo = null;
        //}
        //[Obsolete]
        //public static void CreateShapeFile(Hashtable Area_Shp, string DestFilePath, string DestFileName)
        //{
        //    ShapeType ShapeType = ShapeType.Polygon;
        //    CreateShapeFile(Area_Shp, DestFilePath, DestFileName, ShapeType);
        //}
        //*** Create Shape File for selected polygon from single shape file - Used for Split process
        public static void CreateShapeFile(string AreaIds, string SrcFilePath, string SrcFileName, string DestFilePath, string DestFileName)
        {
            ShapeInfo ShpInfo = GetShapeInfo(SrcFilePath, SrcFileName);
            string[] AreaId;
            AreaId = AreaIds.Split(","[0]);

            FileHeader SHPFileHeader = new FileHeader();
            FileStream oShpStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shp", FileMode.Create);
            BinaryWriter oShpBinaryWriter = new BinaryWriter(oShpStream);

            FileStream oShxStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shx", FileMode.Create);
            BinaryWriter oShxBinaryWriter = new BinaryWriter(oShxStream);

            FileStream oDbfStream = new FileStream(DestFilePath + "\\" + DestFileName + ".dbf", FileMode.Create);

            //- Get Encoding used in source Shape file (.dbf)
            System.Text.Encoding FileEncoding = ShapeFileReader.GetEncodingInDBF(SrcFilePath + "\\" + SrcFileName + ".dbf");
            BinaryWriter oDbfBinaryWriter = new BinaryWriter(oDbfStream, FileEncoding); //System.Text.Encoding.ASCII

            SHPFileHeader.FileCode = BigToLittleEndian(9994);
            SHPFileHeader.Version = 1000;
            switch (ShpInfo.ShapeType)
            {
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.Point:
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PointCustom:
                    SHPFileHeader.ShapeType = (int)ShapeType.Point;
                    break;
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolyLine:
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolyLineCustom:
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolyLineFeature:
                    SHPFileHeader.ShapeType = (int)ShapeType.PolyLine;
                    break;
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.Polygon:
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolygonBuffer:
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolygonCustom:
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolygonFeature:
                    SHPFileHeader.ShapeType = (int)ShapeType.Polygon;
                    break;
            }
            ShapeFileHeaderWriter(ref oShpBinaryWriter, SHPFileHeader);
            //*** Write .Shp File Header
            ShapeFileHeaderWriter(ref oShxBinaryWriter, SHPFileHeader);
            //*** Write .Shx File Header
            DbaseFileHeaderWriter(ref oDbfBinaryWriter, AreaId.Length, FileEncoding);
            //*** Write .dbf File Header

            int i;
            int j;
            Shape _Shape = new Shape();
            PointF[] Pts;
            int Offset = 50;
            int RecordNumber = 0;
            int ContentLength;
            int Points;

            switch (SHPFileHeader.ShapeType)
            {
                case 1:
                    PointF Pt;
                    ContentLength = 10;
                    for (j = 0; j <= AreaId.Length - 1; j++)
                    {
                        if (ShpInfo.Records.ContainsKey(AreaId[j]))
                        {
                            _Shape = (Shape)ShpInfo.Records[AreaId[j]];
                            Pt = (PointF)_Shape.Parts[0];

                            if (j == 0)
                            {
                                SHPFileHeader.BoundingBoxXMin = Pt.X;
                                SHPFileHeader.BoundingBoxYMin = Pt.Y;
                                SHPFileHeader.BoundingBoxXMax = Pt.X;
                                SHPFileHeader.BoundingBoxYMax = Pt.Y;
                            }
                            else
                            {
                                SHPFileHeader.BoundingBoxXMin = Math.Min(SHPFileHeader.BoundingBoxXMin, Pt.X);
                                SHPFileHeader.BoundingBoxYMin = Math.Min(SHPFileHeader.BoundingBoxYMin, Pt.Y);
                                SHPFileHeader.BoundingBoxXMax = Math.Max(SHPFileHeader.BoundingBoxXMax, Pt.X);
                                SHPFileHeader.BoundingBoxYMax = Math.Max(SHPFileHeader.BoundingBoxYMax, Pt.Y);
                            }

                            RecordNumber += 1;
                            oShpBinaryWriter.Write(BigToLittleEndian(RecordNumber));
                            oShpBinaryWriter.Write(BigToLittleEndian(ContentLength));
                            oShpBinaryWriter.Write((int)1);
                            //Shape Type
                            oShpBinaryWriter.Write((double)Pt.X);
                            oShpBinaryWriter.Write((double)Pt.Y);
                            IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength);
                            //*** Write SHX Record
                            Offset += (4 + ContentLength);
                            //*** Add 4 for record header '*** Set Offset for next SHX record
                            DbaseRecordWriter(ref oDbfBinaryWriter, _Shape.AreaId, _Shape.AreaName);
                            //*** Write DBF Record
                        }
                    }

                    break;

                case 3:
                case 5:
                    RectangleF _Extent = new RectangleF();
                    for (j = 0; j <= AreaId.Length - 1; j++)
                    {
                        if (ShpInfo.Records.ContainsKey(AreaId[j]))
                        {
                            _Shape = (Shape)ShpInfo.Records[AreaId[j]];
                            if (_Extent.IsEmpty)
                            {
                                _Extent = _Shape.Extent;
                            }
                            else
                            {
                                _Extent = RectangleF.Union(_Extent, _Shape.Extent);
                            }

                            int[] NumPoints = new int[_Shape.Parts.Count];
                            Points = 0;
                            for (i = 0; i <= _Shape.Parts.Count - 1; i++)
                            {
                                Pts = (PointF[])_Shape.Parts[i];
                                NumPoints[i] = Pts.Length;
                                Points += Pts.Length;
                            }
                            ContentLength = ((11 + _Shape.Parts.Count) * 2) + (Points * 8);
                            RecordNumber += 1;
                            PolygonRecordWriter(ref oShpBinaryWriter, RecordNumber, ContentLength, Points, NumPoints, _Shape, SHPFileHeader.ShapeType);
                            IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength);
                            //*** Write SHX Record
                            Offset += (4 + ContentLength);
                            //*** Add 4 for record header '*** Set Offset for next SHX record
                            DbaseRecordWriter(ref oDbfBinaryWriter, _Shape.AreaId, _Shape.AreaName);
                            //*** Write DBF Record
                        }
                    }

                    SHPFileHeader.BoundingBoxXMin = _Extent.X;
                    SHPFileHeader.BoundingBoxYMin = _Extent.Y;
                    SHPFileHeader.BoundingBoxXMax = _Extent.X + _Extent.Width;
                    SHPFileHeader.BoundingBoxYMax = _Extent.Y + _Extent.Height;
                    // _Extent = null;
                    break;
            }

            //*** Reset FileLength and Extent in Shp File Header
            oShpStream.Position = 24;
            oShpBinaryWriter.Write(BigToLittleEndian((int)oShpStream.Length / 2));
            oShpStream.Position = 36;
            oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin);
            oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin);
            oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax);
            oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax);

            //*** Reset FileLength and Extent in Shx File Header
            oShxStream.Position = 24;
            oShxBinaryWriter.Write(BigToLittleEndian((int)oShxStream.Length / 2));
            oShxStream.Position = 36;
            oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin);
            oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin);
            oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax);
            oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax);

            //*** Dispose
            oShpBinaryWriter.Close();
            oShxBinaryWriter.Close();
            oDbfBinaryWriter.Close();
            oShpStream.Close();
            oShxStream.Close();
            oDbfStream.Close();
            oShpBinaryWriter = null;
            oShxBinaryWriter = null;
            oDbfBinaryWriter = null;
            ShpInfo = null;
        }
Example #27
0
 public bool importFile(int fnum,bool isid,byte[] data)
 {
     DinnerEntry e = fileInfo(fnum, isid);
     FileHeader hdr = readHeader(fnum, isid);
     byte[] fl;
     if (e.compressed && hdr.compressed && hdr.tot_size != e.size)
     {
         UInt32 decompsz = (uint)data.Length;
         if (e.header)
         {
             hdr = new FileHeader(data);
             fl = data;
             data = new byte[fl.Length - FileHeader.size];
             Array.Copy(fl, FileHeader.size, data, 0, data.Length);
             hdr.setSize(decompsz);
         }
         else
             hdr.setSize(decompsz);
         byte[] cmpr = RNCCompressor.compress(data);
         data=new byte[cmpr.Length+FileHeader.size];
         Array.Copy(hdr.buf, 0, data, 0, FileHeader.size);
         Array.Copy(cmpr, 0, data, FileHeader.size, cmpr.Length);
     }
     int resize = data.Length - (int)e.size;
     if (resize != 0)
         resizeDisk(resize,ref e);
     if (InstallPack)
     {
         FileStream f = new FileStream(Path.Combine(path,fnum.ToString()+".bin"), FileMode.Create);
         f.Write(data, 0, data.Length);
         f.Close();
     }
     Array.Copy(data, 0, diskbuf, e.offset, data.Length);
     return true;
 }
Example #28
0
        public static void CreateBlockFile(string sBlockId, string sBlockName, Hashtable Area_Shp, string DestFilePath, string DestFileName, ShapeType ShapeType)
        {
            Hashtable Shp_Area = new Hashtable();

            //- Get Encoding used in Current CultureInfo of OS.
            System.Text.Encoding FileEncoding = System.Text.Encoding.Default;

            //*** Build ShapeFile - AreaId Collection with unique Shape and comma delimited Areaids
            IDictionaryEnumerator dicEnumerator = Area_Shp.GetEnumerator();
            while (dicEnumerator.MoveNext())
            {
                if (Shp_Area.ContainsKey(dicEnumerator.Value))
                {
                    Shp_Area[dicEnumerator.Value] = Shp_Area[dicEnumerator.Value].ToString() + "," + dicEnumerator.Key.ToString();
                }
                else
                {
                    Shp_Area.Add(dicEnumerator.Value, dicEnumerator.Key);
                }
            }

            FileHeader SHPFileHeader = new FileHeader();
            FileStream oShpStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shp", FileMode.Create);
            BinaryWriter oShpBinaryWriter = new BinaryWriter(oShpStream);

            FileStream oShxStream = new FileStream(DestFilePath + "\\" + DestFileName + ".shx", FileMode.Create);
            BinaryWriter oShxBinaryWriter = new BinaryWriter(oShxStream);

            FileStream oDbfStream = new FileStream(DestFilePath + "\\" + DestFileName + ".dbf", FileMode.Create);
            BinaryWriter oDbfBinaryWriter = new BinaryWriter(oDbfStream, FileEncoding);

            SHPFileHeader.FileCode = BigToLittleEndian(9994);
            SHPFileHeader.Version = 1000;
            SHPFileHeader.ShapeType = (int)ShapeType;
            ShapeFileHeaderWriter(ref oShpBinaryWriter, SHPFileHeader);
            //*** Write .Shp File Header
            ShapeFileHeaderWriter(ref oShxBinaryWriter, SHPFileHeader);
            //*** Write .Shx File Header
            DbaseFileHeaderWriter(ref oDbfBinaryWriter, 1, FileEncoding);
            //Area_Shp.Count '*** Write .dbf File Header

            int i;
            int j;
            string[] AreaId;
            string SrcFile;
            string SrcFilePath;
            string SrcFileName;
            ShapeInfo ShpInfo;
            Shape _Shape = new Shape();
            Shape _Block = new Shape();
            RectangleF _Extent = new RectangleF();

            PointF[] Pts;
            int Offset = 50;
            int RecordNumber;
            int ContentLength;
            int Points;
            int BlockPointCount = 0;
            int[] BlockPartOffset = null;

            // Iterate for all distinct shape files to
            //      set single composite multipart polygon containing all distinct shapes in refrred shapefiles
            //      set composite extent

            dicEnumerator = Shp_Area.GetEnumerator();
            while (dicEnumerator.MoveNext())
            {
                SrcFile = dicEnumerator.Key.ToString();
                AreaId = dicEnumerator.Value.ToString().Split(","[0]);

                if (File.Exists(SrcFile))
                {
                    SrcFilePath = Path.GetDirectoryName(SrcFile);
                    SrcFileName = Path.GetFileNameWithoutExtension(SrcFile);
                    ShpInfo = GetShapeInfo(SrcFilePath, SrcFileName);
                    switch (ShapeType)
                    {
                        case ShapeType.Point:
                            //TODO
                            break;
                        case ShapeType.Polygon:
                        case ShapeType.PolyLine:

                            // Iterate for each shape
                            IDictionaryEnumerator dicShapeEnumerator = ShpInfo.Records.GetEnumerator();
                            while (dicShapeEnumerator.MoveNext())
                            {

                                _Shape = (Shape)dicShapeEnumerator.Value;
                                if (_Extent.IsEmpty)
                                {
                                    _Extent = _Shape.Extent;
                                }
                                else
                                {
                                    _Extent = RectangleF.Union(_Extent, _Shape.Extent);
                                }
                                int iPrevPartsCount = _Block.Parts.Count;

                                int[] temp = new int[iPrevPartsCount + _Shape.Parts.Count];

                                if (BlockPartOffset != null)
                                    Array.Copy(BlockPartOffset, temp, Math.Min(BlockPartOffset.Length, temp.Length));

                                BlockPartOffset = temp;

                                Points = 0;

                                // Iterate for each part
                                for (i = 0; i <= _Shape.Parts.Count - 1; i++)
                                {
                                    Pts = (PointF[])_Shape.Parts[i];
                                    _Block.Parts.Add(Pts);
                                    BlockPartOffset[iPrevPartsCount + i] = Pts.Length;
                                    Points += Pts.Length;
                                    BlockPointCount += Pts.Length;
                                }
                            }
                            break;
                    }
                }
            }

            _Block.Extent = _Extent;
            RecordNumber = 1;
            //*** All Shape will be added as parts of a single block
            ContentLength = ((11 + _Block.Parts.Count) * 2) + (BlockPointCount * 8);
            PolygonRecordWriter(ref oShpBinaryWriter, RecordNumber, ContentLength, BlockPointCount, BlockPartOffset, _Block, SHPFileHeader.ShapeType);
            IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength);
            //*** Write SHX Record
            DbaseRecordWriter(ref oDbfBinaryWriter, sBlockId, sBlockName);
            //*** Write DBF Record

            if (ShapeType == ShapeType.Polygon | ShapeType == ShapeType.PolyLine)
            {
                SHPFileHeader.BoundingBoxXMin = _Extent.X;
                SHPFileHeader.BoundingBoxYMin = _Extent.Y;
                SHPFileHeader.BoundingBoxXMax = _Extent.X + _Extent.Width;
                SHPFileHeader.BoundingBoxYMax = _Extent.Y + _Extent.Height;
            }

            //*** Reset FileLength and Extent in Shp File Header
            oShpStream.Position = 24;
            oShpBinaryWriter.Write(BigToLittleEndian((int)oShpStream.Length / 2));
            oShpStream.Position = 36;
            oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin);
            oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin);
            oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax);
            oShpBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax);

            //*** Reset FileLength and Extent in Shx File Header
            oShxStream.Position = 24;
            oShxBinaryWriter.Write(BigToLittleEndian((int)oShxStream.Length / 2));
            oShxStream.Position = 36;
            oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMin);
            oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMin);
            oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxXMax);
            oShxBinaryWriter.Write(SHPFileHeader.BoundingBoxYMax);

            //*** Dispose
            oShpBinaryWriter.Close();
            oShxBinaryWriter.Close();
            oDbfBinaryWriter.Close();
            oShpStream.Close();
            oShxStream.Close();
            oDbfStream.Close();
            oShpBinaryWriter = null;
            oShxBinaryWriter = null;
            oDbfBinaryWriter = null;
            // _Extent = null;
            ShpInfo = null;
        }
Example #29
0
        FileHeader GetHeaderPrototype()
        {
            FileHeader header = new FileHeader(FileVersion, 0, this.fileType);

            return(header);
        }
Example #30
0
        public static void CreateShapefile(Layer _Layer, string FilePath, string FileName)
        {
            System.Text.Encoding FileEncoding = System.Text.Encoding.Default;

            FileHeader SHPFileHeader = new FileHeader();
            FileStream oShpStream = new FileStream(FilePath + "\\" + FileName + ".shp", FileMode.Create);
            BinaryWriter oShpBinaryWriter = new BinaryWriter(oShpStream);

            FileStream oShxStream = new FileStream(FilePath + "\\" + FileName + ".shx", FileMode.Create);
            BinaryWriter oShxBinaryWriter = new BinaryWriter(oShxStream);

            FileStream oDbfStream = new FileStream(FilePath + "\\" + FileName + ".dbf", FileMode.Create);
            BinaryWriter oDbfBinaryWriter = new BinaryWriter(oDbfStream, FileEncoding);
            SHPFileHeader.FileCode = BigToLittleEndian(9994);
            SHPFileHeader.Version = 1000;
            switch (_Layer.LayerType)
            {

                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.Point:
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PointCustom:
                    SHPFileHeader.ShapeType = (int)ShapeType.Point;
                    break;
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolyLine:
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolyLineCustom:
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolyLineFeature:
                    SHPFileHeader.ShapeType = (int)ShapeType.PolyLine;
                    break;
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.Polygon:
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolygonBuffer:
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolygonCustom:
                case DevInfo.Lib.DI_LibBAL.UI.Presentations.Map.ShapeType.PolygonFeature:
                    SHPFileHeader.ShapeType = (int)ShapeType.Polygon;
                    break;
            }
            SHPFileHeader.BoundingBoxXMin = _Layer.Extent.X;
            SHPFileHeader.BoundingBoxYMin = _Layer.Extent.Y;
            SHPFileHeader.BoundingBoxXMax = _Layer.Extent.X + _Layer.Extent.Width;
            SHPFileHeader.BoundingBoxYMax = _Layer.Extent.Y + _Layer.Extent.Height;

            ShapeFileHeaderWriter(ref oShpBinaryWriter, SHPFileHeader);
            //*** Write .Shp File Header
            ShapeFileHeaderWriter(ref oShxBinaryWriter, SHPFileHeader);
            //*** Write .Shx File Header
            DbaseFileHeaderWriter(ref oDbfBinaryWriter, _Layer.RecordCount, FileEncoding);
            //*** Write .dbf File Header

            int i;
            Shape _Shape = new Shape();
            PointF[] Pts;
            int Offset = 50;
            int RecordNumber = 0;
            int ContentLength;
            int Points;

            IDictionaryEnumerator dicEnumerator = _Layer.Records.GetEnumerator();
            switch (SHPFileHeader.ShapeType)
            {
                case 1:
                    PointF Pt;
                    ContentLength = 10;
                    while (dicEnumerator.MoveNext())
                    {
                        _Shape = (Shape)dicEnumerator.Value;
                        Pt = (PointF)_Shape.Parts[0];
                        RecordNumber += 1;
                        oShpBinaryWriter.Write(BigToLittleEndian(RecordNumber));
                        oShpBinaryWriter.Write(BigToLittleEndian(ContentLength));
                        oShpBinaryWriter.Write((int)1);
                        //Shape Type
                        oShpBinaryWriter.Write((double)Pt.X);
                        oShpBinaryWriter.Write((double)Pt.Y);
                        IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength);
                        //*** Write SHX Record
                        Offset += (4 + ContentLength);
                        //*** Add 4 for record header '*** Set Offset for next SHX record
                        DbaseRecordWriter(ref oDbfBinaryWriter, _Shape.AreaId, _Shape.AreaName);
                        //*** Write DBF Record
                    }

                    break;
                case 3:
                case 5:
                    while (dicEnumerator.MoveNext())
                    {
                        _Shape = (Shape)dicEnumerator.Value;
                        int[] NumPoints = new int[_Shape.Parts.Count];
                        Points = 0;
                        for (i = 0; i <= _Shape.Parts.Count - 1; i++)
                        {
                            Pts = (PointF[])_Shape.Parts[i];
                            NumPoints[i] = Pts.Length;
                            Points += Pts.Length;
                        }
                        ContentLength = ((11 + _Shape.Parts.Count) * 2) + (Points * 8);
                        RecordNumber += 1;
                        PolygonRecordWriter(ref oShpBinaryWriter, RecordNumber, ContentLength, Points, NumPoints, _Shape, SHPFileHeader.ShapeType);
                        IndexRecordWriter(ref oShxBinaryWriter, Offset, ContentLength);
                        //*** Write SHX Record
                        Offset += (4 + ContentLength);
                        //*** Add 4 for record header '*** Set Offset for next SHX record
                        DbaseRecordWriter(ref oDbfBinaryWriter, _Shape.AreaId, _Shape.AreaName);
                        //*** Write DBF Record
                    }

                    break;
            }

            //*** Reset FileLength in Shp File Header
            oShpStream.Position = 24;
            oShpBinaryWriter.Write(BigToLittleEndian((int)oShpStream.Length / 2));

            //*** Reset FileLength in Shx File Header
            oShxStream.Position = 24;
            oShxBinaryWriter.Write(BigToLittleEndian((int)oShxStream.Length / 2));

            //*** Dispose
            oShpBinaryWriter.Close();
            oShxBinaryWriter.Close();
            oDbfBinaryWriter.Close();
            oShpStream.Close();
            oShxStream.Close();
            oDbfStream.Close();
            oShpBinaryWriter = null;
            oShxBinaryWriter = null;
            oDbfBinaryWriter = null;
            oShpStream = null;
            oShxStream = null;
            oDbfStream = null;
            dicEnumerator = null;
        }
Example #31
0
 /// <exception cref="System.IO.IOException"></exception>
 private void Init(string name)
 {
     a    = new RawText(ReadFile(name + "_PreImage"));
     b    = new RawText(ReadFile(name + "_PostImage"));
     file = ParseTestPatchFile(name + ".patch").GetFiles()[0];
 }
Example #32
0
            internal void Write(BinaryWriterEx bw, List <File> files)
            {
                bw.BigEndian = BigEndian;
                bw.WriteASCII("BHF4");
                bw.WriteBoolean(Flag1);
                bw.WriteBoolean(Flag2);
                bw.WriteByte(0);
                bw.WriteByte(0);
                bw.WriteInt32(0x10000);
                bw.WriteInt32(files.Count);
                bw.WriteInt64(0x40);
                bw.WriteASCII(Timestamp);
                if (Format == 0x0C || Format == 0x30)
                {
                    bw.WriteInt64(0x18);
                }
                else if (Format == 0x2E || Format == 0x74)
                {
                    bw.WriteInt64(0x24);
                }
                else if (Format == 0x3E)
                {
                    bw.WriteInt64(0x28);
                }
                bw.WriteInt64(0);

                bw.WriteBoolean(Unicode);
                bw.WriteByte(Format);
                bw.WriteByte(Extended);
                bw.WriteByte(0);

                bw.WriteInt32(0);
                if (Extended == 4)
                {
                    bw.ReserveInt64("HashGroups");
                }
                else
                {
                    bw.WriteInt64(0);
                }

                for (int i = 0; i < files.Count; i++)
                {
                    FileHeader.Write(bw, files[i], i, Format);
                }

                for (int i = 0; i < files.Count; i++)
                {
                    File file = files[i];
                    bw.FillInt32($"FileName{i}", (int)bw.Position);
                    if (Unicode)
                    {
                        bw.WriteUTF16(file.Name, true);
                    }
                    else
                    {
                        bw.WriteShiftJIS(file.Name, true);
                    }
                }

                if (Extended == 4)
                {
                    uint groupCount = 0;
                    for (uint p = (uint)files.Count / 7; p <= 100000; p++)
                    {
                        if (Util.IsPrime(p))
                        {
                            groupCount = p;
                            break;
                        }
                    }

                    if (groupCount == 0)
                    {
                        throw new InvalidOperationException("Hash group count not determined in BXF4.");
                    }

                    var hashLists = new List <PathHash> [groupCount];
                    for (int i = 0; i < groupCount; i++)
                    {
                        hashLists[i] = new List <PathHash>();
                    }

                    for (int i = 0; i < files.Count; i++)
                    {
                        var  pathHash = new PathHash(i, files[i].Name);
                        uint group    = pathHash.Hash % groupCount;
                        hashLists[group].Add(pathHash);
                    }

                    for (int i = 0; i < groupCount; i++)
                    {
                        hashLists[i].Sort((ph1, ph2) => ph1.Hash.CompareTo(ph2.Hash));
                    }

                    var hashGroups = new List <HashGroup>();
                    var pathHashes = new List <PathHash>();

                    int count = 0;
                    foreach (List <PathHash> hashList in hashLists)
                    {
                        int index = count;
                        foreach (PathHash pathHash in hashList)
                        {
                            pathHashes.Add(pathHash);
                            count++;
                        }

                        hashGroups.Add(new HashGroup(index, count - index));
                    }

                    bw.Pad(0x8);
                    bw.FillInt64("HashGroups", bw.Position);
                    bw.ReserveInt64("PathHashes");
                    bw.WriteUInt32(groupCount);
                    bw.WriteInt32(0x00080810);

                    foreach (HashGroup hashGroup in hashGroups)
                    {
                        hashGroup.Write(bw);
                    }

                    // No padding after section 1
                    bw.FillInt64("PathHashes", bw.Position);
                    foreach (PathHash pathHash in pathHashes)
                    {
                        pathHash.Write(bw);
                    }
                }
            }
Example #33
0
 internal FileInfoRarFilePart(MarkHeader mh, FileHeader fh, FileInfo fi)
     : base(mh, fh, true)
 {
     FileInfo = fi;
 }
        public async Task <MemoryStream> SerializeCAS(CASData saveData)
        {
            var qw = new SymbolBufferWriter();

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    FileHeader header = new FileHeader();
                    header.Write(bw); //write place holder

                    int header_size = (int)ms.Position;

                    var retMs = new MemoryStream();


                    var summary_info = (List <SymbolEntry>)saveData.summary;
                    summary_info = await GenerateChecksums(summary_info);

                    qw.SerializeBuffer(bw, summary_info);
                    var summary_size = ms.Position - header_size;

                    var save_data = (List <SymbolEntry>)saveData.save_data;
                    save_data = await GenerateChecksums(save_data);

                    qw.SerializeBuffer(bw, save_data);
                    var save_size = ms.Position - summary_size - header_size;

                    byte[] summary_buffer = summary_buffer = ms.GetBuffer().Skip(header_size).Take((int)summary_size).ToArray();

                    header.summaryInfoChecksum = await checksumResolver.GenerateChecksum(summary_buffer);

                    header.checksum        = 0;
                    header.dataSize        = (int)(summary_size + header_size + save_size);
                    header.summaryInfoSize = (int)summary_size;
                    header.version         = version;

                    bw.BaseStream.Seek(0, SeekOrigin.Begin);
                    header.Write(bw);

                    byte[] buff = ms.GetBuffer().Take(header.dataSize).ToArray();
                    header.checksum = await checksumResolver.GenerateChecksum(buff);

                    bw.BaseStream.Seek(0, SeekOrigin.Begin);
                    header.Write(bw);
                    buff = ms.GetBuffer();

                    bw.BaseStream.Seek(0, SeekOrigin.End);
                    var desired_size = fixedFileSize - header.dataSize;
                    for (int i = 0; i < desired_size; i++)
                    {
                        bw.Write((byte)'i');
                    }

                    ms.WriteTo(retMs);



                    return(retMs);
                }
            }
        }
        public HttpResponseMessage UpdateMetadata(string fileSystemName, string fileName)
        {
            bool isConflictResolved = false;

            var canonicalFilename = FileHeader.Canonize(fileName);

            var sourceServerInfo = InnerHeaders.Value <ServerInfo>(SyncingMultipartConstants.SourceServerInfo);
            // REVIEW: (Oren) It works, but it seems to me it is not an scalable solution.
            var sourceFileETag = Guid.Parse(InnerHeaders.GetValues(Constants.MetadataEtagField).First().Trim('\"'));

            Log.Debug("Starting to update a metadata of file '{0}' with ETag {1} from {2} because of synchronization", fileName,
                      sourceFileETag, sourceServerInfo);

            var report = new SynchronizationReport(canonicalFilename, sourceFileETag, SynchronizationType.MetadataUpdate);

            try
            {
                Storage.Batch(accessor =>
                {
                    AssertFileIsNotBeingSynced(canonicalFilename, accessor);
                    FileLockManager.LockByCreatingSyncConfiguration(canonicalFilename, sourceServerInfo, accessor);
                });

                SynchronizationTask.IncomingSynchronizationStarted(canonicalFilename, sourceServerInfo, sourceFileETag, SynchronizationType.MetadataUpdate);

                PublishSynchronizationNotification(fileSystemName, canonicalFilename, sourceServerInfo, report.Type, SynchronizationAction.Start);

                Storage.Batch(accessor => StartupProceed(canonicalFilename, accessor));

                var localMetadata  = GetLocalMetadata(canonicalFilename);
                var sourceMetadata = GetFilteredMetadataFromHeaders(InnerHeaders);

                AssertConflictDetection(canonicalFilename, localMetadata, sourceMetadata, sourceServerInfo, out isConflictResolved);

                Historian.UpdateLastModified(sourceMetadata);

                Storage.Batch(accessor => accessor.UpdateFileMetadata(canonicalFilename, sourceMetadata));

                Search.Index(canonicalFilename, sourceMetadata);

                if (isConflictResolved)
                {
                    ConflictArtifactManager.Delete(canonicalFilename);
                }

                PublishFileNotification(fileName, FileChangeAction.Update);
            }
            catch (Exception ex)
            {
                if (ShouldAddExceptionToReport(ex))
                {
                    report.Exception = ex;

                    Log.WarnException(
                        string.Format("Error was occurred during metadata synchronization of file '{0}' from {1}", fileName, sourceServerInfo), ex);
                }
            }
            finally
            {
                FinishSynchronization(canonicalFilename, report, sourceServerInfo, sourceFileETag);
            }

            PublishSynchronizationNotification(fileSystemName, fileName, sourceServerInfo, report.Type, SynchronizationAction.Finish);

            if (isConflictResolved)
            {
                Publisher.Publish(new ConflictNotification
                {
                    FileName = fileName,
                    Status   = ConflictStatus.Resolved
                });
            }

            if (report.Exception == null)
            {
                Log.Debug("Metadata of file '{0}' was synchronized successfully from {1}", fileName, sourceServerInfo);
            }

            return(this.GetMessageWithObject(report, HttpStatusCode.OK));
        }
Example #36
0
 public static string SyncNameForFile(string fileName, string destination)
 {
     return(SyncNamePrefix + "/" + Uri.EscapeUriString(destination) + FileHeader.Canonize(fileName));
 }
        public HttpResponseMessage Rename(string fileSystemName, string fileName, string rename)
        {
            bool isConflictResolved = false;

            var canonicalFilename = FileHeader.Canonize(fileName);
            var canonicalRename   = FileHeader.Canonize(rename);

            var sourceServerInfo = InnerHeaders.Value <ServerInfo>(SyncingMultipartConstants.SourceServerInfo);
            var sourceFileETag   = Guid.Parse(InnerHeaders.GetValues(Constants.MetadataEtagField).First().Trim('\"'));
            var sourceMetadata   = GetFilteredMetadataFromHeaders(InnerHeaders);

            Log.Debug("Starting to rename a file '{0}' to '{1}' with ETag {2} from {3} because of synchronization", fileName,
                      rename, sourceFileETag, sourceServerInfo);

            var report = new SynchronizationReport(canonicalFilename, sourceFileETag, SynchronizationType.Rename);

            try
            {
                Storage.Batch(accessor =>
                {
                    AssertFileIsNotBeingSynced(canonicalFilename, accessor);
                    FileLockManager.LockByCreatingSyncConfiguration(canonicalFilename, sourceServerInfo, accessor);
                });

                SynchronizationTask.IncomingSynchronizationStarted(canonicalFilename, sourceServerInfo, sourceFileETag, SynchronizationType.Rename);

                PublishSynchronizationNotification(fileSystemName, canonicalFilename, sourceServerInfo, report.Type, SynchronizationAction.Start);

                Storage.Batch(accessor => StartupProceed(canonicalFilename, accessor));

                var localMetadata = GetLocalMetadata(canonicalFilename);

                AssertConflictDetection(canonicalFilename, localMetadata, sourceMetadata, sourceServerInfo, out isConflictResolved);

                if (isConflictResolved)
                {
                    ConflictArtifactManager.Delete(canonicalFilename);
                }

                StorageOperationsTask.RenameFile(new RenameFileOperation
                {
                    FileSystem             = FileSystem.Name,
                    Name                   = canonicalFilename,
                    Rename                 = canonicalRename,
                    MetadataAfterOperation = sourceMetadata.WithETag(sourceFileETag).DropRenameMarkers()
                });
            }
            catch (Exception ex)
            {
                if (ShouldAddExceptionToReport(ex))
                {
                    report.Exception = ex;
                    Log.WarnException(string.Format("Error was occurred during renaming synchronization of file '{0}' from {1}", fileName, sourceServerInfo), ex);
                }
            }
            finally
            {
                FinishSynchronization(canonicalFilename, report, sourceServerInfo, sourceFileETag);
            }

            PublishSynchronizationNotification(fileSystemName, canonicalFilename, sourceServerInfo, report.Type, SynchronizationAction.Finish);

            if (isConflictResolved)
            {
                Publisher.Publish(new ConflictNotification
                {
                    FileName = fileName,
                    Status   = ConflictStatus.Resolved
                });
            }

            if (report.Exception == null)
            {
                Log.Debug("File '{0}' was renamed to '{1}' during synchronization from {2}", fileName, rename, sourceServerInfo);
            }

            return(GetMessageWithObject(report));
        }
Example #38
0
 public static string ConflictConfigNameForFile(string fileName)
 {
     return(ConflictConfigNamePrefix + FileHeader.Canonize(fileName));
 }
Example #39
0
        static void Main(string[] args)
        {
            string catFileName   = "";
            string inFileName    = "";
            string baseDirectory = "out";
            bool   dryRun        = false;

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i] == "-c")
                {
                    catFileName = args[i + 1];
                }
                if (args[i] == "-f")
                {
                    inFileName = args[i + 1];
                }
                if (args[i] == "-d")
                {
                    baseDirectory = args[i + 1];
                }
                if (args[i] == "--dry")
                {
                    dryRun = true;
                }
            }

            if (inFileName.Length == 0 || !File.Exists(inFileName))
            {
                Console.WriteLine("Usage: hpbackup -c <catalog file name> -f <data file name> [-d <output directory>]");
                return;
            }

            byte[] bytes = new byte[65536];

            // First, read the catalog.
            var catalog = new List <FileHeader>();

            try
            {
                using (var stream = new FileStream(catFileName, FileMode.Open, FileAccess.Read))
                {
                    while (stream.Position < stream.Length)
                    {
                        var header = new FileHeader(stream);

                        if (!header.Valid)
                        {
                            AlignToNextBlock(stream);
                            continue;
                        }

                        catalog.Add(header);
                        Console.WriteLine(stream.Position.ToString("X") + ":  " + header.Size.ToString("X") + " -- " + header.DateTime.ToString() + " -- " + header.Name);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
                return;
            }

            // ...And read the contents, hoping that the data perfectly lines up with the contents.
            try
            {
                using (var stream = new FileStream(inFileName, FileMode.Open, FileAccess.Read))
                {
                    while (stream.Position < stream.Length && catalog.Count > 0)
                    {
                        FileHeader currentFile = null;
                        while (catalog.Count > 0)
                        {
                            currentFile = catalog[0];
                            catalog.RemoveAt(0);
                            if (currentFile.Size > 0)
                            {
                                break;
                            }
                            else
                            {
                                currentFile = null;
                            }
                        }
                        if (currentFile == null)
                        {
                            Console.WriteLine("Warning: Reached end of catalog with file data remaining.");
                            break;
                        }

                        string filePath = baseDirectory;
                        if (currentFile.Subdirectory.Length > 0)
                        {
                            string[] dirArray = currentFile.Subdirectory.Split('\\');
                            for (int i = 0; i < dirArray.Length; i++)
                            {
                                filePath = Path.Combine(filePath, dirArray[i]);
                            }
                        }
                        if (!dryRun)
                        {
                            Directory.CreateDirectory(filePath);
                        }
                        filePath = Path.Combine(filePath, currentFile.Name);

                        Console.WriteLine("Restoring: " + filePath);

                        using (var f = dryRun ? (Stream) new MemoryStream() : (Stream) new FileStream(filePath, FileMode.Create, FileAccess.Write))
                        {
                            long bytesLeft = currentFile.Size;
                            while (bytesLeft > 0)
                            {
                                int bytesToRead = bytes.Length;
                                if (bytesToRead > bytesLeft)
                                {
                                    bytesToRead = (int)bytesLeft;
                                }
                                stream.Read(bytes, 0, bytesToRead);
                                f.Write(bytes, 0, bytesToRead);

                                if (bytesLeft == currentFile.Size)
                                {
                                    if (!QicUtils.Utils.VerifyFileFormat(currentFile.Name, bytes))
                                    {
                                        Console.WriteLine(stream.Position.ToString("X") + " -- Warning: file format doesn't match: " + filePath);
                                        Console.ReadKey();
                                    }
                                }

                                bytesLeft -= bytesToRead;
                            }
                        }

                        if (!dryRun)
                        {
                            File.SetCreationTime(filePath, currentFile.DateTime);
                            File.SetLastWriteTime(filePath, currentFile.DateTime);
                            //File.SetAttributes(filePath, header.Attributes);
                        }
                    }

                    if (catalog.Count > 0)
                    {
                        Console.WriteLine("Warning: Reached end of file data with catalog items remaining.");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
            }
        }
Example #40
0
 public static string SyncResultNameForFile(string fileName)
 {
     return(SyncResultNamePrefix + FileHeader.Canonize(fileName));
 }
Example #41
0
        /// <summary>
        /// ZIP local file header
        /// Write file header to output file with CRC and compressed length set to zero
        /// </summary>
        /// <param name="fullFileName">Full name of the file.</param>
        /// <param name="archiveFileName">Name of the archive file.</param>
        /// <exception cref="UZipDotNet.Exception">Duplicate file name</exception>
        private void WriteFileHeader(string fullFileName, string archiveFileName)
        {
            //
            //	file header
            //
            //	Pos		Len
            //	0		4		Local file header signature = 0x04034b50
            //	4		2		Version needed to extract (minimum)
            //	6		2		General purpose bit flag
            //	8		2		Compression method
            //	10		2		File last modification time
            //	12		2		File last modification date
            //	14		4		CRC-32
            //	18		4		Compressed size
            //	22		4		Uncompressed size
            //	26		2		File name length (n)
            //	28		2		Extra field length (m)
            //	30		n		File name
            //	30+n	m		Extra field

            // save write file length for possible error
            _writeStartPosition = _writeStream.Length;

            // file info
            var fi = new FileInfo(fullFileName);

            // Directory flag
            bool dirFlag = (fi.Attributes & FileAttributes.Directory) != 0;

            // create file header structure (name, time, attributes and version)
            _fileHeaderInfo = new FileHeader(archiveFileName, fi.LastWriteTime, fi.Attributes, _writeStream.Length, dirFlag ? 0 : fi.Length);

            // compression method deflate
            if (!dirFlag) _fileHeaderInfo.CompMethod = 8;

            // test for duplicate file name
            if (ZipDir.BinarySearch(_fileHeaderInfo) >= 0) throw new Exception("Duplicate file name");

            // local file header signature
            _writeFile.Write((uint)0x04034b50);

            // version needed to extract 2.0 (file system is dos)
            _writeFile.Write(_fileHeaderInfo.Version);

            // general purpose bit flag
            _writeFile.Write(_fileHeaderInfo.BitFlags);

            // compression method deflate
            _writeFile.Write(_fileHeaderInfo.CompMethod);

            // last mod file time
            _writeFile.Write(_fileHeaderInfo.FileTime);

            // last mod file date
            _writeFile.Write(_fileHeaderInfo.FileDate);

            // CRC-32 is set to zero until CRC will be calculated
            _writeFile.Write(_fileHeaderInfo.FileCRC32);

            // compressed size is set to zero until the compressed size is known
            _writeFile.Write(_fileHeaderInfo.CompSize);

            // uncompressed size
            _writeFile.Write(_fileHeaderInfo.FileSize);

            // convert filename string to array of bytes using DOS (IBM OEM code page 437) encoding
            byte[] zipFileName = Utils.EncodeFilename(_fileHeaderInfo.FileName);

            // file name length
            _writeFile.Write((Int16)zipFileName.Length);

            // extra field length (for file times)
            _writeFile.Write((Int16)(dirFlag ? 0 : 36));

            // file name
            _writeFile.Write(zipFileName);

            // extra field
            if (!dirFlag)
            {
                // NTFS tag of length 32, reserved area of zero, File times tag of 1 with length 24
                _writeFile.Write((UInt32)0x0020000a);
                _writeFile.Write((UInt32)0);
                _writeFile.Write((UInt32)0x00180001);
                _writeFile.Write(fi.LastWriteTime.ToFileTime());
                _writeFile.Write((File.GetLastAccessTime(fullFileName).ToFileTime()));
                _writeFile.Write((File.GetCreationTime(fullFileName).ToFileTime()));
            }

            // save write file length for possible rewind
            _writeRewindPosition = _writeStream.Length;
        }
Example #42
0
 public static string DownloadingFileName(string fileName)
 {
     return(FileHeader.Canonize(fileName) + DownloadingFileSuffix);
 }
Example #43
0
 internal FileHeader toFileHeader(string response)
 {
     if (response == "")
     {
         return null;
     }
     FileHeader result = new FileHeader();
     string[] flds = StringUtils.split(response, StringUtils.CARET);
     result.Name = flds[0];
     result.AlternateName = "";
     int i = 0;
     while (StringUtils.isNumericChar(flds[1][i]))
     {
         result.AlternateName += flds[1][i];
         i++;
     }
     if (i < flds[1].Length)
     {
         result.Characteristics = new ArrayList();
         do
         {
             result.Characteristics.Add(flds[1][i]);
             i++;
         } while (i < flds[1].Length);
     }
     result.LatestId = flds[2];
     result.NumberOfRecords = Convert.ToInt64(flds[3]);
     return result;
 }
        /// <summary>
        /// Inserts any missing items from the file header.
        /// Also formats the existing header ensuring that the top and bottom line start with 2 slashes and a space and that a newline follows the header.
        /// </summary>
        /// <param name="options">
        /// The options.
        /// </param>
        /// <param name="file">
        /// The file to update.
        /// </param>
        private void UpdateFileHeader(DocumentationOptions options, ICSharpFile file)
        {
            // The idea here is to load the existing header into our FileHeader object
            // The FileHeader object will ensure that the format of the header is correct even if we're not changing its contents
            // Thus we'll swap it out if its changed at the end.
            string fileName = file.GetSourceFile().ToProjectFile().Location.Name;
            UpdateFileHeaderStyle updateFileHeaderOption = options.SA1633SA1641UpdateFileHeader;

            if (updateFileHeaderOption == UpdateFileHeaderStyle.Ignore)
            {
                return;
            }

            DocumentationRulesConfiguration docConfig = this.GetDocumentationRulesConfig(file);
            string summaryText = Utils.GetSummaryText(file);
            FileHeader fileHeader = new FileHeader(file) { InsertSummary = options.SA1639FileHeaderMustHaveSummary };

            switch (updateFileHeaderOption)
            {
                case UpdateFileHeaderStyle.ReplaceCopyrightElement:
                    fileHeader.FileName = fileName;
                    fileHeader.CompanyName = docConfig.CompanyName;
                    fileHeader.CopyrightText = docConfig.Copyright;
                    fileHeader.Summary = string.IsNullOrEmpty(fileHeader.Summary) ? summaryText : fileHeader.Summary;
                    break;

                case UpdateFileHeaderStyle.ReplaceAll:
                    fileHeader.FileName = fileName;
                    fileHeader.CompanyName = docConfig.CompanyName;
                    fileHeader.CopyrightText = docConfig.Copyright;
                    fileHeader.Summary = summaryText;
                    break;

                case UpdateFileHeaderStyle.InsertMissing:
                    fileHeader.FileName = string.IsNullOrEmpty(fileHeader.FileName) ? fileName : fileHeader.FileName;
                    fileHeader.CompanyName = string.IsNullOrEmpty(fileHeader.CompanyName) ? docConfig.CompanyName : fileHeader.CompanyName;
                    fileHeader.CopyrightText = string.IsNullOrEmpty(fileHeader.CopyrightText) ? docConfig.Copyright : fileHeader.CopyrightText;
                    fileHeader.Summary = string.IsNullOrEmpty(fileHeader.Summary) ? summaryText : fileHeader.Summary;
                    break;
            }

            fileHeader.Update();
        }
Example #45
0
        /// <summary>
        /// Loads a BTI file from a byte array. BTI files have two ways of being stored,
        /// both externally in their own file (simple), and embedded within another 
        /// file (complex). If you're loading from an isolated file, simply pass the
        /// entire file to data and zero for the other two parameters.
        /// 
        /// However, embedded files are more complicated. An embedded file stores the
        /// data in the following format: [bti header][bti header][bti header][bti data]
        /// [bti data][bti data]. The embedded bti files store their offsets relative not
        /// to the start of the first bti header, but to themselves.
        /// 
        /// To handle this, pass the offset into the byte[] that the header is at for 
        /// <paramref name="mainOffset"/> and then pass (btiHeaderIndex * FileHeader.Size)
        /// for <paramref name="dataOffset"/>. This load function will resolve the offsets
        /// for loading.
        /// </summary>
        /// <param name="data">Array holding entire BTI file.</param>
        /// <param name="mainOffset">Offset from start of array to BTI file.</param>
        /// <param name="dataOffset">Additional offset required by embedded BTI files.</param>
        public void Load(byte[] data, uint mainOffset, uint dataOffset)
        {
            _header = new FileHeader();
            _header.Load(data, mainOffset);

            //Copy our public settings out of the header and into the BinaryTextureImage instance.
            Format = _header.Format;
            Width = _header.Width;
            Height = _header.Height;
            WrapS = _header.WrapS;
            WrapT = _header.WrapT;
            MinFilter = _header.FilterSettingMin;
            MagFilter = _header.FilterSettingMag;

            //Grab the palette data
            _imagePalette = new Palette();
            _imagePalette.Load(data, _header.PaletteEntryCount, dataOffset + _header.PaletteDataOffset);

            //Now lets load a copy of the image data out of the file.
            _argbImageData = DecodeData(data, Width, Height, dataOffset + _header.ImageDataOffset, Format, _imagePalette, _header.PaletteFormat);
        }
        /// <summary>
        /// Inserts copyright text into the file's header.
        /// </summary>
        /// <param name="file">
        /// The file to insert the company name into.
        /// </param>
        public void InsertCopyrightText(ICSharpFile file)
        {
            DocumentationRulesConfiguration docConfig = this.GetDocumentationRulesConfig(file);

            FileHeader fileHeader = new FileHeader(file) { CopyrightText = docConfig.Copyright };

            fileHeader.Update();
        }
Example #47
0
			public void AfterChange(FileHeader instance, RavenJObject metadata)
			{
				
			}
 /// <summary>
 /// Inserts a summary into the file's header.
 /// </summary>
 /// <param name="file">
 /// The file to insert into.
 /// </param>
 public void InsertFileHeaderSummary(ICSharpFile file)
 {
     FileHeader fileHeader = new FileHeader(file) { Summary = Utils.GetSummaryText(file) };
     fileHeader.Update();
 }
        /// <summary>
        /// 读取光谱文件
        /// </summary>
        public static bool ReadFile(byte[] fileData, FileFormat fileFormat)
        {
            fileFormat.dataInfoList    = null;
            fileFormat.acquisitionInfo = null;
            fileFormat.xDataList       = null;
            fileFormat.fileInfo        = null;
            fileFormat.yDataList       = null;

            MainHeader mainheader = GetMainHeader(fileData);

            if (mainheader.fileMark == UInt16.MaxValue)  //读取文件头错误
            {
                return(false);
            }

            //填充光谱文件信息
            fileFormat.fileInfo            = new FileFormat.FileInfo();
            fileFormat.fileInfo.createTime = CreateFileDateTime(mainheader.fileTime);
            fileFormat.fileInfo.dataCount  = (int)mainheader.specCols;
            fileFormat.fileInfo.fileMemo   = CommonMethod.CovertByteArrayToString(mainheader.tile, Encoding.Default);
            //仪器描述 仪器名称-序列号
            fileFormat.fileInfo.instDescription = CommonMethod.CovertByteArrayToString(mainheader.instrument, Encoding.Default) + instSerialSeprator +
                                                  CommonMethod.CovertByteArrayToString(mainheader.serialNo, Encoding.Default);
            fileFormat.fileInfo.modifyFlag = 0;
            fileFormat.fileInfo.resolution = 16;                        //固定值

            fileFormat.fileInfo.specType = FileFormat.SPECTYPE.SPCNIR;  //固定值
            fileFormat.fileInfo.xType    = FileFormat.XAXISTYPE.XNMETR; //mainheader.xType = 1
            fileFormat.fileInfo.zType    = FileFormat.ZAXISTYPE.XMSEC;  //固定值
            fileFormat.fileInfo.fzinc    = 0.5f;                        //固定值
            fileFormat.fileInfo.fspare   = new float[8];                //固定值

            //读取X轴数据(肯定是均匀的X轴)
            fileFormat.xDataList = new List <double[]>();
            double[] tempx = new double[mainheader.specCols];
            int      index = 0;

            for (int i = 0; i < mainheader.rangeCount; i++)  //每个光谱段读取
            {
                for (int j = 0; j < mainheader.rangeCols[i]; j++)
                {
                    tempx[index] = mainheader.rangeFirstX[i] + j * mainheader.rangeStepX[i];
                    index++;
                }
            }
            fileFormat.xDataList.Add(tempx);

            //获取Y数据以及格式信息
            fileFormat.dataInfoList = new List <FileFormat.DataInfo>();
            fileFormat.yDataList    = new List <double[]>();

            for (int i = 0; i < mainheader.fileCount; i++)
            {
                FileHeader fileHeader = GetFileHeader(fileData, i);
                if (fileHeader.position == UInt16.MaxValue)
                {
                    return(false);
                }

                //读取Y轴数据
                double[] ydata = GetSpectrumData(fileData, i);
                if (ydata == null || ydata.Length == 0)
                {
                    return(false);
                }
                fileFormat.yDataList.Add(ydata);

                //Y轴数据格式
                FileFormat.DataInfo info = new FileFormat.DataInfo();
                info.dataTitle = CommonMethod.CovertByteArrayToString(fileHeader.sampleNo, Encoding.Default);
                info.dataType  = FileFormat.YAXISTYPE.YABSRB;
                info.firstX    = mainheader.rangeFirstX[0];
                info.lastX     = mainheader.rangeLastX[mainheader.rangeCount - 1];
                info.maxYValue = ydata.Max();
                info.minYValue = ydata.Min();
                fileFormat.dataInfoList.Add(info);
            }

            //读取光谱参数
            fileFormat.acquisitionInfo          = new FileFormat.AcquisitionInfo();
            fileFormat.acquisitionInfo.GAIN     = 0;
            fileFormat.acquisitionInfo.HIGHPASS = 0;
            fileFormat.acquisitionInfo.LOWPASS  = 0;
            fileFormat.acquisitionInfo.LWN      = 0;
            fileFormat.acquisitionInfo.SPEED    = null;
            fileFormat.acquisitionInfo.SCANS    = 0;
            fileFormat.acquisitionInfo.SCANSBG  = 0;
            fileFormat.acquisitionInfo.IRMODE   = FileFormat.enumIRMODE.NearIR;

            return(true);
        }
Example #50
0
        public override void Save(BinaryWriter stream)
        {
            //Write the file header
            FileHeader header = new FileHeader();
            header.ChunkCount = _chunkList.Count;
            header.Save(stream);

            //Save the current position of the stream, then allocate numChunkHeaders * chunkHeaderSize
            //bytes in the stream. We'll then create the chunk headers as we write the chunk data,
            //and then come back to this position and write the headers in afterwards.
            int chunkHeaderOffset = (int) stream.BaseStream.Position;
            stream.BaseStream.Position += _chunkList.Count*ChunkHeader.Size;
            List<ChunkHeader> chunkHeaders = new List<ChunkHeader>();

            int rtblHeaderOffset;

            foreach (KeyValuePair<Type, List<BaseChunk>> pair in _chunkList)
            {
                ChunkHeader chunkHeader = new ChunkHeader();
                chunkHeader.ChunkOffset = (int) stream.BaseStream.Position;
                chunkHeader.Tag = pair.Value[0].ChunkName; //ToDo: We're in trouble if the chunk has no children.
                chunkHeader.ElementCount = pair.Value.Count;

                chunkHeaders.Add(chunkHeader);

                if (chunkHeader.Tag == "RTBL")
                {
                    rtblHeaderOffset = (int) stream.BaseStream.Position;
                    stream.BaseStream.Position += pair.Value.Count*RTBLChunk.Header.Size;

                    //Then write all of the Entry and Table pairs.
                    foreach (BaseChunk chunk in pair.Value)
                    {
                        RTBLChunk rtblHeader = (RTBLChunk) chunk;
                        rtblHeader.EntryHeader.EntryOffset = (int) stream.BaseStream.Position;

                        //Write the EntryData to disk which writes the Table offset as being
                        //immediately after itself.
                        rtblHeader.EntryHeader.Entry.WriteData(stream);
                        rtblHeader.EntryHeader.Entry.Table.WriteData(stream);
                    }
                    
                    //Then go back and write all of the rtblHeaders to disk now that we've set their offsets.
                    stream.BaseStream.Position = rtblHeaderOffset;
                    foreach (BaseChunk baseChunk in pair.Value)
                    {
                        baseChunk.WriteData(stream);
                    }

                    //Finally skip us to the next clear spot in the damn file.
                    stream.Seek(0, SeekOrigin.End);
                }
                else
                {
                    //Write all of the chunk data into the stream
                    for (int i = 0; i < pair.Value.Count; i++)
                    {
                        BaseChunk chunk = pair.Value[i];
                        chunk.WriteData(stream);
                    }
                }
            }

            //Now that we've created teh chunk headers and they have correct offsets set, lets go back
            //and write them to the actual file.
            stream.BaseStream.Position = chunkHeaderOffset;
            foreach (ChunkHeader chunkHeader in chunkHeaders)
            {
                chunkHeader.WriteData(stream);
            }
        }
Example #51
0
 public static string DeletingFileName(string fileName, int deleteVersion = 0)
 {
     return(FileHeader.Canonize(fileName) + (deleteVersion > 0 ? deleteVersion.ToString(CultureInfo.InvariantCulture) : string.Empty) + DeletingFileSuffix);
 }
Example #52
0
 internal FileInfoRarFilePart(FileInfoRarArchiveVolume volume, MarkHeader mh, FileHeader fh, FileInfo fi)
     : base(mh, fh, volume.Stream, volume.Password)
 {
     this.volume = volume;
     FileInfo    = fi;
 }
        /// <summary>
        /// Inserts the company name into the file's header.
        /// </summary>
        /// <param name="file">
        /// The file to insert the company name into.
        /// </param>
        public void InsertCompanyName(ICSharpFile file)
        {
            DocumentationRulesConfiguration docConfig = this.GetDocumentationRulesConfig(file);

            FileHeader fileHeader = new FileHeader(file) { CompanyName = docConfig.CompanyName };

            fileHeader.Update();
        }
Example #54
0
 public static string RenameOperationConfigNameForFile(string fileName)
 {
     return(RenameOperationConfigPrefix + FileHeader.Canonize(fileName));
 }
        /// <summary>
        /// Updates the existing header or inserts one if missing.
        /// </summary>
        /// <param name="file">
        /// THe file to check the header on.
        /// </param>
        public void InsertFileHeader(ICSharpFile file)
        {
            FileHeader fileHeader = new FileHeader(file);
            DocumentationRulesConfiguration docConfig = this.GetDocumentationRulesConfig(file);

            fileHeader.FileName = file.GetSourceFile().ToProjectFile().Location.Name;
            fileHeader.CompanyName = docConfig.CompanyName;
            fileHeader.CopyrightText = docConfig.Copyright;
            fileHeader.Summary = Utils.GetSummaryText(file);

            fileHeader.Update();
        }
Example #56
0
 public Task <Stream> DownloadFileAsync(FileHeader file)
 {
     return(sources[currentSource].DownloadFileAsync(file));
 }
        /// <summary>
        /// Inserts the file name into the file.
        /// </summary>
        /// <param name="file">
        /// The file to insert into.
        /// </param>
        public void InsertFileName(ICSharpFile file)
        {
            string fileName = file.GetSourceFile().ToProjectFile().Location.Name;

            FileHeader fileHeader = new FileHeader(file) { FileName = fileName };
            fileHeader.Update();
        }
 internal override RarFilePart CreateFilePart(FileHeader fileHeader, MarkHeader markHeader)
 {
     return(new NonSeekableStreamFilePart(markHeader, fileHeader, streamOwner));
 }
Example #59
0
        public override void Load(byte[] data)
        {
            int offset = 0;
            FileHeader header = new FileHeader();
            header.Load(data, ref offset);

            _chunkList = new Dictionary<Type, List<BaseChunk>>();

            var chnkHeaders = new List<ChunkHeader>();
            for (int i = 0; i < header.ChunkCount; i++)
            {
                ChunkHeader chunkHeader = new ChunkHeader();
                chunkHeader.Load(data, ref offset);
                chnkHeaders.Add(chunkHeader);
            }

            var orderedList = chnkHeaders.OrderBy(kvp => kvp.ChunkOffset);

            foreach (ChunkHeader chunkHeader in orderedList)
            {
                for (int k = 0; k < chunkHeader.ElementCount; k++)
                {
                    BaseChunk chunk; 

                    switch (chunkHeader.Tag.Substring(0, 3).ToUpper())
                    {
                        case "ENV": chunk = new EnvrChunk(); break; 
                        case "COL": chunk = new ColoChunk(); break;
                        case "PAL": chunk = new PaleChunk(); break;
                        case "VIR": chunk = new VirtChunk(); break;
                        case "SCL": chunk = new SclsChunk(); break;
                        case "PLY": chunk = new PlyrChunk(); break;
                        case "RPA": chunk = new RPATChunk(); break;
                        case "PAT": chunk = new PathChunk(); break;
                        case "RPP": chunk = new RppnChunk(); break;
                        case "PPN": chunk = new PpntChunk(); break;
                        case "SON": chunk = new SondChunk(); break;
                        case "FIL": chunk = new FiliChunk(); break;
                        case "MEC": chunk = new MecoChunk(); break;
                        case "MEM": chunk = new MemaChunk(); break;
                        case "TRE": chunk = new TresChunk(); break;
                        case "SHI": chunk = new ShipChunk(); break;
                        case "MUL": chunk = new MultChunk(); break;
                        case "LGH": chunk = new LghtChunk(); break;
                        case "LGT": chunk = new LgtvChunk(); break;
                        case "RAR": chunk = new RaroChunk(); break;
                        case "ARO": chunk = new ArobChunk(); break;
                        case "EVN": chunk = new EvntChunk(); break;
                        case "TGO": chunk = new TgobChunk(); break;
                        case "ACT": 
                            chunk = new ActrChunk();
                            if (!chunkHeader.Tag.ToUpper().EndsWith("R"))
                            {
                                chunk.ChunkLayer = EditorHelpers.ConvertStringToLayerId(chunkHeader.Tag.ToUpper().Substring(3, 1));
                            }
                            break;
                        case "SCO": 
                            chunk = new ScobChunk();
                            if (!chunkHeader.Tag.EndsWith("B"))
                            {
                                chunk.ChunkLayer = EditorHelpers.ConvertStringToLayerId(chunkHeader.Tag.ToUpper().Substring(3, 1));
                            }
                            break;
                        case "STA": chunk = new StagChunk(); break;
                        case "RCA": chunk = new RcamChunk(); break;
                        case "CAM": chunk = new CamrChunk(); break;
                        case "FLO": chunk = new FlorChunk(); break;
                        case "TWO": chunk = new TwoDChunk(); break;
                        case "2DM": chunk = new TwoDMAChunk(); break;
                        case "DMA": chunk = new DMAPChunk(); break;
                        case "LBN": chunk = new LbnkChunk(); break;
                        case "TGD": chunk = new TgdrChunk(); break;
                        case "RTB": chunk = new RTBLChunk(); break;
                        
                        default:
                            Console.WriteLine("Unsupported Chunk Tag: " + chunkHeader.Tag + " Chunk will not be saved!");
                            chunk = null;
                            break;
                    }

                    if(chunk == null)
                        continue;

                    //Console.WriteLine(chunkHeader.Tag + " offset: " + chunkHeader.ChunkOffset);
                    chunk.ChunkName = chunkHeader.Tag;
                    chunk.LoadData(data, ref chunkHeader.ChunkOffset);
                    AddChunk(chunk);
                }
            }

        }
Example #60
0
        /// <summary>
        /// Unpacks the packed raw image into <paramref name="image" />.
        /// </summary>
        private void UnpackImage(FileHeader fileHeader, ByteMemoryArea image)
        {
            var w = new LeImageWriter(image.Bytes);
            //
            // Still looking for additional information on Pharlap file packing
            //
            // Packing implemented is currently based on reviewing code and interpretation of data against loading program in debugger.
            // Record is 16 bit word.
            // If bit 15 is clear ie 0-7FFF, load the next record number of bytes into memory
            //
            // If bit 15 is set, ie 8000-FFFF use value lower 15 bits for size of repeat area
            // Next byte (dataSize) defines size of item to be repeated
            // If dataSize size is larger than size of repeat area, corrupt file
            // if dataSize is 0, then is either fill with zero or skip, size of repeat area
            // Read itemSize number of bytes for the repeatData
            // copying repeatData until filled size of repeat area
            //

            var rdr = new LeImageReader(RawImage, FileHeaderOffset + fileHeader.offset_load_image);

            while (w.Position < fileHeader.memory_requirements)
            {
                if (!rdr.TryReadUInt16(out ushort us))
                {
                    throw new BadImageFormatException("Unexpected EOF while loading program.");
                }
                if ((us & 0x8000) == 0)
                {
                    rdr.ReadBytes(w.Bytes, w.Position, us);
                    w.Position += us;
                }
                else
                {
                    us &= 0x7FFF;
                    if (!rdr.TryReadByte(out var dataSize))
                    {
                        throw new BadImageFormatException("Unexpected EOF while loading program.");
                    }
                    if (dataSize > us)
                    {
                        throw new BadImageFormatException("Corrupt file");  // Corrupt file, Repeated data shouldn't be bigger than size of repeat block
                    }
                    if (dataSize == 0)
                    {
                        for (int i = 0; i < us; ++i)
                        {
                            w.WriteByte(dataSize);
                        }
                    }
                    else
                    {
                        var repeatData = new byte[dataSize];
                        for (int i = 0; i < dataSize; ++i)
                        {
                            if (!rdr.TryReadByte(out var b))
                            {
                                throw new BadImageFormatException("Unexpected EOF while loading program.");
                            }
                            repeatData[i] = b;
                        }
                        for (int i = 0; i < us; i += dataSize)
                        {
                            w.WriteBytes(repeatData);
                        }
                    }
                }
            }
        }