Merge pull request #278 from PhenomRetroShare/Fix_bencode.c_CompilationForC

Fix compilation of bencode.c for C compilator.
This commit is contained in:
Cyril Soler 2016-02-14 13:35:32 -05:00
commit 44882b941c

View File

@ -20,7 +20,6 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <iostream>
#include <stdlib.h> /* malloc() realloc() free() strtoll() */ #include <stdlib.h> /* malloc() realloc() free() strtoll() */
#include <string.h> /* memset() */ #include <string.h> /* memset() */
#include "util/bdstring.h" #include "util/bdstring.h"
@ -112,13 +111,15 @@ static char *_be_decode_str(const char **data, long long *data_len)
if (**data == ':') { if (**data == ':') {
char *_ret = (char *) malloc(sizeof(sllen) + len + 1); char *_ret = (char *) malloc(sizeof(sllen) + len + 1);
if(_ret == NULL) if(_ret == NULL)
{ {
std::cerr << "(EE) " << __PRETTY_FUNCTION__ << ": ERROR. cannot allocate memory for " << len+1+sizeof(sllen) << " bytes." << std::endl; fprintf(stderr, "(EE) bencode::_be_decode_str(): "
return NULL; "ERROR. cannot allocate memory for %lu bytes.\n"
} , len+1+sizeof(sllen) );
return ret;
}
memcpy(_ret, &sllen, sizeof(sllen)); memcpy(_ret, &sllen, sizeof(sllen));
ret = _ret + sizeof(sllen); ret = _ret + sizeof(sllen);
memcpy(ret, *data + 1, len); memcpy(ret, *data + 1, len);
@ -504,17 +505,19 @@ be_node *be_create_str(const char *str)
{ {
/* must */ /* must */
be_node *n = be_alloc(BE_STR); be_node *n = NULL;
int len = strlen(str); int len = strlen(str);
long long int sllen = len; long long int sllen = len;
char *_ret = (char *) malloc(sizeof(sllen) + len + 1); char *_ret = (char *) malloc(sizeof(sllen) + len + 1);
if(_ret == NULL)
if(_ret == NULL) {
{ fprintf(stderr, "(EE) bencode::be_create_str(): "
std::cerr << "(EE) " << __PRETTY_FUNCTION__ << ": ERROR. cannot allocate memory for " << len+1+sizeof(sllen) << " bytes." << std::endl; "ERROR. cannot allocate memory for %lu bytes.\n"
return NULL; , len+1+sizeof(sllen) );
} return n;
}
char *ret = NULL; char *ret = NULL;
n = be_alloc(BE_STR);
memcpy(_ret, &sllen, sizeof(sllen)); memcpy(_ret, &sllen, sizeof(sllen));
ret = _ret + sizeof(sllen); ret = _ret + sizeof(sllen);
@ -530,16 +533,18 @@ be_node *be_create_str_wlen(const char *str, int len) /* not including \0 */
{ {
/* must */ /* must */
be_node *n = be_alloc(BE_STR); be_node *n = NULL;
long long int sllen = len; long long int sllen = len;
char *_ret = (char *) malloc(sizeof(sllen) + len + 1); char *_ret = (char *) malloc(sizeof(sllen) + len + 1);
if(_ret == NULL)
if(_ret == NULL) {
{ fprintf(stderr, "(EE) bencode::be_create_str_wlen(): "
std::cerr << "(EE) " << __PRETTY_FUNCTION__ << ": ERROR. cannot allocate memory for " << len+1+sizeof(sllen) << " bytes." << std::endl; "ERROR. cannot allocate memory for %lu bytes.\n"
return NULL; , len+1+sizeof(sllen) );
} return n;
}
char *ret = NULL; char *ret = NULL;
n = be_alloc(BE_STR);
memcpy(_ret, &sllen, sizeof(sllen)); memcpy(_ret, &sllen, sizeof(sllen));
ret = _ret + sizeof(sllen); ret = _ret + sizeof(sllen);
@ -570,7 +575,8 @@ int be_add_keypair(be_node *dict, const char *str, be_node *node)
} }
// get to end of dict. // get to end of dict.
for(i = 0; dict->val.d[i].val; i++); for(i = 0; dict->val.d[i].val; i++)
;//Silent empty body for loop for clang
//fprintf(stderr, "be_add_keypair() i = %d\n",i); //fprintf(stderr, "be_add_keypair() i = %d\n",i);
@ -581,12 +587,13 @@ int be_add_keypair(be_node *dict, const char *str, be_node *node)
int len = strlen(str); int len = strlen(str);
long long int sllen = len; long long int sllen = len;
char *_ret = (char *) malloc(sizeof(sllen) + len + 1); char *_ret = (char *) malloc(sizeof(sllen) + len + 1);
if(_ret == NULL)
if(_ret == NULL) {
{ fprintf(stderr, "(EE) bencode::be_create_str_wlen(): "
std::cerr << "(EE) " << __PRETTY_FUNCTION__ << ": ERROR. cannot allocate memory for " << len+1+sizeof(sllen) << " bytes." << std::endl; "ERROR. cannot allocate memory for %lu bytes.\n"
return 0; , len+1+sizeof(sllen) );
} return 0;
}
char *ret = NULL; char *ret = NULL;
//fprintf(stderr, "be_add_keypair() key len = %d\n",len); //fprintf(stderr, "be_add_keypair() key len = %d\n",len);
@ -616,7 +623,8 @@ int be_add_list(be_node *list, be_node *node)
} }
// get to end of dict. // get to end of dict.
for(i = 0; list->val.l[i]; i++); for(i = 0; list->val.l[i]; i++)
;//Silent empty body for loop for clang
/* realloc space */ /* realloc space */
list->val.l = (be_node **) realloc(list->val.l, (i + 2) * sizeof(*list->val.l)); list->val.l = (be_node **) realloc(list->val.l, (i + 2) * sizeof(*list->val.l));