#75, add txt read/write

This commit is contained in:
Erik Ekman 2009-09-20 15:11:04 +00:00
parent 1c8f6e522f
commit 26d59a9886
2 changed files with 63 additions and 0 deletions

View File

@ -125,6 +125,35 @@ readdata(char *packet, char **src, char *dst, size_t len)
return len; return len;
} }
int
readtxtbin(char *packet, char **src, size_t srcremain, char *dst, size_t dstremain)
{
unsigned char *uc;
int tocopy;
int dstused = 0;
while (srcremain > 0)
{
uc = (unsigned char*) (*src);
tocopy = *uc;
(*src)++;
srcremain--;
if (tocopy > srcremain)
return 0; /* illegal, better have nothing */
if (tocopy > dstremain)
return 0; /* doesn't fit, better have nothing */
memcpy(dst, *src, tocopy);
dst += tocopy;
(*src) += tocopy;
srcremain -= tocopy;
dstremain -= tocopy;
dstused += tocopy;
}
return dstused;
}
int int
putname(char **buf, size_t buflen, const char *host) putname(char **buf, size_t buflen, const char *host)
{ {
@ -212,3 +241,35 @@ putdata(char **dst, char *data, size_t len)
return len; return len;
} }
int
puttxtbin(char **buf, size_t bufremain, char *from, size_t fromremain)
{
unsigned char uc;
unsigned char *ucp = &uc;
char *cp = (char *) ucp;
int tocopy;
int bufused = 0;
while (fromremain > 0)
{
tocopy = fromremain;
if (tocopy > 252)
tocopy = 252; /* allow off-by-1s in caches etc */
if (tocopy + 1 > bufremain)
return -1; /* doesn't fit, better have nothing */
uc = tocopy;
**buf = *cp;
(*buf)++;
bufremain--;
bufused++;
memcpy(*buf, from, tocopy);
(*buf) += tocopy;
from += tocopy;
bufremain -= tocopy;
fromremain -= tocopy;
bufused += tocopy;
}
return bufused;
}

View File

@ -21,11 +21,13 @@ int readname(char *, int, char **, char *, size_t);
int readshort(char *, char **, short *); int readshort(char *, char **, short *);
int readlong(char *, char **, uint32_t *); int readlong(char *, char **, uint32_t *);
int readdata(char *, char **, char *, size_t); int readdata(char *, char **, char *, size_t);
int readtxtbin(char *, char **, size_t, char *, size_t);
int putname(char **, size_t, const char *); int putname(char **, size_t, const char *);
int putbyte(char **, unsigned char); int putbyte(char **, unsigned char);
int putshort(char **, unsigned short); int putshort(char **, unsigned short);
int putlong(char **, uint32_t); int putlong(char **, uint32_t);
int putdata(char **, char *, size_t); int putdata(char **, char *, size_t);
int puttxtbin(char **, size_t, char *, size_t);
#endif #endif