plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

string.c (736B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <venti.h>
      4 
      5 int
      6 vtputstring(Packet *p, char *s)
      7 {
      8 	uchar buf[2];
      9 	int n;
     10 
     11 	if(s == nil){
     12 		werrstr("null string in packet");
     13 		return -1;
     14 	}
     15 	n = strlen(s);
     16 	if(n > VtMaxStringSize){
     17 		werrstr("string too long in packet");
     18 		return -1;
     19 	}
     20 	buf[0] = n>>8;
     21 	buf[1] = n;
     22 	packetappend(p, buf, 2);
     23 	packetappend(p, (uchar*)s, n);
     24 	return 0;
     25 }
     26 
     27 int
     28 vtgetstring(Packet *p, char **ps)
     29 {
     30 	uchar buf[2];
     31 	int n;
     32 	char *s;
     33 
     34 	if(packetconsume(p, buf, 2) < 0)
     35 		return -1;
     36 	n = (buf[0]<<8) + buf[1];
     37 	if(n > VtMaxStringSize) {
     38 		werrstr("string too long in packet");
     39 		return -1;
     40 	}
     41 	s = vtmalloc(n+1);
     42 	if(packetconsume(p, (uchar*)s, n) < 0){
     43 		vtfree(s);
     44 		return -1;
     45 	}
     46 	s[n] = 0;
     47 	*ps = s;
     48 	return 0;
     49 }