Use MEM2 and improve formatting

This commit is contained in:
wiidev 2020-09-14 18:23:54 +01:00
parent f5496be239
commit c16b3adea9
3 changed files with 127 additions and 125 deletions

View File

@ -31,8 +31,7 @@
#ifndef BASE64_H #ifndef BASE64_H
#define BASE64_H #define BASE64_H
#include <stdio.h> #include "memory/mem2.hpp"
#include <stdlib.h>
static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@ -81,7 +80,7 @@ char* base64( const void* binaryData, int len, int *flen )
int pad = ((modulusLen & 1) << 1) + ((modulusLen & 2) >> 1); // 2 gives 1 and 1 gives 2, but 0 gives 0. int pad = ((modulusLen & 1) << 1) + ((modulusLen & 2) >> 1); // 2 gives 1 and 1 gives 2, but 0 gives 0.
*flen = 4 * (len + pad) / 3; *flen = 4 * (len + pad) / 3;
res = (char*) malloc( *flen + 1 ); // and one for the null res = (char *)MEM2_alloc(*flen + 1); // and one for the null
if (!res) if (!res)
return 0; return 0;
@ -123,16 +122,19 @@ unsigned char* unbase64( const char* ascii, int len, int *flen )
int charNo; int charNo;
int pad = 0; int pad = 0;
if ((len <= 0) || (len % 4 != 0)) { // 2 accesses below would be OOB. if ((len <= 0) || (len % 4 != 0))
{ // 2 accesses below would be OOB.
// catch empty string or incorrect padding size, return NULL as result. // catch empty string or incorrect padding size, return NULL as result.
*flen = 0; *flen = 0;
return 0; return 0;
} }
if( safeAsciiPtr[ len-1 ]=='=' ) ++pad; if (safeAsciiPtr[len - 1] == '=')
if( safeAsciiPtr[ len-2 ]=='=' ) ++pad; ++pad;
if (safeAsciiPtr[len - 2] == '=')
++pad;
*flen = 3 * len / 4 - pad; *flen = 3 * len / 4 - pad;
bin = (unsigned char*)malloc( *flen ); bin = (unsigned char *)MEM2_alloc(*flen);
if (!bin) if (!bin)
return 0; return 0;

View File

@ -268,7 +268,7 @@ bool connect_proxy(HTTP_INFO *httpinfo, char *host, char *username, char *passwo
if (!(auth = base64(credentials, strlen(credentials), &len))) if (!(auth = base64(credentials, strlen(credentials), &len)))
return false; return false;
len = snprintf(request, sizeof(request), "CONNECT %s:%i HTTP/1.1\r\nProxy-Authorization: Basic %s\r\nUser-Agent: curl/7.55.1\r\n\r\n", host, httpinfo->use_https ? 443 : 80, auth); len = snprintf(request, sizeof(request), "CONNECT %s:%i HTTP/1.1\r\nProxy-Authorization: Basic %s\r\nUser-Agent: curl/7.55.1\r\n\r\n", host, httpinfo->use_https ? 443 : 80, auth);
free(auth); MEM2_free(auth);
} }
else else
len = snprintf(request, sizeof(request), "CONNECT %s:%i HTTP/1.1\r\nUser-Agent: curl/7.55.1\r\n\r\n", host, httpinfo->use_https ? 443 : 80); len = snprintf(request, sizeof(request), "CONNECT %s:%i HTTP/1.1\r\nUser-Agent: curl/7.55.1\r\n\r\n", host, httpinfo->use_https ? 443 : 80);

View File

@ -20,7 +20,7 @@ void getProxyInfo()
int fd = ISFS_Open("/shared2/sys/net/02/config.dat", ISFS_OPEN_READ); int fd = ISFS_Open("/shared2/sys/net/02/config.dat", ISFS_OPEN_READ);
if (fd >= 0) if (fd >= 0)
{ {
fstats stats ATTRIBUTE_ALIGN(32) = {}; fstats stats ATTRIBUTE_ALIGN(32);
if(ISFS_GetFileStats(fd, &stats) >= 0) if(ISFS_GetFileStats(fd, &stats) >= 0)
{ {
if (stats.file_length > 0) if (stats.file_length > 0)