Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Segmentation fault reading char array 1

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

Following a previous post I managed to create a code to encrypt Tcl code into C; now the problem is to decrypt it to be "Evaled"

The tcl code is stored as follows:
static char tcl_script[] =
"«šíðéåøäíŠüã\n"
"«šÌíåçæûüúéüíûšÜëä§ÜãšéæìšËšáæüíúéëüáçæšÿàáäíšìçáæïšûçåíüàáæïšýûíîýä©\n"
"«šÇúáïáæéääñšÿúáüüíæšíæüáúíäñšáæšÜëä§ÜãšêýüšéììíìšËšëéääûšéûšéšìíåçŠ\n"
"\n"
"«šºžšÎíêš¹±±œš¥šÚŠšÀéïíæšúàéïíæÈéÿá¥êúíåíúàéþíæŠìí\n"
"«š¹ŒšÉøúá䚺žž»šÎšËçýüíäšîëçýüíäÈäïëŠëçåšåçìáîáíìš\n"
"\n"
"«š¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥\n"
"«šÉšûáåøäíšìíëáåéäšìíïúííšüçšìíï€åáæ€ûíëšëçæþíúüíú\n"

...

Now in the code I have tried
char *t = tcl_script;
int xor 14;
while (*t) {
while (*t != '\n') {
/* decrypt*/
if ( *t>=0x20 ){ *t ^= xor; xor = (xor+1)&0x1f; }

}
t++;

}

This is causing a segmentation fault. why?

Thanks!
 
No idea - your code doesn't even compile.

> "«šíðéåøäíŠüã\n"
This is very unportable code.

Make your crypt function output strings in unambiguous hexadecimal like

Code:
char tcl_crypt[] = {
  "\xaa\xda\x34\x9f\n"



--
 
Some days ago I offered this (hexa) form to the author (in another his/her thread on this topic), alas...
For some reason he (she?) rejects the proposition...
It's a very interesting style: declare const array then modify it after obviously incorrect pointer assignment...
 
Salem:
This is strange I get not errors when I compile it??
The fucntion I use to encrypt is the same as to decrypt:
in xor=14
if ( *t>=0x20 ){ *t ^= xor; xor = (xor+1)&0x1f; }

what value for xor should I take then?

ArkM:
I am certainly not as good as you, simply trying to write some C code the best I can. The only solution you proposed is to use 3rd party software which is not acceptable. This should be done in C.
I have tried coping tcl_script to a new variable with
d= (char*) malloc(sizeof(tcl_script));
strcpy(d,tcl_script);

while (*t) {
while (*t != '\n') {
*d++ = *t++;
if ( *t>=0x20 ){ *d = *t^xor; xor = (xor+1)&0x1f; }
}
t++;
}

but this does not work either. As you pointed out there is an assignement pointer problem is the above not allowed?


 
Look at this
Code:
/* encrypt.c */
#include <stdio.h>
#include <ctype.h>

/* creates a 'C' char array called encrypt with the encrypted message */
void encrypt ( char *msg ) {
    char ch;
    int  xor = 14;
    printf( "char encrypt[] = \n\"" );  /* start of 'C' char array */
    while ( (ch=*msg++) != '\0' ) {
        if ( ch == '\n' ) {
            printf( "\\n\"\n\"" );      /* ends of a string fragment */
        } else
        if ( isspace(ch) ) {
            printf( " " );
        } else {
            ch = ch ^ xor;
            xor = (xor+1)&0x1f;
            printf( "\\x%02x", ch );    /* unambiguous characters */
        }
    }
    printf( "\";\n" );
}


int main ( ) {
    char msg[] = "this is a message\n"
        "spread over\n"
        "several lines\n";
    encrypt( msg );
    return 0;
}

Code:
/* decrypt.c */
#include <stdio.h>
#include <ctype.h>

/* this is the file you get by doing */
/* encrypt > encrypt.h */
#include "encrypt.h"

void decrypt ( char *msg ) {
    char ch;
    int  xor = 14;
    while ( (ch=*msg++) != '\0' ) {
        if ( ch == '\n' ) {
            printf( "\n" );
        } else
        if ( isspace(ch) ) {
            printf( " " );
        } else {
            ch = ch ^ xor;
            xor = (xor+1)&0x1f;
            printf( "%c", ch );
        }
    }
}

int main ( ) {
    decrypt( encrypt );
    return 0;
}

Using these commands to compile and run the programs
Code:
$ gcc encrypt.c
$ ./a.out > encrypt.h
$ gcc decrypt.c
$ ./a.out
this is a message
spread over
several lines

--
 
Salem: thanks a lot this helps however what I really need is to store the decrypted char into a new char [] which will be "evalled" by a Tcl function all at once not line by line.

Thanks!
 
fabien
" which will be "evalled" by a Tcl "
if you use progA in conjunction with progB
let the faster one do the job, in your exp
tcl is not the faster :(
just an opinion.
 
> what I really need is to store the decrypted char into a new char []
So replace a printf with say
Code:
buff[i++] = ch;

Once you've got a decrypted ch, do whatever you want with it

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top