339 |
mateuszvis |
1 |
/*
|
|
|
2 |
* unpacks a http "chunked" transfer into a raw data stream.
|
|
|
3 |
* this file is part of the pkgnet tool from the SvarDOS project.
|
|
|
4 |
*
|
|
|
5 |
* Copyright (C) 2021 Mateusz Viste
|
|
|
6 |
*/
|
|
|
7 |
|
|
|
8 |
#include <stdlib.h> /* atol() */
|
|
|
9 |
#include <string.h>
|
|
|
10 |
|
|
|
11 |
#include "unchunk.h"
|
|
|
12 |
|
341 |
mateuszvis |
13 |
|
|
|
14 |
/* transforms a http CHUNKED stream into actual data, returns the amount of
|
|
|
15 |
* raw data to read or -1 on error. st MUST be zeroed before first call. */
|
|
|
16 |
int unchunk(unsigned char *buff, int bufflen, struct unchunk_state *st) {
|
339 |
mateuszvis |
17 |
int hdrstart, hdrend;
|
|
|
18 |
|
|
|
19 |
/* if chunk header was being in progress, try to decode it now */
|
341 |
mateuszvis |
20 |
if (st->bytesleft == -1) {
|
|
|
21 |
int partial_hdr_len = strlen(st->partial_hdr);
|
339 |
mateuszvis |
22 |
/* locate header terminator, if available */
|
|
|
23 |
for (hdrend = 0; (hdrend < bufflen) && (buff[hdrend] != '\n'); hdrend++);
|
|
|
24 |
if (partial_hdr_len + hdrend > 15) return(-1); /* error: chunk header too long */
|
|
|
25 |
|
|
|
26 |
/* copy header to buffer */
|
341 |
mateuszvis |
27 |
memcpy(st->partial_hdr + partial_hdr_len, buff, hdrend);
|
|
|
28 |
st->partial_hdr[partial_hdr_len + hdrend] = 0;
|
339 |
mateuszvis |
29 |
|
|
|
30 |
/* quit if header still no complete */
|
|
|
31 |
if (hdrend >= bufflen) return(0);
|
|
|
32 |
|
|
|
33 |
/* good, got whole header */
|
|
|
34 |
bufflen -= hdrend + 1;
|
|
|
35 |
memmove(buff, buff + hdrend + 1, bufflen);
|
341 |
mateuszvis |
36 |
st->bytesleft = strtol(st->partial_hdr, NULL, 16);
|
339 |
mateuszvis |
37 |
}
|
|
|
38 |
|
|
|
39 |
AGAIN:
|
|
|
40 |
|
341 |
mateuszvis |
41 |
if (bufflen <= st->bytesleft) {
|
|
|
42 |
st->bytesleft -= bufflen;
|
339 |
mateuszvis |
43 |
return(bufflen);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/* else bufflen > bytesleft */
|
|
|
47 |
|
|
|
48 |
/* skip trailing \r\n after chunk */
|
341 |
mateuszvis |
49 |
for (hdrstart = st->bytesleft; hdrstart < bufflen; hdrstart++) if ((buff[hdrstart] != '\r') && (buff[hdrstart] != '\n')) break;
|
339 |
mateuszvis |
50 |
/* skip chunk size (look for its \n terminator) */
|
|
|
51 |
for (hdrend = hdrstart; (hdrend < bufflen) && (buff[hdrend] != '\n'); hdrend++);
|
|
|
52 |
|
|
|
53 |
if (hdrend < bufflen) { /* full header found */
|
|
|
54 |
long newchunklen;
|
|
|
55 |
/* read the header length */
|
|
|
56 |
newchunklen = strtol((char *)buff + hdrstart, NULL, 16);
|
|
|
57 |
/* move data over header to get a contiguous block of data */
|
341 |
mateuszvis |
58 |
memmove(buff + st->bytesleft, buff + hdrend + 1, bufflen - (hdrend + 1));
|
|
|
59 |
bufflen -= (hdrend + 1 - st->bytesleft);
|
339 |
mateuszvis |
60 |
/* update bytesleft */
|
341 |
mateuszvis |
61 |
st->bytesleft += newchunklen;
|
339 |
mateuszvis |
62 |
/* loop again */
|
|
|
63 |
goto AGAIN;
|
|
|
64 |
} else { /* partial header */
|
341 |
mateuszvis |
65 |
if ((bufflen - st->bytesleft) > 15) return(-1); /* error: chunk header appears to be longer than 15 characters */
|
|
|
66 |
memset(st->partial_hdr, 0, sizeof(st->partial_hdr));
|
|
|
67 |
memcpy(st->partial_hdr, buff + st->bytesleft, bufflen - st->bytesleft);
|
|
|
68 |
bufflen -= (bufflen - st->bytesleft);
|
|
|
69 |
st->bytesleft = -1; /* "header in progress" */
|
339 |
mateuszvis |
70 |
return(bufflen);
|
|
|
71 |
}
|
|
|
72 |
}
|