File: bei.c

package info (click to toggle)
ion 3.2.1%2Bdfsg-1.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 23,768 kB
  • ctags: 11,049
  • sloc: ansic: 141,798; sh: 22,848; makefile: 7,818; python: 1,638; sql: 311; perl: 197; awk: 178; xml: 50; java: 19
file content (1211 lines) | stat: -rw-r--r-- 27,268 bytes parent folder | download | duplicates (2)
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
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
/******************************************************************************
 	bei.c:	Implementation of functions that manipulate extension blocks
 	        within bundles and acquisition structures.

	Author: Scott Burleigh, JPL

	Modification History:
	Date      Who   What

	Copyright (c) 2010, California Institute of Technology.
	ALL RIGHTS RESERVED.  U.S. Government Sponsorship acknowledged.
 ******************************************************************************/

/*****************************************************************************
 **
 ** File Name: bei.c
 **
 ** Subsystem: BP
 **
 ** Description: This file provides function implementations used to find and
 **              manipulate extension blocks within bundles and acquisition
 **              structures.
 **
 ** Assumptions:
 **      1.
 **
 ** Modification History:
 **
 **  MM/DD/YY  AUTHOR        IONWG#    DESCRIPTION
 **  --------  ------------  -------  ---------------------------------------
 **  Original  S. Burleigh            Initial Implementation
 **  04/15/10  E. Birrane     105     Built bei.c file libbpP.c
 *****************************************************************************/

#include "bpP.h"
#include "bei.h"

/******************************************************************************
 *               OPERATIONS ON THE EXTENSION DEFINITIONS ARRAY                *
 ******************************************************************************/

void	getExtensionDefs(ExtensionDef **array, int *count)
{
#ifdef BP_EXTENDED
#include "bpextensions.c"
#else
#include "noextensions.c"
#endif
	static int	extensionsCt = sizeof extensions / sizeof(ExtensionDef);

	*array = extensions;
	*count = extensionsCt;
}

static unsigned int	getExtensionRank(ExtensionDef *def)
{
	ExtensionDef	*extensions;
	int		extensionsCt;

	getExtensionDefs(&extensions, &extensionsCt);
	return ((unsigned long) def - (unsigned long) extensions)
			/ sizeof(ExtensionDef);
}

ExtensionDef	*findExtensionDef(unsigned char type, unsigned char idx)
{
	ExtensionDef	*extensions;
	int		extensionsCt;
	int		i;
	ExtensionDef	*def;

	if (type == 0) return NULL;
	getExtensionDefs(&extensions, &extensionsCt);
	for (i = 0, def = extensions; i < extensionsCt; i++, def++)
	{
		if (def->type == type && def->listIdx == idx)
		{
			return def;
		}
	}

	return NULL;
}

/******************************************************************************
 *                               BUNDLE OPERATIONS                            *
 ******************************************************************************/

/**
 * Adds a collaboration block with the current identifier to the list of
 * collaboration blocks for the current bundle.
 */
int 	addCollaborationBlock(Bundle *bundle, CollabBlockHdr *blkHdr)
{
	/*	First, make sure we don't already have a collaboration block
	 *	with this identifier in the bundle...
	 */

	Sdr		bpSdr = getIonsdr();
	Object		addr;
	CollabBlockHdr	oldHdr;
	Object		newBlkAddr;

	CHKERR(bundle);
	CHKERR(blkHdr);
	addr = findCollaborationBlock(bundle, blkHdr->type, blkHdr->id);
	if (addr != 0)
	{
		sdr_read(bpSdr, (char *) &oldHdr, addr, sizeof(CollabBlockHdr));
		if (oldHdr.size != blkHdr->size)
		{
			putErrmsg("Collab block size mismatch.",
					itoa(oldHdr.size));
			return -1;
		}

		/*	Re-use existing collaboration block.		*/

		sdr_write(bpSdr, addr, (char *) blkHdr, blkHdr->size);
		return 0;
	}

	/*
	 * Otherwise, we are free to add this collaboration block to the bundle.
	 * Add to the end of the list of collab blocks. Order does not matter
	 * for collaboration blocks.
	 */

	newBlkAddr = sdr_malloc(bpSdr, blkHdr->size);
	if (newBlkAddr == 0)
	{
		putErrmsg("Can't acquire collaboration block.",
				itoa(blkHdr->size));
		return -1;
	}

	/* TODO: This may now be a redundant check. Consider removing. */

	if (bundle->collabBlocks == 0)
	{
		bundle->collabBlocks = sdr_list_create(bpSdr);
	}

	/* Insert the new block. */

	if (sdr_list_insert_last(bpSdr, bundle->collabBlocks, newBlkAddr) == 0)
	{
		putErrmsg("Failed inserting collab block.", NULL);
		return -1;
	}

	/* Create the stored collab block and write it to the SDR */

	sdr_write(bpSdr, newBlkAddr, (char *) blkHdr, blkHdr->size);
	return 0;
}

int	attachExtensionBlock(ExtensionDef *def, ExtensionBlock *blk,
		Bundle *bundle)
{
	Object	blkAddr;
	int	additionalOverhead;

	CHKERR(def);
	CHKERR(blk);
	CHKERR(bundle);
	blk->type = def->type;
	blkAddr = sdr_malloc(getIonsdr(), sizeof(ExtensionBlock));
	CHKERR(blkAddr);
	if (insertExtensionBlock(def, blk, blkAddr, bundle, def->listIdx) < 0)
	{
		putErrmsg("Failed attaching extension block.", NULL);
		return -1;
	}

	bundle->extensionsLength[def->listIdx] += blk->length;
	additionalOverhead = SDR_LIST_ELT_OVERHEAD + sizeof(ExtensionBlock)
			+ blk->length + blk->size;
	bundle->dbOverhead += additionalOverhead;
	return 0;
}

int 	copyCollaborationBlocks(Bundle *newBundle, Bundle *oldBundle)
{
	Sdr	bpSdr = getIonsdr();
	Object	elt;
	Object	blkAddr;
		OBJ_POINTER(CollabBlockHdr, blkHdr);

	CHKERR(newBundle);
	CHKERR(oldBundle);

	/* Destroy collaboration blocks that might be in the new bundle. */
	destroyCollaborationBlocks(newBundle);

	/* Copy the collaboration blocks. */
	newBundle->collabBlocks = sdr_list_create(bpSdr);
	CHKERR(newBundle->collabBlocks);

	/* For each collaboration block that we need to copy... */
	for (elt = sdr_list_first(bpSdr, oldBundle->collabBlocks); elt;
			elt = sdr_list_next(bpSdr, elt))
	{
		blkAddr = sdr_list_data(bpSdr, elt);
		GET_OBJ_POINTER(bpSdr, CollabBlockHdr, blkHdr, blkAddr);
		CHKERR(blkHdr);
		if (addCollaborationBlock(newBundle, blkHdr) < 0)
		{
			putErrmsg("Failed copying collaboration block.", NULL);
			return -1;
		}
	}

	return 0;
}

int	copyExtensionBlocks(Bundle *newBundle, Bundle *oldBundle)
{
	Sdr		bpSdr = getIonsdr();
	int		i;
	Object		elt;
	Object		blkAddr;
			OBJ_POINTER(ExtensionBlock, oldBlk);
	Object		newBlkAddr;
	ExtensionBlock	newBlk;
	char		*buf = NULL;
	unsigned int	buflen = 0;
	ExtensionDef	*def;

	CHKERR(newBundle);
	CHKERR(oldBundle);
	copyCollaborationBlocks(newBundle, oldBundle);
	for (i = 0; i < 2; i++)
	{
		newBundle->extensions[i] = sdr_list_create(bpSdr);
		CHKERR(newBundle->extensions[i]);
		if (oldBundle->extensions[i] == 0)
		{
			continue;
		}

		for (elt = sdr_list_first(bpSdr, oldBundle->extensions[i]); elt;
				elt = sdr_list_next(bpSdr, elt))
		{
			blkAddr = sdr_list_data(bpSdr, elt);
			GET_OBJ_POINTER(bpSdr, ExtensionBlock, oldBlk, blkAddr);
			if (oldBlk->length > buflen)
			{
				if (buf)
				{
					MRELEASE(buf);
				}

				buflen = oldBlk->length;
				buf = MTAKE(buflen);
				if (buf == NULL)
				{
					putErrmsg("Extension too big to copy.",
							itoa(buflen));
					return -1;
				}
			}

			memset((char *) &newBlk, 0, sizeof(ExtensionBlock));
			newBlk.type = oldBlk->type;
			newBlk.blkProcFlags = oldBlk->blkProcFlags;
			newBlk.dataLength = oldBlk->dataLength;
			if (oldBlk->eidReferences == 0)
			{
				newBlk.eidReferences = 0;
			}
			else
			{
				newBlk.eidReferences = sdr_list_create(bpSdr);
				CHKERR(newBlk.eidReferences);
				for (elt = sdr_list_first(bpSdr,
						oldBlk->eidReferences); elt;
						elt = sdr_list_next(bpSdr, elt))
				{
					if (sdr_list_insert_last(bpSdr,
						newBlk.eidReferences,
						sdr_list_data(bpSdr, elt)) == 0)
					{
						putErrmsg("No space for EIDR.",
								NULL);
						return -1;
					}
				}
			}

			newBlk.length = oldBlk->length;
			if (newBlk.length == 0)
			{
				newBlk.bytes = 0;
			}
			else
			{
				newBlk.bytes = sdr_malloc(bpSdr, newBlk.length);
				CHKERR(newBlk.bytes);
				sdr_read(bpSdr, buf, oldBlk->bytes,
						newBlk.length);
				sdr_write(bpSdr, newBlk.bytes, buf,
						newBlk.length);
			}

			def = findExtensionDef(oldBlk->type, i);
			if (def && def->copy)
			{
				/*	Must copy extension object.	*/

				if (def->copy(&newBlk, oldBlk) < 0)
				{
					putErrmsg("Can't copy extension obj.",
							utoa(oldBlk->size));
					return -1;
				}
			}
			else
			{
				newBlk.object = 0;
				newBlk.size = 0;
			}

			newBlkAddr = sdr_malloc(bpSdr, sizeof(ExtensionBlock));
			CHKERR(newBlkAddr);
			if (insertExtensionBlock(def, &newBlk, newBlkAddr,
					newBundle, i) < 0)
			{
				putErrmsg("Failed copying ext. block.", NULL);
				return -1;
			}
		}

		newBundle->extensionsLength[i] = oldBundle->extensionsLength[i];
	}

	if (buf)
	{
		MRELEASE(buf);
	}

	return 0;
}

void	deleteExtensionBlock(Object elt, unsigned int listIdx)
{
	Sdr		bpSdr = getIonsdr();
	Object		blkAddr;
			OBJ_POINTER(ExtensionBlock, blk);
	ExtensionDef	*def;

	CHKVOID(elt);
	blkAddr = sdr_list_data(bpSdr, elt);
	sdr_list_delete(bpSdr, elt, NULL, NULL);
	GET_OBJ_POINTER(bpSdr, ExtensionBlock, blk, blkAddr);
	def = findExtensionDef(blk->type, listIdx);
	if (def && def->release)
	{
		def->release(blk);
	}

	if (blk->eidReferences)
	{
		sdr_list_destroy(bpSdr, blk->eidReferences, NULL, NULL);
	}

	if (blk->bytes)
	{
		sdr_free(bpSdr, blk->bytes);
	}

	sdr_free(bpSdr, blkAddr);
}


void 	destroyCollaborationBlocks(Bundle *bundle)
{
	Object	elt;
	Object	blkAddr;
	Sdr	bpSdr = getIonsdr();

	CHKVOID(bundle);
	if (bundle->collabBlocks == 0)
	{
		return;
	}

	while (1)
	{
		elt = sdr_list_first(bpSdr, bundle->collabBlocks);
		if (elt == 0)
		{
			break;
		}

		blkAddr = sdr_list_data(bpSdr, elt);
		sdr_list_delete(bpSdr, elt, NULL, NULL);
		sdr_free(bpSdr, blkAddr);
	}

	sdr_list_destroy(bpSdr, bundle->collabBlocks, NULL, NULL);
	bundle->collabBlocks = 0;
}

void	destroyExtensionBlocks(Bundle *bundle)
{
	Sdr	bpSdr = getIonsdr();
	int	i;
	Object	elt;

	CHKVOID(bundle);
	for (i = 0; i < 2; i++)
	{
		if (bundle->extensions[i] == 0)
		{
			continue;
		}

		while (1)
		{
			elt = sdr_list_first(bpSdr, bundle->extensions[i]);
			if (elt == 0)
			{
				break;
			}

			deleteExtensionBlock(elt, i);
		}

		sdr_list_destroy(bpSdr, bundle->extensions[i], NULL, NULL);
	}
}

Object  findCollaborationBlock(Bundle *bundle, unsigned char type,
		unsigned int id)
{
	Sdr	bpSdr = getIonsdr();
	Object	elt;
	Object	addr;
		OBJ_POINTER(CollabBlockHdr, blkHdr);

	CHKZERO(bundle);
	for (elt = sdr_list_first(bpSdr, bundle->collabBlocks); elt;
			elt = sdr_list_next(bpSdr, elt))
	{
		addr = sdr_list_data(bpSdr, elt);
		GET_OBJ_POINTER(bpSdr, CollabBlockHdr, blkHdr, addr);
		if ((blkHdr->type == type) && (blkHdr->id == id))
		{
			return addr;
		}
	}

	return 0;
}

Object	findExtensionBlock(Bundle *bundle, unsigned int type, unsigned int idx)
{
	Sdr	bpSdr = getIonsdr();
	Object	elt;
	Object	addr;
		OBJ_POINTER(ExtensionBlock, blk);

	CHKZERO(bundle);
	for (elt = sdr_list_first(bpSdr, bundle->extensions[idx]); elt;
			elt = sdr_list_next(bpSdr, elt))
	{
		addr = sdr_list_data(bpSdr, elt);
		GET_OBJ_POINTER(bpSdr, ExtensionBlock, blk, addr);
		if (blk->type == type)
		{
			return elt;
		}
	}

	return 0;
}

int	insertExtensionBlock(ExtensionDef *def, ExtensionBlock *newBlk,
		Object blkAddr, Bundle *bundle, unsigned char listIdx)
{
	Sdr	bpSdr = getIonsdr();
	int	result;
	Object	elt;
		OBJ_POINTER(ExtensionBlock, blk);

	CHKERR(newBlk);
	CHKERR(bundle);
	if (def == NULL)	/*	Don't care where this goes.	*/
	{
		if (listIdx == 0)	/*	Pre-payload block.	*/
		{
			/*	Insert after all pre-payload blocks
			 *	inserted by the local node and after
			 *	all preceding pre-payload blocks
			 *	inserted by upstream nodes.		*/

			newBlk->rank = 255;
		}
		else			/*	Post-payload block.	*/
		{
			/*	Insert *before* all post-payload blocks
			 *	inserted by the local node and after
			 *	all preceding post-payload blocks
			 *	inserted by upstream nodes.		*/

			newBlk->rank = 0;
		}
	}
	else			/*	Order in list is important.	*/
	{
		newBlk->rank = getExtensionRank(def);
	}

	for (elt = sdr_list_first(bpSdr, bundle->extensions[listIdx]);
			elt; elt = sdr_list_next(bpSdr, elt))
	{
		GET_OBJ_POINTER(bpSdr, ExtensionBlock, blk,
				sdr_list_data(bpSdr, elt));
		if (blk->rank > newBlk->rank)
		{
			break;	/*	Found a higher-ranking block.	*/
		}
	}

	if (elt)	/*	Add before first higher-ranking block.	*/
	{
		result = sdr_list_insert_before(bpSdr, elt, blkAddr);
	}
	else		/*	There is no higher-ranking block.	*/
	{
		result = sdr_list_insert_last(bpSdr,
				bundle->extensions[listIdx], blkAddr);
	}

	if (result == 0)
	{
		putErrmsg("Failed inserting extension block.", NULL);
		return -1;
	}

	sdr_write(bpSdr, blkAddr, (char *) newBlk, sizeof(ExtensionBlock));
	return 0;
}

int	patchExtensionBlocks(Bundle *bundle)
{
	ExtensionDef	*extensions;
	int		extensionsCt;
	int		i;
	ExtensionDef	*def;
	ExtensionBlock	blk;

	CHKERR(bundle);
	getExtensionDefs(&extensions, &extensionsCt);
	for (i = 0, def = extensions; i < extensionsCt; i++, def++)
	{
		if (def->type != 0 && def->offer != NULL
		&& findExtensionBlock(bundle, def->type, def->listIdx) == 0)
		{
			/*	This is a type of extension block that
			 *	the local node normally offers, which
			 *	is missing from the received bundle.
			 *	Insert a block of this type if it is
			 *	appropriate.				*/

			memset((char *) &blk, 0, sizeof(ExtensionBlock));
			blk.type = def->type;
			if (def->offer(&blk, bundle) < 0)
			{
				putErrmsg("Failed offering patch block.",
						NULL);
				return -1;
			}

			if (blk.length == 0 && blk.size == 0)
			{
				continue;
			}

			if (attachExtensionBlock(def, &blk, bundle) < 0)
			{
				putErrmsg("Failed attaching patch block.",
						NULL);
				return -1;
			}
		}
	}

	return 0;
}

static void	adjustDbOverhead(Bundle *bundle, unsigned int oldLength,
			unsigned int newLength, unsigned int oldSize,
			unsigned int newSize)
{
	bundle->dbOverhead -= oldLength;
	bundle->dbOverhead += newLength;
	bundle->dbOverhead -= oldSize;
	bundle->dbOverhead += newSize;
}

int	processExtensionBlocks(Bundle *bundle, int fnIdx, void *context)
{
	Sdr			bpSdr = getIonsdr();
	int			oldDbOverhead;
	int			i;
	Object			elt;
	Object			nextElt;
	Object			blkAddr;
	ExtensionBlock		blk;
	ExtensionDef		*def;
	BpExtBlkProcessFn	processExtension;
	unsigned int		oldLength;
	unsigned int		oldSize;
	unsigned int		wasSuppressed;

	CHKERR(bundle);
	oldDbOverhead = bundle->dbOverhead;
	for (i = 0; i < 2; i++)
	{
		if (bundle->extensions[i] == 0)
		{
			continue;
		}

		for (elt = sdr_list_first(bpSdr, bundle->extensions[i]); elt;
				elt = nextElt)
		{
			nextElt = sdr_list_next(bpSdr, elt);
			blkAddr = sdr_list_data(bpSdr, elt);
			sdr_stage(bpSdr, (char *) &blk, blkAddr,
					sizeof(ExtensionBlock));
			def = findExtensionDef(blk.type, i);
			if (def == NULL
			|| (processExtension = def->process[fnIdx]) == NULL)
			{
				continue;
			}

			oldLength = blk.length;
			oldSize = blk.size;
			wasSuppressed = blk.suppressed;
			if (processExtension(&blk, bundle, context) < 0)
			{
				putErrmsg("Failed processing extension block.",
						def->name);
				return -1;
			}

			if (blk.length == 0)	/*	Scratched.	*/
			{
				bundle->extensionsLength[i] -= oldLength;
				adjustDbOverhead(bundle, oldLength, 0,
						oldSize, 0);
				deleteExtensionBlock(elt, i);
				continue;
			}

			/*	Revise aggregate extensions length as
			 *	necessary.				*/

			if (wasSuppressed)
			{
				if (!blk.suppressed)	/*	restore	*/
				{
					bundle->extensionsLength[i]
							+= blk.length;
				}

				/*	Still suppressed: no change.	*/
			}
			else	/*	Wasn't suppressed before.	*/
			{
				if (!blk.suppressed)
				{
					/*	Still not suppressed,
					 *	but length may have
					 *	changed.  Subtract the
					 *	old length and add the
					 *	new length.		*/

					bundle->extensionsLength[i]
							-= oldLength;
					bundle->extensionsLength[i]
							+= blk.length;
				}
				else	/*	Newly suppressed.	*/
				{
					bundle->extensionsLength[i]
							-= oldLength;
				}
			}

			if (blk.length != oldLength || blk.size != oldSize)
			{
				adjustDbOverhead(bundle, oldLength, blk.length,
						oldSize, blk.size);
			}

			sdr_write(bpSdr, blkAddr, (char *) &blk,
					sizeof(ExtensionBlock));
		}
	}

	if (bundle->dbOverhead != oldDbOverhead)
	{
		zco_reduce_heap_occupancy(bpSdr, oldDbOverhead);
		zco_increase_heap_occupancy(bpSdr, bundle->dbOverhead);
	}

	return 0;
}

int  	resizeCollaborationBlock(Bundle *bundle, Object addr, Object data)
{
	/* TODO: Implement. */
	return 0;
}

void	restoreExtensionBlock(ExtensionBlock *blk)
{
	CHKVOID(blk);
	blk->suppressed = 0;
}

void	scratchExtensionBlock(ExtensionBlock *blk)
{
	CHKVOID(blk);
	blk->length = 0;
}

int	serializeExtBlk(ExtensionBlock *blk, Lyst eidReferences,
		char *blockData)
{
	Sdr		bpSdr = getIonsdr();
	unsigned int	blkProcFlags;
	Sdnv		blkProcFlagsSdnv;
	unsigned int	dataLength;
	Sdnv		dataLengthSdnv;
	int		listLength;
	LystElt		elt;
	unsigned int	offset;
	Sdnv		offsetSdnv;
	unsigned int	referenceCount;
	Sdnv		referenceCountSdnv;
	char		*blkBuffer;
	char		*cursor;

	CHKERR(blk);
	blkProcFlags = blk->blkProcFlags;
	dataLength = blk->dataLength;
	if (blk->bytes)
	{
		sdr_free(bpSdr, blk->bytes);
	}

	blk->bytes = 0;
	if (blk->eidReferences)
	{
		sdr_list_destroy(bpSdr, blk->eidReferences, NULL, NULL);
	}

	blk->eidReferences = 0;
	encodeSdnv(&blkProcFlagsSdnv, blkProcFlags);
	encodeSdnv(&dataLengthSdnv, dataLength);
	blk->length = 1 + blkProcFlagsSdnv.length + dataLengthSdnv.length
			+ dataLength;
	if (eidReferences != NULL)
	{
		listLength = lyst_length(eidReferences);

		/*	Each reference is a pair of offsets, i.e., two
		 *	list elements.					*/

		if (listLength & 0x00000001)	/*	Not pairs.	*/
		{
			putErrmsg("Invalid EID references list.", NULL);
			return -1;
		}

		blkProcFlags |= BLK_HAS_EID_REFERENCES;
		referenceCount = (listLength >> 1) & 0x7fffffff;
		encodeSdnv(&referenceCountSdnv, referenceCount);
		blk->length += referenceCountSdnv.length;
		blk->eidReferences = sdr_list_create(bpSdr);
		CHKERR(blk->eidReferences);
		for (elt = lyst_first(eidReferences); elt; elt = lyst_next(elt))
		{
			offset = (unsigned long) lyst_data(elt);
			encodeSdnv(&offsetSdnv, offset);
			blk->length += offsetSdnv.length;
			oK(sdr_list_insert_last(bpSdr,
					blk->eidReferences, offset));
		}
	}

	blk->bytes = sdr_malloc(bpSdr, blk->length);
	if (blk->bytes == 0)
	{
		putErrmsg("No space for block.", itoa(blk->length));
		return -1;
	}

	blkBuffer = MTAKE(blk->length);
	if (blkBuffer == NULL)
	{
		putErrmsg("No space for block buffer.", itoa(blk->length));
		return -1;
	}

	*blkBuffer = blk->type;
	cursor = blkBuffer + 1;
	memcpy(cursor, blkProcFlagsSdnv.text, blkProcFlagsSdnv.length);
	cursor += blkProcFlagsSdnv.length;
	if (eidReferences)
	{
		memcpy(cursor, referenceCountSdnv.text,
				referenceCountSdnv.length);
		cursor += referenceCountSdnv.length;
		for (elt = lyst_first(eidReferences); elt; elt = lyst_next(elt))
		{
			offset = (unsigned long) lyst_data(elt);
			encodeSdnv(&offsetSdnv, offset);
			memcpy(cursor, offsetSdnv.text, offsetSdnv.length);
			cursor += offsetSdnv.length;
		}
	}

	memcpy(cursor, dataLengthSdnv.text, dataLengthSdnv.length);
	cursor += dataLengthSdnv.length;
	memcpy(cursor, blockData, dataLength);
	sdr_write(bpSdr, blk->bytes, blkBuffer, blk->length);
	MRELEASE(blkBuffer);
	return 0;
}

void	suppressExtensionBlock(ExtensionBlock *blk)
{
	CHKVOID(blk);
	blk->suppressed = 1;
}

int 	updateCollaborationBlock(Object collabAddr, CollabBlockHdr *blkHdr)
{
	Sdr bpSdr = getIonsdr();

	CHKERR(collabAddr);
	CHKERR(blkHdr);

	/* TODO: When and if resizing collaboration blocks are supported, must
	 *       check to be sure size did not change if writing collab back to
	 *       sdr.  If it did change, must try and reallocate SDR storage.
	 */

	sdr_write(bpSdr, collabAddr, (char *) blkHdr, blkHdr->size);
	return 0;
}

/******************************************************************************
 *                        ACQUISITION WORK-AREA OPERATIONS                    *
 ******************************************************************************/

int	acquireExtensionBlock(AcqWorkArea *work, ExtensionDef *def,
		unsigned char *startOfBlock, unsigned int blockLength,
		unsigned char blkType, unsigned int blkProcFlags,
		Lyst *eidReferences, unsigned int dataLength)
{
	Bundle		*bundle = &(work->bundle);
	int		blkSize;
	AcqExtBlock	*blk;
	Sdnv		blkProcFlagsSdnv;
	int		i;
	LystElt		elt;
	int		additionalOverhead;

	CHKERR(work);
	CHKERR(startOfBlock);
	CHKERR(eidReferences);
	blkSize = sizeof(AcqExtBlock) + (blockLength - 1);
	blk = (AcqExtBlock *) MTAKE(blkSize);
	if (blk == NULL)
	{
		putErrmsg("Can't acquire extension block.", itoa(blkSize));
		return -1;
	}

	/*	Populate the extension block structure.			*/

	memset((char *) blk, 0, sizeof(AcqExtBlock));
	blk->type = blkType;
	blk->blkProcFlags = blkProcFlags;
	blk->eidReferences = *eidReferences;
	*eidReferences = NULL;
	blk->dataLength = dataLength;
	blk->length = blockLength;
	memcpy(blk->bytes, startOfBlock, blockLength);

	/*	Block processing flags may already have been modified.	*/

	encodeSdnv(&blkProcFlagsSdnv, blkProcFlags);
	memcpy(blk->bytes + 1, blkProcFlagsSdnv.text, blkProcFlagsSdnv.length);

	/*	Store extension block within bundle.			*/

	i = work->currentExtBlocksList;
	elt = lyst_insert_last(work->extBlocks[i], blk);
	if (elt == NULL)
	{
		MRELEASE(blk);
		putErrmsg("Can't acquire extension block.", NULL);
		return -1;
	}

	/*	Block-specific processing.				*/

	if (def && def->acquire)
	{
		switch (def->acquire(blk, work))
		{
		case 0:		/*	Malformed block.		*/
			work->malformed = 1;
			break;

		case 1:		/*	No problem.			*/
			break;

		default:	/*	System failure.			*/
			lyst_delete(elt);
			MRELEASE(blk);
			putErrmsg("Can't accept extension block.",
					itoa(blkType));
			return -1;
		}

		if (blk->length == 0)	/*	Discarded.		*/
		{
			deleteAcqExtBlock(elt, i);
			return 0;
		}
	}

	/*	Note that this calculation of dbObverhead is tentative,
	 *	to aid in making acquisition decisions.  The real
	 *	calculation happens when the bundle is accepted and
	 *	the extension blocks are recorded.			*/

	bundle->extensionsLength[i] += blk->length;
	additionalOverhead = SDR_LIST_ELT_OVERHEAD + sizeof(ExtensionBlock)
			+ blk->length + blk->size;
	bundle->dbOverhead += additionalOverhead;
	return 0;
}

int 	addAcqCollabBlock(AcqWorkArea *work, CollabBlockHdr *blkHdr)
{
	LystElt		elt;
	char		*blk;

	CHKERR(work);
	CHKERR(blkHdr);
	elt = findAcqCollabBlock(work, blkHdr->type, blkHdr->id);
	if (elt != NULL)
	{
		putErrmsg("Collab block already exists in work area.", NULL);
		return -1;
	}

	blk = MTAKE(blkHdr->size);
	if (blk == NULL)
	{
		putErrmsg("Can't acquire collaboration block.",
				itoa(blkHdr->size));
		return -1;
	}

	/*	Populate the collaboration block structure.		*/
	memcpy(blk, (char *) blkHdr, blkHdr->size);

	/* TODO: This may be a redundant check.  Consider removing. */
	if (work->collabBlocks == 0)
	{
		work->collabBlocks = lyst_create_using(getIonMemoryMgr());
	}

	/*	Store extension block within bundle.			*/
	elt = lyst_insert_last(work->collabBlocks, blk);
	if (elt == NULL)
	{
		MRELEASE(blk);
		putErrmsg("Can't acquire collaboration block.",
				itoa(blkHdr->size));
		return -1;
	}

	return 0;
}

int	checkExtensionBlocks(AcqWorkArea *work)
{
	Bundle		*bundle = &(work->bundle);
	int		i;
	LystElt		elt;
	LystElt		nextElt;
	AcqExtBlock	*blk;
	ExtensionDef	*def;
	unsigned int	oldLength;

	CHKERR(work);
	bundle->clDossier.authentic = work->authentic;
	for (i = 0; i < 2; i++)
	{
		for (elt = lyst_first(work->extBlocks[i]); elt; elt = nextElt)
		{
			nextElt = lyst_next(elt);
			blk = (AcqExtBlock *) lyst_data(elt);
			def = findExtensionDef(blk->type, i);
			if (def == NULL || def->check == NULL)
			{
				continue;
			}

			oldLength = blk->length;
			switch (def->check(blk, work))
			{
			case 3:		/*	Bundle is corrupt.	*/
				bundle->corrupt = 1;
				break;

			case 2:		/*	Bundle is authentic.	*/
				bundle->clDossier.authentic = 1;
				break;

			case 1:		/*	Bundle is inauthentic.	*/
				bundle->clDossier.authentic = 0;
				break;

			case 0:		/*	No additional info.	*/
				break;

			default:
				putErrmsg("Failed checking extension block.",
						def->name);
				return -1;
			}

			if (blk->length == 0)	/*	Discarded.	*/
			{
				bundle->extensionsLength[i] -= oldLength;
				deleteAcqExtBlock(elt, i);
				continue;
			}

			/*	Revise aggregate extensions length as
			 *	necessary.				*/

			if (blk->length != oldLength)
			{
				bundle->extensionsLength[i] -= oldLength;
				bundle->extensionsLength[i] += blk->length;
			}
		}
	}

	return 0;
}

void	deleteAcqExtBlock(LystElt elt, unsigned int listIdx)
{
	AcqExtBlock	*blk;
	ExtensionDef	*def;

	CHKVOID(elt);
	blk = (AcqExtBlock *) lyst_data(elt);
	def = findExtensionDef(blk->type, listIdx);
	if (def && def->clear)
	{
		def->clear(blk);
	}

	if (blk->eidReferences)
	{
		lyst_destroy(blk->eidReferences);
	}

	MRELEASE(blk);
	lyst_delete(elt);
}

void 	destroyAcqCollabBlocks(AcqWorkArea *work)
{
	LystElt	elt;
	void	*blk = NULL;

	CHKVOID(work);
	while (1)
	{
		elt = lyst_first(work->collabBlocks);
		if (elt == NULL)
		{
			return;
		}

		blk = lyst_data(elt);
		if (blk != NULL)	/*	Should always be true.	*/
		{
			MRELEASE(blk);
		}

		lyst_delete(elt);
	}
}

void	discardExtensionBlock(AcqExtBlock *blk)
{
	CHKVOID(blk);
	blk->length = 0;
}

LystElt	findAcqCollabBlock(AcqWorkArea *work, unsigned char type,
		unsigned int id)
{
	LystElt		elt;
	CollabBlockHdr	*blkHdr;

	CHKNULL(work);
	for (elt = lyst_first(work->collabBlocks); elt; elt = lyst_next(elt))
	{
		blkHdr = (CollabBlockHdr *) lyst_data(elt);
		if (blkHdr == NULL)	/*	Should never happen.	*/
		{
			continue;
		}

		if ((blkHdr->type == type) && (blkHdr->id == id))
		{
			break;
		}
	}

	return elt;
}

int	recordExtensionBlocks(AcqWorkArea *work)
{
	Sdr		bpSdr = getIonsdr();
	Bundle		*bundle = &(work->bundle);
	int		i;
	LystElt		elt;
	AcqExtBlock	*oldBlk;
	ExtensionBlock	newBlk;
	int		headerLength;
	ExtensionDef	*def;
	Object		newBlkAddr;
	int		additionalOverhead;

	CHKERR(work);
	bundle->collabBlocks = sdr_list_create(bpSdr);
	CHKERR(bundle->collabBlocks);
	for (i = 0; i < 2; i++)
	{
		bundle->extensions[i] = sdr_list_create(bpSdr);
		CHKERR(bundle->extensions[i]);
		bundle->extensionsLength[i] = 0;
		for (elt = lyst_first(work->extBlocks[i]); elt;
				elt = lyst_next(elt))
		{
			oldBlk = (AcqExtBlock *) lyst_data(elt);
			memset((char *) &newBlk, 0, sizeof(ExtensionBlock));
			newBlk.type = oldBlk->type;
			newBlk.blkProcFlags = oldBlk->blkProcFlags;
			newBlk.dataLength = oldBlk->dataLength;
			headerLength = oldBlk->length - oldBlk->dataLength;
			if (serializeExtBlk(&newBlk, oldBlk->eidReferences,
				(char *) (oldBlk->bytes + headerLength)) < 0)
			{
				putErrmsg("No space for block.", NULL);
				return -1;
			}

			def = findExtensionDef(oldBlk->type, i);
			if (def && def->record)
			{
				/*	Record extension object.	*/

				def->record(&newBlk, oldBlk);
			}
			else
			{
				newBlk.object = 0;
			}

			newBlkAddr = sdr_malloc(bpSdr, sizeof(ExtensionBlock));
			CHKERR(newBlkAddr);
			if (insertExtensionBlock(def, &newBlk, newBlkAddr,
					bundle, i) < 0)
			{
				putErrmsg("Failed recording ext. block.", NULL);
				return -1;
			}

			bundle->extensionsLength[i] += newBlk.length;
			additionalOverhead = SDR_LIST_ELT_OVERHEAD
					+ sizeof(ExtensionBlock)
					+ newBlk.length + newBlk.size;
			bundle->dbOverhead += additionalOverhead;
		}
	}

	return 0;
}