Subject: RE: Greetings and a quick note about licening issuse in theMakefile.win32 in 1.2.4

RE: Greetings and a quick note about licening issuse in theMakefile.win32 in 1.2.4

From: C Johnson <libssh2_at_xepol.com>
Date: Mon, 15 Mar 2010 11:07:49 -0600

 

> [mailto:libssh2-devel-bounces_at_cool.haxx.se] On Behalf Of Peter Stuge
>
> I'm not sure. C Johnson, could you please explain a little
> bit more about those translated headers and demos? An example
> would be great!

If I understand correctly, libssh2 is meant to be used as a shared library
instead of linked directly into projects. This is definitely how it would
be accessed from Delphi.

However, because Delphi is Object Pascal it can not use any of the current
files to build the linkage to the shared library. Instead, the header files
have to be translated. Amoung other problems, Delphi (and pascal in
general) doesn't have a pre-processor so many of the #define wedges need to
be recoded as full functions/procedures.

Simple conversions can be:

#define LIBSSH2_VERSION_MAJOR 1
#define LIBSSH2_VERSION_MINOR 2
#define LIBSSH2_VERSION_PATCH 5

To

Const
  LIBSSH2_VERSION_MAJOR = 1;
  LIBSSH2_VERSION_MINOR = 2;
  LIBSSH2_VERSION_PATCH = 5;

And more complicated conversions such as

#define LIBSSH2_ALLOC_FUNC(name) void *name(size_t count, void **abstract)
#define LIBSSH2_REALLOC_FUNC(name) void *name(void *ptr, size_t count, \
                                              void **abstract)
#define LIBSSH2_FREE_FUNC(name) void name(void *ptr, void **abstract)

Which are function call types convert to

Type
  LIBSSH2_ALLOC_FUNC = Procedure(count : size_t; Var abstract : Pointer );
cdecl;
  LIBSSH2_REALLOC_FUNC = Procedure(ptr : Pointer; count : size_t; Var
abstract : Pointer); cdecl;
  LIBSSH2_FREE_FUNC = Procedure (ptr : Pointer; Var abstract : Pointer);
cdecl;

And actual library calls such as

libssh2_session_init_ex(LIBSSH2_ALLOC_FUNC((*my_alloc)),
                        LIBSSH2_FREE_FUNC((*my_free)),
                        LIBSSH2_REALLOC_FUNC((*my_realloc)), void
*abstract);

Convert to

  Function libssh2_session_init_ex(my_alloc : LIBSSH2_ALLOC_FUNC;
                                   my_free : LIBSSH2_FREE_FUNC;
                                   my_realloc : LIBSSH2_REALLOC_FUNC;
                                   abstract : Pointer) : PLIBSSH2_SESSION;
cdecl; external 'libssh2.dll' name 'libssh2_session_init_ex'

And helper macros like

#define libssh2_session_init() libssh2_session_init_ex(NULL, NULL, NULL,
NULL)

Become

Function libssh2_session_init : PLIBSSH2_SESSION;
Begin
  Result := libssh2_session_init_ex(nil, nil, nil, nil);
End;

There are a few additional issues that arise from translation. For example,
Pascal isn't a case sensitive language and source files called units are
considered a name space. This means that the LIBSSH2_PUBLICKEY.h
translation as Unit LIBSSH2_PUBLICKEY.pas inherently causes a name collision
conflict with LIBSSH2_PUBLICKEY (typedef'd from the _LIBSSH2_PUBLICKEY
struct). In Pascal, all structs (called records) are already types, and do
not need typedef aliasing, so the solution is either to rename the file (in
the pascal translation - I'm not a fan), or just dropping use of
LIBSSH2_PUBLICKEY as a type infavor of _LIBSSH2_PUBLICKEY (again, only in
Pascal code).

A few additional cases of case sensitivity conflict also happen with

As an alternate solution it would also be possible to fold all libssh2.h,
libssh2_publickey.h and libssh2_sftp.h into a single libssh2.pas import file
to avoid the name conflicts - in fact, this might be the simplest method in
the long run.

One translation issue I am still struggling with is that pascal does not
consider pointers and arrays to be interchangable. This definitely affects
how items like _libssh2_publickey_attribute gets treated.

My goal is to produce a translation of the headers that is as pure as
possible to minimize any actual code differences as possible - so that
someone trying to use libssh2 in Delphi can look at code written in C and
expect to write nearly idenitical code (language syntax changes not
withstanding) - and vice vera.

In translating the C Demo programs to Dephi, the goal would be as close to a
line for line translation as possible with the possible exception of more
stringent resource management for when errors do occur.

The concept of creating higher level objects that encapuslate and hide
libssh2 itself fall outside of what I was suggesting to provide.

I have an initial translation of the header files already converted and a
single demo app translated - they are by no means final, but definitely work
in all the areas I have tested so far. I would be willing to email them to
anyone interested in looking at them in their entirty - they compress in to
a 17K .7z file. Just drop me an email and I will happilly fire off a copy.

- C Johnson

_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2010-03-15