Skip to content

Commit

Permalink
Merge pull request #281 from Wolf3s/C89
Browse files Browse the repository at this point in the history
Add msvc and gnu89 support
  • Loading branch information
sahlberg authored Nov 20, 2023
2 parents cd0d6cf + 3644a5a commit ebb1633
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 31 deletions.
12 changes: 6 additions & 6 deletions lib/aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,12 +443,12 @@ static void BlockCopy(uint8_t* output, uint8_t* input)

void AES128_ECB_encrypt(uint8_t* input, const uint8_t* key, uint8_t* output)
{
// Copy input to output, and work in-memory on output
BlockCopy(output, input);

// The array that stores the round keys.
uint8_t roundKey[176];

// Copy input to output, and work in-memory on output
BlockCopy(output, input);

KeyExpansion(key, roundKey);

// The next function call encrypts the PlainText with the Key using AES algorithm.
Expand All @@ -457,12 +457,12 @@ void AES128_ECB_encrypt(uint8_t* input, const uint8_t* key, uint8_t* output)

void AES128_ECB_decrypt(uint8_t* input, const uint8_t* key, uint8_t *output)
{
// Copy input to output, and work in-memory on output
BlockCopy(output, input);

// The array that stores the round keys.
uint8_t roundKey[176];

// Copy input to output, and work in-memory on output
BlockCopy(output, input);

// The KeyExpansion routine must be called before encryption.
KeyExpansion(key, roundKey);

Expand Down
30 changes: 20 additions & 10 deletions lib/compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@ ssize_t writev(int fd, const struct iovec *vector, int count)
{
/* Find the total number of bytes to be written. */
size_t bytes = 0;
for (int i = 0; i < count; ++i) {
int i;
char *buffer;
size_t to_copy;
char *bp;

for (i = 0; i < count; ++i) {
/* Check for ssize_t overflow. */
if (((ssize_t)-1) - bytes < vector[i].iov_len) {
errno = EINVAL;
Expand All @@ -182,16 +187,16 @@ ssize_t writev(int fd, const struct iovec *vector, int count)
bytes += vector[i].iov_len;
}

char *buffer = (char *)malloc(bytes);
buffer = (char *)malloc(bytes);
if (buffer == NULL)
/* XXX I don't know whether it is acceptable to try writing
the data in chunks. Probably not so we just fail here. */
return -1;

/* Copy the data into BUFFER. */
size_t to_copy = bytes;
char *bp = buffer;
for (int i = 0; i < count; ++i) {
to_copy = bytes;
bp = buffer;
for (i = 0; i < count; ++i) {
size_t copy = (vector[i].iov_len < to_copy) ? vector[i].iov_len : to_copy;

memcpy((void *)bp, (void *)vector[i].iov_base, copy);
Expand All @@ -214,7 +219,12 @@ ssize_t readv (int fd, const struct iovec *vector, int count)
{
/* Find the total number of bytes to be read. */
size_t bytes = 0;
for (int i = 0; i < count; ++i)
int i;
char *buffer;
ssize_t bytes_read;
char *bp;

for (i = 0; i < count; ++i)
{
/* Check for ssize_t overflow. */
if (((ssize_t)-1) - bytes < vector[i].iov_len) {
Expand All @@ -224,21 +234,21 @@ ssize_t readv (int fd, const struct iovec *vector, int count)
bytes += vector[i].iov_len;
}

char *buffer = (char *)malloc(bytes);
buffer = (char *)malloc(bytes);
if (buffer == NULL)
return -1;

/* Read the data. */
ssize_t bytes_read = read(fd, buffer, bytes);
bytes_read = read(fd, buffer, bytes);
if (bytes_read < 0) {
free(buffer);
return -1;
}

/* Copy the data from BUFFER into the memory specified by VECTOR. */
bytes = bytes_read;
char *bp = buffer;
for (int i = 0; i < count; ++i) {
bp = buffer;
for (i = 0; i < count; ++i) {
size_t copy = (vector[i].iov_len < bytes) ? vector[i].iov_len : bytes;

memcpy((void *)vector[i].iov_base, (void *)bp, copy);
Expand Down
6 changes: 4 additions & 2 deletions lib/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ struct smb2_url *smb2_parse_url(struct smb2_context *smb2, const char *url)
struct smb2_url *u;
char *ptr, *tmp, str[MAX_URL_SIZE];
char *args;
char *shared_folder;
int len_shared_folder;

if (strncmp(url, "smb://", 6)) {
smb2_set_error(smb2, "URL does not start with 'smb://'");
Expand Down Expand Up @@ -210,12 +212,12 @@ struct smb2_url *smb2_parse_url(struct smb2_context *smb2, const char *url)

ptr = str;

char *shared_folder = strchr(ptr, '/');
shared_folder = strchr(ptr, '/');
if (!shared_folder) {
smb2_set_error(smb2, "Wrong URL format");
return NULL;
}
int len_shared_folder = strlen(shared_folder);
len_shared_folder = strlen(shared_folder);

/* domain */
if ((tmp = strchr(ptr, ';')) != NULL && strlen(tmp) > len_shared_folder) {
Expand Down
3 changes: 2 additions & 1 deletion lib/smb2-cmd-set-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ smb2_encode_set_info_request(struct smb2_context *smb2,
struct smb2_iovec *iov;
struct smb2_file_end_of_file_info *eofi;
struct smb2_file_rename_info *rni;
struct utf16 *name;

len = SMB2_SET_INFO_REQUEST_SIZE & 0xfffffffe;
buf = calloc(len, sizeof(uint8_t));
Expand Down Expand Up @@ -112,7 +113,7 @@ smb2_encode_set_info_request(struct smb2_context *smb2,
case SMB2_FILE_RENAME_INFORMATION:
rni = req->input_data;

struct utf16 *name = utf8_to_utf16((char *)(rni->file_name));
name = utf8_to_utf16((char *)(rni->file_name));
if (name == NULL) {
smb2_set_error(smb2, "Could not convert name into UTF-16");
return -1;
Expand Down
9 changes: 5 additions & 4 deletions lib/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,11 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o
t_socket fd;
socklen_t socksize;
struct sockaddr_storage ss;

#if 0 == CONFIGURE_OPTION_TCP_LINGER
int const yes = 1;
struct LingerStruct { int l_onoff; /* linger active */ int l_linger; /* how many seconds to linger for */ };
struct LingerStruct const lin = { .l_onoff = 1, .l_linger = 0 }; /* if l_linger is zero, sends RST after FIN */
#endif
memset(&ss, 0, sizeof(ss));
switch (ai->ai_family) {
case AF_INET:
Expand Down Expand Up @@ -894,10 +898,7 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o
set_tcp_sockopt(fd, TCP_NODELAY, 1);

#if 0 == CONFIGURE_OPTION_TCP_LINGER
int const yes = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
struct LingerStruct { int l_onoff; /* linger active */ int l_linger; /* how many seconds to linger for */ };
struct LingerStruct const lin = { .l_onoff = 1, .l_linger = 0 }; /* if l_linger is zero, sends RST after FIN */
setsockopt(fd, SOL_SOCKET, SO_LINGER, &lin, sizeof lin);
#endif

Expand Down
17 changes: 9 additions & 8 deletions lib/unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ validate_utf8_cp(const char **utf8, uint16_t *ret)
{
int c = *(*utf8)++;
int l, l_tmp;
l = l_tmp = l1(c);
uint32_t cp;

l = l_tmp = l1(c);

switch (l) {
case 0:
/* 7-bit ascii is always ok */
Expand Down Expand Up @@ -208,12 +208,12 @@ utf16_size(const uint16_t *utf16, int utf16_len)
} else if (code < 0xd800 || code - 0xe000 < 0x2000) {
length += 3;
} else if (code < 0xdc00) { /* Surrogate pair */

uint32_t trail;
if (utf16 == utf16_end) { /* It's possible the stream ends with a leading code unit, which is an error */
return length + 3; /* Replacement char */
}

uint32_t trail = le16toh(*utf16);
trail = le16toh(*utf16);
if (trail - 0xdc00 < 0x400) { /* Check that 0xdc00 <= trail < 0xe000 */
code = 0x10000 + ((code & 0x3ff) << 10) + (trail & 0x3ff);
if (code < 0x10000) {
Expand Down Expand Up @@ -241,7 +241,8 @@ utf16_to_utf8(const uint16_t *utf16, int utf16_len)
{
int utf8_len = 1;
char *str, *tmp;

const uint16_t *utf16_end;

/* How many bytes do we need for utf8 ? */
utf8_len += utf16_size(utf16, utf16_len);
str = tmp = (char*)malloc(utf8_len);
Expand All @@ -250,7 +251,7 @@ utf16_to_utf8(const uint16_t *utf16, int utf16_len)
}
str[utf8_len - 1] = 0;

const uint16_t *utf16_end = utf16 + utf16_len;
utf16_end = utf16 + utf16_len;
while (utf16 < utf16_end) {
uint32_t code = le16toh(*utf16++);

Expand All @@ -264,13 +265,13 @@ utf16_to_utf8(const uint16_t *utf16, int utf16_len)
*tmp++ = 0x80 | ((code >> 6) & 0x3f);
*tmp++ = 0x80 | ((code ) & 0x3f);
} else if (code < 0xdc00) { /* Surrogate pair */

uint32_t trail;
if (utf16 == utf16_end) { /* It's possible the stream ends with a leading code unit, which is an error */
*tmp++ = 0xef; *tmp++ = 0xbf; *tmp++ = 0xbd; /* Replacement char */
return str;
}

uint32_t trail = le16toh(*utf16);
trail = le16toh(*utf16);
if (trail - 0xdc00 < 0x400) { /* Check that 0xdc00 <= trail < 0xe000 */
code = 0x10000 + ((code & 0x3ff) << 10) + (trail & 0x3ff);
if (code < 0x10000) {
Expand Down

0 comments on commit ebb1633

Please sign in to comment.