1
0

BencMessageReader.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "benc/Object.h"
  16. #include "benc/String.h"
  17. #include "benc/Dict.h"
  18. #include "benc/List.h"
  19. #include "benc/serialization/standard/BencMessageReader.h"
  20. #include "exception/Er.h"
  21. #include "wire/Message.h"
  22. #include "util/Base10.h"
  23. static Er_DEFUN(Object* readGeneric(struct Message* msg, struct Allocator* alloc));
  24. static Er_DEFUN(int64_t readInt(struct Message* msg, struct Allocator* alloc))
  25. {
  26. int64_t num = Er(Base10_read(msg));
  27. if (Er(Message_epop8(msg)) != 'e') {
  28. Er_raise(Message_getAlloc(msg), "Int not terminated with 'e'");
  29. }
  30. Er_ret(num);
  31. }
  32. static Er_DEFUN(String* readString(struct Message* msg, struct Allocator* alloc))
  33. {
  34. int64_t len = Er(Base10_read(msg));
  35. if (len < 0) {
  36. Er_raise(alloc, "Negative string length");
  37. }
  38. if (Er(Message_epop8(msg)) != ':') {
  39. Er_raise(alloc, "String not deliniated with a ':'");
  40. }
  41. if (len > Message_getLength(msg)) {
  42. Er_raise(alloc, "String too long");
  43. }
  44. String* str = String_newBinary(NULL, len, alloc);
  45. Er(Message_epop(msg, str->bytes, len));
  46. Er_ret(str);
  47. }
  48. static Er_DEFUN(List* readList(struct Message* msg, struct Allocator* alloc))
  49. {
  50. struct List_Item* last = NULL;
  51. for (;;) {
  52. uint8_t chr = Er(Message_epop8(msg));
  53. if (chr == 'e') {
  54. List* out = Allocator_malloc(alloc, sizeof(List));
  55. *out = last;
  56. Er_ret(out);
  57. }
  58. Er(Message_epush8(msg, chr));
  59. struct List_Item* item = Allocator_malloc(alloc, sizeof(struct List_Item));
  60. item->elem = Er(readGeneric(msg, alloc));
  61. item->next = last;
  62. last = item;
  63. }
  64. }
  65. static Er_DEFUN(Dict* readDict(struct Message* msg, struct Allocator* alloc))
  66. {
  67. struct Dict_Entry* last = NULL;
  68. for (;;) {
  69. uint8_t chr = Er(Message_epop8(msg));
  70. if (chr == 'e') {
  71. Dict* out = Allocator_malloc(alloc, sizeof(Dict));
  72. *out = last;
  73. Er_ret(out);
  74. }
  75. Er(Message_epush8(msg, chr));
  76. struct Dict_Entry* entry = Allocator_malloc(alloc, sizeof(struct Dict_Entry));
  77. entry->key = Er(readString(msg, alloc));
  78. entry->val = Er(readGeneric(msg, alloc));
  79. entry->next = last;
  80. last = entry;
  81. }
  82. }
  83. static Er_DEFUN(Object* readGeneric(struct Message* msg, struct Allocator* alloc))
  84. {
  85. uint8_t chr = Er(Message_epop8(msg));
  86. Object* out = Allocator_calloc(alloc, sizeof(Object), 1);
  87. switch (chr) {
  88. case 'l': {
  89. out->type = Object_LIST;
  90. out->as.list = Er(readList(msg, alloc));
  91. break;
  92. }
  93. case 'd': {
  94. out->type = Object_DICT;
  95. out->as.dictionary = Er(readDict(msg, alloc));
  96. break;
  97. }
  98. case 'i': {
  99. out->type = Object_INTEGER;
  100. out->as.number = Er(readInt(msg, alloc));
  101. break;
  102. }
  103. case '0':
  104. case '1':
  105. case '2':
  106. case '3':
  107. case '4':
  108. case '5':
  109. case '6':
  110. case '7':
  111. case '8':
  112. case '9': {
  113. out->type = Object_STRING;
  114. Er(Message_epush8(msg, chr));
  115. out->as.string = Er(readString(msg, alloc));
  116. break;
  117. }
  118. default: Er_raise(alloc, "Unexpected character in message [%c]", (char)chr);
  119. }
  120. Er_ret(out);
  121. }
  122. Er_DEFUN(Dict* BencMessageReader_read(struct Message* msg, struct Allocator* alloc))
  123. {
  124. if (Er(Message_epop8h(msg)) != 'd') {
  125. Er_raise(alloc, "Message does not begin with a 'd' to open the dictionary");
  126. }
  127. Dict* out = Er(readDict(msg, alloc));
  128. Er_ret(out);
  129. }
  130. const char* BencMessageReader_readNoExcept(
  131. struct Message* msg, struct Allocator* alloc, Dict** outPtr)
  132. {
  133. struct Er_Ret* er = NULL;
  134. Dict* out = Er_check(&er, BencMessageReader_read(msg, alloc));
  135. if (er) {
  136. return er->message;
  137. }
  138. *outPtr = out;
  139. return NULL;
  140. }