Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bio_write

76 views
Skip to first unread message

jaze lee

unread,
May 28, 2009, 4:16:09 AM5/28/09
to
hello,
#include <openssl/bio.h>
2 int main() {
3 BIO * b;
4 char buf[100] = "hello world \n";
5 b = BIO_new(BIO_s_file());
6 BIO_set_fp(b, stdout, BIO_NOCLOSE);
7 BIO_write(b, buf, sizeof(buf));
8 return 0;
9 }
after compile , and run, i can see the hello world
but if the source code be changed like this
1 #include <openssl/bio.h>
2 int biotest(char * buf) {
3 BIO * b;
4 // char buf[100] = "hello world \n";
5 b = BIO_new(BIO_s_file());
6 BIO_set_fp(b, stdout, BIO_NOCLOSE);
7 BIO_write(b, buf, sizeof(buf));
8 return 0;
9 }
10 int main()
11 {
12 char buf[100] = "hello world\n";
13 biotest(buf);
14 return 0;
15 }
compile and run, i can only see the hell on my terminal , who knows why, thank
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List openss...@openssl.org
Automated List Manager majo...@openssl.org

Ger Hobbelt

unread,
May 28, 2009, 5:52:10 AM5/28/09
to
This is a classic/basic 'C' programming mistake you made, not an OpenSSL on=
e:

pointers are not arrays are not strings ;-)

sizeof(buf) =3D=3D ?

buf is of type 'char *' and therefore sizeof(buf) =3D=3D sizeof(char *)
which is probably 4 or 8, depending on what platform you build this
for.
If you wish to provide the length of the C string data, pointed at by
pointer 'buf', then strlen() is your man:

either

BIO_puts(b, buf) -- which does this internally

or

BIO_write(b, buf, strlen(buf))

On Thu, May 28, 2009 at 10:15 AM, jaze lee <jaz...@gmail.com> wrote:
> hello,
> =A0 =A0 =A0#include <openssl/bio.h>
> =A02 int main() =A0{
> =A03 =A0 =A0 BIO * b;
> =A04 =A0 =A0char buf[100] =3D "hello world \n";
> =A05 =A0 =A0 b =3D BIO_new(BIO_s_file());
> =A06 =A0 =A0 BIO_set_fp(b, stdout, BIO_NOCLOSE);
> =A07 =A0 =A0 BIO_write(b, buf, sizeof(buf));
> =A08 =A0 =A0 return 0;
> =A09 }


> after compile , and run, i can see the hello world
> but if the source code be changed like this

> =A01 #include <openssl/bio.h>
> =A02 int biotest(char * buf) =A0{
> =A03 =A0 =A0 BIO * b;
> =A04 // =A0char buf[100] =3D "hello world \n";
> =A05 =A0 =A0 b =3D BIO_new(BIO_s_file());
> =A06 =A0 =A0 BIO_set_fp(b, stdout, BIO_NOCLOSE);
> =A07 =A0 =A0 BIO_write(b, buf, sizeof(buf));
> =A08 =A0 =A0 return 0;
> =A09 }
> =A010 int main()
> =A011 {
> =A012 =A0 =A0 char buf[100] =3D "hello world\n";
> =A013 =A0 =A0 biotest(buf);
> =A014 =A0 =A0 return 0;
> =A015 }
> compile and run, i can only see the hell on my terminal , who knows why, =
thank
> ______________________________________________________________________
> OpenSSL Project =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 =A0 http://www.openssl.org
> User Support Mailing List =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0openssl-=
us...@openssl.org
> Automated List Manager =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0 majo...@openssl.org
>
>

--=20
Met vriendelijke groeten / Best regards,

Ger Hobbelt

--------------------------------------------------
web: http://www.hobbelt.com/
http://www.hebbut.net/
mail: g...@hobbelt.com
mobile: +31-6-11 120 978
--------------------------------------------------

Victor Duchovni

unread,
May 28, 2009, 10:36:22 AM5/28/09
to
On Thu, May 28, 2009 at 11:51:42AM +0200, Ger Hobbelt wrote:

> This is a classic/basic 'C' programming mistake you made, not an OpenSSL one:


>
> pointers are not arrays are not strings ;-)
>

> sizeof(buf) == ?
>
> buf is of type 'char *' and therefore sizeof(buf) == sizeof(char *)


> which is probably 4 or 8, depending on what platform you build this
> for.
> If you wish to provide the length of the C string data, pointed at by
> pointer 'buf', then strlen() is your man:
>
> either
>
> BIO_puts(b, buf) -- which does this internally
>
> or
>
> BIO_write(b, buf, strlen(buf))

Only if the data is text. Using strlen() on binary data is another
classic/basic 'C' programming mistake.

--
Viktor.

jazeltq

unread,
May 28, 2009, 10:21:42 PM5/28/09
to
------=_Part_8222_32006444.1243560439637
Content-Type: text/plain; charset=gbk
Content-Transfer-Encoding: quoted-printable


=D4=DA2009-05-28=A3=AC"Victor Duchovni" <Victor....@morganstanley.com>=
=D0=B4=B5=C0=A3=BA


>On Thu, May 28, 2009 at 11:51:42AM +0200, Ger Hobbelt wrote:
>

>> This is a classic/basic 'C' programming mistake you made, not an OpenSSL=
one:
>>=20


>> pointers are not arrays are not strings ;-)

>>=20
>> sizeof(buf) =3D=3D ?
>>=20
>> buf is of type 'char *' and therefore sizeof(buf) =3D=3D sizeof(char *)


>> which is probably 4 or 8, depending on what platform you build this
>> for.
>> If you wish to provide the length of the C string data, pointed at by
>> pointer 'buf', then strlen() is your man:

>>=20
>> either
>>=20


>> BIO_puts(b, buf) -- which does this internally

>>=20
>> or
>>=20


>> BIO_write(b, buf, strlen(buf))
>
>Only if the data is text. Using strlen() on binary data is another
>classic/basic 'C' programming mistake.

if it is binary data, what to do ? Use a Variable to send the the size ?
>
>--=20
>=09Viktor.


>______________________________________________________________________
>OpenSSL Project http://www.openssl.org
>User Support Mailing List openss...@openssl.org
>Automated List Manager majo...@openssl.org

------=_Part_8222_32006444.1243560439637
Content-Type: text/html; charset=gbk
Content-Transfer-Encoding: quoted-printable

<div><br><br></div><div></div><br><pre>=D4=DA2009-05-28=A3=AC"Victor Duchov=
ni" &lt;Victor....@morganstanley.com&gt; =D0=B4=B5=C0=A3=BA<br>&gt;On =
Thu, May 28, 2009 at 11:51:42AM +0200, Ger Hobbelt wrote:<br>&gt;<br>&gt;&g=
t; This is a classic/basic 'C' programming mistake you made, not an OpenSSL=
one:<br>&gt;&gt; <br>&gt;&gt; pointers are not arrays are not strings ;-)<=
br>&gt;&gt; <br>&gt;&gt; sizeof(buf) =3D=3D ?<br>&gt;&gt; <br>&gt;&gt; buf =
is of type 'char *' and therefore sizeof(buf) =3D=3D sizeof(char *)<br>&gt;=
&gt; which is probably 4 or 8, depending on what platform you build this<br=
>&gt;&gt; for.<br>&gt;&gt; If you wish to provide the length of the C strin=
g data, pointed at by<br>&gt;&gt; pointer 'buf', then strlen() is your man:=
<br>&gt;&gt; <br>&gt;&gt; either<br>&gt;&gt; <br>&gt;&gt; BIO_puts(b, buf) =
-- which does this internally<br>&gt;&gt; <br>&gt;&gt; or<br>&gt;&gt; <br>&=
gt;&gt; BIO_write(b, buf, strlen(buf))<br>&gt;<br>&gt;Only if the data is t=
ext. Using strlen() on binary data is another<br>&gt;classic/basic 'C' prog=
ramming mistake.<br>if it is binary data, what to do ? Use a Variable to s=
end the the size ?<br>&gt;
&gt;--=20
&gt;=09Viktor.
&gt;______________________________________________________________________
&gt;OpenSSL Project http://www.openssl.org
&gt;User Support Mailing List openss...@openssl.org
&gt;Automated List Manager majo...@openssl.org
</pre><br><!-- footer --><br><span title=3D"neteasefooter"/><hr/>
<a href=3D"http://512.mail.163.com/mailstamp/stamp/dz/activity.do?from=3Dfo=
oter">=B4=A9=D4=BD=B5=D8=D5=F0=B4=F8 =BC=CD=C4=EE=E3=EB=B4=A8=B5=D8=D5=F0=
=D2=BB=D6=DC=C4=EA</a>
</span>
------=_Part_8222_32006444.1243560439637--

Ger Hobbelt

unread,
May 29, 2009, 2:09:36 AM5/29/09
to
2009/5/29 jazeltq <jaz...@163.com>:

> 在2009-05-28,"Victor Duchovni" <Victor....@morganstanley.com> 写道:
>>On Thu, May 28, 2009 at 11:51:42AM +0200, Ger Hobbelt wrote:
>>
>>Only if the data is text. Using strlen() on binary data is another
>>classic/basic 'C' programming mistake.
> if it is binary data, what to do ? Use a Variable to send the the size ?

yes.

Just like the BIO_read, BIO_write, fwrite, etc. APIs all do too: pass
along a pointer /and/ a length parameter.

--

Met vriendelijke groeten / Best regards,

Ger Hobbelt

--------------------------------------------------
web: http://www.hobbelt.com/
http://www.hebbut.net/
mail: g...@hobbelt.com
mobile: +31-6-11 120 978
--------------------------------------------------

jazeltq

unread,
May 29, 2009, 11:35:34 PM5/29/09
to
------=_Part_74341_12357036.1243643778317

Content-Type: text/plain; charset=gbk
Content-Transfer-Encoding: quoted-printable


=D4=DA2009-05-29=A3=AC"Ger Hobbelt" <g...@hobbelt.com> =D0=B4=B5=C0=A3=BA
>2009/5/29 jazeltq <jaz...@163.com>:
>> =D4=DA2009-05-28=A3=AC"Victor Duchovni" <Victor.Duchovni@morganstanley.c=
om> =D0=B4=B5=C0=A3=BA


>>>On Thu, May 28, 2009 at 11:51:42AM +0200, Ger Hobbelt wrote:
>>>
>>>Only if the data is text. Using strlen() on binary data is another
>>>classic/basic 'C' programming mistake.

>> if it is binary data, what to do ? Use a Variable to send the the size =


?
>
>yes.
>
>Just like the BIO_read, BIO_write, fwrite, etc. APIs all do too: pass
>along a pointer /and/ a length parameter.

Thank you, i get it
>
>--=20


>Met vriendelijke groeten / Best regards,
>
>Ger Hobbelt
>
>--------------------------------------------------
>web: http://www.hobbelt.com/
> http://www.hebbut.net/
>mail: g...@hobbelt.com
>mobile: +31-6-11 120 978
>--------------------------------------------------
>______________________________________________________________________
>OpenSSL Project http://www.openssl.org
>User Support Mailing List openss...@openssl.org
>Automated List Manager majo...@openssl.org

------=_Part_74341_12357036.1243643778317


Content-Type: text/html; charset=gbk
Content-Transfer-Encoding: quoted-printable

<div><br><br></div><div></div><br><pre>=D4=DA2009-05-29=A3=AC"Ger Hobbelt" =
&lt;g...@hobbelt.com&gt; =D0=B4=B5=C0=A3=BA
&gt;2009/5/29 jazeltq &lt;jaz...@163.com&gt;:
&gt;&gt; =D4=DA2009-05-28=A3=AC"Victor Duchovni" &lt;Victor.Duchovni@morgan=
stanley.com&gt; =D0=B4=B5=C0=A3=BA
&gt;&gt;&gt;On Thu, May 28, 2009 at 11:51:42AM +0200, Ger Hobbelt wrote:
&gt;&gt;&gt;
&gt;&gt;&gt;Only if the data is text. Using strlen() on binary data is anot=
her
&gt;&gt;&gt;classic/basic 'C' programming mistake.
&gt;&gt; if it is binary data, what to do ? Use a Variable to send the the=
size ?
&gt;
&gt;yes.
&gt;
&gt;Just like the BIO_read, BIO_write, fwrite, etc. APIs all do too: pass
&gt;along a pointer /and/ a length parameter.
Thank you, i get it<br>&gt;
&gt;--=20
&gt;Met vriendelijke groeten / Best regards,
&gt;
&gt;Ger Hobbelt
&gt;
&gt;--------------------------------------------------
&gt;web: http://www.hobbelt.com/
&gt; http://www.hebbut.net/
&gt;mail: g...@hobbelt.com
&gt;mobile: +31-6-11 120 978
&gt;--------------------------------------------------


&gt;______________________________________________________________________
&gt;OpenSSL Project http://www.openssl.org
&gt;User Support Mailing List openss...@openssl.org
&gt;Automated List Manager majo...@openssl.org
</pre><br><!-- footer --><br><span title=3D"neteasefooter"/><hr/>
<a href=3D"http://512.mail.163.com/mailstamp/stamp/dz/activity.do?from=3Dfo=
oter">=B4=A9=D4=BD=B5=D8=D5=F0=B4=F8 =BC=CD=C4=EE=E3=EB=B4=A8=B5=D8=D5=F0=
=D2=BB=D6=DC=C4=EA</a>
</span>

------=_Part_74341_12357036.1243643778317--

0 new messages