Skip to content

Commit

Permalink
Add support for ntr generated bmp.
Browse files Browse the repository at this point in the history
  • Loading branch information
hax0kartik authored Apr 13, 2017
1 parent 4ec65bb commit e95c2d4
Showing 1 changed file with 40 additions and 87 deletions.
127 changes: 40 additions & 87 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include <3ds.h>
#include <stdarg.h>
#include "common.h"
#include "error.h"
unsigned char *buffer;
char *lobi;
char links[1024];

void error(int error,const char *format, ...)
{
va_list aptr;
Expand All @@ -22,63 +22,17 @@ void error(int error,const char *format, ...)
errorText(&err, buf);
errorDisp(&err);
}
void wlink(const char *a,const char *b){
FILE *pFile;
char buffer[256];
char *k=strrchr(a,'/');
sprintf(buffer,"\n%s : %s",a,k);
pFile=fopen("upload_links.txt", "a+");
fprintf(pFile, "%s", buffer);
fclose(pFile);
}

static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'};
static char *decoding_table = NULL;
static int mod_table[] = {0, 2, 1};

void build_decoding_table() {

decoding_table = malloc(256);

for (int i = 0; i < 64; i++)
decoding_table[(unsigned char) encoding_table[i]] = i;
void wlink(const char *a,const char *b){
FILE *pFile;
char buffer[256];
char *k=strrchr(a,'/');
sprintf(buffer,"\n%s : %s",a,k);
pFile=fopen("upload_links.txt", "a+");
fprintf(pFile, "%s", buffer);
fclose(pFile);
}

char *base64_encode(const unsigned char *data,
size_t input_length,
size_t *output_length) {

*output_length = 4 * ((input_length + 2) / 3);

char *encoded_data = malloc(*output_length);
if (encoded_data == NULL) return NULL;

for (int i = 0, j = 0; i < input_length;) {

uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0;
uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0;
uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0;

uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;

encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
}

for (int i = 0; i < mod_table[input_length % 3]; i++)
encoded_data[*output_length - 1 - i] = '=';

return encoded_data;
}
char *text_ex(char *s,char *PATTERN1,char *PATTERN2)
{
char *target = NULL;
Expand All @@ -89,44 +43,42 @@ char *text_ex(char *s,char *PATTERN1,char *PATTERN2)
start += strlen( PATTERN1 );
if ( end = strstr( start, PATTERN2 ))
{
target = ( char * )malloc( end - start + 1 );
memcpy( target, start, end - start );
target[end - start] = '\0';
}

target = ( char * )malloc( end - start + 1 );
memcpy( target, start, end - start );
target[end - start] = '\0';
}
}
return target;

}
char *ReadFile(const char *name)
{ size_t fileLen;
FILE *file;
file = fopen(name , "rb");
if (!file)
char *ReadFile(const char *name, u32 *size)
{
size_t fileLen;
FILE *file;
file = fopen(name , "rb");
if (!file)
{
error(12,"Unable to open file %s", name);
return 'e';
}
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);
buffer=(char *)malloc(fileLen+1);
if (!buffer)
{
error(13,"%s","Memory error!");
fclose(file);
return 'e';
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);
buffer=(char *)malloc(fileLen+1);
if (!buffer)
{
error(13,"%s","Memory error!");
fclose(file);
return 'e';
}
fread(buffer, fileLen, 1, file);
size_t l =0;
lobi=base64_encode(buffer,fileLen,&l);
fclose(file);
return lobi;
fread(buffer, fileLen, 1, file);
*size = fileLen;
fclose(file);
return buffer;
}

Result http_upload(const char *url,char *buff)
Result http_upload(const char *url, char *buff, u32 buff_size)
{
Result ret = 0;
Result ret = 0;
u32 readsize=0, size=0;
u8 *buf, *lastbuf;
httpcContext context;
Expand Down Expand Up @@ -154,9 +106,9 @@ Result http_upload(const char *url,char *buff)
return 1;
}

ret= httpcAddPostDataAscii (&context, (char*)"image",buff);
ret= httpcAddPostDataBinary(&context, (char*)"image", buff ,buff_size);
printf("\x1b[32;1mRet 0x%08X\n",(int)ret);
ret= httpcAddPostDataAscii (&context, (char*)"type","base64");
ret= httpcAddPostDataAscii (&context, (char*)"type","file");
printf("\x1b[32;1mRet 0x%08X\n",(int)ret);
ret = httpcBeginRequest(&context);
if (ret != 0){
Expand Down Expand Up @@ -235,6 +187,7 @@ char *tl(SwkbdState swkbd ,char *texgen)
int main()
{
Result ret=0;
u32 size = 0;
osSetSpeedupEnable(true);
gfxInitDefault();
httpcInit(0x500000); // Buffer size when POST/PUT.
Expand Down Expand Up @@ -266,11 +219,11 @@ int main()
printf("Uploading :%s\n",current_file);
if(current_file[0]!=0)
{
char *a=ReadFile(current_file);
ret=http_upload("https://api.imgur.com/3/image.xml",a);
char *a=ReadFile(current_file, &size);
ret=http_upload("https://api.imgur.com/3/image.xml", a, size);
if(ret==0)
{
wlink(links,current_file);
wlink(links, current_file);
for(int i=0;i<=511;i++)current_file[i]='\0';
printf("Press B to open the explorer.\n");
}
Expand Down

0 comments on commit e95c2d4

Please # to comment.