links

lynx-like text mode web browser
git clone anongit@rnpnr.xyz:links.git
Log | Files | Refs | Feed | README | LICENSE

Commit: 96b1173d13e56d729b8fb8f19a159e7b40c04ae1
Parent: bc4e7359dc05f1692352cb9c07d649e282884ce1
Author: Randy Palamar
Date:   Tue, 29 Nov 2022 23:15:23 -0700

add_bytes_to_str(): return new text length

Diffstat:
Mconnect.c | 10+++++-----
Mdefault.c | 5++---
Mhtml.c | 6+++---
Mhttp.c | 9+++++----
Mlinks.h | 2+-
Msession.c | 13++++++-------
Mstring.c | 29+++++++++++++++--------------
Mtypes.c | 2+-
Murl.c | 13+++++--------
Mview.c | 14+++++++-------
10 files changed, 50 insertions(+), 53 deletions(-)

diff --git a/connect.c b/connect.c @@ -278,14 +278,14 @@ handle_socks(void *c_) int wr; setcstate(c, S_SOCKS_NEG); set_connection_timeout(c); - add_bytes_to_str(&command, &len, cast_uchar "\004\001", 2); + len = add_bytes_to_str(&command, len, cast_uchar "\004\001", 2); add_chr_to_str(&command, &len, b->l.target_port >> 8); add_chr_to_str(&command, &len, b->l.target_port); - add_bytes_to_str(&command, &len, cast_uchar "\000\000\000\001", 4); + len = add_bytes_to_str(&command, len, cast_uchar "\000\000\000\001", 4); if (strchr(c->socks_proxy, '@')) - add_bytes_to_str(&command, &len, - (unsigned char *)c->socks_proxy, - strcspn(c->socks_proxy, "@")); + len = add_bytes_to_str(&command, len, + (unsigned char *)c->socks_proxy, + strcspn(c->socks_proxy, "@")); add_chr_to_str(&command, &len, 0); if (!(host = get_host_name(c->url))) { free(command); diff --git a/default.c b/default.c @@ -240,7 +240,7 @@ read_config_file(unsigned char *name) for (i = 0; i < r; i++) if (!cfg_buffer[i]) cfg_buffer[i] = ' '; - add_bytes_to_str(&s, &l, cfg_buffer, r); + l = add_bytes_to_str(&s, l, cfg_buffer, r); } free(cfg_buffer); if (r == -1) { @@ -618,8 +618,7 @@ str_wr(struct option *o, unsigned char **s, int *l) add_nm(o, s, l); if (strlen(cast_const_char o->ptr) + 1 > (size_t)o->max) { unsigned char *s1 = NULL; - int l1 = 0; - add_bytes_to_str(&s1, &l1, o->ptr, o->max - 1); + add_bytes_to_str(&s1, 0, o->ptr, o->max - 1); add_quoted_to_str(s, l, s1); free(s1); } else diff --git a/html.c b/html.c @@ -2434,7 +2434,7 @@ abort: add_to_str(&lbl, &lbl_l, q); free(q); } - add_bytes_to_str(&vlbl, &vlbl_l, s, l); + vlbl_l = add_bytes_to_str(&vlbl, vlbl_l, s, l); } if (eof - html >= 2 && (html[1] == '!' || html[1] == '?')) { html = skip_comment(html, eof); @@ -3731,7 +3731,7 @@ se4: free(*menu); return -1; } - add_bytes_to_str(&label, &lblen, s, ss - s); + lblen = add_bytes_to_str(&label, lblen, s, ss - s); s = ss; if (eof - s >= 2 && (s[1] == '!' || s[1] == '?')) { s = skip_comment(s, eof); @@ -3930,7 +3930,7 @@ xse: while (s < eof && *s != '<') xsp: s++; - add_bytes_to_str(title, &tlen, s1, s - s1); + tlen = add_bytes_to_str(title, tlen, s1, s - s1); if (s >= eof) goto se; if (eof - s >= 2 && (s[1] == '!' || s[1] == '?')) { diff --git a/http.c b/http.c @@ -321,7 +321,8 @@ http_bad_url: &u_host_len, NULL, NULL, NULL, NULL, NULL)) goto http_bad_url; u2 = NULL; - add_bytes_to_str(&u2, &u2_len, u, u_host + u_host_len - u); + u2_len = + add_bytes_to_str(&u2, u2_len, u, u_host + u_host_len - u); add_to_str(&u2, &u2_len, proxies.dns_append); add_to_str(&u2, &u2_len, u_host + u_host_len); } @@ -658,7 +659,7 @@ add_post_header(unsigned char **hdr, int *l, unsigned char **post) unsigned char *pd = cast_uchar strchr((char *)*post, '\n'); if (pd) { add_to_str(hdr, l, cast_uchar "Content-Type: "); - add_bytes_to_str(hdr, l, *post, pd - *post); + *l = add_bytes_to_str(hdr, *l, *post, pd - *post); add_to_str(hdr, l, cast_uchar "\r\n"); *post = pd + 1; } @@ -690,8 +691,8 @@ add_extra_options(unsigned char **hdr, int *l) free(x); new_hdr = NULL; new_l = 0; - add_bytes_to_str(&new_hdr, &new_l, *hdr, - v - *hdr); + new_l = add_bytes_to_str( + &new_hdr, new_l, *hdr, v - *hdr); while (*++c == ' ') ; add_to_str(&new_hdr, &new_l, c); diff --git a/links.h b/links.h @@ -255,7 +255,7 @@ int xstrcmp(const unsigned char *s1, const unsigned char *s2); void add_to_strn(unsigned char **s, unsigned char *a); void extend_str(unsigned char **s, int n); -void add_bytes_to_str(unsigned char **s, int *l, unsigned char *a, size_t ll); +size_t add_bytes_to_str(unsigned char **, size_t, unsigned char *, size_t); void add_to_str(unsigned char **s, int *l, unsigned char *a); void add_chr_to_str(unsigned char **s, int *l, unsigned char a); void add_unsigned_num_to_str(unsigned char **s, int *l, off_t n); diff --git a/session.c b/session.c @@ -1409,7 +1409,7 @@ subst_file(unsigned char *prog, unsigned char *file, int cyg_subst) int p; for (p = 0; prog[p] && prog[p] != '%'; p++) ; - add_bytes_to_str(&n, &l, prog, p); + l = add_bytes_to_str(&n, l, prog, p); prog += p; if (*prog == '%') { if (cyg_subst) { @@ -3332,12 +3332,11 @@ create_session_info(int cp, unsigned char *url, unsigned char *framename, l1 = 0; i = NULL; - *ll = 0; - add_bytes_to_str(&i, ll, (unsigned char *)&cp, sizeof(int)); - add_bytes_to_str(&i, ll, (unsigned char *)&l, sizeof(int)); - add_bytes_to_str(&i, ll, (unsigned char *)&l1, sizeof(int)); - add_bytes_to_str(&i, ll, url, l); - add_bytes_to_str(&i, ll, framename, l1); + *ll = add_bytes_to_str(&i, 0, (unsigned char *)&cp, sizeof(int)); + *ll = add_bytes_to_str(&i, *ll, (unsigned char *)&l, sizeof(int)); + *ll = add_bytes_to_str(&i, *ll, (unsigned char *)&l1, sizeof(int)); + *ll = add_bytes_to_str(&i, *ll, url, l); + *ll = add_bytes_to_str(&i, *ll, framename, l1); return i; } diff --git a/string.c b/string.c @@ -94,34 +94,35 @@ extend_str(unsigned char **s, int n) *s = xrealloc(*s, l + n + 1); } -void -add_bytes_to_str(unsigned char **s, int *l, unsigned char *a, size_t ll) +/* returns: new text length excluding the 0 byte */ +size_t +add_bytes_to_str(unsigned char **s, size_t sl, unsigned char *a, size_t al) { unsigned char *p = *s; - size_t ol = *l; - - if (!ll) - return; + size_t ol = sl; - *l = *l + ll; - - p = xreallocarray(p, *l + 1, sizeof(unsigned char)); - p[*l] = 0; - memcpy(p + ol, a, ll); + if (al == 0) + return ol; + sl += al; + p = xreallocarray(p, sl + 1, sizeof(unsigned char)); + p[sl] = 0; + memcpy(p + ol, a, al); *s = p; + + return sl; } void add_to_str(unsigned char **s, int *l, unsigned char *a) { - add_bytes_to_str(s, l, a, strlen(cast_const_char a)); + *l = add_bytes_to_str(s, *l, a, strlen(cast_const_char a)); } void add_chr_to_str(unsigned char **s, int *l, unsigned char a) { - add_bytes_to_str(s, l, &a, 1); + *l = add_bytes_to_str(s, *l, &a, 1); } void @@ -150,7 +151,7 @@ d10: sn %= 10; } *p++ = '0' + sn; - add_bytes_to_str(s, l, a, p - a); + *l = add_bytes_to_str(s, *l, a, p - a); } else { snzprint(a, 64, n); add_to_str(s, l, a); diff --git a/types.c b/types.c @@ -1117,7 +1117,7 @@ get_content_type_by_extension(unsigned char *url) exxt = NULL; el = 0; add_to_str(&exxt, &el, cast_uchar "application/x-"); - add_bytes_to_str(&exxt, &el, ext, extl); + el = add_bytes_to_str(&exxt, el, ext, extl); foreach (struct list, l, ll, assoc.list_entry) { struct assoc *a = get_struct(l, struct assoc, head); if (is_in_list(a->ct, exxt, el)) diff --git a/url.c b/url.c @@ -414,8 +414,7 @@ free_u_ret_up: if (!data) data = u; r = NULL; - rl = 0; - add_bytes_to_str(&r, &rl, u, post_seq - u); + rl = add_bytes_to_str(&r, 0, u, post_seq - u); q = strlen((char *)data); if (q && (data[q - 1] == '&' || data[q - 1] == '?')) ; @@ -490,7 +489,7 @@ match: res = NULL; l = 0; add_to_str(&res, &l, cast_uchar patterns[i].result1); - add_bytes_to_str(&res, &l, id, id_end - id); + l = add_bytes_to_str(&res, l, id, id_end - id); add_to_str(&res, &l, cast_uchar patterns[i].result2); free(n); return res; @@ -582,8 +581,7 @@ insert_wd(unsigned char **up, unsigned char *cwd) } free(host); url = NULL; - url_l = 0; - add_bytes_to_str(&url, &url_l, u, 7); + url_l = add_bytes_to_str(&url, 0, u, 7); for (cw = cwd; *cw; cw++) { unsigned char c = *cw; if (c < ' ' || c == '%' || c >= 127) { @@ -1264,7 +1262,7 @@ next_host_elem: } } - add_bytes_to_str(&p, &pl, host, l); + pl = add_bytes_to_str(&p, pl, host, l); advance_host: if (l != len) { @@ -1297,8 +1295,7 @@ idn_encode_url(unsigned char *url, int decode) return NULL; p = NULL; - pl = 0; - add_bytes_to_str(&p, &pl, url, host - url); + pl = add_bytes_to_str(&p, 0, url, host - url); add_to_str(&p, &pl, h); add_to_str(&p, &pl, host + holen); free(h); diff --git a/view.c b/view.c @@ -1930,7 +1930,7 @@ bnd: bound_ptrs, (nbound_ptrs + ALLOC_GR) * sizeof(int)); } bound_ptrs[nbound_ptrs++] = *len; - add_bytes_to_str(data, len, bound, BL); + *len = add_bytes_to_str(data, *len, bound, BL); if (flg) break; add_to_str(data, len, @@ -2006,8 +2006,8 @@ bnd: goto error; } if (rd) - add_bytes_to_str(data, len, - buffer, rd); + *len = add_bytes_to_str( + data, *len, buffer, rd); } while (rd); EINTRLOOP(rs, close(fh)); } @@ -2143,7 +2143,7 @@ get_form_url(struct session *ses, struct f_data_c *f, struct form_control *form, else { add_to_str(&go, &l, cast_uchar "multipart/form-data; boundary="); - add_bytes_to_str(&go, &l, bound, BL); + l = add_bytes_to_str(&go, l, bound, BL); add_to_str(&go, &l, cast_uchar "\n"); } for (i = 0; i < len; i++) { @@ -2179,9 +2179,9 @@ get_link_url(struct session *ses, struct f_data_c *f, struct link *l, + strlen(cast_const_char l->where) - 4), "?0,0")) { unsigned char *nu = NULL; - int ll = 0; - add_bytes_to_str(&nu, &ll, l->where, - strlen(cast_const_char l->where) - 3); + int ll = add_bytes_to_str( + &nu, 0, l->where, + strlen(cast_const_char l->where) - 3); add_num_to_str(&nu, &ll, ismap_x); add_chr_to_str(&nu, &ll, ','); add_num_to_str(&nu, &ll, ismap_y);