VersionList.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "dht/dhtcore/VersionList.h"
  16. #include "memory/Allocator.h"
  17. #include "io/Reader.h"
  18. #include "io/Writer.h"
  19. #include "io/ArrayWriter.h"
  20. #include "io/ArrayReader.h"
  21. #include "util/Endian.h"
  22. #include <stdio.h>
  23. struct VersionList* VersionList_parse(String* str, struct Allocator* alloc)
  24. {
  25. const uint8_t numberSize = str->bytes[0];
  26. if (str->len < 1 || numberSize == 0 || numberSize > 4) {
  27. return NULL;
  28. }
  29. uint32_t length = (str->len - 1) / numberSize;
  30. if ((length * numberSize) != (str->len - 1)) {
  31. return NULL;
  32. }
  33. struct VersionList* list = VersionList_new(length, alloc);
  34. struct Reader* r = ArrayReader_new(str->bytes + 1, str->len - 1, alloc);
  35. for (int i = 0; i < (int)list->length; i++) {
  36. uint32_t ver = 0;
  37. Reader_read(r, (uint8_t*) &ver, numberSize);
  38. ver = Endian_bigEndianToHost32(ver);
  39. list->versions[i] = ver >> ((4-numberSize) * 8);
  40. }
  41. return list;
  42. }
  43. String* VersionList_stringify(struct VersionList* list, struct Allocator* alloc)
  44. {
  45. uint8_t numberSize = 1;
  46. uint32_t max = 0xff;
  47. for (int i = 0; i < (int)list->length; i++) {
  48. while (list->versions[i] >= max) {
  49. numberSize++;
  50. max = max << 8 | 0xff;
  51. }
  52. }
  53. String* out = String_newBinary(NULL, (numberSize * list->length + 1), alloc);
  54. struct Writer* w = ArrayWriter_new(out->bytes, out->len, alloc);
  55. Writer_write(w, &numberSize, 1);
  56. for (int i = 0; i < (int)list->length; i++) {
  57. uint32_t ver = list->versions[i] << ((4-numberSize) * 8);
  58. ver = Endian_hostToBigEndian32(ver);
  59. Writer_write(w, (uint8_t*) &ver, numberSize);
  60. }
  61. Writer_write(w, &numberSize, 1);
  62. return out;
  63. }
  64. struct VersionList* VersionList_new(uint32_t length, struct Allocator* alloc)
  65. {
  66. struct VersionList* out = Allocator_clone(alloc, (&(struct VersionList) {
  67. .length = length,
  68. .alloc = alloc
  69. }));
  70. out->versions = Allocator_calloc(alloc, 4, length);
  71. return out;
  72. }