private static VBRData getXingInfo(BufferedBinaryReader source) { VBRData result = new VBRData(); byte[] data = new byte[8]; result.Found = true; result.ID = VBR_ID_XING.ToCharArray(); source.Seek(4, SeekOrigin.Current); source.Read(data, 0, 8); result.Frames = data[0] * 0x1000000 + data[1] * 0x10000 + data[2] * 0x100 + data[3]; result.Bytes = data[4] * 0x1000000 + data[5] * 0x10000 + data[6] * 0x100 + data[7]; source.Seek(103, SeekOrigin.Current); result.Scale = source.ReadByte(); source.Read(data, 0, 8); result.VendorID = Utils.Latin1Encoding.GetString(data, 0, 8); return(result); }
private static VBRData findVBR(BufferedBinaryReader source, long position) { VBRData result; byte[] data = new byte[4]; // Check for VBR header at given position source.Seek(position, SeekOrigin.Begin); source.Read(data, 0, 4); string vbrId = Utils.Latin1Encoding.GetString(data); if (VBR_ID_XING.Equals(vbrId)) { result = getXingInfo(source); } else if (VBR_ID_FHG.Equals(vbrId)) { result = getFhGInfo(source); } else { result = new VBRData(); result.Reset(); } return(result); }
private static VBRData getFhGInfo(BufferedBinaryReader source) { VBRData result = new VBRData(); byte[] data = new byte[9]; // Extract FhG VBR info at given position result.Found = true; result.ID = VBR_ID_FHG.ToCharArray(); source.Seek(5, SeekOrigin.Current); source.Read(data, 0, 9); result.Scale = data[0]; result.Bytes = data[1] * 0x1000000 + data[2] * 0x10000 + data[3] * 0x100 + data[4]; result.Frames = data[5] * 0x1000000 + data[6] * 0x10000 + data[7] * 0x100 + data[8]; result.VendorID = ""; return(result); }
private protected override IEnumerable <float> DoEnumerateDataValues(BufferedBinaryReader reader, DataSection dataSection, long dataPointsNumber) { var data = reader.Read((int)dataSection.DataLength); var img = J2kImage.FromBytes(data); if (img.NumberOfComponents <= 0) { return(new float[0]); } var values = img.GetComponent(0); return(Unpack(values)); }
// ********************* Auxiliary functions & voids ******************** private Boolean ReadTag(BufferedBinaryReader source) { var result = false; // Read tag source.Seek(-ID3V1_TAG_SIZE, SeekOrigin.End); // ID3v1 tags are C-String(null-terminated)-based tags encoded in ASCII var data = new Byte[ID3V1_TAG_SIZE]; source.Read(data, 0, ID3V1_TAG_SIZE); var header = Utils.Latin1Encoding.GetString(data, 0, 3); if (header.Equals(ID3V1_ID)) { var endComment = new Byte[2]; structureHelper.AddZone(source.Position - ID3V1_TAG_SIZE, ID3V1_TAG_SIZE); tagData.Title = Utils.Latin1Encoding.GetString(data, 3, 30).Replace("\0", ""); tagData.Artist = Utils.Latin1Encoding.GetString(data, 33, 30).Replace("\0", ""); tagData.Album = Utils.Latin1Encoding.GetString(data, 63, 30).Replace("\0", ""); tagData.RecordingYear = Utils.Latin1Encoding.GetString(data, 93, 4).Replace("\0", ""); tagData.Comment = Utils.Latin1Encoding.GetString(data, 97, 28).Replace("\0", ""); Array.Copy(data, 125, endComment, 0, 2); tagVersion = GetTagVersion(endComment); // Fill properties using tag data if (TAG_VERSION_1_0 == tagVersion) { Comment = tagData.Comment + Utils.Latin1Encoding.GetString(endComment, 0, 2).Replace("\0", ""); } else { Comment = tagData.Comment; Track = endComment[1]; } tagData.Genre = (data[127] < MAX_MUSIC_GENRES) ? MusicGenre[data[127]] : ""; result = true; } return(result); }
/// <summary> /// Inflate compressed swf /// </summary> private void Inflate() { // read size br.BaseStream.Position = 4; // skip signature int size = Convert.ToInt32(br.ReadUInt32()); // read swf head byte[] uncompressed = new byte[size]; br.BaseStream.Position = 0; br.Read(uncompressed, 0, 8); // header data is not compress // un-zip byte[] compressed = br.ReadBytes(size); uncompressed = DeflatorWraper.Decompress(compressed); // new memory stream for uncompressed swf MemoryStream m = new MemoryStream(uncompressed); br = new BufferedBinaryReader(m); br.BaseStream.Position = 0; }
internal static IndicatorSection BuildFrom(BufferedBinaryReader reader) { var fileStart = reader.Read(Constants.GribFileStart.Length); if (!Constants.GribFileStart.SequenceEqual(fileStart)) { throw new BadGribFormatException("Invalid file start."); } // Ignore the 2 reserved bytes reader.Skip(2); var disciplineNumber = reader.ReadUInt8(); var gribEdition = reader.ReadUInt8(); if (gribEdition != 2) { throw new NotSupportedException($"Only GRIB edition 2 is supported. GRIB edition {gribEdition} is not yet supported"); } var totalLength = reader.ReadUInt64(); return(new IndicatorSection(disciplineNumber, 2, totalLength)); }
// --------------------------------------------------------------------------- // Read total samples of OGG file, which are located on the very last page of the file private ulong getSamples(BufferedBinaryReader source) { OggHeader header = new OggHeader(); string headerId; byte typeFlag; byte[] lacingValues = new byte[255]; byte nbLacingValues = 0; long nextPageOffset = 0; // TODO - fine tune seekSize value int seekSize = (int)Math.Round(MAX_PAGE_SIZE * 0.75); if (seekSize > source.Length) { seekSize = (int)Math.Round(source.Length * 0.5); } source.Seek(-seekSize, SeekOrigin.End); if (!StreamUtils.FindSequence(source, Utils.Latin1Encoding.GetBytes(OGG_PAGE_ID))) { LogDelegator.GetLogDelegate()(Log.LV_ERROR, "No OGG header found; aborting read operation"); // Throw exception ? return(0); } source.Seek(-4, SeekOrigin.Current); // Iterate until last page is encountered do { if (source.Position + nextPageOffset + 27 > source.Length) // End of stream about to be reached => last OGG header did not have the proper type flag { break; } source.Seek(nextPageOffset, SeekOrigin.Current); headerId = Utils.Latin1Encoding.GetString(source.ReadBytes(4)); if (headerId.Equals(OGG_PAGE_ID)) { source.Seek(1, SeekOrigin.Current); typeFlag = source.ReadByte(); source.Seek(20, SeekOrigin.Current); nbLacingValues = source.ReadByte(); nextPageOffset = 0; source.Read(lacingValues, 0, nbLacingValues); for (int i = 0; i < nbLacingValues; i++) { nextPageOffset += lacingValues[i]; } } else { LogDelegator.GetLogDelegate()(Log.LV_ERROR, "Invalid OGG header found while looking for total samples; aborting read operation"); // Throw exception ? return(0); } } while (0 == (typeFlag & 0x04)); // 0x04 marks the last page of the logical bitstream // Stream is positioned at the end of the last page header; backtracking to read AbsolutePosition field source.Seek(-nbLacingValues - 21, SeekOrigin.Current); return(source.ReadUInt64()); }
private static FrameHeader findFrame(BufferedBinaryReader source, ref VBRData oVBR, SizeInfo sizeInfo) { byte[] headerData = new byte[4]; FrameHeader result = new FrameHeader(); source.Read(headerData, 0, 4); result.Found = isValidFrameHeader(headerData); /* * Many things can actually be found before a proper MP3 header : * - Padding with 0x55, 0xAA and even 0xFF bytes * - RIFF header declaring either MP3 or WAVE data * - Xing encoder-specific frame * - One of the above with a few "parasite" bytes before their own header * * The most solid way to deal with all of them is to "scan" the file until proper MP3 header is found. * This method may not the be fastest, but ensures audio data is actually detected, whatever garbage lies before */ if (!result.Found) { // "Quick win" for files starting with padding bytes // 4 identical bytes => MP3 starts with padding bytes => Skip padding if ((headerData[0] == headerData[1]) && (headerData[1] == headerData[2]) && (headerData[2] == headerData[3])) { // Scan the whole padding until it stops while (headerData[0] == source.ReadByte()) { ; } source.Seek(-1, SeekOrigin.Current); // If padding uses 0xFF bytes, take one step back in case MP3 header lies there if (0xFF == headerData[0]) { source.Seek(-1, SeekOrigin.Current); } source.Read(headerData, 0, 4); result.Found = isValidFrameHeader(headerData); } // Blindly look for the MP3 header if (!result.Found) { source.Seek(-4, SeekOrigin.Current); long limit = sizeInfo.ID3v2Size + (long)Math.Round((source.Length - sizeInfo.ID3v2Size) * 0.3); // Look for the beginning of the MP3 header (2nd byte is variable, so it cannot be searched that way) while (!result.Found && source.Position < limit) { while (0xFF != source.ReadByte() && source.Position < limit) { ; } source.Seek(-1, SeekOrigin.Current); source.Read(headerData, 0, 4); result.Found = isValidFrameHeader(headerData); // Valid header candidate found // => let's see if it is a legit MP3 header by using its Size descriptor to find the next header if (result.Found) { result.LoadFromByteArray(headerData); result.Position = source.Position - 4; result.Size = getFrameSize(result); byte[] nextHeaderData = new byte[4]; source.Seek(result.Position + result.Size, SeekOrigin.Begin); source.Read(nextHeaderData, 0, 4); result.Found = isValidFrameHeader(nextHeaderData); if (result.Found) { source.Seek(result.Position + 4, SeekOrigin.Begin); // Go back to header candidate position break; } else { // Restart looking for a candidate source.Seek(result.Position + 1, SeekOrigin.Begin); } } else { source.Seek(-3, SeekOrigin.Current); } } } } if (result.Found) { result.LoadFromByteArray(headerData); result.Position = source.Position - 4; // result.Xing = isXing(i + 4, Data); // Will look into it when encoder ID is needed by upper interfaces // Look for VBR signature oVBR = findVBR(source, result.Position + getVBRDeviation(result)); } return(result); }
/// <summary> /// Загрузка базы данных в память /// </summary> /// <returns></returns> public void Load() { string filePath = Path.Combine(_environment.ContentRootPath, _dbFileName); BinGeoModel binModel; using (FileStream stream = new FileStream(filePath, FileMode.Open)) { Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew(); using (BufferedBinaryReader bufferedReader = new BufferedBinaryReader(stream, 65536)) { stopwatch.Start(); bufferedReader.FillBuffer(); int version = bufferedReader.ReadInt32(); byte[] nameBytes = bufferedReader.Read(0, 32); ulong timestamp = bufferedReader.ReadUInt64(); int records = bufferedReader.ReadInt32(); uint offsetRanges = bufferedReader.ReadUInt32(); uint offsetCities = bufferedReader.ReadUInt32(); uint offsetLocations = bufferedReader.ReadUInt32(); binModel = new BinGeoModel(version, nameBytes, timestamp, records, offsetRanges, offsetCities, offsetLocations); int currentIndex = 0; binModel.IpRangeCollection = new BinIpRange[binModel.RecordsCount]; while (bufferedReader.FillBuffer() && currentIndex < binModel.RecordsCount) { for (; currentIndex < binModel.RecordsCount && bufferedReader.NumBytesAvailable >= IpRangeBytesCount; currentIndex++) { binModel.IpRangeCollection[currentIndex] = new BinIpRange() { IpFrom = bufferedReader.ReadUInt32(), IpTo = bufferedReader.ReadUInt32(), LocationIndex = bufferedReader.ReadUInt32() }; } } currentIndex = 0; binModel.LocationCollection = new BinLocation[binModel.RecordsCount]; while (bufferedReader.FillBuffer() && currentIndex < binModel.RecordsCount) { for (; currentIndex < binModel.RecordsCount && bufferedReader.NumBytesAvailable >= LocationBytesCount; currentIndex++) { byte[] country = bufferedReader.Read(0, 8); byte[] region = bufferedReader.Read(0, 12); byte[] postal = bufferedReader.Read(0, 12); byte[] city = bufferedReader.Read(0, 24); byte[] organization = bufferedReader.Read(0, 32); float latitude = bufferedReader.ReadSingle(); float longitude = bufferedReader.ReadSingle(); binModel.LocationCollection[currentIndex] = new BinLocation(country, region, postal, city, organization, latitude, longitude); } } currentIndex = 0; binModel.Indexes = new uint[binModel.RecordsCount]; while (bufferedReader.FillBuffer() && currentIndex < binModel.RecordsCount) { for (; currentIndex < binModel.RecordsCount && bufferedReader.NumBytesAvailable >= IndexBytesCount; currentIndex++) { binModel.Indexes[currentIndex] = bufferedReader.ReadUInt32(); } } stopwatch.Stop(); DatabaseLoadedTimeMs = stopwatch.ElapsedMilliseconds; _logger.LogInformation($"Database loading time: {stopwatch.ElapsedMilliseconds} ms"); } // Отображение объектов сущностей двоичной базы в объекты бизнес сущностей GeoModel = _mapper.Map <GeoModel>(binModel); } }