Add size_t arg to strn_to_sid_t()

This commit is contained in:
Andrew Bettison 2014-04-29 13:39:21 +09:30
parent c222727a46
commit 008dd6ab21
4 changed files with 16 additions and 17 deletions

View File

@ -28,30 +28,29 @@ int cmp_sid_t(const sid_t *a, const sid_t *b)
return memcmp(a, b, sizeof a->binary);
}
int str_to_sid_t(sid_t *sid, const char *hex)
int str_to_sid_t(sid_t *sidp, const char *hex)
{
if (strcmp(hex, "broadcast") == 0) {
if (sid)
*sid = SID_BROADCAST;
return 0;
}
return sid ? fromhexstr(sid->binary, hex, sizeof sid->binary) : is_xstring(hex, SID_STRLEN) ? 0 : -1;
const char *end;
return strn_to_sid_t(sidp, hex, SIZE_MAX, &end) != -1 && *end == '\0' ? 0 : -1;
}
int strn_to_sid_t(sid_t *sid, const char *hex, const char **endp)
int strn_to_sid_t(sid_t *sidp, const char *hex, size_t hexlen, const char **endp)
{
if (str_startswith(hex, "broadcast", endp)) {
if (sid)
*sid = SID_BROADCAST;
if (strn_startswith(hex, hexlen, "broadcast", endp)) {
if (sidp)
*sidp = SID_BROADCAST;
return 0;
}
sid_t tmp;
if (hexlen < sizeof tmp.binary * 2)
return -1;
int n = fromhex(tmp.binary, hex, sizeof tmp.binary);
if (n != sizeof tmp.binary)
return -1;
*sid = tmp;
if (sidp)
*sidp = tmp;
if (endp)
*endp = hex + sizeof sid->binary * 2;
*endp = hex + sizeof tmp.binary * 2;
return 0;
}

View File

@ -121,12 +121,12 @@ int restful_meshms_(httpd_request *r, const char *remainder)
const char *verb = HTTP_VERB_GET;
HTTP_HANDLER *handler = NULL;
const char *end;
if (strn_to_sid_t(&r->sid1, remainder, &end) != -1) {
if (strn_to_sid_t(&r->sid1, remainder, SIZE_MAX, &end) != -1) {
remainder = end;
if (strcmp(remainder, "/conversationlist.json") == 0) {
handler = restful_meshms_conversationlist_json;
remainder = "";
} else if (*remainder == '/' && strn_to_sid_t(&r->sid2, remainder + 1, &end) != -1) {
} else if (*remainder == '/' && strn_to_sid_t(&r->sid2, remainder + 1, SIZE_MAX, &end) != -1) {
remainder = end;
if (strcmp(remainder, "/messagelist.json") == 0) {
handler = restful_meshms_messagelist_json;

View File

@ -414,7 +414,7 @@ static int insert_mime_part_end(struct http_request *hr)
httpd_request *r = (httpd_request *) hr;
if (r->u.insert.current_part == PART_AUTHOR) {
if ( r->u.insert.author_hex_len != sizeof r->u.insert.author_hex
|| strn_to_sid_t(&r->u.insert.author, r->u.insert.author_hex, NULL) == -1
|| strn_to_sid_t(&r->u.insert.author, r->u.insert.author_hex, sizeof r->u.insert.author_hex, NULL) == -1
)
return http_response_form_part(r, "Invalid", PART_AUTHOR, r->u.insert.author_hex, r->u.insert.author_hex_len);
r->u.insert.received_author = 1;

View File

@ -192,7 +192,7 @@ typedef struct sid_binary {
int cmp_sid_t(const sid_t *a, const sid_t *b);
int str_to_sid_t(sid_t *sid, const char *hex);
int strn_to_sid_t(sid_t *sid, const char *hex, const char **endp);
int strn_to_sid_t(sid_t *sid, const char *hex, size_t hexlen, const char **endp);
#define alloca_tohex_sas(sas) alloca_tohex((sas), SAS_SIZE)