Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > "pointer to char" address restoring problem

Reply
Thread Tools

"pointer to char" address restoring problem

 
 
Mahesh
Guest
Posts: n/a
 
      02-21-2008
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main(void)
{
int key = 120;
char *s = "String to Encrypt using XOR";
char *ss = s;
char echar;
char dchar;
size_t sizeofmsg = strlen(s)*sizeof(char*);

char *emsg = (char*)malloc(sizeofmsg);
char *dmsg = (char*)malloc(sizeofmsg);

char *ddmsg = dmsg;
char *eemsg = emsg;
int lenofmsg = strlen(s);
int count=0;
int i=0;

while(count++<strlen(s)){

echar = (char)(*ss^key);
emsg = &echar;
emsg++;
ss++;
}
count=0;
emsg = eemsg; // HERE when i restore the address of emsg via
eemsg, i'm not able to
// decrypt. How to restore the address of
original pointer to char ?.
i=0;
printf("\n");
while(count++<strlen(s)){

echar = *emsg;
dchar = (char)(echar ^ key);
ddmsg = &dchar;
printf(" %c",dchar);
emsg++;
ddmsg++;
}
printf("\n");
getch();
return 0;
}
 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      02-21-2008
Mahesh wrote:
> #include <stdio.h>
> #include <string.h>
> #include <malloc.h>


Not a standard C header; use <stdlib.h> for malloc(), realloc(),
free().

> int main(void)
> {
> int key = 120;
> char *s = "String to Encrypt using XOR";
> char *ss = s;
> char echar;
> char dchar;
> size_t sizeofmsg = strlen(s)*sizeof(char*);


Wrong calculation.
It should be (number of elements)*(size of the type s points to).
That is,
sizeofmsg = (strlen(s) + 1) * sizeof *s;
as s points to char and sizeof (char) is guaranteed to be 1,
you can write
sizeofmsg = strlen(s) + 1;
The "+1" stems from the string terminator that is not included
in the string length but contributes to the string's size.

>
> char *emsg = (char*)malloc(sizeofmsg);
> char *dmsg = (char*)malloc(sizeofmsg);


Note that the cast is unnecessary and can hide an error; around
here,
T *p = malloc(NumberOfElems * sizeof *p);
is recommended.

> char *ddmsg = dmsg;
> char *eemsg = emsg;
> int lenofmsg = strlen(s);
> int count=0;
> int i=0;
>
> while(count++<strlen(s)){
>
> echar = (char)(*ss^key);


Note that XOR encryption of the value key leads to 0 ('\0'),
which also is the string terminator.

> emsg = &echar;


You probably mean
*emsg = echar;

&echar is just that, the address of echar; you do not change
the contents of the storage area you got from malloc().
So, in every iteration, you point emsg at the same object.

Note that for s, you iterate on the copy, and for emsg, you
iterate on the "original". This can lead to problems.

> emsg++;
> ss++;
> }
> count=0;
> emsg = eemsg; // HERE when i restore the address of emsg via
> eemsg, i'm not able to
> // decrypt. How to restore the address of
> original pointer to char ?.


// style comments break easily on usenet; I recommend that you use
/**/ comments which cannot change semantics or "compilability" due
to line breaks

> i=0;
> printf("\n");
> while(count++<strlen(s)){
>
> echar = *emsg;
> dchar = (char)(echar ^ key);
> ddmsg = &dchar;
> printf(" %c",dchar);
> emsg++;
> ddmsg++;
> }
> printf("\n");
> getch();
> return 0;
> }


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
Mahesh
Guest
Posts: n/a
 
      02-21-2008
On Feb 21, 12:37*pm, Michael Mair <Michael.M...@invalid.invalid>
wrote:
> Mahesh wrote:
> > #include <stdio.h>
> > #include <string.h>
> > #include <malloc.h>

>
> Not a standard C header; use <stdlib.h> for malloc(), realloc(),
> free().
>
> > int main(void)
> > {
> > * * int key = 120;
> > * * char *s = "String to Encrypt using XOR";
> > * * char *ss = s;
> > * * char echar;
> > * * char dchar;
> > * * size_t sizeofmsg = strlen(s)*sizeof(char*);

>
> Wrong calculation.
> It should be (number of elements)*(size of the type s points to).
> That is,
> * sizeofmsg = (strlen(s) + 1) * sizeof *s;
> as s points to char and sizeof (char) is guaranteed to be 1,
> you can write
> * sizeofmsg = strlen(s) + 1;
> The "+1" stems from the string terminator that is not included
> in the string length but contributes to the string's size.
>
>
>
> > * * char *emsg = (char*)malloc(sizeofmsg);
> > * * char *dmsg = (char*)malloc(sizeofmsg);

>
> Note that the cast is unnecessary and can hide an error; around
> here,
> * T *p = malloc(NumberOfElems * sizeof *p);
> is recommended.
>
> > * * char *ddmsg = dmsg;
> > * * char *eemsg = emsg;
> > * * int lenofmsg = strlen(s);
> > * * int count=0;
> > * * int i=0;

>
> > * * while(count++<strlen(s)){

>
> > * * * * echar = (char)(*ss^key);

>
> Note that XOR encryption of the value key leads to 0 ('\0'),
> which also is the string terminator.
>
> > * * * * emsg = &echar;

>
> You probably mean
> * *emsg = echar;
>
> &echar is just that, the address of echar; you do not change
> the contents of the storage area you got from malloc().
> So, in every iteration, you point emsg at the same object.
>
> Note that for s, you iterate on the copy, and for emsg, you
> iterate on the "original". This can lead to problems.
>
> > * * * * emsg++;
> > * * * * ss++;
> > * * }
> > * * count=0;
> > * * emsg = eemsg; *// HERE when i restore the address of emsg via
> > eemsg, i'm not able to
> > * * * * * * * * * * * * * * // decrypt. How to restore the address of
> > original pointer to char ?.

>
> // style comments break easily on usenet; I recommend that you use
> /**/ comments which cannot change semantics or "compilability" due
> to line breaks
>
> > * * i=0;
> > * * printf("\n");
> > * * while(count++<strlen(s)){

>
> > * * * * echar = *emsg;
> > * * * * dchar = (char)(echar ^ key);
> > * * * * ddmsg = &dchar;
> > * * * * printf(" %c",dchar);
> > * * * * emsg++;
> > * * * * ddmsg++;
> > * * }
> > * * printf("\n");
> > * * getch();
> > * * return 0;
> > }

>
> Cheers
> * Michael
> --
> E-Mail: Mine is an * /at/ gmx /dot/ de * address.


Thanks you so much Michael for you invaluable reply.

-
Mahesh
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      02-21-2008
Mahesh wrote:
>
> On Feb 21, 12:37 pm, Michael Mair <Michael.M...@invalid.invalid>
> wrote:
> > Mahesh wrote:
> > > #include <stdio.h>
> > > #include <string.h>
> > > #include <malloc.h>

> >
> > Not a standard C header; use <stdlib.h> for malloc(), realloc(),
> > free().
> >
> > > int main(void)
> > > {
> > > int key = 120;
> > > char *s = "String to Encrypt using XOR";
> > > char *ss = s;
> > > char echar;
> > > char dchar;
> > > size_t sizeofmsg = strlen(s)*sizeof(char*);

> >
> > Wrong calculation.
> > It should be (number of elements)*(size of the type s points to).
> > That is,
> > sizeofmsg = (strlen(s) + 1) * sizeof *s;
> > as s points to char and sizeof (char) is guaranteed to be 1,
> > you can write
> > sizeofmsg = strlen(s) + 1;
> > The "+1" stems from the string terminator that is not included
> > in the string length but contributes to the string's size.


These two idioms are both worth knowing:

ptr = malloc(NMEMB * sizeof *ptr);

ptr = malloc(STRING_LENGTH + 1);

--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Restoring a Profile RyeTronics Firefox 2 07-08-2006 05:54 AM
Problem Restoring a Profile RyeTronics Firefox 0 07-08-2006 01:55 AM
Restoring a drive image problem Dan Computer Support 2 12-26-2004 11:29 AM
DriveImage problem restoring =?UTF-8?B?UGFsaW5kcuKYu21l?= Computer Support 22 04-13-2004 03:56 AM
problem restoring my Outlook Express Meggie Computer Support 15 09-13-2003 03:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57