marshall.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Marshalling and unmarshalling.
  2. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <cc1plugin-config.h>
  16. #include <new>
  17. #include <string.h>
  18. #include "marshall.hh"
  19. #include "connection.hh"
  20. #include "rpc.hh"
  21. cc1_plugin::status
  22. cc1_plugin::unmarshall_check (connection *conn, unsigned long long check)
  23. {
  24. unsigned long long r;
  25. if (!unmarshall (conn, &r))
  26. return FAIL;
  27. return check == r ? OK : FAIL;
  28. }
  29. cc1_plugin::status
  30. cc1_plugin::marshall_intlike (connection *conn, unsigned long long val)
  31. {
  32. if (!conn->send ('i'))
  33. return FAIL;
  34. return conn->send (&val, sizeof (val));
  35. }
  36. cc1_plugin::status
  37. cc1_plugin::unmarshall_intlike (connection *conn, unsigned long long *result)
  38. {
  39. if (!conn->require ('i'))
  40. return FAIL;
  41. return conn->get (result, sizeof (*result));
  42. }
  43. cc1_plugin::status
  44. cc1_plugin::marshall (connection *conn, const char *str)
  45. {
  46. if (!conn->send ('s'))
  47. return FAIL;
  48. unsigned long long len = str == NULL ? -1ULL : strlen (str);
  49. if (!conn->send (&len, sizeof (len)))
  50. return FAIL;
  51. if (str == NULL)
  52. return OK;
  53. return conn->send (str, len);
  54. }
  55. cc1_plugin::status
  56. cc1_plugin::unmarshall (connection *conn, char **result)
  57. {
  58. unsigned long long len;
  59. if (!conn->require ('s'))
  60. return FAIL;
  61. if (!conn->get (&len, sizeof (len)))
  62. return FAIL;
  63. if (len == -1ULL)
  64. {
  65. *result = NULL;
  66. return OK;
  67. }
  68. char *str = new (std::nothrow) char[len + 1];
  69. if (str == NULL)
  70. return FAIL;
  71. if (!conn->get (str, len))
  72. {
  73. delete[] str;
  74. return FAIL;
  75. }
  76. str[len] = '\0';
  77. *result = str;
  78. return OK;
  79. }
  80. cc1_plugin::status
  81. cc1_plugin::marshall_array_start (connection *conn, char id,
  82. size_t n_elements)
  83. {
  84. if (!conn->send (id))
  85. return FAIL;
  86. unsigned long long r = n_elements;
  87. if (!conn->send (&r, sizeof (r)))
  88. return FAIL;
  89. return OK;
  90. }
  91. cc1_plugin::status
  92. cc1_plugin::marshall_array_elmts (connection *conn, size_t n_bytes,
  93. void *elements)
  94. {
  95. return conn->send (elements, n_bytes);
  96. }
  97. cc1_plugin::status
  98. cc1_plugin::unmarshall_array_start (connection *conn, char id,
  99. size_t *n_elements)
  100. {
  101. unsigned long long len;
  102. if (!conn->require (id))
  103. return FAIL;
  104. if (!conn->get (&len, sizeof (len)))
  105. return FAIL;
  106. *n_elements = len;
  107. return OK;
  108. }
  109. cc1_plugin::status
  110. cc1_plugin::unmarshall_array_elmts (connection *conn, size_t n_bytes,
  111. void *elements)
  112. {
  113. return conn->get (elements, n_bytes);
  114. }
  115. cc1_plugin::status
  116. cc1_plugin::marshall (connection *conn, const gcc_type_array *a)
  117. {
  118. size_t len;
  119. if (a)
  120. len = a->n_elements;
  121. else
  122. len = (size_t)-1;
  123. if (!marshall_array_start (conn, 'a', len))
  124. return FAIL;
  125. if (!a)
  126. return OK;
  127. return marshall_array_elmts (conn, len * sizeof (a->elements[0]),
  128. a->elements);
  129. }
  130. cc1_plugin::status
  131. cc1_plugin::unmarshall (connection *conn, gcc_type_array **result)
  132. {
  133. size_t len;
  134. if (!unmarshall_array_start (conn, 'a', &len))
  135. return FAIL;
  136. if (len == (size_t)-1)
  137. {
  138. *result = NULL;
  139. return OK;
  140. }
  141. cc1_plugin::unique_ptr<gcc_type_array> gta (new gcc_type_array {});
  142. gta->n_elements = len;
  143. gta->elements = new gcc_type[len];
  144. if (!unmarshall_array_elmts (conn,
  145. len * sizeof (gta->elements[0]),
  146. gta->elements))
  147. return FAIL;
  148. *result = gta.release ();
  149. return OK;
  150. }