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

View file

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

View file

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

View file

@ -147,7 +147,8 @@ ROUTE_IMPL(RouteRefresh, args)
/* Update the refresh token to point to the new access token */
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();
HashMapSet(response, "access_token", JsonValueString(StrDuplicate(newAccessToken->string)));
HashMapSet(response, "expires_in_ms", JsonValueInteger(newAccessToken->lifetime));

View file

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

View file

@ -156,7 +156,7 @@ StrRandom(size_t len)
static const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
char *str;
int * nums;
int *nums;
size_t i;
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);
HashMapSet(DbJson(rtRef), "refreshes",
JsonValueString(StrDuplicate(result->accessToken->string)));
JsonValueString(StrDuplicate(result->accessToken->string)));
DbUnlock(user->db, rtRef);
}
@ -331,7 +331,7 @@ UserLogin(User * user, char *password, char *deviceId, char *deviceDisplayName,
}
HashMapSet(device, "accessToken",
JsonValueString(StrDuplicate(result->accessToken->string)));
JsonValueString(StrDuplicate(result->accessToken->string)));
return result;
}
@ -382,7 +382,7 @@ UserCheckPassword(User * user, char *password)
}
int
UserSetPassword(User *user, char *password)
UserSetPassword(User * user, char *password)
{
HashMap *json;
@ -409,7 +409,7 @@ UserSetPassword(User *user, char *password)
}
int
UserDeactivate(User *user)
UserDeactivate(User * user)
{
HashMap *json;
@ -426,7 +426,7 @@ UserDeactivate(User *user)
}
HashMap *
UserGetDevices(User *user)
UserGetDevices(User * user)
{
HashMap *json;
@ -441,7 +441,7 @@ UserGetDevices(User *user)
}
UserAccessToken *
UserGenerateAccessToken(User *user, char *deviceId, int withRefresh)
UserGenerateAccessToken(User * user, char *deviceId, int withRefresh)
{
UserAccessToken *token;
@ -463,7 +463,7 @@ UserGenerateAccessToken(User *user, char *deviceId, int withRefresh)
if (withRefresh)
{
token->lifetime = 1000 * 60 * 60 * 24 * 7; /* 1 Week */
token->lifetime = 1000 * 60 * 60 * 24 * 7; /* 1 Week */
}
else
{
@ -474,7 +474,7 @@ UserGenerateAccessToken(User *user, char *deviceId, int withRefresh)
}
int
UserAccessTokenSave(Db *db, UserAccessToken *token)
UserAccessTokenSave(Db * db, UserAccessToken * token)
{
DbRef *ref;
HashMap *json;

View file

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

View file

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