suuid.inc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. {
  2. $Id: sysutils.pp,v 1.59 2005/03/25 22:53:39 jonas Exp $
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Florian Klaempfl
  5. member of the Free Pascal development team
  6. Sysutils unit for linux
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. Const
  14. KernelUUID = '/proc/sys/kernel/random/uuid';
  15. Procedure GetURandomBytes(Var Buf; NBytes : Integer);
  16. Var
  17. fd,I : Integer;
  18. P : PByte;
  19. begin
  20. P:=@Buf;
  21. fd:=FileOpen('/dev/urandom',fmOpenRead or fmShareDenyNone);
  22. if (fd>=0) then
  23. Try
  24. While (NBytes>0) do
  25. begin
  26. I:=FileRead(fd,P^,nbytes);
  27. If I>0 then
  28. begin
  29. Inc(P,I);
  30. Dec(NBytes,I);
  31. end;
  32. end;
  33. Finally
  34. FileClose(Fd);
  35. end
  36. else
  37. GetRandomBytes(Buf,NBytes);
  38. end;
  39. Function CreateKernelGUID(Var GUID : TGUID) : Boolean;
  40. Const
  41. UUIDLen = 36;
  42. Var
  43. fd: Longint;
  44. S : String;
  45. begin
  46. fd:=FileOpen(KernelUUID,fmOpenRead or fmShareDenyNone);
  47. Result:=(Fd>=0);
  48. if Result then
  49. try
  50. SetLength(S,UUIDLen);
  51. SetLength(S,FileRead(fd,S[1],UUIDLen));
  52. Result:=(Length(S)=UUIDLen);
  53. If Result then
  54. GUID:=StringToGUID('{'+S+'}');
  55. finally
  56. FileClose(FD);
  57. end;
  58. end;
  59. Function SysCreateGUID(out GUID : TGUID) : Integer;
  60. begin
  61. if not CreateKernelGUID(Guid) then
  62. GetURandomBytes(GUID,SizeOf(Guid));
  63. Result:=0;
  64. end;