marshall.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Marshalling and unmarshalling.
  2. Copyright (C) 2014 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. cc1_plugin::status
  21. cc1_plugin::unmarshall_check (connection *conn, unsigned long long check)
  22. {
  23. unsigned long long r;
  24. if (!unmarshall (conn, &r))
  25. return FAIL;
  26. return check == r ? OK : FAIL;
  27. }
  28. cc1_plugin::status
  29. cc1_plugin::marshall_intlike (connection *conn, unsigned long long val)
  30. {
  31. if (!conn->send ('i'))
  32. return FAIL;
  33. return conn->send (&val, sizeof (val));
  34. }
  35. cc1_plugin::status
  36. cc1_plugin::unmarshall_intlike (connection *conn, unsigned long long *result)
  37. {
  38. if (!conn->require ('i'))
  39. return FAIL;
  40. return conn->get (result, sizeof (*result));
  41. }
  42. cc1_plugin::status
  43. cc1_plugin::unmarshall (connection *conn, enum gcc_c_symbol_kind *result)
  44. {
  45. protocol_int p;
  46. if (!unmarshall_intlike (conn, &p))
  47. return FAIL;
  48. *result = (enum gcc_c_symbol_kind) p;
  49. return OK;
  50. }
  51. cc1_plugin::status
  52. cc1_plugin::unmarshall (connection *conn, enum gcc_c_oracle_request *result)
  53. {
  54. protocol_int p;
  55. if (!unmarshall_intlike (conn, &p))
  56. return FAIL;
  57. *result = (enum gcc_c_oracle_request) p;
  58. return OK;
  59. }
  60. cc1_plugin::status
  61. cc1_plugin::unmarshall (connection *conn, enum gcc_qualifiers *result)
  62. {
  63. protocol_int p;
  64. if (!unmarshall_intlike (conn, &p))
  65. return FAIL;
  66. *result = (enum gcc_qualifiers) p;
  67. return OK;
  68. }
  69. cc1_plugin::status
  70. cc1_plugin::marshall (connection *conn, const char *str)
  71. {
  72. if (!conn->send ('s'))
  73. return FAIL;
  74. unsigned long long len = str == NULL ? -1ULL : strlen (str);
  75. if (!conn->send (&len, sizeof (len)))
  76. return FAIL;
  77. if (str == NULL)
  78. return OK;
  79. return conn->send (str, len);
  80. }
  81. cc1_plugin::status
  82. cc1_plugin::unmarshall (connection *conn, char **result)
  83. {
  84. unsigned long long len;
  85. if (!conn->require ('s'))
  86. return FAIL;
  87. if (!conn->get (&len, sizeof (len)))
  88. return FAIL;
  89. if (len == -1ULL)
  90. {
  91. *result = NULL;
  92. return OK;
  93. }
  94. char *str = new (std::nothrow) char[len + 1];
  95. if (str == NULL)
  96. return FAIL;
  97. if (!conn->get (str, len))
  98. {
  99. delete[] str;
  100. return FAIL;
  101. }
  102. str[len] = '\0';
  103. *result = str;
  104. return OK;
  105. }
  106. cc1_plugin::status
  107. cc1_plugin::marshall (connection *conn, const gcc_type_array *a)
  108. {
  109. if (!conn->send ('a'))
  110. return FAIL;
  111. unsigned long long r = a->n_elements;
  112. if (!conn->send (&r, sizeof (r)))
  113. return FAIL;
  114. return conn->send (a->elements, r * sizeof (a->elements[0]));
  115. }
  116. cc1_plugin::status
  117. cc1_plugin::unmarshall (connection *conn, gcc_type_array **result)
  118. {
  119. unsigned long long len;
  120. if (!conn->require ('a'))
  121. return FAIL;
  122. if (!conn->get (&len, sizeof (len)))
  123. return FAIL;
  124. *result = new gcc_type_array;
  125. (*result)->n_elements = len;
  126. (*result)->elements = new gcc_type[len];
  127. if (!conn->get ((*result)->elements, len * sizeof ((*result)->elements[0])))
  128. {
  129. delete[] (*result)->elements;
  130. delete *result;
  131. return FAIL;
  132. }
  133. return OK;
  134. }