; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: OS dependent part of different random number generators.
;
;  Target OS: Linux
;
;  Dependencies: memory.asm
;
;  Notes:
;
;_________________________________________________________________________________________




body GetRandomBytes
begin
        pushad

        stdcall GetMem, [.count]
        mov     esi, eax
        mov     edi, eax


        mov     eax, sys_open
        mov     ebx, cRandomFile
        mov     ecx, O_RDONLY
        int     $80
        test    eax, eax
        js      .error

        mov     ebx, eax

.loop:
        mov     eax, sys_read
        mov     ecx, esi
        mov     edx, [.count]
        int     $80
        test    eax, eax
        js      .error2

        add     esi, eax
        sub     [.count], eax
        jnz     .loop

        clc
        mov     [esp+4*regEAX], edi
        popad
        return


.error2:
        mov     eax, sys_close
        int     $80

.error:
        stdcall FreeMem, edi
        stc
        popad
        return
endp


cRandomFile  text "/dev/urandom"        ; we will use /dev/urandom, because don't want to block.