Link to home
Start Free TrialLog in
Avatar of oveh
oveh

asked on

Returning an array from a function ?

I'm playing with a hashing algorithm called MD-4,
and need to return the contents of an
'unsigned char digest[16]'
from my function, rather than the address to it.

How should i set it up ?

I'm using the global.h and the md.h files which can be found in this article:
http://theory.lcs.mit.edu/~rivest/Rivest-MD4.txt 

The test code would look something like this:
#include....various
// define MD-4 specific things
#define MD  4      
#define MD_CTX MD4_CTX
#define MDInit MD4Init
#define MDUpdate MD4Update
#define MDFinal MD4Final

int main (int argc, char *argv[])      
{
      char *teststreng;
      unsigned char *string = "abc";
      char *test;

      teststreng = OWF (string);
      test = MDPrint (teststreng);
       printf("The answer is: %s \n",test);
      return (0);
}

unsigned char * OWF (unsigned char *string)
{ int i,j;
 MD_CTX context;
 unsigned char digest[ 16 ];  
 char *test ;      
 unsigned int len = strlen (string);

// The MD-4 calls...puts the 4 blocks a 32 bits into
// digest (128 bits, 16 bytes)
 MDInit   (&context) ;
 MDUpdate (&context, string, len);
 MDFinal  (digest, &context);

// MDPrint makes the digest into hexadecimals    
  test = MDPrint (digest);      
  printf("MD%d - 1 = %s\n", MD,test);
 
 return digest;
}
 
// turns a unsigned char into a hexadecimal string
char *MDPrint (unsigned char *digest)
{ int i;
  static char buf[64];
   for (i = 0; i < 16; i++) sprintf(&(buf[i*2]),"%02x", digest[i]);
   return(buf);
}
Avatar of ozo
ozo
Flag of United States of America image

You can return a struct containing unsigned char digest[16]
Or you could pass to the function the address of an array in which to return the contents.
Avatar of oveh
oveh

ASKER

So how would the code be like ?

something like :
typedef struct bit_struct{ unsigned char digest[16]; } bitstring;

bitstring * OWF ( unsigned char *)

and then what ??
I'm rather fresh at structs....

bitstring OWF( unsigned char * string )
Avatar of oveh

ASKER

how should I treat digest in the OWF ?

it's just a sample code...I'm primarily interested in knowing all the details needed for returning the
contents of an array from a function.

so...how does the introduction of struct into the code affect the code. What is added ? and where?

(thanks for your time by the way )
Avatar of oveh

ASKER

how should I treat digest in the OWF ?

it's just a sample code...I'm primarily interested in knowing all the details needed for returning the
contents of an array from a function.

so...how does the introduction of struct into the code affect the code. What is added ? and where?

(thanks for your time by the way )
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial