Download raw body.
move got_pkt_readlen() to separate function
On Tue, Apr 16, 2024 at 07:07:14PM +0200, Tobias Heider wrote: > We'll need this in got-fetch-http > > ----------------------------------------------- > commit 98fc2289f1bf714314940fbb725d95ea15ced3f1 (xdigit) > from: Tobias Heider <me@tobhe.de> > date: Tue Apr 16 16:30:10 2024 UTC > > Make got_pkt_readlen() a separate function. There are multiple > places where we need to parse strings with the same format. ok stsp@ > diff 3bdb50664480ef16bc57431ac739d4010133d2a2 98fc2289f1bf714314940fbb725d95ea15ced3f1 > commit - 3bdb50664480ef16bc57431ac739d4010133d2a2 > commit + 98fc2289f1bf714314940fbb725d95ea15ced3f1 > blob - 009561888bba0c0f18ea02211c6639bf87cfb368 > blob + 8295c80b38111178704802e1cfa5b090781c7ed5 > --- lib/got_lib_pkt.h > +++ lib/got_lib_pkt.h > @@ -20,6 +20,7 @@ > const struct got_error *got_pkt_readn(ssize_t *off, int fd, void *buf, > size_t n); > const struct got_error *got_pkt_flushpkt(int fd, int chattygot); > +const struct got_error *got_pkt_readlen(int *len, const char *str, int chattygot); > const struct got_error *got_pkt_readhdr(int *datalen, int fd, int chattygot); > const struct got_error *got_pkt_readpkt(int *outlen, int fd, char *buf, > int buflen, int chattygot); > blob - fad7f03e4f70f1513ceb7af03bb71e1e7907a950 > blob + 6bb2b9c99379691b62c97f575b8b014e0c0e5a5d > --- lib/pkt.c > +++ lib/pkt.c > @@ -58,6 +58,37 @@ got_pkt_flushpkt(int fd, int chattygot) > return NULL; > } > > +const struct got_error * > +got_pkt_readlen(int *len, const char *str, int chattygot) > +{ > + char *e; > + int i; > + > + *len = 0; > + for (i = 0; i < 4; i++) { > + if (!isprint((unsigned char)str[i])) > + return got_error_msg(GOT_ERR_BAD_PACKET, > + "unprintable character in pkt-line length field"); > + } > + for (i = 0; i < 4; i++) { > + if (!isxdigit((unsigned char)str[i])) { > + if (chattygot) > + fprintf(stderr, "%s: bad length: '%s'\n", > + getprogname(), str); > + return got_error_msg(GOT_ERR_BAD_PACKET, > + "packet length not specified in hex"); > + } > + } > + errno = 0; > + *len = strtol(str, &e, 16); > + if (str[0] == '\0' || *e != '\0') > + return got_error(GOT_ERR_BAD_PACKET); > + if (errno == ERANGE || *len > INT_MAX || *len < INT_MIN) > + return got_error_msg(GOT_ERR_BAD_PACKET, "bad pkt-line length"); > + > + return NULL; > +} > + > /* > * Packet header contains a 4-byte hexstring which specifies the length > * of data which follows. > @@ -65,12 +96,10 @@ got_pkt_flushpkt(int fd, int chattygot) > const struct got_error * > got_pkt_readhdr(int *datalen, int fd, int chattygot) > { > - static const struct got_error *err = NULL; > + static const struct got_error *err; > char lenstr[5]; > - long len; > - char *e; > - int n, i; > ssize_t r; > + int n; > > *datalen = 0; > > @@ -86,36 +115,14 @@ got_pkt_readhdr(int *datalen, int fd, int chattygot) > if (r != 4) > return got_error_msg(GOT_ERR_BAD_PACKET, > "wrong packet header length"); > - > lenstr[4] = '\0'; > - for (i = 0; i < 4; i++) { > - if (!isprint((unsigned char)lenstr[i])) > - return got_error_msg(GOT_ERR_BAD_PACKET, > - "unprintable character in packet length field"); > - } > - for (i = 0; i < 4; i++) { > - if (!isxdigit((unsigned char)lenstr[i])) { > - if (chattygot) > - fprintf(stderr, "%s: bad length: '%s'\n", > - getprogname(), lenstr); > - return got_error_msg(GOT_ERR_BAD_PACKET, > - "packet length not specified in hex"); > - } > - } > - errno = 0; > - len = strtol(lenstr, &e, 16); > - if (lenstr[0] == '\0' || *e != '\0') > - return got_error(GOT_ERR_BAD_PACKET); > - if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN)) > - return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length"); > - if (len > INT_MAX || len < INT_MIN) > - return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length"); > - n = len; > + > + err = got_pkt_readlen(&n, lenstr, chattygot); > if (n == 0) > - return NULL; > + return err; > if (n <= 4) > return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short"); > - n -= 4; > + n -= 4; > > *datalen = n; > return NULL; > >
move got_pkt_readlen() to separate function