+static int move_to_next_with_matches(sd_journal *j, JournalFile *f, Object **o, uint64_t *p) {
+ int r;
+ uint64_t cp;
+ Object *c;
+
+ assert(j);
+ assert(f);
+ assert(o);
+ assert(p);
+
+ if (!j->matches) {
+ /* No matches is easy, just go on to the next entry */
+
+ if (f->current_offset > 0) {
+ r = journal_file_move_to_object(f, f->current_offset, OBJECT_ENTRY, &c);
+ if (r < 0)
+ return r;
+ } else
+ c = NULL;
+
+ return journal_file_next_entry(f, c, o, p);
+ }
+
+ /* So there are matches we have to adhere to, let's find the
+ * first entry that matches all of them */
+
+ if (f->current_offset > 0)
+ cp = f->current_offset;
+ else {
+ r = journal_file_find_first_entry(f, j->matches->data, j->matches->size, &c, &cp);
+ if (r <= 0)
+ return r;
+
+ /* We can shortcut this if there's only one match */
+ if (j->n_matches == 1) {
+ *o = c;
+ *p = cp;
+ return r;
+ }
+ }
+
+ for (;;) {
+ uint64_t np, n;
+ bool found;
+ Match *m;
+
+ r = journal_file_move_to_object(f, cp, OBJECT_ENTRY, &c);
+ if (r < 0)
+ return r;
+
+ n = journal_file_entry_n_items(c);
+
+ /* Make sure we don't match the entry we are starting
+ * from. */
+ found = f->current_offset != cp;
+
+ np = 0;
+ LIST_FOREACH(matches, m, j->matches) {
+ uint64_t q, k;
+
+ for (k = 0; k < n; k++)
+ if (c->entry.items[k].hash == m->le_hash)
+ break;
+
+ if (k >= n) {
+ /* Hmm, didn't find any field that matched, so ignore
+ * this match. Go on with next match */
+
+ found = false;
+ continue;
+ }
+
+ /* Hmm, so, this field matched, let's remember
+ * where we'd have to try next, in case the other
+ * matches are not OK */
+ q = le64toh(c->entry.items[k].next_entry_offset);
+ if (q > np)
+ np = q;
+ }
+
+ /* Did this entry match against all matches? */
+ if (found) {
+ *o = c;
+ *p = cp;
+ return 1;
+ }
+
+ /* Did we find a subsequent entry? */
+ if (np == 0)
+ return 0;
+
+ /* Hmm, ok, this entry only matched partially, so
+ * let's try another one */
+ cp = np;
+ }
+}
+