473,785 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memfrob and strfry ? OT

My string.h headers declares two functions I have been using called
memfrob and strfry. They are encryption types functions. My man pages say
they are standard to linux c and gnu c. They sure aren't in my C books.
Interesting functions by they're OT here but this raises to me a question.
If a function returns a pointer to a void and and as it's first parameter a
pointer to a void. Then should that function's first parameter accept a
string or pointer to char or any other data type since the parameter is
declared a void * ?

Bill
Jun 27 '08 #1
35 5687
In article <Kl2Zj.25383$sX 5.23589@trnddc0 2>,
Bill Cunningham <no****@nspam.c omwrote:
My string.h headers declares two functions I have been using called
memfrob and strfry.
Laugh? I almost did.

-- Richard
--
:wq
Jun 27 '08 #2

"Richard Tobin" <ri*****@cogsci .ed.ac.ukwrote in message
news:g1******** ***@pc-news.cogsci.ed. ac.uk...
Laugh? I almost did.

-- Richard
<gigglewell it is kinda funny. Strfry looks like stirfry and memfrob
like some kind of frog. But no kidding they exist.

Bill
Jun 27 '08 #3
"Bill Cunningham" <no****@nspam.c omwrites:
My string.h headers declares two functions I have been using called
memfrob and strfry. They are encryption types functions. My man pages say
they are standard to linux c and gnu c. They sure aren't in my C books.
Interesting functions by they're OT here but this raises to me a question.
If a function returns a pointer to a void and and as it's first parameter a
pointer to a void. Then should that function's first parameter accept a
string or pointer to char or any other data type since the parameter is
declared a void * ?
Since you're asking about what it accepts as an argument, the type
that it returns is irrelevant. And since strfry() takes an argument
of type char* and returns a char* result, it's also irrelevant. The
only thing you're asking about is the first parameter of memfrob().

For reference, the declaration is:

void *memfrob(void *s, size_t n);

(The function itself is off-topic; the fact that you can declare a
function like that is not. The use of a name starting with "mem"
raises some issues that I'll ignore.)

Any pointer type (other than a pointer-to-function type) can be
implicitly converted to void*. So the first argument in a call to
memfrob() can be an expression of any pointer type other than a
pointer-to-function type. (This includes array expressions, such as
string literals, which are converted to pointer values.) It can also
be a null pointer constant, such as the macro NULL or a literal 0
(though you wouldn't want to pass a null pointer to memfrob()).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #4

"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...

[snip]
Any pointer type (other than a pointer-to-function type) can be
implicitly converted to void*. So the first argument in a call to
memfrob() can be an expression of any pointer type other than a
pointer-to-function type. (This includes array expressions, such as
string literals, which are converted to pointer values.) It can also
be a null pointer constant, such as the macro NULL or a literal 0
(though you wouldn't want to pass a null pointer to memfrob()).
Thank you Keith. So if I wanted a function to return any number of types
as well as a void should a function return a void? That's my question.

Bill
Jun 27 '08 #5
"Bill Cunningham" <no****@nspam.c omwrites:
"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...

[snip]
>Any pointer type (other than a pointer-to-function type) can be
implicitly converted to void*. So the first argument in a call to
memfrob() can be an expression of any pointer type other than a
pointer-to-function type. (This includes array expressions, such as
string literals, which are converted to pointer values.) It can also
be a null pointer constant, such as the macro NULL or a literal 0
(though you wouldn't want to pass a null pointer to memfrob()).
Thank you Keith. So if I wanted a function to return any number
of types as well as a void should a function return a void? That's
my question.
No, that's an entirely new question, to which the answer is no.

A function declared to return void doesn't return anything.

A function declared to return void* (a very different thing from void)
returns what you can think of as a generic pointer; it can point to
any object. But to do that, there has to be an object for it to point
to, (which means you have think about allocating and deallocating it),
and you have to know somehow what type it points to if you want to do
anything with it.

What problem are you trying to solve? It's very likely that "a
function to return any number of types" isn't the right solution.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #6

"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...

[snip]
What problem are you trying to solve? It's very likely that "a
function to return any number of types" isn't the right solution.
I've been working with this OT function memfrb. It's a type I've never
encountered. I wouldn't know how in C to take a block of memory I tried 64
bytes the partition table I saved from a mbr. I thought I'd use this to
encypt some bytes. Closest I got was some garbage and a segmention fault.

void *pv;
pv=memfrb("file ",64);
printf("%s\n",p v);

Didn't work.

printf("%s\n",( memfrob("b",64) );

didn't work. How would you use this type of function? Not so much memfrob
but a function that returned a void* and took a void*s as a first parameter.
I've never worked with voids is the simple statement. I wouldn't know what
to do with this one.

Bill
Jun 27 '08 #7
"Bill Cunningham" <no****@nspam.c omwrites:
"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...

[snip]
>What problem are you trying to solve? It's very likely that "a
function to return any number of types" isn't the right solution.
I've been working with this OT function memfrb. It's a type I've never
encountered. I wouldn't know how in C to take a block of memory I tried 64
bytes the partition table I saved from a mbr. I thought I'd use this to
encypt some bytes. Closest I got was some garbage and a segmention fault.

void *pv;
pv=memfrb("file ",64);
printf("%s\n",p v);
It's "memfrob", not "memfrb". This typo indicates that you didn't
copy-and-paste your actual code, so I can't reliably infer anything.

Again, for reference, the declaration is:

void *memfrob(void *s, size_t n);

It's a very very poor encryption routine (it merely xors each byte
with the number 42). It can defeat a rudimentary attempt to search
for readable strings, nothing more.
Didn't work.
Well of course it didn't work. Your call

memfrob("file", 64);

attempts to modify the first 64 bytes of the string literal "file".
This immediately invokes undefined behavior by (a) attempting to
modify a string literal, and (b) attempting to modify 64 bytes of a
5-byte object.

If you want to "encrypt" data from a file, you first need to open the
file, and then read the data from the file. Passing the string "file"
to memfrob(), or to any other non-I/O function, will not magically
access a file named "file".
printf("%s\n",( memfrob("b",64) );
And what is "b" supposed to be?
didn't work. How would you use this type of function? Not so much memfrob
but a function that returned a void* and took a void*s as a first parameter.
I've never worked with voids is the simple statement. I wouldn't know what
to do with this one.
Ok, you want to learn about void*.

My advice: Forget about memfrob(). It's non-standard, and it doesn't
do anything particularly useful. Take a look at the various standard
mem*() functions: memcpy, memmove, memcmp, memchr, memset. To start
learning about them, read a book.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #8
On 22 May, 00:36, "Bill Cunningham" <nos...@nspam.c omwrote:
My string.h headers declares two functions I have been using called
memfrob and strfry. They are encryption types functions. My man pages say
they are standard to linux c and gnu c. They sure aren't in my C books.
Interesting functions by they're OT here but this raises to me a question.
If a function returns a pointer to a void and and as it's first parameter a
pointer to a void. Then should that function's first parameter accept a
string or pointer to char or any other data type since the parameter is
declared a void * ?
What you should pass as a first parameter depends on what the function
does. For example realloc() accepts as a first parameter void * but
you
cannot pass anything you want to it , it has to be either NULL or a
pointer previously returned by malloc() or realloc().

On 22 May, 03:06, "Bill Cunningham" <nos...@nspam.c omwrote:
How would you use this type of function? Not so much memfrob
but a function that returned a void* and took a void*s as a first parameter.
I've never worked with voids is the simple statement. I wouldn't know what
to do with this one.
Find some code which uses realloc() and study it.

Jun 27 '08 #9
On 22 May, 01:03, Keith Thompson <ks...@mib.orgw rote:
"Bill Cunningham" <nos...@nspam.c omwrites:
My string.h headers declares two functions I have been using called
memfrob and strfry. They are encryption types functions. My man pages say
they are standard to linux c and gnu c. They sure aren't in my C books.
Interesting functions by they're OT here but this raises to me a question.
If a function returns a pointer to a void and and as it's first parameter a
pointer to a void. Then should that function's first parameter accept a
string or pointer to char or any other data type since the parameter is
declared a void * ?

<SNIP>

Any pointer type (other than a pointer-to-function type) can be
implicitly converted to void*.
Of course on Linux a void * can hold even pointers to functions.

Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.