Format source code.

This commit is contained in:
Jordan Bancino 2023-02-17 03:23:25 +00:00
parent ff879e715f
commit fa88fc3323
9 changed files with 41 additions and 37 deletions

View file

@ -1082,7 +1082,7 @@ JsonDecode(FILE * stream)
} }
JsonValue * JsonValue *
JsonGet(HashMap *json, size_t nArgs, ...) JsonGet(HashMap * json, size_t nArgs,...)
{ {
va_list argp; va_list argp;
@ -1090,7 +1090,7 @@ JsonGet(HashMap *json, size_t nArgs, ...)
JsonValue *val = NULL; JsonValue *val = NULL;
size_t i; size_t i;
if (!json || ! nArgs) if (!json || !nArgs)
{ {
return NULL; return NULL;
} }
@ -1123,7 +1123,7 @@ finish:
} }
JsonValue * JsonValue *
JsonSet(HashMap *json, JsonValue *newVal, size_t nArgs, ...) JsonSet(HashMap * json, JsonValue * newVal, size_t nArgs,...)
{ {
HashMap *tmp = json; HashMap *tmp = json;
JsonValue *val = NULL; JsonValue *val = NULL;
@ -1165,7 +1165,7 @@ finish:
} }
int int
JsonCreate(HashMap *json, JsonValue *newVal, size_t nArgs, ...) JsonCreate(HashMap * json, JsonValue * newVal, size_t nArgs,...)
{ {
HashMap *tmp = json; HashMap *tmp = json;
JsonValue *val = NULL; JsonValue *val = NULL;

View file

@ -29,24 +29,25 @@
#include <pthread.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
/* Generate random numbers using rejection sampling. /* Generate random numbers using rejection sampling. The basic idea is
* The basic idea is to "reroll" if a number happens to be * to "reroll" if a number happens to be outside the range. However
* outside the range. However this could be extremely inefficient. * this could be extremely inefficient.
* *
* Another idea would just be to "reroll" if the generated number * Another idea would just be to "reroll" if the generated number ends up
* ends up in the previously "biased" range, and THEN do a modulo. * in the previously "biased" range, and THEN do a modulo.
* *
* This would be far more efficient for small values of max, * This would be far more efficient for small values of max, and fixes the
* and fixes the bias issue. */ * bias issue. */
/* This algorithm therefore computes N random numbers generally in /* This algorithm therefore computes N random numbers generally in O(N)
* O(N) time, while being less biased. */ * time, while being less biased. */
void void
RandIntN(int * buf, size_t size, unsigned int max) RandIntN(int *buf, size_t size, unsigned int max)
{ {
static pthread_mutex_t seedLock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t seedLock = PTHREAD_MUTEX_INITIALIZER;
static unsigned int seed = 0; static unsigned int seed = 0;
int tmp; int tmp;
/* Limit the range to banish all previously biased results */ /* Limit the range to banish all previously biased results */
const int allowed = RAND_MAX - RAND_MAX % max; const int allowed = RAND_MAX - RAND_MAX % max;
@ -58,7 +59,7 @@ RandIntN(int * buf, size_t size, unsigned int max)
/* Generate a seed from the system time, PID, and TID */ /* Generate a seed from the system time, PID, and TID */
seed = UtilServerTs() ^ getpid() ^ (unsigned long) pthread_self(); seed = UtilServerTs() ^ getpid() ^ (unsigned long) pthread_self();
} }
/* Generate {size} random numbers. */ /* Generate {size} random numbers. */
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
{ {
@ -73,11 +74,13 @@ RandIntN(int * buf, size_t size, unsigned int max)
} }
pthread_mutex_unlock(&seedLock); pthread_mutex_unlock(&seedLock);
} }
/* Generate just 1 random number */ /* Generate just 1 random number */
int int
RandInt(unsigned int max) RandInt(unsigned int max)
{ {
int val = 0; int val = 0;
RandIntN(&val, 1, max); RandIntN(&val, 1, max);
return val; return val;
} }

View file

@ -249,14 +249,14 @@ ROUTE_IMPL(RouteLogin, args)
response = HashMapCreate(); response = HashMapCreate();
HashMapSet(response, "access_token", HashMapSet(response, "access_token",
JsonValueString(loginInfo->accessToken->string)); JsonValueString(loginInfo->accessToken->string));
HashMapSet(response, "device_id", HashMapSet(response, "device_id",
JsonValueString(loginInfo->accessToken->deviceId)); JsonValueString(loginInfo->accessToken->deviceId));
if (refreshToken) if (refreshToken)
{ {
HashMapSet(response, "expires_in_ms", HashMapSet(response, "expires_in_ms",
JsonValueInteger(loginInfo->accessToken->lifetime)); JsonValueInteger(loginInfo->accessToken->lifetime));
HashMapSet(response, "refresh_token", HashMapSet(response, "refresh_token",
JsonValueString(loginInfo->refreshToken)); JsonValueString(loginInfo->refreshToken));
} }

View file

@ -147,7 +147,8 @@ ROUTE_IMPL(RouteRefresh, args)
/* Update the refresh token to point to the new access token */ /* Update the refresh token to point to the new access token */
JsonValueFree(HashMapSet(DbJson(rtRef), "refreshes", JsonValueString(StrDuplicate(newAccessToken->string)))); JsonValueFree(HashMapSet(DbJson(rtRef), "refreshes", JsonValueString(StrDuplicate(newAccessToken->string))));
/* Return the new access token and expiration timestamp to the client */ /* Return the new access token and expiration timestamp to the
* client */
response = HashMapCreate(); response = HashMapCreate();
HashMapSet(response, "access_token", JsonValueString(StrDuplicate(newAccessToken->string))); HashMapSet(response, "access_token", JsonValueString(StrDuplicate(newAccessToken->string)));
HashMapSet(response, "expires_in_ms", JsonValueInteger(newAccessToken->lifetime)); HashMapSet(response, "expires_in_ms", JsonValueInteger(newAccessToken->lifetime));

View file

@ -209,14 +209,14 @@ ROUTE_IMPL(RouteRegister, args)
initialDeviceDisplayName, refreshToken); initialDeviceDisplayName, refreshToken);
HashMapSet(response, "access_token", HashMapSet(response, "access_token",
JsonValueString(loginInfo->accessToken->string)); JsonValueString(loginInfo->accessToken->string));
HashMapSet(response, "device_id", HashMapSet(response, "device_id",
JsonValueString(loginInfo->accessToken->deviceId)); JsonValueString(loginInfo->accessToken->deviceId));
if (refreshToken) if (refreshToken)
{ {
HashMapSet(response, "expires_in_ms", HashMapSet(response, "expires_in_ms",
JsonValueInteger(loginInfo->accessToken->lifetime)); JsonValueInteger(loginInfo->accessToken->lifetime));
HashMapSet(response, "refresh_token", HashMapSet(response, "refresh_token",
JsonValueString(loginInfo->refreshToken)); JsonValueString(loginInfo->refreshToken));
} }

View file

@ -156,7 +156,7 @@ StrRandom(size_t len)
static const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; static const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *str; char *str;
int * nums; int *nums;
size_t i; size_t i;
if (!len) if (!len)

View file

@ -278,7 +278,7 @@ UserLogin(User * user, char *password, char *deviceId, char *deviceDisplayName,
rtRef = DbCreate(user->db, 3, "tokens", "refresh", result->refreshToken); rtRef = DbCreate(user->db, 3, "tokens", "refresh", result->refreshToken);
HashMapSet(DbJson(rtRef), "refreshes", HashMapSet(DbJson(rtRef), "refreshes",
JsonValueString(StrDuplicate(result->accessToken->string))); JsonValueString(StrDuplicate(result->accessToken->string)));
DbUnlock(user->db, rtRef); DbUnlock(user->db, rtRef);
} }
@ -331,7 +331,7 @@ UserLogin(User * user, char *password, char *deviceId, char *deviceDisplayName,
} }
HashMapSet(device, "accessToken", HashMapSet(device, "accessToken",
JsonValueString(StrDuplicate(result->accessToken->string))); JsonValueString(StrDuplicate(result->accessToken->string)));
return result; return result;
} }
@ -382,7 +382,7 @@ UserCheckPassword(User * user, char *password)
} }
int int
UserSetPassword(User *user, char *password) UserSetPassword(User * user, char *password)
{ {
HashMap *json; HashMap *json;
@ -409,7 +409,7 @@ UserSetPassword(User *user, char *password)
} }
int int
UserDeactivate(User *user) UserDeactivate(User * user)
{ {
HashMap *json; HashMap *json;
@ -426,7 +426,7 @@ UserDeactivate(User *user)
} }
HashMap * HashMap *
UserGetDevices(User *user) UserGetDevices(User * user)
{ {
HashMap *json; HashMap *json;
@ -441,7 +441,7 @@ UserGetDevices(User *user)
} }
UserAccessToken * UserAccessToken *
UserGenerateAccessToken(User *user, char *deviceId, int withRefresh) UserGenerateAccessToken(User * user, char *deviceId, int withRefresh)
{ {
UserAccessToken *token; UserAccessToken *token;
@ -463,7 +463,7 @@ UserGenerateAccessToken(User *user, char *deviceId, int withRefresh)
if (withRefresh) if (withRefresh)
{ {
token->lifetime = 1000 * 60 * 60 * 24 * 7; /* 1 Week */ token->lifetime = 1000 * 60 * 60 * 24 * 7; /* 1 Week */
} }
else else
{ {
@ -474,7 +474,7 @@ UserGenerateAccessToken(User *user, char *deviceId, int withRefresh)
} }
int int
UserAccessTokenSave(Db *db, UserAccessToken *token) UserAccessTokenSave(Db * db, UserAccessToken * token)
{ {
DbRef *ref; DbRef *ref;
HashMap *json; HashMap *json;

View file

@ -105,12 +105,12 @@ extern HashMap *
JsonDecode(FILE *); JsonDecode(FILE *);
extern JsonValue * extern JsonValue *
JsonGet(HashMap *, size_t, ...); JsonGet(HashMap *, size_t,...);
extern JsonValue * extern JsonValue *
JsonSet(HashMap *, JsonValue *, size_t, ...); JsonSet(HashMap *, JsonValue *, size_t,...);
extern int extern int
JsonCreate(HashMap *, JsonValue *, size_t, ...); JsonCreate(HashMap *, JsonValue *, size_t,...);
#endif /* TELODENDRIA_JSON_H */ #endif /* TELODENDRIA_JSON_H */

View file

@ -85,6 +85,6 @@ extern UserAccessToken *
UserGenerateAccessToken(User *, char *, int); UserGenerateAccessToken(User *, char *, int);
extern int extern int
UserAccessTokenSave(Db *, UserAccessToken *); UserAccessTokenSave(Db *, UserAccessToken *);
#endif /* TELODENDRIA_USER_H */ #endif /* TELODENDRIA_USER_H */