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 <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. for (;;) {
  52. uint8_t chr = Message_pop8(msg, eh);
  53. if (chr == 'e') {
  54. List* out = Allocator_malloc(alloc, sizeof(List));
  55. *out = last;
  56. return out;
  57. }
  58. Message_push8(msg, chr, eh);
  59. struct List_Item* item = Allocator_malloc(alloc, sizeof(struct List_Item));
  60. item->elem = readGeneric(msg, alloc, eh);
  61. item->next = last;
  62. last = item;
  63. }
  64. }
  65. static Dict* readDict(struct Message* msg, struct Allocator* alloc, struct Except* eh)
  66. {
  67. struct Dict_Entry* last = NULL;
  68. for (;;) {
  69. uint8_t chr = Message_pop8(msg, eh);
  70. if (chr == 'e') {
  71. Dict* out = Allocator_malloc(alloc, sizeof(Dict));
  72. *out = last;
  73. return out;
  74. }
  75. Message_push8(msg, chr, eh);
  76. struct Dict_Entry* entry = Allocator_malloc(alloc, sizeof(struct Dict_Entry));
  77. entry->key = readString(msg, alloc, eh);
  78. entry->val = readGeneric(msg, alloc, eh);
  79. entry->next = last;
  80. last = entry;
  81. }
  82. }
  83. static Object* readGeneric(struct Message* msg, struct Allocator* alloc, struct Except* eh)
  84. {
  85. uint8_t chr = Message_pop8(msg, eh);
  86. Object* out = Allocator_calloc(alloc, sizeof(Object), 1);
  87. switch (chr) {
  88. case 'l': {
  89. out->type = Object_LIST;
  90. out->as.list = readList(msg, alloc, eh);
  91. break;
  92. }
  93. case 'd': {
  94. out->type = Object_DICT;
  95. out->as.dictionary = readDict(msg, alloc, eh);
  96. break;
  97. }
  98. case 'i': {
  99. out->type = Object_INTEGER;
  100. out->as.number = readInt(msg, alloc, eh);
  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. Message_push8(msg, chr, eh);
  115. out->as.string = readString(msg, alloc, eh);
  116. break;
  117. }
  118. default: Except_throw(eh, "Unexpected character in message [%c]", (char)chr);
  119. }
  120. return out;
  121. }
  122. Dict* BencMessageReader_read(struct Message* msg, struct Allocator* alloc, struct Except* eh)
  123. {
  124. if (Message_pop8(msg, eh) != 'd') {
  125. Except_throw(eh, "Message does not begin with a 'd' to open the dictionary");
  126. }
  127. return readDict(msg, alloc, eh);
  128. }
  129. char* BencMessageReader_readNoExcept(struct Message* msg, struct Allocator* alloc, Dict** outPtr)
  130. {
  131. struct Jmp j;
  132. Jmp_try(j) {
  133. Dict* out = BencMessageReader_read(msg, alloc, &j.handler);
  134. *outPtr = out;
  135. return NULL;
  136. } Jmp_catch {
  137. String* str = String_new(j.message, alloc);
  138. return str->bytes;
  139. }
  140. }