From 1f7445ad37835188ddf7579dabff3127193bdf9d Mon Sep 17 00:00:00 2001 From: BONNAURE Olivier Date: Tue, 9 Apr 2024 17:25:35 +0200 Subject: [PATCH] Update documentation --- tool/net/help.txt | 3 +++ tool/net/lfuncs.c | 60 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/tool/net/help.txt b/tool/net/help.txt index 8095149cd..ab6af435d 100644 --- a/tool/net/help.txt +++ b/tool/net/help.txt @@ -1062,6 +1062,9 @@ FUNCTIONS Server Name Indicator (SNI) when performing Fetch() requests. This function is not available in unsecure mode. + UuidV4() -> str + Returns an uuid v4 string. + Fetch(url:str[,body:str|{method=value:str,body=value:str,headers=table,...}]) ├─→ status:int, {header:str=value:str,...}, body:str └─→ nil, error:str diff --git a/tool/net/lfuncs.c b/tool/net/lfuncs.c index ebcb1b1a3..8c6793e80 100644 --- a/tool/net/lfuncs.c +++ b/tool/net/lfuncs.c @@ -830,27 +830,49 @@ int LuaVisualizeControlCodes(lua_State *L) { return LuaCoder(L, VisualizeControlCodes); } + int LuaUuidV4(lua_State *L) { - char *template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"; - char *uuid = (char *)malloc(strlen(template) + 1); - if (uuid == NULL) { - return 0; // Memory allocation failed - } + char v[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + static char uuid_str[37] = {0}; - srand(time(NULL)); - - for (int i = 0; i < strlen(template); i++) { - char c = template[i]; - if (c == 'x') { - uuid[i] = (rand() % 16 < 10) ? (rand() % 10 + '0') : (rand() % 6 + 'a'); - } else if (c == 'y') { - uuid[i] = (rand() % 4 + '8'); - } else { - uuid[i] = c; - } - } - uuid[strlen(template)] = '\0'; - lua_pushfstring(L, uuid); + uuid_str[0] = v[rand()%16]; + uuid_str[1] = v[rand()%16]; + uuid_str[2] = v[rand()%16]; + uuid_str[3] = v[rand()%16]; + uuid_str[4] = v[rand()%16]; + uuid_str[5] = v[rand()%16]; + uuid_str[6] = v[rand()%16]; + uuid_str[7] = v[rand()%16]; + uuid_str[8] = '-'; + uuid_str[9] = v[rand()%16]; + uuid_str[10] = v[rand()%16]; + uuid_str[11] = v[rand()%16]; + uuid_str[12] = v[rand()%16]; + uuid_str[13] = '-'; + uuid_str[14] = '4'; + uuid_str[15] = v[rand()%16]; + uuid_str[16] = v[rand()%16]; + uuid_str[17] = v[rand()%16]; + uuid_str[18] = '-'; + uuid_str[19] = v[8 + rand()%4]; + uuid_str[20] = v[rand()%16]; + uuid_str[21] = v[rand()%16]; + uuid_str[22] = v[rand()%16]; + uuid_str[23] = '-'; + uuid_str[24] = v[rand()%16]; + uuid_str[25] = v[rand()%16]; + uuid_str[26] = v[rand()%16]; + uuid_str[27] = v[rand()%16]; + uuid_str[28] = v[rand()%16]; + uuid_str[29] = v[rand()%16]; + uuid_str[30] = v[rand()%16]; + uuid_str[31] = v[rand()%16]; + uuid_str[32] = v[rand()%16]; + uuid_str[33] = v[rand()%16]; + uuid_str[34] = v[rand()%16]; + uuid_str[35] = v[rand()%16]; + uuid_str[36] = '\0'; + lua_pushfstring(L, uuid_str); return 1; }