Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
random_number.c File Reference

Implements OP_RANDOM_NUMBER to generate cryptographically secure random bytes. More...

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for random_number.c:

Go to the source code of this file.

Functions

 if (lv.type !=VAL_INT)
 if (len< 0)
 if (len==0)
 if (len > MAX_RAW)
 if (!raw||!hex)
 if (!ok)
 for (int64_t i=0;i< len;++i)
 free (raw)
 free (hex)
 free_value (lv)
 push_value (vm, s)

Variables

case OP_RANDOM_NUMBER
int64_t len = lv.i
const int64_t MAX_RAW = (1LL << 20)
unsigned char * raw = (unsigned char *)malloc((size_t)len)
char * hex = (char *)malloc((size_t)len * 2 + 1)
int ok = 0
Value s = make_string(hex)
 break

Detailed Description

Implements OP_RANDOM_NUMBER to generate cryptographically secure random bytes.

Behavior:

  • Pops len (int, number of raw bytes to generate) and pushes a lowercase hex string of length 2*len.
  • Uses the platform-preferred CSPRNG (e.g., getrandom/arc4random/BCryptGenRandom); falls back to /dev/urandom on UNIX.
  • Caps requested length to prevent excessive allocations.

Errors:

  • If len is not an int, or negative, prints an error and pushes an empty string.
  • If OS RNG is unavailable or allocation fails, prints an error and terminates the VM (exit(1)).

Definition in file random_number.c.

Function Documentation

◆ for()

for()

Definition at line 138 of file random_number.c.

◆ free() [1/2]

free(hex)

◆ free() [2/2]

free(raw)
Here is the caller graph for this function:

◆ free_value()

free_value(lv)
Here is the caller graph for this function:

◆ if() [1/6]

if(!ok)

Definition at line 128 of file random_number.c.

Here is the call graph for this function:

◆ if() [2/6]

if(!raw||!hex)

Definition at line 81 of file random_number.c.

Here is the call graph for this function:

◆ if() [3/6]

if(len,
MAX_RAW )

Definition at line 72 of file random_number.c.

Here is the call graph for this function:

◆ if() [4/6]

if()

Definition at line 58 of file random_number.c.

Here is the call graph for this function:

◆ if() [5/6]

if(len = = 0)

Definition at line 64 of file random_number.c.

Here is the call graph for this function:

◆ if() [6/6]

if(lv.type ! = VAL_INT)

Definition at line 50 of file random_number.c.

Here is the call graph for this function:

◆ push_value()

push_value(vm,
s )
Here is the caller graph for this function:

Variable Documentation

◆ break

break

Definition at line 150 of file random_number.c.

◆ hex

hex[len *2] = (char *)malloc((size_t)len * 2 + 1)

Definition at line 80 of file random_number.c.

◆ len

int64_t len = lv.i

Definition at line 57 of file random_number.c.

◆ MAX_RAW

const int64_t MAX_RAW = (1LL << 20)

Definition at line 71 of file random_number.c.

◆ ok

ok = 0

Definition at line 89 of file random_number.c.

◆ OP_RANDOM_NUMBER

case OP_RANDOM_NUMBER

Definition at line 47 of file random_number.c.

◆ raw

unsigned char* raw = (unsigned char *)malloc((size_t)len)

Definition at line 79 of file random_number.c.

◆ s

Definition at line 145 of file random_number.c.