+ /* do a binary search */
+ int count = l / entry_size;
+ int i = 0;
+
+ while (count)
+ {
+ int step = count >> 1;
+
+ fseek(m_structure_read, (i + step) * entry_size, SEEK_SET);
+ if (!fread(d, 1, entry_size, m_structure_read))
+ {
+ eDebug("read error at entry %d", i);
+ return -1;
+ }
+
+#if BYTE_ORDER != BIG_ENDIAN
+ d[0] = bswap_64(d[0]);
+ d[1] = bswap_64(d[1]);
+#endif
+// eDebug("%d: %08llx > %llx", i, d[0], d[1]);
+
+ if (d[0] < (unsigned long long)offset)
+ {
+ i += step + 1;
+ count -= step + 1;
+ } else
+ count = step;
+ }
+
+ eDebug("found %d", i);
+
+ /* put that in the middle */
+ i -= struture_cache_entries / 2;
+ if (i < 0)
+ i = 0;
+ eDebug("cache starts at %d", i);
+ fseek(m_structure_read, i * entry_size, SEEK_SET);
+ int num = fread(m_structure_cache, entry_size, struture_cache_entries, m_structure_read);
+ eDebug("%d entries", num);
+ for (i = 0; i < struture_cache_entries; ++i)
+ {
+ if (i < num)
+ {
+#if BYTE_ORDER != BIG_ENDIAN
+ m_structure_cache[i * 2] = bswap_64(m_structure_cache[i * 2]);
+ m_structure_cache[i * 2 + 1] = bswap_64(m_structure_cache[i * 2 + 1]);
+#endif
+ } else
+ {
+ m_structure_cache[i * 2] = 0x7fffffffffffffffULL; /* fill with harmless content */
+ m_structure_cache[i * 2 + 1] = 0;
+ }
+ }
+ m_structure_cache_valid = 1;
+ }