The implementation part is primarily intended for the implementation of the functions and procedures declared in the interface part. However, it can also contain declarations of it’s own: the declarations inside the implementation part are not accessible outside the unit.
unit_ciphers.pas(10,1) Fatal: Syntax error, "IMPLEMENTATION" expected but "USES" found
unit unit_ciphers;
{$mode objfpc}
interface
function Code(key:string; uncrypted_string:string; level:integer):string; {$ifndef linux} stdcall; {$else} cdecl; {$endif}
function Decode(key:string; crypted_string:string; level:integer):string; {$ifndef linux} stdcall; {$else} cdecl; {$endif}
uses
Classes, SysUtils, DCPblowfish, DCPrijndael, DCPserpent, DCPidea, DCPmars,
DCPtwofish, DCPcast128, DCPcast256, DCPdes, DCPice, DCPtea, DCPsha1,
DCPsha256, DCPmd5;
implementation
//function Code(key:string; uncrypted_string:string; level:integer):string; {$ifndef linux} stdcall; {$else} cdecl; {$endif}
//function Decode(key:string; crypted_string:string; level:integer):string; {$ifndef linux} stdcall; {$else} cdecl; {$endif}
function Encode(key:string; uncrypted_string:string; level:integer):string; {$ifndef linux} stdcall; {$else} cdecl; {$endif} //Encode(key, text, level)
var
motor: TDCP_blowfish;
i: integer;
t: string;
(...)
unit unit_ciphers;
{$mode objfpc}
uses
Classes, SysUtils, DCPblowfish, DCPrijndael, DCPserpent, DCPidea, DCPmars,
DCPtwofish, DCPcast128, DCPcast256, DCPdes, DCPice, DCPtea, DCPsha1,
DCPsha256, DCPmd5;
interface
function Code(key:string; uncrypted_string:string; level:integer):string; {$ifndef linux} stdcall; {$else} cdecl; {$endif}
function Decode(key:string; crypted_string:string; level:integer):string; {$ifndef linux} stdcall; {$else} cdecl; {$endif}
(...)
Put the uses clause below interface.
Bart
unit
interface
uses
implementation
uses
and have a look at the freepascal unit syntax about what goes where and why
unit_ciphers.pas(7,124) Error: Procedure directive "FORWARD" not allowed in interface section
Here is a reworked version of your unit from the initial post which compiles fine:
unit unit_ciphers; {$mode objfpc}{$H+} interface uses // In fact, your code does not use these units at all. Classes, SysUtils; // Therefore these lines could be skipped. function Code(key:string; uncrypted_string:string; level:integer):string; {$ifndef linux} stdcall; {$else} cdecl; {$endif} function Decode(key:string; crypted_string:string; level:integer):string; {$ifndef linux} stdcall; {$else} cdecl; {$endif} implementation uses DCPblowfish, DCPtwofish, DCPsha256; function Code(key:string; uncrypted_string:string; level:integer):string; {$ifndef linux} stdcall; {$else} cdecl; {$endif} //Encode(key, text, level) var motor: TDCP_blowfish; i: integer; t: string; begin if level=0 then level:=1 else if level>2 then level:=2; motor:=TDCP_blowfish.Create(nil); t:=uncrypted_string; for i:=1 to level do begin motor.InitStr(key,TDCP_sha256); t:=motor.EncryptString(t); motor.Burn; end; result:=t; motor.Free; end; function Decode(key:string; crypted_string:string; level:integer):string; {$ifndef linux} stdcall; {$else} cdecl; {$endif} //Decode(key, text, level) var motor: TDCP_blowfish; i: integer; t: string; begin if level=0 then level:=1 else if level>2 then level:=2; motor:=TDCP_blowfish.Create(nil); t:=crypted_string; for i:=1 to level do begin motor.InitStr(key,TDCP_sha256); t:=motor.DecryptString(t); motor.Burn; end; result:=t; motor.Free; end; end.
Some rules:The interface declarations of your code do need need the DCP* units, therefore, I moved them to the "uses" clause of the implementation part. This step is not absolutely necessary, but in large project you may run into circular unit references (unit A uses unit B, and unit B uses unit A) if you don't do this.
- The first (non-comment) line must be "unit <unitname>".
- The second (non-comment) line must be "interface".
- Then: "uses <comma-separated list of units needed by the declarations in the interface part>". In your code, in fact, the units Classes and SysUtils are not needed at all. So, you can skip this line here altogether. But normally this does not happen all the time, and therefore I left them just to show you a valid unit structure.
- Publicly visible declarations: type, const, procedure and function headers, NO code
- Line "Implementation"
- Then: "uses <comma-separated list of units needed by the declarations in the implementation part>". A comment here again: Your unit actually only uses DCPblowfish, DCPtwofish and DCPsha256; so, there is no need to add the other DCP units. This may add unnecessary code to your program (initialization).
- Then additional declarations private to the unit, auxiliary code
- code for the units listed under "Interface".
- Last line "end." (with a period!)
Regarding the {$MODE ObjFPC} directive I also added {$H+} which avoids using short string in this unit (you probably want to encrypt strings longer than 255 characters!).