xdrstring.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <string.h>
  2. #include "xdr.h"
  3. int db__send_string_array(dbString * a, int count)
  4. {
  5. int i;
  6. int stat;
  7. stat = db__send_int(count);
  8. for (i = 0; stat == DB_OK && i < count; i++)
  9. stat = db__send_string(&a[i]);
  10. return stat;
  11. }
  12. /* note: dbString *a; ...(...,&a...) */
  13. int db__recv_string_array(dbString ** a, int *n)
  14. {
  15. int i, count;
  16. int stat;
  17. dbString *b;
  18. *n = 0;
  19. *a = NULL;
  20. stat = db__recv_int(&count);
  21. if (stat != DB_OK)
  22. return stat;
  23. if (count < 0) {
  24. db_protocol_error();
  25. return DB_PROTOCOL_ERR;
  26. }
  27. b = db_alloc_string_array(count);
  28. if (b == NULL)
  29. return DB_MEMORY_ERR;
  30. for (i = 0; i < count; i++) {
  31. stat = db__recv_string(&b[i]);
  32. if (stat != DB_OK) {
  33. db_free_string_array(b, count);
  34. return stat;
  35. }
  36. }
  37. *n = count;
  38. *a = b;
  39. return DB_OK;
  40. }
  41. int db__send_string(dbString * x)
  42. {
  43. int stat = DB_OK;
  44. const char *s = db_get_string(x);
  45. int len = s ? strlen(s) + 1 : 1;
  46. if (!s)
  47. s = ""; /* don't send a NULL string */
  48. if (!db__send(&len, sizeof(len)))
  49. stat = DB_PROTOCOL_ERR;
  50. if (!db__send(s, len))
  51. stat = DB_PROTOCOL_ERR;
  52. if (stat == DB_PROTOCOL_ERR)
  53. db_protocol_error();
  54. return stat;
  55. }
  56. /*
  57. * db__recv_string (dbString *x)
  58. * reads a string from transport
  59. *
  60. * returns DB_OK, DB_MEMORY_ERR, or DB_PROTOCOL_ERR
  61. * x.s will be NULL if error
  62. *
  63. * NOTE: caller MUST initialize x by calling db_init_string()
  64. */
  65. int db__recv_string(dbString * x)
  66. {
  67. int stat = DB_OK;
  68. int len;
  69. char *s;
  70. if (!db__recv(&len, sizeof(len)))
  71. stat = DB_PROTOCOL_ERR;
  72. if (len <= 0) /* len will include the null byte */
  73. stat = DB_PROTOCOL_ERR;
  74. if (db_enlarge_string(x, len) != DB_OK)
  75. stat = DB_PROTOCOL_ERR;
  76. s = db_get_string(x);
  77. if (!db__recv(s, len))
  78. stat = DB_PROTOCOL_ERR;
  79. if (stat == DB_PROTOCOL_ERR)
  80. db_protocol_error();
  81. return stat;
  82. }
  83. int db__send_Cstring(const char *s)
  84. {
  85. dbString x;
  86. db_init_string(&x);
  87. db_set_string_no_copy(&x, (char *)s);
  88. return db__send_string(&x);
  89. }