buffer stuff from Olaf and ip[46]_fmt stuff from me.

master
leitner 24 years ago
parent 6838f0f0bb
commit fc4193e1ce

@ -6,9 +6,10 @@ int buffer_get_token(buffer* b,char* x,unsigned int len,const char* charset,unsi
int blen;
for (blen=0;blen<len;++blen) {
if ((r=buffer_getc(b,*x))<0) return r;
if (r==0) return blen;
if (byte_chr(charset,clen,*x)<clen) return blen;
register int r;
if ((r=buffer_getc(b,x))<0) return r;
if (r==0) break;
if (byte_chr(charset,setlen,*x)<setlen) break;
++x;
}
return blen;

@ -5,8 +5,9 @@ int buffer_getn(buffer* b,char* x,unsigned int len) {
int blen;
for(blen=0;blen<len;++blen) {
register int r;
if ((r=buffer_getc(b,x))<0) return r;
if (r==0) return blen;
if (r==0) break;
++x;
}
return blen;

@ -0,0 +1,9 @@
#ifndef IP4_H
#define IP4_H
extern unsigned int ip4_scan(const char *src,char *ip);
extern unsigned int ip4_fmt(char *dest,const char *ip);
#define IP4_FMT 20
#endif

@ -0,0 +1,27 @@
.TH ip4_fmt 3
.SH NAME
ip4_fmt \- write a formatted ASCII representation of an IPv4 number
.SH SYNTAX
.B #include <ip4.h>
unsigned int \fBip4_fmt\fP(char *\fIdest\fR,const char \fIip\fR[4]);
.SH DESCRIPTION
ip4_fmt formats an IPv4 number in dotted-decimal ASCII representation
from \fIip\fR and writes the result into \fIdest\fR. It returns the
number of bytes written.
If \fIdest\fR equals FMT_LEN (i.e. is zero), ip4_fmt returns the number
of bytes it would have written.
ip4_fmt does not append \\0.
For convenience, ip4.h defines the integer IP4_FMT to be big enough to
contain every possible ip4_fmt output plus \\0.
.SH EXAMPLE
#include <ip4.h>
char buf[IP4_FMT];
char ip[4];
buf[ip4_fmt(buf,ip)]=0;
.SH "SEE ALSO"
ip4_scan(3), ip6_fmt(3)

@ -0,0 +1,16 @@
#include "fmt.h"
#include "ip4.h"
unsigned int ip4_fmt(char *s,const char ip[4])
{
unsigned int len;
int i;
len = 0;
for (i=0; i<4; ++i) {
register unsigned int j;
len+=(j=fmt_ulong(s,(unsigned long) (unsigned char) ip[i]))+1;
if (s && i<3) { s+=i; *s++='.'; }
}
return len;
}

@ -0,0 +1,32 @@
.TH ip6_fmt 3
.SH NAME
ip6_fmt \- write a formatted ASCII representation of an IPv6 number
.SH SYNTAX
.B #include <ip6.h>
unsigned int \fBip6_fmt\fP(char *\fIdest\fR,const char \fIip\fR[16]);
.SH DESCRIPTION
ip6_fmt formats an IPv6 number in ASCII representation from \fIip\fR and
writes the result into \fIdest\fR. It returns the number of bytes
written.
ip6_fmt will apply "::" compression to the output.
If \fIip\fR is an IPv4-mapped IPv6 address, ip6_fmt will output the last
4 bytes as IPv4 number in dotted-decimal notation.
If \fIdest\fR equals FMT_LEN (i.e. is zero), ip6_fmt returns the number
of bytes it would have written.
ip6_fmt does not append \\0.
For convenience, ip6.h defines the integer IP6_FMT to be big enough to
contain every possible ip6_fmt output plus \\0.
.SH EXAMPLE
#include <ip6.h>
char buf[IP6_FMT];
char ip[16];
buf[ip6_fmt(buf,ip)]=0;
.SH "SEE ALSO"
ip6_scan(3), ip4_fmt(3)

@ -0,0 +1,47 @@
#include "fmt.h"
#include "byte.h"
#include "ip4.h"
#include "ip6.h"
unsigned int ip6_fmt(char *s,const char ip[16])
{
unsigned int len;
unsigned int i;
unsigned int temp;
unsigned int compressing;
int j;
len = 0; compressing = 0;
for (j=0; j<16; j+=2) {
if (j==12 && ip6_isv4mapped(ip)) {
temp=ip4_fmt(s,ip+12);
len+=temp;
if (s) s+=temp;
break;
}
temp = ((unsigned long) (unsigned char) ip[j] << 8) +
(unsigned long) (unsigned char) ip[j+1];
if (temp == 0) {
if (!compressing) {
compressing=1;
if (j==0) {
if (s) *s++=':'; ++len;
}
}
} else {
if (compressing) {
compressing=0;
if (s) *s++=':'; ++len;
}
i = fmt_xlong(s,temp); len += i; if (s) s += i;
if (j<14) {
if (s) *s++ = ':';
++len;
}
}
}
/* if (s) *s=0; */
return len;
}

@ -0,0 +1,21 @@
#include "ip6.h"
static char tohex(char num) {
if (num<10)
return num+'0';
else if (num<16)
return num-10+'a';
else
return -1;
}
unsigned int ip6_fmt_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);
}
return 32;
}
Loading…
Cancel
Save