BencSerializer.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #ifndef BencSerializer_H
  16. #define BencSerializer_H
  17. #include "benc/Object.h"
  18. #include "io/Writer.h"
  19. #include "io/Reader.h"
  20. struct BencSerializer
  21. {
  22. /**
  23. * Serialize a string and write to a writer.
  24. *
  25. * @param writer the Writer to write to.
  26. * @param string the string to write.
  27. * @return whatever the Writer returns when writing.
  28. */
  29. int32_t (* const serializeString)(struct Writer* writer,
  30. const String* string);
  31. /**
  32. * Parse a string, reading in with the first callback and writing to the second.
  33. *
  34. * @param reader a Reader which will be asked for the data to parse. This implementation
  35. * assumes the reader's pointer is alligned on the first digit of the length
  36. * of the string and will leave the read pointer on the first character AFTER
  37. * the end of the string.
  38. * @param writer a Allocator which will be used to store data.
  39. * @param stringPointer a pointer which will be set to the string.
  40. * @return 0 if everything goes well, -1 if NULL returned by the writer indicating an array
  41. * overflow, -2 if -1 returned by the reader indicating an array underflow,
  42. * -3 if content unparsable.
  43. */
  44. int32_t (* const parseString)(struct Reader* reader,
  45. struct Allocator* allocator,
  46. String** stringPointer);
  47. /**
  48. * Write an integer as decimal in bencoded format.
  49. * the integer 10 would be written as "i10e"
  50. *
  51. * @param writer the Writer to write to.
  52. * @param integer the number to write.
  53. * @return whatever the Writer returns when writing.
  54. */
  55. int32_t (* const serializeint64_t)(struct Writer* writer,
  56. int64_t integer);
  57. /**
  58. * Parse an integer, read in with the reader and set the intPointer to the value of the int.
  59. *
  60. * @param reader a Reader which will be asked for the data to parse. This implementation
  61. * assumes the reader's pointer is alligned on the 'i' which begins the integer
  62. * and will leave the read pointer on the first character AFTER the 'e' which
  63. * ends the integer.
  64. * @param output a pointer to a memory location which will be set to the value of the int.
  65. * @return 0 if everything goes well, -2 if -1 returned by the reader indicating an
  66. * array underflow, -3 if content unparsable.
  67. */
  68. int32_t (* const parseint64_t)(struct Reader* reader,
  69. int64_t* output);
  70. /**
  71. * Serialize a list.
  72. *
  73. * @param writer the Writer to write to.
  74. * @param List the list to serialize.
  75. * @return whatever the Writer returns when writing.
  76. */
  77. int32_t (* const serializeList)(struct Writer* writer,
  78. const List* list);
  79. /**
  80. * Parse a list.
  81. *
  82. * @param reader a Reader which will be asked for the data to parse. This implementation
  83. * assumes the reader's pointer is alligned on the 'l' which signifies the
  84. * beginning of the list. This will leave the pointer on the first character
  85. * AFTER the 'e' which signifies the end of the list.
  86. * @param allocator a Allocator which will be used to store data.
  87. * @param output a pointer which will be set to the location of the List.
  88. * @return 0 if everything goes well, -2 if -1 returned by the reader indicating an array
  89. * underflow, -3 if content unparsable.
  90. */
  91. int32_t (* const parseList)(struct Reader* reader,
  92. struct Allocator* allocator,
  93. List* output);
  94. /**
  95. * Serialize a dictionary.
  96. *
  97. * @param writer the Writer to write to.
  98. * @param dictionary the dictionary to serialize.
  99. * @return whatever the Writer returns when writing.
  100. */
  101. int32_t (* const serializeDictionary)(struct Writer* writer,
  102. const Dict* dictionary);
  103. /**
  104. * Parse a dictionary, reading in with the first callback and writing to the second.
  105. *
  106. * @param reader a Reader which will be asked for the data to parse. This implementation
  107. * assumes the reader's pointer is alligned on the 'd' which indicates
  108. * dictionary and will leave the read pointer on the first character AFTER
  109. * the 'e' which ends the dictionary.
  110. * @param allocator a Allocator which will be used to store data.
  111. * @param output a dictionary pointer which will be set to the output. Dict* out = NULL;
  112. is enough.
  113. * @return 0 if everything goes well -2 if -1 returned by read indicating an array underflow,
  114. * -3 if content unparsable.
  115. */
  116. int32_t (* const parseDictionary)(struct Reader* reader,
  117. struct Allocator* allocator,
  118. Dict* output);
  119. };
  120. #endif