/*! * Requires: * * The "current" pointer in "b" points to encoded raw data. * * Ensures: * * The address of the first byte of the data is returned via "p", * and the length is returned via "len". If NULL, they are not * set. * * On return, the current pointer of "b" will point to the character * following the data length and the data. * */ lwres_result_t lwres_data_parse(lwres_buffer_t *b, unsigned char **p, lwres_uint16_t *len) { lwres_uint16_t datalen; unsigned char *data; REQUIRE(b != NULL); /* * Pull off the length (2 bytes) */ if (!SPACE_REMAINING(b, 2)) return (LWRES_R_UNEXPECTEDEND); datalen = lwres_buffer_getuint16(b); /* * Set the pointer to this string to the right place, then * advance the buffer pointer. */ if (!SPACE_REMAINING(b, datalen)) return (LWRES_R_UNEXPECTEDEND); data = b->base + b->current; lwres_buffer_forward(b, datalen); if (len != NULL) *len = datalen; if (p != NULL) *p = data; return (LWRES_R_SUCCESS); }
/*% Offers the same semantics as lwres_nooprequest_parse() except it yields a lwres_noopresponse_t structure. */ lwres_result_t lwres_noopresponse_parse(lwres_context_t *ctx, lwres_buffer_t *b, lwres_lwpacket_t *pkt, lwres_noopresponse_t **structp) { int ret; lwres_noopresponse_t *req; REQUIRE(ctx != NULL); REQUIRE(b != NULL); REQUIRE(pkt != NULL); REQUIRE(structp != NULL && *structp == NULL); if ((pkt->pktflags & LWRES_LWPACKETFLAG_RESPONSE) == 0) return (LWRES_R_FAILURE); req = CTXMALLOC(sizeof(lwres_noopresponse_t)); if (req == NULL) return (LWRES_R_NOMEMORY); if (!SPACE_REMAINING(b, sizeof(lwres_uint16_t))) { ret = LWRES_R_UNEXPECTEDEND; goto out; } req->datalength = lwres_buffer_getuint16(b); if (!SPACE_REMAINING(b, req->datalength)) { ret = LWRES_R_UNEXPECTEDEND; goto out; } req->data = b->base + b->current; lwres_buffer_forward(b, req->datalength); if (LWRES_BUFFER_REMAINING(b) != 0) { ret = LWRES_R_TRAILINGDATA; goto out; } /* success! */ *structp = req; return (LWRES_R_SUCCESS); /* Error return */ out: CTXFREE(req, sizeof(lwres_noopresponse_t)); return (ret); }
/*! * Requires: * * The "current" pointer in "b" point to an encoded string. * * Ensures: * * The address of the first byte of the string is returned via "c", * and the length is returned via "len". If NULL, they are not * set. * * On return, the current pointer of "b" will point to the character * following the string length, the string, and the trailing NULL. * */ lwres_result_t lwres_string_parse(lwres_buffer_t *b, char **c, lwres_uint16_t *len) { lwres_uint16_t datalen; char *string; REQUIRE(b != NULL); /* * Pull off the length (2 bytes) */ if (!SPACE_REMAINING(b, 2)) return (LWRES_R_UNEXPECTEDEND); datalen = lwres_buffer_getuint16(b); /* * Set the pointer to this string to the right place, then * advance the buffer pointer. */ if (!SPACE_REMAINING(b, datalen)) return (LWRES_R_UNEXPECTEDEND); string = (char *)b->base + b->current; lwres_buffer_forward(b, datalen); /* * Skip the "must be zero" byte. */ if (!SPACE_REMAINING(b, 1)) return (LWRES_R_UNEXPECTEDEND); if (0 != lwres_buffer_getuint8(b)) return (LWRES_R_FAILURE); if (len != NULL) *len = datalen; if (c != NULL) *c = string; return (LWRES_R_SUCCESS); }