Binary32Field.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. Binary32Field Class
  8. Field data layout:
  9. [4 bytes] length
  10. [length bytes] binary data
  11. --------------------------------------------------------------------------- */
  12. #include "../nde.h"
  13. #include "Binary32Field.h"
  14. #include "../ndestring.h"
  15. //---------------------------------------------------------------------------
  16. Binary32Field::Binary32Field(const uint8_t *_Data, size_t len) : BinaryField(_Data, len)
  17. {
  18. InitField();
  19. }
  20. //---------------------------------------------------------------------------
  21. void Binary32Field::InitField(void)
  22. {
  23. Type = FIELD_BINARY32;
  24. }
  25. //---------------------------------------------------------------------------
  26. Binary32Field::Binary32Field()
  27. {
  28. InitField();
  29. }
  30. //---------------------------------------------------------------------------
  31. void Binary32Field::ReadTypedData(const uint8_t *data, size_t len)
  32. {
  33. uint32_t c;
  34. size_t pos = 0;
  35. CHECK_INT(len); //len-=4;
  36. c = GET_INT(); pos += 4;
  37. if (c && c<=len)
  38. {
  39. Size = c;
  40. ndestring_release((ndestring_t)Data);
  41. Data = (uint8_t *)ndestring_malloc(c);
  42. GET_BINARY(Data, data, c, pos);
  43. }
  44. }
  45. //---------------------------------------------------------------------------
  46. void Binary32Field::WriteTypedData(uint8_t *data, size_t len)
  47. {
  48. uint32_t c;
  49. size_t pos = 0;
  50. CHECK_INT(len); //len-=4;
  51. if (Data && Size<=len)
  52. {
  53. c = (uint32_t)Size;
  54. PUT_INT(c); pos += 4;
  55. if (Data)
  56. PUT_BINARY(data, (unsigned char*)Data, c, pos);
  57. }
  58. else
  59. {
  60. PUT_INT(0);
  61. }
  62. }
  63. //---------------------------------------------------------------------------
  64. size_t Binary32Field::GetDataSize(void)
  65. {
  66. if (!Data) return 4;
  67. return Size + 4;
  68. }