Skip to main content
GameDev.net gamedev.net
🔒 Locked

Memory Reader

Started by starfleetrp Aug 6, 2006 at 4:56 PM 5 replies 850+ views
Original Post
starfleetrp
starfleetrp
Hi, I am curently trying to figure out how I could read data from memory like you do from a file with fread. I do not really know where to begin since I would probably store the data as an array of chars. However with fread you specify the file to write to and that can be an integer or whatever. So I am assuming I would get an error if I tried to convery it to an integer. Also how can I use type cast if I do not even know to what format I am moving the data to. If you have done something simular to this or know of a way of doing this I would greatly appreciate it. I need this for a game project that I am working on. I need to read the encrpyed data from the file and then manupulate it from memory. I need to use it something like fread because all of my code uses that and sometimes I need to phase the data like for a 3ds file.
CrazyCdn
CrazyCdn
Huh? You load the file, decrypt it (you know the memory address from this) you also know the format. You then make any changes you need by moving things around, allocating new memory etc. Not sure what the issue is. Not sure what fread has to do with anything besides initially loading a file either. Can you be a weeeee bit more specific?
"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin
starfleetrp
starfleetrp
Well pretty much I want to manipulate the memory like I would if it was on the hard drive. I would like to create a function like fread but that returns the data in the memory. I can not really think of a better way to put this. Ill post an example of what I mean

Lets say I have a file with the data "abcdefghij"
so I would do fread(&data, 1, 3, mFile); // mFile = fopen("c:\\file.txt", ios::binary

so now data = "abc"

Now lets say my function that will act as fread does for a file but for memory
memread(&data, 1, 3, mMemFile); //mMemFile is an pointer to a memory file class, which would contain an array of data "abcdefg"

so now data would not be "abc"

So pretty much I would like to use the simplicity of fread but instead of reading from a file id like to read from memory. I was wondering how exactly I would go about doing this.

I know that the mMemFile would contain the array of data(I think char), the current position and the size.

However I am not sure how I would go about transfering chars to any other format since data could be an int, char, long, an array etc.

I hope this helps if not id be happy to clarify. I would like to do it this way since I have 500 or so different functions that use fread. However now all/most of the files they are going to be reading will be encrypted, so id like to make it easy to read the now un-encypted files from memory, since all id have to do is "find and replace fread, fmemread", and all of the fopens and fcloses with ones that will work accordingly.


EDIT: I can not just give the un-encrypted data to the functions that use it because one file might be phased into 300 different reads (like a 3ds) or a custom file type etc.
DanielH
DanielH
Just codeing the MemoryReader isn't too hard. But if you want to use the same interface for both files and ram access you need to make a wrapper for the file access aswell.

class DataReader{public:  virtual void read(void * dst, int size) = 0;  virtual void write(void * src, int size) = 0;};class MemoryReader : public DataReader{private:  char * m_data;  int m_offset;public:  void read(void * dst, int size)  {    memcpy(dst, m_data + m_offset, size);    m_offset += size;  }};class FileReader : public DataReader{private:  fstream m_file;public:  void read(void * dst, int size)  {     m_file.read(dst, size);  }};
JohnBolton
JohnBolton
You cannot use fread to read a "file" that is in memory (if that's what you are asking). You can follow DanielH's suggestion, or you can also implement versions of std::ostream and std::istream that encrypt and decrypt to/from a memory buffer.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
starfleetrp
starfleetrp
Ah thanks for some reason I completely forgot about memcpy that was exactly what I was looking for!
Zahlman
Zahlman
Quote:
Original post by JohnBolton
You cannot use fread to read a "file" that is in memory (if that's what you are asking). You can follow DanielH's suggestion, or you can also implement versions of std::ostream and std::istream that encrypt and decrypt to/from a memory buffer.


Or you can try using a stringstream.

Please, don't use memcpy() in C++. Use std::copy() (from ) instead. It provides a much nicer, type-safe interface, will call copy-constructors for things of object type, and can be used with any kind of iterator (not just raw pointers).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.