~samiam/MaraDNS

ref: 3.5.0021 MaraDNS/libs/JsStrOS.c -rw-r--r-- 28.7 KiB
ca00f282 — Sam Trenholme MaraDNS release 3.5.0021 4 months ago
                                                                                
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
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
/* Copyright (c) 2002-2019 Sam Trenholme
 *
 * TERMS
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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 'as is' with no guarantees of correctness or
 * fitness for purpose.
 */

/* Headers for the underlying OS calls */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/file.h>
#include <limits.h>
#include <unistd.h>
#ifdef THREADS
#include <pthread.h>
#endif /* THREADS */

/* Headers for the string routines */
#include "JsStr.h"

/* Structures that keep track of allocated memory */
#ifdef DEBUG
#define HASH_SIZE 100000
typedef struct lt_hash_spot {
    void *allocated_memory;
    struct lt_hash_spot *next;
    char *de_donde; /* From where */
    int allocated_size;
    } lt_hash_spot;
int lt_hash_initialized = 0;
lt_hash_spot lt_hash[HASH_SIZE];
int total_allocated_memory = 0;
#define THREADS
#endif /* DEBUG */
#ifdef THREADS
pthread_mutex_t alloc_lock = PTHREAD_MUTEX_INITIALIZER;
#endif /* THREADS */

/* js_alloc: Allocate memory from the underlying OS.
   input: The unit count and unit size of memory to allocate
   output: An anonymous pointer to the allocated memory */

void *js_alloc(int unit_count, int unit_size) {
    void *data;
#ifdef DEBUG
    unsigned int hash_index,counter;
    lt_hash_spot *new,*point;
#endif /* DEBUG */
    /* Sanity check: Never allow this; makes C act buggy */
    if(unit_size == 0 || unit_count == 0)
        return 0;
    data = (void *)malloc(unit_count * unit_size);
#ifdef DEBUG
    /* If debug is set, we have a 64k element hash table which uses the low
       16 bits of the memory allocated as the hash index.  Each element in
       the hash table is a linked list, which has the address and number of
       bytes allocated for each piece of allocated memory */

    hash_index = (int)data;
    hash_index %= HASH_SIZE;
#ifdef THREADS
    pthread_mutex_lock(&alloc_lock);
#endif /* THREADS */
    /* Initialize the lt hash the first time we run this routine */
    if(lt_hash_initialized == 0) {
        for(counter = 0; counter < HASH_SIZE; counter++)
            lt_hash[counter].allocated_size = 0;
        lt_hash_initialized = 1;
        }
    /* If the hash pointer is empty, use that */
    if(lt_hash[hash_index].allocated_size == 0) {
        lt_hash[hash_index].allocated_size = unit_count * unit_size;
        lt_hash[hash_index].allocated_memory = data;
        lt_hash[hash_index].de_donde = 0;
        lt_hash[hash_index].next = 0;
        }
    /* Otherwise, go down the linked list, place the new node at the
       end of the branch in question */
    else {
        new = malloc(sizeof(lt_hash_spot));
        if(new == NULL) {
            printf("Aieeee, can not allocate memeory for lt_hash_spot!\n");
            exit(1);
            }
        new->allocated_size = unit_count * unit_size;
        new->next = 0;
        new->allocated_memory = data;
        new->de_donde = 0;
        point = lt_hash[hash_index].next;
        if(point == 0) {
            lt_hash[hash_index].next = new;
            }
        else {
            while(point->next != 0)
                point = point->next;
            point->next = new;
            }
        }
    total_allocated_memory += unit_count * unit_size;
#ifdef THREADS
    pthread_mutex_unlock(&alloc_lock);
#endif /* THREADS */
#endif /* DEBUG */
    if(data == NULL) {
        /* Securty: In a situtation where we can not allocate memory,
           the subsequent behavior of the program is undefined.  Hence,
           the best thing to do is exit then and there */
        printf("Aieeeeee, can not allocate memory (increase max_mem maybe?)!");
        exit(64);
        return (void *)0;
        }
    return data;
    }

/* js_alloc_DEBUG: Allocate memory from the underlying OS.
   input: The unit count and unit size of memory to allocate,
          from where they allocated this memory
   output: An anonymous pointer to the allocated memory */

void *js_alloc_DEBUG(int unit_count, int unit_size, char *whence) {
    void *data;
#ifdef DEBUG
    unsigned int hash_index,counter;
    lt_hash_spot *new,*point;
    char *why;
#endif /* DEBUG */
    data = (void *)malloc(unit_count * unit_size);
#ifdef DEBUG
    /* If debug is set, we have a 64k element hash table which uses the low
       16 bits of the memory allocated as the hash index.  Each element in
       the hash table is a linked list, which has the address and number of
       bytes allocated for each piece of allocated memory */

    hash_index = (int)data;
    hash_index %= HASH_SIZE;
    /* Set up the string used to tell people why this memory was
       allocated */
    if((why = malloc(strlen(whence) + 2)) == 0) {
        printf("Aieeee, mem alloc problem\n");
        exit(64);
        }
    strncpy(why,whence,strlen(whence) + 1);
#ifdef THREADS
    pthread_mutex_lock(&alloc_lock);
#endif /* THREADS */
    /* Initialize the lt hash the first time we run this routine */
    if(lt_hash_initialized == 0) {
        for(counter = 0; counter < HASH_SIZE; counter++)
            lt_hash[counter].allocated_size =0;
        lt_hash_initialized = 1;
        }
    /* If the hash pointer is empty, use that */
    if(lt_hash[hash_index].allocated_size == 0) {
        lt_hash[hash_index].allocated_size = unit_count * unit_size;
        lt_hash[hash_index].allocated_memory = data;
        lt_hash[hash_index].de_donde = why;
        lt_hash[hash_index].next = 0;
        }
    /* Otherwise, go down the linked list, place the new node at the
       end of the branch in question */
    else {
        new = malloc(sizeof(lt_hash_spot));
        if(new == NULL) {
            printf("Aieeee, can not allocate memeory for lt_hash_spot!\n");
            exit(1);
            }
        new->allocated_size = unit_count * unit_size;
        new->next = 0;
        new->allocated_memory = data;
        new->de_donde = why;
        point = lt_hash[hash_index].next;
        if(point == 0) {
            lt_hash[hash_index].next = new;
            }
        else {
            while(point->next != 0)
                point = point->next;
            point->next = new;
            }
        }
    total_allocated_memory += unit_count * unit_size;
#ifdef THREADS
    pthread_mutex_unlock(&alloc_lock);
#endif /* THREADS */
#endif /* DEBUG */
    if(data == NULL) {
        /* Securty: In a situtation where we can not allocate memory,
           the subsequent behavior of the program is undefined.  Hence,
           the best thing to do is exit then and there */
        printf("Aieeeeee, can not allocate memory!");
        exit(64);
        return (void *)0;
        }
    return data;
    }

/* js_dealloc: Deallocate memory from the underlying OS
   input: A pointer to the memory we wish to free
   output: -1 on failure, 1 on success */

int js_dealloc(void *pointer) {
#ifdef DEBUG
    lt_hash_spot *lpoint,*llast;
    unsigned int hash_index;
#endif /* DEBUG */
    if(pointer == 0) /* We don't bother with already dealloced memory */
            return 1;
#ifdef DEBUG
    /* We need to find out the number of bytes that we have allocated
       for this given address; subtract that from the total amount of
       memory used, then removed the node which keeps track of the allocated
       memory in question.  */
#ifdef THREADS
    pthread_mutex_lock(&alloc_lock);
#endif /* THREADS */
    hash_index = (int)pointer;
    hash_index %= HASH_SIZE;
    if(lt_hash[hash_index].allocated_memory == pointer) {
        lpoint = lt_hash[hash_index].next;
        total_allocated_memory -= lt_hash[hash_index].allocated_size;
        if(lpoint == 0) {
            lt_hash[hash_index].allocated_size = 0;
            lt_hash[hash_index].allocated_memory = 0;
            if(lt_hash[hash_index].de_donde != 0)
                free(lt_hash[hash_index].de_donde);
            lt_hash[hash_index].de_donde = 0;
            }
        else {
            lt_hash[hash_index].allocated_size = lpoint->allocated_size;
            lt_hash[hash_index].allocated_memory = lpoint->allocated_memory;
            lt_hash[hash_index].next = lpoint->next;
            if(lt_hash[hash_index].de_donde != 0)
                free(lt_hash[hash_index].de_donde);
            lt_hash[hash_index].de_donde = lpoint->de_donde;
            free(lpoint);
            }
        }
    else {
        lpoint = lt_hash[hash_index].next;
        llast = 0;
        while(lpoint->allocated_memory != pointer) {
            llast = lpoint;
            lpoint = lpoint->next;
            if(lpoint == 0) {
                printf("Fatal: lpoint is 0; should not be\n");
                printf("Debugging info: hash_index %x\n",hash_index);
                lpoint = lt_hash[hash_index].next;
                while(lpoint != 0) {
                    printf("lpoint: %p\n",lpoint);
                    lpoint = lpoint->next;
                    }
                exit(1);
                }
            }
        total_allocated_memory -= lpoint->allocated_size;
        if(llast == 0) {
            lt_hash[hash_index].next = lpoint->next;
            }
        else {
            llast->next = lpoint->next;
            }
        if(lpoint->de_donde != 0)
            free(lpoint->de_donde);
        free(lpoint);
        }
#ifdef THREADS
    pthread_mutex_unlock(&alloc_lock);
#endif /* THREADS */
#endif /* DEBUG */
    free(pointer);
    return 1;
    }

#ifdef DEBUG
/* Function which prints to the standard output all unallocated strings which
   we have a record of */
int js_show_leaks() {
    lt_hash_spot *point;
    int counter,total = 0;
    for(counter = 0; counter < HASH_SIZE; counter++) {
        point = &lt_hash[counter];
        while(point != 0 && point->allocated_memory != 0) {
            if(point->de_donde != 0) {
                printf("%s: %d bytes at %p\n",point->de_donde,
                       point->allocated_size,point->allocated_memory);
                total += point->allocated_size;
                }
            point = point->next;
            }
        }
    printf("Allocated memory accounted for:       %d\n",total);
    printf("Allocated memory *not* accounted for: %d\n",
           total_allocated_memory - total);
    printf("Total memory allocated:               %d\n",
           total_allocated_memory);
    return 0;
    }
#else  /* DEBUG */
#define js_show_leaks()
#endif /* DEBUG */

/* Routine which tells us how much memory we have allocated.
   Input: None
   Output: The number of bytes allocated by js_alloc routines; 0 if
           it can not be determine (we do not have DEBUG set to keep
           track of it)
*/

int js_tell_memory_allocated() {
#ifdef DEBUG
    return total_allocated_memory;
#else
    return 0;
#endif
    }

/* js_show_stdout: Display the contents of a given js_string
                          object on standard output
   input: Pointer to js_string object
   output: -1 on failure, 1 on success */
int js_show_stdout(js_string *js) {
    int counter = 0;

    if(js_has_sanity(js) < 0)
        return -1;

    while(counter < js->unit_size * js->unit_count) {
        putc(*(js->string + counter),stdout);
        counter++;
        }

    return 1;
    }

/* show_esc_stdout: Display a csv1-compatible backslash escaped
                    version of a given js_string object on standard output
   input: Pointer to js_string object
   output: -1 on failure, 1 on success */
int show_esc_stdout(js_string *js) {
    int counter = 0;
    unsigned char this;

    if(js_has_sanity(js) < 0)
        return -1;

    if(js->unit_size != 1) /* There is a bug which changes this while keeping
                              the sanity */
        return -1;
    while(counter < js->unit_size * js->unit_count) {
        this = *(js->string + counter);
        if(this < 32 || this > 126) {
            printf("\\%03o",this);
            }
        else if(this == '\\' || this == '%') {
            printf("\\%c",this);
            }
        else {
            putc(*(js->string + counter),stdout);
            }
        counter++;
        }

    return 1;
    }

/* safe_esc_stdout: Display a safe csv2-compatible backslash escaped
                    version of a dlabel; this will make an
                    hex escape sequence anything
                    that is not [A-Za-z0-9\-\_]
   input: Pointer to js_string object
   output: -1 on failure, 1 on success */
int safe_esc_stdout(js_string *js) {
    int counter = 0;
    unsigned char this;

    if(js_has_sanity(js) < 0)
        return -1;

    if(js->unit_size != 1) /* There is a bug which changes this while keeping
                              the sanity */
        return -1;
    while(counter < js->unit_size * js->unit_count) {
        this = *(js->string + counter);
        /* A lot of the fetchzone code makes the first character a
         * space, which needs to be faithfully copied */
        if(this == ' ' && counter == 0) {
                printf(" ");
        }
        else if((this >= 'A' && this <= 'Z') || (this >= 'a' && this <= 'z')
           || this == '-' || this == '_' || this > 127 /* UTF-8 */
           || this == '.' || this == '@' || (this >= '0' && this <= '9')) {
            printf("%c",this);
            }
        /* We show a star only if it is the first character and the second
         * character is a '.' character */
        else if(this == '*' && counter == 0 && js->unit_count > 2 &&
                *(js->string + 1) == '.') {
            printf("%c",this);
            }
        else {
            printf("\\x%02x",this);
            }
        counter++;
        }

    return 1;
    }

/* js_getline_stdin: Get a line from the standard input
   input: To to js_string object to put stdin contents in
   output: JS_ERROR on error, JS_SUCCESS on success.
   note: js->encoding needs to be set to something besides JS_BINARY */
int js_getline_stdin(js_string *js) {
    char *temp; /* Temporary place to put chars until they become
                   a part of js */
    js_string *newlines, *append;
    int counter = 0;
    temp = js_alloc(js->unit_size,1);
    newlines = js_create(256,js->unit_size);
    if(newlines == 0)
        return JS_ERROR;
    newlines->encoding = js->encoding;
    append = js_create(256,js->unit_size);
    if(append == 0) {
        js_destroy(newlines);
        js_dealloc(temp);
        return JS_ERROR;
        }
    js_newline_chars(newlines);
    if(js_has_sanity(js) == JS_ERROR)
        goto error;
    if(temp == 0) {
        js_destroy(newlines);
        js_destroy(append);
        js_dealloc(temp);
        return JS_ERROR;
        }
    /* Blank out the js string */
    js->unit_count = 0;
    while(!feof(stdin) && js_match(newlines,js) == -2) {
        temp[counter] = getc(stdin);
        counter++;
        if(counter >= js->unit_size) {
            counter = 0;
            js_str2js(append,temp,1,js->unit_size);
            if(js_append(append,js) == JS_ERROR)
                goto error;
            }
        }

    /* Success! */
    js_destroy(append);
    js_destroy(newlines);
    js_dealloc(temp);
    return JS_SUCCESS;

    error:
        js_destroy(append);
        js_destroy(newlines);
        js_dealloc(temp);
        return JS_ERROR;
    }

/* js_open: Open a file
   input:  js_string pointing to file we wish to open, flags
   output: void */
void js_open(js_string *filename, js_file *desc, int flags) {
    char temp[256];

    /* Return if the length of the string is greater than
       the space we allocated for the filename */
    if(filename->unit_count * filename->unit_size > 255) {
        desc->filetype = -1;
        return;
        }

    /* Copy over the filename to the temp string to make it a
       null terminated string */

    js_js2str(filename,temp,255);

    desc->filetype = JS_OPEN2;
    desc->file_desc = open(temp,flags,00600);
    desc->number = -1; /* You can set up buffering later if you want */
    desc->eof = 0; /* Are we at the end of file? */
    desc->buffer = 0;

    if(desc->file_desc == -1) {
        desc->filetype = -1;
        return;
        }

    return;

    }

/* js_open_append: Open a file for appending
   input:  js_string pointing to file we wish to open
   output: JS_SUCCESS on success, JS_ERROR on error */
int js_open_append(js_string *filename, js_file *desc) {
    js_open(filename,desc,O_WRONLY | O_APPEND | O_CREAT);
    if(desc->filetype == -1)
        return JS_ERROR;
    else
        return JS_SUCCESS;
    }

/* js_open_write: Open a file for writing
   input:  js_string pointing to file we wish to open
   output: JS_SUCCESS on success, JS_ERROR on error */
int js_open_write(js_string *filename, js_file *desc) {
    js_open(filename,desc,O_WRONLY | O_CREAT);
    if(desc->filetype == -1)
        return JS_ERROR;
    else
        return JS_SUCCESS;
    }

/* js_open_read: Open a file for reading
   input:  js_string pointing to file we wish to open
   output: JS_SUCCESS on success, JS_ERROR on error */
int js_open_read(js_string *filename,js_file *desc) {
    js_open(filename,desc,O_RDONLY);
    if(desc->filetype == -1)
        return JS_ERROR;
    else
        return JS_SUCCESS;
    }

/* js_rewind: Rewind to the beginning of a given file
   input: File descriptor
   output: JS_ERROR (-1) on error, JS_SUCCESS on success */
int js_rewind(js_file *desc) {

    if(desc == 0)
        return JS_ERROR;
    if(desc->filetype != JS_OPEN2)
        return JS_ERROR;

    /* Get rid of the buffer */
    if(desc->buffer != 0)
        js_destroy(desc->buffer);
    desc->number = -1;

    if(lseek(desc->file_desc,0,SEEK_SET) == -1)
        return JS_ERROR;

    return JS_SUCCESS;

    }

/* js_read: Read n bytes from a file, and place those bytes in a js_string
            object.
   input: File descriptor, js_string object, bytes to read
   output: JS_ERROR (-1) on error, bytes read on success */
int js_read(js_file *desc, js_string *js, int count) {

    ssize_t value;
    int ret;

    /* Sanity checks */
    if(js_has_sanity(js) == JS_ERROR)
        return JS_ERROR;
    if(desc->filetype != JS_OPEN2)
        return JS_ERROR;
    if(count % js->unit_size != 0)
        return JS_ERROR;
#ifndef MINGW32
    if(count < 0 || count > 1048576)
        return JS_ERROR;
#if (SSIZE_MAX < 1048576)
    if(count > SSIZE_MAX)
        return JS_ERROR;
#endif /* SSIZE_MAX small */
#else
    if(count < 0 || count > 1024)
        return JS_ERROR;
#endif

    if(count > js->unit_size * js->max_count)
        return JS_ERROR;

    value = read(desc->file_desc,js->string,count);

    if(value == -1)
        return JS_ERROR;

    ret = value;

    if(ret % js->unit_size != 0) {
         js->unit_count = 0;
         return JS_ERROR;
         }

    js->unit_count = ret / js->unit_size;

    return ret;

    }

/* js_write: Write from a js_string object in to a file
   input: File descriptor, js_string object
   output: JS_ERROR (-1) on error, JS_SUCCESS on success */
int js_write(js_file *desc, js_string *js) {

    ssize_t value;
    int to_write,written;

    /* Sanity checks */
    if(js_has_sanity(js) == JS_ERROR)
        return JS_ERROR;
    if(desc->filetype != JS_OPEN2)
        return JS_ERROR;

    to_write = js->unit_size * js->unit_count;
#ifndef MINGW32
    if(to_write < 0 || to_write > 1048576)
        return JS_ERROR;
#if (SSIZE_MAX < 1048576)
    if(to_write > SSIZE_MAX)
        return JS_ERROR;
#endif /* SSIZE_MAX small */
#else
    if(to_write < 0 || to_write > 1024)
        return JS_ERROR;
#endif

    value = write(desc->file_desc,js->string,to_write);

    written = value;

    if(written != to_write)
        return JS_ERROR;

    return JS_SUCCESS;

    }

/* js_close: Close an opened file
   input: js file descriptor
   output: JS_ERROR (-1) on error, JS_SUCCESS (1) on success */
int js_close(js_file *desc) {
    int ret;

    /* Close the file, making sure we closed it */
    ret = close(desc->file_desc);

    /* Get rid of the buffer */
    if(desc->buffer != 0)
        js_destroy(desc->buffer);
    desc->number = -1;

    if(ret == -1)
        return JS_ERROR;

    /* Change the filetype of the closed file to an invalid filetype */
    desc->filetype = JS_ERROR;

    return JS_SUCCESS;

    }


/* Some UNIX systems do not have a flock(), since it did not become part
   of the POSIX specification.  Since MaraDNS does not currently use
   these routines, we can safely not compile them. */

#ifndef NO_FLOCK

/* js_lock: Lock a file using flock()
   input: Pointer to file desc
   output: JS_ERROR on error, JS_SUCCESS on success */
int js_lock(js_file *desc) {

    /* Lock the file: This may freeze here waiting for lock */
    flock(desc->file_desc,LOCK_EX);

    return JS_SUCCESS;

    }

/* js_unlock: Unlock a file using flock()
   input: Pointer to file desc
   output: JS_ERROR on error, JS_SUCCESS on success */
int js_unlock(js_file *desc) {

    /* Lock the file: This may freeze here waiting for lock */
    flock(desc->file_desc,LOCK_UN);

    return JS_SUCCESS;

    }

#endif /* SOLARIS, see commentary above */

/* js_buf_eof: Tell us if we hit the end of a given file
   input: Pointer to file descriptor
   output: 0 if not end of file, otherwise 1 */
int js_buf_eof(js_file *desc) {
    if(desc->eof && desc->number >= desc->buffer->unit_count)
        return 1;
    return 0;
    }

/* js_buf_read: Read a new chunk of the file and put it in the buffer
   input: Pointer to a file descriptor
   output: JS_ERROR (-1) on error, JS_SUCCESS(1) on success */
int js_buf_read(js_file *desc) {
    int bytes_read;

    /* Sanity check */
    if(js_has_sanity(desc->buffer) == -1)
        return -1;
    bytes_read = js_read(desc,desc->buffer,
                         JS_BUFSIZE * desc->buffer->unit_size);
    if(bytes_read != JS_BUFSIZE * desc->buffer->unit_size)
        desc->eof = 1;
    desc->number = 0;

    return JS_SUCCESS;
    }

/* js_buf_getline: Grab a line from an opened file (uses buffering)
   input: Pointer to file descriptor of open file, pointer to js_string to
          put line in
   output: JS_ERROR (-1) on error, JS_SUCCESS(1) on success,
           -2 if the line is too long for the string
 */

int js_buf_getline(js_file *desc, js_string *js) {
    js_string *newlines, *temp;
    int next_newln;
    int overflowed = 0;

    /* Sanity check */
    if(js_has_sanity(js) == -1)
        return -1;

    /* This is tricky to do because we have to do our own buffering */
    /* If the buffer is not there yet, create one */
    if(desc->number == -1) {
        if(desc->buffer == 0)
            desc->buffer = js_create(JS_BUFSIZE + 10,js->unit_size);
        js_buf_read(desc);
        }

    /* Sanity check */
    if(js->unit_size != desc->buffer->unit_size)
        return -1;

    /* Make an js_string object that is all of the allowed newline
       characters */
    newlines = js_create(js->max_count,js->unit_size);
    js_copy(js,newlines);
    js_newline_chars(newlines);

    /* Find the next newline character in the string */
    next_newln = js_match_offset(newlines,desc->buffer,desc->number);
    /* If we are at the end of the buffer in question w/o finding a
       newline... */
    if(next_newln == -2 && desc->eof == 0) {
        /* ...then we put the rest of the buffer in js, but only if
           it will fit in js.  Otherwise, we blank out js and return
           success. */
        if(JS_BUFSIZE + 1 - desc->number < js->max_count && overflowed == 0)
            js_substr(desc->buffer,js,desc->number,
                      JS_BUFSIZE - desc->number);
        else {
            js_str2js(js,"",0,js->unit_size); /* blank line if overflow */
            overflowed = 1;
            }
        /* And load up a new buffer */
        js_buf_read(desc);
        /* If the new buffer does not have a newline in it, and is a full-sized
           buffer, then we handle this special case */
        while(js_match(newlines,desc->buffer) == -2 && desc->eof == 0) {
            if(js->unit_count + JS_BUFSIZE < js->max_count && overflowed == 0)
                js_append(desc->buffer,js);
            else {
                js_str2js(js,"",0,js->unit_size);
                overflowed = 1;
                }
            js_buf_read(desc);
            }
        next_newln = js_match(newlines,desc->buffer);
        temp = js_create(js->max_count,js->unit_size);
        js_substr(desc->buffer,temp,0,next_newln + 1);
        js_append(temp,js);
        js_destroy(temp);
        if(next_newln != -2)
            desc->number = next_newln + 1;
        if(desc->number >= JS_BUFSIZE)
            js_buf_read(desc);
        js_destroy(newlines);
        if(overflowed == 1)
            return -2;
        return JS_SUCCESS;
        }
    else if(next_newln == -2) {
        /* We have EOF.  Read the string and return it */
        /* With overflow checking, of course */
        if(desc->buffer->unit_count - desc->number < js->max_count
           && overflowed == 0)
            js_substr(desc->buffer,js,desc->number,
                      desc->buffer->unit_count - desc->number);
        else {
            js_str2js(js,"",0,js->unit_size);
            overflowed = 1;
            }

        /* Make sure js_buf_eof sees it as an EOF */
        desc->number = desc->buffer->unit_count + 1;

        js_destroy(newlines);
        if(overflowed == 1)
            return -2;
        return JS_SUCCESS;
        }

    /* At this point, we can assume that we found a newline in the buffer */
    if(next_newln + 1 - desc->number < js->max_count) {
        js_substr(desc->buffer,js,desc->number,next_newln + 1 - desc->number);
        desc->number = next_newln + 1;
        if(desc->number >= JS_BUFSIZE)
            js_buf_read(desc);
        }
    else
        js_str2js(js,"",0,js->unit_size);

    js_destroy(newlines);

    if(overflowed == 1)
        return -2;

    return JS_SUCCESS;

    }

/* js_qstr2js: "Quick" version of js_str2js routine.
   input: pointer to js object, NULL-terminated string
   output: JS_ERROR on failure, JS_SUCCESS on success
   note: it's here because of strlen call */
int js_qstr2js(js_string *js, char *string) {
    if(js == 0) {
        return JS_ERROR;
        }
    return js_str2js(js,string,strlen(string),js->unit_size);
    }

/* js_adduint32: Add a 32-bit number to the end of a js_string obejct
                 in big-endian format, where the js->unit_size is one.
   input: pointer to js_string object, number to add
   output: JS_ERROR on error, JS_SUCCESS on success
   (This is in OS because of the dependency on uint32_t
*/

int js_adduint32(js_string *js, uint32_t number) {

    /* sanity checks */
    if(js_has_sanity(js) == JS_ERROR)
        return JS_ERROR;
    if(js->unit_size != 1)
        return JS_ERROR;
    /* No buffer overflows */
    if(js->unit_count + 4 >= js->max_count)
        return JS_ERROR;

    /* Add the uint16 to the end of the string */
    *(js->string + js->unit_count) = (number >> 24) & 0xff;
    *(js->string + js->unit_count + 1) = (number >> 16) & 0xff;
    *(js->string + js->unit_count + 2) = (number >> 8) & 0xff;
    *(js->string + js->unit_count + 3) = number & 0xff;
    js->unit_count += 4;

    return JS_SUCCESS;
    }

/* js_readuint32: Read a single uint32 (in big-endian format)
                  from a js_string object
   input: pointer to js_string object, offset from beginning
          of string (0 is beginning of string, 1 second byte, etc.)
   output: JS_ERROR on error, value of uint32 on success
           (Hack: 0xffffffff is the same as -1 in comparisons)
   (This is in OS because of the dependency on uint32_t)
*/

uint32_t js_readuint32(js_string *js, unsigned int offset) {

    uint32_t ret;
    /* sanity checks */
    if(js_has_sanity(js) == JS_ERROR)
        return 0xffffffff;
    if(js->unit_size != 1)
        return 0xffffffff;
    if(offset > (js->unit_count - 4) || offset < 0)
        return 0xffffffff;

    ret = ((*(js->string + offset) << 24) & 0xff000000) |
          ((*(js->string + offset + 1) << 16) & 0xff0000) |
          ((*(js->string + offset + 2) << 8) & 0xff00) |
           (*(js->string + offset + 3) & 0xff);

    /* Make sure we do not inadvertently return JS_ERROR */
    if(ret == 0xffffffff)
        ret = 0xfffffffe;

    return ret;

    }

/* js_strnlen: Determine the length of a null-terminated string, up
 * to the length determined by the limit.  This is here because strnlen
 * is, alas, not a portable string library call.  This call is originally
 * by Matthew T. Russotto */
int js_strnlen(char *s, uint32_t limit) {
        uint32_t len;
        if(limit > 2147483600 || limit < 0) { /* Just under 2 ** 31 */
                return JS_ERROR;
        }
        if(s == NULL) {
                return JS_ERROR;
        }
        len = 0;
        while(len < limit && (*s++)) {
                len++;
        }
        return len;
}