64 bit types and C libraries

Regan Heath regan at netmail.co.nz
Tue May 22 08:11:12 PDT 2007


Daniel Keep Wrote:
> Regan Heath wrote:
> > Hi all,
> > 
> > I am throwing my hand at writing D ports of the OpenSSL libraries.  It looks like a mammoth task but I figure I'll just chip away at it a piece at a time.  
> > 
> > This seems like the right time to ask if anyone else is having a go at it, or wants to give me a hand.  I've actually just got a job (yay me) but wont start for a bit (11th June) so I have some spare time.  Once I start work I wont have as much time so it would be good to find someone else who can help.
> > 
> > In the meantime I have reached a point where a type PQ_64BIT is used.  Looking at the headers it appears this type is defined differently for each system/compiler based on whether they have a 64 bit type, so on windows using M$ compilers etc it is __int64 for example.  In the rest of cases it is a custom BIGNUM struct.
> > 
> > My question is this.. what the heck do I put in my D port when I see this type used?
> > 
> > In D, long is a 64 bit type, but... I'm not writing new code I am writing a wrapper so I need to put the actual type the C library is using (was compiled using), otherwise any struct using PQ_64BIT might be the wrong size and disastrous things might happen.
> > 
> > Am I understanding this correctly?
> > 
> > It seems that I can probably get away with swapping PQ_64BIT for long in the short term, as I suspect my library was compiled with a 64bit type, not the BIGNUM struct...
> > 
> > Thoughts?
> 
> Having a very quick poke around the OpenSSL docs, it looks like PQ_64BIT
> is designed to be used as an opaque type.  The problem, of course, is
> that you don't know how the library was compiled.
> 
> It looks like the only safe way to do it is to mimic the C headers.
> Something like this:
> 
> version( SIXTY_FOUR_BIT )
> {
>     typedef ulong PQ_64BIT; // Note that I think OpenSSL uses ulongs!
>     void pq_64bit_init(ref PQ_64BIT x) {}
>     void pq_64bit_add_word(ref PQ_64BIT x, ref PQ_64BIT w) {x += w}
> }
> else
> {
>     typedef BIGNUM PQ_64BIT;
>     void pq_64bit_init(ref PQ_64BIT x) { BN_INIT(&x); }
>     void pq_64bit_add_word(ref PQ_64BIT x, ref PQ_64BIT w) {
> BN_add_word(&x, &w); }
> }
> 
> And so on.  Ugly as hell, but that's how OpenSSL is doing things.
> 
> Hope that's of some help :P

I think you're right.  Ick.

Regan


More information about the Digitalmars-d-learn mailing list