BencMessageReader.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <http://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 "exception/Except.h"
  20. #include "exception/Jmp.h"
  21. #include "wire/Message.h"
  22. #include "util/Base10.h"
  23. static Object* readGeneric(struct Message* msg, struct Allocator* alloc, struct Except* eh);
  24. static int64_t readInt(struct Message* msg, struct Allocator* alloc, struct Except* eh)
  25. {
  26. int64_t num = Base10_read(msg, eh);
  27. if (Message_pop8(msg, eh) != 'e') {
  28. Except_throw(eh, "Int not terminated with 'e'");
  29. }
  30. return num;
  31. }
  32. static String* readString(struct Message* msg, struct Allocator* alloc, struct Except* eh)
  33. {
  34. int64_t len = Base10_read(msg, eh);
  35. if (len < 0) {
  36. Except_throw(eh, "Negative string length");
  37. }
  38. if (Message_pop8(msg, eh) != ':') {
  39. Except_throw(eh, "String not deliniated with a ':'");
  40. }
  41. if (len > msg->length) {
  42. Except_throw(eh, "String too long");
  43. }
  44. String* str = String_newBinary(NULL, len, alloc);
  45. Message_pop(msg, str->bytes, len, eh);
  46. return str;
  47. }
  48. static List* readList(struct Message* msg, struct Allocator* alloc, struct Except* eh)
  49. {
  50. struct List_Item* last = NULL;
  51. struct List_Item* first = NULL;
  52. for (;;) {
  53. uint8_t chr = Message_pop8(msg, eh);
  54. if (chr == 'e') {
  55. List* out = Allocator_malloc(alloc, sizeof(List));
  56. *out = first;
  57. return out;
  58. }
  59. Message_push8(msg, chr, eh);
  60. struct List_Item* item = Allocator_malloc(alloc, sizeof(struct List_Item));
  61. item->elem = readGeneric(msg, alloc, eh);
  62. if (!last) {
  63. first = last = item;
  64. } else {
  65. last->next = item;
  66. last = item;
  67. }
  68. }
  69. }
  70. static Dict* readDict(struct Message* msg, struct Allocator* alloc, struct Except* eh)
  71. {
  72. struct Dict_Entry* last = NULL;
  73. for (;;) {
  74. uint8_t chr = Message_pop8(msg, eh);
  75. if (chr == 'e') {
  76. Dict* out = Allocator_malloc(alloc, sizeof(Dict));
  77. *out = last;
  78. return out;
  79. }
  80. Message_push8(msg, chr, eh);
  81. struct Dict_Entry* entry = Allocator_malloc(alloc, sizeof(struct Dict_Entry));
  82. entry->key = readString(msg, alloc, eh);
  83. entry->val = readGeneric(msg, alloc, eh);
  84. entry->next = last;
  85. last = entry;
  86. }
  87. }
  88. static Object* readGeneric(struct Message* msg, struct Allocator* alloc, struct Except* eh)
  89. {
  90. uint8_t chr = Message_pop8(msg, eh);
  91. Object* out = Allocator_calloc(alloc, sizeof(Object), 1);
  92. switch (chr) {
  93. case 'l': {
  94. out->type = Object_LIST;
  95. out->as.list = readList(msg, alloc, eh);
  96. break;
  97. }
  98. case 'd': {
  99. out->type = Object_DICT;
  100. out->as.dictionary = readDict(msg, alloc, eh);
  101. break;
  102. }
  103. case 'i': {
  104. out->type = Object_INTEGER;
  105. out->as.number = readInt(msg, alloc, eh);
  106. break;
  107. }
  108. case '0':
  109. case '1':
  110. case '2':
  111. case '3':
  112. case '4':
  113. case '5':
  114. case '6':
  115. case '7':
  116. case '8':
  117. case '9': {
  118. out->type = Object_STRING;
  119. Message_push8(msg, chr, eh);
  120. out->as.string = readString(msg, alloc, eh);
  121. break;
  122. }
  123. default: Except_throw(eh, "Unexpected character in message [%c]", (char)chr);
  124. }
  125. return out;
  126. }
  127. Dict* BencMessageReader_read(struct Message* msg, struct Allocator* alloc, struct Except* eh)
  128. {
  129. if (Message_pop8(msg, eh) != 'd') {
  130. Except_throw(eh, "Message does not begin with a 'd' to open the dictionary");
  131. }
  132. return readDict(msg, alloc, eh);
  133. }
  134. char* BencMessageReader_readNoExcept(struct Message* msg, struct Allocator* alloc, Dict** outPtr)
  135. {
  136. struct Jmp j;
  137. Jmp_try(j) {
  138. Dict* out = BencMessageReader_read(msg, alloc, &j.handler);
  139. *outPtr = out;
  140. return NULL;
  141. } Jmp_catch {
  142. String* str = String_new(j.message, alloc);
  143. return str->bytes;
  144. }
  145. }