BinaryField.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. BinaryField Class
  8. Field data layout:
  9. [2 bytes] length
  10. [length bytes] binary data
  11. --------------------------------------------------------------------------- */
  12. #include "../nde.h"
  13. #include "BinaryField.h"
  14. #include "../NDEString.h"
  15. //---------------------------------------------------------------------------
  16. BinaryField::BinaryField(const uint8_t *_Data, int len)
  17. {
  18. InitField();
  19. Type = FIELD_BINARY;
  20. if (_Data && len > 0)
  21. {
  22. Data = (uint8_t *)ndestring_malloc(len);
  23. memcpy(Data, _Data, len);
  24. Size = len;
  25. }
  26. }
  27. //---------------------------------------------------------------------------
  28. void BinaryField::InitField(void)
  29. {
  30. Type = FIELD_BINARY;
  31. Data = NULL;
  32. Size = 0;
  33. }
  34. //---------------------------------------------------------------------------
  35. BinaryField::BinaryField()
  36. {
  37. InitField();
  38. }
  39. //---------------------------------------------------------------------------
  40. BinaryField::~BinaryField()
  41. {
  42. ndestring_release((ndestring_t)Data);
  43. }
  44. //---------------------------------------------------------------------------
  45. void BinaryField::ReadTypedData(const uint8_t *data, size_t len)
  46. {
  47. unsigned short c;
  48. int pos = 0;
  49. CHECK_SHORT(len);
  50. c = GET_SHORT(); pos += 2;
  51. if (c && c<=len)
  52. {
  53. Size = c;
  54. ndestring_release((ndestring_t)Data);
  55. Data = (uint8_t *)ndestring_malloc(c);
  56. GET_BINARY(Data, data, c, pos);
  57. }
  58. }
  59. //---------------------------------------------------------------------------
  60. void BinaryField::WriteTypedData(uint8_t *data, size_t len)
  61. {
  62. size_t pos = 0;
  63. CHECK_SHORT(len);
  64. if (Data && Size<=len)
  65. {
  66. unsigned short c = (unsigned short)Size;
  67. PUT_SHORT(c); pos += 2;
  68. if (Data)
  69. PUT_BINARY(data, (unsigned char*)Data, c, pos);
  70. }
  71. else
  72. {
  73. PUT_SHORT(0);
  74. }
  75. }
  76. //---------------------------------------------------------------------------
  77. const uint8_t *BinaryField::GetData(size_t *len)
  78. {
  79. if (len)
  80. *len = Size;
  81. return Data;
  82. }
  83. //---------------------------------------------------------------------------
  84. void BinaryField::SetData(const uint8_t *_Data, size_t len)
  85. {
  86. if (!_Data || !len) return;
  87. ndestring_release((ndestring_t)Data);
  88. Size = 0;
  89. Data = (uint8_t *)ndestring_malloc(len);
  90. memcpy(Data, _Data, len);
  91. Size = len;
  92. }
  93. //---------------------------------------------------------------------------
  94. size_t BinaryField::GetDataSize(void)
  95. {
  96. if (!Data) return 2;
  97. return Size + 2;
  98. }
  99. //---------------------------------------------------------------------------
  100. int BinaryField::Compare(Field *Entry)
  101. {
  102. if (!Entry) return -1;
  103. size_t compare_length;
  104. const uint8_t *compare_data = ((BinaryField*)Entry)->GetData(&compare_length);
  105. return memcmp(Data, compare_data, min(compare_length, Size));
  106. }
  107. //---------------------------------------------------------------------------
  108. bool BinaryField::ApplyFilter(Field *FilterData, int op)
  109. {
  110. size_t l, s;
  111. const uint8_t *p = ((BinaryField *)FilterData)->GetData(&l);
  112. const uint8_t *d = GetData(&s);
  113. if (!p)
  114. p = (const uint8_t *)"";
  115. if (!d)
  116. d = (const uint8_t *)"";
  117. bool r;
  118. switch (op)
  119. {
  120. case FILTER_EQUALS:
  121. if (l != s)
  122. r = false;
  123. else
  124. r = !memcmp(d, p, min(s, l));
  125. break;
  126. case FILTER_CONTAINS:
  127. if (l > s)
  128. r = FALSE;
  129. else
  130. r = !!memmem(d, s, p, l);
  131. break;
  132. case FILTER_ABOVE:
  133. r = (memcmp(d, p, min(s, l)) > 0);
  134. break;
  135. case FILTER_BELOW:
  136. r = (memcmp(d, p, min(s, l)) < 0);
  137. break;
  138. case FILTER_BELOWOREQUAL:
  139. r = (memcmp(d, p, min(s, l)) <= 0);
  140. break;
  141. case FILTER_ABOVEOREQUAL:
  142. r = (memcmp(d, p, min(s, l)) >= 0);
  143. break;
  144. case FILTER_ISEMPTY:
  145. r = (s == 0);
  146. break;
  147. case FILTER_ISNOTEMPTY:
  148. r = (s != 0);
  149. break;
  150. default:
  151. r = true;
  152. break;
  153. }
  154. return r;
  155. }