I wrote a C program to perform an XOR encryption,
my problem is that the program is not able to encrypt files with more than 24 characters.
The code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BUF_SIZE 2
char* xor(char*, char*);
char* gen_key(size_t);
int main(int argc, char **argv) {
char *buffer = NULL,* encrypted_buffer = NULL;
size_t file_size;
char *key = gen_key(6);
char tmp_buffer[BUF_SIZE];
FILE *finput = NULL, *foutput = NULL;
finput = fopen("file.txt", "rb");
fseek(finput, 0, SEEK_END);
file_size = ftell(finput);
rewind(finput);
printf("File size : %d\n", (int)file_size);
buffer = (char*)malloc((file_size + 1) * sizeof(char));
memset(buffer, 0, sizeof(buffer));
while (!feof(finput)) {
memset(tmp_buffer, 0, sizeof(tmp_buffer));
fgets(tmp_buffer, sizeof(tmp_buffer), finput);
strcat(buffer, tmp_buffer);
}
printf("%s", buffer);
encrypted_buffer = xor(buffer, key);
free(buffer);
buffer = xor(encrypted_buffer, key);
printf("Encrypted : %s\n", encrypted_buffer);
printf("Decrypted : %s\n", buffer);
printf("EOF\n");
free(encrypted_buffer);
fclose(finput);
return 0;
}
char *gen_key(size_t length) {
srand(time(NULL));
const char charset[] = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz012345679";
const size_t charset_size = (sizeof(charset) - 1);
unsigned int i;
char *key = NULL;
key = (char*)malloc((length + 1) * sizeof(char));
memset(key, 0, sizeof(key));
for (i = 0; i < length; i++)
key[i] = charset[rand() % charset_size];
return key;
}
char *xor(char *file, char *key) {
unsigned int i;
char *xor = NULL;
xor = (char*)malloc(sizeof(file));
memset(xor, 0, sizeof(xor));
for (i = 0; i < strlen(file); i++)
*(xor + i) = *(file + i) ^ *(key + (i % strlen(key) - 1));
return xor;
}
And the output is :
File size : 55
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklimnopqrstuvwxyz
Encrypted : A2#G8- M >7S$1!
Decrypted : ABCDEFGHIJKLMNOPQRSTUVWX!:!e!
EOF
There are multiple problems in your code:
The buffer size is very small: #define BUF_SIZE 2. You should use a reasonable size for the line buffer, such as 80 or 100.
in memset(buffer, 0, sizeof(buffer));, buffer is a pointer, so sizeof(buffer) is not the size of the array, just the size of the pointer. Use file_size + 1 in this case.
You make the same mistake in other places in your code: pass the size of the buffers instead of relying on the sizeof() operator.
while (!feof(f)) is always wrong: you could jut use fread to read the file in one step or read line by line with:
while (fgets(tmp_buffer, sizeof(tmp_buffer), finput)) {
...
Note that there is a major problem with your approach: the key is composed of letters and digits and the file is assumed to contain text. If the file contains one of the characters in the key at the appropriate position, xoring this character with the key byte will produce a null byte that will stop the output in printf() and that would also stop the decryption if you were to store it in an output file. You rightfully use binary mode ("rb") for the file stream, but you should also make no assumptions on the file contents and handle null bytes transparently.
Here is a modified version of your program:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
unsigned char *xor(unsigned char *file, size_t size, const char *key);
char *gen_key(size_t length);
void print_buffer(const char *msg, unsigned char *buf, size_t size) {
printf("%s: ", msg);
for (size_t i = 0; i < size; i++) {
switch (buf[i]) {
case '\n':
printf("\\n");
break;
case '\\':
printf("\\\\");
break;
default:
if (buf[i] >= ' ' && buf[i] < 127)
putchar(buf[i]);
else
printf("\\%02x", buf[i]);
break;
}
}
printf("\n");
}
int main(int argc, char **argv) {
long file_size, nread, nwritten;
unsigned char *buffer, *encrypted_buffer, *decrypted_buffer;
char *key = gen_key(6);
FILE *finput = fopen("file.txt", "rb");
if (finput == NULL) {
fprintf(stderr, "cannot open file.txt: %s\n", strerror(errno));
return 1;
}
fseek(finput, 0, SEEK_END);
file_size = ftell(finput);
rewind(finput);
printf("File size: %ld\n", file_size);
buffer = calloc(file_size, sizeof(char));
nread = fread(buffer, 1, file_size, finput);
if (nread != file_size) {
fprintf(stderr, "error reading file.txt: read %ld bytes, expected %ld\n",
nread, file_size);
}
fclose(finput);
FILE *foutput = fopen("output.bin", "wb");
if (foutput == NULL) {
fprintf(stderr, "cannot open output.bin: %s\n", strerror(errno));
return 1;
}
encrypted_buffer = xor(buffer, nread, key);
nwritten = fwrite(encrypted_buffer, 1, nread, foutput);
if (nwritten != nread) {
fprintf(stderr, "error writing output.bin: wrote %ld bytes, expected %ld\n",
nwritten, nread);
}
fclose(foutput);
decrypted_buffer = xor(encrypted_buffer, nread, key);
printf("Key : %s\n", key);
print_buffer("Original ", buffer, nread);
print_buffer("Encrypted", encrypted_buffer, nread);
print_buffer("Decrypted", decrypted_buffer, nread);
if (!memcmp(decrypted_buffer, buffer, nread))
printf("OK\n");
free(decrypted_buffer);
free(encrypted_buffer);
free(buffer);
return 0;
}
char *gen_key(size_t length) {
const char charset[] = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz012345679";
const size_t charset_size = sizeof(charset) - 1;
char *key = (char*)calloc(length + 1, sizeof(char));
size_t i;
srand(time(NULL));
for (i = 0; i < length; i++)
key[i] = charset[rand() % charset_size];
return key;
}
unsigned char *xor(unsigned char *file, size_t size, const char *key) {
size_t i, keylen = strlen(key);
unsigned char *xor = calloc(size, sizeof(char));
for (i = 0; i < size; i++)
xor[i] = file[i] ^ key[i % keylen];
return xor;
}
Related
I have tested the following code to read the tester.txt file and it outputted my expectation, however, when I inserted similar code in my complete code, it just stuck at: "Data from the file:" Why it is not outputting my expectation?
#include <stdio.h>
#include <stdlib.h>
char c[1000];
FILE *fptr;
int fileReader(){
if ((fptr = fopen("tester.txt", "r")) == NULL) {
printf("Error! File cannot be opened.");
// Program exits if the file pointer returns NULL.
exit(1);
}
// reads text until newline is encountered
fscanf(fptr, "%[^\n]", c);
fclose(fptr);
}
int main() {
fileReader();
printf("Data from the file:\n%s", c);
char* anotherone = (char*) c;
printf(anotherone);
return 0;
}
Another code to read file:
int fileReader() {
fp = fopen("tester.txt", "r");
fscanf(fp, "%s", c);
fclose(fp);
}
The complete code is:
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int success = 0;
void handleOpenSSLErrors(void)
{
ERR_print_errors_fp(stderr);
abort();
}
unsigned char* decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv ){
EVP_CIPHER_CTX *ctx;
unsigned char *plaintexts;
int len;
int plaintext_len;
unsigned char* plaintext = malloc(ciphertext_len);
bzero(plaintext,ciphertext_len);
if(!(ctx = EVP_CIPHER_CTX_new())) handleOpenSSLErrors();
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
handleOpenSSLErrors();
EVP_CIPHER_CTX_set_key_length(ctx, EVP_MAX_KEY_LENGTH);
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handleOpenSSLErrors();
plaintext_len = len;
// return 1 if decryption successful, otherwise 0
if(1 == EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
success = 1;
plaintext_len += len;
/* Add the null terminator */
plaintext[plaintext_len] = 0;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
//delete [] plaintext;
return plaintext;
}
size_t calcDecodeLength(char* b64input) {
size_t len = strlen(b64input), padding = 0;
if (b64input[len-1] == '=' && b64input[len-2] == '=') //last two chars are =
padding = 2;
else if (b64input[len-1] == '=') //last char is =
padding = 1;
return (len*3)/4 - padding;
}
void Base64Decode( char* b64message, unsigned char** buffer, size_t* length) {
BIO *bio, *b64; // A BIO is an I/O strean abstraction
int decodeLen = calcDecodeLength(b64message);
*buffer = (unsigned char*)malloc(decodeLen + 1);
(*buffer)[decodeLen] = '\0';
bio = BIO_new_mem_buf(b64message, -1);
b64 = BIO_new(BIO_f_base64());
bio = BIO_push(b64, bio);
//BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Do not use newlines to flush buffer
*length = BIO_read(bio, *buffer, strlen(b64message));
BIO_free_all(bio);
}
void initAES(const unsigned char *pass, unsigned char* salt, unsigned char* key, unsigned char* iv )
{
//initialisatio of key and iv with 0
bzero(key,sizeof(key));
bzero(iv,sizeof(iv));
EVP_BytesToKey(EVP_aes_128_cbc(), EVP_sha1(), salt, pass, strlen(pass), 1, key, iv);
}
int checkPlaintext(char* plaintext, char* result){
int length = 10; // we just check the first then characters
return strncmp(plaintext, result, length);
}
char *cas[1000];
FILE *fptr;
int fileReader(){
if ((fptr = fopen("tester.txt", "r")) == NULL) {
printf("Error! File cannot be opened.");
// Program exits if the file pointer returns NULL.
exit(1);
}
// reads text until newline is encountered
fscanf(fptr, "%[^\n]", cas);
fclose(fptr);
}
int main (void)
{
fileReader();
printf("Data from the file:\n%s", cas);
char* ciphertext_base64 = (char*) cas;
char* plaintext = "to be or not to be is the question";
char dict[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int decryptedtext_len, ciphertext_len, dict_len;
// cipher (binary) pointer and length
size_t cipher_len; // size_t is sizeof(type)
unsigned char* ciphertext;
unsigned char salt[8];
ERR_load_crypto_strings();
Base64Decode(ciphertext_base64, &ciphertext, &cipher_len);
unsigned char key[16];
unsigned char iv[16];
unsigned char plainpassword[] = "00000";
unsigned char* password = &plainpassword[0];
// retrive the slater from ciphertext (binary)
if (strncmp((const char*)ciphertext,"Salted__",8) == 0) { // find the keyword "Salted__"
memcpy(salt,&ciphertext[8],8);
ciphertext += 16;
cipher_len -= 16;
}
dict_len = strlen(dict);
time_t begin = time(NULL);
for(int i=0; i<dict_len; i++)
for(int j=0; j<dict_len; j++)
for(int k=0; k<dict_len; k++)
for(int l=0; l<dict_len; l++)
for(int m=0; m<dict_len; m++){
*password = dict[i];
*(password+1) = dict[j];
*(password+2) = dict[k];
*(password+3) = dict[l];
*(password+4) = dict[m];
initAES(password, salt, key, iv);
unsigned char* result = decrypt(ciphertext, cipher_len, key, iv);
if (success == 1){
if(checkPlaintext(plaintext, result)==0){
printf("Password is %s\n", password);
time_t end = time(NULL);
printf("Time elpased is %ld seconds", (end - begin));
return 0;
}
}
free(result);
}
// Clean up
EVP_cleanup();
ERR_free_strings();
return 0;
}
char *cas[1000]; should be char cas[1000];, I would say this is the main issue, you are using an array of pointers as if it was a character array, this is very likely what's causing your program to fail.
Fix bzero, pass the size of the array as argument. When you use sizeof(key) in initAES(), what you are actually getting is the size of the pointer, not the size of the container it points to. More oh this in How to find the 'sizeof' (a pointer pointing to an array)?
You can add size of the array as a global constant with a macro:
#define SIZE 16 // size of the arrays
//...
void initAES(const unsigned char *pass, unsigned char* salt, unsigned char* key, unsigned char* iv )
{
//initialisatio of key and iv with 0
bzero(key, SIZE);
bzero(iv, SIZE);
EVP_BytesToKey(EVP_aes_128_cbc(), EVP_sha1(), salt, pass, strlen(pass), 1, key, iv);
}
You can use it to define the size in main making your code more consistent.
int main (void)
{
//...
unsigned char key[SIZE];
unsigned char iv[SIZE];
//...
}
For good measure make fileReader have void return type if you don't want it to return an int.
I always advise the use of compiler warnings at full blast and preferably to treat warnings as errors.
I am trying to get the read() system call to read my file line by line and reverse each line to stdout. My issue is getting read() to read my file line by line because normally it just reads the whole file. I want the LINE_BUFFER size to be the max size a line can be.
I tried implementing a function to try to do this but it seems to break the program and I am a little lost on how to approach this problem.
Here is my code:
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#define LINE_BUFFER 1024
int charCount(const char *name1);
ssize_t readline (char *buf, size_t a, int b, off_t *offset);
int main(int argc, char* argv[]) {
if(argc ==2){
charCount(argv[1]);
}else{
printf("Provide a file\n");
}
return 0;
}
int charCount(const char *name1)
{
char buffer[LINE_BUFFER];
int fd;
ssize_t len = 0;
int nread;
int i = 0;
off_t offset = 0;
if ((fd = open(name1, O_RDONLY)) == -1)
{
perror("Error in opening file");
return (-1);
}
int size = lseek(fd,-1, SEEK_END);
while(size>=0)
{
while((len = readline(buffer,LINE_BUFFER,fd,&offset)) != -1){
write(1,buffer,1);
lseek(fd, -2,SEEK_CUR);
size--;
}
}
close(fd);
return(0);
}
ssize_t readline(char *buf, size_t a, int b, off_t *offset)
{
int fd;
ssize_t nchr =0;
ssize_t idx =0;
char *p = NULL;
if ((nchr = lseek(fd, *offset, SEEK_SET)) != -1){
nchr = read(fd,buf,a);
}
p = buf;
while(idx<nchr && *p != '\n') p++,idx++;
*p =0;
if(idx == nchr) {
*offset + nchr;
return nchr < (ssize_t)a ? nchr : 0;
}
*offset += idx+1;
return idx;
}
I'm not sure what you mean when you say that "normally it just reads the whole file". read has a fairly low maximum size it will read, typically 4KiB or 8KiB. In any case, I put together some code to reverse line of a file.
#include <assert.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int xopen(const char *, int);
void * Realloc(void *, size_t);
void reverse(char *, char *);
char * findchr(char *, char *, char);
int
main(int argc, char **argv)
{
ssize_t rc;
size_t siz = BUFSIZ; /* size available to read into */
char *buf = Realloc(NULL, BUFSIZ + siz); /* Pad the front */
char *s = buf + BUFSIZ; /* first char of a line */
char *prev = s; /* start of data from previous read */
char *end = s; /* one past last char read from input */
int fd = argc > 1 ? xopen(argv[1], O_RDONLY) : STDIN_FILENO;
while(( rc = read( fd, s, BUFSIZ )) > 0 ) {
char *eol; /* A newline, or one past valid data */
end = s + rc;
if( (eol = findchr(s, end, '\n')) == end ) {
/* No newlines found in the last read. Read more. */
if( end > buf + siz ) {
ptrdiff_t e_off = end - buf;
ptrdiff_t p_off = prev - buf;
siz += BUFSIZ;
buf = Realloc(buf, BUFSIZ + siz);
eol = end = buf + e_off;
prev = buf + p_off;
}
s = end;
assert( s <= buf + siz );
continue;
}
s = prev;
do {
assert(*eol == '\n');
assert(eol < end);
reverse(s, eol-1);
s = eol + 1;
assert(s <= end);
} while( (eol = findchr(s, end, '\n')) < end );
assert(eol == end);
assert(eol[-1] != '\n' || s == end);
fwrite(prev, 1, s - prev, stdout);
prev = buf + BUFSIZ - (end - s);
memcpy(prev, s, end - s);
eol = s = buf + BUFSIZ;
}
if(rc == -1) {
perror(argc > 1 ? argv[1] : "stdin");
return EXIT_FAILURE;
}
if(prev < s) {
reverse(prev, s-1);
fwrite(prev, 1, s - prev, stdout);
}
return EXIT_SUCCESS;
}
/*
* Find v between str and end. If not found,
* return end. (This is basically strchr, but
* doesn't care about nul.)
*/
char *
findchr(char *str, char *end, char v) {
assert(str <= end);
while( str < end && *str != v )
str += 1;
return str;
}
void
reverse(char *start, char *end)
{
for( ; start < end; start++, end-- ) {
char tmp = *end;
*end = *start;
*start = tmp;
}
}
void *
Realloc( void *buf, size_t s )
{
buf = realloc( buf, s );
if( buf == NULL) { perror("realloc"); exit(EXIT_FAILURE); }
return buf;
}
int
xopen(const char *path, int flag)
{
int fd = open(path, flag);
if( fd == -1 ) { perror(path); exit(EXIT_FAILURE); }
return fd;
}
int main()
{
//FILE *out = fopen("keimeno.txt", "w+");
FILE *in = fopen("keimeno.txt", "r");
int fullbufflen=0 , i;
char buffer[100];
fgets(buffer, 100, in);
int bufflen = strlen(buffer);
char *text;
text =calloc(bufflen,sizeof(char));
char *strcat(text, buffer);
// printf("line of \"keimeno.txt\": %s", buffer);
// printf("size of line \"keimeno.txt\": %i\n\n", bufflen);
fullbufflen = bufflen;
while(fgets(buffer, 100, in)!=NULL)
{
// printf("line of \"keimeno.txt\": %s", buffer);
// printf("size of line \"keimeno.txt\": %i\n\n", bufflen);
text =realloc(text,bufflen*sizeof(char));
char *strcat(text, buffer);
fullbufflen = bufflen + fullbufflen ;
}
for (i = 0;i<fullbufflen;i++)
{
printf("%c\n",text[i]);
}
}
I am trying to copy the full text file (keimeno.txt) into a dynamic memory array, with a buffer of 100 character at most everytime. To test it at the end i tried to prinf the results. And i just cant get it to work. Dont know if there is a problem on the printf at the end, or the whole program is just wrong.
Also the dynamic array is supposed to have 0 size at the beggining, so if someone could tell me how to do that too, it would be welcome.
Thanks in advance.
There are a few issues:
char *strcat(text, buffer);
This is not how you call a function. This is a function declaration, and an incorrect one at that since it doesn't define the types of the arguments.
To call strcat, just do this:
strcat(text, buffer);
Next, you're not allocating enough space for your buffers:
text =calloc(bufflen,sizeof(char));
You need to add space for the null bytes that terminates the string:
text =calloc(bufflen + 1,sizeof(char));
Similarly here:
text =realloc(text,bufflen*sizeof(char));
This only reallocates a total of bufflen bytes. It does not add bufflen bytes to what was already allocated, and bufflen is unchanged from when it was first set outside of the while loop. Do this instead:
bufflen = strlen(buffer);
text =realloc(text,bufflen+fullbufflen+1);
This gives you enough space for the current length, the additional buffer, and the null byte.
Finally, make sure you fclose(in) and free(text) at the end to clean up your resources, and be sure to check the return value of fopen to ensure that the file opened successfully, and realloc/calloc to ensure that your allocations worked.
After the above changes, you code should look like this:
int main(void)
{
//FILE *out = fopen("keimeno.txt", "w+");
FILE *in = fopen("keimeno.txt", "r");
if (in == NULL) {
perror("open failed");
exit(1);
}
int fullbufflen=0 , i;
char buffer[100];
fgets(buffer, 100, in);
int bufflen = strlen(buffer);
char *text;
text =calloc(bufflen+1,sizeof(char));
if (text == NULL) {
perror("calloc failed");
exit(1);
}
strcat(text, buffer);
// printf("line of \"keimeno.txt\": %s", buffer);
// printf("size of line \"keimeno.txt\": %i\n\n", bufflen);
fullbufflen = bufflen;
while(fgets(buffer, 100, in)!=NULL)
{
// printf("line of \"keimeno.txt\": %s", buffer);
// printf("size of line \"keimeno.txt\": %i\n\n", bufflen);
bufflen = strlen(buffer);
text =realloc(text,bufflen+fullbufflen+1);
if (text == NULL) {
perror("realloc failed");
exit(1);
}
strcat(text, buffer);
fullbufflen = bufflen + fullbufflen ;
}
fclose(in);
for (i = 0;i<fullbufflen;i++)
{
printf("%c\n",text[i]);
}
free(text);
}
Here is one possible solution:
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#define ERR (-1)
#define GROW 100
static void die ( const char * msg ) { perror(msg); exit(1); }
int main ( void ) {
char * filename = "in.txt";
size_t buffSize = 0;
size_t buffUsed = 0;
ssize_t bytesRead = 0;
char * buffer = NULL;
char * tmp;
int fd;
fd = open(filename, O_RDONLY, 0);
if (fd == ERR) die("open");
do {
buffUsed += (size_t)bytesRead;
if (buffUsed == buffSize) {
tmp = realloc(buffer, buffSize += GROW);
if (tmp == NULL) die("realloc");
buffer = tmp;
}
bytesRead = read(fd, buffer + buffUsed, buffSize - buffUsed);
if (bytesRead == ERR) die("read");
} while (bytesRead > 0);
if (write(STDOUT_FILENO, buffer, buffUsed) == ERR) die("write");
free(buffer);
if (close(fd) == ERR) die("close");
return 0;
}
Like the original, the input filename is hard-coded which sub-optimal...
I'm trying to change the bytes corresponding to file size in the header of a .wav file, which are the 4th and 40th bytes. Currently, I am copying the entire header to a buffer, attempting to edit it there and then writing it to a destination file, but it doesn't seem to work.
int main(int argc, char * argv []){
int delay = atoi(argv[1]);
FILE * source = fopen(argv[2], "rb");
FILE * destination = fopen(argv[3], "wb");
void * header = malloc(44); // size of a wav file header
fread(header, sizeof(header), 1, source);
// my attempt at changing the 4nd and 40th bytes
sizeptr1 = (unsigned int *)(header + 4);
sizeptr2 = (unsigned int *)(header + 40);
*sizeptr1 = *sizeptr1 + delay;
*sizeptr2 = *sizeptr2 + delay
fwrite(header, sizeof(header), 1, destination);
return 0;
}
What would the most efficient way to change these bytes and write the new header to the output file be?
A byte array is easiest, if you are sure which bytes you want to change. For example the 4th byte is at buffer[3].
#include <stdio.h>
#include <stdlib.h>
#define BUFFSIZE 1024
#define HEADSIZE 44
int main(int argc, char * argv []) {
int delay;
size_t bytes;
FILE * source = NULL;
FILE * destination = NULL;
unsigned char buffer[BUFFSIZE];
if (argc < 4) return 0; // check enough args
delay = atoi(argv[1]);
if (NULL == (source = fopen(argv[2], "rb")))
return 0;
if (NULL == (destination = fopen(argv[3], "wb")))
return 0;
// copy & alter buffer
if (HEADSIZE != fread(buffer, 1, HEADSIZE, source))
return 0;
buffer[4] = delay;
buffer[40] = delay;
if (HEADSIZE != fwrite(buffer, 1, HEADSIZE, destination))
return 0;
// copy rest of file
while ((bytes = fread(buffer, 1, BUFFSIZE, source)) != 0) {
if (bytes != fwrite(buffer, 1, bytes, destination))
return 0;
}
if (0 == fclose(destination))
printf("Success\n");
fclose (source);
return 0;
}
This is one way of editing bytes in a file, check if it will works for you.
int
ChangeHeaderByte(
const char* fileName,
unsigned int pos[],
unsigned int val[],
unsigned int size
)
{
unsigned int i = 0;
unsigned int temp = 0;
FILE * fileHandle = fopen(fileName, "r+");
if (fileHandle == 0) {
fprintf(stderr,"Error: %s : %d : Failed to open file ' %s '\n",
__func__,__LINE__,fileName);
return (1);
}
for ( i = 0; i < size ; ++i )
{
if ( fseek(fileHandle, pos[i] - (unsigned int)1, SEEK_SET) == 0 )
{
if ( fread(&temp,sizeof(unsigned int),1,fileHandle) == 1 )
{
if ( fseek(fileHandle, pos[i] - (unsigned int)1 , SEEK_SET) == 0 )
{
val[i] += temp;
fwrite(&val[i], sizeof(unsigned int), 1, fileHandle);
}
else
{
fprintf(stderr,"Error: %s : %d : fseek() failed on file ' %s '\n",
__func__,__LINE__,fileName);
return (1);
}
}
else
{
fprintf(stderr,"Error: %s : %d : fread() failed on file ' %s '\n",
__func__,__LINE__,fileName);
return (1);
}
} else {
fprintf(stderr,"Error: %s : %d : fseek() failed on file ' %s '\n",
__func__,__LINE__,fileName);
return (1);
}
}
fclose(fileHandle);
return (0);
}
int main(void)
{
const unsigned int size = 2;
char fileName[] = "C:\\Users\\sridhar\\test\\test.wav";
unsigned int pos[2] = { 4 , 40 };
unsigned int val[2] = { 123456, 123456 };
ChangeHeaderByte(fileName,pos,val,size);
return 0;
}
How can I traverse a file line by line going from the bottom up? For example, here is my code for traversing it from top to bottom:
void *readFileTB(char *str1)
{
int size = 1024;
char *buffer = malloc(size);
FILE *fp;
fp = fopen("testfile.txt", "r");
while(fgets(buffer, 1024, fp)) //get a line from a file
{
printf(buffer);
}
return 0;
}
If a file contains:
line1onetest
line2twotest
line3threetest
This function will print the following if executed:
line1onetest
line2twotest
line3threetest
How can I write a function that does the above but in the opposite direction so it outputs the following?
line3threetest
line2twotest
line1onetest
Any thoughts?
Line by line it's a little difficult. If we start with bytes, it's pretty simply: we first fseek to a little before the bottom:
if(fseek(fp, 256, SEEK_END) == -1) { /* handle error */ }
Since we've seeked 256 bytes before the end, we can read 256 bytes. Then we can seek back 256 more bytes, etc., until we hit the top of the file.
Now if you're trying to read lines of text, this can be tricky: you need to read a number of bytes at the end of the file and find the last newline character. If there is none, you didn't read enough and you need to read more. Once you've found it, your line starts there. To read the next line, you need to seek backwards again and don't go past your previous line start.
Here, I got a bit into it and coded the entire thing. I have no clue if it's any good, but at least you can get an idea of how it works. (I have a feeling there are better ways to do this)
To compile and use program:
$ gcc -Wall -o reverser main.c
Usage:
$ ./reverser text.txt
text.txt contents:
int main(int argc, char *argv[]){
if(argc != 2)
return 1;
print_rev_file(argv[1], 64);
return 0;
}
Results:
}
return 0;
print_rev_file(argv[1]);
return 1;
if(argc != 2)
int main(int argc, char *argv[]){
How to use in code:
main.c
#include <header.h>
int main(int argc, char *argv[]){
if(argc != 2)
return 1;
print_rev_file(argv[1], 64);
return 0;
}
header.h:
Note: It's bad form to use double underscore because many compilers use them.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
///SUPPORT for get_line
size_t __file_size(FILE ** file); //It'd make sense to homogenize the parameters...
char * __get_line_copy(FILE ** file, size_t chunk_size, size_t position);
char * __get_line(int line_number, FILE ** file, size_t chunk_size);
size_t __nth_line(FILE ** file, int line_number, size_t chunk_size);
unsigned int __line_count(FILE ** file, size_t chunk_size);
#define file_size(x) __file_size(&x)
size_t __file_size(FILE ** file){
size_t old_pos = ftell(*file);
fseek(*file, 0, SEEK_END);
int file_size = ftell(*file);
fseek(*file, 0, old_pos);
return file_size;
}
char * __get_line_copy(FILE ** file, size_t chunk_size, size_t position){
int i;
char c;
char * buffer = malloc(chunk_size);
memset(buffer, 0, chunk_size);
size_t old_pos = ftell(*file);
fseek(*file, position, SEEK_SET);
for(i = 0; (i < chunk_size && (c = fgetc(*file)) != '\n' && !feof(*file)); i++){
*(buffer+i) = c;
}
*(buffer+chunk_size) = '\0';
fseek(*file, 0, old_pos);
return buffer;
}
#define FIRST 0
#define LAST -1
#define get_line(x, y, z) __get_line(x, &y, z);
char * __get_line(int line_number, FILE ** file, size_t chunk_size){
char * line = __get_line_copy(file, chunk_size, __nth_line(file, line_number, chunk_size));
return line;
}
size_t __nth_line(FILE ** file, int line_number, size_t chunk_size){
int i = 0, index;
size_t old_pos = ftell(*file);
fseek(*file, 0, SEEK_SET);
if(line_number > 0){
while(i <= line_number && !feof(*file)){
if(fgetc(*file) == '\n')
i++;
}
} else {
while(!feof(*file)){
if(fgetc(*file) == '\n')
i++;
}
index = i + (line_number+1);
fseek(*file, 0, SEEK_SET);
int i = 0;
while(i < index){
if(fgetc(*file) == '\n')
i++;
}
}
size_t position = ftell(*file);
fseek(*file, 0, old_pos);
return position;
}
#define line_count(x, y) __line_count(&x, y)
unsigned int __line_count(FILE ** file, size_t chunk_size){
int i = 1;
while(!feof(*file))
if(fgetc(*file) == '\n')
i++;
return i;
}
int print_rev_file(char * filename, size_t buffer){
FILE * file = fopen(filename, "r");
if(file == NULL){
return -1;
}
int i, lines = line_count(file, buffer);
for(i = 0; i < lines; i++){
char * line = get_line(LAST-i, file, buffer);
puts(line);
free(line);
}
return 0;
}
int main(int argc, char *argv[]){
if(argc != 2)
return 1;
print_rev_file(argv[1], 64);
return 0;
}
There's a utility in the GNU coreutils called tac that does exactly that.
You can view the source for it below.
http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=blob_plain;f=src/tac.c;hb=HEAD