~ubuntu-branches/ubuntu/quantal/ncbi-tools6/quantal

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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
/*  ncbibs.c
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE                          
*               National Center for Biotechnology Information
*                                                                          
*  This software/database is a "United States Government Work" under the   
*  terms of the United States Copyright Act.  It was written as part of    
*  the author's official duties as a United States Government employee and 
*  thus cannot be copyrighted.  This software/database is freely available 
*  to the public for use. The National Library of Medicine and the U.S.    
*  Government have not placed any restriction on its use or reproduction.  
*                                                                          
*  Although all reasonable efforts have been taken to ensure the accuracy  
*  and reliability of the software and data, the NLM and the U.S.          
*  Government do not and cannot warrant the performance or results that    
*  may be obtained by using this software or data. The NLM and the U.S.    
*  Government disclaim all warranties, express or implied, including       
*  warranties of performance, merchantability or fitness for any particular
*  purpose.                                                                
*                                                                          
*  Please cite the author in any work or product based on this material.   
*
* ===========================================================================
*
* File Name:  ncbibs.c
*
* Author:  Jim Ostell
*
* Version Creation Date:  3/4/91
*
* $Revision: 6.8 $
*
* File Description:
*   ByteStore functions
*      NOTE: increasing BS_MAXALLOC will require changing len, and
* 		   len_avail to Int4 (ncbibs.h)
*
*   This version needs the following enhancements:
*   1) a BSCompact function to pack a fragmented ByteStore
*   2) use made of MIN_BSALLOC to make all allocated space at
*      least MIN_BSALLOC long, so it would be possible to have len_avail
*      bigger than len at internal BSUnits as well as at the end.  This
*      will make insertion and appending of small amounts much more
*      efficient.
*
* Modifications:  
* --------------------------------------------------------------------------
* Date     Name        Description of modification
* -------  ----------  -----------------------------------------------------
*
* ==========================================================================
*/

#include <ncbi.h>
#include <ncbiwin.h>

         /* maximum size allocated for BSUnit.str */
#define MAX_BSALLOC 32700
         /* minimum size block to allocate except on new */
#define MIN_BSALLOC 1024

#ifdef MSC_VIRT
extern Nlm_Boolean wrote_to_handle;  /* needed by Microsoft DOS Virtual mem */
#undef MAX_BSALLOC					 /* bad with big blocks */
#define MAX_BSALLOC 10000
#endif

/*****************************************************************************
*
*   Pointer Nlm_BSMerge(bsp, dest)
*      if dest == NULL, allocates storage for merge and returns pointer to it
*
*****************************************************************************/
NLM_EXTERN Nlm_VoidPtr LIBCALL  Nlm_BSMerge (Nlm_ByteStorePtr bsp, Nlm_VoidPtr dest)

{
	Nlm_BytePtr tmp, from;
	Nlm_BSUnitPtr bsup;
    size_t size;

	if (bsp == NULL)
		return NULL;

    size = Nlm_BSLen(bsp) + 1;
    if (size > SIZE_MAX) {
		ErrPostEx(SEV_ERROR, 0, 0, "size[%u] is bigger then SIZE_MAX[%u]",
			size, SIZE_MAX);
        /* should post an error here */
        return NULL;
    }

	if (dest == NULL)         /* allocate storage with room for null at end */
	{
		dest = Nlm_MemNew(size);
		if (dest == NULL) return dest;
	}

	tmp = (Nlm_BytePtr) dest;
	bsup = bsp->chain;

	while (bsup != NULL)
	{
		from = (Nlm_BytePtr) Nlm_HandLock(bsup->str);
		Nlm_MemCopy(tmp, from, bsup->len);
		Nlm_HandUnlock(bsup->str);
		tmp += bsup->len;
		bsup = bsup->next;
	}

	return dest;
}

/*****************************************************************************
*
*   ByteStorePtr Nlm_BSNew(len)
*   	creates a big enough bytestore for len
*
*****************************************************************************/
NLM_EXTERN Nlm_ByteStorePtr LIBCALL Nlm_BSNew (Nlm_Int4 len)

{
	Nlm_ByteStorePtr bsp;
	Nlm_Int4 added;

	bsp = (Nlm_ByteStorePtr) Nlm_MemNew(sizeof(ByteStore));
	if (bsp == NULL)
		return NULL;

	added = Nlm_BSAdd(bsp, len, FALSE);
	if (added < len)
		bsp = Nlm_BSFree(bsp);
	return bsp;
}

/*****************************************************************************
*
*   Int4 BSAdd(bsp, len, use_min_size)
*   	adds len bytes BEFORE current bsp->seekptr
*       bsp->seekptr returned pointing at first added byte
*   	returns bytes added
*       if (use_min_size) then does not add anything smaller than MIN_BSALLOC
*
*****************************************************************************/
NLM_EXTERN Nlm_Int4 LIBCALL Nlm_BSAdd (Nlm_ByteStorePtr bsp, Nlm_Int4 len, Nlm_Boolean use_min_size)

{
	Nlm_BSUnitPtr bsup,      /* current bsunit */
		          ccbsup,    /* bsp->curchain */
				lastbsup,    /* bsunit after added section  */
				prevbsup;    /* bsunit before added section */
	Nlm_BytePtr to, from;
	Nlm_Int4 added = 0,
		      tlen;
	Nlm_Handle thand;

	if ((bsp == NULL) || (len == 0))
		return added;

	lastbsup = NULL;
	prevbsup = NULL;
	ccbsup = bsp->curchain;

	if (bsp->chain != NULL)        /* add or insert in exisiting chain */
	{
		if (bsp->seekptr == bsp->chain_offset)   /* before start of block */
		{
			lastbsup = ccbsup;       /* it comes after */
			if (bsp->seekptr != 0)          /* something before it */
			{
				prevbsup = bsp->chain;
				while (prevbsup->next != ccbsup)
					prevbsup = prevbsup->next;
			}
		}										 /* at very end of data */
		else if (bsp->seekptr == (bsp->chain_offset + ccbsup->len))
		{
			prevbsup = ccbsup; /* JK */
		}										 /* after all blocks */
		else if (bsp->seekptr >= (bsp->chain_offset + ccbsup->len_avail))
		{
			prevbsup = ccbsup;
		}
		else                /* split a bsunit */
		{
			bsup = (Nlm_BSUnitPtr) Nlm_MemNew(sizeof(Nlm_BSUnit));
			if (bsup == NULL) return added;
			if (bsp->chain_offset != 0)          /* not first BSUnit */
			{
				prevbsup = bsp->chain;
				while (prevbsup->next != ccbsup)
					prevbsup = prevbsup->next;
				prevbsup->next = bsup;      /* insert new bsunit */
			}
			else
			    bsp->chain = bsup;
			bsup->next = ccbsup;
			tlen = bsp->seekptr - bsp->chain_offset;  /* len of first half */
			bsup->str = Nlm_HandNew((size_t) tlen);
			if (bsup->str == NULL) return added;
			bsup->len = (Nlm_Int2) tlen;
			bsup->len_avail = (Nlm_Int2) tlen;
			to = (Nlm_BytePtr) Nlm_HandLock(bsup->str);
			thand = ccbsup->str;
			from = (Nlm_BytePtr) Nlm_HandLock(thand);
			Nlm_MemCopy(to, from, (size_t) tlen);
#ifdef MSC_VIRT
			wrote_to_handle = TRUE;
#endif
			Nlm_HandUnlock(bsup->str);
			from += tlen;      /* point past copied region */
			tlen = ccbsup->len - tlen;    /* the last half */
			ccbsup->len = (Nlm_Int2) tlen;
			ccbsup->len_avail = (Nlm_Int2) tlen;
			ccbsup->str = Nlm_HandNew((size_t) tlen);
			if (ccbsup->str == NULL) return added;
			to = (Nlm_BytePtr) Nlm_HandLock(ccbsup->str);
			Nlm_MemCopy(to, from, (size_t) tlen);
#ifdef MSC_VIRT
	wrote_to_handle = TRUE;
#endif
			Nlm_HandUnlock(ccbsup->str);
			Nlm_HandUnlock(thand);
			Nlm_HandFree(thand);
			prevbsup = bsup;
			lastbsup = ccbsup;
		}
	}

	ccbsup = NULL;
	bsup = NULL;
	while (len)
	{
		bsup = (Nlm_BSUnitPtr) Nlm_MemNew(sizeof(Nlm_BSUnit));
		if (bsup == NULL)
			return added;
		if (len < MAX_BSALLOC)

		{
			if ((use_min_size) && (len < MIN_BSALLOC))
				len = MIN_BSALLOC;
			tlen = len;
		}
		else
			tlen = MAX_BSALLOC;
		bsup->str = Nlm_HandNew((size_t)tlen);
		if (bsup->str == NULL)
		{
			Nlm_MemFree(bsup);
			return added;
		}
		bsup->len_avail = (Nlm_Int2) tlen;
		if (prevbsup == NULL)
			bsp->chain = bsup;
		else
			prevbsup->next = bsup;
		if (ccbsup == NULL)
			ccbsup = bsup;          /* current position */
		prevbsup = bsup;
		len -= tlen;
		added += tlen;
	}
	if (bsup != NULL)
		bsup->next = lastbsup;
	bsp->curchain = ccbsup;
	bsp->chain_offset = bsp->seekptr;   /* added block starts at seekptr */
	return added;
}

/*****************************************************************************
*
*   Int4 BSDelete(bsp, len)
*   	deletes len bytes starting at current bsp->seekptr
*   	returns bytes deleted
*
*****************************************************************************/
NLM_EXTERN Nlm_Int4 LIBCALL  Nlm_BSDelete (Nlm_ByteStorePtr bsp, Nlm_Int4 len)

{
	Nlm_BSUnitPtr bsup,      /* current bsunit */
		          ccbsup,    /* bsp->curchain */
				nextbsup,    /* bsunit after added section  */
				prevbsup;    /* bsunit before added section */
	Nlm_BytePtr to, from;
	Nlm_Int4 added = 0,
		      offset,
		      tlen,
			  start,
			  save;
	Nlm_Handle thand;

	if ((bsp == NULL) || (len == 0) || (bsp->chain == NULL) ||
		(bsp->seekptr >= bsp->totlen))
		return added;

	if ((bsp->seekptr + len) > bsp->totlen)   /* deleting too much */
		len = bsp->totlen - bsp->seekptr;

	nextbsup = NULL;
	ccbsup = bsp->curchain;
	start = bsp->chain_offset;
	offset = bsp->seekptr - bsp->chain_offset;
	if (offset)           /* will leave part of first bsunit */
		prevbsup = ccbsup;
	else if (bsp->chain_offset != 0)          /* something before this bsunit */
	{
		prevbsup = bsp->chain;
		while (prevbsup->next != ccbsup)
			prevbsup = prevbsup->next;
	}
	else
		prevbsup = NULL;       /* will remove beginning of first bsunit */

	bsup = ccbsup;
	while (len)
	{
		nextbsup = bsup->next;
		tlen = bsup->len - offset;
		if (tlen > len)
			tlen = len;			 /* will remove tlen bytes from this BSUnit*/
		save = bsup->len - tlen;
		if (save)    /* some bytes left after delete */
		{
			thand = bsup->str;
			bsup->str = Nlm_HandNew((size_t) save);
			if (bsup->str == NULL) return added;
			bsup->len = (Nlm_Int2) save;
			bsup->len_avail = (Nlm_Int2) save;
			to = (Nlm_BytePtr) Nlm_HandLock(bsup->str);
			from = (Nlm_BytePtr) Nlm_HandLock(thand);
			if (offset)         /* get the beginning */
				Nlm_MemCopy(to, from, (size_t) offset);
			save -= offset;
			if (save)           /* save end */
				Nlm_MemCopy((to + offset), (from + (offset + tlen)), (size_t)save);
			Nlm_HandUnlock(thand);
			Nlm_HandFree(thand);
#ifdef MSC_VIRT
	wrote_to_handle = TRUE;
#endif
			Nlm_HandUnlock(bsup->str);
			if (tlen < len)
				bsup = nextbsup;
		}
		else                    /* delete the whole thing */
		{
			Nlm_HandFree(bsup->str);
			Nlm_MemFree(bsup);
			bsup = nextbsup;
		}
		offset = 0;
		len -= tlen;
		added += tlen;
	}
	if ((bsup != prevbsup) ||   /* break in chain, rejoin it */
             ((bsup == NULL) && (prevbsup == NULL)))   /* chain gone */
	{
		if (prevbsup == NULL)
			bsp->chain = bsup;
		else
			prevbsup->next = bsup;
	}
	bsp->curchain = bsup;
	bsp->totlen -= added;
	if (bsp->totlen)
	{
		offset = bsp->seekptr;
		start = 0;
		bsup = bsp->chain;
		while (offset >= (start + bsup->len_avail))
		{
			if (bsup->next == NULL)    /* EOF */
				offset = 0;            /* stop loop */
			else
			{
				start += bsup->len;
				bsup = bsup->next;
			}
		}
		bsp->curchain = bsup;
		bsp->chain_offset = start;
	}
	else
	{
		bsp->chain_offset = 0;
		bsp->curchain = bsp->chain;
	}
	return added;
}

/*****************************************************************************
*
*   Nlm_Int4 Nlm_BSTell(bsp)
*
*****************************************************************************/
NLM_EXTERN Nlm_Int4 LIBCALL  Nlm_BSTell (Nlm_ByteStorePtr bsp)

{
	if (bsp == NULL)
		return 0L;

	return bsp->seekptr;
}

/*****************************************************************************
*
*   Int2 Nlm_BSSeek(bsp, offset, origin)
*   	seeks as fseek()
*   	trys to lock a BSUnit at that seek position
*
*****************************************************************************/
NLM_EXTERN Nlm_Int2 LIBCALL  Nlm_BSSeek (Nlm_ByteStorePtr bsp, Nlm_Int4 offset, Nlm_Int2 origin)

{
	Nlm_Int4 sp, start = 0;
	Nlm_BSUnitPtr bsup;
	Nlm_Boolean done;

	if (bsp == NULL)
		return 1;
	if (bsp->chain == NULL)
		return 1;

	sp = bsp->seekptr;

	switch (origin)
	{
		case SEEK_SET:
			if ((offset > bsp->totlen) || (offset < 0))
				return 1;
			sp = offset;
			break;
		case SEEK_CUR:
			if (((sp + offset) > bsp->totlen) ||
				((sp + offset) < 0 ))
				return 1;
			sp += offset;
			break;
		case SEEK_END:
			if ((ABS(offset) > bsp->totlen) || (offset > 0))
				return 1;
			sp = bsp->totlen + offset;
			break;
		default:
			return 1;
	}

    if (sp == bsp->seekptr)     /* already in right position */
        return 0;

	bsp->seekptr = sp;
	                             /* if a valid seek, lock the right BSUnit */
	bsup = bsp->curchain;

	if (sp == bsp->totlen)    /* seek to EOF */
	{
		if (sp > (bsp->chain_offset + bsup->len_avail))
		{
			start = bsp->chain_offset;
			while (sp > (start + bsup->len_avail))
			{
				start += bsup->len;
				bsup = bsup->next;
			}
		}
	}
	else if ((sp < bsp->chain_offset) ||
		(sp >= (bsp->chain_offset + bsup->len)))
	{
		if (sp < bsp->chain_offset)
		{
			bsup = bsp->chain;
			start = 0;
		}
		else
		{
			start = bsp->chain_offset + bsup->len;
			bsup = bsup->next;
		}
		done = FALSE;
		while (! done)
		{
			if ((start + bsup->len) > sp)
				done = TRUE;
			else
			{
				start += bsup->len;
				bsup = bsup->next;
			}
		}
	}
	if (bsup != bsp->curchain)
	{
		bsp->chain_offset = start;
		bsp->curchain = bsup;
	}
	return 0;
}
/*****************************************************************************
*
*   Int4 Nlm_BSLen(bsp)
*      gives cumulative length of strings in store (not counting \0)
*
*****************************************************************************/
NLM_EXTERN Nlm_Int4 LIBCALL  Nlm_BSLen (Nlm_ByteStorePtr bsp)

{
	if (bsp == NULL)
		return 0L;

	return bsp->totlen;
}

/*****************************************************************************
*
*   Int2 BSPutByte(bsp, value)
*       - returns value on success, EOF on failure
*       - BSPutByte(bsp, EOF) truncates at seekptr
*
*****************************************************************************/
NLM_EXTERN Nlm_Int2 LIBCALL  Nlm_BSPutByte (Nlm_ByteStorePtr bsp, Nlm_Int2 value)

{
	Nlm_Uint1 b;
    if (value == EOF)
        Nlm_BSDelete(bsp, INT4_MAX);
    else {
    	b = (Nlm_Uint1) value;
        if (Nlm_BSWrite(bsp, (Nlm_VoidPtr)&b, 1) == 0)
            return EOF;
    }
    return value;
}

/*****************************************************************************
*
*   Int4 BSWrite(bsp, ptr, len)
*   	returns bytes written from ptr to bsp
*       writes from current seekptr position
*       seekptr left pointing after last byte written
*       allocates storage if needed
*       writes over any data already there
*
*****************************************************************************/
NLM_EXTERN Nlm_Int4 LIBCALL  Nlm_BSWrite (Nlm_ByteStorePtr bsp, Nlm_VoidPtr ptr, Nlm_Int4 len)

{
	Nlm_BytePtr from, to;
	Nlm_BSUnitPtr bsup;
	Nlm_Int4 tlen, added = 0, offset, start, diff;

	if ((bsp == NULL) || (len <= 0))
		return added;

	bsup = bsp->curchain;
	from = (Nlm_BytePtr) ptr;
	offset = bsp->seekptr - bsp->chain_offset;
	start = bsp->chain_offset;



 	while (len)
	{

		if (bsup == NULL)
			tlen = 0;
		else
			tlen = bsup->len_avail - offset;
		if (len < tlen)
			tlen = len;
		if (! tlen)       /* out of space */
		{

			if (Nlm_BSAdd(bsp, len, TRUE) < len)
				return added;
			bsup = bsp->curchain;
			offset = bsp->seekptr - bsp->chain_offset;
			tlen = bsup->len_avail;

			if (len < tlen)

				tlen = len;
		}
		else
		{
			bsp->chain_offset = start;
			bsp->curchain = bsup;
		}
		to = (Nlm_BytePtr)Nlm_HandLock(bsup->str) + offset;
		Nlm_MemCopy(to, from, (size_t) tlen);
#ifdef MSC_VIRT
	wrote_to_handle = TRUE;
#endif
		Nlm_HandUnlock(bsup->str);
		if (bsup->len < (Nlm_Int2)(tlen + offset))   /* added more to this bsunit */
		{
			diff = (tlen + offset) - bsup->len;
			bsp->totlen += diff;
			bsup->len += (Nlm_Int2) diff;

		}
		offset = 0;        /* only offset on first one */
		len -= tlen;
		bsp->seekptr += tlen;
		start += bsup->len;
		added += tlen;
		from += tlen;
		if (len)
			bsup = bsup->next;
	}
	                 /* pointing past end of this BSunit */

	if ((bsp->seekptr - bsp->chain_offset) >= bsup->len_avail)
	{
		if (bsup->next != NULL)      /* not end of space */
		{
			bsp->curchain = bsup->next;
			bsp->chain_offset += bsup->len;  /* should = len_avail */
		}
	}
	return added;
}
/*****************************************************************************
*
*   Int4 BSRead(bsp, ptr, len)
*   	returns bytes read from bsp to ptr
*       reads from current seekptr position
*       seekptr left pointing after last byte read
*
*****************************************************************************/
NLM_EXTERN Nlm_Int4 LIBCALL  Nlm_BSRead (Nlm_ByteStorePtr bsp, Nlm_VoidPtr ptr, Nlm_Int4 len)

{
	Nlm_BytePtr from, to;
	Nlm_BSUnitPtr bsup;
	Nlm_Int4 tlen, added = 0, offset, start;

	if ((bsp == NULL) || (len <= 0))
		return added;

	bsup = bsp->curchain;
	to = (Nlm_BytePtr) ptr;
	offset = bsp->seekptr - bsp->chain_offset;
	start = bsp->chain_offset;

 	while (len)
	{
		if (bsup == NULL)
			return added;
		tlen = bsup->len - offset;
		if (len < tlen)
			tlen = len;
		if (! tlen)       /* out of data */
			return added;
		bsp->chain_offset = start;
		from = (Nlm_BytePtr)Nlm_HandLock(bsup->str) + offset;
		Nlm_MemCopy(to, from, (size_t) tlen);
		Nlm_HandUnlock(bsup->str);
		offset = 0;        /* only offset on first one */
		len -= tlen;
		bsp->seekptr += tlen;
		bsp->curchain = bsup;
		start += bsup->len;
		added += tlen;
		to += tlen;
		if (len)
			bsup = bsup->next;
	}                                    /*** end of this bsunit? **/
	if ((bsp->seekptr - bsp->chain_offset) == bsup->len)
	{
		if (bsup->next != NULL)    /* not EOF */
		{
			bsp->curchain = bsup->next;
			bsp->chain_offset += bsup->len;
		}
	}
	return added;
}

/*****************************************************************************
*
*   Int2 BSGetByte(bsp)
*      reads a byte and increments the seekptr
*      returns EOF on End-Of-BS or error
*
*****************************************************************************/
NLM_EXTERN Nlm_Int2 LIBCALL  Nlm_BSGetByte (Nlm_ByteStorePtr bsp)

{
	Nlm_Int2 retval;
	Nlm_BytePtr ptr;
	Nlm_Int4 diff;
	Nlm_BSUnitPtr bsup;

	if ((bsp == NULL) || (bsp->totlen == 0) || (bsp->seekptr == bsp->totlen))
		return EOF;      /* EOF */

	diff = bsp->seekptr - bsp->chain_offset;
	bsup = bsp->curchain;
	ptr = (Nlm_BytePtr) Nlm_HandLock(bsup->str);
	retval = (Nlm_Int2) *(ptr + diff);
	Nlm_HandUnlock(bsup->str);

	bsp->seekptr++;
	diff++;
	if (diff == bsup->len)
	{
		if (bsp->seekptr != bsp->totlen)    /* not EOF */
		{
			bsp->curchain = bsup->next;
			bsp->chain_offset += bsup->len;
		}
	}
	return retval;	
}

/*****************************************************************************
*
*   Int4 BSInsert(bsp, ptr, len)
*   	returns bytes written from ptr into bsp
*       writes from current seekptr position
*       seekptr left pointing after last byte written
*       allocates storage if needed
*       inserts data BEFORE seekptr
*
*****************************************************************************/
NLM_EXTERN Nlm_Int4 LIBCALL  Nlm_BSInsert (Nlm_ByteStorePtr bsp, Nlm_VoidPtr ptr, Nlm_Int4 len)

{
	Nlm_Int4 added;

	added = Nlm_BSAdd(bsp, len, FALSE);
	if (added != len)
	{
		Nlm_BSDelete(bsp, added);
		return 0;
	}
	added = Nlm_BSWrite(bsp, ptr, len);
	return added;
}

/*****************************************************************************
*
*   Int4 BSInsertFromBS(bsp, bsp2, len)
*   	returns bytes written from bsp2 into bsp
*       reads from bsp2 starting from current seek position
*       writes from current seekptr position
*       seekptr left pointing after last byte written
*       allocates storage if needed
*       inserts data BEFORE seekptr
*
*****************************************************************************/
NLM_EXTERN Nlm_Int4 LIBCALL  Nlm_BSInsertFromBS (Nlm_ByteStorePtr bsp, Nlm_ByteStorePtr bsp2, Nlm_Int4 len)
{
	Nlm_Int4 added;
	Nlm_Int2 x;

	added = Nlm_BSAdd(bsp, len, FALSE);
	if (added != len)
	{
		Nlm_BSDelete(bsp, added);
		return 0;
	}

	for (added = 0; added < len; added++)
	{
		x = Nlm_BSGetByte(bsp2);
		Nlm_BSPutByte(bsp, x);
	}
	return added;
}

/*****************************************************************************
*
*   ByteStorePtr Nlm_BSFree(bsp)
*
*****************************************************************************/
NLM_EXTERN Nlm_ByteStorePtr LIBCALL  Nlm_BSFree (Nlm_ByteStorePtr bsp)

{
	Nlm_BSUnitPtr bsup, tmp;

	if (bsp == NULL)
		return NULL;
		
	bsup = bsp->chain;
	while (bsup != NULL)
	{
		tmp = bsup;
		bsup = bsup->next;
		Nlm_HandFree(tmp->str);
		Nlm_MemFree(tmp);
	}
	return (Nlm_ByteStorePtr) Nlm_MemFree(bsp);
}

/*****************************************************************************
*
*   ByteStorePtr Nlm_BSDup(bsp)
*
*****************************************************************************/

#define BUFSIZE 256

NLM_EXTERN Nlm_ByteStorePtr LIBCALL Nlm_BSDup (Nlm_ByteStorePtr source)

{
  Nlm_Byte          buf [BUFSIZE + 4];
  Nlm_Int4          count;
  Nlm_ByteStorePtr  dest;
  Nlm_Int4          sourceLen;
  Nlm_Int4          sourceLoc;

  dest = NULL;
  if (source != NULL) {
    sourceLen = Nlm_BSLen (source);
    dest = Nlm_BSNew (sourceLen);
    /* read the original location */
    sourceLoc = Nlm_BSTell(source);
    Nlm_BSSeek(source, 0L, SEEK_SET);
    if (dest != NULL) {
      count = MIN (sourceLen, (Nlm_Int4) BUFSIZE);
      while (count > 0 && sourceLen > 0) {
        Nlm_BSRead (source, buf, count);
        Nlm_BSWrite (dest, buf, count);
        sourceLen -= count;
        count = MIN (sourceLen, (Nlm_Int4) BUFSIZE);
      }
      /* for neatness, make the duplicate point to the
	     same location as the old one */
      Nlm_BSSeek(dest, sourceLoc, SEEK_SET);
    }
    /* restore original location */
    Nlm_BSSeek(source, sourceLoc, SEEK_SET);
  }
  return dest;
}

/*****************************************************************************
*
*   Boolean Nlm_BSEqual (bs1, bs2)
*
*****************************************************************************/

NLM_EXTERN Nlm_Boolean LIBCALL Nlm_BSEqual (Nlm_ByteStorePtr bs1, Nlm_ByteStorePtr bs2)

{
  Nlm_Byte  buf1 [256], buf2 [256];
  Nlm_Int4  count1, count2, idx;

  if (bs1 == NULL || bs2 == NULL) {
    if (bs1 == NULL && bs2 == NULL) return TRUE;
    if (Nlm_BSLen (bs1) == 0 && Nlm_BSLen (bs2) == 0) return TRUE;
    return FALSE;
  }

  Nlm_BSSeek (bs1, 0, SEEK_SET);
  Nlm_BSSeek (bs2, 0, SEEK_SET);

  count1 = Nlm_BSRead (bs1, buf1, sizeof (buf1));
  count2 = Nlm_BSRead (bs2, buf2, sizeof (buf2));

  while (count1 == count2 && count2 > 0) {
    for (idx = 0; idx < count1; idx++) {
      if (buf1 [idx] != buf2 [idx]) return FALSE;
    }

    count1 = Nlm_BSRead (bs1, buf1, sizeof (buf1));
    count2 = Nlm_BSRead (bs2, buf2, sizeof (buf2));
  }

  if (count1 != count2) return FALSE;

  return TRUE;
}


/* for copying and swapping of UID lists passed over network to BIG_ENDIAN server */

NLM_EXTERN Nlm_ByteStorePtr LIBCALL Nlm_BSDupAndSwapUint4 (Nlm_ByteStorePtr source)

{
  Nlm_Byte          buf [BUFSIZE + 4];
  Nlm_Int4          count;
  Nlm_ByteStorePtr  dest;
  Nlm_Int4          sourceLen;
  Nlm_Int4          sourceLoc;
  Nlm_Int4          swapLen;

  dest = NULL;
  if (source != NULL) {
    sourceLen = Nlm_BSLen (source);
    dest = Nlm_BSNew (sourceLen);
    /* read the original location */
    sourceLoc = Nlm_BSTell(source);
    Nlm_BSSeek(source, 0L, SEEK_SET);
    if (dest != NULL) {
      count = MIN (sourceLen, (Nlm_Int4) BUFSIZE);
      while (count > 0 && sourceLen > 0) {
        swapLen = Nlm_BSRead (source, buf, count);
        swapLen /= 4;
        /* swap Uint4 if IS_LITTLE_ENDIAN */
        Nlm_SwapUint4Buff ((Nlm_Uint4Ptr) buf, swapLen);
        Nlm_BSWrite (dest, buf, count);
        sourceLen -= count;
        count = MIN (sourceLen, (Nlm_Int4) BUFSIZE);
      }
      /* for neatness, make the duplicate point to the
	     same location as the old one */
      Nlm_BSSeek(dest, sourceLoc, SEEK_SET);
    }
    /* restore original location */
    Nlm_BSSeek(source, sourceLoc, SEEK_SET);
  }
  return dest;
}

/****************************************************************************
*
*   Integer storage utilities
*      These assume integers are store in BIG_ENDIAN order in the ByteStore
*      They read and write Uint2 or Uint4 in the NATIVE endian order
*      All work with UNSIGNED 2 or 4 byte integers
*         (you should cast to make them signed)
*      These are just helper functions. They do no internal consistency
*         checking.
*      They are primarily to facilitate encoding SEQUENCE OF INTEGER as
*         OCTET STRING for ASN.1
*
****************************************************************************/

NLM_EXTERN Nlm_Uint2 LIBCALL Nlm_BSGetUint2 (Nlm_ByteStorePtr bsp)
{
	Nlm_Uint2 retval = 0;

	Nlm_BSRead(bsp, (Nlm_VoidPtr)(&retval), (Nlm_Int4)2);
	Nlm_SwapUint2(retval);

	return retval;
}

NLM_EXTERN Nlm_Uint4 LIBCALL Nlm_BSGetUint4 (Nlm_ByteStorePtr bsp)
{
	Nlm_Uint4 retval = 0;

	Nlm_BSRead(bsp, (Nlm_VoidPtr)(&retval), (Nlm_Int4)4);
	Nlm_SwapUint4(retval);

	return retval;
}

NLM_EXTERN Nlm_Int2 LIBCALL Nlm_BSPutUint2 (Nlm_ByteStorePtr bsp, Nlm_Uint2 value)
{
#ifdef IS_LITTLE_ENDIAN
	Nlm_SwitchUint2(value);
#endif
	if (Nlm_BSWrite(bsp, (Nlm_VoidPtr)(&value), (Nlm_Int4)2) == (Nlm_Int2)2)
		return (Nlm_Int2)1;
	else
		return (Nlm_Int2)0;
}

NLM_EXTERN Nlm_Int2 LIBCALL Nlm_BSPutUint4 (Nlm_ByteStorePtr bsp, Nlm_Uint4 value)
{
#ifdef IS_LITTLE_ENDIAN
	Nlm_SwitchUint4(value);
#endif
	if (Nlm_BSWrite(bsp, (Nlm_VoidPtr)(&value), (Nlm_Int4)4) == (Nlm_Int2)4)
		return (Nlm_Int2)1;
	else
		return (Nlm_Int2)0;
}

       /* In functions below, size is 2 or 4 */
       /* Integers are converted from ByteStore to native endian (BSUintXRead) */
	   /*   or from native endian to ByteStore (BSUintXWrite) */
	   /* len is number of INTEGERS, not number of bytes */
	   /* returns count of integers put in ptr */
	   /* WARNING: On LITTLE_ENDIAN machines the data in ptr is changed to BIG_ENDIAN in the */
	   /*   XXWrite functions and LEFT THAT WAY */
NLM_EXTERN Nlm_Int4 LIBCALL Nlm_BSUint4Read (Nlm_ByteStorePtr bsp, Nlm_Uint4Ptr ptr, Nlm_Int4 len)
{
	Nlm_Int4 len2;

	len2 = Nlm_BSRead(bsp, (Nlm_VoidPtr)ptr, (len * 4));
	len2 /= 4;
	Nlm_SwapUint4Buff(ptr, len2);
	return len2;
}

NLM_EXTERN Nlm_Int4 LIBCALL Nlm_BSUint4Write (Nlm_ByteStorePtr bsp, Nlm_Uint4Ptr ptr, Nlm_Int4 len)
{
	Nlm_Int4 len2;

#ifdef IS_LITTLE_ENDIAN
	Nlm_SwitchUint4Buff(ptr, len);
#endif
	len2 = Nlm_BSWrite(bsp, (Nlm_VoidPtr)ptr, (len * 4));
	return (len2 / 4);
}

NLM_EXTERN Nlm_Int4 LIBCALL Nlm_BSUint2Read (Nlm_ByteStorePtr bsp, Nlm_Uint2Ptr ptr, Nlm_Int4 len)
{
	Nlm_Int4 len2;

	len2 = Nlm_BSRead(bsp, (Nlm_VoidPtr)ptr, (len * 2));
	len2 /= 2;
	Nlm_SwapUint2Buff(ptr, len2);
	return len2;
}

NLM_EXTERN Nlm_Int4 LIBCALL Nlm_BSUint2Write (Nlm_ByteStorePtr bsp, Nlm_Uint2Ptr ptr, Nlm_Int4 len)
{
	Nlm_Int4 len2;

#ifdef IS_LITTLE_ENDIAN
	Nlm_SwitchUint2Buff(ptr, len);
#endif
	len2 = Nlm_BSWrite(bsp, (Nlm_VoidPtr)ptr, (len * 2));
	return (len2 / 2);
}