compiling in gcc goes well, gives segmentation fault
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
compiling in gcc goes well, gives segmentation fault
As an exercise in c pointers, I was trying to merge together two programs that I found online that extract the title from a webshots image and then convert it to a jpg. I think that I'm handling the pointers correctly (returning a string from a function by passing the pointer), but although it compiles, it does not run correctly (gives a segmentation fault). The code is below. Any clues on where my error might be?
Thanks,
Jon
Code:
/* A program to convert .wbz files from webshots.com */
/* to .jpg files by stripping excess bytes before */
/* Jpeg Header and bytes for thumbnail image. */
/* */
/* The first format has the text Adobe in the file twice. */
/* A thumbnail is in between, which gets stripped. The */
/* The second type has encrypted header and so I decrypt */
/* with one of two keys. the first 100 bytes after WWBB0000 */
/* or WWBB1111 are replaced by the next 100 bytes which */
/* need to be decrypted first */
/* then the original 2nd 100 bytes then the rest of file. */
/* By Gyrf on 22 March 2001 */
/* Modified by jshine on 17 December 2004 */
#include <stdio.h>
#include <string.h>
#define ADOBE "Adobe"
#define ALGO1 "WWBB0000"
#define ALGO2 "WWBB1111"
#define HDR1 0xFF
#define HDR2 0xD8
int main (int argc,char *argv[])
{
FILE *fp1, *fp2;
char *infile, *outfile;
char *filenames[100];
char isalgo1[9];
char isadobe[6];
int letter,flag=0,flag1=0,ishdr1=0,ishdr2=0;
int verwb=0,cnt=0,cnt1=0,A[100],B[100],counter=0;
int magicvalue=0,C[100];
char *getfilenames(char *[],FILE *);
if(argc >= 1)
{
infile = argv[1];
outfile = getfilenames(filenames,fp1);
}
else
{
printf("Usage: wbz2jpg inputfile.wbz\n");
exit (1);
}
if((fp1 =fopen(infile,"r")) == NULL)
{
printf("Can't open infile %s\n",infile);
exit (0);
}
if((fp2 =fopen(outfile,"w")) == NULL)
{
printf("Can't open outfile %s\n",outfile);
fclose(fp1);
exit (0);
}
while((letter= fgetc(fp1)) != EOF)
{
if(letter==0xFF) /*first hex digit of jpeg */
{
ishdr1= letter;
ishdr2= fgetc(fp1);
if(HDR1==ishdr1 && HDR2==ishdr2)flag=1;
fseek(fp1,-1L,SEEK_CUR); /* back up one */
}
if(letter==0x41) /* A in Adobe */
{
isadobe[0]=letter;
isadobe[1]= fgetc(fp1);
isadobe[2]= fgetc(fp1);
isadobe[3]= fgetc(fp1);
isadobe[4]= fgetc(fp1);
isadobe[5]='\0';
fseek(fp1,-4L,SEEK_CUR); /* back up four */
if(strcmp(ADOBE,isadobe)==0)flag1++;
}
if(flag == 1 && flag1 != 1)
{
fseek(fp1,-1L,SEEK_CUR); /* Back up and read */
letter=fgetc(fp1); /* byte just analyzed */
fputc(letter,fp2); /* and write it to .jpg */
}
}
if(flag == 1 && flag1 > 1){
printf("%s written, type 1 Adobe enbedded.\n",outfile);
fclose(fp1);
fclose(fp2);
return 0;}
else{
rewind(fp1);
rewind(fp2);
}
while(cnt1<100 && ((letter= fgetc(fp1)) != EOF))
{
if(letter==0x57) /* W in WWBBxxxx */
{
isalgo1[0]=letter;
isalgo1[1]= fgetc(fp1);
isalgo1[2]= fgetc(fp1);
isalgo1[3]= fgetc(fp1);
isalgo1[4]= fgetc(fp1);
isalgo1[5]= fgetc(fp1);
isalgo1[6]= fgetc(fp1);
isalgo1[7]= fgetc(fp1);
isalgo1[8]='\0';
if(strcmp(ALGO1,isalgo1)==0)
{flag1++;verwb=1;}
if(strcmp(ALGO2,isalgo1)==0)
{flag1++;verwb=2;}
if(!verwb)fseek(fp1,-7L,SEEK_CUR);
}
/* matrix stuff goes here. write now decrypt next */
if(verwb>1)
{
if(verwb==1)magicvalue=0xA4;
if(verwb==2)magicvalue=0xF2;
while(cnt<100 && ((letter= fgetc(fp1)) != EOF)){
A[cnt]=letter;cnt++;
}
while(cnt1<100 && ((letter= fgetc(fp1)) != EOF)){
C[cnt1]=letter;B[cnt1]=letter;cnt1++;
}
/* decryption algorythm */
counter=0;
while(counter<100){
A[counter] = ~A[counter];
B[counter] = B[counter] ^ A[counter];
B[counter] = B[counter] ^ magicvalue;
counter++;
}
}
}
counter=0; /* altered 2nd 100 bytes */
while(counter<100)
{
letter=B[counter];
fputc(letter,fp2);
counter++;
}
counter=0; /* saved 2nd 100 bytes */
while(counter<100)
{
letter=C[counter];
fputc(letter,fp2);
counter++;
}
while((letter = fgetc(fp1)) != EOF)
{
fputc(letter,fp2);
}
printf("%s written, type %s encrypted.\n",outfile,isalgo1);
fclose(fp1);
fclose(fp2);
return 0;
}
char *getfilenames(char *filenames[],FILE *fp1)
{
int countlines=0,count=0,it_cnt=0,pig=0,letter;
char imagetitle[16];
static char nme[100];
while((letter = fgetc(fp1)) != EOF)
{
if(letter==0x53)
{
imagetitle[it_cnt++]=letter;
while(it_cnt<15 && (letter = fgetc(fp1)) != EOF)
{
imagetitle[it_cnt++]=letter;
}
imagetitle[it_cnt]='\0';
it_cnt=0;
if(strcmp("STR_ImageTitle=",imagetitle)==0)
{
count=0;
while((letter = fgetc(fp1)) != EOF && letter > 0x13)
/* 0D 0A end of title, 0x13 CR in there */
{
if(letter == 0x20)letter=0x5f;
if(letter != 0x2c){
if(letter>64 && letter<91)letter+=32;
nme[count++]=letter;
}
}
nme[count]='\0';
countlines++;
}
else{fseek(fp1,-14L,SEEK_CUR);}
}
}
strcat(nme,".jpg");
return nme;
}
1) you pass an array of char* to get_filenames but you never touch that parameter and the funtion never fills it in. What is it for?
2) There is the chance that the variable nme is not initialized before you call strcat() on it.
3) You should sprinkle the code with printf()s to see how far it gets.
4) You should compile the code with -g3 and execute it in gdb to see where it is crashing.
And, for the love of everything holy, work on your formatting. It's inconsistent, hard to follow, and that is a tried and true source of creeping bugs.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.