From 6c475b0d66dff5ba9c12b3d1a3bbb16816ce3e2b Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Thu, 21 Jun 2007 10:37:40 +0200 Subject: [PATCH] libucw: added several const qualifiers --- lib/alloc_str.c | 4 +- lib/base224.c | 4 +- lib/base224.h | 4 +- lib/base64.c | 12 +-- lib/base64.h | 4 +- lib/bbuf.c | 8 +- lib/bbuf.h | 8 +- lib/carefulio.c | 4 +- lib/conf-parse.c | 10 +-- lib/conf.h | 8 +- lib/ctmatch.c | 2 +- lib/lib.h | 56 +++++++------- lib/log-file.c | 2 +- lib/log.c | 10 +-- lib/md5hex.c | 4 +- lib/mempool-str.c | 4 +- lib/mempool.h | 6 +- lib/mmap.c | 2 +- lib/patmatch.h | 2 +- lib/proctitle.c | 2 +- lib/regex.c | 8 +- lib/runcmd.c | 16 ++-- lib/stkstring.c | 2 +- lib/stkstring.h | 12 +-- lib/string.c | 2 +- lib/sync.c | 2 +- lib/unaligned.h | 178 ++++++++++++++++++++++++--------------------- lib/unicode-utf8.c | 8 +- lib/unicode.h | 10 +-- lib/url.c | 24 +++--- lib/url.h | 18 ++--- lib/wildmatch.c | 6 +- lib/wildmatch.h | 6 +- 33 files changed, 230 insertions(+), 218 deletions(-) diff --git a/lib/alloc_str.c b/lib/alloc_str.c index ab7b4672..05e803c7 100644 --- a/lib/alloc_str.c +++ b/lib/alloc_str.c @@ -11,8 +11,8 @@ #include -byte * -xstrdup(byte *s) +char * +xstrdup(const char *s) { uns l = strlen(s) + 1; return memcpy(xmalloc(l), s, l); diff --git a/lib/base224.c b/lib/base224.c index 70e25bf3..3d624717 100644 --- a/lib/base224.c +++ b/lib/base224.c @@ -59,7 +59,7 @@ encode_block(byte *w, u32 hi, u32 lo) } uns -base224_encode(byte *dest, byte *src, uns len) +base224_encode(byte *dest, const byte *src, uns len) { u32 lo=0, hi=0; /* 64-bit buffer accumulating input bits */ uns i=0; /* How many source bits do we have buffered */ @@ -96,7 +96,7 @@ base224_encode(byte *dest, byte *src, uns len) } uns -base224_decode(byte *dest, byte *src, uns len) +base224_decode(byte *dest, const byte *src, uns len) { u32 hi=0, lo=0; /* 64-bit buffer accumulating output bits */ uns i=0; /* How many bits do we have accumulated */ diff --git a/lib/base224.h b/lib/base224.h index 7ae89739..7e815d8c 100644 --- a/lib/base224.h +++ b/lib/base224.h @@ -7,8 +7,8 @@ * of the GNU Lesser General Public License. */ -uns base224_encode(byte *dest, byte *src, uns len); -uns base224_decode(byte *dest, byte *src, uns len); +uns base224_encode(byte *dest, const byte *src, uns len); +uns base224_decode(byte *dest, const byte *src, uns len); /* * Warning: when encoding, at least 4 bytes of extra space are needed. diff --git a/lib/base64.c b/lib/base64.c index 0f1b0144..ef8faa45 100644 --- a/lib/base64.c +++ b/lib/base64.c @@ -14,19 +14,19 @@ #include -static byte base64_table[] = +static const byte base64_table[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0' }; -static byte base64_pad = '='; +static const byte base64_pad = '='; uns -base64_encode(byte *dest, byte *src, uns len) +base64_encode(byte *dest, const byte *src, uns len) { - byte *current = src; + const byte *current = src; uns i = 0; while (len > 2) { /* keep going until we have less than 24 bits */ @@ -58,9 +58,9 @@ base64_encode(byte *dest, byte *src, uns len) /* as above, but backwards. :) */ uns -base64_decode(byte *dest, byte *src, uns len) +base64_decode(byte *dest, const byte *src, uns len) { - byte *current = src; + const byte *current = src; uns ch; uns i = 0, j = 0; static byte reverse_table[256]; diff --git a/lib/base64.h b/lib/base64.h index 240ffe0c..78909664 100644 --- a/lib/base64.h +++ b/lib/base64.h @@ -7,8 +7,8 @@ * of the GNU Lesser General Public License. */ -uns base64_encode(byte *dest, byte *src, uns len); -uns base64_decode(byte *dest, byte *src, uns len); +uns base64_encode(byte *dest, const byte *src, uns len); +uns base64_decode(byte *dest, const byte *src, uns len); /* * Use this macro to calculate buffer size. diff --git a/lib/bbuf.c b/lib/bbuf.c index fae58700..9d4af26f 100644 --- a/lib/bbuf.c +++ b/lib/bbuf.c @@ -13,7 +13,7 @@ #include char * -bb_vprintf_at(bb_t *bb, uns ofs, char *fmt, va_list args) +bb_vprintf_at(bb_t *bb, uns ofs, const char *fmt, va_list args) { bb_grow(bb, ofs + 1); va_list args2; @@ -44,7 +44,7 @@ bb_vprintf_at(bb_t *bb, uns ofs, char *fmt, va_list args) } char * -bb_printf_at(bb_t *bb, uns ofs, char *fmt, ...) +bb_printf_at(bb_t *bb, uns ofs, const char *fmt, ...) { va_list args; va_start(args, fmt); @@ -54,13 +54,13 @@ bb_printf_at(bb_t *bb, uns ofs, char *fmt, ...) } char * -bb_vprintf(bb_t *bb, char *fmt, va_list args) +bb_vprintf(bb_t *bb, const char *fmt, va_list args) { return bb_vprintf_at(bb, 0, fmt, args); } char * -bb_printf(bb_t *bb, char *fmt, ...) +bb_printf(bb_t *bb, const char *fmt, ...) { va_list args; va_start(args, fmt); diff --git a/lib/bbuf.h b/lib/bbuf.h index 2585bec3..22e62bb0 100644 --- a/lib/bbuf.h +++ b/lib/bbuf.h @@ -14,9 +14,9 @@ #define GBUF_PREFIX(x) bb_##x #include "lib/gbuf.h" -char *bb_vprintf(bb_t *bb, char *fmt, va_list args); -char *bb_printf(bb_t *bb, char *fmt, ...); -char *bb_vprintf_at(bb_t *bb, uns ofs, char *fmt, va_list args); -char *bb_printf_at(bb_t *bb, uns ofs, char *fmt, ...); +char *bb_vprintf(bb_t *bb, const char *fmt, va_list args); +char *bb_printf(bb_t *bb, const char *fmt, ...); +char *bb_vprintf_at(bb_t *bb, uns ofs, const char *fmt, va_list args); +char *bb_printf_at(bb_t *bb, uns ofs, const char *fmt, ...); #endif diff --git a/lib/carefulio.c b/lib/carefulio.c index 7bfbc784..b8d865d4 100644 --- a/lib/carefulio.c +++ b/lib/carefulio.c @@ -34,9 +34,9 @@ careful_read(int fd, void *buf, int len) } int -careful_write(int fd, void *buf, int len) +careful_write(int fd, const void *buf, int len) { - byte *pos = buf; + const byte *pos = buf; while (len) { int l = write(fd, pos, len); diff --git a/lib/conf-parse.c b/lib/conf-parse.c index 9a82f9c9..2d93cc4c 100644 --- a/lib/conf-parse.c +++ b/lib/conf-parse.c @@ -35,7 +35,7 @@ static const struct unit units[] = { }; static const struct unit * -lookup_unit(byte *value, byte *end, byte **msg) +lookup_unit(const byte *value, const byte *end, byte **msg) { if (end && *end) { if (end == value || end[1] || *end >= '0' && *end <= '9') @@ -53,7 +53,7 @@ lookup_unit(byte *value, byte *end, byte **msg) static char cf_rngerr[] = "Number out of range"; byte * -cf_parse_int(byte *str, int *ptr) +cf_parse_int(const byte *str, int *ptr) { byte *msg = NULL; if (!*str) @@ -82,7 +82,7 @@ cf_parse_int(byte *str, int *ptr) } byte * -cf_parse_u64(byte *str, u64 *ptr) +cf_parse_u64(const byte *str, u64 *ptr) { byte *msg = NULL; if (!*str) @@ -111,7 +111,7 @@ cf_parse_u64(byte *str, u64 *ptr) } byte * -cf_parse_double(byte *str, double *ptr) +cf_parse_double(const byte *str, double *ptr) { byte *msg = NULL; if (!*str) @@ -131,7 +131,7 @@ cf_parse_double(byte *str, double *ptr) } byte * -cf_parse_ip(byte *p, u32 *varp) +cf_parse_ip(const byte *p, u32 *varp) { if (!*p) return "Missing IP address"; diff --git a/lib/conf.h b/lib/conf.h index c56cea48..7f176103 100644 --- a/lib/conf.h +++ b/lib/conf.h @@ -154,10 +154,10 @@ void cf_declare_section(byte *name, struct cf_section *sec, uns allow_unknown); void cf_init_section(byte *name, struct cf_section *sec, void *ptr, uns do_bzero); /* Parsers for basic types: conf-parse.c */ -byte *cf_parse_int(byte *str, int *ptr); -byte *cf_parse_u64(byte *str, u64 *ptr); -byte *cf_parse_double(byte *str, double *ptr); -byte *cf_parse_ip(byte *p, u32 *varp); +byte *cf_parse_int(const byte *str, int *ptr); +byte *cf_parse_u64(const byte *str, u64 *ptr); +byte *cf_parse_double(const byte *str, double *ptr); +byte *cf_parse_ip(const byte *p, u32 *varp); #endif diff --git a/lib/ctmatch.c b/lib/ctmatch.c index 59799070..7e807760 100644 --- a/lib/ctmatch.c +++ b/lib/ctmatch.c @@ -11,7 +11,7 @@ #include "lib/chartype.h" int -match_ct_patt(byte *p, byte *t) +match_ct_patt(const char *p, const char *t) { if (*p == '*' && !p[1]) /* "*" matches everything */ return 1; diff --git a/lib/lib.h b/lib/lib.h index bad9f30d..2230a8cd 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -99,16 +99,16 @@ extern void (*log_die_hook)(void); struct tm; extern void (*log_switch_hook)(struct tm *tm); -void log_msg(unsigned int cat, const char *msg, ...) FORMAT_CHECK(printf,2,3); +void log_msg(uns cat, const char *msg, ...) FORMAT_CHECK(printf,2,3); #define log log_msg -void vlog_msg(unsigned int cat, const char *msg, va_list args); +void vlog_msg(uns cat, const char *msg, va_list args); void die(const char *, ...) NONRET FORMAT_CHECK(printf,1,2); -void log_init(byte *argv0); -void log_file(byte *name); +void log_init(const char *argv0); +void log_file(const char *name); void log_fork(void); int log_switch(void); -void assert_failed(char *assertion, char *file, int line) NONRET; +void assert_failed(const char *assertion, const char *file, int line) NONRET; void assert_failed_noinfo(void) NONRET; #ifdef DEBUG_ASSERTS @@ -151,17 +151,17 @@ static inline void log_switch_enable(void) { ASSERT(log_switch_nest); log_switch * their own xmalloc and we don't want to interfere with them, hence * the renaming. */ -void *xmalloc(unsigned) LIKE_MALLOC; -void *xrealloc(void *, unsigned); +void *xmalloc(uns) LIKE_MALLOC; +void *xrealloc(void *, uns); void xfree(void *); #endif -void *xmalloc_zero(unsigned) LIKE_MALLOC; -byte *xstrdup(byte *) LIKE_MALLOC; +void *xmalloc_zero(uns) LIKE_MALLOC; +char *xstrdup(const char *) LIKE_MALLOC; /* Content-Type pattern matching and filters */ -int match_ct_patt(byte *, byte *); +int match_ct_patt(const char *, const char *); /* wordsplit.c */ @@ -170,13 +170,13 @@ int wordsplit(byte *, byte **, uns); /* pat(i)match.c: Matching of shell patterns */ -int match_pattern(byte *, byte *); -int match_pattern_nocase(byte *, byte *); +int match_pattern(const char *, const char *); +int match_pattern_nocase(const char *, const char *); /* md5hex.c */ -void md5_to_hex(byte *, byte *); -void hex_to_md5(byte *, byte *); +void md5_to_hex(const byte *, byte *); +void hex_to_md5(const byte *, byte *); #define MD5_SIZE 16 #define MD5_HEX_SIZE 33 @@ -203,10 +203,10 @@ void get_last_timeval(struct timeval *tv); typedef struct regex regex; -regex *rx_compile(byte *r, int icase); +regex *rx_compile(const byte *r, int icase); void rx_free(regex *r); -int rx_match(regex *r, byte *s); -int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen); +int rx_match(regex *r, const byte *s); +int rx_subst(regex *r, const byte *by, const byte *src, byte *dest, uns destlen); /* random.c */ @@ -217,13 +217,13 @@ u64 random_max_u64(u64 max); /* mmap.c */ -void *mmap_file(byte *name, unsigned *len, int writeable); +void *mmap_file(const byte *name, unsigned *len, int writeable); void munmap_file(void *start, unsigned len); /* proctitle.c */ void setproctitle_init(int argc, char **argv); -void setproctitle(char *msg, ...) FORMAT_CHECK(printf,1,2); +void setproctitle(const char *msg, ...) FORMAT_CHECK(printf,1,2); char *getproctitle(void); /* randomkey.c */ @@ -237,21 +237,21 @@ int format_exit_status(byte *msg, int stat); /* runcmd.c */ -int run_command(byte *cmd, ...); -void NONRET exec_command(byte *cmd, ...); -void echo_command(byte *buf, int size, byte *cmd, ...); -int run_command_v(byte *cmd, va_list args); -void NONRET exec_command_v(byte *cmd, va_list args); -void echo_command_v(byte *buf, int size, byte *cmd, va_list args); +int run_command(const byte *cmd, ...); +void NONRET exec_command(const byte *cmd, ...); +void echo_command(byte *buf, int size, const byte *cmd, ...); +int run_command_v(const byte *cmd, va_list args); +void NONRET exec_command_v(const byte *cmd, va_list args); +void echo_command_v(byte *buf, int size, const byte *cmd, va_list args); /* carefulio.c */ int careful_read(int fd, void *buf, int len); -int careful_write(int fd, void *buf, int len); +int careful_write(int fd, const void *buf, int len); /* sync.c */ -void sync_dir(byte *name); +void sync_dir(const byte *name); /* sighandler.c */ @@ -263,7 +263,7 @@ sh_sighandler_t set_signal_handler(int signum, sh_sighandler_t new); /* string.c */ -byte *str_unesc(byte *dest, byte *src); +byte *str_unesc(byte *dest, const byte *src); byte *str_format_flags(byte *dest, const byte *fmt, uns flags); /* bigalloc.c */ diff --git a/lib/log-file.c b/lib/log-file.c index f880dc1d..401e797c 100644 --- a/lib/log-file.c +++ b/lib/log-file.c @@ -67,7 +67,7 @@ internal_log_switch(struct tm *tm) } void -log_file(byte *name) +log_file(const char *name) { if (name) { diff --git a/lib/log.c b/lib/log.c index 713fa768..8f56f06f 100644 --- a/lib/log.c +++ b/lib/log.c @@ -114,7 +114,7 @@ die(const char *msg, ...) } void -assert_failed(char *assertion, char *file, int line) +assert_failed(const char *assertion, const char *file, int line) { log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line); abort(); @@ -126,10 +126,10 @@ assert_failed_noinfo(void) die("Internal error: Assertion failed."); } -static byte * -log_basename(byte *n) +static const char * +log_basename(const char *n) { - byte *p = n; + const char *p = n; while (*n) if (*n++ == '/') @@ -138,7 +138,7 @@ log_basename(byte *n) } void -log_init(byte *argv0) +log_init(const char *argv0) { if (argv0) { diff --git a/lib/md5hex.c b/lib/md5hex.c index b9eda391..5f2e8ebb 100644 --- a/lib/md5hex.c +++ b/lib/md5hex.c @@ -13,7 +13,7 @@ #include void -md5_to_hex(byte *s, byte *d) +md5_to_hex(const byte *s, byte *d) { int i; for(i=0; i char * -mp_strdup(struct mempool *p, char *s) +mp_strdup(struct mempool *p, const char *s) { uns l = strlen(s) + 1; char *t = mp_alloc_fast_noalign(p, l); @@ -23,7 +23,7 @@ mp_strdup(struct mempool *p, char *s) } void * -mp_memdup(struct mempool *p, void *s, uns len) +mp_memdup(struct mempool *p, const void *s, uns len) { void *t = mp_alloc_fast(p, len); memcpy(t, s, len); diff --git a/lib/mempool.h b/lib/mempool.h index 4575d871..c53423ab 100644 --- a/lib/mempool.h +++ b/lib/mempool.h @@ -274,11 +274,11 @@ void mp_pop(struct mempool *pool); /*** mempool-str.c ***/ -char *mp_strdup(struct mempool *, char *) LIKE_MALLOC; -void *mp_memdup(struct mempool *, void *, uns) LIKE_MALLOC; +char *mp_strdup(struct mempool *, const char *) LIKE_MALLOC; +void *mp_memdup(struct mempool *, const void *, uns) LIKE_MALLOC; char *mp_multicat(struct mempool *, ...) LIKE_MALLOC SENTINEL_CHECK; static inline char * LIKE_MALLOC -mp_strcat(struct mempool *mp, char *x, char *y) +mp_strcat(struct mempool *mp, const char *x, const char *y) { return mp_multicat(mp, x, y, NULL); } diff --git a/lib/mmap.c b/lib/mmap.c index aa636222..e2ea6ff6 100644 --- a/lib/mmap.c +++ b/lib/mmap.c @@ -16,7 +16,7 @@ #include void * -mmap_file(byte *name, unsigned *len, int writeable) +mmap_file(const byte *name, unsigned *len, int writeable) { int fd = open(name, writeable ? O_RDWR : O_RDONLY); struct stat st; diff --git a/lib/patmatch.h b/lib/patmatch.h index 817c92da..a47669f9 100644 --- a/lib/patmatch.h +++ b/lib/patmatch.h @@ -8,7 +8,7 @@ */ int -MATCH_FUNC_NAME(byte *p, byte *s) +MATCH_FUNC_NAME(const char *p, const char *s) { while (*p) { diff --git a/lib/proctitle.c b/lib/proctitle.c index f605aef1..4e154184 100644 --- a/lib/proctitle.c +++ b/lib/proctitle.c @@ -54,7 +54,7 @@ setproctitle_init(int argc, char **argv) } void -setproctitle(char *msg, ...) +setproctitle(const char *msg, ...) { va_list args; byte buf[256]; diff --git a/lib/regex.c b/lib/regex.c index 2ff21676..adff133e 100644 --- a/lib/regex.c +++ b/lib/regex.c @@ -31,7 +31,7 @@ struct regex { }; regex * -rx_compile(byte *p, int icase) +rx_compile(const byte *p, int icase) { regex *r = xmalloc_zero(sizeof(regex)); @@ -54,7 +54,7 @@ rx_free(regex *r) } int -rx_match(regex *r, byte *s) +rx_match(regex *r, const byte *s) { int err = regexec(&r->rx, s, 10, r->matches, 0); if (!err) @@ -71,7 +71,7 @@ rx_match(regex *r, byte *s) } int -rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen) +rx_subst(regex *r, const byte *by, const byte *src, byte *dest, uns destlen) { byte *end = dest + destlen - 1; @@ -88,7 +88,7 @@ rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen) uns j = *by++ - '0'; if (j <= r->rx.re_nsub && r->matches[j].rm_so >= 0) { - byte *s = src + r->matches[j].rm_so; + const byte *s = src + r->matches[j].rm_so; uns i = r->matches[j].rm_eo - r->matches[j].rm_so; if (dest + i >= end) return -1; diff --git a/lib/runcmd.c b/lib/runcmd.c index 7f373877..de32b9b6 100644 --- a/lib/runcmd.c +++ b/lib/runcmd.c @@ -16,7 +16,7 @@ #include void NONRET -exec_command_v(byte *cmd, va_list args) +exec_command_v(const byte *cmd, va_list args) { va_list cargs; va_copy(cargs, args); @@ -26,7 +26,7 @@ exec_command_v(byte *cmd, va_list args) cnt++; va_end(cargs); char **argv = alloca(sizeof(byte *) * cnt); - argv[0] = cmd; + argv[0] = (char *)cmd; cnt = 1; va_copy(cargs, args); while (arg = va_arg(cargs, byte *)) @@ -41,7 +41,7 @@ exec_command_v(byte *cmd, va_list args) } int -run_command_v(byte *cmd, va_list args) +run_command_v(const byte *cmd, va_list args) { pid_t p = fork(); if (p < 0) @@ -70,11 +70,11 @@ run_command_v(byte *cmd, va_list args) } void -echo_command_v(byte *buf, int size, byte *cmd, va_list args) +echo_command_v(byte *buf, int size, const byte *cmd, va_list args) { byte *limit = buf + size - 4; byte *p = buf; - byte *arg = cmd; + const byte *arg = cmd; do { int l = strlen(arg); @@ -94,7 +94,7 @@ echo_command_v(byte *buf, int size, byte *cmd, va_list args) } int -run_command(byte *cmd, ...) +run_command(const byte *cmd, ...) { va_list args; va_start(args, cmd); @@ -104,7 +104,7 @@ run_command(byte *cmd, ...) } void NONRET -exec_command(byte *cmd, ...) +exec_command(const byte *cmd, ...) { va_list args; va_start(args, cmd); @@ -112,7 +112,7 @@ exec_command(byte *cmd, ...) } void -echo_command(byte *buf, int len, byte *cmd, ...) +echo_command(byte *buf, int len, const byte *cmd, ...) { va_list args; va_start(args, cmd); diff --git a/lib/stkstring.c b/lib/stkstring.c index f9ba323d..19c3a5dd 100644 --- a/lib/stkstring.c +++ b/lib/stkstring.c @@ -73,7 +73,7 @@ stk_vprintf_internal(const char *fmt, va_list args) } void -stk_hexdump_internal(char *dst, byte *src, uns n) +stk_hexdump_internal(char *dst, const byte *src, uns n) { for (uns i=0; i #include -#define stk_strdup(s) ({ char *_s=(s); uns _l=strlen(_s)+1; char *_x=alloca(_l); memcpy(_x, _s, _l); _x; }) -#define stk_strndup(s,n) ({ char *_s=(s); uns _l=strnlen(_s,(n)); char *_x=alloca(_l+1); memcpy(_x, _s, _l); _x[_l]=0; _x; }) -#define stk_strcat(s1,s2) ({ char *_s1=(s1); char *_s2=(s2); uns _l1=strlen(_s1); uns _l2=strlen(_s2); char *_x=alloca(_l1+_l2+1); memcpy(_x,_s1,_l1); memcpy(_x+_l1,_s2,_l2+1); _x; }) -#define stk_strmulticat(s...) ({ char *_s[]={s}; char *_x=alloca(stk_array_len(_s, ARRAY_SIZE(_s)-1)); stk_array_join(_x, _s, ARRAY_SIZE(_s)-1, 0); _x; }) +#define stk_strdup(s) ({ const char *_s=(s); uns _l=strlen(_s)+1; char *_x=alloca(_l); memcpy(_x, _s, _l); _x; }) +#define stk_strndup(s,n) ({ const char *_s=(s); uns _l=strnlen(_s,(n)); char *_x=alloca(_l+1); memcpy(_x, _s, _l); _x[_l]=0; _x; }) +#define stk_strcat(s1,s2) ({ const char *_s1=(s1); const char *_s2=(s2); uns _l1=strlen(_s1); uns _l2=strlen(_s2); char *_x=alloca(_l1+_l2+1); memcpy(_x,_s1,_l1); memcpy(_x+_l1,_s2,_l2+1); _x; }) +#define stk_strmulticat(s...) ({ const char *_s[]={s}; char *_x=alloca(stk_array_len(_s, ARRAY_SIZE(_s)-1)); stk_array_join(_x, _s, ARRAY_SIZE(_s)-1, 0); _x; }) #define stk_strarraycat(s,n) ({ char **_s=(s); int _n=(n); char *_x=alloca(stk_array_len(_s,_n)); stk_array_join(_x, _s, _n, 0); _x; }) #define stk_strjoin(s,n,sep) ({ char **_s=(s); int _n=(n); char *_x=alloca(stk_array_len(_s,_n)+_n-1); stk_array_join(_x, _s, _n, (sep)); _x; }) #define stk_printf(f...) ({ uns _l=stk_printf_internal(f); char *_x=alloca(_l); sprintf(_x, f); _x; }) #define stk_vprintf(f, args) ({ uns _l=stk_vprintf_internal(f, args); char *_x=alloca(_l); vsprintf(_x, f, args); _x; }) #define stk_hexdump(s,n) ({ uns _n=(n); char *_x=alloca(3*_n+1); stk_hexdump_internal(_x,(byte*)(s),_n); _x; }) -#define stk_str_unesc(s) ({ byte *_s=(s); byte *_d=alloca(strlen(_s)+1); str_unesc(_d, _s); _d; }) +#define stk_str_unesc(s) ({ const byte *_s=(s); byte *_d=alloca(strlen(_s)+1); str_unesc(_d, _s); _d; }) uns stk_array_len(char **s, uns cnt); void stk_array_join(char *x, char **s, uns cnt, uns sep); uns stk_printf_internal(const char *x, ...) FORMAT_CHECK(printf,1,2); uns stk_vprintf_internal(const char *x, va_list args); -void stk_hexdump_internal(char *dst, byte *src, uns n); +void stk_hexdump_internal(char *dst, const byte *src, uns n); diff --git a/lib/string.c b/lib/string.c index 3f4c045f..3bc231a6 100644 --- a/lib/string.c +++ b/lib/string.c @@ -17,7 +17,7 @@ /* Expands C99-like escape sequences. * It is safe to use the same buffer for both input and output. */ byte * -str_unesc(byte *d, byte *s) +str_unesc(byte *d, const byte *s) { while (*s) { diff --git a/lib/sync.c b/lib/sync.c index 1b3a0d15..5faada81 100644 --- a/lib/sync.c +++ b/lib/sync.c @@ -10,7 +10,7 @@ #include void -sync_dir(byte *name) +sync_dir(const byte *name) { int fd = open(name, O_RDONLY #ifdef CONFIG_LINUX diff --git a/lib/unaligned.h b/lib/unaligned.h index 115ed073..62a6643d 100644 --- a/lib/unaligned.h +++ b/lib/unaligned.h @@ -13,150 +13,162 @@ /* Big endian format */ #if defined(CPU_ALLOW_UNALIGNED) && defined(CPU_BIG_ENDIAN) -static inline uns get_u16_be(byte *p) { return *(u16 *)p; } -static inline u32 get_u32_be(byte *p) { return *(u32 *)p; } -static inline u64 get_u64_be(byte *p) { return *(u64 *)p; } -static inline void put_u16_be(byte *p, uns x) { *(u16 *)p = x; } -static inline void put_u32_be(byte *p, u32 x) { *(u32 *)p = x; } -static inline void put_u64_be(byte *p, u64 x) { *(u64 *)p = x; } +static inline uns get_u16_be(const void *p) { return *(u16 *)p; } +static inline u32 get_u32_be(const void *p) { return *(u32 *)p; } +static inline u64 get_u64_be(const void *p) { return *(u64 *)p; } +static inline void put_u16_be(void *p, uns x) { *(u16 *)p = x; } +static inline void put_u32_be(void *p, u32 x) { *(u32 *)p = x; } +static inline void put_u64_be(void *p, u64 x) { *(u64 *)p = x; } #else -static inline uns get_u16_be(byte *p) +static inline uns get_u16_be(const void *p) { - return (p[0] << 8) | p[1]; + const byte *c = p; + return (c[0] << 8) | c[1]; } -static inline u32 get_u32_be(byte *p) +static inline u32 get_u32_be(const void *p) { - return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; + const byte *c = p; + return (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]; } -static inline u64 get_u64_be(byte *p) +static inline u64 get_u64_be(const void *p) { - return ((u64) get_u32_be(p) << 32) | get_u32_be(p+4); + return ((u64) get_u32_be(p) << 32) | get_u32_be((const byte *)p+4); } -static inline void put_u16_be(byte *p, uns x) +static inline void put_u16_be(void *p, uns x) { - p[0] = x >> 8; - p[1] = x; + byte *c = p; + c[0] = x >> 8; + c[1] = x; } -static inline void put_u32_be(byte *p, u32 x) +static inline void put_u32_be(void *p, u32 x) { - p[0] = x >> 24; - p[1] = x >> 16; - p[2] = x >> 8; - p[3] = x; + byte *c = p; + c[0] = x >> 24; + c[1] = x >> 16; + c[2] = x >> 8; + c[3] = x; } -static inline void put_u64_be(byte *p, u64 x) +static inline void put_u64_be(void *p, u64 x) { put_u32_be(p, x >> 32); - put_u32_be(p+4, x); + put_u32_be((byte *)p+4, x); } #endif /* Little-endian format */ #if defined(CPU_ALLOW_UNALIGNED) && !defined(CPU_BIG_ENDIAN) -static inline uns get_u16_le(byte *p) { return *(u16 *)p; } -static inline u32 get_u32_le(byte *p) { return *(u32 *)p; } -static inline u64 get_u64_le(byte *p) { return *(u64 *)p; } -static inline void put_u16_le(byte *p, uns x) { *(u16 *)p = x; } -static inline void put_u32_le(byte *p, u32 x) { *(u32 *)p = x; } -static inline void put_u64_le(byte *p, u64 x) { *(u64 *)p = x; } +static inline uns get_u16_le(const void *p) { return *(u16 *)p; } +static inline u32 get_u32_le(const void *p) { return *(u32 *)p; } +static inline u64 get_u64_le(const void *p) { return *(u64 *)p; } +static inline void put_u16_le(void *p, uns x) { *(u16 *)p = x; } +static inline void put_u32_le(void *p, u32 x) { *(u32 *)p = x; } +static inline void put_u64_le(void *p, u64 x) { *(u64 *)p = x; } #else -static inline uns get_u16_le(byte *p) +static inline uns get_u16_le(const void *p) { - return p[0] | (p[1] << 8); + const byte *c = p; + return c[0] | (c[1] << 8); } -static inline u32 get_u32_le(byte *p) +static inline u32 get_u32_le(const void *p) { - return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); + const byte *c = p; + return c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24); } -static inline u64 get_u64_le(byte *p) +static inline u64 get_u64_le(const void *p) { - return get_u32_le(p) | ((u64) get_u32_le(p+4) << 32); + return get_u32_le(p) | ((u64) get_u32_le((const byte *)p+4) << 32); } -static inline void put_u16_le(byte *p, uns x) +static inline void put_u16_le(void *p, uns x) { - p[0] = x; - p[1] = x >> 8; + byte *c = p; + c[0] = x; + c[1] = x >> 8; } -static inline void put_u32_le(byte *p, u32 x) +static inline void put_u32_le(void *p, u32 x) { - p[0] = x; - p[1] = x >> 8; - p[2] = x >> 16; - p[3] = x >> 24; + byte *c = p; + c[0] = x; + c[1] = x >> 8; + c[2] = x >> 16; + c[3] = x >> 24; } -static inline void put_u64_le(byte *p, u64 x) +static inline void put_u64_le(void *p, u64 x) { put_u32_le(p, x); - put_u32_le(p+4, x >> 32); + put_u32_le((byte *)p+4, x >> 32); } #endif -static inline u64 get_u40_be(byte *p) +static inline u64 get_u40_be(const void *p) { - return ((u64)p[0] << 32) | get_u32_be(p+1); + const byte *c = p; + return ((u64)c[0] << 32) | get_u32_be(c+1); } -static inline void put_u40_be(byte *p, u64 x) +static inline void put_u40_be(void *p, u64 x) { - p[0] = x >> 32; - put_u32_be(p+1, x); + byte *c = p; + c[0] = x >> 32; + put_u32_be(c+1, x); } -static inline u64 get_u40_le(byte *p) +static inline u64 get_u40_le(const void *p) { - return get_u32_le(p) | ((u64) p[4] << 32); + const byte *c = p; + return get_u32_le(c) | ((u64) c[4] << 32); } -static inline void put_u40_le(byte *p, u64 x) +static inline void put_u40_le(void *p, u64 x) { - put_u32_le(p, x); - p[4] = x >> 32; + byte *c = p; + put_u32_le(c, x); + c[4] = x >> 32; } /* The native format */ #ifdef CPU_BIG_ENDIAN -static inline uns get_u16(byte *p) { return get_u16_be(p); } -static inline u32 get_u32(byte *p) { return get_u32_be(p); } -static inline u64 get_u64(byte *p) { return get_u64_be(p); } -static inline u64 get_u40(byte *p) { return get_u40_be(p); } -static inline void put_u16(byte *p, uns x) { return put_u16_be(p, x); } -static inline void put_u32(byte *p, u32 x) { return put_u32_be(p, x); } -static inline void put_u64(byte *p, u64 x) { return put_u64_be(p, x); } -static inline void put_u40(byte *p, u64 x) { return put_u40_be(p, x); } +static inline uns get_u16(const void *p) { return get_u16_be(p); } +static inline u32 get_u32(const void *p) { return get_u32_be(p); } +static inline u64 get_u64(const void *p) { return get_u64_be(p); } +static inline u64 get_u40(const void *p) { return get_u40_be(p); } +static inline void put_u16(void *p, uns x) { return put_u16_be(p, x); } +static inline void put_u32(void *p, u32 x) { return put_u32_be(p, x); } +static inline void put_u64(void *p, u64 x) { return put_u64_be(p, x); } +static inline void put_u40(void *p, u64 x) { return put_u40_be(p, x); } #else -static inline uns get_u16(byte *p) { return get_u16_le(p); } -static inline u32 get_u32(byte *p) { return get_u32_le(p); } -static inline u64 get_u64(byte *p) { return get_u64_le(p); } -static inline u64 get_u40(byte *p) { return get_u40_le(p); } -static inline void put_u16(byte *p, uns x) { return put_u16_le(p, x); } -static inline void put_u32(byte *p, u32 x) { return put_u32_le(p, x); } -static inline void put_u64(byte *p, u64 x) { return put_u64_le(p, x); } -static inline void put_u40(byte *p, u64 x) { return put_u40_le(p, x); } +static inline uns get_u16(const void *p) { return get_u16_le(p); } +static inline u32 get_u32(const void *p) { return get_u32_le(p); } +static inline u64 get_u64(const void *p) { return get_u64_le(p); } +static inline u64 get_u40(const void *p) { return get_u40_le(p); } +static inline void put_u16(void *p, uns x) { return put_u16_le(p, x); } +static inline void put_u32(void *p, u32 x) { return put_u32_le(p, x); } +static inline void put_u64(void *p, u64 x) { return put_u64_le(p, x); } +static inline void put_u40(void *p, u64 x) { return put_u40_le(p, x); } #endif /* Just for completeness */ -static inline uns get_u8(byte *p) { return *p; } -static inline void put_u8(byte *p, uns x) { *p = x; } +static inline uns get_u8(const void *p) { return *(const byte *)p; } +static inline void put_u8(void *p, uns x) { *(byte *)p = x; } /* Backward compatibility macros */ -#define GET_U8(p) get_u8((byte*)(p)) -#define GET_U16(p) get_u16((byte*)(p)) -#define GET_U32(p) get_u32((byte*)(p)) -#define GET_U64(p) get_u64((byte*)(p)) -#define GET_U40(p) get_u40((byte*)(p)) - -#define PUT_U8(p,x) put_u8((byte*)(p),x); -#define PUT_U16(p,x) put_u16((byte*)(p),x) -#define PUT_U32(p,x) put_u32((byte*)p,x) -#define PUT_U64(p,x) put_u64((byte*)p,x) -#define PUT_U40(p,x) put_u40((byte*)p,x) +#define GET_U8(p) get_u8(p) +#define GET_U16(p) get_u16(p) +#define GET_U32(p) get_u32(p) +#define GET_U64(p) get_u64(p) +#define GET_U40(p) get_u40(p) + +#define PUT_U8(p,x) put_u8(p,x); +#define PUT_U16(p,x) put_u16(p,x) +#define PUT_U32(p,x) put_u32(p,x) +#define PUT_U64(p,x) put_u64(p,x) +#define PUT_U40(p,x) put_u40(p,x) #endif diff --git a/lib/unicode-utf8.c b/lib/unicode-utf8.c index 8c8ac1bd..a52e553c 100644 --- a/lib/unicode-utf8.c +++ b/lib/unicode-utf8.c @@ -12,7 +12,7 @@ #include "lib/unicode.h" uns -utf8_strlen(byte *str) +utf8_strlen(const byte *str) { uns len = 0; while (*str) @@ -24,10 +24,10 @@ utf8_strlen(byte *str) } uns -utf8_strnlen(byte *str, uns n) +utf8_strnlen(const byte *str, uns n) { uns len = 0; - byte *end = str + n; + const byte *end = str + n; while (str < end) { UTF8_SKIP(str); @@ -37,7 +37,7 @@ utf8_strnlen(byte *str, uns n) } uns -utf8_check(byte *s) +utf8_check(const byte *s) { #define UTF8_CHECK_NEXT if (unlikely((*s & 0xc0) != 0x80)) goto bad; s++ while (*s) diff --git a/lib/unicode.h b/lib/unicode.h index 716b1515..685e9f81 100644 --- a/lib/unicode.h +++ b/lib/unicode.h @@ -106,7 +106,7 @@ utf8_get(const byte *p, uns *uu) } static inline byte * -utf8_32_get(byte *p, uns *uu) +utf8_32_get(const byte *p, uns *uu) { uns u = *p++; if (u < 0x80) @@ -149,7 +149,7 @@ get1: UTF8_GET_NEXT; else goto bad; *uu = u; - return p; + return (byte *)p; } #define PUT_UTF8(p,u) p = utf8_put(p, u) @@ -202,8 +202,8 @@ utf8_encoding_len(uns c) /* unicode-utf8.c */ -uns utf8_strlen(byte *str); -uns utf8_strnlen(byte *str, uns n); -uns utf8_check(byte *str); +uns utf8_strlen(const byte *str); +uns utf8_strnlen(const byte *str, uns n); +uns utf8_check(const byte *str); #endif diff --git a/lib/url.c b/lib/url.c index c4662372..da3074bf 100644 --- a/lib/url.c +++ b/lib/url.c @@ -58,7 +58,7 @@ enhex(uns x) } int -url_deescape(byte *s, byte *d) +url_deescape(const byte *s, byte *d) { byte *dstart = d; byte *end = d + MAX_URL_SIZE - 10; @@ -100,7 +100,7 @@ url_deescape(byte *s, byte *d) *d++ = *s++; else if (Cspace(*s)) { - byte *s0 = s; + const byte *s0 = s; while (Cspace(*s)) s++; if (!url_ignore_spaces || !(!*s || d == dstart)) @@ -121,7 +121,7 @@ url_deescape(byte *s, byte *d) } int -url_enescape(byte *s, byte *d) +url_enescape(const byte *s, byte *d) { byte *end = d + MAX_URL_SIZE - 10; unsigned int c; @@ -151,7 +151,7 @@ url_enescape(byte *s, byte *d) } int -url_enescape_friendly(byte *src, byte *dest) +url_enescape_friendly(const byte *src, byte *dest) { byte *end = dest + MAX_URL_SIZE - 10; while (*src) @@ -179,7 +179,7 @@ byte *url_proto_names[URL_PROTO_MAX] = URL_PNAMES; static int url_proto_path_flags[URL_PROTO_MAX] = URL_PATH_FLAGS; uns -identify_protocol(byte *p) +identify_protocol(const byte *p) { uns i; @@ -492,7 +492,7 @@ url_canonicalize(struct url *u) /* Pack a broken-down URL */ static byte * -append(byte *d, byte *s, byte *e) +append(byte *d, const byte *s, byte *e) { if (d) while (*s) @@ -571,7 +571,7 @@ url_error(uns err) /* Standard cookbook recipes */ int -url_canon_split_rel(byte *u, byte *buf1, byte *buf2, struct url *url, struct url *base) +url_canon_split_rel(const byte *u, byte *buf1, byte *buf2, struct url *url, struct url *base) { int err; @@ -585,7 +585,7 @@ url_canon_split_rel(byte *u, byte *buf1, byte *buf2, struct url *url, struct url } int -url_auto_canonicalize_rel(byte *src, byte *dst, struct url *base) +url_auto_canonicalize_rel(const byte *src, byte *dst, struct url *base) { byte buf1[MAX_URL_SIZE], buf2[MAX_URL_SIZE], buf3[MAX_URL_SIZE]; int err; @@ -665,13 +665,13 @@ int main(int argc, char **argv) #endif struct component { - byte *start; + const byte *start; int length; u32 hash; }; static inline u32 -hashf(byte *start, int length) +hashf(const byte *start, int length) { u32 hf = length; while (length-- > 0) @@ -701,11 +701,11 @@ repeat_count(struct component *comp, uns count, uns len) } int -url_has_repeated_component(byte *url) +url_has_repeated_component(const byte *url) { struct component *comp; uns comps, comp_len, rep_prefix; - byte *c; + const byte *c; uns i; for (comps=0, c=url; c; comps++) diff --git a/lib/url.h b/lib/url.h index 8e92d7c5..9390ae93 100644 --- a/lib/url.h +++ b/lib/url.h @@ -29,9 +29,9 @@ /* Remove/Introduce '%' escapes */ -int url_deescape(byte *s, byte *d); -int url_enescape(byte *s, byte *d); -int url_enescape_friendly(byte *src, byte *dest); // for cards.c only +int url_deescape(const byte *s, byte *d); +int url_enescape(const byte *s, byte *d); +int url_enescape_friendly(const byte *src, byte *dest); // for cards.c only /* URL splitting and normalization */ @@ -50,15 +50,15 @@ int url_split(byte *s, struct url *u, byte *d); int url_normalize(struct url *u, struct url *b); int url_canonicalize(struct url *u); int url_pack(struct url *u, byte *d); -int url_canon_split_rel(byte *url, byte *buf1, byte *buf2, struct url *u, struct url *base); -int url_auto_canonicalize_rel(byte *src, byte *dst, struct url *base); -uns identify_protocol(byte *p); -int url_has_repeated_component(byte *url); +int url_canon_split_rel(const byte *url, byte *buf1, byte *buf2, struct url *u, struct url *base); +int url_auto_canonicalize_rel(const byte *src, byte *dst, struct url *base); +uns identify_protocol(const byte *p); +int url_has_repeated_component(const byte *url); -static inline int url_canon_split(byte *url, byte *buf1, byte *buf2, struct url *u) +static inline int url_canon_split(const byte *url, byte *buf1, byte *buf2, struct url *u) { return url_canon_split_rel(url, buf1, buf2, u, NULL); } -static inline int url_auto_canonicalize(byte *src, byte *dst) +static inline int url_auto_canonicalize(const byte *src, byte *dst) { return url_auto_canonicalize_rel(src, dst, NULL); } /* Error codes */ diff --git a/lib/wildmatch.c b/lib/wildmatch.c index fed2ddb9..e0b5e2ec 100644 --- a/lib/wildmatch.c +++ b/lib/wildmatch.c @@ -99,7 +99,7 @@ wp_new_state(struct wildpatt *w, u32 set) } struct wildpatt * -wp_compile(byte *p, struct mempool *pool) +wp_compile(const byte *p, struct mempool *pool) { struct wildpatt *w; uns i; @@ -148,7 +148,7 @@ wp_prune_cache(struct wildpatt *w) } int -wp_match(struct wildpatt *w, byte *s) +wp_match(struct wildpatt *w, const byte *s) { struct dfa_state *d; @@ -175,7 +175,7 @@ wp_match(struct wildpatt *w, byte *s) } int -wp_min_size(byte *p) +wp_min_size(const byte *p) { int s = 0; diff --git a/lib/wildmatch.h b/lib/wildmatch.h index 4fa77982..a429da13 100644 --- a/lib/wildmatch.h +++ b/lib/wildmatch.h @@ -10,6 +10,6 @@ struct wildpatt; struct mempool; -struct wildpatt *wp_compile(byte *, struct mempool *); -int wp_match(struct wildpatt *, byte *); -int wp_min_size(byte *); +struct wildpatt *wp_compile(const byte *, struct mempool *); +int wp_match(struct wildpatt *, const byte *); +int wp_min_size(const byte *); -- 2.39.2