forked from filiphsandstrom/DownloadMii-3DS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownload.cpp
56 lines (48 loc) · 1.03 KB
/
download.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <3ds.h>
#include <netdb.h>
#include "download.h"
using namespace std;
Result networkInit(){
httpcInit();
return 0;
}
char* downloadFile(char* url){
Result r;
httpcContext c;
u8 *b; //buffer
u32 statuscode=0;
u32 contentsize=0;
char* file;
r = httpcOpenContext(&c, url, 0);
if(r != 0){
return "error: httpcOpenContext";
}
r = httpcBeginRequest(&c);
if(r != 0){
return "error: httpcBeginRequest";
}
r = httpcGetResponseStatusCode(&c, &statuscode, 0);
if((r != 0) || statuscode != 200){
return "error: httpcGetResponseStatusCode";
}
r = httpcGetDownloadSizeState(&c, NULL, &contentsize);
if(r != 0){
return "error: httpcGetDownloadSizeState";
}
b = (u8*)malloc(contentsize);
if(b==NULL)return "error: (u8*)malloc(contentsize)";
memset(b, 0, contentsize);
r = httpcDownloadData(&c, b, contentsize, NULL);
if(r != 0)
{
free(b);
return "error: httpcDownloadData";
}
file = (char*)b;
httpcCloseContext(&c);
return file;
}