BinaryField.cpp 3.8 KB

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