summaryrefslogtreecommitdiff
path: root/source4/heimdal/lib/base/bsearch.c
blob: 278962172683096cb4adb1e9a8d29a034799cb37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
/*
 * Copyright (c) 2011, Secure Endpoints Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "baselocl.h"

#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_IO_H
#include <io.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <fcntl.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <errno.h>
#include <assert.h>

/*
 * This file contains functions for binary searching flat text in memory
 * and in text files where each line is a [variable length] record.
 * Each record has a key and an optional value separated from the key by
 * unquoted whitespace.  Whitespace in the key, and leading whitespace
 * for the value, can be quoted with backslashes (but CR and LF must be
 * quoted in such a way that they don't appear in the quoted result).
 *
 * Binary searching a tree are normally a dead simple algorithm.  It
 * turns out that binary searching flat text with *variable* length
 * records is... tricky.  There's no indexes to record beginning bytes,
 * thus any index selected during the search is likely to fall in the
 * middle of a record.  When deciding to search a left sub-tree one
 * might fail to find the last record in that sub-tree on account of the
 * right boundary falling in the middle of it -- the chosen solution to
 * this makes left sub-tree searches slightly less efficient than right
 * sub-tree searches.
 *
 * If binary searching flat text in memory is tricky, using block-wise
 * I/O instead is trickier!  But it's necessary in order to support
 * large files (which we either can't or wouldn't want to read or map
 * into memory).  Each block we read has to be large enough that the
 * largest record can fit in it.  And each block might start and/or end
 * in the middle of a record.  Here it is the right sub-tree searches
 * that are less efficient than left sub-tree searches.
 *
 * bsearch_common() contains the common text block binary search code.
 *
 * _bsearch_text() is the interface for searching in-core text.
 * _bsearch_file() is the interface for block-wise searching files.
 */

struct bsearch_file_handle {
    int fd;          /* file descriptor */
    char *cache;     /* cache bytes */
    char *page;      /* one double-size page worth of bytes */
    size_t file_sz;  /* file size */
    size_t cache_sz; /* cache size */
    size_t page_sz;  /* page size */
};

/* Find a new-line */
static const char *
find_line(const char *buf, size_t i, size_t right)
{
    if (i == 0)
	return &buf[i];
    for (; i < right; i++) {
	if (buf[i] == '\n') {
	    if ((i + 1) < right)
		return &buf[i + 1];
	    return NULL;
	}
    }
    return NULL;
}

/*
 * Common routine for binary searching text in core.
 *
 * Perform a binary search of a char array containing a block from a
 * text file where each line is a record (LF and CRLF supported).  Each
 * record consists of a key followed by an optional value separated from
 * the key by whitespace.  Whitespace can be quoted with backslashes.
 * It's the caller's responsibility to encode/decode keys/values if
 * quoting is desired; newlines should be encoded such that a newline
 * does not appear in the result.
 *
 * All output arguments are optional.
 *
 * Returns 0 if key is found, -1 if not found, or an error code such as
 * ENOMEM in case of error.
 *
 * Inputs:
 *
 * @buf          String to search
 * @sz           Size of string to search
 * @key          Key string to search for
 * @buf_is_start True if the buffer starts with a record, false if it
 *               starts in the middle of a record or if the caller
 *               doesn't know.
 *
 * Outputs:
 *
 * @value        Location to store a copy of the value (caller must free)
 * @location     Record location if found else the location where the
 *               record should be inserted (index into @buf)
 * @cmp	         Set to less than or greater than 0 to indicate that a
 *               key not found would have fit in an earlier or later
 *               part of a file.  Callers should use this to decide
 *               whether to read a block to the left or to the right and
 *               search that.
 * @loops        Location to store a count of bisections required for
 *               search (useful for confirming logarithmic performance)
 */
static int
bsearch_common(const char *buf, size_t sz, const char *key,
	       int buf_is_start, char **value, size_t *location,
	       int *cmp, size_t *loops)
{
    const char *linep;
    size_t key_start, key_len; /* key string in buf */
    size_t val_start, val_len; /* value string in buf */
    int key_cmp = -1;
    size_t k;
    size_t l;    /* left side of buffer for binary search */
    size_t r;    /* right side of buffer for binary search */
    size_t rmax; /* right side of buffer for binary search */
    size_t i;    /* index into buffer, typically in the middle of l and r */
    size_t loop_count = 0;
    int ret = -1;

    if (value)
	*value = NULL;
    if (cmp)
	*cmp = 0;
    if (loops)
	*loops = 0;

    /* Binary search; file should be sorted */
    for (l = 0, r = rmax = sz, i = sz >> 1; i >= l && i < rmax; loop_count++) {
	heim_assert(i < sz, "invalid aname2lname db index");

	/* buf[i] is likely in the middle of a line; find the next line */
	linep = find_line(buf, i, rmax);
	k = linep ? linep - buf : i;
	if (linep == NULL || k >= rmax) {
	    /*
	     * No new line found to the right; search to the left then
	     * but don't change rmax (this isn't optimal, but it's
	     * simple).
	     */
	    if (i == l)
		break;
	    r = i;
	    i = l + ((r - l) >> 1);
	    continue;
	}
	i = k;
	heim_assert(i >= l && i < rmax, "invalid aname2lname db index");

	/* Got a line; check it */

	/* Search for and split on unquoted whitespace */
	val_start = 0;
	for (key_start = i, key_len = 0, val_len = 0, k = i; k < rmax; k++) {
	    if (buf[k] == '\\') {
		k++;
		continue;
	    }
	    if (buf[k] == '\r' || buf[k] == '\n') {
		/* We now know where the key ends, and there's no value */
		key_len = k - i;
		break;
	    }
	    if (!isspace((unsigned char)buf[k]))
		continue;

	    while (k < rmax && isspace((unsigned char)buf[k])) {
		key_len = k - i;
		k++;
	    }
	    if (k < rmax)
		val_start = k;
	    /* Find end of value */
	    for (; k < rmax && buf[k] != '\0'; k++) {
		if (buf[k] == '\r' || buf[k] == '\n') {
		    val_len = k - val_start;
		    break;
		}
	    }
	    break;
	}

	/*
	 * The following logic is for dealing with partial buffers,
	 * which we use for block-wise binary searches of large files
	 */
	if (key_start == 0 && !buf_is_start) {
	    /*
	     * We're at the beginning of a block that might have started
	     * in the middle of a record whose "key" might well compare
	     * as greater than the key we're looking for, so we don't
	     * bother comparing -- we know key_cmp must be -1 here.
	     */
	    key_cmp = -1;
	    break;
	}
	if ((val_len && buf[val_start + val_len] != '\n') ||
	    (!val_len && buf[key_start + key_len] != '\n')) {
	    /*
	     * We're at the end of a block that ends in the middle of a
	     * record whose "key" might well compare as less than the
	     * key we're looking for, so we don't bother comparing -- we
	     * know key_cmp must be >= 0 but we can't tell.  Our caller
	     * will end up reading a double-size block to handle this.
	     */
	    key_cmp = 1;
	    break;
	}

	key_cmp = strncmp(key, &buf[key_start], key_len);
	if (key_cmp == 0 && strlen(key) != key_len)
	    key_cmp = 1;
	if (key_cmp < 0) {
	    /* search left */
	    r = rmax = (linep - buf);
	    i = l + ((r - l) >> 1);
	    if (location)
		*location = key_start;
	} else if (key_cmp > 0) {
	    /* search right */
	    if (l == i)
		break; /* not found */
	    l = i;
	    i = l + ((r - l) >> 1);
	    if (location)
		*location = val_start + val_len;
	} else {
	    /* match! */
	    if (location)
		*location = key_start;
	    ret = 0;
	    if (val_len && value) {
		/* Avoid strndup() so we don't need libroken here yet */
		*value = malloc(val_len + 1);
		if (!*value)
		    ret = errno;
		(void) memcpy(*value, &buf[val_start], val_len);
		(*value)[val_len] = '\0';
	    }
	    break;
	}
    }

    if (cmp)
	*cmp = key_cmp;
    if (loops)
	*loops = loop_count;

    return ret;
}

/*
 * Binary search a char array containing sorted text records separated
 * by new-lines (or CRLF).  Each record consists of a key and an
 * optional value following the key, separated from the key by unquoted
 * whitespace.
 *
 * All output arguments are optional.
 *
 * Returns 0 if key is found, -1 if not found, or an error code such as
 * ENOMEM in case of error.
 *
 * Inputs:
 *
 * @buf      Char array pointer
 * @buf_sz   Size of buf
 * @key      Key to search for
 *
 * Outputs:
 *
 * @value    Location where to put the value, if any (caller must free)
 * @location Record location if found else the location where the record
 *           should be inserted (index into @buf)
 * @loops    Location where to put a number of loops (or comparisons)
 *           needed for the search (useful for benchmarking)
 */
int
_bsearch_text(const char *buf, size_t buf_sz, const char *key,
	       char **value, size_t *location, size_t *loops)
{
    return bsearch_common(buf, buf_sz, key, 1, value, location, NULL, loops);
}

#define MAX_BLOCK_SIZE (1024 * 1024)
#define DEFAULT_MAX_FILE_SIZE (1024 * 1024)
/*
 * Open a file for binary searching.  The file will be read in entirely
 * if it is smaller than @max_sz, else a cache of @max_sz bytes will be
 * allocated.
 *
 * Returns 0 on success, else an error number or -1 if the file is empty.
 *
 * Inputs:
 *
 * @fname   Name of file to open
 * @max_sz  Maximum size of cache to allocate, in bytes (if zero, default)
 * @page_sz Page size (must be a power of two, larger than 256, smaller
 *          than 1MB; if zero use default)
 * 
 * Outputs:
 *
 * @bfh     Handle for use with _bsearch_file() and _bsearch_file_close()
 * @reads   Number of reads performed
 */
int
_bsearch_file_open(const char *fname, size_t max_sz, size_t page_sz,
		    bsearch_file_handle *bfh, size_t *reads)
{
    bsearch_file_handle new_bfh = NULL;
    struct stat st;
    size_t i;
    int fd;
    int ret;

    *bfh = NULL;

    if (reads)
	*reads = 0;

    fd = open(fname, O_RDONLY);
    if (fd == -1)
	return errno;

    if (fstat(fd, &st) == -1) {
	ret = errno;
	goto err;
    }

    if (st.st_size == 0) {
	ret = -1; /* no data -> no binary search */
	goto err;
    }

    /* Validate / default arguments */
    if (max_sz == 0)
	max_sz = DEFAULT_MAX_FILE_SIZE;
    for (i = page_sz; i; i >>= 1) {
	/* Make sure page_sz is a power of two */
	if ((i % 2) && (i >> 1)) {
	    page_sz = 0;
	    break;
	}
    }
    if (page_sz == 0)
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
	page_sz = st.st_blksize;
#else
	page_sz = 4096;
#endif
    for (i = page_sz; i; i >>= 1) {
	/* Make sure page_sz is a power of two */
	if ((i % 2) && (i >> 1)) {
	    /* Can't happen! Filesystems always use powers of two! */
	    page_sz = 4096;
	    break;
	}
    }
    if (page_sz > MAX_BLOCK_SIZE)
	page_sz = MAX_BLOCK_SIZE;

    new_bfh = calloc(1, sizeof (*new_bfh));
    if (new_bfh == NULL) {
	ret = ENOMEM;
	goto err;
    }

    new_bfh->fd = fd;
    new_bfh->page_sz = page_sz;
    new_bfh->file_sz = st.st_size;

    if (max_sz >= st.st_size) {
	/* Whole-file method */
	new_bfh->cache = malloc(st.st_size + 1);
	if (new_bfh->cache) {
	    new_bfh->cache[st.st_size] = '\0';
	    new_bfh->cache_sz = st.st_size;
	    ret = read(fd, new_bfh->cache, st.st_size);
	    if (ret < 0) {
		ret = errno;
		goto err;
	    }
	    if (ret != st.st_size) {
		ret = EIO; /* XXX ??? */
		goto err;
	    }
	    if (reads)
		*reads = 1;
	    (void) close(fd);
	    new_bfh->fd = -1;
	    *bfh = new_bfh;
	    return 0;
	}
    }

    /* Block-size method, or above malloc() failed */
    new_bfh->page = malloc(new_bfh->page_sz << 1);
    if (new_bfh->page == NULL) {
	/* Can't even allocate a single double-size page! */
	ret = ENOMEM;
	goto err;
    }

    new_bfh->cache_sz = max_sz < st.st_size ? max_sz : st.st_size;
    new_bfh->cache = malloc(new_bfh->cache_sz);
    *bfh = new_bfh;

    /*
     * malloc() may have failed because we were asking for a lot of
     * memory, but we may still be able to operate without a cache,
     * so let's not fail.
     */
    if (new_bfh->cache == NULL) {
	new_bfh->cache_sz = 0;
	return 0;
    }

    /* Initialize cache */
    for (i = 0; i < new_bfh->cache_sz; i += new_bfh->page_sz)
	new_bfh->cache[i] = '\0';
    return 0;

err:
    (void) close(fd);
    if (new_bfh) {
	free(new_bfh->page);
	free(new_bfh->cache);
	free(new_bfh);
    }
    return ret;
}

/*
 * Indicate whether the given binary search file handle will be searched
 * with block-wise method.
 */
void
_bsearch_file_info(bsearch_file_handle bfh,
		    size_t *page_sz, size_t *max_sz, int *blockwise)
{
    if (page_sz)
	*page_sz = bfh->page_sz;
    if (max_sz)
	*max_sz = bfh->cache_sz;
    if (blockwise)
	*blockwise = (bfh->file_sz != bfh->cache_sz);
}

/*
 * Close the given binary file search handle.
 *
 * Inputs:
 *
 * @bfh Pointer to variable containing handle to close.
 */
void
_bsearch_file_close(bsearch_file_handle *bfh)
{
    if (!*bfh)
	return;
    if ((*bfh)->fd >= 0)
	(void) close((*bfh)->fd);
    if ((*bfh)->page)
	free((*bfh)->page);
    if ((*bfh)->cache)
	free((*bfh)->cache);
    free(*bfh);
    *bfh = NULL;
}

/*
 * Private function to get a page from a cache.  The cache is a char
 * array of 2^n - 1 double-size page worth of bytes, where n is the
 * number of tree levels that the cache stores.  The cache can be
 * smaller than n implies.
 *
 * The page may or may not be valid.  If the first byte of it is NUL
 * then it's not valid, else it is.
 *
 * Returns 1 if page is in cache and valid, 0 if the cache is too small
 * or the page is invalid.  The page address is output in @buf if the
 * cache is large enough to contain it regardless of whether the page is
 * valid.
 *
 * Inputs:
 *
 * @bfh      Binary search file handle
 * @level    Level in the tree that we want a page for
 * @page_idx Page number in the given level (0..2^level - 1)
 *
 * Outputs:
 *
 * @buf      Set to address of page if the cache is large enough
 */
static int
get_page_from_cache(bsearch_file_handle bfh, size_t level, size_t page_idx,
		    char **buf)
{
    size_t idx = 0;
    size_t page_sz;

    page_sz = bfh->page_sz << 1; /* we use double-size pages in the cache */

    *buf = NULL;

    /*
     * Compute index into cache.  The cache is basically an array of
     * double-size pages.  The first (zeroth) double-size page in the
     * cache will be the middle page of the file -- the root of the
     * tree.  The next two double-size pages will be the left and right
     * pages of the second level in the tree.  The next four double-size
     * pages will be the four pages at the next level.  And so on for as
     * many pages as fit in the cache.
     *
     * The page index is the number of the page at the given level.  We
     * then compute (2^level - 1 + page index) * 2page size, check that
     * we have that in the cache, check that the page has been read (it
     * doesn't start with NUL).
     */
    if (level)
	idx = (1 << level) - 1 + page_idx;
    if (((idx + 1) * page_sz * 2) > bfh->cache_sz)
	return 0;

    *buf = &bfh->cache[idx * page_sz * 2];
    if (bfh->cache[idx * page_sz * 2] == '\0')
	return 0; /* cache[idx] == NUL -> page not loaded in cache */
    return 1;
}

/*
 * Private function to read a page of @page_sz from @fd at offset @off
 * into @buf, outputing the number of bytes read, which will be the same
 * as @page_sz unless the page being read is the last page, in which
 * case the number of remaining bytes in the file will be output.
 *
 * Returns 0 on success or an errno value otherwise (EIO if reads are
 * short).
 *
 * Inputs:
 *
 * @bfh        Binary search file handle
 * @level      Level in the binary search tree that we're at
 * @page_idx   Page "index" at the @level of the tree that we want
 * @page       Actual page number that we want
 * want_double Whether we need a page or double page read
 *
 * Outputs:
 *
 * @buf        Page read or cached
 * @bytes      Bytes read (may be less than page or double page size in
 *             the case of the last page, of course)
 */
static int
read_page(bsearch_file_handle bfh, size_t level, size_t page_idx, size_t page,
	  int want_double, const char **buf, size_t *bytes)
{
    int ret;
    off_t off;
    size_t expected;
    size_t wanted;
    char *page_buf;

    /* Figure out where we're reading and how much */
    off = page * bfh->page_sz;
    if (off < 0)
	return EOVERFLOW;

    wanted = bfh->page_sz << want_double;
    expected = ((bfh->file_sz - off) > wanted) ? wanted : bfh->file_sz - off;

    if (get_page_from_cache(bfh, level, page_idx, &page_buf)) {
	*buf = page_buf;
	*bytes = expected;
	return 0; /* found in cache */
    }


    *bytes = 0;
    *buf = NULL;

    /* OK, we have to read a page or double-size page */

    if (page_buf)
	want_double = 1; /* we'll be caching; we cache double-size pages */
    else
	page_buf = bfh->page; /* we won't cache this page */

    wanted = bfh->page_sz << want_double;
    expected = ((bfh->file_sz - off) > wanted) ? wanted : bfh->file_sz - off;

#ifdef HAVE_PREAD
    ret = pread(bfh->fd, page_buf, expected, off);
#else
    if (lseek(bfh->fd, off, SEEK_SET) == (off_t)-1)
	return errno;
    ret = read(bfh->fd, page_buf, expected);
#endif
    if (ret < 0)
	return errno;
    
    if (ret != expected)
	return EIO; /* XXX ??? */

    *buf = page_buf;
    *bytes = expected;
    return 0;
}

/*
 * Perform a binary search of a file where each line is a record (LF and
 * CRLF supported).  Each record consists of a key followed by an
 * optional value separated from the key by whitespace.  Whitespace can
 * be quoted with backslashes.  It's the caller's responsibility to
 * encode/decode keys/values if quoting is desired; newlines should be
 * encoded such that a newline does not appear in the result.
 *
 * The search is done with block-wise I/O (i.e., the whole file is not
 * read into memory).
 *
 * All output arguments are optional.
 *
 * Returns 0 if key is found, -1 if not found, or an error code such as
 * ENOMEM in case of error.
 *
 * NOTE: We could improve this by not freeing the buffer, instead
 *       requiring that the caller provide it.  Further, we could cache
 *       the top N levels of [double-size] pages (2^N - 1 pages), which
 *       should speed up most searches by reducing the number of reads
 *       by N.
 *
 * Inputs:
 *
 * @fd           File descriptor (file to search)
 * @page_sz      Page size (if zero then the file's st_blksize will be used)
 * @key          Key string to search for
 *
 * Outputs:
 *
 * @value        Location to store a copy of the value (caller must free)
 * @location     Record location if found else the location where the
 *               record should be inserted (index into @buf)
 * @loops        Location to store a count of bisections required for
 *               search (useful for confirming logarithmic performance)
 * @reads        Location to store a count of pages read during search
 *               (useful for confirming logarithmic performance)
 */
int
_bsearch_file(bsearch_file_handle bfh, const char *key,
	       char **value, size_t *location, size_t *loops, size_t *reads)
{
    int ret;
    const char *buf;
    size_t buf_sz;
    size_t page, l, r;
    size_t my_reads = 0;
    size_t my_loops_total = 0;
    size_t my_loops;
    size_t level;        /* level in the tree */
    size_t page_idx = 0; /* page number in the tree level */
    size_t buf_location;
    int cmp;
    int buf_ends_in_eol = 0;
    int buf_is_start = 0;

    if (reads)
	*reads = 0;

    /* If whole file is in memory then search that and we're done */
    if (bfh->file_sz == bfh->cache_sz)
	return _bsearch_text(bfh->cache, bfh->cache_sz, key, value, location, loops);

    /* Else block-wise binary search */

    if (value)
	*value = NULL;
    if (loops)
	*loops = 0;

    l = 0;
    r = (bfh->file_sz / bfh->page_sz) + 1;
    for (level = 0, page = r >> 1; page >= l && page < r ; level++) {
	ret = read_page(bfh, level, page_idx, page, 0, &buf, &buf_sz);
	if (ret != 0)
	    return ret;
	my_reads++;
	if (buf[buf_sz - 1] == '\r' || buf[buf_sz - 1] == '\n')
	    buf_ends_in_eol = 1;
	else
	    buf_ends_in_eol = 0;

	buf_is_start = page == 0 ? 1 : 0;
	ret = bsearch_common(buf, (size_t)buf_sz, key, buf_is_start,
			     value, &buf_location, &cmp, &my_loops);
	if (ret > 0)
	    return ret;
	/* Found or no we update stats */
	my_loops_total += my_loops;
	if (loops)
	    *loops = my_loops_total;
	if (reads)
	    *reads = my_reads;
	if (location)
	    *location = page * bfh->page_sz + buf_location;
	if (ret == 0)
	    return 0; /* found! */
	/* Not found */
	if (cmp < 0) {
	    /* Search left */
	    page_idx <<= 1;
	    r = page;
	    page = l + ((r - l) >> 1);
	    continue;
	} else {
	    /*
	     * Search right, but first search the current and next
	     * blocks in case that the record we're looking for either
	     * straddles the boundary between this and the next record,
	     * or in case the record starts exactly at the next page.
	     */
	    heim_assert(cmp > 0, "cmp > 0");

	    if (!buf_ends_in_eol || page == l || page == (r - 1)) {
		ret = read_page(bfh, level, page_idx, page, 1, &buf, &buf_sz);
		if (ret != 0)
		    return ret;
		my_reads++;

		buf_is_start = page == l ? 1 : 0;

		ret = bsearch_common(buf, (size_t)buf_sz, key, buf_is_start,
				     value, &buf_location, &cmp, &my_loops);
		if (ret > 0)
		    return ret;
		my_loops_total += my_loops;
		if (loops)
		    *loops = my_loops_total;
		if (reads)
		    *reads = my_reads;
		if (location)
		    *location = page * bfh->page_sz + buf_location;
		if (ret == 0)
		    return 0;
	    }

	    /* Oh well, search right */
	    if (l == page && r == (l + 1))
		break;
	    page_idx = (page_idx << 1) + 1;
	    l = page;
	    page = l + ((r - l) >> 1);
	    continue;
	}
    }
    return -1;
}


static int
stdb_open(void *plug, const char *dbtype, const char *dbname,
	     heim_dict_t options, void **db, heim_error_t *error)
{
    bsearch_file_handle bfh;
    char *p;
    int ret;

    if (error)
	*error = NULL;
    if (dbname == NULL || *dbname == '\0') {
	if (error)
	    *error = heim_error_create(EINVAL,
				       N_("DB name required for sorted-text DB "
					  "plugin", ""));
	return EINVAL;
    }
    p = strrchr(dbname, '.');
    if (p == NULL || strcmp(p, ".txt") != 0) {
	if (error)
	    *error = heim_error_create(ENOTSUP,
				       N_("Text file (name ending in .txt) "
				       "required for sorted-text DB plugin",
				       ""));
	return ENOTSUP;
    }

    ret = _bsearch_file_open(dbname, 0, 0, &bfh, NULL);
    if (ret)
	return ret;

    *db = bfh;
    return 0;
}

static int
stdb_close(void *db, heim_error_t *error)
{
    bsearch_file_handle bfh = db;

    if (error)
	*error = NULL;
    _bsearch_file_close(&bfh);
    return 0;
}

static heim_data_t
stdb_copy_value(void *db, heim_string_t table, heim_data_t key,
	       heim_error_t *error)
{
    bsearch_file_handle bfh = db;
    const char *k;
    char *v;
    heim_data_t value;
    int ret;

    if (error)
	*error = NULL;

    if (table == NULL)
	table = HSTR("");

    if (table != HSTR(""))
	return NULL;

    if (heim_get_tid(key) == HEIM_TID_STRING)
	k = heim_string_get_utf8((heim_string_t)key);
    else
	k = (const char *)heim_data_get_ptr(key);
    ret = _bsearch_file(bfh, k, &v, NULL, NULL, NULL);
    if (ret != 0) {
	if (ret > 0 && error)
	    *error = heim_error_create(ret, "%s", strerror(ret));
	return NULL;
    }
    value = heim_data_create(v, strlen(v));
    free(v);
    /* XXX Handle ENOMEM */
    return value;
}

struct heim_db_type heim_sorted_text_file_dbtype = {
    1, stdb_open, NULL, stdb_close, NULL, NULL, NULL, NULL, NULL, NULL,
    stdb_copy_value, NULL, NULL, NULL
};