unit CELIKAPI;

interface
uses
{$IFDEF WIN32}
  Windows;
{$ELSE}
  Wintypes, WinProcs;
{$ENDIF}


{/// CelikApi.h }
{/// }
{ #pragma once }

{$IFDEF __cplusplus}
{$ENDIF}

{$IFNDEF WINAPI}
const
  WINAPI = __stdcall;
{$ENDIF}

{$IFNDEF EID_API}
{$DEFINE EID_API}
{$ENDIF}

{/// }
{/// Constants }
{/// }

{/// Size of all UTF-8 fields in bytes }
const
  EID_MAX_DocRegNo: Integer = 9;
  EID_MAX_IssuingDate: Integer = 10;
  EID_MAX_ExpiryDate: Integer = 10;
  EID_MAX_IssuingAuthority: Integer = 30;

  EID_MAX_PersonalNumber: Integer = 13;
  EID_MAX_Surname: Integer = 60;
  EID_MAX_GivenName: Integer = 40;
  EID_MAX_ParentGivenName: Integer = 25;
  EID_MAX_Sex: Integer = 2;
  EID_MAX_PlaceOfBirth: Integer = 25;
  EID_MAX_StateOfBirth: Integer = 25;
  EID_MAX_DateOfBirth: Integer = 10;
  EID_MAX_CommunityOfBirth: Integer = 25;

  EID_MAX_State: Integer = 3;
  EID_MAX_Community: Integer = 25;
  EID_MAX_Place: Integer = 25;
  EID_MAX_Street: Integer = 36;
  EID_MAX_HouseNumber: Integer = 5;
  EID_MAX_HouseLetter: Integer = 2;
  EID_MAX_Entrance: Integer = 3;
  EID_MAX_Floor: Integer = 3;
  EID_MAX_ApartmentNumber: Integer = 6;

  EID_MAX_Portrait: Integer = 7700;




{/// }
{/// Function return values }
{/// }
  EID_OK: Integer = 0;
  EID_E_GENERAL_ERROR: Integer = -1;
  EID_E_INVALID_PARAMETER: Integer = -2;
  EID_E_VERSION_NOT_SUPPORTED: Integer = -3;
  EID_E_NOT_INITIALIZED: Integer = -4;
  EID_E_UNABLE_TO_EXECUTE: Integer = -5;
  EID_E_READER_ERROR: Integer = -6;
  EID_E_CARD_MISSING: Integer = -7;
  EID_E_CARD_UNKNOWN: Integer = -8;
  EID_E_CARD_MISMATCH: Integer = -9;
  EID_E_UNABLE_TO_OPEN_SESSION: Integer = -10;
  EID_E_DATA_MISSING: Integer = -11;
  EID_E_CARD_SECFORMAT_CHECK_ERROR: Integer = -12;
  EID_E_SECFORMAT_CHECK_CERT_ERROR: Integer = -13;

{/// }
{/// Structures }
{/// }

{/// NOTE: char arrays DO NOT have zero char at the end }

type
  tagEID_DOCUMENT_DATA = record
    docRegNo: Array[0..EID_MAX_DocRegNo-1] of Char;
    docRegNoSize: Integer;
    issuingDate: Array[0..EID_MAX_IssuingDate-1] of Char;
    issuingDateSize: Integer;
    expiryDate: Array[0..EID_MAX_ExpiryDate-1] of Char;
    expiryDateSize: Integer;
    issuingAuthority: Array[0..EID_MAX_IssuingAuthority-1] of Char;
    issuingAuthoritySize: Integer;
  end {tagEID_DOCUMENT_DATA};

type
  tagEID_FIXED_PERSONAL_DATA = record
    personalNumber: Array[0..EID_MAX_PersonalNumber-1] of Integer;
    personalNumberSize: Integer;
    surname: Array[0..EID_MAX_Surname-1] of Char;
    surnameSize: Integer;
    givenName: Array[0..EID_MAX_GivenName-1] of Char;
    givenNameSize: Integer;
    parentGivenName: Array[0..EID_MAX_ParentGivenName-1] of Char;
    parentGivenNameSize: Integer;
    sex: Array[0..EID_MAX_Sex-1] of Char;
    sexSize: Integer;
    placeOfBirth: Array[0..EID_MAX_PlaceOfBirth-1] of Char;
    placeOfBirthSize: Integer;
    stateOfBirth: Array[0..EID_MAX_StateOfBirth-1] of Char;
    stateOfBirthSize: Integer;
    dateOfBirth: Array[0..EID_MAX_DateOfBirth-1] of Char;
    dateOfBirthSize: Integer;
    communityOfBirth: Array[0..EID_MAX_CommunityOfBirth-1] of Char;
    communityOfBirthSize: Integer;
  end {tagEID_FIXED_PERSONAL_DATA};

type
  tagEID_VARIABLE_PERSONAL_DATA = record
    state: Array[0..EID_MAX_State-1] of Char;
    stateSize: Integer;
    community: Array[0..EID_MAX_Community-1] of Char;
    communitySize: Integer;
    place: Array[0..EID_MAX_Place-1] of Char;
    placeSize: Integer;
    street: Array[0..EID_MAX_Street-1] of Char;
    streetSize: Integer;
    houseNumber: Array[0..EID_MAX_HouseNumber-1] of Char;
    houseNumberSize: Integer;
    houseLetter: Array[0..EID_MAX_HouseLetter-1] of Char;
    houseLetterSize: Integer;
    entrance: Array[0..EID_MAX_Entrance-1] of Char;
    entranceSize: Integer;
    floor: Array[0..EID_MAX_Floor-1] of Char;
    floorSize: Integer;
    apartmentNumber: Array[0..EID_MAX_ApartmentNumber-1] of Char;
    apartmentNumberSize: Integer;
  end {tagEID_VARIABLE_PERSONAL_DATA};

type
  tagEID_PORTRAIT = record
    portrait: Array[0..EID_MAX_Portrait-1] of BYTE;
    portraitSize: Integer;
  end {tagEID_PORTRAIT};


{/// }
{/// Functions }
{/// }
var
  EidStartup: function(nApiVersion: Integer): EID_API {$IFDEF WIN32} stdcall {$ENDIF}; 
var
  EidCleanup: function: EID_API {$IFDEF WIN32} stdcall {$ENDIF}; 

var
  EidBeginRead: function(var szReader: CSTR): EID_API {$IFDEF WIN32} stdcall {$ENDIF}; 
var
  EidEndRead: function: EID_API {$IFDEF WIN32} stdcall {$ENDIF}; 

var
  EidReadDocumentData: function(var pData: EID_DOCUMENT_DATA): EID_API {$IFDEF WIN32} stdcall {$ENDIF}; 
var
  EidReadFixedPersonalData: function(var pData: EID_FIXED_PERSONAL_DATA): EID_API {$IFDEF WIN32} stdcall {$ENDIF}; 
var
  EidReadVariablePersonalData: function(var pData: EID_VARIABLE_PERSONAL_DATA): EID_API {$IFDEF WIN32} stdcall {$ENDIF}; 
var
  EidReadPortrait: function(var pData: EID_PORTRAIT): EID_API {$IFDEF WIN32} stdcall {$ENDIF}; 


{$IFDEF __cplusplus}
{$ENDIF}

var
  DLLLoaded: Boolean { is DLL (dynamically) loaded already? }
    {$IFDEF WIN32} = False; {$ENDIF}

implementation

var
  SaveExit: pointer;
  DLLHandle: THandle;
{$IFNDEF MSDOS}
  ErrorMode: Integer;
{$ENDIF}

  procedure NewExit; far;
  begin
    ExitProc := SaveExit;
    FreeLibrary(DLLHandle)
  end {NewExit};

procedure LoadDLL;
begin
  if DLLLoaded then Exit;
{$IFNDEF MSDOS}
  ErrorMode := SetErrorMode($8000{SEM_NoOpenFileErrorBox});
{$ENDIF}
  DLLHandle := LoadLibrary('CELIKAPI.DLL');
  if DLLHandle >= 32 then
  begin
    DLLLoaded := True;
    SaveExit := ExitProc;
    ExitProc := @NewExit;
    @EidStartup := GetProcAddress(DLLHandle,'EidStartup');
  {$IFDEF WIN32}
    Assert(@EidStartup <> nil);
  {$ENDIF}
    @EidCleanup := GetProcAddress(DLLHandle,'EidCleanup');
  {$IFDEF WIN32}
    Assert(@EidCleanup <> nil);
  {$ENDIF}
    @EidBeginRead := GetProcAddress(DLLHandle,'EidBeginRead');
  {$IFDEF WIN32}
    Assert(@EidBeginRead <> nil);
  {$ENDIF}
    @EidEndRead := GetProcAddress(DLLHandle,'EidEndRead');
  {$IFDEF WIN32}
    Assert(@EidEndRead <> nil);
  {$ENDIF}
    @EidReadDocumentData := GetProcAddress(DLLHandle,'EidReadDocumentData');
  {$IFDEF WIN32}
    Assert(@EidReadDocumentData <> nil);
  {$ENDIF}
    @EidReadFixedPersonalData := GetProcAddress(DLLHandle,'EidReadFixedPersonalData');
  {$IFDEF WIN32}
    Assert(@EidReadFixedPersonalData <> nil);
  {$ENDIF}
    @EidReadVariablePersonalData := GetProcAddress(DLLHandle,'EidReadVariablePersonalData');
  {$IFDEF WIN32}
    Assert(@EidReadVariablePersonalData <> nil);
  {$ENDIF}
    @EidReadPortrait := GetProcAddress(DLLHandle,'EidReadPortrait');
  {$IFDEF WIN32}
    Assert(@EidReadPortrait <> nil);
  {$ENDIF}
  end
  else
  begin
    DLLLoaded := False;
    { Error: CELIKAPI.DLL could not be loaded !! }
  end;
{$IFNDEF MSDOS}
  SetErrorMode(ErrorMode)
{$ENDIF}
end {LoadDLL};

begin
  LoadDLL;
end.
