Saving data into the program? - c

Is it possible for one portable program to keep save data inside the application?
I don't want the program to create folders or files.

In order to do it in a portable way, you should have no assumptions about the architecture or operating system: you may or may not have access to the executable in the first place (it could be argv[0], but maybe it isn't. If you had access to the executable file, you could have the rights to open the file and modify it, but maybe you cannot do it.
If, anyway, you want to try it, you could:
Check if argv[0] is a file, that you have read and write permission, and if it is really your code (looking for a random string you can leave somewhere in your code, for example).
Choose a string to mark your modifications, for example, "Edenia", and check if the last bytes of that file are those. If so, the file has been previously modified, and you can read your data process it.
When you want to store additional data, add it to the end of the file (if it was not modified yet), or substitute the modifications it had. Don't forget to add the mark at the end of the file ("Edenia", or whatever).
Anyway, I still think this is not the proper way to store data: try to use external storage (files, database, etc) if you can.

Related

Can I access and change my iNode values of a file?

I know that files in unix systems are represented by their inodes.
Can I as a user have access to these values and change them?
Say, replace the values between two adjacent blocks, and in this way change the file?
Can I overwrite only one block in the middle?
I'm asking this in the context of file manipulation in C (I want to write a program that appends to the beginning, or middle part of a file, and not just to the end).
The user has read access to some of this information using the stat() system call provided he has proper access rights to the directory containing the inode.
The information can only be changed indirectly (timestamps, for example, by accessing the file itself). There is no direct way to mess with this information.
Some file systems might give a bit more access possibilities by exposing some of the information in ioctl() calls. What might or might not be exposed is a decision of the driver/file system developer.

C function to modify inode?

I was learning how linux file system works and I came across the concept of inodes. I have written a C program to read a particular inode and print its contents.
Now I wan't to modify the contents of inode from my C code. I know this could break the filesystem if something goes wrong but still I want to try it.
How can I achieve this?
You need to access what is called the "meta" information of the drive - the information about the information on the drive - not the normal information. To do that, you need to open the drive itself rather than any file or directory inside the drive.
If you're talking i-nodes, then you're on Linux and the ext filesystem, so the drive name will be something like /dev/sdb. Be careful: this is the whole disk, NOT one partition/volume/slice within it. That might be called /dev/sdb2 or something - different types of Linux call them different things.
Once you have the partition open, you can treat it just like a (very large!) file: a succession of bytes that coincidentally happen to be arranged as sectors on the hard disk. You can seek to any position and read the data there. If you want to overwrite it, you can - but as you say:
You may completely destroy the data on your hard disk!
Perhaps mount a USB stick (with nothing important on it) and experiment on that? And make VERY sure that you open ITS name and not your main disk's name!

Self Modification of .exe c file after execution

I have a c code with a character array initialized to "hello world".
I would like to know if there is a possibility to re-initialize this character array upon each execution of the code, to some other random string. If not C, may I know if such an implementation is possible in any other programming language?
In detail, let's say my code looks like:
char c[] = "hello world";
.
.
After executing this code once, I want the char array c to be initialized automatically to some other random string (and not "hello world") and this should be a permanent change. This need is for security reasons. May I know if such a thing is even possible?
If not, may I know if it is possible to let the code self-destroy after executing it once?
It would be much easier to answer this question if you could describe what you are trying to achieve, rather than a particular mechanism you thought of to achieve it.
Modifying the running executable is typically prevented by modern operating systems, and for good reasons (security, integrity, etc). And modifying the executable on-disk is also inadvisable, for similar reasons. I'm sure there are other ways to achieve what you wish without resorting to self-modifying code.
I have a c code with a character array initialized to "hello world'. I would like to know if there is a possibility to re-initialize a this character array upon each execution of the code, to some other random string.
Yes, it's possible, but you would need to give the user write access to the executable to do so, find the correct offset in the binary, patch the file and save it intact atomically, etc, etc. There are many ways this can go wrong. Don't do it this way.
Instead of a statically allocated string in the executable itself, just have a separate resource file that contains the string(s) and whatever other state is required. You can change this resource file (INI file, data file, whatever) to modify the string as and when required (or even delete it). To provide security, you can digitally sign the file, which allows you to verify the contents are legitimate. If the signature fails, the software can refuse to work. You can also encrypt the contents so that it cannot be read out by an inquisitive user. (Unless they are handy with a debugger!)
If not C, may I know if such an implementation is possible in any other programming language?
It's mainly an OS restriction, including a regular user being able to write to an installed app in a system folder (typically something you do not want to allow). And if you can't do it in C, you probably can't do it in another language!
After executing this code once, I want the char array c to be initialized automatically to some other random string (and not "hello world") and this should be a permanent change. ... May I know if such a thing is even possible?
Yes indeed, but use a separate file as described above. Don't modify the executable itself.
This need is for security reasons.
The reason self-modifying code is disallowed in the first place is for security reasons. If you are attempting to implement some kind of copy protection, you would do well to research existing methods and tools and best practices, and possibly even use an off-the-shelf solution. This stuff is hard, and there are people who crack software for fun who could easily get around all but the most sophisticated protection schemes.
If not, may I know if it is possible to let the code self-destroy after executing it once?
You might be able to get the executable to delete itself, but this would typically require elevated privileges too.
Just get the program to check the signature on the resource file and refuse to run if it isn't valid.
You can self modify a EXE file, but you need to know:
You are basically reinventing the wheel doing it that way
Self modifying code is usually (in fact, most likely) flagged by antivirus software. Destroying the EXE after it is executed is even more shady.
How EXE (and ELF) files are executed
What the assembly language is

Temporary File in C

I am writing a program which outputs a file. This file has two parts of the content. The second part however, is computed before the first. I was thinking of creating a temporary file, writing the data to it. And then creating a permanent file and then dumping the temp file content into the permanent one and deleting that file. I saw some posts that this does not work, and it might produce some problems among different compilers or something.
The data is a bunch of chars. Every 32 chars have to appear on a different line. I can store it in a linked list or something, but I do not want to have to write a linked list for that.
Does anyone have any suggestions or alternative methods?
A temporary file can be created, although some people do say they have problems with this, i personally have used them with no issues. Using the platform functions to obtain a temporary file is the best option. Dont assume you can write to c:\ etc on windows as this isnt always possible. Dont assume a filename incase the file is already used etc. Not using temporary files correctly is what causes people problems, rather than temporary files being bad
Is there any reason you cannot just keep the second part in ram until you are ready for the first? Otherwise, can you work out the size needed for the first part and leave that section of the file blank to come back to fill in later on. This would eliminate the needs of the temporary file.
Both solutions you propose could work. You can output intermediate results to a temporary file, and then later append that file to the file that contains the dataset that you want to present first. You could also store your intermediate data in memory. The right data structure depends on how you want to organize the data.
As one of the other answerers notes, files are inherently platform specific. If your code will only run on a single platform, then this is less of a concern. If you need to support multiple platforms, then you may need to special case some or all of those platforms, if you go with the temporary file solution. Whether this is a deal-breaker for you depends on how much complexity this adds compared to structuring and storing your data in memory.

How would I go about checking the file system I'm working on in C

I'm making a program and one of the things it needs to do is transfer files. I would like to be able to check before I start moving files if the File system supports files of size X. What is the best way of going about this?
Go on with using a function like ftruncate to create a file of the desired size in advance, before the moving, and do the appropriate error-handling in case it fails.
There's no C standard generic API for this. You could simply try creating a file and writing junk to it until it is the size you want, then deleting it, but even that isn't guaranteed to give you the info you need - for instance another process might have come and written a large file in between your test and your transfer, taking up space you were hoping to use.

Resources