Apply clang-format code style to all source files

This commit is contained in:
icex2
2019-10-20 00:50:40 +02:00
parent c600e1a423
commit c3bc8782f4
401 changed files with 13844 additions and 11361 deletions
+4 -4
View File
@@ -17,9 +17,10 @@ void array_remove_(size_t itemsz, struct array *array, size_t i)
array->nitems--;
memmove(((uint8_t *) array->items) + i * itemsz,
((uint8_t *) array->items) + (i + 1) * itemsz,
(array->nitems - i) * itemsz);
memmove(
((uint8_t *) array->items) + i * itemsz,
((uint8_t *) array->items) + (i + 1) * itemsz,
(array->nitems - i) * itemsz);
}
void *array_reserve_(size_t itemsz, struct array *array, size_t nitems)
@@ -55,4 +56,3 @@ void array_fini(struct array *array)
{
free(array->items);
}
+4 -7
View File
@@ -9,17 +9,14 @@ struct array {
size_t nalloced;
};
#define array_item(type, array, i) \
(&(((type *) (array)->items)[i]))
#define array_item(type, array, i) (&(((type *) (array)->items)[i]))
#define array_reserve(type, array, nitems) \
((type *) array_reserve_(sizeof(type), array, nitems))
((type *) array_reserve_(sizeof(type), array, nitems))
#define array_remove(type, array, i) \
array_remove_(sizeof(type), array, i)
#define array_remove(type, array, i) array_remove_(sizeof(type), array, i)
#define array_append(type, array) \
array_reserve(type, array, 1)
#define array_append(type, array) array_reserve(type, array, 1)
void array_init(struct array *array);
void *array_reserve_(size_t itemsz, struct array *array, size_t nitems);
+6 -7
View File
@@ -8,8 +8,8 @@
#include <windows.h>
static void push_argv(int *argc, char ***argv, const char *begin,
const char *end)
static void
push_argv(int *argc, char ***argv, const char *begin, const char *end)
{
size_t nchars;
char *str;
@@ -37,7 +37,7 @@ void args_recover(int *argc_out, char ***argv_out)
argv = NULL;
quote = false;
for (begin = pos = GetCommandLine() ; *pos ; pos++) {
for (begin = pos = GetCommandLine(); *pos; pos++) {
switch (*pos) {
case '"':
if (!quote) {
@@ -81,7 +81,7 @@ void args_free(int argc, char **argv)
{
int i;
for (i = 0 ; i < argc ; i++) {
for (i = 0; i < argc; i++) {
free(argv[i]);
}
@@ -98,7 +98,7 @@ char *args_join(int argc, char **argv)
nchars = 0;
for (i = 0 ; i < argc ; i++) {
for (i = 0; i < argc; i++) {
/* 3 = leading space plus two surrounding quotes.
The first element has no leading space, but this is counterbalanced
by the fact that the string needs a NUL terminator. */
@@ -108,7 +108,7 @@ char *args_join(int argc, char **argv)
str = xmalloc(nchars);
pos = str;
for (i = 0 ; i < argc ; i++) {
for (i = 0; i < argc; i++) {
if (i != 0) {
*pos++ = ' ';
}
@@ -126,4 +126,3 @@ char *args_join(int argc, char **argv)
return str;
}
+9 -9
View File
@@ -8,15 +8,15 @@ uint8_t crc8(const void *ptr, size_t nbytes, uint8_t in)
size_t i;
size_t j;
//log_assert(ptr != NULL);
// log_assert(ptr != NULL);
bytes = ptr;
crc = ~in;
for (i = 0 ; i < nbytes ; i++) {
for (i = 0; i < nbytes; i++) {
crc ^= bytes[i];
for (j = 0 ; j < 8 ; j++) {
for (j = 0; j < 8; j++) {
if (crc & 1) {
crc = (crc >> 1) ^ 0x8C;
} else {
@@ -35,15 +35,15 @@ uint16_t crc16(const void *ptr, size_t nbytes, uint16_t in)
size_t i;
size_t j;
//log_assert(ptr != NULL);
// log_assert(ptr != NULL);
bytes = ptr;
crc = ~in;
for (i = 0 ; i < nbytes ; i++) {
for (i = 0; i < nbytes; i++) {
crc ^= bytes[i];
for (j = 0 ; j < 8 ; j++) {
for (j = 0; j < 8; j++) {
if (crc & 1) {
crc = (crc >> 1) ^ 0x8408;
} else {
@@ -62,15 +62,15 @@ uint32_t crc32(const void *ptr, size_t nbytes, uint32_t in)
size_t i;
size_t j;
//log_assert(ptr != NULL);
// log_assert(ptr != NULL);
bytes = ptr;
crc = ~in;
for (i = 0 ; i < nbytes ; i++) {
for (i = 0; i < nbytes; i++) {
crc ^= bytes[i];
for (j = 0 ; j < 8 ; j++) {
for (j = 0; j < 8; j++) {
if (crc & 1) {
crc = (crc >> 1) ^ 0xEDB88320;
} else {
+58 -52
View File
@@ -7,10 +7,10 @@
#include <stdlib.h>
#include <string.h>
static const char vista_prov[]
= "Microsoft Enhanced RSA and AES Cryptographic Provider";
static const char winxp_prov[]
= "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)";
static const char vista_prov[] =
"Microsoft Enhanced RSA and AES Cryptographic Provider";
static const char winxp_prov[] =
"Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)";
static int init_count;
static HCRYPTPROV prov;
@@ -36,10 +36,10 @@ void crypto_init(void)
prov_name = vista_prov;
}
if (!CryptAcquireContext(&prov, NULL, prov_name, PROV_RSA_AES,
CRYPT_VERIFYCONTEXT)) {
log_fatal("CryptAcquireContext failed: %08x",
(unsigned int) GetLastError());
if (!CryptAcquireContext(
&prov, NULL, prov_name, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
log_fatal(
"CryptAcquireContext failed: %08x", (unsigned int) GetLastError());
}
}
@@ -57,13 +57,16 @@ void crypto_fini(void)
log_misc("Shutting down win32 crypto wrappers");
if (!CryptReleaseContext(prov, 0)) {
log_fatal("CryptReleaseContext failed: %08x",
(unsigned int) GetLastError());
log_fatal(
"CryptReleaseContext failed: %08x", (unsigned int) GetLastError());
}
}
void md5_compute(const void *in_bytes, size_t in_nbytes, uint8_t *out_bytes,
size_t out_nbytes)
void md5_compute(
const void *in_bytes,
size_t in_nbytes,
uint8_t *out_bytes,
size_t out_nbytes)
{
struct md5 md5;
@@ -75,16 +78,16 @@ void md5_compute(const void *in_bytes, size_t in_nbytes, uint8_t *out_bytes,
void md5_init(struct md5 *md5)
{
if (!CryptCreateHash(prov, CALG_MD5, 0, 0, &md5->hash)) {
log_fatal("CryptCreateHash(CALG_MD5) failed: %08x",
(unsigned int) GetLastError());
log_fatal(
"CryptCreateHash(CALG_MD5) failed: %08x",
(unsigned int) GetLastError());
}
}
void md5_append(struct md5 *md5, const void *bytes, size_t nbytes)
{
if (!CryptHashData(md5->hash, bytes, nbytes, 0)) {
log_fatal("CryptHashData failed: %08x",
(unsigned int) GetLastError());
log_fatal("CryptHashData failed: %08x", (unsigned int) GetLastError());
}
}
@@ -98,18 +101,22 @@ void md5_fini(struct md5 *md5, uint8_t *output, size_t nbytes)
if (output != NULL) {
if (!CryptGetHashParam(md5->hash, HP_HASHVAL, output, &tmp, 0)) {
log_fatal("Error getting MD5 result: %08x",
(unsigned int) GetLastError());
log_fatal(
"Error getting MD5 result: %08x",
(unsigned int) GetLastError());
}
}
if (!CryptDestroyHash(md5->hash)) {
log_fatal("CryptDestroyHash failed: %08x",
(unsigned int) GetLastError());
log_fatal(
"CryptDestroyHash failed: %08x", (unsigned int) GetLastError());
}
}
#define SWAP(x, y, tmp) tmp = x; x = y; y = tmp
#define SWAP(x, y, tmp) \
tmp = x; \
x = y; \
y = tmp
void arc4_init(struct arc4 *arc4, const uint8_t *key, size_t key_nbytes)
{
@@ -120,13 +127,13 @@ void arc4_init(struct arc4 *arc4, const uint8_t *key, size_t key_nbytes)
arc4->i = 0;
arc4->j = 0;
for (i = 0 ; i < 256 ; i++) {
for (i = 0; i < 256; i++) {
arc4->S[i] = (uint8_t) i;
}
j = 0;
for (i = 0 ; i < 256 ; i++) {
for (i = 0; i < 256; i++) {
j = j + arc4->S[i] + key[i % key_nbytes];
SWAP(arc4->S[i], arc4->S[j], tmp);
}
@@ -137,38 +144,37 @@ void arc4_apply(struct arc4 *arc4, uint8_t *bytes, size_t nbytes)
size_t i;
uint8_t tmp;
for (i = 0 ; i < nbytes ; i++) {
for (i = 0; i < nbytes; i++) {
arc4->i += 1;
arc4->j += arc4->S[arc4->i];
SWAP(arc4->S[arc4->i], arc4->S[arc4->j], tmp);
bytes[i] ^= arc4->S[(uint8_t) (arc4->S[arc4->i] + arc4->S[arc4->j])];
bytes[i] ^= arc4->S[(uint8_t)(arc4->S[arc4->i] + arc4->S[arc4->j])];
}
}
static uint32_t blowfish_F(struct blowfish* blowfish, uint32_t x)
static uint32_t blowfish_F(struct blowfish *blowfish, uint32_t x)
{
unsigned short a, b, c, d;
uint32_t y;
unsigned short a, b, c, d;
uint32_t y;
d = x & 0x00FF;
x >>= 8;
c = x & 0x00FF;
x >>= 8;
b = x & 0x00FF;
x >>= 8;
a = x & 0x00FF;
d = x & 0x00FF;
x >>= 8;
c = x & 0x00FF;
x >>= 8;
b = x & 0x00FF;
x >>= 8;
a = x & 0x00FF;
y = blowfish->S[0][a] + blowfish->S[1][b];
y = y ^ blowfish->S[2][c];
y = y + blowfish->S[3][d];
return y;
y = blowfish->S[0][a] + blowfish->S[1][b];
y = y ^ blowfish->S[2][c];
y = y + blowfish->S[3][d];
return y;
}
void blowfish_init(struct blowfish* blowfish, const uint8_t* key,
size_t key_length)
void blowfish_init(
struct blowfish *blowfish, const uint8_t *key, size_t key_length)
{
int i, j, k;
uint32_t data, datal, datar;
@@ -205,12 +211,12 @@ void blowfish_init(struct blowfish* blowfish, const uint8_t* key,
}
}
void blowfish_encrypt(struct blowfish* blowfish, uint32_t* xl, uint32_t* xr)
void blowfish_encrypt(struct blowfish *blowfish, uint32_t *xl, uint32_t *xr)
{
uint32_t Xl;
uint32_t Xr;
uint32_t temp;
short i;
uint32_t Xl;
uint32_t Xr;
uint32_t temp;
short i;
Xl = *xl;
Xr = *xr;
@@ -232,12 +238,12 @@ void blowfish_encrypt(struct blowfish* blowfish, uint32_t* xl, uint32_t* xr)
*xr = Xr;
}
void blowfish_decrypt(struct blowfish* blowfish, uint32_t* xl, uint32_t* xr)
void blowfish_decrypt(struct blowfish *blowfish, uint32_t *xl, uint32_t *xr)
{
uint32_t Xl;
uint32_t Xr;
uint32_t temp;
short i;
uint32_t Xl;
uint32_t Xr;
uint32_t temp;
short i;
Xl = *xl;
Xr = *xr;
+10 -6
View File
@@ -1,8 +1,8 @@
#ifndef UTIL_CRYPTO_H
#define UTIL_CRYPTO_H
#include <windows.h>
#include <wincrypt.h>
#include <windows.h>
#include <stddef.h>
#include <stdint.h>
@@ -26,8 +26,11 @@ void crypto_init(void);
void crypto_gen_random(void *bytes, size_t nbytes);
void crypto_fini(void);
void md5_compute(const void *in_bytes, size_t in_nbytes, uint8_t *out_bytes,
size_t out_nbytes);
void md5_compute(
const void *in_bytes,
size_t in_nbytes,
uint8_t *out_bytes,
size_t out_nbytes);
void md5_init(struct md5 *md5);
void md5_append(struct md5 *md5, const void *bytes, size_t nbytes);
void md5_fini(struct md5 *md5, uint8_t *output, size_t nbytes);
@@ -35,8 +38,9 @@ void md5_fini(struct md5 *md5, uint8_t *output, size_t nbytes);
void arc4_init(struct arc4 *arc4, const uint8_t *key, size_t key_nbytes);
void arc4_apply(struct arc4 *arc4, uint8_t *bytes, size_t nbytes);
void blowfish_init(struct blowfish* blowfish, const uint8_t* key, size_t key_length);
void blowfish_encrypt(struct blowfish* blowfish, uint32_t* xl, uint32_t* xr);
void blowfish_decrypt(struct blowfish* blowfish, uint32_t* xl, uint32_t* xr);
void blowfish_init(
struct blowfish *blowfish, const uint8_t *key, size_t key_length);
void blowfish_encrypt(struct blowfish *blowfish, uint32_t *xl, uint32_t *xr);
void blowfish_decrypt(struct blowfish *blowfish, uint32_t *xl, uint32_t *xr);
#endif
+1 -1
View File
@@ -13,6 +13,6 @@
#define lengthof(x) (sizeof(x) / sizeof(x[0]))
#define containerof(ptr, outer_t, member) \
((void *) (((uint8_t *) ptr) - offsetof(outer_t, member)))
((void *) (((uint8_t *) ptr) - offsetof(outer_t, member)))
#endif
+26 -27
View File
@@ -1,5 +1,5 @@
#include <windows.h>
#include <shlobj.h>
#include <windows.h>
#include <errno.h>
#include <stdbool.h>
@@ -12,8 +12,8 @@
#include "util/mem.h"
#include "util/str.h"
bool file_load(const char *filename, void **out_bytes, size_t *out_nbytes,
bool text_mode)
bool file_load(
const char *filename, void **out_bytes, size_t *out_nbytes, bool text_mode)
{
FILE *f;
void *bytes;
@@ -70,11 +70,11 @@ bool file_load(const char *filename, void **out_bytes, size_t *out_nbytes,
/* Add null terminator */
if (text_mode) {
((char*) bytes)[nbytes - 1] = '\0';
((char *) bytes)[nbytes - 1] = '\0';
}
log_misc("File loaded %s, size %lu", filename,
(long unsigned int) *out_nbytes);
log_misc(
"File loaded %s, size %lu", filename, (long unsigned int) *out_nbytes);
return true;
@@ -120,22 +120,21 @@ open_fail:
return false;
}
bool path_exists(const char* path)
bool path_exists(const char *path)
{
DWORD attrib = GetFileAttributes(path);
return attrib != INVALID_FILE_ATTRIBUTES;
}
bool path_exists_wstr(const wchar_t* path)
bool path_exists_wstr(const wchar_t *path)
{
DWORD attrib = GetFileAttributesW(path);
return attrib != INVALID_FILE_ATTRIBUTES;
}
FILE *fopen_appdata(const char *vendor, const char *filename,
const char *mode)
FILE *fopen_appdata(const char *vendor, const char *filename, const char *mode)
{
char root[MAX_PATH];
char path[MAX_PATH];
@@ -145,18 +144,17 @@ FILE *fopen_appdata(const char *vendor, const char *filename,
hr = SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, root);
if (FAILED(hr)) {
log_warning("Failed to get AppData folder: hr=%08x",
(unsigned int) hr);
log_warning("Failed to get AppData folder: hr=%08x", (unsigned int) hr);
return NULL;
}
str_format(path, sizeof(path), "%s\\%s", root, vendor);
if (!CreateDirectory(path, NULL)
&& GetLastError() != ERROR_ALREADY_EXISTS) {
log_warning("Failed to create %s: %08x",
path, (unsigned int) GetLastError());
if (!CreateDirectory(path, NULL) &&
GetLastError() != ERROR_ALREADY_EXISTS) {
log_warning(
"Failed to create %s: %08x", path, (unsigned int) GetLastError());
return NULL;
}
@@ -165,8 +163,8 @@ FILE *fopen_appdata(const char *vendor, const char *filename,
f = fopen(path, mode);
if (f == NULL) {
log_warning("fopen(\"%s\", \"%s\") failed: %s",
path, mode, strerror(errno));
log_warning(
"fopen(\"%s\", \"%s\") failed: %s", path, mode, strerror(errno));
}
return f;
@@ -179,17 +177,17 @@ bool path_mkdir(const char *path)
str_cpy(buf, lengthof(buf), path);
for (pos = path_next_element(buf)
; pos
; pos = path_next_element(pos + 1)) {
for (pos = path_next_element(buf); pos; pos = path_next_element(pos + 1)) {
*pos = '\0';
if (strlen(buf) != 2 || buf[1] != ':') {
CreateDirectory(buf, NULL);
if (GetLastError() != ERROR_ALREADY_EXISTS) {
log_warning("%s: Cannot create directory: %#x",
buf, (unsigned int) GetLastError());
log_warning(
"%s: Cannot create directory: %#x",
buf,
(unsigned int) GetLastError());
return false;
}
@@ -201,8 +199,10 @@ bool path_mkdir(const char *path)
CreateDirectory(buf, NULL);
if (GetLastError() != ERROR_ALREADY_EXISTS) {
log_warning("%s: Cannot create directory: %#x",
buf, (unsigned int) GetLastError());
log_warning(
"%s: Cannot create directory: %#x",
buf,
(unsigned int) GetLastError());
return false;
}
@@ -214,7 +214,7 @@ char *path_next_element(char *path)
{
char *c;
for (c = path ; *c ; c++) {
for (c = path; *c; c++) {
if (*c == '/' || *c == '\\') {
return c;
}
@@ -264,4 +264,3 @@ void write_str(FILE *f, const char *str)
fwrite(&len, sizeof(len), 1, f);
fwrite(str, len, 1, f);
}
+12 -13
View File
@@ -17,8 +17,8 @@
* false for binary mode
* @return True on success, false on error
*/
bool file_load(const char *filename, void **bytes, size_t *nbytes,
bool text_mode);
bool file_load(
const char *filename, void **bytes, size_t *nbytes, bool text_mode);
/**
* Save a buffer to a file (overwrite any existing files with the same name)
@@ -36,7 +36,7 @@ bool file_save(const char *filename, const void *bytes, size_t nbytes);
* @param path Path as c-string.
* @return True if exists, false otherwise
*/
bool path_exists(const char* path);
bool path_exists(const char *path);
/**
* Check if a normal file, folder, symlink, ... exists
@@ -44,10 +44,9 @@ bool path_exists(const char* path);
* @param path Path as w-string.
* @return True if exists, false otherwise
*/
bool path_exists_wstr(const wchar_t* path);
bool path_exists_wstr(const wchar_t *path);
FILE *fopen_appdata(const char *vendor, const char *filename,
const char *mode);
FILE *fopen_appdata(const char *vendor, const char *filename, const char *mode);
/**
* Create a new directory
@@ -58,17 +57,17 @@ FILE *fopen_appdata(const char *vendor, const char *filename,
bool path_mkdir(const char *path);
char *path_next_element(char *path);
#define read_bin(f, val, size) (fread((val), (size), 1, (f)) == 1)
#define read_u8(f, val) (fread((val), 1, 1, (f)) == 1)
#define read_u16(f, val) (fread((val), 2, 1, (f)) == 1)
#define read_u32(f, val) (fread((val), 4, 1, (f)) == 1)
#define read_bin(f, val, size) (fread((val), (size), 1, (f)) == 1)
#define read_u8(f, val) (fread((val), 1, 1, (f)) == 1)
#define read_u16(f, val) (fread((val), 2, 1, (f)) == 1)
#define read_u32(f, val) (fread((val), 4, 1, (f)) == 1)
bool read_str(FILE *f, char **str);
#define write_bin(f, val, size) fwrite((val), (size), 1, (f))
#define write_u8(f, val) fwrite((val), 1, 1, (f))
#define write_u16(f, val) fwrite((val), 2, 1, (f))
#define write_u32(f, val) fwrite((val), 4, 1, (f))
#define write_u8(f, val) fwrite((val), 1, 1, (f))
#define write_u16(f, val) fwrite((val), 2, 1, (f))
#define write_u32(f, val) fwrite((val), 4, 1, (f))
void write_str(FILE *f, const char *str);
+10 -7
View File
@@ -31,9 +31,9 @@ bool hex_decode(void *ptr, size_t nbytes, const char *chars, size_t nchars)
log_assert(nchars <= 2 * nbytes);
log_assert(nchars % 2 == 0);
for (i = 0 ; i < nchars / 2; i++) {
if ( !hex_decode_nibble(chars[2 * i + 0], &hi) ||
!hex_decode_nibble(chars[2 * i + 1], &lo)) {
for (i = 0; i < nchars / 2; i++) {
if (!hex_decode_nibble(chars[2 * i + 0], &hi) ||
!hex_decode_nibble(chars[2 * i + 1], &lo)) {
return false;
}
@@ -43,8 +43,12 @@ bool hex_decode(void *ptr, size_t nbytes, const char *chars, size_t nchars)
return true;
}
static void hex_encode(const void *ptr, size_t nbytes, char *chars,
size_t nchars, const char *digits)
static void hex_encode(
const void *ptr,
size_t nbytes,
char *chars,
size_t nchars,
const char *digits)
{
const uint8_t *bytes = ptr;
@@ -52,7 +56,7 @@ static void hex_encode(const void *ptr, size_t nbytes, char *chars,
log_assert(nchars >= 2 * nbytes + 1);
for (i = 0 ; i < nbytes ; i++) {
for (i = 0; i < nbytes; i++) {
chars[i * 2 + 0] = digits[bytes[i] >> 4];
chars[i * 2 + 1] = digits[bytes[i] & 15];
}
@@ -69,4 +73,3 @@ void hex_encode_uc(const void *bytes, size_t nbytes, char *chars, size_t nchars)
{
hex_encode(bytes, nbytes, chars, nchars, "0123456789ABCDEF");
}
+5 -6
View File
@@ -4,11 +4,10 @@
#include <stdbool.h>
#include <stddef.h>
bool hex_decode(void *bytes, size_t nbytes, const char *chars,
size_t nchars);
void hex_encode_uc(const void *bytes, size_t nbytes, char *chars,
size_t nchars);
void hex_encode_lc(const void *bytes, size_t nbytes, char *chars,
size_t nchars);
bool hex_decode(void *bytes, size_t nbytes, const char *chars, size_t nchars);
void hex_encode_uc(
const void *bytes, size_t nbytes, char *chars, size_t nchars);
void hex_encode_lc(
const void *bytes, size_t nbytes, char *chars, size_t nchars);
#endif
+35 -13
View File
@@ -4,7 +4,8 @@
#include "util/hr.h"
HRESULT hr_from_win32(void)
HRESULT
hr_from_win32(void)
{
return HRESULT_FROM_WIN32(GetLastError());
}
@@ -20,20 +21,41 @@ void hr_propagate_win32_(HRESULT hr)
} else {
// https://docs.microsoft.com/en-us/windows/desktop/seccrypto/common-hresult-values
switch (hr) {
case E_ABORT: result = ERROR_OPERATION_ABORTED; break;
case E_ACCESSDENIED: result = ERROR_ACCESS_DENIED; break;
case E_FAIL: result = ERROR_GEN_FAILURE; break;
case E_HANDLE: result = ERROR_INVALID_HANDLE; break;
case E_INVALIDARG: result = ERROR_INVALID_PARAMETER; break;
case E_NOINTERFACE: result = ERROR_NOT_SUPPORTED; break;
case E_NOTIMPL: result = ERROR_NOT_SUPPORTED; break;
case E_OUTOFMEMORY: result = ERROR_OUTOFMEMORY; break;
case E_POINTER: result = ERROR_INVALID_ADDRESS; break;
case E_UNEXPECTED: result = ERROR_INTERNAL_ERROR; break;
default: result = ERROR_INTERNAL_ERROR; break;
case E_ABORT:
result = ERROR_OPERATION_ABORTED;
break;
case E_ACCESSDENIED:
result = ERROR_ACCESS_DENIED;
break;
case E_FAIL:
result = ERROR_GEN_FAILURE;
break;
case E_HANDLE:
result = ERROR_INVALID_HANDLE;
break;
case E_INVALIDARG:
result = ERROR_INVALID_PARAMETER;
break;
case E_NOINTERFACE:
result = ERROR_NOT_SUPPORTED;
break;
case E_NOTIMPL:
result = ERROR_NOT_SUPPORTED;
break;
case E_OUTOFMEMORY:
result = ERROR_OUTOFMEMORY;
break;
case E_POINTER:
result = ERROR_INVALID_ADDRESS;
break;
case E_UNEXPECTED:
result = ERROR_INTERNAL_ERROR;
break;
default:
result = ERROR_INTERNAL_ERROR;
break;
}
}
SetLastError(result);
}
+8 -8
View File
@@ -35,16 +35,16 @@ void iobuf_flip(struct const_iobuf *dest, const struct iobuf *src)
dest->nbytes = src->pos;
}
void iobuf_log(struct iobuf* buffer, const char* tag)
void iobuf_log(struct iobuf *buffer, const char *tag)
{
char* str;
char *str;
size_t str_len;
str_len = buffer->nbytes * 2 + 1;
str = xmalloc(str_len);
log_misc("[%s] (%d %d)", tag, (uint32_t) buffer->nbytes,
(uint32_t) buffer->pos);
log_misc(
"[%s] (%d %d)", tag, (uint32_t) buffer->nbytes, (uint32_t) buffer->pos);
hex_encode_uc(buffer->bytes, buffer->nbytes, str, str_len);
@@ -53,16 +53,16 @@ void iobuf_log(struct iobuf* buffer, const char* tag)
free(str);
}
void iobuf_log_const(struct const_iobuf* buffer, const char* tag)
void iobuf_log_const(struct const_iobuf *buffer, const char *tag)
{
char* str;
char *str;
size_t str_len;
str_len = buffer->nbytes * 2 + 1;
str = xmalloc(str_len);
log_misc("[%s] (%d %d)", tag, (uint32_t) buffer->nbytes,
(uint32_t) buffer->pos);
log_misc(
"[%s] (%d %d)", tag, (uint32_t) buffer->nbytes, (uint32_t) buffer->pos);
hex_encode_uc(buffer->bytes, buffer->nbytes, str, str_len);
+2 -2
View File
@@ -20,8 +20,8 @@ size_t iobuf_move(struct iobuf *dest, struct const_iobuf *src);
void iobuf_flip(struct const_iobuf *dest, const struct iobuf *src);
void iobuf_log(struct iobuf* buffer, const char* tag);
void iobuf_log(struct iobuf *buffer, const char *tag);
void iobuf_log_const(struct const_iobuf* buffer, const char* tag);
void iobuf_log_const(struct const_iobuf *buffer, const char *tag);
#endif
+5 -9
View File
@@ -4,9 +4,8 @@
#include "util/list.h"
extern inline struct list_node *list_peek_head(struct list *list);
extern inline const struct list_node *list_peek_head_const(
const struct list *list
);
extern inline const struct list_node *
list_peek_head_const(const struct list *list);
void list_init(struct list *list)
{
@@ -30,7 +29,7 @@ bool list_contains(struct list *list, struct list_node *node)
{
struct list_node *pos;
for (pos = list->head ; pos != NULL ; pos = pos->next) {
for (pos = list->head; pos != NULL; pos = pos->next) {
if (pos == node) {
return true;
}
@@ -64,9 +63,8 @@ void list_remove(struct list *list, struct list_node *node)
struct list_node *pos;
struct list_node *prev;
for (prev = NULL, pos = list->head
; pos != NULL
; prev = pos, pos = pos->next) {
for (prev = NULL, pos = list->head; pos != NULL;
prev = pos, pos = pos->next) {
if (pos == node) {
if (prev != NULL) {
prev->next = node->next;
@@ -84,5 +82,3 @@ void list_remove(struct list *list, struct list_node *node)
}
}
}
+21 -18
View File
@@ -16,17 +16,17 @@ static void log_builtin_fatal(const char *module, const char *fmt, ...);
static void log_builtin_info(const char *module, const char *fmt, ...);
static void log_builtin_misc(const char *module, const char *fmt, ...);
static void log_builtin_warning(const char *module, const char *fmt, ...);
static void log_builtin_format(unsigned int msg_level, const char *module,
const char *fmt, va_list ap);
static void log_builtin_format(
unsigned int msg_level, const char *module, const char *fmt, va_list ap);
#define IMPLEMENT_SINK(name, msg_level) \
#define IMPLEMENT_SINK(name, msg_level) \
static void name(const char *module, const char *fmt, ...) \
{ \
va_list ap; \
\
va_start(ap, fmt); \
log_builtin_format(msg_level, module, fmt, ap); \
va_end(ap); \
{ \
va_list ap; \
\
va_start(ap, fmt); \
log_builtin_format(msg_level, module, fmt, ap); \
va_end(ap); \
}
IMPLEMENT_SINK(log_builtin_info, 2)
@@ -45,8 +45,8 @@ static void log_builtin_fatal(const char *module, const char *fmt, ...)
ExitProcess(EXIT_FAILURE);
}
static void log_builtin_format(unsigned int msg_level, const char *module,
const char *fmt, va_list ap)
static void log_builtin_format(
unsigned int msg_level, const char *module, const char *fmt, va_list ap)
{
static const char chars[] = "FWIM";
@@ -56,8 +56,8 @@ static void log_builtin_format(unsigned int msg_level, const char *module,
int result;
str_vformat(msg, sizeof(msg), fmt, ap);
result = str_format(line, sizeof(line), "%c:%s: %s\n", chars[msg_level],
module, msg);
result = str_format(
line, sizeof(line), "%c:%s: %s\n", chars[msg_level], module, msg);
if (msg_level <= log_level) {
log_writer(log_writer_ctx, line, result);
@@ -69,8 +69,11 @@ void log_assert_body(const char *file, int line, const char *function)
log_impl_fatal("assert", "%s:%d: function `%s'", file, line, function);
}
void log_to_external(log_formatter_t misc, log_formatter_t info,
log_formatter_t warning, log_formatter_t fatal)
void log_to_external(
log_formatter_t misc,
log_formatter_t info,
log_formatter_t warning,
log_formatter_t fatal)
{
log_impl_misc = misc;
log_impl_info = info;
@@ -116,11 +119,12 @@ void log_writer_stderr(void *ctx, const char *chars, size_t nchars)
void log_writer_file(void *ctx, const char *chars, size_t nchars)
{
fwrite(chars, 1, nchars, (FILE *) ctx);
fflush((FILE*) ctx);
fflush((FILE *) ctx);
}
void log_writer_null(void *ctx, const char *chars, size_t nchars)
{}
{
}
log_formatter_t log_impl_misc = log_builtin_misc;
log_formatter_t log_impl_info = log_builtin_info;
@@ -128,4 +132,3 @@ log_formatter_t log_impl_warning = log_builtin_warning;
log_formatter_t log_impl_fatal = log_builtin_fatal;
static log_writer_t log_writer = log_writer_null;
static unsigned int log_level = 4;
+18 -10
View File
@@ -25,11 +25,11 @@
/* This doesn't really belong here, but it's what libavs does so w/e */
#define log_assert(x) \
do { \
if (!(x)) { \
#define log_assert(x) \
do { \
if (!(x)) { \
log_assert_body(__FILE__, __LINE__, __FUNCTION__); \
} \
} \
} while (0)
#else
@@ -37,14 +37,19 @@
#define log_misc(...)
#define log_info(...)
#define log_warning(...)
#define log_assert(x) do { if (!(x)) { abort(); } } while (0)
#define log_assert(x) \
do { \
if (!(x)) { \
abort(); \
} \
} while (0)
#endif
#define log_fatal(...) \
do { \
#define log_fatal(...) \
do { \
log_impl_fatal(LOG_MODULE, __VA_ARGS__); \
abort(); \
abort(); \
} while (0)
typedef void (*log_writer_t)(void *ctx, const char *chars, size_t nchars);
@@ -55,8 +60,11 @@ extern log_formatter_t log_impl_warning;
extern log_formatter_t log_impl_fatal;
void log_assert_body(const char *file, int line, const char *function);
void log_to_external(log_formatter_t misc, log_formatter_t info,
log_formatter_t warning, log_formatter_t fatal);
void log_to_external(
log_formatter_t misc,
log_formatter_t info,
log_formatter_t warning,
log_formatter_t fatal);
void log_to_writer(log_writer_t writer, void *ctx);
void log_set_level(unsigned int new_level);
+20 -14
View File
@@ -2,10 +2,10 @@
#include <windows.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "util/log.h"
@@ -58,32 +58,38 @@ bool mem_nop(size_t mem_offset, size_t length)
{
DWORD oldProt;
if (VirtualProtect((void*) (mem_offset), length, PAGE_EXECUTE_READWRITE,
&oldProt) == 0) {
if (VirtualProtect(
(void *) (mem_offset), length, PAGE_EXECUTE_READWRITE, &oldProt) ==
0) {
return false;
}
for (size_t i = 0; i < length; i++) {
((uint8_t*) mem_offset)[i] = 0x90;
((uint8_t *) mem_offset)[i] = 0x90;
}
if (VirtualProtect((void*) (mem_offset), length, oldProt, &oldProt) == 0) {
if (VirtualProtect((void *) (mem_offset), length, oldProt, &oldProt) == 0) {
return false;
}
return true;
}
void* mem_find_signiture(const uint8_t* sig, uint32_t sig_len,
int32_t sig_offset, void* start_addr, void* end_addr, int32_t alignment)
void *mem_find_signiture(
const uint8_t *sig,
uint32_t sig_len,
int32_t sig_offset,
void *start_addr,
void *end_addr,
int32_t alignment)
{
size_t pos;
size_t pos;
for (pos = (size_t) start_addr; pos < (size_t) end_addr; pos += alignment) {
if (!memcmp((void*) pos, sig, sig_len)) {
return (void*) (pos + sig_offset);
}
}
for (pos = (size_t) start_addr; pos < (size_t) end_addr; pos += alignment) {
if (!memcmp((void *) pos, sig, sig_len)) {
return (void *) (pos + sig_offset);
}
}
return NULL;
return NULL;
}
+8 -3
View File
@@ -1,8 +1,8 @@
#ifndef UTIL_MEM_H
#define UTIL_MEM_H
#include <stddef.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
void *xcalloc(size_t nbytes);
@@ -24,7 +24,12 @@ bool mem_nop(size_t mem_offset, size_t length);
* @return If found, pointer to memory where the signiture was found (first
* occurance) with the signiture offset added, NULL otherwise
*/
void* mem_find_signiture(const uint8_t* sig, uint32_t sig_len,
int32_t sig_offset, void* start_addr, void* end_addr, int32_t alignment);
void *mem_find_signiture(
const uint8_t *sig,
uint32_t sig_len,
int32_t sig_offset,
void *start_addr,
void *end_addr,
int32_t alignment);
#endif
+11 -10
View File
@@ -41,15 +41,16 @@ static int msg_thread_proc(void *param)
Because THAT totally makes sense. */
hwnd = CreateWindowEx(0, msg_wndclass_name, NULL, 0, 0, 0, 0, 0,
NULL, NULL, inst, NULL);
hwnd = CreateWindowEx(
0, msg_wndclass_name, NULL, 0, 0, 0, 0, 0, NULL, NULL, inst, NULL);
msg_window_setup(hwnd); /* callback */
PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
SetEvent(msg_thread_ready);
while (msg_thread_step(hwnd));
while (msg_thread_step(hwnd))
;
msg_window_teardown(hwnd); /* callback */
@@ -64,8 +65,8 @@ static bool msg_thread_step(HWND hwnd)
MSG msg;
DWORD result;
result = MsgWaitForMultipleObjectsEx(1, &msg_thread_stop, INFINITE,
QS_ALLINPUT, MWMO_ALERTABLE);
result = MsgWaitForMultipleObjectsEx(
1, &msg_thread_stop, INFINITE, QS_ALLINPUT, MWMO_ALERTABLE);
switch (result) {
case WAIT_OBJECT_0:
@@ -83,14 +84,15 @@ static bool msg_thread_step(HWND hwnd)
return true;
case WAIT_FAILED:
log_warning("MsgWaitForMultipleObjectsEx failed: %08x",
(unsigned int) GetLastError());
log_warning(
"MsgWaitForMultipleObjectsEx failed: %08x",
(unsigned int) GetLastError());
return false;
default:
log_warning("Spurious wakeup: result = %08x",
(unsigned int) result);
log_warning(
"Spurious wakeup: result = %08x", (unsigned int) result);
return true;
}
@@ -115,4 +117,3 @@ void msg_thread_fini(void)
CloseHandle(msg_thread_stop);
}
+2 -2
View File
@@ -7,8 +7,8 @@ void msg_thread_fini(void);
/* Define these callbacks */
extern void msg_window_setup(HWND hwnd);
extern LRESULT WINAPI msg_window_proc(HWND hwnd, UINT msg, WPARAM wparam,
LPARAM lparam);
extern LRESULT WINAPI
msg_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
extern void msg_window_teardown(HWND hwnd);
#endif
+107 -60
View File
@@ -13,7 +13,7 @@
#include <inttypes.h>
static bool net_parse_url(const char* str, struct net_addr* addr)
static bool net_parse_url(const char *str, struct net_addr *addr)
{
char host[NET_MAX_URL_HOSTNAME_LEN + 1];
char path[NET_MAX_URL_PATH_LEN + 1];
@@ -38,8 +38,8 @@ static bool net_parse_url(const char* str, struct net_addr* addr)
/* Try url with IPV4 address */
if (sscanf(str, "%d.%d.%d.%d", &ipv4[0], &ipv4[1], &ipv4[2],
&ipv4[3]) == 4) {
if (sscanf(str, "%d.%d.%d.%d", &ipv4[0], &ipv4[1], &ipv4[2], &ipv4[3]) ==
4) {
addr->url.type = NET_ADDR_TYPE_IPV4;
addr->url.ipv4.addr = (ipv4[0] & 0xFF) | ((ipv4[1] & 0xFF) << 8) |
((ipv4[2] & 0xFF) << 16) | ((ipv4[3] & 0xFF) << 24);
@@ -48,8 +48,15 @@ static bool net_parse_url(const char* str, struct net_addr* addr)
/* Port and trailing path are optional, check with and without */
toks = sscanf(str, "%d.%d.%d.%d:%d/%s", &ipv4[0], &ipv4[1],
&ipv4[2], &ipv4[3], &port, path);
toks = sscanf(
str,
"%d.%d.%d.%d:%d/%s",
&ipv4[0],
&ipv4[1],
&ipv4[2],
&ipv4[3],
&port,
path);
if (toks > 4) {
if (toks >= 5) {
@@ -61,8 +68,14 @@ static bool net_parse_url(const char* str, struct net_addr* addr)
}
} else {
/* No port, but may have trailing path */
toks = sscanf(str, "%d.%d.%d.%d/%s", &ipv4[0], &ipv4[1],
&ipv4[2], &ipv4[3], path);
toks = sscanf(
str,
"%d.%d.%d.%d/%s",
&ipv4[0],
&ipv4[1],
&ipv4[2],
&ipv4[3],
path);
if (toks > 4) {
strcpy(addr->url.path, path);
@@ -93,7 +106,6 @@ static bool net_parse_url(const char* str, struct net_addr* addr)
toks = sscanf(str, "%[^/]/%s", host, path);
strcpy(addr->url.hostname.host, host);
if (toks > 1) {
strcpy(addr->url.path, path);
}
@@ -103,7 +115,7 @@ static bool net_parse_url(const char* str, struct net_addr* addr)
return true;
}
static bool net_parse_ipv4_or_hostname(const char* str, struct net_addr* addr)
static bool net_parse_ipv4_or_hostname(const char *str, struct net_addr *addr)
{
char host[NET_MAX_URL_HOSTNAME_LEN + 1];
uint32_t ipv4[4];
@@ -112,15 +124,15 @@ static bool net_parse_ipv4_or_hostname(const char* str, struct net_addr* addr)
memset(host, 0, sizeof(host));
toks = sscanf(str, "%d.%d.%d.%d:%d", &ipv4[0], &ipv4[1], &ipv4[2], &ipv4[3],
&port);
toks = sscanf(
str, "%d.%d.%d.%d:%d", &ipv4[0], &ipv4[1], &ipv4[2], &ipv4[3], &port);
if (toks >= 4) {
addr->type = NET_ADDR_TYPE_IPV4;
addr->ipv4.addr = (ipv4[0] & 0xFF) | ((ipv4[1] & 0xFF) << 8) |
((ipv4[2] & 0xFF) << 16) | ((ipv4[3] & 0xFF) << 24);
addr->ipv4.port = NET_INVALID_PORT;
if (toks == 5) {
addr->ipv4.port = port;
}
@@ -146,37 +158,61 @@ static bool net_parse_ipv4_or_hostname(const char* str, struct net_addr* addr)
return true;
}
static char* net_ipv4_to_str(const struct net_addr_ipv4* addr)
static char *net_ipv4_to_str(const struct net_addr_ipv4 *addr)
{
size_t len;
uint8_t* addr_ipv4;
char* str;
uint8_t *addr_ipv4;
char *str;
addr_ipv4 = (uint8_t*) &addr->addr;
addr_ipv4 = (uint8_t *) &addr->addr;
if (addr->port != NET_INVALID_PORT) {
len = snprintf(NULL, 0, "%d.%d.%d.%d:%d", addr_ipv4[0],
addr_ipv4[1], addr_ipv4[2], addr_ipv4[3], addr->port);
len = snprintf(
NULL,
0,
"%d.%d.%d.%d:%d",
addr_ipv4[0],
addr_ipv4[1],
addr_ipv4[2],
addr_ipv4[3],
addr->port);
str = xmalloc(len + 1);
sprintf(str, "%d.%d.%d.%d:%d", addr_ipv4[0],
addr_ipv4[1], addr_ipv4[2], addr_ipv4[3], addr->port);
sprintf(
str,
"%d.%d.%d.%d:%d",
addr_ipv4[0],
addr_ipv4[1],
addr_ipv4[2],
addr_ipv4[3],
addr->port);
} else {
len = snprintf(NULL, 0, "%d.%d.%d.%d", addr_ipv4[0], addr_ipv4[1],
addr_ipv4[2], addr_ipv4[3]);
len = snprintf(
NULL,
0,
"%d.%d.%d.%d",
addr_ipv4[0],
addr_ipv4[1],
addr_ipv4[2],
addr_ipv4[3]);
str = xmalloc(len + 1);
sprintf(str, "%d.%d.%d.%d", addr_ipv4[0], addr_ipv4[1],
addr_ipv4[2], addr_ipv4[3]);
sprintf(
str,
"%d.%d.%d.%d",
addr_ipv4[0],
addr_ipv4[1],
addr_ipv4[2],
addr_ipv4[3]);
}
return str;
}
static char* net_hostname_to_str(const struct net_addr_hostname* addr)
static char *net_hostname_to_str(const struct net_addr_hostname *addr)
{
size_t len;
char* str;
char *str;
if (addr->port != NET_INVALID_PORT) {
len = snprintf(NULL, 0, "%s:%d", addr->host, addr->port);
@@ -190,12 +226,12 @@ static char* net_hostname_to_str(const struct net_addr_hostname* addr)
return str;
}
static char* net_url_to_str(const struct net_addr_url* addr)
static char *net_url_to_str(const struct net_addr_url *addr)
{
size_t len;
size_t str_len;
char* tmp;
char* str;
char *tmp;
char *str;
switch (addr->type) {
case NET_ADDR_TYPE_IPV4:
@@ -216,23 +252,34 @@ static char* net_url_to_str(const struct net_addr_url* addr)
str_len = strlen(addr->path);
len = snprintf(NULL, 0, "http%s://%s%s%s", addr->is_https ? "s" : "",
tmp, str_len > 0 ? "/" : "", str_len > 0 ? addr->path : "");
len = snprintf(
NULL,
0,
"http%s://%s%s%s",
addr->is_https ? "s" : "",
tmp,
str_len > 0 ? "/" : "",
str_len > 0 ? addr->path : "");
str = xmalloc(len + 1);
sprintf(str, "http%s://%s%s%s", addr->is_https ? "s" : "",
tmp, str_len > 0 ? "/" : "", str_len > 0 ? addr->path : "");
sprintf(
str,
"http%s://%s%s%s",
addr->is_https ? "s" : "",
tmp,
str_len > 0 ? "/" : "",
str_len > 0 ? addr->path : "");
free(tmp);
return str;
}
static bool net_resolve_hostname_str(const char* host, uint32_t* ipv4)
static bool net_resolve_hostname_str(const char *host, uint32_t *ipv4)
{
struct WSAData data;
struct addrinfo hints;
struct addrinfo* res;
struct addrinfo *res;
int err;
log_assert(host);
@@ -245,7 +292,7 @@ static bool net_resolve_hostname_str(const char* host, uint32_t* ipv4)
/* Using gethostbyname seems to crash on certain setups...use
getaddrinfo (which is not deprecated) */
WSAStartup(MAKEWORD(2,0), &data);
WSAStartup(MAKEWORD(2, 0), &data);
err = getaddrinfo(host, NULL, &hints, &res);
@@ -254,17 +301,17 @@ static bool net_resolve_hostname_str(const char* host, uint32_t* ipv4)
return false;
}
*ipv4 = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.S_un.S_addr;
*ipv4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr.S_un.S_addr;
freeaddrinfo(res);
WSACleanup();
return true;
}
static bool net_resolve_hostname_ipv4(const struct net_addr_ipv4* addr,
struct net_addr_ipv4* res_addr)
static bool net_resolve_hostname_ipv4(
const struct net_addr_ipv4 *addr, struct net_addr_ipv4 *res_addr)
{
res_addr->addr = addr->addr;
res_addr->port = addr->port;
@@ -272,15 +319,15 @@ static bool net_resolve_hostname_ipv4(const struct net_addr_ipv4* addr,
return true;
}
static bool net_resolve_hostname_host(const struct net_addr_hostname* addr,
struct net_addr_ipv4* res_addr)
static bool net_resolve_hostname_host(
const struct net_addr_hostname *addr, struct net_addr_ipv4 *res_addr)
{
res_addr->port = addr->port;
return net_resolve_hostname_str(addr->host, &res_addr->addr);
}
static bool net_resolve_hostname_url(const struct net_addr_url* addr,
struct net_addr_ipv4* res_addr)
static bool net_resolve_hostname_url(
const struct net_addr_url *addr, struct net_addr_ipv4 *res_addr)
{
switch (addr->type) {
case NET_ADDR_TYPE_IPV4:
@@ -298,7 +345,7 @@ static bool net_resolve_hostname_url(const struct net_addr_url* addr,
}
}
bool net_str_parse(const char* str, struct net_addr* addr)
bool net_str_parse(const char *str, struct net_addr *addr)
{
log_assert(str);
log_assert(addr);
@@ -310,7 +357,7 @@ bool net_str_parse(const char* str, struct net_addr* addr)
}
}
char* net_addr_to_str(const struct net_addr* addr)
char *net_addr_to_str(const struct net_addr *addr)
{
log_assert(addr);
@@ -331,13 +378,13 @@ char* net_addr_to_str(const struct net_addr* addr)
}
}
bool net_resolve_hostname(const char* hostname, struct net_addr_ipv4* res_addr)
bool net_resolve_hostname(const char *hostname, struct net_addr_ipv4 *res_addr)
{
return net_resolve_hostname_str(hostname, &res_addr->addr);
}
bool net_resolve_hostname_net_addr(const struct net_addr* addr,
struct net_addr_ipv4* res_addr)
bool net_resolve_hostname_net_addr(
const struct net_addr *addr, struct net_addr_ipv4 *res_addr)
{
switch (addr->type) {
case NET_ADDR_TYPE_IPV4:
@@ -356,11 +403,11 @@ bool net_resolve_hostname_net_addr(const struct net_addr* addr,
}
}
bool net_check_remote_connection(const struct net_addr* addr,
uint32_t timeout_ms)
bool net_check_remote_connection(
const struct net_addr *addr, uint32_t timeout_ms)
{
struct net_addr_ipv4 target;
if (!net_resolve_hostname_net_addr(addr, &target)) {
return false;
}
@@ -368,8 +415,8 @@ bool net_check_remote_connection(const struct net_addr* addr,
return net_check_remote_connection_ipv4(&target, timeout_ms);
}
bool net_check_remote_connection_ipv4(const struct net_addr_ipv4* addr,
uint32_t timeout_ms)
bool net_check_remote_connection_ipv4(
const struct net_addr_ipv4 *addr, uint32_t timeout_ms)
{
struct WSAData data;
SOCKET s;
@@ -381,7 +428,7 @@ bool net_check_remote_connection_ipv4(const struct net_addr_ipv4* addr,
bool success;
struct sockaddr_in sockaddr;
WSAStartup(MAKEWORD(2,0), &data);
WSAStartup(MAKEWORD(2, 0), &data);
err = 0;
success = true;
@@ -406,8 +453,8 @@ bool net_check_remote_connection_ipv4(const struct net_addr_ipv4* addr,
sockaddr.sin_addr.s_addr = addr->addr;
sockaddr.sin_port = htons(addr->port);
if (connect(s, (struct sockaddr*) &sockaddr, sizeof(sockaddr)) ==
SOCKET_ERROR) {
if (connect(s, (struct sockaddr *) &sockaddr, sizeof(sockaddr)) ==
SOCKET_ERROR) {
if (WSAGetLastError() != WSAEWOULDBLOCK) {
/* connection failed */;
success = false;
@@ -418,7 +465,7 @@ bool net_check_remote_connection_ipv4(const struct net_addr_ipv4* addr,
FD_SET(s, &set_e);
timeout.tv_sec = timeout_ms / 1000;
timeout.tv_usec = (timeout_ms % 1000) * 1000;
timeout.tv_usec = (timeout_ms % 1000) * 1000;
err = select(0, NULL, &set_w, &set_e, &timeout);
@@ -435,7 +482,7 @@ bool net_check_remote_connection_ipv4(const struct net_addr_ipv4* addr,
} else {
success = false;
}
closesocket(s);
WSACleanup();
+17 -16
View File
@@ -74,64 +74,65 @@ struct net_addr {
* - Plain hostname with port, e.g. localhost:1234
* - HTTP(s) URL, e.g. http://www.myremote.com
* - HTTP(s) URL with port, e.g. http://www.myremote.com:1234
* - HTTP(s) URL with or without port and path: http://www.myremote.com:1234/somewhere
*
* - HTTP(s) URL with or without port and path:
* http://www.myremote.com:1234/somewhere
*
* @param str String to parse.
* @param addr Pointer to a net_addr struct to write the result to.
* @return True if parsing is successful, false on parse error.
*/
bool net_str_parse(const char* str, struct net_addr* addr);
bool net_str_parse(const char *str, struct net_addr *addr);
// port not printed if invalid
/**
* net_addr struct to string function. Create a single c-string representation.
*
*
* @param addr Input net_addr.
* @return c-string representation. Note: Invalid port values are omitted.
*/
char* net_addr_to_str(const struct net_addr* addr);
char *net_addr_to_str(const struct net_addr *addr);
/**
* Resolve a hostname to an IPV4 address.
*
*
* @param hostname Hostname to resolve, e.g. localhost.
* @param res_addr Pointer to a net_addr_ipv4 struct to store the result to.
* @return True if resolving was successful, false on error.
*/
bool net_resolve_hostname(const char* hostname, struct net_addr_ipv4* res_addr);
bool net_resolve_hostname(const char *hostname, struct net_addr_ipv4 *res_addr);
/**
* Resolve a net_addr struct to an IPV4 address. If the net_addr struct is
* actually of IPV4 type, the IPV4 address and port are simply copied to the
* result struct.
*
*
* @param addr net_addr struct to resolve.
* @param res_addr Pointer to a net_addr_ipv4 struct to store the result to.
* @return True if resolving was successful, false on error.
*/
bool net_resolve_hostname_net_addr(const struct net_addr* addr,
struct net_addr_ipv4* res_addr);
bool net_resolve_hostname_net_addr(
const struct net_addr *addr, struct net_addr_ipv4 *res_addr);
/**
* Check if a remote server is reachable, i.e. it is possible to open a
* connection to it.
*
*
* @param addr net_addr struct of target server to check.
* @param timeout_ms Timeout for the check in ms.
* @return True if target server is reachable/connectable, false otherwise.
*/
bool net_check_remote_connection(const struct net_addr* addr,
uint32_t timeout_ms);
bool net_check_remote_connection(
const struct net_addr *addr, uint32_t timeout_ms);
/**
* Check if a remote server is reachable, i.e. it is possible to open a
* connection to it.
*
*
* @param addr net_addr_ipv4 struct of target server to check.
* @param timeout_ms Timeout for the check in ms.
* @return True if target server is reachable/connectable, false otherwise.
*/
bool net_check_remote_connection_ipv4(const struct net_addr_ipv4* addr,
uint32_t timeout_ms);
bool net_check_remote_connection_ipv4(
const struct net_addr_ipv4 *addr, uint32_t timeout_ms);
#endif
+7 -9
View File
@@ -38,9 +38,9 @@ bool wstr_ends_with(const wchar_t *haystack, const wchar_t *needle)
}
return !memcmp(
&haystack[haystack_len - needle_len],
needle,
needle_len * sizeof(wchar_t));
&haystack[haystack_len - needle_len],
needle,
needle_len * sizeof(wchar_t));
}
size_t str_format(char *buf, size_t nchars, const char *fmt, ...)
@@ -80,8 +80,7 @@ size_t wstr_format(wchar_t *buf, size_t nchars, const wchar_t *fmt, ...)
return result;
}
size_t wstr_vformat(wchar_t *buf, size_t nchars, const wchar_t *fmt,
va_list ap)
size_t wstr_vformat(wchar_t *buf, size_t nchars, const wchar_t *fmt, va_list ap)
{
int result;
@@ -166,7 +165,7 @@ void str_trim(char *str)
{
char *pos;
for (pos = str + strlen(str) - 1 ; pos > str ; pos--) {
for (pos = str + strlen(str) - 1; pos > str; pos--) {
if (!isspace(*pos)) {
return;
}
@@ -219,8 +218,8 @@ bool wstr_narrow(const wchar_t *src, char **dest)
*dest = xmalloc(nbytes);
if (WideCharToMultiByte(CP_ACP, 0, src, -1, *dest, nbytes, NULL, NULL)
!= nbytes) {
if (WideCharToMultiByte(CP_ACP, 0, src, -1, *dest, nbytes, NULL, NULL) !=
nbytes) {
goto conv_fail;
}
@@ -251,4 +250,3 @@ bool wstr_eq(const wchar_t *lhs, const wchar_t *rhs)
return wcscmp(lhs, rhs) == 0;
}
}
+3 -3
View File
@@ -2,8 +2,8 @@
#define UTIL_STR_H
#include <stdarg.h>
#include <stddef.h>
#include <stdbool.h>
#include <stddef.h>
#include <wchar.h>
void str_cat(char *dest, size_t dnchars, const char *src);
@@ -23,7 +23,7 @@ bool wstr_ends_with(const wchar_t *haystack, const wchar_t *needle);
bool wstr_eq(const wchar_t *lhs, const wchar_t *rhs);
size_t wstr_format(wchar_t *buf, size_t nchars, const wchar_t *fmt, ...);
bool wstr_narrow(const wchar_t *src, char **dest);
size_t wstr_vformat(wchar_t *buf, size_t nchars, const wchar_t *fmt,
va_list ap);
size_t
wstr_vformat(wchar_t *buf, size_t nchars, const wchar_t *fmt, va_list ap);
#endif
+10 -11
View File
@@ -1,5 +1,5 @@
#include <windows.h>
#include <process.h>
#include <windows.h>
#include <stddef.h>
#include <stdint.h>
@@ -31,8 +31,8 @@ static unsigned int STDCALL crt_thread_shim(void *outer_ctx)
return proc(inner_ctx);
}
int crt_thread_create(int (*proc)(void *), void *ctx, uint32_t stack_sz,
unsigned int priority)
int crt_thread_create(
int (*proc)(void *), void *ctx, uint32_t stack_sz, unsigned int priority)
{
struct shim_ctx sctx;
uintptr_t thread_id;
@@ -51,20 +51,20 @@ int crt_thread_create(int (*proc)(void *), void *ctx, uint32_t stack_sz,
void crt_thread_destroy(int thread_id)
{
CloseHandle((HANDLE) (uintptr_t) thread_id);
CloseHandle((HANDLE)(uintptr_t) thread_id);
}
void crt_thread_join(int thread_id, int *result)
{
WaitForSingleObject((HANDLE) (uintptr_t) thread_id, INFINITE);
WaitForSingleObject((HANDLE)(uintptr_t) thread_id, INFINITE);
if (result) {
GetExitCodeThread((HANDLE) (uintptr_t) thread_id, (DWORD *) result);
GetExitCodeThread((HANDLE)(uintptr_t) thread_id, (DWORD *) result);
}
}
void thread_api_init(thread_create_t create, thread_join_t join,
thread_destroy_t destroy)
void thread_api_init(
thread_create_t create, thread_join_t join, thread_destroy_t destroy)
{
if (create == NULL || join == NULL || destroy == NULL) {
abort();
@@ -75,8 +75,8 @@ void thread_api_init(thread_create_t create, thread_join_t join,
thread_impl_destroy = destroy;
}
int thread_create(int (*proc)(void *), void *ctx, uint32_t stack_sz,
unsigned int priority)
int thread_create(
int (*proc)(void *), void *ctx, uint32_t stack_sz, unsigned int priority)
{
return thread_impl_create(proc, ctx, stack_sz, priority);
}
@@ -90,4 +90,3 @@ void thread_destroy(int thread_id)
{
thread_impl_destroy(thread_id);
}
+6 -6
View File
@@ -5,15 +5,15 @@
#include "bemanitools/glue.h"
int crt_thread_create(int (*proc)(void *), void *ctx, uint32_t stack_sz,
unsigned int priority);
int crt_thread_create(
int (*proc)(void *), void *ctx, uint32_t stack_sz, unsigned int priority);
void crt_thread_join(int thread_id, int *result);
void crt_thread_destroy(int thread_id);
void thread_api_init(thread_create_t create, thread_join_t join,
thread_destroy_t destroy);
int thread_create(int (*proc)(void *), void *ctx, uint32_t stack_sz,
unsigned int priority);
void thread_api_init(
thread_create_t create, thread_join_t join, thread_destroy_t destroy);
int thread_create(
int (*proc)(void *), void *ctx, uint32_t stack_sz, unsigned int priority);
void thread_join(int thread_id, int *result);
void thread_destroy(int thread_id);
+1 -1
View File
@@ -22,7 +22,7 @@ uint64_t time_get_counter(void)
LARGE_INTEGER time;
log_assert(QueryPerformanceCounter(&time) == TRUE);
return (uint64_t) time.QuadPart;
}
+23 -12
View File
@@ -4,8 +4,12 @@
#include "util/str.h"
#include "util/winres.h"
void vrsprintf(char *dest, size_t nchars, HINSTANCE inst,
unsigned int fmt_resource, va_list ap)
void vrsprintf(
char *dest,
size_t nchars,
HINSTANCE inst,
unsigned int fmt_resource,
va_list ap)
{
char fmt_buffer[1024];
@@ -14,8 +18,8 @@ void vrsprintf(char *dest, size_t nchars, HINSTANCE inst,
dest[nchars - 1] = '\0';
}
void rsprintf(char *dest, size_t nchars, HINSTANCE inst,
unsigned int fmt_resource, ...)
void rsprintf(
char *dest, size_t nchars, HINSTANCE inst, unsigned int fmt_resource, ...)
{
va_list ap;
@@ -24,8 +28,12 @@ void rsprintf(char *dest, size_t nchars, HINSTANCE inst,
va_end(ap);
}
void vrswprintf(wchar_t *dest, size_t nchars, HINSTANCE inst,
unsigned int fmt_resource, va_list ap)
void vrswprintf(
wchar_t *dest,
size_t nchars,
HINSTANCE inst,
unsigned int fmt_resource,
va_list ap)
{
wchar_t fmt_buffer[1024];
@@ -34,8 +42,12 @@ void vrswprintf(wchar_t *dest, size_t nchars, HINSTANCE inst,
dest[nchars - 1] = L'\0';
}
void rswprintf(wchar_t *dest, size_t nchars, HINSTANCE inst,
unsigned int fmt_resource, ...)
void rswprintf(
wchar_t *dest,
size_t nchars,
HINSTANCE inst,
unsigned int fmt_resource,
...)
{
va_list ap;
@@ -44,8 +56,8 @@ void rswprintf(wchar_t *dest, size_t nchars, HINSTANCE inst,
va_end(ap);
}
void resource_open(struct resource *res, HINSTANCE inst, const char *type,
unsigned int ord)
void resource_open(
struct resource *res, HINSTANCE inst, const char *type, unsigned int ord)
{
HRSRC h1;
HGLOBAL h2;
@@ -67,7 +79,7 @@ size_t resource_fgets(struct resource *res, char *chars, size_t nchars)
c = '\0';
remain = res->nbytes - res->pos;
for (i = 0 ; i < remain ; i++) {
for (i = 0; i < remain; i++) {
if (c == '\n') {
break;
}
@@ -83,4 +95,3 @@ size_t resource_fgets(struct resource *res, char *chars, size_t nchars)
return i > 0;
}
+22 -10
View File
@@ -22,18 +22,30 @@ struct resource {
size_t pos;
};
void vrsprintf(char *dest, size_t nchars, HINSTANCE inst,
unsigned int fmt_resource, va_list ap);
void rsprintf(char *dest, size_t nchars, HINSTANCE inst,
unsigned int fmt_resource, ...);
void vrsprintf(
char *dest,
size_t nchars,
HINSTANCE inst,
unsigned int fmt_resource,
va_list ap);
void rsprintf(
char *dest, size_t nchars, HINSTANCE inst, unsigned int fmt_resource, ...);
void vrswprintf(wchar_t *dest, size_t nchars, HINSTANCE inst,
unsigned int fmt_resource, va_list ap);
void rswprintf(wchar_t *dest, size_t nchars, HINSTANCE inst,
unsigned int fmt_resource, ...);
void vrswprintf(
wchar_t *dest,
size_t nchars,
HINSTANCE inst,
unsigned int fmt_resource,
va_list ap);
void rswprintf(
wchar_t *dest,
size_t nchars,
HINSTANCE inst,
unsigned int fmt_resource,
...);
void resource_open(struct resource *res, HINSTANCE inst, const char *type,
unsigned int ord);
void resource_open(
struct resource *res, HINSTANCE inst, const char *type, unsigned int ord);
size_t resource_fgets(struct resource *res, char *chars, size_t nchars);
#endif