Hi Everyone,
I am having a few issues while attempting to use Kevin Easton's base64
encryption algorythm. (See http://groups.google.com/groups?selm...to.pcug.org.au)
here is a sample program I am using to test the algorythm.
----- start main.c -----
#include <stdio.h>
#include "base64.h"
int main(void)
{
char string[5] = "hello";
int len;
char string2[10];
len = strlen(string);
printf("%s\n", string);
b64_encode(string2, string, len);
printf("%s\n", string2);
return 0;
}
----- end main.c -----
I compile the program using:
gcc base64.c main.c -o base
And when I run it I get the following output.
mick@codegurus $ ./base
hello?(????
aGVsbG/wBCjG7L+/AQ==??????(
Does anyone know where its going wrong? 20 1849
Materialised wrote: Hi Everyone,
I am having a few issues while attempting to use Kevin Easton's base64 encryption algorythm. (See
<snip> char string[5] = "hello";
Needs to be size "6" to hold the null character.
Ed.
I'll have a go! NEWBIE ANSWER -- probably full of errors too :)
Materialised wrote: #include <stdio.h> #include "base64.h"
int main(void) { char string[5] = "hello";
string[0] = 'h'; /* this */
string[1] = 'e'; /* makes */
string[2] = 'l'; /* string */
string[3] = 'l'; /* 5 chars */
string[4] = 'o'; /* long! */
/* "hello" needs at least 6 chars: 'h', 'e', 'l', 'l', 'o', '\0' */
no more chars have been allocated
--> UB (undefined behaviour) ???
you are trying to copy '\0' to a place in memory you do not own.
You may want to let the compiler decide on the array size:
char string[] = "hello";
or specify enough size for your constants
char string[32] = "hello";
int len; char string2[10]; len = strlen(string);
I think strlen() will go on after the final 'o' checking for the null
terminator which can be _anywhere_
printf("%s\n", string);
same thing here
b64_encode(string2, string, len);
and here
printf("%s\n", string2);
return 0; }
----- end main.c -----
I compile the program using:
gcc base64.c main.c -o base
And when I run it I get the following output. mick@codegurus $ ./base hello?(????
Lucky you got a '\0' not very far away :)
--
USENET would be a better place if everybody read: : mail address : http://www.catb.org/~esr/faqs/smart-questions.html : is valid for : http://www.netmeister.org/news/learn2quote2.html : "text/plain" : http://www.expita.com/nomime.html : to 10K bytes :
Pedro Graca <he****@hotpop.com> wrote: Materialised wrote: #include <stdio.h> #include "base64.h"
int main(void) { char string[5] = "hello"; string[0] = 'h'; /* this */ string[1] = 'e'; /* makes */ string[2] = 'l'; /* string */ string[3] = 'l'; /* 5 chars */ string[4] = 'o'; /* long! */ /* "hello" needs at least 6 chars: 'h', 'e', 'l', 'l', 'o', '\0' */
no more chars have been allocated
--> UB (undefined behaviour) ???
Not undefined behaviour.
you are trying to copy '\0' to a place in memory you do not own.
It's actually a special case of character array
initialisation -- if there is not enough space for the
terminating null, it will not be assigned.
--
Michael
On Fri, 02 Apr 2004 05:09:07 +0000, Michael Fyles wrote: Pedro Graca <he****@hotpop.com> wrote: Materialised wrote: #include <stdio.h> #include "base64.h"
int main(void) { char string[5] = "hello"; string[0] = 'h'; /* this */ string[1] = 'e'; /* makes */ string[2] = 'l'; /* string */ string[3] = 'l'; /* 5 chars */ string[4] = 'o'; /* long! */ /* "hello" needs at least 6 chars: 'h', 'e', 'l', 'l', 'o', '\0' */
no more chars have been allocated
--> UB (undefined behaviour) ???
Not undefined behaviour.
Until you call a function which expects a C string. Without the
terminating NUL, it is not a string. you are trying to copy '\0' to a place in memory you do not own.
It's actually a special case of character array initialisation -- if there is not enough space for the terminating null, it will not be assigned.
--Mac
Michael Fyles <mf@dcs.warwick.ac.uk> spoke thus: Not undefined behaviour.
But OP's attempt to print it was, correct?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Materialised dropped $0.03 in the slot and wrote: I am having a few issues while attempting to use Kevin Easton's base64 encryption algorythm.
<snip> here is a sample program I am using to test the algorythm.
----- start main.c -----
#include <stdio.h> #include "base64.h"
int main(void) { char string[5] = "hello";
Bzzzt-----------------^^
Here is your problem. "hello" takes six chars (when you include the
terminating 0)
make that:
char string[] = "hello";
and let the compiler count the number of chars needed. int len; char string2[10]; len = strlen(string); printf("%s\n", string); b64_encode(string2, string, len); printf("%s\n", string2);
return 0; }
----- end main.c -----
I compile the program using:
gcc base64.c main.c -o base
And when I run it I get the following output. mick@codegurus $ ./base hello?(???? aGVsbG/wBCjG7L+/AQ==??????(
Does anyone know where its going wrong?
Yep you thought that "hello" was five chars long :-)
Roger
Materialised wrote: here is a sample program I am using to test the algorythm.
^^^^^^^^^
I like this word :) Yeah, I got good algorythm.
--
Thomas.
Thomas Stegen wrote: Materialised wrote: here is a sample program I am using to test the algorythm. ^^^^^^^^^
I like this word :) Yeah, I got good algorythm.
Algorhythm,
Algomusic,
Algorhythm,
Who could ask for anything more?
-- Er*********@sun.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Eric Sosman wrote:
| Thomas Stegen wrote:
|
|>Materialised wrote:
|>
|>>here is a sample program I am using to test the algorythm.
|>
|> ^^^^^^^^^
|>
|>I like this word :) Yeah, I got good algorythm.
|
|
| Algorhythm,
| Algomusic,
| Algorhythm,
| Who could ask for anything more?
"Algorythm" sounds like a method of birth control for programmers ;-)
- --
Lew Pitcher, IT Consultant, Enterprise Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
iD8DBQFAbb6LagVFX4UWr64RAqT6AKCXg0DmOffJRQ2PAvPyKz ySzkTKSACeLz2U
zwnH0ydnwt66a8BMj+Tjrx0=
=6A5R
-----END PGP SIGNATURE-----
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in
news:c4**********@chessie.cirr.com: Michael Fyles <mf@dcs.warwick.ac.uk> spoke thus:
Not undefined behaviour.
But OP's attempt to print it was, correct?
Indeed. printf() (and all other string-expecting C functions, whether or
not the Standard mentions them) expects a null-terminated string.
In <0L********************@onewest.net> August Derleth <se*@sig.now> writes: Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in news:c4**********@chessie.cirr.com:
Michael Fyles <mf@dcs.warwick.ac.uk> spoke thus:
Not undefined behaviour.
But OP's attempt to print it was, correct?
Indeed. printf() (and all other string-expecting C functions, whether or not the Standard mentions them) expects a null-terminated string.
Wrong!
fangorn:~/tmp 38> cat test.c
#include <stdio.h>
int main(void)
{
char word[5] = "hello";
printf("%.5s\n", word);
return 0;
}
fangorn:~/tmp 39> gcc test.c
fangorn:~/tmp 40> ./a.out
hello
And, to prove that it worked by design and not by accident:
s If no l length modifier is present, the argument shall
be a pointer to the initial element of an array of
character type. Characters from the array are
written up to (but not including) the terminating null
character. If the precision is specified, no more than
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
that many bytes are written. If the precision is not
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^
specified or is greater than the size of the array,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
the array shall contain a null character.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The standard is usually very precise WRT when a null-terminated string
is actually expected and when any ordinary character array will do.
No need for bogus rules of thumb.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In <0L********************@onewest.net> August Derleth <se*@sig.now> writes: Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in news:c4**********@chessie.cirr.com:
Michael Fyles <mf@dcs.warwick.ac.uk> spoke thus:
Not undefined behaviour.
But OP's attempt to print it was, correct?
Indeed. printf() (and all other string-expecting C functions, whether or not the Standard mentions them) expects a null-terminated string.
Wrong!
fangorn:~/tmp 38> cat test.c
#include <stdio.h>
int main(void)
{
char word[5] = "hello";
printf("%.5s\n", word);
return 0;
}
fangorn:~/tmp 39> gcc test.c
fangorn:~/tmp 40> ./a.out
hello
And, to prove that it worked by design and not by accident:
s If no l length modifier is present, the argument shall
be a pointer to the initial element of an array of
character type. Characters from the array are
written up to (but not including) the terminating null
character. If the precision is specified, no more than
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
that many bytes are written. If the precision is not
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^
specified or is greater than the size of the array,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
the array shall contain a null character.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The standard is usually very precise WRT when a null-terminated string
is actually expected and when any ordinary character array will do.
No need for bogus rules of thumb.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
August Derleth wrote: Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in news:c4**********@chessie.cirr.com:
Michael Fyles <mf@dcs.warwick.ac.uk> spoke thus:
Not undefined behaviour.
But OP's attempt to print it was, correct?
Indeed. printf() (and all other string-expecting C functions, whether or not the Standard mentions them) expects a null-terminated string.
Nit-pick: If the "%s" conversion is used with a precision
(e.g. "%.10s" or "%.*s") the corresponding argument need only
point to the start of a character array, not necessarily zero-
terminated.
char hello[5] = "hello"; /* no terminator */
printf ("%s\n", hello); /* undefined behavior */
printf ("%.4s\n", hello); /* well-defined; prints "hell\n" */
-- Er*********@sun.com
August Derleth wrote: Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote in news:c4**********@chessie.cirr.com:
Michael Fyles <mf@dcs.warwick.ac.uk> spoke thus:
Not undefined behaviour.
But OP's attempt to print it was, correct?
Indeed. printf() (and all other string-expecting C functions, whether or not the Standard mentions them) expects a null-terminated string.
Nit-pick: If the "%s" conversion is used with a precision
(e.g. "%.10s" or "%.*s") the corresponding argument need only
point to the start of a character array, not necessarily zero-
terminated.
char hello[5] = "hello"; /* no terminator */
printf ("%s\n", hello); /* undefined behavior */
printf ("%.4s\n", hello); /* well-defined; prints "hell\n" */
-- Er*********@sun.com
Dan Pop <Da*****@cern.ch> spoke thus: char word[5] = "hello"; printf("%.5s\n", word);
Or, better yet,
printf( "%.*s\n", sizeof word, word );
Right?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Dan Pop <Da*****@cern.ch> spoke thus: char word[5] = "hello"; printf("%.5s\n", word);
Or, better yet,
printf( "%.*s\n", sizeof word, word );
Right?
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Christopher Benson-Manica wrote: Dan Pop <Da*****@cern.ch> spoke thus:
char word[5] = "hello"; printf("%.5s\n", word);
Or, better yet,
printf( "%.*s\n", sizeof word, word );
Right?
printf ("%.*s\n", (int)(sizeof word), word);
-- Er*********@sun.com
Christopher Benson-Manica wrote: Dan Pop <Da*****@cern.ch> spoke thus:
char word[5] = "hello"; printf("%.5s\n", word);
Or, better yet,
printf( "%.*s\n", sizeof word, word );
Right?
printf ("%.*s\n", (int)(sizeof word), word);
-- Er*********@sun.com
In <c4**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes: Dan Pop <Da*****@cern.ch> spoke thus:
char word[5] = "hello"; printf("%.5s\n", word);
Or, better yet,
printf( "%.*s\n", sizeof word, word );
Right?
Wrong. Undefined behaviour is never better than well defined behaviour.
Even if correctly done, your variant is ludicrous for my trivial example
and unlikely to be of much use in real life code, where the actual value
is seldom dictated by the size of the array (whose definition may not
even be in scope).
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In <c4**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes: Dan Pop <Da*****@cern.ch> spoke thus:
char word[5] = "hello"; printf("%.5s\n", word);
Or, better yet,
printf( "%.*s\n", sizeof word, word );
Right?
Wrong. Undefined behaviour is never better than well defined behaviour.
Even if correctly done, your variant is ludicrous for my trivial example
and unlikely to be of much use in real life code, where the actual value
is seldom dictated by the size of the array (whose definition may not
even be in scope).
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Oz |
last post by:
This is long. Bear with me, as I will really go through all the convoluted
stuff that shows there is a problem with streams (at least when used to
redirect stdout).
The basic idea is that my...
|
by: LineVoltageHalogen |
last post by:
Greetings All, I was hoping that someone out there has run into this
issue before and can shed some light on it for me. I have a stored
procedure that essentially does a mini ETL from a source...
|
by: grundmann |
last post by:
Hello,
i got a strange compiler error.
When compiling the following:
// forward declarations
typedef AvlTree<LineSegment,LineSegmentComperator> LSTree;
void handleEventPoint (const...
|
by: leonecla |
last post by:
Hi everybody,
I'm facing a very very strange problem with a very very simple C
program...
My goal should be to write to a binary file some numbers (integers),
each one represented as a sequence...
|
by: Martin Feuersteiner |
last post by:
Dear Group
I'm having a very weird problem. Any hints are greatly appreciated.
I'm returning two values from a MS SQL Server 2000 stored procedure to my
Webapplication and store them in...
|
by: soeren |
last post by:
Hello,
two days ago I stumbled across a very strange problem that came up when
we were printing tiny double numbers as strings and trying to read them
on another place. This is part of an object...
|
by: Ian |
last post by:
Hi everyone,
I have found some bizarre (to me...!) behaviour of the Form_Activate
function. I have a form which has a button control used to close the
form and a subform with a datasheet view...
|
by: Paul |
last post by:
Hi all,
I've hit a strange problem with my ASP .NET site - occasionally when I
go to a page, it will have part of another request's output mixed in
with my output. This breaks the HTML on the...
|
by: ZuLuuuuuu |
last post by:
Hello, I have a strange problem or the problem is very obvious and I can't see it :) I'm kind of a new C programmer so the chance of the latter is more :)
Anyway, I have a structure like this:
...
|
by: danep2 |
last post by:
Hello all
This is a really strange problem. I have code that performs a few
calculations based on input from a joystick, and writes these values
to a file using basically the following code:
...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |