Download Horizon :: Staff Members :: Save Vault :: XboxMB YouTube


Old 04-15-2011   #1 (permalink)
Experiment5X's Avatar
BlueJ
Join Date: Sep 2010
Location: New York
Posts: 1,343
Thanks: 1,031
Default Add bytes to a file?

Hi, I was wondering how I could add bytes to a file. For example I want to add let's say 16 bytes to the file at the offset 0x20. I don't want to write over existing ones, I need to add them to the file so it will be larger. Any help would be appreciated, thanks.
__________________
Experiment5X is online now Send a message via AIM to Experiment5X
Reply With Quote


Old 04-15-2011   #2 (permalink)
Regular Member
Retrix's Avatar
Join Date: Nov 2010
Location: Maryland
Posts: 1,915
Thanks: 1,191
Default Re: Add bytes to a file?

What kind of file?
be more elaborate
__________________
Retrix is offline
Reply With Quote


Old 04-15-2011   #3 (permalink)
Experiment5X's Avatar
BlueJ
Join Date: Sep 2010
Location: New York
Posts: 1,343
Thanks: 1,031
Default Re: Add bytes to a file?

Quote:
Originally Posted by Retrix View Post
What kind of file?
be more elaborate
Any file.

This:


To this:
__________________
Experiment5X is online now Send a message via AIM to Experiment5X
Reply With Quote




Old 04-15-2011   #4 (permalink)
Cheater912's Avatar
x &= ~0;
Join Date: Jul 2010
Location: New York
Posts: 6,680
Thanks: 16,730
Default Re: Add bytes to a file?

You can't really. You shouldn't have to either.

You have to make a new file or overwrite everything.
__________________
Cheater912 is offline
Reply With Quote
The following user thanked this post: Experiment5X


Old 04-15-2011   #5 (permalink)

Oscar's Avatar
Join Date: Oct 2010
Location: UK
Posts: 1,071
Thanks: 1,464
Default Re: Add bytes to a file?

If the file isn't to big you could probably just get away with reading the file into the memory from the offset to EOF, writing the bytes you want to insert then writing the end of the file after that.

Code:
var iostream = File.Open("file.bin", FileMode.Open);
byte[] buffer = new byte[iostream.Length - 0x20];

iostream.Seek(0x20, SeekOrigin.Begin);
iostream.Read(buffer, 0, buffer.Length);
iostream.Seek(0x20, SeekOrigin.Begin);
iostream.Write(new byte[16], 0, 16); //bytes to insert
iostream.Write(buffer, 0, buffer.Length);
iostream.Close();
It's still kinda improper, always best to look for an alternative way.
Oscar is offline Send a message via AIM to Oscar
Reply With Quote
The following users thanked this post: Experiment5X, KeyboardCat


Old 04-17-2011   #6 (permalink)
Eaton's Avatar
Join Date: Sep 2010
Location: USA
Posts: 488
Thanks: 594
Default Re: Add bytes to a file?

Code:
        /// <summary>
        /// Inserts bytes into the given stream.
        /// </summary>
        /// <param name="Source">The stream to insert bytes into.</param>
        /// <param name="Offset">Position to insert bytes into.</param>
        /// <param name="Data">Bytes to insert.</param>
        public static byte[] InsertBytes(Stream Source, int Offset, byte[] Data)
        {
            using (var io = new EndianIO(Source, EndianType.LittleEndian))
            {
                io.Open();
                var firstArray = io.Reader.ReadBytes(Offset);
                var secondArray = io.Reader.ReadBytes((int)io.Stream.Length - (int)io.Position);
                var newArray = new byte[Source.Length + Data.Length];
                Buffer.BlockCopy(firstArray, 0, newArray, 0, firstArray.Length);
                Buffer.BlockCopy(Data, 0, newArray, firstArray.Length, Data.Length);
                Buffer.BlockCopy(secondArray, 0, newArray, firstArray.Length + Data.Length, secondArray.Length);
                return newArray;
            }
        }

        /// <summary>
        /// Inserts bytes into the given byte array.
        /// </summary>
        /// <param name="Source">The byte array to insert bytes into.</param>
        /// <param name="Offset">Position to insert bytes into.</param>
        /// <param name="Data">Bytes to insert.</param>
        public static byte[] InsertBytes(byte[] Source, int Offset, byte[] Data)
        {
            using (var io = new EndianIO(Source, EndianType.LittleEndian))
            {
                io.Open();
                var firstArray = io.Reader.ReadBytes(Offset);
                var secondArray = io.Reader.ReadBytes((int)io.Stream.Length - (int)io.Position);
                var newArray = new byte[Source.Length + Data.Length];
                Buffer.BlockCopy(firstArray, 0, newArray, 0, firstArray.Length);
                Buffer.BlockCopy(Data, 0, newArray, firstArray.Length, Data.Length);
                Buffer.BlockCopy(secondArray, 0, newArray, firstArray.Length + Data.Length, secondArray.Length);
                return newArray;
            }
        }

        /// <summary>
        /// Deletes bytes from the given stream.
        /// </summary>
        /// <param name="Source">The stream to delete bytes from.</param>
        /// <param name="Offset">Position to delete bytes from.</param>
        /// <param name="Size">How many bytes to delete.</param>
        public static byte[] DeleteBytes(Stream Source, int Offset, int Size)
        {
            using (var io = new EndianIO(Source, EndianType.LittleEndian))
            {
                io.Open();
                var firstArray = io.Reader.ReadBytes(Offset);
                io.SetPosition(Size, SeekOrigin.Current);
                var secondArray = io.Reader.ReadBytes((int)io.Stream.Length - (int)io.Position);
                var newArray = new byte[Source.Length - Size];
                Buffer.BlockCopy(firstArray, 0, newArray, 0, firstArray.Length);
                Buffer.BlockCopy(secondArray, 0, newArray, firstArray.Length, secondArray.Length);
                return newArray;
            }
        }

        /// <summary>
        /// Deletes bytes from the given byte array.
        /// </summary>
        /// <param name="Source">The byte array to delete bytes from.</param>
        /// <param name="Offset">Position to delete bytes from.</param>
        /// <param name="Size">How many bytes to delete.</param>
        public static byte[] DeleteBytes(byte[] Source, int Offset, int Size)
        {
            using (var io = new EndianIO(Source, EndianType.LittleEndian))
            {
                io.Open();
                var firstArray = io.Reader.ReadBytes(Offset);
                io.SetPosition(Size, SeekOrigin.Current);
                var secondArray = io.Reader.ReadBytes((int)io.Stream.Length - (int)io.Position);
                var newArray = new byte[Source.Length - Size];
                Buffer.BlockCopy(firstArray, 0, newArray, 0, firstArray.Length);
                Buffer.BlockCopy(secondArray, 0, newArray, firstArray.Length, secondArray.Length);
                return newArray;
            }
        }
Eaton is offline
Reply With Quote
The following users thanked this post: Experiment5X, Hetelek

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 06:27 PM.


 

Powered by vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
COPYRIGHT (c) 2010 - 2013 - XboxMB - DESIGN BY:
EDENWEBS.COM