string.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <venti.h>
  12. int
  13. vtputstring(Packet *p, char *s)
  14. {
  15. uint8_t buf[2];
  16. int n;
  17. if(s == nil){
  18. werrstr("null string in packet");
  19. return -1;
  20. }
  21. n = strlen(s);
  22. if(n > VtMaxStringSize){
  23. werrstr("string too long in packet");
  24. return -1;
  25. }
  26. buf[0] = n>>8;
  27. buf[1] = n;
  28. packetappend(p, buf, 2);
  29. packetappend(p, (uint8_t*)s, n);
  30. return 0;
  31. }
  32. int
  33. vtgetstring(Packet *p, char **ps)
  34. {
  35. uint8_t buf[2];
  36. int n;
  37. char *s;
  38. if(packetconsume(p, buf, 2) < 0)
  39. return -1;
  40. n = (buf[0]<<8) + buf[1];
  41. if(n > VtMaxStringSize) {
  42. werrstr("string too long in packet");
  43. return -1;
  44. }
  45. s = vtmalloc(n+1);
  46. if(packetconsume(p, (uint8_t*)s, n) < 0){
  47. vtfree(s);
  48. return -1;
  49. }
  50. s[n] = 0;
  51. *ps = s;
  52. return 0;
  53. }