Representation of a C binary file [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
For a homework assignment I created a simple compression/decompression program that makes use of a naive implementation of run-length encoding. I've gotten my program working; compressing and decompressing any text file with a pretty large number of characters (e.g. the program source) works flawlessly. As an experiment I tried to compress/decompress the binary of the compression program itself. This resulted in a file that was much smaller than the original binary, and is obviously un-runnable. What is causing this data-loss?
My assumption was that it's related to how binary files are represented, but I can't figure much out past that.

Possible issues:
Your program opens the binary file in the text mode, which damages the '\r' and '\n' bytes
Your program incorrectly handles zero bytes, treating them as ends of strings ('\0') and not as data of its own
Your program uses char (that is actually signed char) for the bytes of data and correctly works only with non-negative values, which ASCII chars of English text are, but fails to work with arbitrary char/byte values, which may be negative
Your program has an overflow somewhere which shows up only on big files
Your program has some other data-dependent bug

If the platform is linux (as the question is tagged), there's no difference between binary and text modes. So it shouldn't be that; but even so, the files should be opened as binary.
I suspect that your problem is the program treats '\0' characters as terminators (or otherwise specially) instead of as valid data.

Related

Buffer overflow with disabled shell? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
AFAIK, a buffer overflow is achieved by overwriting memory adjacent to a C variable's buffer. This overwriting is used to spawn a shell which executes commands.
But what if the user that is running the program vulnerable to a buffer overflow has the shell disabled ?
/etc/passwd:
user1:x:1000:1000:user1,,,,:/home/user1:/bin/false
sudo -u user1 /usr/bin/programname
"Shell disabled" only matters if you're actually logging in. If you're exploiting an already running program then you don't need to log in.
Exploits do not use a shell which is configured for a user — they normally include a binary code, shellcode, which are functionally equivalent to a primitive shell, meaning that it will start any chosen executable — for example a real shell program. Exploited program is then tricked to execute this code.
There are many different shellcodes available on the net, for example which do not include a byte '\0', so they will be passed unharmed as a C string, or which only include printable characters, valid unicode strings etc.

How to handle large inputs in c [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I was solving a practice problem on a site which states that
The purpose of this problem is to verify whether the method you are
using to read input data is sufficiently fast to handle problems
branded with the enormous Input/Output warning. You are expected to be
able to process at least 2.5MB of input data per second at runtime.
Also how do I optimize input/output routines other than printf and scanf?
It is operating system specific (because the C standard only knows about <stdio.h>). With Linux consider using low-level syscalls for efficiency, like open(2), mmap(2), read(2), pread(2), write(2). You might also want to use readahead(2). Don't forget to make I/O in rather large blocks (e.g. 128Kbytes), page aligned if possible. Read the Advanced Linux Programming book.
If restricted to standard C99 functions, use fread(3) on rather big chunks. Consider also increasing the internal buffer with setvbuf(3)
And 2.5Mbyte/sec is not very impressive. Probably, the bottleneck is the hardware, but you should be able to get perhaps 20 or 50Mbytes/sec on a standard desktop hardware. Using SSD would help a big lot.

Extracting information from lines of a file in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I would like to extract file names and their corresponding MD5 sum from a check sum file in a format such as this-
MD5 (FreeBSD-8.2-RELEASE-amd64-bootonly.iso) = 2587cb3d466ed19a7dc77624540b0f72
I would prefer to do this locally within the program, which rules out awk and the like.
You can read lines easily enough using fgets(). Don't even think of using gets().
If you're reasonably confident you won't be dealing with filenames containing spaces or close parentheses, you can use sscanf() to extract the bits and pieces:
char hash_type[16];
char file_name[1024];
char hash_value[128];
if (sscanf(line, "%15s (%1023s) = %127s", hash_type, file_name, hash_value) == 3)
...good to go...
else
...something went wrong...
Note the sizes specified in the sscanf() string compared to the variable definitions; there isn't an easy way to generalize that other than by using snprintf() to create the format string:
char format[32];
snprintf(format, sizeof(format), "%%1%zus )%%%zus) = %%%zus",
sizeof(hash_type)-1, sizeof(file_name)-1, sizeof(hash_value)-1);
Your alternative is some routine forward parsing to locate the hash type and the open parenthesis before the start of the file name, and some trickier backwards parsing, skipping over the hash value and finding the equals and the last close parenthesis, and then collecting the various parts.
You should be able to implement this with fopen(), fgets() and strchr() - but first you will need to nail down the format of the file more precisely (for example: what happens if the filename includes a ) character?)
I wouldn't advocate it in most languages, but why not just hit it up with POSIX regex?

Create a utf8 file in linux in c programing [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have to create a utf8 file (say utf8_test.txt) in linux (Ubuntu), in c.
I tried fopen(), but it create hte file depending on the Locale - as the locale was en_IN, it created in Ascii I guess.
Is there any interface or function by which I can specify the format of the file to open or I need to add some byte in the beginning of the file, so that the OS understands that it is a UTF8 file?
Please give your valuable inputs.
Thank you.
GLib contains routines for working with UTF-8 text, and libiconv can be used to convert between various charsets, including UTF-8.
fopen can be used to write a binary stream of data. Your locale is not relevant.
You should check whether you are actually sending a UTF-8 byte stream to the file. You can do this by running a hex-editor on the file, e.g. xxd, and seeing if a UTF-8 sequence appears in the file.
If you do not have UTF-8 bytes in the file then the byte stream you are sending to the file is incorrect.
If you do have UTF-8 bytes in the file then your issue is just one of display.

password complexity in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Hi I'm on my internship and my company told me that I've to implement password complexity using C language. The password must be Alpha numeric (eg h#ll0). Since I'm new to C , I found some difficulty. I google "password complexity in C " but no luck there. Can someone gave me some sample or explain me how to do it programmatically.
Thanks a lot in advance
Kevin
A better Google term would be "strong password":
http://en.wikipedia.org/wiki/Password_strength
But most of the articles you will find will not be for the C language, and they will probably suggest using a regular expression.
It would probably not be too hard to write your own low-level code to do the check as others have suggested. That would save you the trouble of generating a dependency on some C-language regular expression library to use. However, there is an advantage in using a regular expression because it means that non-C programmers would have a better chance at updating the rule at a later date, and it may make errors less likely to boot. It depends on your particular situation.
(Also, if other parts of your C code need regular expressions, then linking one in might be something you're going to need to do anyway and you'd get it "for free"...)
In any case, this StackOverflow question has a link to a regex.h tutorial, and more may be added to it in the future:
C - pellucid regex.h use tutorial
You don't provide enough information. By password complexity I assume you mean password strength.
I'm not in the business of writing code for someone, but if what you're looking to do is determine whether or not a password contains both a letter and a number, is at least n characters long, etc., C has functions you can do this with. isalnum(), isdigit(), and isalpha() come to mind for testing. These all return nonzero values to indicate true.
In terms of speed, C is fast on its own but remember with these that there is no need to parse the entire password -- all you need is for the function to return a nonzero value at some point. (All of these functions parse by character; C strings are char arrays.)
http://icecube.wisc.edu/~dglo/c_class/charfunc.html This is a good little reference for character parsing functions.
It depends on how the password is encoded, you may need an ASCII character chart or a unicode character chart. For each character in the input password, categorize it into groups number, uppercase letter, lowercase letter or special characters and so on.
here are the links to the tables:
http://www.asciitable.com/
http://www.tamasoft.co.jp/en/general-info/unicode.html

Resources