/// <summary> /// Returns this DBF header in it's original raw binary data form. /// </summary> /// <returns>An Array of type System.Byte[]</returns> public byte[] GetDbfHeader() { try { ByteCollection bytes = new ByteCollection(32 + this._flds.Count * 32); // Keep the same file type ID. bytes.Add(this._id); // Then we right the last time the DBF was updated. byte lstUpYr = (byte)(this._lastUpd.Year - 1900), lstUpMn = (byte)this._lastUpd.Month, lstUpDy = (byte)this._lastUpd.Day; bytes.AddRange(lstUpYr, lstUpMn, lstUpDy); // Next comes the number of records in the table. byte[] recCnt = Hex.GetBytes(Hex.ToHex(this._recCount)); Array.Reverse(recCnt); bytes.AddRange(recCnt); // Now, the offset to the actual data. This is the header data (32 bytes), plus // the field headers (each field header is 32 bytes) plus 1 byte for the // header termination character. byte[] datOfst = Hex.GetBytes(Hex.ToHex(this._dataOffset)); Array.Reverse(datOfst); bytes.AddRange(datOfst); // Then, the size of each record in the table. byte[] recSz = Hex.GetBytes(Hex.ToHex(this._recSize)); Array.Reverse(recSz); bytes.AddRange(recSz); // Next, comes 16 "empty" bytes. byte[] empBts = new byte[16]; empBts.Initialize(); bytes.AddRange(empBts); // Then the CDX structure flag. bytes.Add(Convert.ToByte(this._cdxStruct)); // Then three (3) more "empty" bytes. empBts = new byte[3]; empBts.Initialize(); bytes.AddRange(empBts); // Finally, we write the file headers. for (int i = 0; i < this._flds.Count; i++) { bytes.AddRange(this._flds[0].GetFieldHeader()); } // Don't forget the header terminator character. bytes.Add(Convert.ToByte("0x0D")); // And... we're done ;) return(bytes.ToArray()); } catch { throw; } }
private void bFromFile_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() != DialogResult.OK) { return; } byte[] newdata = File.ReadAllBytes(openFileDialog1.FileName); bytes.Clear(); bytes.AddRange(newdata); hexBox1.Refresh(); }
public byte[] GetRawHeader() { ByteCollection hdr = new ByteCollection(); foreach (DbfField fld in this.List) { hdr.AddRange(fld.GetFieldHeader()); } hdr.Add(0x0D); return(hdr.ToArray()); }
public static byte[] GetBinary(DateTime value) { ByteCollection bytes = new ByteCollection(); bytes.AddRange(Hex.GetBytes(value.ToString("yyyy"))); bytes.AddRange(Hex.GetBytes(value.ToString("MM"))); bytes.AddRange(Hex.GetBytes(value.ToString("dd"))); bytes.AddRange(Hex.GetBytes(value.ToString("HH"))); bytes.AddRange(Hex.GetBytes(value.ToString("mm"))); bytes.AddRange(Hex.GetBytes(value.ToString("ss"))); return(bytes.ToArray()); }
public byte[] GetRawHeader() { ByteCollection hdr = new ByteCollection(); foreach (DbfField fld in this.List) hdr.AddRange(fld.GetFieldHeader()); hdr.Add(0x0D); return hdr.ToArray(); }
//*************************************************************************** // Public Methods // //public void ReadFile(string filename) //{ // using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) // this.ReadFile(fs); //} public void ReadFile(string filename) { this.LoadFile = filename; FileStream fs = null; BinaryReader br = null; try { fs = new FileStream(filename, FileMode.Open, FileAccess.Read); br = new BinaryReader(fs); // The first four bytes are mostly irrelevant. Byte offsets 0 & 1 // *should* always be the same and can contain one of two values: // 49 49 = _II_ = Little-Endian byte format // 4D 4D = _MM_ = Big-Endian byte format // Byte offsets 2-3 will *always* contain the value 2A 00 (assuming // little-endian), which is the Short integer value 42. This is // the "version" number of the file intended only to identify // the file as a TIFF image. According to the Aldus format spec, // this number was chosen for its deep philosophical significance. byte[] header = br.ReadBytes(4); this._littleEndian = (header[0] == 0x49); int verNum = Hex.GetInteger(new byte[] { header[2], header[3] }, this._littleEndian); if (verNum != 42) { throw new Exception("File does not appear to be a valid TIFF image."); } // The next four bytes contain the byte offset of the first IDF // (Image Data Folder) in the file. int idfOffset = Hex.GetInteger(br.ReadBytes(4), this._littleEndian); // A single file can contain multiple IDF's, so we have to keep // reading until we encounter a null pointer ('00 00 00 00') // for the next IDF offset. bool eofIDF = false; while (!eofIDF) { // Seek to the next IDF position. if (!br.BaseStream.CanSeek) { throw new Exception("Unable to read data from stream. Stream cannot seek."); } br.BaseStream.Seek(idfOffset, SeekOrigin.Begin); // Once we jump to that offset, we find a Short value specifying // the number of tags in this IDF. int tagCnt = Hex.GetInteger(br.ReadBytes(2), this._littleEndian); // Now we're ready to start grabbing the IDF data stream, which // consists of 'tagCnt' sets of 12 bytes. ByteCollection idfStrm = new ByteCollection(); for (int i = 0; i < tagCnt; i++) { idfStrm.AddRange(br.ReadBytes(12)); } // Once we've gotten the data stream, create a new TiffIdf // object with it. this._idfs.Add(new TiffIdf(this, idfStrm.ToArray())); // After we've read the IDF's tag data, the next four bytes // contain a Long value containing a pointer to the next // IDF in the file ('0' if no more IDF's exist). idfOffset = Hex.GetInteger(br.ReadBytes(4), this._littleEndian); eofIDF = (idfOffset == 0); } } catch (Exception ex) { throw new Exception("Unable to load binary TIFF file.", ex); } finally { if (br != null) { br.Close(); } if (fs != null) { fs.Dispose(); } } }
//*************************************************************************** // Public Methods // public byte[] GetValue() { ByteCollection retVal = new ByteCollection(); if (this.ValueIsPointer && this._owner.Owner.GetType().Name == typeof(TiffReader).Name) { // The tag value contains a pointer to a byte address in the // original file where the literal data is stored. #region Read data from file FileStream fs = null; BinaryReader br = null; try { // Get a stream and binary reader for the file this tag // belongs to. fs = new FileStream(((TiffReader)this._owner.Owner).LoadFile, FileMode.Open, FileAccess.Read); br = new BinaryReader(fs); // The Value property will return an integer value containing // the byte offset of the actual data in the file, so the // first thing we need to do is jump to that byte address. br.BaseStream.Seek(this.Value, SeekOrigin.Begin); // Now the read cursor's in the right spot, so we just have to // read however many fields of whatever byte-length. for (int i = 0; i < this.FieldCount; i++) { byte[] val; switch (this.FieldType) { case TiffFieldType.ByteType: val = br.ReadBytes(1); break; case TiffFieldType.AsciiType: val = br.ReadBytes(1); break; case TiffFieldType.ShortType: val = br.ReadBytes(2); break; case TiffFieldType.LongType: val = br.ReadBytes(4); break; case TiffFieldType.Rational: // The Rational field type takes a little extra work, // since it's actually two different long values // per field entry. The first value represents the // Numerator, while the second value is the // Denominator. // NOTE: These values are always in sequencial byte- // order, so we don't use Array.Reverse(). byte[] num = br.ReadBytes(4); byte[] den = br.ReadBytes(4); // We have to initialized the 'val' variable to an // empty array before we can use the // Array.Copy() function on it. val = new byte[8]; // Now, we just copy the two byte arrays into the // output array. Array.Copy(num, 0, val, 0, 4); Array.Copy(den, 0, val, 4, 4); break; default: // If we somehow have a different value for the // Fieldtype (which *shouldn't* be possible), // populate the 'val' variable with an empty // array to ensure we don't needlessly throw a // NullReference exception when we try to check // the length of the array. val = new byte[0]; break; } // End Switch(this.FieldType) // Don't forget to swap the byte orders if we're working with // a little-endian file (which will be 90% of the time). // NOTE: We don't want to swap the byte-order of Rational // field types. if (val.Length > 1 && this.FieldType != TiffFieldType.Rational && this._owner.Owner.LittleEndian) { Array.Reverse(val); } // And, finally, add the value to the collection. if (val.Length > 0) { retVal.AddRange(val); } } // Next i } // End Try catch { // Just throw the exception back up the call stack so that the // calling function can deal with it. throw; } finally { // Make sure we release the BinaryReader and FileStream objects. if (br != null) { br.Close(); } if (fs != null) { fs.Dispose(); } } #endregion } else if (this.ValueIsPointer) { // This tag is part of an IDF belonging to a TiffWriter object, so // we need to look for the real data in the TiffWriter's // _metaData field. int metaOffset = this.Value; for (int i = 0; i < this.FieldCount; i++) { // We still need to be concious of what type of data we're // getting from the binary stream. switch (this.FieldType) { case TiffFieldType.AsciiType: retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + i]); break; case TiffFieldType.ByteType: retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + i]); break; case TiffFieldType.ShortType: retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 2)]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 2) + 1]); break; case TiffFieldType.LongType: retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 4)]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 4) + 1]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 4) + 2]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 4) + 3]); break; case TiffFieldType.Rational: retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8)]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8) + 1]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8) + 2]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8) + 3]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8) + 4]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8) + 5]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8) + 6]); retVal.Add(((TiffWriter)this._owner.Owner)._metaData[metaOffset + (i * 8) + 7]); break; } } } else { // The tag value contains the literal data, not a pointer, so we // just need to pull the data straight from the binary data // we're already holding. #region Read data from tag if (this.FieldType == TiffFieldType.AsciiType || this.FieldType == TiffFieldType.ByteType) { // We're just retrieving single byte values, so just dump them // into the ByteCollection in order. for (int i = 0; i < this.FieldCount; i++) { retVal.Add(this._raw[i + 8]); } } else if (this.FieldType == TiffFieldType.ShortType) { // Here, we read either one or two 2-byte integer values. for (int i = 0; i < this.FieldCount; i++) { byte[] val = new byte[] { this._raw[(i * 2) + 8], this._raw[(i * 2) + 9] }; if (this._owner.Owner.LittleEndian) { Array.Reverse(val); } retVal.AddRange(val); } } else { // If we hit here, we must be pulling a Long value, which means // we there can only be a single field value. byte[] val = new byte[4]; for (int i = 0; i < val.Length; i++) { val[i] = this._raw[i + 8]; } if (this._owner.Owner.LittleEndian) { Array.Reverse(val); } retVal.AddRange(val); } #endregion } return(retVal.ToArray()); }
public void SetTag(int idfNum, TiffTagType tagType, params object[] value) { if (tagType == TiffTagType.StripOffsets || tagType == TiffTagType.StripByteCounts) { throw new Exception("The 'StripOffsets' and 'StripByteCounts' tags will be constructed automatically and may not be manually configured."); } ByteCollection tagBts = new ByteCollection(); tagBts.AddRange(Hex.GetBinary((short)tagType, this._littleEndian)); TiffFieldType fldType = (TiffFieldType)Enum.Parse(typeof(TiffTagIDFieldType), tagType.ToString()); tagBts.AddRange(Hex.GetBinary((short)fldType, this._littleEndian)); tagBts.AddRange(Hex.GetBinary(value.Length, this._littleEndian)); if (TiffTag.MustBePointer(fldType, value.Length) || fldType == TiffFieldType.AsciiType) { // We'll have to add the data to the _metaData ByteCollection, // since it won't fit into the tag itself. We'll also need // to make a pointer to the data in the tag. int metaPointer = this._metaData.Count; ByteCollection tmpMetaData = new ByteCollection(); // We need to process each passed value. Often there may be only // one, but we still have to be prepared for multiple values. for (int i = 0; i < value.Length; i++) { try { // First, we need to figure out what kind of data we're deeling with, // since it will determine what we do with it. switch (fldType) { case TiffFieldType.AsciiType: // ASCII type is easy... we just convert the text to ASCII // bytes and dump them to the metaData collection. tmpMetaData.AddRange(System.Text.ASCIIEncoding.ASCII.GetBytes(value[i].ToString())); break; case TiffFieldType.ByteType: tmpMetaData.Add((byte)value[i]); break; case TiffFieldType.ShortType: tmpMetaData.AddRange(Hex.GetBinary((short)value[i], this._littleEndian)); break; case TiffFieldType.LongType: tmpMetaData.AddRange(Hex.GetBinary((int)value[i], this._littleEndian)); break; case TiffFieldType.Rational: string[] vals = ((string)value[i]).Split('/', '|'); tmpMetaData.AddRange(Hex.GetBinary(int.Parse(vals[0]))); tmpMetaData.AddRange(Hex.GetBinary(int.Parse(vals[1]))); break; } } catch { // The most likely reson for ending up here is that we got // passed some data that didn't match the field type for // the specified field. Just chunk the error back up the // stack and let the caller deal with it. throw; } // We don't want to add the values to the actuall metaData // collection until we're sure we've got it all processes // and converted. Otherwise, we might end up wtih 'orphaned' // meta data. this._metaData.AddRange(tmpMetaData.ToArray()); tagBts.AddRange(Hex.GetBinary(metaPointer, this._littleEndian)); } } else { // The tag value is small enough to reside inside the tag itself. // This automatically rules out certain data types such as // Rational and ASCII, so we should only be dealing with // Byte, Short, and Long here. for (int i = 0; i < value.Length; i++) { if (value[i].GetType().Name.ToLower() == "int32") { tagBts.AddRange(Hex.GetBinary((short)value[i], this._littleEndian)); } else if (value[i].GetType().Name.ToLower() == "int64") { tagBts.AddRange(Hex.GetBinary((int)value[i], this._littleEndian)); } } } // If we ended up with less than 12 bytes for the tag data, it means // we're storing a short or byte value that doesn't 'fill' the tag's // value space, so we just need to add 'padding'. int btShort = 12 - tagBts.Count; for (int t = 0; t < btShort; t++) { tagBts.Add(byte.Parse("00")); } // Now, we're read to add the tag to the IDF. this._idfs[idfNum].Tags.Add(new TiffTag(this._idfs[idfNum], tagBts.ToArray())); }
public static byte[] GetBinary(DateTime value) { ByteCollection bytes = new ByteCollection(); bytes.AddRange(Hex.GetBytes(value.ToString("yyyy"))); bytes.AddRange(Hex.GetBytes(value.ToString("MM"))); bytes.AddRange(Hex.GetBytes(value.ToString("dd"))); bytes.AddRange(Hex.GetBytes(value.ToString("HH"))); bytes.AddRange(Hex.GetBytes(value.ToString("mm"))); bytes.AddRange(Hex.GetBytes(value.ToString("ss"))); return bytes.ToArray(); }
/// <summary> /// Returns this DBF header in it's original raw binary data form. /// </summary> /// <returns>An Array of type System.Byte[]</returns> public byte[] GetDbfHeader() { try { ByteCollection bytes = new ByteCollection(32 + this._flds.Count * 32); // Keep the same file type ID. bytes.Add(this._id); // Then we right the last time the DBF was updated. byte lstUpYr = (byte)(this._lastUpd.Year - 1900), lstUpMn = (byte)this._lastUpd.Month, lstUpDy = (byte)this._lastUpd.Day; bytes.AddRange(lstUpYr, lstUpMn, lstUpDy); // Next comes the number of records in the table. byte[] recCnt = Hex.GetBytes(Hex.ToHex(this._recCount)); Array.Reverse(recCnt); bytes.AddRange(recCnt); // Now, the offset to the actual data. This is the header data (32 bytes), plus // the field headers (each field header is 32 bytes) plus 1 byte for the // header termination character. byte[] datOfst = Hex.GetBytes(Hex.ToHex(this._dataOffset)); Array.Reverse(datOfst); bytes.AddRange(datOfst); // Then, the size of each record in the table. byte[] recSz = Hex.GetBytes(Hex.ToHex(this._recSize)); Array.Reverse(recSz); bytes.AddRange(recSz); // Next, comes 16 "empty" bytes. byte[] empBts = new byte[16]; empBts.Initialize(); bytes.AddRange(empBts); // Then the CDX structure flag. bytes.Add(Convert.ToByte(this._cdxStruct)); // Then three (3) more "empty" bytes. empBts = new byte[3]; empBts.Initialize(); bytes.AddRange(empBts); // Finally, we write the file headers. for (int i = 0; i < this._flds.Count; i++) bytes.AddRange(this._flds[0].GetFieldHeader()); // Don't forget the header terminator character. bytes.Add(Convert.ToByte("0x0D")); // And... we're done ;) return bytes.ToArray(); } catch { throw; } }