get rid of more -Wconversion warnings

master
leitner 11 years ago
parent f7fee036c1
commit 4a04c40595

@ -23,3 +23,4 @@ havealloca.h
uudecode uudecode
haveuint128.h haveuint128.h
ent ent
tags

@ -16,20 +16,22 @@ typedef struct buffer {
size_t p; /* current position */ size_t p; /* current position */
size_t n; /* current size of string in buffer */ size_t n; /* current size of string in buffer */
size_t a; /* allocated buffer size */ size_t a; /* allocated buffer size */
int fd; /* passed as first argument to op */
ssize_t (*op)(); /* use read(2) or write(2) */ ssize_t (*op)(); /* use read(2) or write(2) */
enum { NOTHING, FREE, MUNMAP } todo; void* cookie; /* used internally by the to-stralloc buffers, and for buffer chaining */
void* cookie; 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; } buffer;
#define BUFFER_INIT(op,fd,buf,len) { (buf), 0, 0, (len), (fd), (op), NOTHING, NULL } #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), (fd), (op), FREE } #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_INIT_READ(op,fd,buf,len) BUFFER_INIT(op,fd,buf,len) /*obsolete*/
#define BUFFER_INSIZE 8192 #define BUFFER_INSIZE 8192
#define BUFFER_OUTSIZE 8192 #define BUFFER_OUTSIZE 8192
void buffer_init(buffer* b,ssize_t (*op)(),int fd,char* y,size_t ylen); 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_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); int buffer_mmapread(buffer* b,const char* filename);
void buffer_close(buffer* b); 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); ssize_t buffer_get(buffer* b,char* x,size_t len);
int buffer_feed(buffer* b); ssize_t buffer_feed(buffer* b);
int buffer_getc(buffer* b,char* x); ssize_t buffer_getc(buffer* b,char* x);
ssize_t buffer_getn(buffer* b,char* x,size_t len); ssize_t buffer_getn(buffer* b,char* x,size_t len);
/* read bytes until the destination buffer is full (len bytes), end of /* read bytes until the destination buffer is full (len bytes), end of

@ -1,22 +1,7 @@
#include <buffer.h> #include <buffer.h>
#include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#ifdef __MINGW32__
#include <windows.h>
#else
#include <sys/mman.h>
#endif
void buffer_close(buffer* b) { void buffer_close(buffer* b) {
if (b->fd != -1) close(b->fd); if (b->fd != -1) close(b->fd);
switch (b->todo) { if (b->deinit) b->deinit(b);
case FREE: free(b->x); break;
case MUNMAP:
#ifdef __MINGW32__
UnmapViewOfFile(b->x);
#else
munmap(b->x,b->a); break;
#endif
default: ;
}
} }

@ -1,14 +1,14 @@
#include "buffer.h" #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) { 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) if ((w=buffer_stubborn_read(b->op,b->fd,b->x,b->a,b))<0)
return -1; return -1;
b->n=w; b->n=(size_t)w;
b->p=0; b->p=0;
} }
return (b->n-b->p); return (ssize_t)(b->n-b->p);
} }

@ -1,10 +1,13 @@
#include "buffer.h" #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) { 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 */ if (!(p=b->p)) return 0; /* buffer already empty */
b->p=0; 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;
} }

@ -0,0 +1,6 @@
#include <stdlib.h>
#include "buffer.h"
void buffer_free(void* buf) {
free(buf);
}

@ -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) { void buffer_frombuf(buffer* b,const char* x,size_t l) {
b->x=x; b->x=(char*)x;
b->p=0; b->p=0;
b->n=l; b->n=l;
b->a=l; b->a=l;

@ -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 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 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 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 The preallocated space and the \fBread operation\fR are specified by
\fIb\fR. You must initialize \fBb\fR using buffer_init before calling \fIb\fR. You must initialize \fBb\fR using buffer_init before calling

@ -2,16 +2,17 @@
#include "buffer.h" #include "buffer.h"
ssize_t buffer_get(buffer* b,char* x,size_t len) { ssize_t buffer_get(buffer* b,char* x,size_t len) {
unsigned long done; ssize_t done;
int blen; ssize_t blen;
done=0; done=0;
if ((ssize_t)len < 0) len=(ssize_t)(((size_t)-1)>>1);
while (len) { while (len) {
if ((blen=buffer_feed(b))<=0) return blen; if ((blen=buffer_feed(b))<=0) return blen;
if ((unsigned long int) blen>=len) if (blen>=(ssize_t)len)
blen=len; blen=(ssize_t)len;
byte_copy(x,blen,b->x+b->p); byte_copy(x,(size_t)blen,b->x+b->p);
b->p+=blen; b->p+=(size_t)blen;
len-=blen; len-=(size_t)blen;
x+=blen; x+=blen;
done+=blen; done+=blen;
} }

@ -5,12 +5,13 @@
ssize_t buffer_get_token(buffer* b,char* x,size_t len,const char* charset,size_t setlen) { ssize_t buffer_get_token(buffer* b,char* x,size_t len,const char* charset,size_t setlen) {
size_t blen; size_t blen;
if ((ssize_t)len<0) len=(ssize_t)(((size_t)-1)>>1);
for (blen=0;blen<len;++blen) { for (blen=0;blen<len;++blen) {
register int r; register ssize_t r;
if ((r=buffer_getc(b,x))<0) return r; if ((r=buffer_getc(b,x))<0) return r;
if (r==0) { *x=0; break; } if (r==0) { *x=0; break; }
if (byte_chr(charset,setlen,*x)<setlen) break; if (byte_chr(charset,setlen,*x)<setlen) break;
++x; ++x;
} }
return blen; return (ssize_t)blen;
} }

@ -7,7 +7,7 @@ ssize_t buffer_get_token_pred(buffer* b,char* x,size_t len,
unsigned int blen; unsigned int blen;
for (blen=0;blen<len;++blen) { for (blen=0;blen<len;++blen) {
register int r; register ssize_t r;
if ((r=buffer_getc(b,x))<0) return r; if ((r=buffer_getc(b,x))<0) return r;
if (r==0) break; if (r==0) break;
if (p(x-blen,blen+1)) break; if (p(x-blen,blen+1)) break;

@ -1,9 +1,9 @@
#include "byte.h" #include "byte.h"
#include "buffer.h" #include "buffer.h"
int buffer_getc(buffer* b,char* x) { ssize_t buffer_getc(buffer* b,char* x) {
if (b->p==b->n) { if (b->p==b->n) {
register int blen; register ssize_t blen;
if ((blen=buffer_feed(b))<=0) return blen; if ((blen=buffer_feed(b))<=0) return blen;
} }
*x=b->x[b->p]; *x=b->x[b->p];

@ -4,11 +4,12 @@
ssize_t buffer_getn(buffer* b,char* x,size_t len) { ssize_t buffer_getn(buffer* b,char* x,size_t len) {
size_t blen; size_t blen;
if ((ssize_t)len < 0) len=((size_t)-1)>>1;
for(blen=0;blen<len;++blen) { for(blen=0;blen<len;++blen) {
register ssize_t r; register ssize_t r;
if ((r=buffer_getc(b,x))<0) return r; if ((r=buffer_getc(b,x))<0) return r;
if (r==0) break; if (r==0) break;
++x; ++x;
} }
return blen; return (ssize_t)blen;
} }

@ -8,6 +8,6 @@ void buffer_init(buffer* b,ssize_t (*op)(),int fd,
b->a=ylen; b->a=ylen;
b->p=0; b->p=0;
b->n=0; b->n=0;
b->todo=NOTHING;
b->cookie=0; b->cookie=0;
b->deinit=0;
} }

@ -3,5 +3,5 @@
void buffer_init_free(buffer* b,ssize_t (*op)(),int fd, void buffer_init_free(buffer* b,ssize_t (*op)(),int fd,
char* y,size_t ylen) { char* y,size_t ylen) {
buffer_init(b,op,fd,y,ylen); buffer_init(b,op,fd,y,ylen);
b->todo=FREE; b->deinit=buffer_free;
} }

@ -10,6 +10,6 @@ int buffer_mmapread(buffer* b,const char* filename) {
b->p=0; b->a=b->n; b->p=0; b->a=b->n;
b->fd=-1; b->fd=-1;
b->op=op; b->op=op;
b->todo=MUNMAP; b->deinit=buffer_munmap;
return 0; return 0;
} }

@ -0,0 +1,15 @@
#include "buffer.h"
#ifdef __MINGW32__
#include <windows.h>
#else
#include <sys/mman.h>
#endif
void buffer_munmap(void* buf) {
buffer* b=(buffer*)buf;
#ifdef __MINGW32__
UnmapViewOfFile(b->x);
#else
munmap(b->x,b->a);
#endif
}

@ -2,7 +2,7 @@
#include "buffer.h" #include "buffer.h"
int buffer_putalign(buffer* b,const char* buf,size_t len) { int buffer_putalign(buffer* b,const char* buf,size_t len) {
unsigned int tmp; size_t tmp;
while (len>(tmp=b->a-b->p)) { while (len>(tmp=b->a-b->p)) {
byte_copy(b->x+b->p, tmp, buf); byte_copy(b->x+b->p, tmp, buf);
b->p+=tmp; b->p+=tmp;

@ -2,14 +2,14 @@
#include "buffer.h" #include "buffer.h"
int buffer_stubborn(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie) { int buffer_stubborn(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie) {
int w; ssize_t w;
while (len) { while (len) {
if ((w=op(fd,buf,len,cookie))<0) { if ((w=op(fd,buf,len,cookie))<0) {
if (errno == EINTR) continue; if (errno == EINTR) continue;
return -1; return -1;
}; };
buf+=w; buf+=w;
len-=w; len-=(size_t)w;
} }
return 0; return 0;
} }

@ -1,8 +1,8 @@
#include <errno.h> #include <errno.h>
#include "buffer.h" #include "buffer.h"
int buffer_stubborn_read(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie) { ssize_t buffer_stubborn_read(ssize_t (*op)(),int fd,const char* buf, size_t len,void* cookie) {
int w; ssize_t w;
for (;;) { for (;;) {
if ((w=op(fd,buf,len,cookie))<0) if ((w=op(fd,buf,len,cookie))<0)
if (errno == EINTR) continue; if (errno == EINTR) continue;

@ -4,14 +4,13 @@
static ssize_t strallocwrite(int fd,char* buf,size_t len,void* myself) { static ssize_t strallocwrite(int fd,char* buf,size_t len,void* myself) {
buffer* b=myself; buffer* b=myself;
stralloc* sa=b->cookie; stralloc* sa=b->cookie;
sa->len+=len;
if (stralloc_readyplus(sa,1024)==0) return 0; if (stralloc_readyplus(sa,1024)==0) return 0;
b->x=sa->s+sa->len; b->x=sa->s+sa->len;
b->p=0; b->p=0;
b->a=1024; b->a=1024;
(void)fd; (void)fd;
(void)buf; (void)buf;
return len; return (ssize_t)len;
} }
int buffer_tosa(buffer* b,stralloc* sa) { int buffer_tosa(buffer* b,stralloc* sa) {

@ -57,7 +57,7 @@ unsigned int dns_domain_length(const char *);
int dns_domain_equal(const char *,const char *); int dns_domain_equal(const char *,const char *);
int dns_domain_suffix(const char *,const char *); int dns_domain_suffix(const char *,const char *);
unsigned int dns_domain_suffixpos(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 *); int dns_domain_todot_cat(stralloc *,const char *);
unsigned int dns_packet_copy(const char *,unsigned int,unsigned int,char *,unsigned int); unsigned int dns_packet_copy(const char *,unsigned int,unsigned int,char *,unsigned int);

@ -3,7 +3,7 @@
#include "byte.h" #include "byte.h"
#include "dns.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]; char label[63];
unsigned int labellen = 0; /* <= sizeof label */ unsigned int labellen = 0; /* <= sizeof label */

@ -16,15 +16,15 @@ int dns_domain_todot_cat(stralloc *out,const char *d)
while (ch--) { while (ch--) {
ch2 = *d++; ch2 = *d++;
if ((ch2 >= 'A') && (ch2 <= 'Z')) if ((ch2 >= 'A') && (ch2 <= 'Z'))
ch2 += 32; ch2 = (char)(ch2 + 32);
if (((ch2 >= 'a') && (ch2 <= 'z')) || ((ch2 >= '0') && (ch2 <= '9')) || (ch2 == '-') || (ch2 == '_')) { if (((ch2 >= 'a') && (ch2 <= 'z')) || ((ch2 >= '0') && (ch2 <= '9')) || (ch2 == '-') || (ch2 == '_')) {
if (!stralloc_append(out,&ch2)) return 0; if (!stralloc_append(out,&ch2)) return 0;
} }
else { else {
ch3 = ch2; ch3 = (unsigned char)ch2;
buf[3] = '0' + (ch3 & 7); ch3 >>= 3; buf[3] = (char)('0' + (ch3 & 7)); ch3 >>= 3;
buf[2] = '0' + (ch3 & 7); ch3 >>= 3; buf[2] = (char)('0' + (ch3 & 7)); ch3 >>= 3;
buf[1] = '0' + (ch3 & 7); buf[1] = (char)('0' + (ch3 & 7));
buf[0] = '\\'; buf[0] = '\\';
if (!stralloc_catb(out,buf,4)) return 0; if (!stralloc_catb(out,buf,4)) return 0;
} }

@ -32,7 +32,7 @@ static int init(stralloc *rules)
i = 0; i = 0;
for (j = 0;j < data.len;++j) for (j = 0;j < data.len;++j)
if (data.s[j] == '\n') { 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) { while (rules->len) {
if (rules->s[rules->len - 1] != ' ') if (rules->s[rules->len - 1] != ' ')
if (rules->s[rules->len - 1] != '\t') if (rules->s[rules->len - 1] != '\t')

@ -6,8 +6,8 @@
extern "C" { extern "C" {
#endif #endif
unsigned int scan_ip4(const char *src,char *ip); size_t scan_ip4(const char *src,char *ip);
unsigned int fmt_ip4(char *dest,const char *ip); size_t fmt_ip4(char *dest,const char *ip);
/* for djb backwards compatibility */ /* for djb backwards compatibility */
#define ip4_scan scan_ip4 #define ip4_scan scan_ip4

16
ip6.h

@ -9,16 +9,16 @@
extern "C" { extern "C" {
#endif #endif
unsigned int scan_ip6(const char* src,char* ip); size_t scan_ip6(const char* src,char* ip);
unsigned int fmt_ip6(char* dest,const char* ip); size_t fmt_ip6(char* dest,const char* ip);
unsigned int fmt_ip6c(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); size_t scan_ip6if(const char* src,char* ip,uint32* scope_id);
unsigned int fmt_ip6if(char* dest,const char* ip,uint32 scope_id); size_t fmt_ip6if(char* dest,const char* ip,uint32 scope_id);
unsigned int fmt_ip6ifc(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 *); size_t scan_ip6_flat(const char *src,char *);
unsigned int fmt_ip6_flat(char *dest,const char *); size_t fmt_ip6_flat(char *dest,const char *);
/* /*
ip6 address syntax: (h = hex digit), no leading '0' required ip6 address syntax: (h = hex digit), no leading '0' required

@ -27,7 +27,9 @@ char* mmap_private(const char* filename,size_t * filesize) {
int fd=open_read(filename); int fd=open_read(filename);
char *map; char *map;
if (fd>=0) { 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); map=(char*)mmap(0,*filesize,PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,0);
if (map==(char*)-1) if (map==(char*)-1)
map=0; map=0;

@ -27,7 +27,9 @@ extern char* mmap_read(const char* filename,size_t * filesize) {
int fd=open_read(filename); int fd=open_read(filename);
char *map; char *map;
if (fd>=0) { 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); map=mmap(0,*filesize,PROT_READ,MAP_SHARED,fd,0);
if (map==(char*)-1) if (map==(char*)-1)
map=0; map=0;

@ -27,7 +27,9 @@ extern char* mmap_shared(const char* filename,size_t * filesize) {
int fd=open_rw(filename); int fd=open_rw(filename);
char *map; char *map;
if (fd>=0) { 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); map=mmap(0,*filesize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if (map==(char*)-1) if (map==(char*)-1)
map=0; map=0;

@ -4,7 +4,7 @@ openreadclose \- read a whole file into a stralloc
.SH SYNTAX .SH SYNTAX
.B #include <openreadclose.h> .B #include <openreadclose.h>
int \fBopenreadclose\fP(const char *\fIfilename\fR, ssize_t \fBopenreadclose\fP(const char *\fIfilename\fR,
stralloc* \fIsa\fR,size_t \fIbufsize\fR); stralloc* \fIsa\fR,size_t \fIbufsize\fR);
.SH DESCRIPTION .SH DESCRIPTION
openreadclose opens the file \fIfilename\fR for reading and reads the openreadclose opens the file \fIfilename\fR for reading and reads the

@ -2,19 +2,19 @@
#include <errno.h> #include <errno.h>
#include "readclose.h" #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 (;;) { for (;;) {
if (!stralloc_readyplus(sa,bufsize)) { close(fd); return -1; } if (!stralloc_readyplus(sa,bufsize)) { close(fd); return -1; }
r = read(fd,sa->s + sa->len,bufsize); r = read(fd,sa->s + sa->len,bufsize);
if (r == -1) if (errno == EINTR) continue; if (r == -1) if (errno == EINTR) continue;
if (r <= 0) { close(fd); return r; } 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; } if (!stralloc_copys(sa,"")) { close(fd); return -1; }
return readclose_append(fd,sa,bufsize); return readclose_append(fd,sa,bufsize);

@ -4,7 +4,7 @@ readclose_append \- read a whole file into a stralloc
.SH SYNTAX .SH SYNTAX
.B #include <readclose.h> .B #include <readclose.h>
int \fBreadclose_append\fP(int fd,stralloc* \fIsa\fR, ssize_t \fBreadclose_append\fP(int fd,stralloc* \fIsa\fR,
size_t \fIbufsize\fR); size_t \fIbufsize\fR);
.SH DESCRIPTION .SH DESCRIPTION
readclose_append reads the readclose_append reads the

@ -1,14 +1,17 @@
#ifndef READCLOSE_H #ifndef READCLOSE_H
#define READCLOSE_H #define READCLOSE_H
/* for ssize_t */
#include <sys/types.h>
#include "stralloc.h" #include "stralloc.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
int readclose_append(int fd,stralloc *buf,size_t initlen); ssize_t readclose_append(int fd,stralloc *buf,size_t initlen);
int readclose(int fd,stralloc *buf,size_t initlen); ssize_t readclose(int fd,stralloc *buf,size_t initlen);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -52,5 +52,5 @@ size_t scan_double(const char *in, double *dest) {
} }
done: done:
*dest=(neg?-d:d); *dest=(neg?-d:d);
return c-in; return (size_t)(c-in);
} }

@ -1,14 +1,13 @@
#include "fmt.h" #include "fmt.h"
#include "ip4.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; size_t i,len;
int i;
len = 0; len = 0;
for (i=0; i<4; ++i) { 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; len+=(j=fmt_ulong(s,(unsigned long) (unsigned char) ip[i]))+1;
if (s && i<3) { s+=j; *s++='.'; } if (s && i<3) { s+=j; *s++='.'; }
} }

@ -3,9 +3,10 @@
#include "ip4.h" #include "ip4.h"
#include "ip6.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) { for (k=0; k<16; k+=2) {
if (ip[k]==0 && ip[k+1]==0) { if (ip[k]==0 && ip[k+1]==0) {

@ -1,17 +1,14 @@
#include "ip6.h" #include "ip6.h"
#include "haveinline.h" #include "haveinline.h"
#include "fmt.h"
static inline char tohex(char c) { size_t fmt_ip6_flat(char *s,const char ip[16])
return c>=10?c-10+'a':c+'0';
}
unsigned int fmt_ip6_flat(char *s,const char ip[16])
{ {
int i; int i;
if (!s) return 32; if (!s) return 32;
for (i=0; i<16; i++) { for (i=0; i<16; i++) {
*s++=tohex((unsigned char)ip[i] >> 4); *s++=fmt_tohex((char)((unsigned char)ip[i] >> 4));
*s++=tohex((unsigned char)ip[i] & 15); *s++=fmt_tohex((unsigned char)ip[i] & 15);
} }
return 32; return 32;
} }

@ -3,7 +3,7 @@
#include "ip4.h" #include "ip4.h"
#include "ip6.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)) if (ip6_isv4mapped(ip))
return fmt_ip4(s,ip+12); return fmt_ip4(s,ip+12);

@ -3,8 +3,8 @@
#include "fmt.h" #include "fmt.h"
#include "socket.h" #include "socket.h"
unsigned int fmt_ip6if(char* dest,const char* ip,uint32 scope_id) { size_t fmt_ip6if(char* dest,const char* ip,uint32 scope_id) {
int i=fmt_ip6(dest,ip); size_t i=fmt_ip6(dest,ip);
if (scope_id) { if (scope_id) {
if (dest) { if (dest) {
dest[i]='%'; ++i; dest+=i; dest[i]='%'; ++i; dest+=i;

@ -1,18 +1,17 @@
#include "scan.h" #include "scan.h"
#include "ip4.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; unsigned long u;
int i;
len = 0; len = 0;
for (i=0; i<4; ++i) { for (i=0; i<4; ++i) {
register unsigned int j; register size_t j;
len+=(j=scan_ulong(s,&u))+1; len+=(j=scan_ulong(s,&u))+1;
if (!j) return 0; if (!j || u>255) return 0;
ip[i]=u; s+=j; ip[i]=(char)u; s+=j;
if (i<3 && *s!='.') return 0; ++s; if (i<3 && *s!='.') return 0; ++s;
} }
return len-1; return len-1;

@ -10,10 +10,10 @@
* 3. The last two words may be written as IPv4 address * 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; size_t i;
unsigned int len=0; size_t len=0;
unsigned long u; unsigned long u;
char suffix[16]; char suffix[16];
@ -45,8 +45,8 @@ unsigned int scan_ip6(const char *s,char ip[16])
else else
return 0; return 0;
} }
ip[prefixlen++] = (u >> 8); ip[prefixlen++] = (char)(u >> 8);
ip[prefixlen++] = (u & 255); ip[prefixlen++] = (char)u;
s += i; len += i; s += i; len += i;
if (prefixlen==16) if (prefixlen==16)
return len; return len;
@ -68,7 +68,7 @@ unsigned int scan_ip6(const char *s,char ip[16])
break; break;
} }
if (suffixlen+prefixlen<=12 && s[i]=='.') { if (suffixlen+prefixlen<=12 && s[i]=='.') {
int j=scan_ip4(s,suffix+suffixlen); size_t j=scan_ip4(s,suffix+suffixlen);
if (j) { if (j) {
suffixlen+=4; suffixlen+=4;
len+=j; len+=j;
@ -76,8 +76,8 @@ unsigned int scan_ip6(const char *s,char ip[16])
} else } else
prefixlen=12-suffixlen; /* make end-of-loop test true */ prefixlen=12-suffixlen; /* make end-of-loop test true */
} }
suffix[suffixlen++] = (u >> 8); suffix[suffixlen++] = (char)(u >> 8);
suffix[suffixlen++] = (u & 255); suffix[suffixlen++] = (char)u;
s += i; len += i; s += i; len += i;
if (prefixlen+suffixlen==16) if (prefixlen+suffixlen==16)
break; break;

@ -1,16 +1,16 @@
#include "scan.h" #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++) { for (i=0; i<16; i++) {
int tmp; int tmp;
tmp=scan_fromhex(*s++); tmp=scan_fromhex((unsigned char)*s++);
if (tmp<0) return 0; if (tmp<0) return 0;
ip[i]=tmp << 4; ip[i]=(char)(tmp << 4);
tmp=scan_fromhex(*s++); tmp=scan_fromhex((unsigned char)*s++);
if (tmp<0) return 0; if (tmp<0) return 0;
ip[i]+=tmp; ip[i] = ip[i] | (char)tmp;
} }
return 32; return 32;
} }

@ -4,11 +4,11 @@
#include "socket.h" #include "socket.h"
#include "havealloca.h" #include "havealloca.h"
unsigned int scan_ip6if(const char* src,char* ip,uint32* scope_id) { size_t scan_ip6if(const char* src,char* ip,uint32* scope_id) {
int i=scan_ip6(src,ip); size_t i=scan_ip6(src,ip);
*scope_id=0; *scope_id=0;
if (src[i]=='%') { if (src[i]=='%') {
int j; size_t j;
char* tmp; char* tmp;
for (j=i+1; isalnum(src[j]); ++j) ; for (j=i+1; isalnum(src[j]); ++j) ;
if (!src[j]) if (!src[j])

@ -28,6 +28,6 @@ int socket_listen(int s,unsigned int backlog) {
} }
return r; return r;
#else #else
return listen(s, backlog); return listen(s, (int)backlog);
#endif #endif
} }

@ -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); int stralloc_catulong0(stralloc* sa,unsigned long int in,size_t n);
/* stralloc_catlong0 appends a '0' padded ASCII representation of in */ /* 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); int stralloc_catlong0(stralloc* sa,signed long int in,size_t n);
/* stralloc_free frees the storage associated with sa */ /* stralloc_free frees the storage associated with sa */
void stralloc_free(stralloc* sa); void stralloc_free(stralloc* sa);
#define stralloc_catlong(sa,l) (stralloc_catlong0((sa),(l),0)) #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_catuint0(sa,i,n) (stralloc_catulong0((sa),(i),(n)))
#define stralloc_catint0(sa,i,n) (stralloc_catlong0((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_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). */ /* remove last char. Return removed byte as unsigned char (or -1 if stralloc was empty). */
int stralloc_chop(stralloc* sa); int stralloc_chop(stralloc* sa);
@ -129,7 +133,7 @@ int buffer_putsa(buffer* b,stralloc* sa);
/* write stralloc to buffer and flush */ /* write stralloc to buffer and flush */
int buffer_putsaflush(buffer* b,stralloc* sa); 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 * EOF was hit or -1 on error. In contrast to the non-stralloc token
* functions, the separator is also put in the stralloc; use * functions, the separator is also put in the stralloc; use
* stralloc_chop or stralloc_chomp to get rid of it. */ * 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); 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! */ * Do not change the stralloc after this! */
void buffer_fromsa(buffer* b,stralloc* sa); void buffer_fromsa(buffer* b,stralloc* sa);
int buffer_tosa(buffer*b,stralloc* sa); /* write to sa, auto-growing it */
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus

@ -2,9 +2,11 @@
#include "fmt.h" #include "fmt.h"
int stralloc_catlong0(stralloc *sa,signed long int in,size_t n) { 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))) { int neg=-(in<0);
sa->len+=fmt_minus(sa->s+sa->len,in); if (neg) in=-in;
sa->len+=fmt_ulong0(sa->s+sa->len,in>=0?in:-in,n); 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; return 1;
} else } else
return 0; return 0;

4
t.c

@ -50,6 +50,10 @@ static int64 writecb(int64 fd,const void* buf,uint64 n) {
int main(int argc,char* argv[]) { int main(int argc,char* argv[]) {
char buf[1024]; char buf[1024];
size_t l; 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\\\\")); assert(fmt_jsonescape(buf,"foo\nbar\\",8)==14 && byte_equal(buf,14,"foo\\u000abar\\\\"));
memset(buf,0,sizeof(buf)); memset(buf,0,sizeof(buf));
assert(scan_jsonescape("foo\\u000abar\\\\",buf,&l)==14 && l==8 && byte_equal(buf,8,"foo\nbar\\")); assert(scan_jsonescape("foo\\u000abar\\\\",buf,&l)==14 && l==8 && byte_equal(buf,8,"foo\nbar\\"));

@ -5,12 +5,12 @@ void tai_pack(char *s,const struct tai *t)
uint64 x; uint64 x;
x = t->x; x = t->x;
s[7] = x & 255; x >>= 8; s[7] = (char)x; x >>= 8;
s[6] = x & 255; x >>= 8; s[6] = (char)x; x >>= 8;
s[5] = x & 255; x >>= 8; s[5] = (char)x; x >>= 8;
s[4] = x & 255; x >>= 8; s[4] = (char)x; x >>= 8;
s[3] = x & 255; x >>= 8; s[3] = (char)x; x >>= 8;
s[2] = x & 255; x >>= 8; s[2] = (char)x; x >>= 8;
s[1] = x & 255; x >>= 8; s[1] = (char)x; x >>= 8;
s[0] = x; s[0] = (char)x;
} }

@ -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->nano = u->nano + v->nano;
t->atto = u->atto + v->atto; t->atto = u->atto + v->atto;
if (t->atto > 999999999UL) { if (t->atto > 999999999UL) {
t->atto -= 1000000000UL; t->atto -= (uint32)1000000000UL;
++t->nano; ++t->nano;
} }
if (t->nano > 999999999UL) { if (t->nano > 999999999UL) {
t->nano -= 1000000000UL; t->nano -= (uint32)1000000000UL;
++t->sec.x; ++t->sec.x;
} }
} }

@ -4,7 +4,7 @@
void taia_addsec(struct taia *t,const struct taia *u,long secs) 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->nano = u->nano;
t->atto = u->atto; t->atto = u->atto;
} }

@ -4,8 +4,8 @@
void taia_half(tai6464* t,const tai6464* u) { void taia_half(tai6464* t,const tai6464* u) {
t->atto = u->atto >> 1; 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; 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; t->sec.x = u->sec.x >> 1;
} }

@ -23,7 +23,7 @@ void taia_now(struct taia *t)
struct timeval now; struct timeval now;
gettimeofday(&now,(struct timezone *) 0); gettimeofday(&now,(struct timezone *) 0);
tai_unix(&t->sec,now.tv_sec); 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; t->atto = 0;
#endif #endif
} }

@ -2,19 +2,19 @@
void taia_pack(char *s,const struct taia *t) void taia_pack(char *s,const struct taia *t)
{ {
unsigned long x; uint32 x;
tai_pack(s,&t->sec); tai_pack(s,&t->sec);
s += 8; s += 8;
x = t->atto; x = t->atto;
s[7] = x & 255; x >>= 8; s[7] = (char)x; x >>= 8;
s[6] = x & 255; x >>= 8; s[6] = (char)x; x >>= 8;
s[5] = x & 255; x >>= 8; s[5] = (char)x; x >>= 8;
s[4] = x; s[4] = (char)x;
x = t->nano; x = t->nano;
s[3] = x & 255; x >>= 8; s[3] = (char)x; x >>= 8;
s[2] = x & 255; x >>= 8; s[2] = (char)x; x >>= 8;
s[1] = x & 255; x >>= 8; s[1] = (char)x; x >>= 8;
s[0] = x; s[0] = (char)x;
} }

@ -4,18 +4,18 @@
void taia_sub(struct taia *t,const struct taia *u,const struct taia *v) void taia_sub(struct taia *t,const struct taia *u,const struct taia *v)
{ {
unsigned long unano = u->nano; uint32 unano = u->nano;
unsigned long uatto = u->atto; uint32 uatto = u->atto;
t->sec.x = u->sec.x - v->sec.x; t->sec.x = u->sec.x - v->sec.x;
t->nano = unano - v->nano; t->nano = unano - v->nano;
t->atto = uatto - v->atto; t->atto = uatto - v->atto;
if (t->atto > uatto) { if (t->atto > uatto) {
t->atto += 1000000000UL; t->atto += (uint32)1000000000UL;
--t->nano; --t->nano;
} }
if (t->nano > unano) { if (t->nano > unano) {
t->nano += 1000000000UL; t->nano += (uint32)1000000000UL;
--t->sec.x; --t->sec.x;
} }
} }

@ -2,7 +2,7 @@
void taia_unpack(const char* s,struct taia* t) void taia_unpack(const char* s,struct taia* t)
{ {
unsigned long x; uint32_t x;
tai_unpack(s,&t->sec); tai_unpack(s,&t->sec);
s += 8; s += 8;

@ -4,6 +4,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <fmt.h> #include <fmt.h>
#include <stralloc.h>
#include <buffer.h>
#include <scan.h> #include <scan.h>
#include <textcode.h> #include <textcode.h>
#include <byte.h> #include <byte.h>
@ -12,6 +14,7 @@
#include <uint64.h> #include <uint64.h>
#include <openreadclose.h> #include <openreadclose.h>
#include <mmap.h> #include <mmap.h>
#include <str.h>
char buf[100]; char buf[100];
stralloc sa; stralloc sa;
@ -355,5 +358,50 @@ int main() {
assert(stralloc_readyplus(&sa,(size_t)-1)==0); assert(stralloc_readyplus(&sa,(size_t)-1)==0);
assert(stralloc_ready(&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; return 0;
} }

@ -20,7 +20,7 @@ void iopause(iopause_fd *x,unsigned int len,struct taia *deadline,struct taia *s
taia_sub(&t,deadline,&t); taia_sub(&t,deadline,&t);
d = taia_approx(&t); d = taia_approx(&t);
if (d > 1000.0) d = 1000.0; 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) for (i = 0;i < len;++i)

Loading…
Cancel
Save