-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathuri.c
More file actions
413 lines (333 loc) · 7.44 KB
/
Copy pathuri.c
File metadata and controls
413 lines (333 loc) · 7.44 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* Copyright (C) 2026 John Törnblom
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
/**
*
**/
typedef struct http_ctx {
int libnetMemId;
int libsslCtxId;
int libhttpCtxId;
int tmplId;
int connId;
int reqId;
} http_ctx_t;
int sceNetInit();
int sceNetPoolCreate(const char*, int, int);
int sceNetPoolDestroy(int);
int sceSslInit(size_t);
int sceSslTerm(int);
int sceHttpInit(int, int, size_t);
int sceHttpTerm(int);
int sceHttpCreateTemplate(int, const char*, int, int);
int sceHttpsSetSslCallback(int, void*, void*);
int sceHttpSetResponseHeaderMaxSize(int, size_t);
int sceHttpDeleteTemplate(int);
int sceHttpCreateConnectionWithURL(int, const char*, int);
int sceHttpDeleteConnection(int);
int sceHttpCreateRequestWithURL(int, int, const char*, uint64_t);
int sceHttpSendRequest(int, const void*, size_t);
int sceHttpGetResponseContentLength(int, int*, uint64_t*);
int sceHttpGetStatusCode(int, int*);
int sceHttpReadData(int, void *, size_t);
int sceHttpDeleteRequest(int);
/**
*
**/
static int
http_ssl_cb(void) {
return 0;
}
/**
*
**/
static int
http_init(http_ctx_t* ctx, const char* agent, const char* url) {
int err;
ctx->libnetMemId = -1;
ctx->libsslCtxId = -1;
ctx->libhttpCtxId = -1;
ctx->tmplId = -1;
ctx->reqId = -1;
if((err=sceNetInit()) < 0) {
return err;
}
if((ctx->libnetMemId=sceNetPoolCreate("http_get", 16*1024, 0)) < 0) {
return ctx->libnetMemId;
}
if((ctx->libsslCtxId=sceSslInit(128*1024)) < 0) {
return ctx->libsslCtxId;
}
if((ctx->libhttpCtxId=sceHttpInit(ctx->libnetMemId, ctx->libsslCtxId,
32*1024)) < 0) {
return ctx->libhttpCtxId;
}
if((ctx->tmplId=sceHttpCreateTemplate(ctx->libhttpCtxId, agent, 2, 1)) < 0) {
return ctx->tmplId;
}
if((err=sceHttpSetResponseHeaderMaxSize(ctx->tmplId, 0x2000)) < 0) {
return err;
}
if((err=sceHttpsSetSslCallback(ctx->tmplId, http_ssl_cb, 0))) {
return err;
}
if((ctx->connId=sceHttpCreateConnectionWithURL(ctx->tmplId, url, 0)) < 0) {
return ctx->connId;
}
if((ctx->reqId=sceHttpCreateRequestWithURL(ctx->connId, 0, url, 0)) < 0) {
return ctx->reqId;
}
return 0;
}
/**
*
**/
static int
http_request(http_ctx_t* ctx, uint8_t** data, size_t* len) {
int status = -1;
int err;
int n;
if((err=sceHttpSendRequest(ctx->reqId, 0, 0))) {
return err;
}
if((err=sceHttpGetStatusCode(ctx->reqId, &status))) {
return err;
}
if((err=sceHttpGetResponseContentLength(ctx->reqId, &err, len))) {
return err;
}
if(!len || status != 200) {
// TODO: support resources without a content length
return status;
}
if(!(*data=malloc(*len))) {
return -1;
}
if((n=sceHttpReadData(ctx->reqId, *data, *len) < 0)) {
return n;
}
return status;
}
/**
*
**/
static void
http_fini(http_ctx_t* ctx) {
if(ctx->reqId >= 0) {
sceHttpDeleteRequest(ctx->reqId);
}
if(ctx->connId >= 0) {
sceHttpDeleteConnection(ctx->connId);
}
if(ctx->tmplId >= 0) {
sceHttpDeleteTemplate(ctx->tmplId);
}
if(ctx->libhttpCtxId >= 0) {
sceHttpTerm(ctx->libhttpCtxId);
}
if(ctx->libsslCtxId >= 0) {
sceSslTerm(ctx->libsslCtxId);
}
if(ctx->libnetMemId >= 0) {
sceNetPoolDestroy(ctx->libnetMemId);
}
}
/**
*
**/
static uint8_t*
http_get(const char* url, size_t* len) {
http_ctx_t ctx;
size_t size = 0;
uint8_t* data;
if((http_init(&ctx, "elfldr.elf", url))) {
http_fini(&ctx);
return 0;
}
if(http_request(&ctx, &data, &size) != 200) {
http_fini(&ctx);
return 0;
}
http_fini(&ctx);;
if(len) {
*len = size;
}
return data;
}
/**
* Read a payload from he given URL.
**/
static int
url_read(const char* url, uint8_t** content, size_t* content_size) {
if((*content=http_get(url, content_size))) {
return 0;
}
return -1;
}
/**
* Read a payload from given path.
**/
static int
path_read(const char* path, uint8_t** content, size_t* content_size) {
uint8_t* buf;
ssize_t size;
FILE* f;
if(!(f=fopen(path, "rb"))) {
return -1;
}
if(fseek(f, 0, SEEK_END)) {
return -1;
}
if((size=ftell(f)) < 0) {
return -1;
}
if(fseek(f, 0, SEEK_SET)) {
return -1;
}
if(!(buf=malloc(size))) {
return -1;
}
if(fread(buf, 1, size, f) != size) {
free(buf);
return -1;
}
if(fclose(f)) {
free(buf);
return -1;
}
*content = buf;
*content_size = size;
return 0;
}
static int
hexval(char c) {
if(c >= '0' && c <= '9') {
return c - '0';
}
if(c >= 'a' && c <= 'f') {
return c - 'a' + 10;
}
if(c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
return -1;
}
static char*
uri_decode(const char* src, size_t len) {
char* out;
char* dst;
int lo;
int hi;
if(!(out=malloc(len+1))) {
return 0;
}
dst = out;
for(size_t i=0; i<len; i++) {
if(src[i] == '%' && i+2 < len) {
hi = hexval(src[i+1]);
lo = hexval(src[i+2]);
if(hi >= 0 && lo >= 0) {
*dst++ = (char)((hi << 4) | lo);
i += 2;
} else {
*dst++ = src[i];
}
}
else {
*dst++ = src[i];
}
}
*dst = 0;
return out;
}
static char*
uri_get_path(const char* uri) {
size_t len = strlen(uri);
const char* begin = 0;
const char* end = 0;
end = uri + len;
for(int i=0; i<len; i++) {
if(!begin && uri[i] == ':' && uri[i+1] == '/') {
begin = uri + i + 1;
}
else if(uri[i] == '?' || uri[i] == '#' || uri[i] == '\0') {
end = uri + i;
break;
}
}
return uri_decode(begin, end-begin);
}
char*
uri_get_filename(const char* uri) {
size_t len = strlen(uri);
const char* begin = 0;
const char* end = 0;
end = uri + len;
for(int i=0; i<len; i++) {
if(uri[i] == '/') {
begin = uri + i + 1;
}
else if(uri[i] == '?' || uri[i] == '#' || uri[i] == '\0') {
end = uri + i;
break;
}
}
return uri_decode(begin, end-begin);
}
int
uri_get_content(const char* uri, uint8_t** content, size_t* content_size) {
int err = -1;
char *path;
if(!strncmp(uri, "file:/", 6)) {
if((path=uri_get_path(uri))) {
err = path_read(path, content, content_size);
free(path);
}
} else if(!strncmp(uri, "http:/", 6) || !strncmp(uri, "https:/", 7)) {
err = url_read(uri, content, content_size);
}
return err;
}
char*
uri_get_param(const char* uri, const char* name) {
const char* begin;
const char* end;
const char* p;
size_t len;
if(!(p=strchr(uri, '?'))) {
return 0;
}
p++;
len = strlen(name);
while(*p && *p != '#') {
if(!strncmp(p, name, len) && p[len] == '=') {
begin = p + len + 1;
if(!(end=strchr(begin, '&'))) {
if(!(end=strchr(begin, '#'))) {
end = begin + strlen(begin);
}
}
return uri_decode(begin, end-begin);
}
while(*p && *p != '&' && *p != '#') {
p++;
}
if(*p == '&') {
p++;
}
}
return 0;
}