• C

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);
}
ovehAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
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.
0
ovehAuthor Commented:
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....

0
ozoCommented:
bitstring OWF( unsigned char * string )
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

ovehAuthor Commented:
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 )
0
ovehAuthor Commented:
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 )
0
ozoCommented:
bitstring OWF (unsigned char *string)
                { int i,j;
                 MD_CTX context;
                 bitstring contents;    
                 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  (contents.digest, &context);

                // MDPrint makes the digest into hexadecimals    
                  test = MDPrint (contents.digest);
                  printf("MD%d - 1 = %s\n", MD,test);
                   
                 return contents;
}

teststreng = OWF (string).digest;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.