static int paged_results(struct paged_context *ac, struct ldb_reply *ares)
{
	struct ldb_paged_control *paged;
	unsigned int i, num_ctrls;
	int ret;

	if (ac->store == NULL) {
		ret = LDB_ERR_OPERATIONS_ERROR;
		return ldb_module_done(
			ac->req, ac->controls, ares->response, ret);
	}

	while (ac->store->last_i < ac->store->num_entries && ac->size > 0) {
		struct GUID *guid = &ac->store->results[ac->store->last_i++];
		struct ldb_result *result = NULL;

		ac->size--;

		/*
		 * Note: In the case that an object has been moved to a
		 * different place in the LDAP tree, we might expect the object
		 * to disappear from paged results.  If we were going to
		 * implement that behaviour, we would do it here by passing
		 * down the original container DN to the search.
		 * However, testing shows that, on Windows, the moved object
		 * remains in the paged results. So, we are matching Windows
		 * behaviour here by leaving out the scope.
		 */
		ret = paged_search_by_dn_guid(ac->module, ac, &result, guid,
					    ac->req->op.search.attrs,
					    ac->store->expr);
		if (ret == LDAP_NO_SUCH_OBJECT ||
		    (ret == LDB_SUCCESS && result->count == 0)) {
			/* The thing isn't there TODO, which we quietly
			   ignore and go on to send an extra one
			   instead. */
			continue;
		} else if (ret != LDB_SUCCESS) {
			return ldb_module_done(
				ac->req, ac->controls, ares->response, ret);
		}

		ret = ldb_module_send_entry(ac->req, result->msgs[0],
					    NULL);
		if (ret != LDB_SUCCESS) {
			/*
			 * ldb_module_send_entry will have called
			 * ldb_module_done if an error occurred.
			 */
			return ret;
		}
	}

	if (ac->store->first_ref) {
		/* There is no right place to put references in the sorted
		   results, so we send them as soon as possible.
		*/
		ret = send_referrals(ac->store, ac->req);
		if (ret != LDB_SUCCESS) {
			/*
			 * send_referrals will have called ldb_module_done
			 * if an error occurred.
			 */
			return ret;
		}
	}

	/* return result done */
	num_ctrls = 1;
	i = 0;

	if (ac->store->controls != NULL) {
		while (ac->store->controls[i]) i++; /* counting */

		num_ctrls += i;
	}

	ac->controls = talloc_array(ac, struct ldb_control *, num_ctrls +1);
	if (ac->controls == NULL) {
		ret = LDB_ERR_OPERATIONS_ERROR;
		return ldb_module_done(
			ac->req, ac->controls, ares->response, ret);
	}
	ac->controls[num_ctrls] = NULL;

	for (i = 0; i < (num_ctrls -1); i++) {
		ac->controls[i] = talloc_reference(ac->controls,
						   ac->store->controls[i]);
	}

	ac->controls[i] = talloc(ac->controls, struct ldb_control);
	if (ac->controls[i] == NULL) {
		ret = LDB_ERR_OPERATIONS_ERROR;
		return ldb_module_done(
			ac->req, ac->controls, ares->response, ret);
	}

	ac->controls[i]->oid = talloc_strdup(ac->controls[i],
						LDB_CONTROL_PAGED_RESULTS_OID);
	if (ac->controls[i]->oid == NULL) {
		ret = LDB_ERR_OPERATIONS_ERROR;
		return ldb_module_done(
			ac->req, ac->controls, ares->response, ret);
	}

	ac->controls[i]->critical = 0;

	paged = talloc(ac->controls[i], struct ldb_paged_control);
	if (paged == NULL) {
		ret = LDB_ERR_OPERATIONS_ERROR;
		return ldb_module_done(
			ac->req, ac->controls, ares->response, ret);
	}

	ac->controls[i]->data = paged;

	if (ac->size > 0) {
		paged->size = 0;
		paged->cookie = NULL;
		paged->cookie_len = 0;
	} else {
		paged->size = ac->store->num_entries;
		paged->cookie = talloc_strdup(paged, ac->store->cookie);
		paged->cookie_len = strlen(paged->cookie) + 1;
	}

	return LDB_SUCCESS;
}