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

@ -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;
@ -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

@ -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));