LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-17-2004, 06:06 PM   #1
jshine
LQ Newbie
 
Registered: Dec 2004
Location: Rochester, MN
Posts: 2

Rep: Reputation: 0
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;

	}
 
Old 12-17-2004, 09:51 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
My eyes hurt...
 
Old 12-17-2004, 09:52 PM   #3
bm17
Member
 
Registered: Sep 2004
Location: Santa Cruz, CA, USA
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
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.
 
Old 12-17-2004, 10:08 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
Old 12-18-2004, 09:21 AM   #5
Dommy
Member
 
Registered: Jul 2004
Location: Canberra
Distribution: Mint 7
Posts: 204

Rep: Reputation: 30
Make nme a global and initialize it to something

static char nme[100];

The above should work but I've seen it fail
 
Old 12-18-2004, 05:53 PM   #6
jshine
LQ Newbie
 
Registered: Dec 2004
Location: Rochester, MN
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for your ideas everyone. I've gotten it working (and cleaned up a bit).

Jon
 
Old 12-19-2004, 02:08 AM   #7
Dommy
Member
 
Registered: Jul 2004
Location: Canberra
Distribution: Mint 7
Posts: 204

Rep: Reputation: 30
Quote:
Originally posted by jshine
Thanks for your ideas everyone. I've gotten it working (and cleaned up a bit).

Jon
Care to say what you did to get it to work, I'd certainly be interested
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC Compiling (Linux), "Segmentation Fault" Kenji Miyamoto Programming 2 01-13-2005 02:44 AM
gcc, segmentation fault, though compiles... scratch09 Programming 5 11-20-2004 06:11 PM
Slackwaer 9.1 GCC Compiling Segmentation Fault kdepa Slackware 2 03-15-2004 07:31 PM
[gcc] internal error: segmentation fault fskmh Slackware 3 01-22-2003 06:36 PM
gcc / segmentation fault skeletal29 Linux - Software 0 05-05-2002 05:05 AM


All times are GMT -5. The time now is 06:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration