AmendHub

Download:

jcs

/

subtext

/

amendments

/

385

fidopkt: Make fidopkt_encode encode... a fidopkt

Not sure what I was thinking with the old approach

jcs made amendment 385 about 1 year ago
--- fidopkt.c Tue Mar 7 22:45:54 2023 +++ fidopkt.c Thu Mar 9 10:14:27 2023 @@ -199,7 +199,7 @@ fidopkt_parse(char *packet_filename, char *buf, size_t struct fidopkt *ret; struct tm tm = { 0 }; struct fidopkt_address orig_address, dest_address; - size_t message_len, llen; + size_t body_len, llen; long tzoff; char dmon[6]; short dday, dyear, dhour, dmin, dsec, dutc, n; @@ -322,19 +322,19 @@ fidopkt_parse(char *packet_filename, char *buf, size_t goto parse_fail; } - ret->message = malloc(len); - if (ret->message == NULL) { + ret->body = malloc(len); + if (ret->body == NULL) { logger_printf("[fidopkt] malloc(%lu) failed", len); goto parse_fail; } - message_len = 0; + body_len = 0; tear = false; while (len) { llen = fidopkt_read_until(&buf, &len, '\r', sizeof(line), line, false); - if (message_len == 0 && strncmp(line, "AREA:", 5) == 0) { + if (body_len == 0 && strncmp(line, "AREA:", 5) == 0) { line[llen] = '\0'; strlcpy(ret->area, line + 5, sizeof(ret->area)); continue; @@ -372,33 +372,33 @@ fidopkt_parse(char *packet_filename, char *buf, size_t } else if (!tear) { if (line[0] == ' ' && line[1] == ' ') continue; - if (message_len == 0 && + if (body_len == 0 && (line[0] == '\n' || line[0] == '\r' || line[0] == '\0')) continue; if (line[llen - 1] == '\0') { - memcpy(ret->message + message_len, line, llen - 1); - message_len += llen - 1; - ret->message[message_len++] = '\r'; - ret->message[message_len++] = '\n'; + memcpy(ret->body + body_len, line, llen - 1); + body_len += llen - 1; + ret->body[body_len++] = '\r'; + ret->body[body_len++] = '\n'; } else { /* long line */ - memcpy(ret->message + message_len, line, llen); - message_len += llen; + memcpy(ret->body + body_len, line, llen); + body_len += llen; } } } - while (message_len > 1) { - if (ret->message[message_len - 1] == '\n' && - ret->message[message_len - 2] == '\r') - message_len -= 2; - else if (ret->message[message_len - 1] == '\r') - message_len -= 1; + while (body_len > 1) { + if (ret->body[body_len - 1] == '\n' && + ret->body[body_len - 2] == '\r') + body_len -= 2; + else if (ret->body[body_len - 1] == '\r') + body_len -= 1; else break; } - ret->message[message_len] = '\0'; - ret->message_len = message_len; + ret->body[body_len] = '\0'; + ret->body_len = body_len; ret->time = mktime(&tm); if (ret->time == -1) { @@ -428,14 +428,14 @@ fidopkt_parse(char *packet_filename, char *buf, size_t (tzoff >= 0 ? "+" : ""), tz); logger_printf("[fidopkt] Message:"); - lastbreak = ret->message; - for (n = 0; n <= ret->message_len; n++) { - if (ret->message[n] == '\n' || n == ret->message_len) { - was = ret->message[n]; - ret->message[n] = '\0'; + lastbreak = ret->body; + for (n = 0; n <= ret->body_len; n++) { + if (ret->body[n] == '\n' || n == ret->body_len) { + was = ret->body[n]; + ret->body[n] = '\0'; logger_printf("[fidopkt] %s", lastbreak); - ret->message[n] = was; - lastbreak = ret->message + n + 1; + ret->body[n] = was; + lastbreak = ret->body + n + 1; } } #endif @@ -448,35 +448,32 @@ parse_fail: } size_t -fidopkt_encode(char **pkt_buf, char *pkt_password, - struct fidopkt_address *orig_node, struct fidopkt_address *dest_node, - struct fidopkt_msgid *msgid, unsigned long time, short tzoff, char *from, - char *to, char *area, char *reply, char *subject, char *body) +fidopkt_encode(struct fidopkt *pkt, char **ret_buf, char *pkt_password, + short tzoff) { static char scratch[30]; struct tm *tm; char *buf; - size_t len, off, buf_size, subject_len, body_len; + size_t len, off, buf_size, subject_len; - subject_len = strlen(subject); - body_len = strlen(body); + subject_len = strlen(pkt->subject); - buf_size = FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_HEADER_SIZE + subject_len + - body_len + 512; + buf_size = FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_HEADER_SIZE + + subject_len + pkt->body_len + 512; buf = xmalloczero(buf_size); - tm = localtime(&time); + tm = localtime(&pkt->time); - PUT_U16(buf + FIDOPKT_ORIGZONE, orig_node->zone); - PUT_U16(buf + FIDOPKT_QORIGZONE, orig_node->zone); - PUT_U16(buf + FIDOPKT_ORIGNET, orig_node->net); - PUT_U16(buf + FIDOPKT_ORIGNODE, orig_node->node); + PUT_U16(buf + FIDOPKT_ORIGZONE, pkt->orig.zone); + PUT_U16(buf + FIDOPKT_QORIGZONE, pkt->orig.zone); + PUT_U16(buf + FIDOPKT_ORIGNET, pkt->orig.net); + PUT_U16(buf + FIDOPKT_ORIGNODE, pkt->orig.node); memcpy(buf + FIDOPKT_PASSWORD, pkt_password, strlen(pkt_password)); - PUT_U16(buf + FIDOPKT_DESTZONE, dest_node->zone); - PUT_U16(buf + FIDOPKT_QDESTZONE, dest_node->zone); - PUT_U16(buf + FIDOPKT_DESTNET, dest_node->net); - PUT_U16(buf + FIDOPKT_DESTNODE, dest_node->node); + PUT_U16(buf + FIDOPKT_DESTZONE, pkt->dest.zone); + PUT_U16(buf + FIDOPKT_QDESTZONE, pkt->dest.zone); + PUT_U16(buf + FIDOPKT_DESTNET, pkt->dest.net); + PUT_U16(buf + FIDOPKT_DESTNODE, pkt->dest.node); PUT_U16(buf + FIDOPKT_YEAR, tm->tm_year + 1900); PUT_U16(buf + FIDOPKT_MONTH, tm->tm_mon + 1); @@ -493,14 +490,14 @@ fidopkt_encode(char **pkt_buf, char *pkt_password, PUT_U16(buf + FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_PKTTYPE, 2); PUT_U16(buf + FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_DESTNET, - dest_node->net); + pkt->dest.net); PUT_U16(buf + FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_DESTNODE, - dest_node->node); + pkt->dest.node); PUT_U16(buf + FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_ORIGNET, - orig_node->net); + pkt->orig.net); PUT_U16(buf + FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_ORIGNODE, - orig_node->node); + pkt->orig.node); off = FIDOPKT_HEADER_SIZE + FIDOPKT_MSG_HEADER_SIZE; @@ -511,28 +508,29 @@ fidopkt_encode(char **pkt_buf, char *pkt_password, off++; /* to, null-terminated */ - len = strlen(to); + len = strlen(pkt->to); if (!grow_to_fit(&buf, &buf_size, off, len + 1, len + 1)) goto fail; - memcpy(buf + off, to, len); + memcpy(buf + off, pkt->to, len); off += len + 1; /* from, null-terminated */ - len = strlen(from); + len = strlen(pkt->from); if (!grow_to_fit(&buf, &buf_size, off, len + 1, len + 1)) goto fail; - memcpy(buf + off, from, len); + memcpy(buf + off, pkt->from, len); off += len + 1; /* subject, null-terminated */ - if (!grow_to_fit(&buf, &buf_size, off, subject_len + 1, subject_len + 1)) + if (!grow_to_fit(&buf, &buf_size, off, subject_len + 1, + subject_len + 1)) goto fail; - memcpy(buf + off, subject, subject_len); + memcpy(buf + off, pkt->subject, subject_len); off += subject_len + 1; /* optional area (non-kludge) \r-terminated */ - if (area != NULL) { - len = snprintf(scratch, sizeof(scratch), "AREA: %s\r", area); + if (pkt->area[0] != '\0') { + len = snprintf(scratch, sizeof(scratch), "AREA: %s\r", pkt->area); if (len > sizeof(scratch)) goto fail; if (!grow_to_fit(&buf, &buf_size, off, len, len)) @@ -543,8 +541,8 @@ fidopkt_encode(char **pkt_buf, char *pkt_password, /* kludge line: INTL to from, \r-terminated */ len = snprintf(scratch, sizeof(scratch), "%cINTL %d:%d/%d %d:%d/%d\r", - 0x1, dest_node->zone, dest_node->net, dest_node->node, - orig_node->zone, orig_node->net, orig_node->node); + 0x1, pkt->dest.zone, pkt->dest.net, pkt->dest.node, + pkt->orig.zone, pkt->orig.net, pkt->orig.node); if (len > sizeof(scratch)) goto fail; if (!grow_to_fit(&buf, &buf_size, off, len, len)) @@ -553,7 +551,7 @@ fidopkt_encode(char **pkt_buf, char *pkt_password, off += len; /* kludge line: TZOFF tzoff, \r-terminated */ - len = snprintf(scratch, sizeof(scratch), "%cTZUTC: %+04d\r", + len = snprintf(scratch, sizeof(scratch), "%cTZUTC: %+05d\r", 0x1, tzoff); if (len > sizeof(scratch)) goto fail; @@ -564,7 +562,7 @@ fidopkt_encode(char **pkt_buf, char *pkt_password, /* kludge line: MSGID zone:net/node id, \r-terminated */ len = snprintf(scratch, sizeof(scratch), "%cMSGID: %d:%d/%d %lx\r", - 0x1, msgid->zone, msgid->net, msgid->node, msgid->id); + 0x1, pkt->msgid.zone, pkt->msgid.net, pkt->msgid.node, pkt->msgid.id); if (len > sizeof(scratch)) goto fail; if (!grow_to_fit(&buf, &buf_size, off, len, len)) @@ -573,9 +571,9 @@ fidopkt_encode(char **pkt_buf, char *pkt_password, off += len; /* kludge line: REPLY orig-msgid, \r-terminated */ - if (reply != NULL && reply[0]) { + if (pkt->reply[0] != '\0') { len = snprintf(scratch, sizeof(scratch), "%cREPLY: %s\r", - 0x1, reply); + 0x1, pkt->reply); if (len > sizeof(scratch)) goto fail; if (!grow_to_fit(&buf, &buf_size, off, len, len)) @@ -594,13 +592,14 @@ fidopkt_encode(char **pkt_buf, char *pkt_password, memcpy(buf + off, scratch, len); off += len; - if (!grow_to_fit(&buf, &buf_size, off, body_len + 6, body_len + 6)) + if (!grow_to_fit(&buf, &buf_size, off, pkt->body_len + 6, + pkt->body_len + 6)) goto fail; - memcpy(buf + off, body, body_len); - off += body_len; - sprintf(buf + off, "\r--- \r"); + memcpy(buf + off, pkt->body, pkt->body_len); + off += pkt->body_len; + off += sprintf(buf + off, "\r--- \r"); - *pkt_buf = buf; + *ret_buf = buf; return off; fail: @@ -614,8 +613,8 @@ fidopkt_free(struct fidopkt **pktp) { struct fidopkt *pkt = (struct fidopkt *)*pktp; - if (pkt->message) - free(pkt->message); + if (pkt->body) + free(pkt->body); free(pkt); *pktp = NULL; } --- fidopkt.h Tue Mar 7 16:29:01 2023 +++ fidopkt.h Wed Mar 8 17:43:46 2023 @@ -46,17 +46,15 @@ struct fidopkt { char reply[32]; char msgid_orig[64]; struct fidopkt_msgid msgid; - char *message; - size_t message_len; + char *body; + size_t body_len; }; bool fidopkt_parse_address(char *str, struct fidopkt_address **address); struct fidopkt * fidopkt_parse(char *packet_filename, char *buf, size_t len); -size_t fidopkt_encode(char **pkt_buf, char *pkt_password, - struct fidopkt_address *orig_node, struct fidopkt_address *dest_node, - struct fidopkt_msgid *msgid, unsigned long time, short tzoff, char *from, - char *to, char *area, char *reply, char *subject, char *body); +size_t fidopkt_encode(struct fidopkt *pkt, char **buf, char *pkt_password, + short tzoff); void fidopkt_free(struct fidopkt **pktp); #endif