Reindent server/core/utils.c

This commit is contained in:
Johan Wikman
2015-11-30 20:34:53 +02:00
parent fbecf128dd
commit b2dd24ada3

View File

@ -59,11 +59,12 @@ char hex_lower[] = "0123456789abcdefghijklmnopqrstuvwxyz";
* backend read event triggered by EPOLLIN
*****************************************/
int setnonblocking(int fd) {
int setnonblocking(int fd)
{
int fl;
if ((fl = fcntl(fd, F_GETFL, 0)) == -1) {
if ((fl = fcntl(fd, F_GETFL, 0)) == -1)
{
char errbuf[STRERROR_BUFLEN];
MXS_ERROR("Can't GET fcntl for %i, errno = %d, %s.",
fd,
@ -72,7 +73,8 @@ int setnonblocking(int fd) {
return 1;
}
if (fcntl(fd, F_SETFL, fl | O_NONBLOCK) == -1) {
if (fcntl(fd, F_SETFL, fl | O_NONBLOCK) == -1)
{
char errbuf[STRERROR_BUFLEN];
MXS_ERROR("Can't SET fcntl for %i, errno = %d, %s",
fd,
@ -84,16 +86,20 @@ int setnonblocking(int fd) {
}
char *gw_strend(register const char *s) {
while (*s++);
char *gw_strend(register const char *s)
{
while (*s++)
{
;
}
return (char*) (s - 1);
}
/*****************************************
* generate a random char
*****************************************/
static char gw_randomchar() {
static char gw_randomchar()
{
return (char)((random_jkiss() % 78) + 30);
}
@ -101,11 +107,12 @@ static char gw_randomchar() {
* generate a random string
* output must be pre allocated
*****************************************/
int gw_generate_random_str(char *output, int len) {
int gw_generate_random_str(char *output, int len)
{
int i;
for ( i = 0; i < len; ++i ) {
for (i = 0; i < len; ++i )
{
output[i] = gw_randomchar();
}
@ -118,14 +125,17 @@ int gw_generate_random_str(char *output, int len) {
* hex string to binary data
* output must be pre allocated
*****************************************/
int gw_hex2bin(uint8_t *out, const char *in, unsigned int len) {
int gw_hex2bin(uint8_t *out, const char *in, unsigned int len)
{
const char *in_end= in + len;
if (len == 0 || in == NULL) {
if (len == 0 || in == NULL)
{
return 1;
}
while (in < in_end) {
while (in < in_end)
{
register unsigned char b1 = char_val(*in);
uint8_t b2 = 0;
in++;
@ -142,13 +152,16 @@ int gw_hex2bin(uint8_t *out, const char *in, unsigned int len) {
* binary data to hex string
* output must be pre allocated
*****************************************/
char *gw_bin2hex(char *out, const uint8_t *in, unsigned int len) {
char *gw_bin2hex(char *out, const uint8_t *in, unsigned int len)
{
const uint8_t *in_end = in + len;
if (len == 0 || in == NULL) {
if (len == 0 || in == NULL)
{
return NULL;
}
for (; in != in_end; ++in) {
for (; in != in_end; ++in)
{
*out++ = hex_upper[((uint8_t) *in) >> 4];
*out++ = hex_upper[((uint8_t) *in) & 0x0F];
}
@ -163,12 +176,15 @@ char *gw_bin2hex(char *out, const uint8_t *in, unsigned int len) {
* note that XOR(str1, XOR(str1 CONCAT str2)) == str2
* and that XOR(str1, str2) == XOR(str2, str1)
*****************************************************/
void gw_str_xor(uint8_t *output, const uint8_t *input1, const uint8_t *input2, unsigned int len) {
void gw_str_xor(uint8_t *output, const uint8_t *input1, const uint8_t *input2, unsigned int len)
{
const uint8_t *input1_end = NULL;
input1_end = input1 + len;
while (input1 < input1_end)
{
*output++ = *input1++ ^ *input2++;
}
*output = '\0';
}
@ -177,7 +193,8 @@ void gw_str_xor(uint8_t *output, const uint8_t *input1, const uint8_t *input2, u
* fill a 20 bytes preallocated with SHA1 digest (160 bits)
* for one input on in_len bytes
**********************************************************/
void gw_sha1_str(const uint8_t *in, int in_len, uint8_t *out) {
void gw_sha1_str(const uint8_t *in, int in_len, uint8_t *out)
{
unsigned char hash[SHA_DIGEST_LENGTH];
SHA1(in, in_len, hash);
@ -188,7 +205,8 @@ void gw_sha1_str(const uint8_t *in, int in_len, uint8_t *out) {
* fill 20 bytes preallocated with SHA1 digest (160 bits)
* for two inputs, in_len and in2_len bytes
********************************************************/
void gw_sha1_2_str(const uint8_t *in, int in_len, const uint8_t *in2, int in2_len, uint8_t *out) {
void gw_sha1_2_str(const uint8_t *in, int in_len, const uint8_t *in2, int in2_len, uint8_t *out)
{
SHA_CTX context;
unsigned char hash[SHA_DIGEST_LENGTH];
@ -212,17 +230,18 @@ void gw_sha1_2_str(const uint8_t *in, int in_len, const uint8_t *in2, int in2_le
*
*
*/
int gw_getsockerrno(
int fd)
int gw_getsockerrno(int fd)
{
int eno = 0;
socklen_t elen = sizeof(eno);
if (fd <= 0) {
if (fd <= 0)
{
goto return_eno;
}
if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&eno, &elen) != 0){
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&eno, &elen) != 0)
{
eno = 0;
}
@ -237,13 +256,16 @@ return_eno:
* @return The new allocated encrypted password, that the caller must free
*
*/
char *create_hex_sha1_sha1_passwd(char *passwd) {
char *create_hex_sha1_sha1_passwd(char *passwd)
{
uint8_t hash1[SHA_DIGEST_LENGTH] = "";
uint8_t hash2[SHA_DIGEST_LENGTH] = "";
char *hexpasswd = NULL;
if ((hexpasswd = (char *)calloc(SHA_DIGEST_LENGTH * 2 + 1, 1)) == NULL)
{
return NULL;
}
/* hash1 is SHA1(real_password) */
gw_sha1_str((uint8_t *)passwd, strlen(passwd), hash1);