mirror of
https://github.com/Fledge68/WiiFlow_Lite.git
synced 2024-11-01 00:55:06 +01:00
Use MEM2 and improve formatting
This commit is contained in:
parent
f5496be239
commit
c16b3adea9
@ -31,10 +31,9 @@
|
||||
#ifndef BASE64_H
|
||||
#define BASE64_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "memory/mem2.hpp"
|
||||
|
||||
static const char* b64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
static const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
// maps A=>0,B=>1..
|
||||
static const unsigned char unb64[]={
|
||||
@ -69,100 +68,103 @@ static const unsigned char unb64[]={
|
||||
// Converts binary data of length=len to base64 characters.
|
||||
// Length of the resultant string is stored in flen
|
||||
// (you must pass pointer flen).
|
||||
char* base64( const void* binaryData, int len, int *flen )
|
||||
char *base64(const void *binaryData, int len, int *flen)
|
||||
{
|
||||
const unsigned char* bin = (const unsigned char*) binaryData;
|
||||
char* res;
|
||||
const unsigned char *bin = (const unsigned char *)binaryData;
|
||||
char *res;
|
||||
|
||||
int rc = 0; // result counter
|
||||
int byteNo; // I need this after the loop
|
||||
|
||||
int modulusLen = len % 3;
|
||||
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;
|
||||
res = (char*) malloc( *flen + 1 ); // and one for the null
|
||||
if( !res )
|
||||
*flen = 4 * (len + pad) / 3;
|
||||
res = (char *)MEM2_alloc(*flen + 1); // and one for the null
|
||||
if (!res)
|
||||
return 0;
|
||||
|
||||
for( byteNo = 0; byteNo <= len-3; byteNo+=3 )
|
||||
for (byteNo = 0; byteNo <= len - 3; byteNo += 3)
|
||||
{
|
||||
unsigned char BYTE0=bin[byteNo];
|
||||
unsigned char BYTE1=bin[byteNo+1];
|
||||
unsigned char BYTE2=bin[byteNo+2];
|
||||
res[rc++] = b64[ BYTE0 >> 2 ];
|
||||
res[rc++] = b64[ ((0x3&BYTE0)<<4) + (BYTE1 >> 4) ];
|
||||
res[rc++] = b64[ ((0x0f&BYTE1)<<2) + (BYTE2>>6) ];
|
||||
res[rc++] = b64[ 0x3f&BYTE2 ];
|
||||
unsigned char BYTE0 = bin[byteNo];
|
||||
unsigned char BYTE1 = bin[byteNo + 1];
|
||||
unsigned char BYTE2 = bin[byteNo + 2];
|
||||
res[rc++] = b64[BYTE0 >> 2];
|
||||
res[rc++] = b64[((0x3 & BYTE0) << 4) + (BYTE1 >> 4)];
|
||||
res[rc++] = b64[((0x0f & BYTE1) << 2) + (BYTE2 >> 6)];
|
||||
res[rc++] = b64[0x3f & BYTE2];
|
||||
}
|
||||
|
||||
if( pad==2 )
|
||||
if (pad == 2)
|
||||
{
|
||||
res[rc++] = b64[ bin[byteNo] >> 2 ];
|
||||
res[rc++] = b64[ (0x3&bin[byteNo])<<4 ];
|
||||
res[rc++] = b64[bin[byteNo] >> 2];
|
||||
res[rc++] = b64[(0x3 & bin[byteNo]) << 4];
|
||||
res[rc++] = '=';
|
||||
res[rc++] = '=';
|
||||
}
|
||||
else if( pad==1 )
|
||||
else if (pad == 1)
|
||||
{
|
||||
res[rc++] = b64[ bin[byteNo] >> 2 ];
|
||||
res[rc++] = b64[ ((0x3&bin[byteNo])<<4) + (bin[byteNo+1] >> 4) ];
|
||||
res[rc++] = b64[ (0x0f&bin[byteNo+1])<<2 ];
|
||||
res[rc++] = b64[bin[byteNo] >> 2];
|
||||
res[rc++] = b64[((0x3 & bin[byteNo]) << 4) + (bin[byteNo + 1] >> 4)];
|
||||
res[rc++] = b64[(0x0f & bin[byteNo + 1]) << 2];
|
||||
res[rc++] = '=';
|
||||
}
|
||||
|
||||
res[rc]=0; // NULL TERMINATOR!;)
|
||||
res[rc] = 0; // NULL TERMINATOR!;)
|
||||
return res;
|
||||
}
|
||||
|
||||
unsigned char* unbase64( const char* ascii, int len, int *flen )
|
||||
unsigned char *unbase64(const char *ascii, int len, int *flen)
|
||||
{
|
||||
const unsigned char *safeAsciiPtr = (const unsigned char*)ascii;
|
||||
const unsigned char *safeAsciiPtr = (const unsigned char *)ascii;
|
||||
unsigned char *bin;
|
||||
int cb=0;
|
||||
int cb = 0;
|
||||
int charNo;
|
||||
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.
|
||||
*flen=0;
|
||||
*flen = 0;
|
||||
return 0;
|
||||
}
|
||||
if( safeAsciiPtr[ len-1 ]=='=' ) ++pad;
|
||||
if( safeAsciiPtr[ len-2 ]=='=' ) ++pad;
|
||||
if (safeAsciiPtr[len - 1] == '=')
|
||||
++pad;
|
||||
if (safeAsciiPtr[len - 2] == '=')
|
||||
++pad;
|
||||
|
||||
*flen = 3*len/4 - pad;
|
||||
bin = (unsigned char*)malloc( *flen );
|
||||
if( !bin )
|
||||
*flen = 3 * len / 4 - pad;
|
||||
bin = (unsigned char *)MEM2_alloc(*flen);
|
||||
if (!bin)
|
||||
return 0;
|
||||
|
||||
for( charNo=0; charNo <= len - 4 - pad; charNo+=4 )
|
||||
for (charNo = 0; charNo <= len - 4 - pad; charNo += 4)
|
||||
{
|
||||
int A=unb64[safeAsciiPtr[charNo]];
|
||||
int B=unb64[safeAsciiPtr[charNo+1]];
|
||||
int C=unb64[safeAsciiPtr[charNo+2]];
|
||||
int D=unb64[safeAsciiPtr[charNo+3]];
|
||||
int A = unb64[safeAsciiPtr[charNo]];
|
||||
int B = unb64[safeAsciiPtr[charNo + 1]];
|
||||
int C = unb64[safeAsciiPtr[charNo + 2]];
|
||||
int D = unb64[safeAsciiPtr[charNo + 3]];
|
||||
|
||||
bin[cb++] = (A<<2) | (B>>4);
|
||||
bin[cb++] = (B<<4) | (C>>2);
|
||||
bin[cb++] = (C<<6) | (D);
|
||||
bin[cb++] = (A << 2) | (B >> 4);
|
||||
bin[cb++] = (B << 4) | (C >> 2);
|
||||
bin[cb++] = (C << 6) | (D);
|
||||
}
|
||||
|
||||
if( pad==1 )
|
||||
if (pad == 1)
|
||||
{
|
||||
int A=unb64[safeAsciiPtr[charNo]];
|
||||
int B=unb64[safeAsciiPtr[charNo+1]];
|
||||
int C=unb64[safeAsciiPtr[charNo+2]];
|
||||
int A = unb64[safeAsciiPtr[charNo]];
|
||||
int B = unb64[safeAsciiPtr[charNo + 1]];
|
||||
int C = unb64[safeAsciiPtr[charNo + 2]];
|
||||
|
||||
bin[cb++] = (A<<2) | (B>>4);
|
||||
bin[cb++] = (B<<4) | (C>>2);
|
||||
bin[cb++] = (A << 2) | (B >> 4);
|
||||
bin[cb++] = (B << 4) | (C >> 2);
|
||||
}
|
||||
else if( pad==2 )
|
||||
else if (pad == 2)
|
||||
{
|
||||
int A=unb64[safeAsciiPtr[charNo]];
|
||||
int B=unb64[safeAsciiPtr[charNo+1]];
|
||||
int A = unb64[safeAsciiPtr[charNo]];
|
||||
int B = unb64[safeAsciiPtr[charNo + 1]];
|
||||
|
||||
bin[cb++] = (A<<2) | (B>>4);
|
||||
bin[cb++] = (A << 2) | (B >> 4);
|
||||
}
|
||||
|
||||
return bin;
|
||||
|
@ -268,7 +268,7 @@ bool connect_proxy(HTTP_INFO *httpinfo, char *host, char *username, char *passwo
|
||||
if (!(auth = base64(credentials, strlen(credentials), &len)))
|
||||
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);
|
||||
free(auth);
|
||||
MEM2_free(auth);
|
||||
}
|
||||
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);
|
||||
|
@ -20,15 +20,15 @@ void getProxyInfo()
|
||||
int fd = ISFS_Open("/shared2/sys/net/02/config.dat", ISFS_OPEN_READ);
|
||||
if (fd >= 0)
|
||||
{
|
||||
fstats stats ATTRIBUTE_ALIGN(32) = {};
|
||||
fstats stats ATTRIBUTE_ALIGN(32);
|
||||
if(ISFS_GetFileStats(fd, &stats) >= 0)
|
||||
{
|
||||
if (stats.file_length > 0)
|
||||
{
|
||||
buffer = (char*)MEM2_alloc(ALIGN32(stats.file_length));
|
||||
buffer = (char *)MEM2_alloc(ALIGN32(stats.file_length));
|
||||
if (buffer)
|
||||
{
|
||||
if(ISFS_Read(fd, buffer, stats.file_length) == 7004)
|
||||
if (ISFS_Read(fd, buffer, stats.file_length) == 7004)
|
||||
{
|
||||
proxy_enabled = buffer[44];
|
||||
strncpy(proxy_address, buffer + 48, sizeof(proxy_address) - 1);
|
||||
@ -44,7 +44,7 @@ void getProxyInfo()
|
||||
}
|
||||
}
|
||||
|
||||
char* getProxyAddress()
|
||||
char *getProxyAddress()
|
||||
{
|
||||
if (mainMenu.proxyUseSystem)
|
||||
return proxy_enabled ? proxy_address : NULL;
|
||||
@ -58,14 +58,14 @@ u16 getProxyPort()
|
||||
return mainMenu.proxyPort;
|
||||
}
|
||||
|
||||
char* getProxyUsername()
|
||||
char *getProxyUsername()
|
||||
{
|
||||
if (mainMenu.proxyUseSystem)
|
||||
return proxy_enabled ? proxy_username : NULL;
|
||||
return (strlen(mainMenu.proxyUsername) > 0) ? mainMenu.proxyUsername : NULL;
|
||||
}
|
||||
|
||||
char* getProxyPassword()
|
||||
char *getProxyPassword()
|
||||
{
|
||||
if (mainMenu.proxyUseSystem)
|
||||
return proxy_enabled ? proxy_password : NULL;
|
||||
|
Loading…
Reference in New Issue
Block a user