diff --git a/.cvsignore b/.cvsignore index 35eb6f0..c97a903 100644 --- a/.cvsignore +++ b/.cvsignore @@ -23,3 +23,4 @@ havealloca.h uudecode haveuint128.h ent +tags diff --git a/buffer.h b/buffer.h index 24ff5e1..935b9bf 100644 --- a/buffer.h +++ b/buffer.h @@ -16,20 +16,22 @@ typedef struct buffer { size_t p; /* current position */ size_t n; /* current size of string in buffer */ size_t a; /* allocated buffer size */ - int fd; /* passed as first argument to op */ ssize_t (*op)(); /* use read(2) or write(2) */ - enum { NOTHING, FREE, MUNMAP } todo; - void* cookie; + void* cookie; /* used internally by the to-stralloc buffers, and for buffer chaining */ + void (*deinit)(void*); /* called to munmap/free cleanup, with a pointer to the buffer as argument */ + int fd; /* passed as first argument to op */ } buffer; -#define BUFFER_INIT(op,fd,buf,len) { (buf), 0, 0, (len), (fd), (op), NOTHING, NULL } -#define BUFFER_INIT_FREE(op,fd,buf,len) { (buf), 0, 0, (len), (fd), (op), FREE } +#define BUFFER_INIT(op,fd,buf,len) { (buf), 0, 0, (len), (op), NULL, NULL, (fd) } +#define BUFFER_INIT_FREE(op,fd,buf,len) { (buf), 0, 0, (len), (op), NULL, buffer_free, (fd) } #define BUFFER_INIT_READ(op,fd,buf,len) BUFFER_INIT(op,fd,buf,len) /*obsolete*/ #define BUFFER_INSIZE 8192 #define BUFFER_OUTSIZE 8192 void buffer_init(buffer* b,ssize_t (*op)(),int fd,char* y,size_t ylen); void buffer_init_free(buffer* b,ssize_t (*op)(),int fd,char* y,size_t ylen); +void buffer_free(void* buf); +void buffer_munmap(void* buf); int buffer_mmapread(buffer* b,const char* filename); void buffer_close(buffer* b); @@ -56,8 +58,8 @@ int buffer_putnlflush(buffer* b); /* put \n and flush */ ) ssize_t buffer_get(buffer* b,char* x,size_t len); -int buffer_feed(buffer* b); -int buffer_getc(buffer* b,char* x); +ssize_t buffer_feed(buffer* b); +ssize_t buffer_getc(buffer* b,char* x); ssize_t buffer_getn(buffer* b,char* x,size_t len); /* read bytes until the destination buffer is full (len bytes), end of diff --git a/buffer/buffer_close.c b/buffer/buffer_close.c index 41d57d9..ebd8691 100644 --- a/buffer/buffer_close.c +++ b/buffer/buffer_close.c @@ -1,22 +1,7 @@ #include -#include #include -#ifdef __MINGW32__ -#include -#else -#include -#endif void buffer_close(buffer* b) { if (b->fd != -1) close(b->fd); - switch (b->todo) { - case FREE: free(b->x); break; - case MUNMAP: -#ifdef __MINGW32__ - UnmapViewOfFile(b->x); -#else - munmap(b->x,b->a); break; -#endif - default: ; - } + if (b->deinit) b->deinit(b); } diff --git a/buffer/buffer_feed.c b/buffer/buffer_feed.c index 4fe06a0..9d97eda 100644 --- a/buffer/buffer_feed.c +++ b/buffer/buffer_feed.c @@ -1,14 +1,14 @@ #include "buffer.h" -extern int buffer_stubborn_read(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie); +extern ssize_t buffer_stubborn_read(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie); -int buffer_feed(buffer* b) { +ssize_t buffer_feed(buffer* b) { if (b->p==b->n) { - int w; + ssize_t w; if ((w=buffer_stubborn_read(b->op,b->fd,b->x,b->a,b))<0) return -1; - b->n=w; + b->n=(size_t)w; b->p=0; } - return (b->n-b->p); + return (ssize_t)(b->n-b->p); } diff --git a/buffer/buffer_flush.c b/buffer/buffer_flush.c index 40eae2a..786cb16 100644 --- a/buffer/buffer_flush.c +++ b/buffer/buffer_flush.c @@ -1,10 +1,13 @@ #include "buffer.h" -extern int buffer_stubborn(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie); +extern ssize_t buffer_stubborn(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie); extern int buffer_flush(buffer* b) { - register int p; + register size_t p; + register ssize_t r; if (!(p=b->p)) return 0; /* buffer already empty */ b->p=0; - return buffer_stubborn(b->op,b->fd,b->x,p,b); + r=buffer_stubborn(b->op,b->fd,b->x,p,b); + if (r>0) r=0; + return (int)r; } diff --git a/buffer/buffer_free.c b/buffer/buffer_free.c new file mode 100644 index 0000000..521deae --- /dev/null +++ b/buffer/buffer_free.c @@ -0,0 +1,6 @@ +#include +#include "buffer.h" + +void buffer_free(void* buf) { + free(buf); +} diff --git a/buffer/buffer_frombuf.c b/buffer/buffer_frombuf.c index fe849ac..0d22e50 100644 --- a/buffer/buffer_frombuf.c +++ b/buffer/buffer_frombuf.c @@ -9,7 +9,7 @@ static ssize_t dummyreadwrite(int fd,char* buf,size_t len) { } void buffer_frombuf(buffer* b,const char* x,size_t l) { - b->x=x; + b->x=(char*)x; b->p=0; b->n=l; b->a=l; diff --git a/buffer/buffer_get.3 b/buffer/buffer_get.3 index de21c26..619301e 100644 --- a/buffer/buffer_get.3 +++ b/buffer/buffer_get.3 @@ -17,7 +17,7 @@ bytes, buffer_get copies only that many bytes, and returns that number. If the string is empty, buffer_get first uses a \fBread operation\fR to feed data into the string. The \fBread operation\fR may indicate end of input, in which case buffer_get returns 0; or a read error, in which -case buffer_get returns -1, setting \fIerrno\fR approporiately. +case buffer_get returns -1, setting \fIerrno\fR appropriately. The preallocated space and the \fBread operation\fR are specified by \fIb\fR. You must initialize \fBb\fR using buffer_init before calling diff --git a/buffer/buffer_get.c b/buffer/buffer_get.c index 5f35009..be64f53 100644 --- a/buffer/buffer_get.c +++ b/buffer/buffer_get.c @@ -2,16 +2,17 @@ #include "buffer.h" ssize_t buffer_get(buffer* b,char* x,size_t len) { - unsigned long done; - int blen; + ssize_t done; + ssize_t blen; done=0; + if ((ssize_t)len < 0) len=(ssize_t)(((size_t)-1)>>1); while (len) { if ((blen=buffer_feed(b))<=0) return blen; - if ((unsigned long int) blen>=len) - blen=len; - byte_copy(x,blen,b->x+b->p); - b->p+=blen; - len-=blen; + if (blen>=(ssize_t)len) + blen=(ssize_t)len; + byte_copy(x,(size_t)blen,b->x+b->p); + b->p+=(size_t)blen; + len-=(size_t)blen; x+=blen; done+=blen; } diff --git a/buffer/buffer_get_token.c b/buffer/buffer_get_token.c index 25ae46d..4106fd0 100644 --- a/buffer/buffer_get_token.c +++ b/buffer/buffer_get_token.c @@ -5,12 +5,13 @@ ssize_t buffer_get_token(buffer* b,char* x,size_t len,const char* charset,size_t setlen) { size_t blen; + if ((ssize_t)len<0) len=(ssize_t)(((size_t)-1)>>1); for (blen=0;blenp==b->n) { - register int blen; + register ssize_t blen; if ((blen=buffer_feed(b))<=0) return blen; } *x=b->x[b->p]; diff --git a/buffer/buffer_getn.c b/buffer/buffer_getn.c index 2c6405a..0981e7a 100644 --- a/buffer/buffer_getn.c +++ b/buffer/buffer_getn.c @@ -4,11 +4,12 @@ ssize_t buffer_getn(buffer* b,char* x,size_t len) { size_t blen; + if ((ssize_t)len < 0) len=((size_t)-1)>>1; for(blen=0;blena=ylen; b->p=0; b->n=0; - b->todo=NOTHING; b->cookie=0; + b->deinit=0; } diff --git a/buffer/buffer_init_free.c b/buffer/buffer_init_free.c index 5173008..35044f6 100644 --- a/buffer/buffer_init_free.c +++ b/buffer/buffer_init_free.c @@ -3,5 +3,5 @@ void buffer_init_free(buffer* b,ssize_t (*op)(),int fd, char* y,size_t ylen) { buffer_init(b,op,fd,y,ylen); - b->todo=FREE; + b->deinit=buffer_free; } diff --git a/buffer/buffer_mmapread.c b/buffer/buffer_mmapread.c index 901aa57..e9239f8 100644 --- a/buffer/buffer_mmapread.c +++ b/buffer/buffer_mmapread.c @@ -10,6 +10,6 @@ int buffer_mmapread(buffer* b,const char* filename) { b->p=0; b->a=b->n; b->fd=-1; b->op=op; - b->todo=MUNMAP; + b->deinit=buffer_munmap; return 0; } diff --git a/buffer/buffer_munmap.c b/buffer/buffer_munmap.c new file mode 100644 index 0000000..ae56d69 --- /dev/null +++ b/buffer/buffer_munmap.c @@ -0,0 +1,15 @@ +#include "buffer.h" +#ifdef __MINGW32__ +#include +#else +#include +#endif + +void buffer_munmap(void* buf) { + buffer* b=(buffer*)buf; +#ifdef __MINGW32__ + UnmapViewOfFile(b->x); +#else + munmap(b->x,b->a); +#endif +} diff --git a/buffer/buffer_putalign.c b/buffer/buffer_putalign.c index 631cd8c..ae39f18 100644 --- a/buffer/buffer_putalign.c +++ b/buffer/buffer_putalign.c @@ -2,7 +2,7 @@ #include "buffer.h" int buffer_putalign(buffer* b,const char* buf,size_t len) { - unsigned int tmp; + size_t tmp; while (len>(tmp=b->a-b->p)) { byte_copy(b->x+b->p, tmp, buf); b->p+=tmp; diff --git a/buffer/buffer_stubborn.c b/buffer/buffer_stubborn.c index 53aa143..3ac07c0 100644 --- a/buffer/buffer_stubborn.c +++ b/buffer/buffer_stubborn.c @@ -2,14 +2,14 @@ #include "buffer.h" int buffer_stubborn(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie) { - int w; + ssize_t w; while (len) { if ((w=op(fd,buf,len,cookie))<0) { if (errno == EINTR) continue; return -1; }; buf+=w; - len-=w; + len-=(size_t)w; } return 0; } diff --git a/buffer/buffer_stubborn2.c b/buffer/buffer_stubborn2.c index 8feddae..b9146e2 100644 --- a/buffer/buffer_stubborn2.c +++ b/buffer/buffer_stubborn2.c @@ -1,8 +1,8 @@ #include #include "buffer.h" -int buffer_stubborn_read(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie) { - int w; +ssize_t buffer_stubborn_read(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie) { + ssize_t w; for (;;) { if ((w=op(fd,buf,len,cookie))<0) if (errno == EINTR) continue; diff --git a/buffer/buffer_tosa.c b/buffer/buffer_tosa.c index f5e4b5b..aa6f119 100644 --- a/buffer/buffer_tosa.c +++ b/buffer/buffer_tosa.c @@ -4,14 +4,13 @@ static ssize_t strallocwrite(int fd,char* buf,size_t len,void* myself) { buffer* b=myself; stralloc* sa=b->cookie; - sa->len+=len; if (stralloc_readyplus(sa,1024)==0) return 0; b->x=sa->s+sa->len; b->p=0; b->a=1024; (void)fd; (void)buf; - return len; + return (ssize_t)len; } int buffer_tosa(buffer* b,stralloc* sa) { diff --git a/dns.h b/dns.h index 3dfcc0e..4fd75a5 100644 --- a/dns.h +++ b/dns.h @@ -57,7 +57,7 @@ unsigned int dns_domain_length(const char *); int dns_domain_equal(const char *,const char *); int dns_domain_suffix(const char *,const char *); unsigned int dns_domain_suffixpos(const char *,const char *); -int dns_domain_fromdot(char **,const char *,unsigned int); +int dns_domain_fromdot(char **,const char *,size_t); int dns_domain_todot_cat(stralloc *,const char *); unsigned int dns_packet_copy(const char *,unsigned int,unsigned int,char *,unsigned int); diff --git a/dns/dns_dfd.c b/dns/dns_dfd.c index eb8aeac..67f4c49 100644 --- a/dns/dns_dfd.c +++ b/dns/dns_dfd.c @@ -3,7 +3,7 @@ #include "byte.h" #include "dns.h" -int dns_domain_fromdot(char **out,const char *buf,unsigned int n) +int dns_domain_fromdot(char **out,const char *buf,size_t n) { char label[63]; unsigned int labellen = 0; /* <= sizeof label */ diff --git a/dns/dns_dtda.c b/dns/dns_dtda.c index ba1db4f..7d4dfe0 100644 --- a/dns/dns_dtda.c +++ b/dns/dns_dtda.c @@ -16,15 +16,15 @@ int dns_domain_todot_cat(stralloc *out,const char *d) while (ch--) { ch2 = *d++; if ((ch2 >= 'A') && (ch2 <= 'Z')) - ch2 += 32; + ch2 = (char)(ch2 + 32); if (((ch2 >= 'a') && (ch2 <= 'z')) || ((ch2 >= '0') && (ch2 <= '9')) || (ch2 == '-') || (ch2 == '_')) { if (!stralloc_append(out,&ch2)) return 0; } else { - ch3 = ch2; - buf[3] = '0' + (ch3 & 7); ch3 >>= 3; - buf[2] = '0' + (ch3 & 7); ch3 >>= 3; - buf[1] = '0' + (ch3 & 7); + ch3 = (unsigned char)ch2; + buf[3] = (char)('0' + (ch3 & 7)); ch3 >>= 3; + buf[2] = (char)('0' + (ch3 & 7)); ch3 >>= 3; + buf[1] = (char)('0' + (ch3 & 7)); buf[0] = '\\'; if (!stralloc_catb(out,buf,4)) return 0; } diff --git a/dns/dns_rcrw.c b/dns/dns_rcrw.c index 0559f51..68616b6 100644 --- a/dns/dns_rcrw.c +++ b/dns/dns_rcrw.c @@ -32,7 +32,7 @@ static int init(stralloc *rules) i = 0; for (j = 0;j < data.len;++j) if (data.s[j] == '\n') { - if (!stralloc_catb(rules,data.s + i,j - i)) return -1; + if (!stralloc_catb(rules,data.s + i,(size_t)(j - i))) return -1; while (rules->len) { if (rules->s[rules->len - 1] != ' ') if (rules->s[rules->len - 1] != '\t') diff --git a/ip4.h b/ip4.h index cbb7435..df74e7e 100644 --- a/ip4.h +++ b/ip4.h @@ -6,8 +6,8 @@ extern "C" { #endif -unsigned int scan_ip4(const char *src,char *ip); -unsigned int fmt_ip4(char *dest,const char *ip); +size_t scan_ip4(const char *src,char *ip); +size_t fmt_ip4(char *dest,const char *ip); /* for djb backwards compatibility */ #define ip4_scan scan_ip4 diff --git a/ip6.h b/ip6.h index 482a582..c85cfa1 100644 --- a/ip6.h +++ b/ip6.h @@ -9,16 +9,16 @@ extern "C" { #endif -unsigned int scan_ip6(const char* src,char* ip); -unsigned int fmt_ip6(char* dest,const char* ip); -unsigned int fmt_ip6c(char* dest,const char* ip); +size_t scan_ip6(const char* src,char* ip); +size_t fmt_ip6(char* dest,const char* ip); +size_t fmt_ip6c(char* dest,const char* ip); -unsigned int scan_ip6if(const char* src,char* ip,uint32* scope_id); -unsigned int fmt_ip6if(char* dest,const char* ip,uint32 scope_id); -unsigned int fmt_ip6ifc(char* dest,const char* ip,uint32 scope_id); +size_t scan_ip6if(const char* src,char* ip,uint32* scope_id); +size_t fmt_ip6if(char* dest,const char* ip,uint32 scope_id); +size_t fmt_ip6ifc(char* dest,const char* ip,uint32 scope_id); -unsigned int scan_ip6_flat(const char *src,char *); -unsigned int fmt_ip6_flat(char *dest,const char *); +size_t scan_ip6_flat(const char *src,char *); +size_t fmt_ip6_flat(char *dest,const char *); /* ip6 address syntax: (h = hex digit), no leading '0' required diff --git a/mmap/mmap_private.c b/mmap/mmap_private.c index 391f2bf..1850ce0 100644 --- a/mmap/mmap_private.c +++ b/mmap/mmap_private.c @@ -27,7 +27,9 @@ char* mmap_private(const char* filename,size_t * filesize) { int fd=open_read(filename); char *map; if (fd>=0) { - *filesize=lseek(fd,0,SEEK_END); + register off_t o=lseek(fd,0,SEEK_END); + if (sizeof(off_t)!=sizeof(size_t) && o > (off_t)(size_t)-1) { close(fd); return 0; } + *filesize=(size_t)o; map=(char*)mmap(0,*filesize,PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,0); if (map==(char*)-1) map=0; diff --git a/mmap/mmap_read.c b/mmap/mmap_read.c index d3ea7f8..61ddb37 100644 --- a/mmap/mmap_read.c +++ b/mmap/mmap_read.c @@ -27,7 +27,9 @@ extern char* mmap_read(const char* filename,size_t * filesize) { int fd=open_read(filename); char *map; if (fd>=0) { - *filesize=lseek(fd,0,SEEK_END); + register off_t o=lseek(fd,0,SEEK_END); + if (sizeof(off_t)!=sizeof(size_t) && o > (off_t)(size_t)-1) { close(fd); return 0; } + *filesize=(size_t)o; map=mmap(0,*filesize,PROT_READ,MAP_SHARED,fd,0); if (map==(char*)-1) map=0; diff --git a/mmap/mmap_shared.c b/mmap/mmap_shared.c index 205f122..d4c99ff 100644 --- a/mmap/mmap_shared.c +++ b/mmap/mmap_shared.c @@ -27,7 +27,9 @@ extern char* mmap_shared(const char* filename,size_t * filesize) { int fd=open_rw(filename); char *map; if (fd>=0) { - *filesize=lseek(fd,0,SEEK_END); + register off_t o=lseek(fd,0,SEEK_END); + if (sizeof(off_t)!=sizeof(size_t) && o > (off_t)(size_t)-1) { close(fd); return 0; } + *filesize=(size_t)o; map=mmap(0,*filesize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); if (map==(char*)-1) map=0; diff --git a/open/openreadclose.3 b/open/openreadclose.3 index 26993c1..8187fbd 100644 --- a/open/openreadclose.3 +++ b/open/openreadclose.3 @@ -4,7 +4,7 @@ openreadclose \- read a whole file into a stralloc .SH SYNTAX .B #include -int \fBopenreadclose\fP(const char *\fIfilename\fR, +ssize_t \fBopenreadclose\fP(const char *\fIfilename\fR, stralloc* \fIsa\fR,size_t \fIbufsize\fR); .SH DESCRIPTION openreadclose opens the file \fIfilename\fR for reading and reads the diff --git a/open/readclose.c b/open/readclose.c index c798773..2ed56d0 100644 --- a/open/readclose.c +++ b/open/readclose.c @@ -2,19 +2,19 @@ #include #include "readclose.h" -int readclose_append(int fd,stralloc *sa,size_t bufsize) +ssize_t readclose_append(int fd,stralloc *sa,size_t bufsize) { - int r; + ssize_t r; for (;;) { if (!stralloc_readyplus(sa,bufsize)) { close(fd); return -1; } r = read(fd,sa->s + sa->len,bufsize); if (r == -1) if (errno == EINTR) continue; if (r <= 0) { close(fd); return r; } - sa->len += r; + sa->len += (size_t)r; } } -int readclose(int fd,stralloc *sa,size_t bufsize) +ssize_t readclose(int fd,stralloc *sa,size_t bufsize) { if (!stralloc_copys(sa,"")) { close(fd); return -1; } return readclose_append(fd,sa,bufsize); diff --git a/open/readclose_append.3 b/open/readclose_append.3 index 2b4ad4e..2f098ac 100644 --- a/open/readclose_append.3 +++ b/open/readclose_append.3 @@ -4,7 +4,7 @@ readclose_append \- read a whole file into a stralloc .SH SYNTAX .B #include -int \fBreadclose_append\fP(int fd,stralloc* \fIsa\fR, +ssize_t \fBreadclose_append\fP(int fd,stralloc* \fIsa\fR, size_t \fIbufsize\fR); .SH DESCRIPTION readclose_append reads the diff --git a/readclose.h b/readclose.h index 4f75a90..0e142dd 100644 --- a/readclose.h +++ b/readclose.h @@ -1,14 +1,17 @@ #ifndef READCLOSE_H #define READCLOSE_H +/* for ssize_t */ +#include + #include "stralloc.h" #ifdef __cplusplus extern "C" { #endif -int readclose_append(int fd,stralloc *buf,size_t initlen); -int readclose(int fd,stralloc *buf,size_t initlen); +ssize_t readclose_append(int fd,stralloc *buf,size_t initlen); +ssize_t readclose(int fd,stralloc *buf,size_t initlen); #ifdef __cplusplus } diff --git a/scan/scan_double.c b/scan/scan_double.c index 1ccb5f8..864900e 100644 --- a/scan/scan_double.c +++ b/scan/scan_double.c @@ -52,5 +52,5 @@ size_t scan_double(const char *in, double *dest) { } done: *dest=(neg?-d:d); - return c-in; + return (size_t)(c-in); } diff --git a/socket/fmt_ip4.c b/socket/fmt_ip4.c index 78e4327..87271fa 100644 --- a/socket/fmt_ip4.c +++ b/socket/fmt_ip4.c @@ -1,14 +1,13 @@ #include "fmt.h" #include "ip4.h" -unsigned int fmt_ip4(char *s,const char ip[4]) +size_t fmt_ip4(char *s,const char ip[4]) { - unsigned int len; - int i; + size_t i,len; len = 0; for (i=0; i<4; ++i) { - register unsigned int j; + register size_t j; len+=(j=fmt_ulong(s,(unsigned long) (unsigned char) ip[i]))+1; if (s && i<3) { s+=j; *s++='.'; } } diff --git a/socket/fmt_ip6.c b/socket/fmt_ip6.c index a661d00..63f6a18 100644 --- a/socket/fmt_ip6.c +++ b/socket/fmt_ip6.c @@ -3,9 +3,10 @@ #include "ip4.h" #include "ip6.h" -unsigned int fmt_ip6(char *s,const char ip[16]) +size_t fmt_ip6(char *s,const char ip[16]) { - unsigned long len,temp, k, pos0=0,len0=0, pos1=0, compr=0; + unsigned long temp; + size_t len, k, pos0=0,len0=0, pos1=0, compr=0; for (k=0; k<16; k+=2) { if (ip[k]==0 && ip[k+1]==0) { diff --git a/socket/fmt_ip6_flat.c b/socket/fmt_ip6_flat.c index a903d78..4351cc3 100644 --- a/socket/fmt_ip6_flat.c +++ b/socket/fmt_ip6_flat.c @@ -1,17 +1,14 @@ #include "ip6.h" #include "haveinline.h" +#include "fmt.h" -static inline char tohex(char c) { - return c>=10?c-10+'a':c+'0'; -} - -unsigned int fmt_ip6_flat(char *s,const char ip[16]) +size_t fmt_ip6_flat(char *s,const char ip[16]) { int i; if (!s) return 32; for (i=0; i<16; i++) { - *s++=tohex((unsigned char)ip[i] >> 4); - *s++=tohex((unsigned char)ip[i] & 15); + *s++=fmt_tohex((char)((unsigned char)ip[i] >> 4)); + *s++=fmt_tohex((unsigned char)ip[i] & 15); } return 32; } diff --git a/socket/fmt_ip6c.c b/socket/fmt_ip6c.c index 64ac8de..86f7d6d 100644 --- a/socket/fmt_ip6c.c +++ b/socket/fmt_ip6c.c @@ -3,7 +3,7 @@ #include "ip4.h" #include "ip6.h" -unsigned int fmt_ip6c(char *s,const char ip[16]) +size_t fmt_ip6c(char *s,const char ip[16]) { if (ip6_isv4mapped(ip)) return fmt_ip4(s,ip+12); diff --git a/socket/fmt_ip6if.c b/socket/fmt_ip6if.c index aba677e..c3d61a1 100644 --- a/socket/fmt_ip6if.c +++ b/socket/fmt_ip6if.c @@ -3,8 +3,8 @@ #include "fmt.h" #include "socket.h" -unsigned int fmt_ip6if(char* dest,const char* ip,uint32 scope_id) { - int i=fmt_ip6(dest,ip); +size_t fmt_ip6if(char* dest,const char* ip,uint32 scope_id) { + size_t i=fmt_ip6(dest,ip); if (scope_id) { if (dest) { dest[i]='%'; ++i; dest+=i; diff --git a/socket/scan_ip4.c b/socket/scan_ip4.c index e375de2..44502f1 100644 --- a/socket/scan_ip4.c +++ b/socket/scan_ip4.c @@ -1,18 +1,17 @@ #include "scan.h" #include "ip4.h" -unsigned int scan_ip4(const char *s,char ip[4]) +size_t scan_ip4(const char *s,char ip[4]) { - unsigned int len; + size_t i,len; unsigned long u; - int i; len = 0; for (i=0; i<4; ++i) { - register unsigned int j; + register size_t j; len+=(j=scan_ulong(s,&u))+1; - if (!j) return 0; - ip[i]=u; s+=j; + if (!j || u>255) return 0; + ip[i]=(char)u; s+=j; if (i<3 && *s!='.') return 0; ++s; } return len-1; diff --git a/socket/scan_ip6.c b/socket/scan_ip6.c index b71afba..5efb0ed 100644 --- a/socket/scan_ip6.c +++ b/socket/scan_ip6.c @@ -10,10 +10,10 @@ * 3. The last two words may be written as IPv4 address */ -unsigned int scan_ip6(const char *s,char ip[16]) +size_t scan_ip6(const char *s,char ip[16]) { - unsigned int i; - unsigned int len=0; + size_t i; + size_t len=0; unsigned long u; char suffix[16]; @@ -45,8 +45,8 @@ unsigned int scan_ip6(const char *s,char ip[16]) else return 0; } - ip[prefixlen++] = (u >> 8); - ip[prefixlen++] = (u & 255); + ip[prefixlen++] = (char)(u >> 8); + ip[prefixlen++] = (char)u; s += i; len += i; if (prefixlen==16) return len; @@ -68,7 +68,7 @@ unsigned int scan_ip6(const char *s,char ip[16]) break; } if (suffixlen+prefixlen<=12 && s[i]=='.') { - int j=scan_ip4(s,suffix+suffixlen); + size_t j=scan_ip4(s,suffix+suffixlen); if (j) { suffixlen+=4; len+=j; @@ -76,8 +76,8 @@ unsigned int scan_ip6(const char *s,char ip[16]) } else prefixlen=12-suffixlen; /* make end-of-loop test true */ } - suffix[suffixlen++] = (u >> 8); - suffix[suffixlen++] = (u & 255); + suffix[suffixlen++] = (char)(u >> 8); + suffix[suffixlen++] = (char)u; s += i; len += i; if (prefixlen+suffixlen==16) break; diff --git a/socket/scan_ip6_flat.c b/socket/scan_ip6_flat.c index d9b28f1..95ac974 100644 --- a/socket/scan_ip6_flat.c +++ b/socket/scan_ip6_flat.c @@ -1,16 +1,16 @@ #include "scan.h" -unsigned int scan_ip6_flat(const char *s,char ip[16]) +size_t scan_ip6_flat(const char *s,char ip[16]) { - int i; + size_t i; for (i=0; i<16; i++) { int tmp; - tmp=scan_fromhex(*s++); + tmp=scan_fromhex((unsigned char)*s++); if (tmp<0) return 0; - ip[i]=tmp << 4; - tmp=scan_fromhex(*s++); + ip[i]=(char)(tmp << 4); + tmp=scan_fromhex((unsigned char)*s++); if (tmp<0) return 0; - ip[i]+=tmp; + ip[i] = ip[i] | (char)tmp; } return 32; } diff --git a/socket/scan_ip6if.c b/socket/scan_ip6if.c index efb7626..cb28bb1 100644 --- a/socket/scan_ip6if.c +++ b/socket/scan_ip6if.c @@ -4,11 +4,11 @@ #include "socket.h" #include "havealloca.h" -unsigned int scan_ip6if(const char* src,char* ip,uint32* scope_id) { - int i=scan_ip6(src,ip); +size_t scan_ip6if(const char* src,char* ip,uint32* scope_id) { + size_t i=scan_ip6(src,ip); *scope_id=0; if (src[i]=='%') { - int j; + size_t j; char* tmp; for (j=i+1; isalnum(src[j]); ++j) ; if (!src[j]) diff --git a/socket/socket_listen.c b/socket/socket_listen.c index 431d3aa..a9efe17 100644 --- a/socket/socket_listen.c +++ b/socket/socket_listen.c @@ -28,6 +28,6 @@ int socket_listen(int s,unsigned int backlog) { } return r; #else - return listen(s, backlog); + return listen(s, (int)backlog); #endif } diff --git a/stralloc.h b/stralloc.h index 1c4d990..c66c04d 100644 --- a/stralloc.h +++ b/stralloc.h @@ -107,15 +107,19 @@ int stralloc_diffs(const stralloc* a,const char* b) __pure__; int stralloc_catulong0(stralloc* sa,unsigned long int in,size_t n); /* stralloc_catlong0 appends a '0' padded ASCII representation of in */ +/* note that the n does not include the sign: + * stralloc_catlong0(&sa,-10,4) -> "-0010" */ int stralloc_catlong0(stralloc* sa,signed long int in,size_t n); /* stralloc_free frees the storage associated with sa */ void stralloc_free(stralloc* sa); #define stralloc_catlong(sa,l) (stralloc_catlong0((sa),(l),0)) +#define stralloc_catulong(sa,l) (stralloc_catulong0((sa),(l),0)) #define stralloc_catuint0(sa,i,n) (stralloc_catulong0((sa),(i),(n))) #define stralloc_catint0(sa,i,n) (stralloc_catlong0((sa),(i),(n))) #define stralloc_catint(sa,i) (stralloc_catlong0((sa),(i),0)) +#define stralloc_catuint(sa,i) (stralloc_catulong0((sa),(i),0)) /* remove last char. Return removed byte as unsigned char (or -1 if stralloc was empty). */ int stralloc_chop(stralloc* sa); @@ -129,7 +133,7 @@ int buffer_putsa(buffer* b,stralloc* sa); /* write stralloc to buffer and flush */ int buffer_putsaflush(buffer* b,stralloc* sa); -/* these "read token" functions return 0 if the token was complete or +/* these "read token" functions return 1 for a complete token, 0 if * EOF was hit or -1 on error. In contrast to the non-stralloc token * functions, the separator is also put in the stralloc; use * stralloc_chop or stralloc_chomp to get rid of it. */ @@ -158,9 +162,10 @@ int buffer_get_token_sa_pred(buffer* b,stralloc* sa,sa_predicate p); int buffer_get_new_token_sa_pred(buffer* b,stralloc* sa,sa_predicate p); -/* make a buffer from a stralloc. +/* make a buffer (for reading) from a stralloc. * Do not change the stralloc after this! */ void buffer_fromsa(buffer* b,stralloc* sa); +int buffer_tosa(buffer*b,stralloc* sa); /* write to sa, auto-growing it */ #endif #ifdef __cplusplus diff --git a/stralloc/stralloc_catlong0.c b/stralloc/stralloc_catlong0.c index b0e03a7..c0e0814 100644 --- a/stralloc/stralloc_catlong0.c +++ b/stralloc/stralloc_catlong0.c @@ -2,9 +2,11 @@ #include "fmt.h" int stralloc_catlong0(stralloc *sa,signed long int in,size_t n) { - if (stralloc_readyplus(sa,fmt_minus(0,in)+fmt_ulong0(0,in,n))) { - sa->len+=fmt_minus(sa->s+sa->len,in); - sa->len+=fmt_ulong0(sa->s+sa->len,in>=0?in:-in,n); + int neg=-(in<0); + if (neg) in=-in; + if (stralloc_readyplus(sa,fmt_minus(0,neg)+fmt_ulong0(0,(unsigned long)in,n))) { + sa->len+=fmt_minus(sa->s+sa->len,neg); + sa->len+=fmt_ulong0(sa->s+sa->len,(unsigned long)in,n); return 1; } else return 0; diff --git a/t.c b/t.c index 82206f9..0903acb 100644 --- a/t.c +++ b/t.c @@ -50,6 +50,10 @@ static int64 writecb(int64 fd,const void* buf,uint64 n) { int main(int argc,char* argv[]) { char buf[1024]; size_t l; + unsigned char c; + printf("%d\n",(c=scan_fromhex('.'))); + (void)argc; + (void)argv; assert(fmt_jsonescape(buf,"foo\nbar\\",8)==14 && byte_equal(buf,14,"foo\\u000abar\\\\")); memset(buf,0,sizeof(buf)); assert(scan_jsonescape("foo\\u000abar\\\\",buf,&l)==14 && l==8 && byte_equal(buf,8,"foo\nbar\\")); diff --git a/tai/tai_pack.c b/tai/tai_pack.c index 0a2bc06..2d27223 100644 --- a/tai/tai_pack.c +++ b/tai/tai_pack.c @@ -5,12 +5,12 @@ void tai_pack(char *s,const struct tai *t) uint64 x; x = t->x; - s[7] = x & 255; x >>= 8; - s[6] = x & 255; x >>= 8; - s[5] = x & 255; x >>= 8; - s[4] = x & 255; x >>= 8; - s[3] = x & 255; x >>= 8; - s[2] = x & 255; x >>= 8; - s[1] = x & 255; x >>= 8; - s[0] = x; + s[7] = (char)x; x >>= 8; + s[6] = (char)x; x >>= 8; + s[5] = (char)x; x >>= 8; + s[4] = (char)x; x >>= 8; + s[3] = (char)x; x >>= 8; + s[2] = (char)x; x >>= 8; + s[1] = (char)x; x >>= 8; + s[0] = (char)x; } diff --git a/taia/taia_add.c b/taia/taia_add.c index 3044a26..01bd5f4 100644 --- a/taia/taia_add.c +++ b/taia/taia_add.c @@ -8,11 +8,11 @@ void taia_add(struct taia *t,const struct taia *u,const struct taia *v) t->nano = u->nano + v->nano; t->atto = u->atto + v->atto; if (t->atto > 999999999UL) { - t->atto -= 1000000000UL; + t->atto -= (uint32)1000000000UL; ++t->nano; } if (t->nano > 999999999UL) { - t->nano -= 1000000000UL; + t->nano -= (uint32)1000000000UL; ++t->sec.x; } } diff --git a/taia/taia_addsec.c b/taia/taia_addsec.c index 03a04b5..32ac295 100644 --- a/taia/taia_addsec.c +++ b/taia/taia_addsec.c @@ -4,7 +4,7 @@ void taia_addsec(struct taia *t,const struct taia *u,long secs) { - t->sec.x = u->sec.x + secs; + t->sec.x = u->sec.x + (uint64)(int64)secs; t->nano = u->nano; t->atto = u->atto; } diff --git a/taia/taia_half.c b/taia/taia_half.c index 189bd0c..9d27719 100644 --- a/taia/taia_half.c +++ b/taia/taia_half.c @@ -4,8 +4,8 @@ void taia_half(tai6464* t,const tai6464* u) { t->atto = u->atto >> 1; - if (u->nano & 1) t->atto += 500000000UL; + if (u->nano & 1) t->atto += (uint32)500000000UL; t->nano = u->nano >> 1; - if (u->sec.x & 1) t->nano += 500000000UL; + if (u->sec.x & 1) t->nano += (uint32)500000000UL; t->sec.x = u->sec.x >> 1; } diff --git a/taia/taia_now.c b/taia/taia_now.c index 54d5837..03c3bc2 100644 --- a/taia/taia_now.c +++ b/taia/taia_now.c @@ -23,7 +23,7 @@ void taia_now(struct taia *t) struct timeval now; gettimeofday(&now,(struct timezone *) 0); tai_unix(&t->sec,now.tv_sec); - t->nano = 1000 * now.tv_usec + 500; + t->nano = (uint32)(1000 * now.tv_usec + 500); t->atto = 0; #endif } diff --git a/taia/taia_pack.c b/taia/taia_pack.c index 89e2c16..b2f9ebf 100644 --- a/taia/taia_pack.c +++ b/taia/taia_pack.c @@ -2,19 +2,19 @@ void taia_pack(char *s,const struct taia *t) { - unsigned long x; + uint32 x; tai_pack(s,&t->sec); s += 8; x = t->atto; - s[7] = x & 255; x >>= 8; - s[6] = x & 255; x >>= 8; - s[5] = x & 255; x >>= 8; - s[4] = x; + s[7] = (char)x; x >>= 8; + s[6] = (char)x; x >>= 8; + s[5] = (char)x; x >>= 8; + s[4] = (char)x; x = t->nano; - s[3] = x & 255; x >>= 8; - s[2] = x & 255; x >>= 8; - s[1] = x & 255; x >>= 8; - s[0] = x; + s[3] = (char)x; x >>= 8; + s[2] = (char)x; x >>= 8; + s[1] = (char)x; x >>= 8; + s[0] = (char)x; } diff --git a/taia/taia_sub.c b/taia/taia_sub.c index 6944689..4e27a3c 100644 --- a/taia/taia_sub.c +++ b/taia/taia_sub.c @@ -4,18 +4,18 @@ void taia_sub(struct taia *t,const struct taia *u,const struct taia *v) { - unsigned long unano = u->nano; - unsigned long uatto = u->atto; + uint32 unano = u->nano; + uint32 uatto = u->atto; t->sec.x = u->sec.x - v->sec.x; t->nano = unano - v->nano; t->atto = uatto - v->atto; if (t->atto > uatto) { - t->atto += 1000000000UL; + t->atto += (uint32)1000000000UL; --t->nano; } if (t->nano > unano) { - t->nano += 1000000000UL; + t->nano += (uint32)1000000000UL; --t->sec.x; } } diff --git a/taia/taia_unpack.c b/taia/taia_unpack.c index f55ae86..d6e5376 100644 --- a/taia/taia_unpack.c +++ b/taia/taia_unpack.c @@ -2,7 +2,7 @@ void taia_unpack(const char* s,struct taia* t) { - unsigned long x; + uint32_t x; tai_unpack(s,&t->sec); s += 8; diff --git a/test/marshal.c b/test/marshal.c index 9062381..57cbad5 100644 --- a/test/marshal.c +++ b/test/marshal.c @@ -4,6 +4,8 @@ #include #include +#include +#include #include #include #include @@ -12,6 +14,7 @@ #include #include #include +#include char buf[100]; stralloc sa; @@ -355,5 +358,50 @@ int main() { assert(stralloc_readyplus(&sa,(size_t)-1)==0); assert(stralloc_ready(&sa,(size_t)-1)==0); + stralloc_zero(&sa); + assert(stralloc_copys(&sa,"hello, ")); + assert(stralloc_cats(&sa,"world!")); + assert(stralloc_diffs(&sa,"hello, world!") == 0); + assert(stralloc_equals(&sa,"hello, world!")); + assert(stralloc_0(&sa)); + assert(str_equal(sa.s,"hello, world!")); + assert(stralloc_equal(&sa,&sa)); + + assert(stralloc_copym(&sa,"hallo",", ","welt","!\n")); + assert(stralloc_equals(&sa,"hallo, welt!\n")); + + stralloc_zero(&sa); + assert(stralloc_catlong(&sa,1)); + assert(stralloc_catint(&sa,2)); + assert(stralloc_catulong(&sa,3)); + assert(stralloc_catuint(&sa,4)); + assert(stralloc_equals(&sa,"1234")); + assert(stralloc_catulong0(&sa,5678,6)); + assert(stralloc_equals(&sa,"1234005678")); + stralloc_zero(&sa); + assert(stralloc_catlong0(&sa,-5678,6)); + assert(stralloc_equals(&sa,"-005678")); + assert(stralloc_chop(&sa)); + assert(stralloc_equals(&sa,"-00567")); + assert(stralloc_chomp(&sa)==0); + assert(stralloc_equals(&sa,"-00567")); + assert(stralloc_cats(&sa,"\r\n")); + assert(stralloc_equals(&sa,"-00567\r\n")); + assert(stralloc_chomp(&sa)==2); + assert(stralloc_equals(&sa,"-00567")); + + stralloc_zero(&sa); + assert(stralloc_copys(&sa,"foo\nbar\r\nbaz")); + { + buffer b; + stralloc s; + buffer_fromsa(&b,&sa); + stralloc_init(&s); + assert(buffer_getline_sa(&b,&s)==1 && stralloc_equals(&s,"foo\n")); + assert(buffer_getnewline_sa(&b,&s)==1 && stralloc_equals(&s,"bar\r\n")); + assert(buffer_getnewline_sa(&b,&s)==0 && stralloc_equals(&s,"baz")); + buffer_close(&b); + } + return 0; } diff --git a/unix/iopause.c b/unix/iopause.c index 2ffdd00..316ea5b 100644 --- a/unix/iopause.c +++ b/unix/iopause.c @@ -20,7 +20,7 @@ void iopause(iopause_fd *x,unsigned int len,struct taia *deadline,struct taia *s taia_sub(&t,deadline,&t); d = taia_approx(&t); if (d > 1000.0) d = 1000.0; - millisecs = d * 1000.0 + 20.0; + millisecs = (int)(d * 1000.0 + 20.0); } for (i = 0;i < len;++i)