File: ms-biff.c

package info (click to toggle)
gnumeric 1.10.8-1squeeze5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 90,968 kB
  • ctags: 23,303
  • sloc: ansic: 248,235; xml: 51,894; sh: 10,491; makefile: 2,822; perl: 2,466; yacc: 1,272; python: 205
file content (796 lines) | stat: -rw-r--r-- 19,014 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
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/**
 * ms-biff.c: MS Excel Biff support...
 *
 * Author:
 *    Jody Goldberg (jody@gnome.org)
 *    Michael Meeks (michael@ximian.com)
 *
 * (C) 1998-2001 Michael Meeks
 * (C) 2002-2005 Jody Goldberg
 **/

#include <gnumeric-config.h>
#include <gnumeric.h>
#include <string.h>

#include "ms-biff.h"
#include "biff-types.h"
#include "ms-excel-util.h"
#include "md5.h"

#include <gsf/gsf-input.h>
#include <gsf/gsf-output.h>
#include <gsf/gsf-utils.h>
#include <gsf/gsf-msole-utils.h>
#include <goffice/goffice.h>

#define BIFF_DEBUG 0
#define sizeof_BIFF_8_FILEPASS	 (6 + 3*16)
#define sizeof_BIFF_2_7_FILEPASS  4

/**
 * The only complicated bits in this code are:
 *  a) Speed optimisation of passing a raw pointer to the mapped stream back
 *  b) Handling decryption
 **/

/*******************************************************************************/
/*                             Helper Functions                                */
/*******************************************************************************/

static void
destroy_sensitive (void *p, size_t len)
{
	if (len > 0) {
		memset (p, 0, len);
		memset (p, 0xaa, len - 1);
		go_destroy_password (p);
	}
}

void
ms_biff_query_dump (BiffQuery *q)
{
	g_print ("Opcode 0x%x length %d malloced? %d\nData:\n", q->opcode, q->length, q->data_malloced);
	if (q->length > 0)
		gsf_mem_dump (q->data, q->length);
/*	dump_stream (q->output); */
}

guint32
ms_biff_query_bound_check (BiffQuery *q, guint32 offset, unsigned len)
{
	if (offset >= q->length) {
		guint16 opcode;

		offset -= q->length;
		if (!ms_biff_query_peek_next (q, &opcode) ||
		    opcode != BIFF_CONTINUE ||
		    !ms_biff_query_next (q)) {
			g_warning ("missing CONTINUE");
			return (guint32)-1;
		}
	}

	if ((offset + len) > q->length) {
		g_warning ("supposedly atomic item of len %u sst spans CONTINUEs, we are screwed", len);
	}
	return offset;
}

/*******************************************************************************/
/*                                 Read Side                                   */
/*******************************************************************************/

/**
 *  ms_biff_password_hash and ms_biff_crypt_seq
 * based on pseudo-code in the OpenOffice.org XL documentation
 **/
static guint16
ms_biff_password_hash  (guint8 const *password)
{
	int tmp, index = 0, len = strlen ((char const *)password);
	guint16 chr, hash= 0;

	do {
		chr = password[index];
		index++;
		tmp = (chr << index);
		hash ^= (tmp & 0x7fff) | (tmp >> 15);
	} while (index < len);
	hash = hash ^ len ^ 0xce4b;

	return hash;
}

static void
ms_biff_crypt_seq (BiffQuery *q, guint16 key, guint8 const *password)
{
	static guint8 const preset [] = {
		0xbb, 0xff, 0xff, 0xba, 0xff, 0xff, 0xb9, 0x80,
		0x00, 0xbe, 0x0f, 0x00, 0xbf, 0x0f, 0x00, 0x00
	};
	guint8 const low  =        key & 0xff;
	guint8 const high = (key >> 8) & 0xff;
	unsigned i, len = strlen ((char const*)password);
	guint8 *seq = q->xor_key;

	strncpy (seq, password, 16);
	for (i = 0; (len + i) < 16; i++)
		seq[len + i] = preset[i];

	for (i = 0; i < 16; i += 2) {
		seq[i]   ^= low;
		seq[i+1] ^= high;
	}

	for (i = 0; i < 16; i++)
		seq[i] = (seq[i] << 2) | (seq[i] >> 6);
}

static gboolean
ms_biff_pre_biff8_query_set_decrypt  (BiffQuery *q, guint8 const *password)
{
	guint16 hash, key;
	guint16 pw_hash = ms_biff_password_hash (password);


	if (q->length == 4) {
		key = GSF_LE_GET_GUINT16(q->data + 0);
		hash = GSF_LE_GET_GUINT16(q->data + 2);
	} else if (q->length == 6) {
		/* BIFF8 record with pre-biff8 crypto, these do exist */
		key = GSF_LE_GET_GUINT16(q->data + 2);
		hash = GSF_LE_GET_GUINT16(q->data + 4);
	} else {
		return FALSE;
	}

	if (hash != pw_hash)
		return FALSE;

	ms_biff_crypt_seq (q, key, password);

	q->encryption = MS_BIFF_CRYPTO_XOR;
	return TRUE;
}

static void
makekey (guint32 block, RC4_KEY *key, const unsigned char *valDigest)
{
	struct md5_ctx ctx;
	unsigned char digest[16];
	guint8 pwarray[64];

	memset (pwarray, 0, 64);

	/* 40 bit of hashed password, set by verify_password () */
	memcpy (pwarray, valDigest, 5);

	/* put block number in byte 6...9 */
	pwarray[5] = (guint8) (block & 0xFF);
	pwarray[6] = (guint8) ((block >> 8) & 0xFF);
	pwarray[7] = (guint8) ((block >> 16) & 0xFF);
	pwarray[8] = (guint8) ((block >> 24) & 0xFF);

	pwarray[9] = 0x80;
	pwarray[56] = 0x48;

	md5_init_ctx (&ctx);
	md5_process_block (pwarray, 64, &ctx);
	md5_read_ctx (&ctx, digest);
	prepare_key (digest, 16, key);

	destroy_sensitive (&ctx, sizeof (ctx));
	destroy_sensitive (digest, sizeof (digest));
	destroy_sensitive (pwarray, sizeof (pwarray));
}

/**
 * verify_password :
 *
 * convert UTF-8-password into UTF-16
 */
static gboolean
verify_password (guint8 const *password, guint8 const *docid,
		 guint8 const *salt_data, guint8 const *hashedsalt_data,
		 unsigned char *valDigest)
{
	guint8 pwarray [64], salt [64], hashedsalt [16];
	struct md5_ctx mdContext;
	unsigned char digest[16];
	RC4_KEY key;
	int offset, keyoffset, i;
	unsigned int tocopy;
	gboolean res;
	gunichar2 *utf16 = g_utf8_to_utf16 (password, -1, NULL, NULL, NULL);

	/* we had better receive valid UTF-8 */
	g_return_val_if_fail (utf16 != NULL, FALSE);

	/* Be careful about endianness */
	memset (pwarray, 0, sizeof (pwarray));
	for (i = 0 ; utf16[i] ; i++) {
		pwarray[(2 * i) + 0] = ((utf16 [i] >> 0) & 0xff);
		pwarray[(2 * i) + 1] = ((utf16 [i] >> 8) & 0xff);
	}
	g_free (utf16);

	pwarray[2 * i] = 0x80;
	pwarray[56] = (i << 4);

	md5_init_ctx (&mdContext);
	md5_process_block (pwarray, 64, &mdContext);
	md5_read_ctx (&mdContext, digest);

	offset = 0;
	keyoffset = 0;
	tocopy = 5;

	md5_init_ctx (&mdContext);
	while (offset != 16) {
		if ((64 - offset) < 5)
			tocopy = 64 - offset;

		memcpy (pwarray + offset, digest + keyoffset, tocopy);
		offset += tocopy;

		if (offset == 64) {
			md5_process_block (pwarray, 64, &mdContext);
			keyoffset = tocopy;
			tocopy = 5 - tocopy;
			offset = 0;
			continue;
		}

		keyoffset = 0;
		tocopy = 5;
		memcpy (pwarray + offset, docid, 16);
		offset += 16;
	}

	/* Fix (zero) all but first 16 bytes */
	pwarray[16] = 0x80;
	memset (pwarray + 17, 0, 47);
	pwarray[56] = 0x80;
	pwarray[57] = 0x0A;

	md5_process_block (pwarray, 64, &mdContext);
	md5_read_ctx (&mdContext, valDigest);

	/* Generate 40-bit RC4 key from 128-bit hashed password */
	makekey (0, &key, valDigest);

	memcpy (salt, salt_data, 16);
	rc4 (salt, 16, &key);
	memcpy (hashedsalt, hashedsalt_data, 16);
	rc4 (hashedsalt, 16, &key);

	salt[16] = 0x80;
	memset (salt + 17, 0, 47);
	salt[56] = 0x80;

	md5_init_ctx (&mdContext);
	md5_process_block (salt, 64, &mdContext);
	md5_read_ctx (&mdContext, digest);

	res = memcmp (digest, hashedsalt, 16) == 0;

	destroy_sensitive (pwarray, sizeof (pwarray));
	destroy_sensitive (salt, sizeof (salt));
	destroy_sensitive (hashedsalt, sizeof (hashedsalt));
	destroy_sensitive (&mdContext, sizeof (mdContext));
	destroy_sensitive (digest, sizeof (digest));
	destroy_sensitive (&key, sizeof (key));

	return res;
}

#define REKEY_BLOCK 0x400
static void
skip_bytes (BiffQuery *q, int start, int count)
{
	static guint8 scratch[REKEY_BLOCK];
	int block;

	block = (start + count) / REKEY_BLOCK;

	if (block != q->block) {
		makekey (q->block = block, &q->rc4_key, q->md5_digest);
		count = (start + count) % REKEY_BLOCK;
	}

	g_assert (count <= REKEY_BLOCK);
	rc4 (scratch, count, &q->rc4_key);
}

/**
 * ms_biff_query_set_decrypt :
 * @q :
 * @password : password in UTF-8 encoding.
 **/
gboolean
ms_biff_query_set_decrypt (BiffQuery *q, MsBiffVersion version,
			   guint8 const *password)
{
	g_return_val_if_fail (q->opcode == BIFF_FILEPASS, FALSE);

	if (password == NULL)
		return FALSE;

	if (version < MS_BIFF_V8 || q->length == 0 || q->data[0] == 0)
		return ms_biff_pre_biff8_query_set_decrypt (q, password);

	XL_CHECK_CONDITION_VAL (q->length == sizeof_BIFF_8_FILEPASS, FALSE);

	if (!verify_password (password, q->data + 6,
			      q->data + 22, q->data + 38, q->md5_digest))
		return FALSE;

	q->encryption = MS_BIFF_CRYPTO_RC4;
	q->block = -1;

	/* For some reaons the 1st record after FILEPASS seems to be unencrypted */
	q->dont_decrypt_next_record = TRUE;

	/* pretend to decrypt the entire stream up till this point, it was not
	 * encrypted, but do it anyway to keep the rc4 state in sync
	 */
	skip_bytes (q, 0, gsf_input_tell (q->input));

	return TRUE;
}
void
ms_biff_query_copy_decrypt (BiffQuery *dst, BiffQuery const *src)
{
	g_return_if_fail (dst != NULL);
	g_return_if_fail (src != NULL);

#warning FINISH this
	switch (src->encryption) {
	default :
	case MS_BIFF_CRYPTO_NONE:
		XL_CHECK_CONDITION (dst->encryption == MS_BIFF_CRYPTO_NONE);
		break;
	case MS_BIFF_CRYPTO_XOR :
		break;
	case MS_BIFF_CRYPTO_RC4 :
		break;
	}
}

BiffQuery *
ms_biff_query_new (GsfInput *input)
{
	BiffQuery *q;

	g_return_val_if_fail (input != NULL, NULL);

	q = g_new0 (BiffQuery, 1);
	q->opcode        = 0;
	q->length        = 0;
	q->data_malloced = q->non_decrypted_data_malloced = FALSE;
	q->data		 = q->non_decrypted_data = NULL;
	q->input         = input;
	q->encryption    = MS_BIFF_CRYPTO_NONE;

#if BIFF_DEBUG > 0
	ms_biff_query_dump (q);
#endif
	return q;
}

gboolean
ms_biff_query_peek_next (BiffQuery *q, guint16 *opcode)
{
	guint8 const *data;
	guint16 len;

	g_return_val_if_fail (opcode != NULL, FALSE);
	g_return_val_if_fail (q != NULL, FALSE);

	data = gsf_input_read (q->input, 4, NULL);
	if (data == NULL)
		return FALSE;
	*opcode = GSF_LE_GET_GUINT16 (data);
	len = GSF_LE_GET_GUINT16 (data + 2);
	gsf_input_seek (q->input, -4, G_SEEK_CUR);

	return gsf_input_remaining (q->input) >= 4 + len;
}

/**
 * Returns FALSE if has hit end
 **/
gboolean
ms_biff_query_next (BiffQuery *q)
{
	guint8 const *data;
	guint16 len;

	g_return_val_if_fail (q != NULL, FALSE);

	if (gsf_input_eof (q->input))
		return FALSE;

	if (q->data_malloced) {
		g_free (q->data);
		q->data = NULL;
		q->data_malloced = FALSE;
	}
	if (q->non_decrypted_data_malloced) {
		g_free (q->non_decrypted_data);
		q->non_decrypted_data = NULL;
		q->non_decrypted_data_malloced = FALSE;
	}

	q->streamPos = gsf_input_tell (q->input);
	data = gsf_input_read (q->input, 4, NULL);
	if (data == NULL)
		return FALSE;
	q->opcode = GSF_LE_GET_GUINT16 (data);
	len = GSF_LE_GET_GUINT16 (data + 2);

	q->data   = NULL;
	q->length = 0;

	/* no biff record should be larger than around 20,000 */
	XL_CHECK_CONDITION_VAL (len < 20000, FALSE);

	if (len > 0) {
		q->data = (guint8 *)gsf_input_read (q->input, len, NULL);
		if (q->data == NULL)
			return FALSE;
	}
	q->length = len;

	if (q->encryption == MS_BIFF_CRYPTO_RC4) {
		q->non_decrypted_data_malloced = q->data_malloced;
		q->non_decrypted_data = q->data;

		q->data_malloced = TRUE;
		q->data = g_new (guint8, q->length);
		memcpy (q->data, q->non_decrypted_data, q->length);

		if (q->dont_decrypt_next_record) {
			skip_bytes (q, q->streamPos, 4 + q->length);
			q->dont_decrypt_next_record = FALSE;
		} else {
			int pos = q->streamPos;
			guint8 *data = q->data;
			int len = q->length;

			/* pretend to decrypt header */
			skip_bytes (q, pos, 4);
			pos += 4;

			while (q->block != (pos + len) / REKEY_BLOCK) {
				int step = REKEY_BLOCK - (pos % REKEY_BLOCK);
				rc4 (data, step, &q->rc4_key);
				data += step;
				pos += step;
				len -= step;
				makekey (++q->block, &q->rc4_key, q->md5_digest);
			}

			rc4 (data, len, &q->rc4_key);
		}
	} else if (q->encryption == MS_BIFF_CRYPTO_XOR) {
		unsigned int offset, k;

		q->non_decrypted_data_malloced = q->data_malloced;
		q->non_decrypted_data = q->data;
		q->data_malloced = TRUE;
		q->data = g_new (guint8, q->length);
		memcpy (q->data, q->non_decrypted_data, q->length);

		offset = (q->streamPos + q->length + 4) % 16;
		for (k= 0; k < q->length; ++k) {
			guint8 tmp = (q->data[k] << 3) | (q->data[k] >> 5);
			q->data[k] = tmp ^ q->xor_key[offset];
			offset = (offset + 1) % 16;
		}
	} else
		q->non_decrypted_data = q->data;

#if BIFF_DEBUG > 2
	printf ("Biff read code 0x%x, length %d\n", q->opcode, q->length);
	ms_biff_query_dump (q);
#endif
	return TRUE;
}

void
ms_biff_query_destroy (BiffQuery *q)
{
	if (q) {
		if (q->data_malloced) {
			g_free (q->data);
			q->data = NULL;
			q->data_malloced = FALSE;
		}
		if (q->non_decrypted_data_malloced) {
			g_free (q->non_decrypted_data);
			q->non_decrypted_data = NULL;
			q->non_decrypted_data_malloced = FALSE;
		}

		/* Paranoia: */
		destroy_sensitive (q, sizeof (*q));

		g_free (q);
	}
}

/*******************************************************************************/
/*                                 Write Side                                  */
/*******************************************************************************/

#define MAX_BIFF7_RECORD_SIZE 0x820
#define MAX_BIFF8_RECORD_SIZE 0x2020

/**
 * ms_biff_put_new :
 * @output   : the output storage
 * @version  :
 * @codepage : Ignored if negative
 *
 * Take responsibility for @output
 * and prepare to generate biff records.
 **/
BiffPut *
ms_biff_put_new (GsfOutput *output, MsBiffVersion version, int codepage)
{
	BiffPut *bp;

	g_return_val_if_fail (output != NULL, NULL);

	bp = g_new (BiffPut, 1);

	bp->opcode        = 0;
	bp->length        = 0;
	bp->length        = 0;
	bp->streamPos     = gsf_output_tell (output);
	bp->data_malloced = FALSE;
	bp->data          = NULL;
	bp->len_fixed     = -1;
	bp->output        = output;
	bp->version       = version;

	bp->buf_len = 2048;	/* maximum size for a biff7 record */
	bp->buf = g_malloc (bp->buf_len);

	if (version >= MS_BIFF_V8) {
		bp->convert = g_iconv_open ("UTF-16LE", "UTF-8");
		bp->codepage = 1200;
	} else {
		bp->codepage = (codepage > 0)
			? codepage : gsf_msole_iconv_win_codepage ();
		bp->convert = gsf_msole_iconv_open_codepage_for_export (bp->codepage);
	}

	return bp;
}

void
ms_biff_put_destroy (BiffPut *bp)
{
	g_return_if_fail (bp != NULL);
	g_return_if_fail (bp->output != NULL);

	if (bp->output != NULL) {
		gsf_output_close (bp->output);
		g_object_unref (G_OBJECT (bp->output));
		bp->output = NULL;
	}
	g_free (bp->buf);
	bp->buf = NULL;
	bp->buf_len = 0;

	gsf_iconv_close (bp->convert);
	bp->convert = NULL;

	g_free (bp);
}

guint8 *
ms_biff_put_len_next (BiffPut *bp, guint16 opcode, guint32 len)
{
	g_return_val_if_fail (bp, NULL);
	g_return_val_if_fail (bp->output, NULL);
	g_return_val_if_fail (bp->data == NULL, NULL);
	g_return_val_if_fail (bp->len_fixed == -1, NULL);

	if (bp->version >= MS_BIFF_V8)
		XL_CHECK_CONDITION_VAL (len < MAX_BIFF8_RECORD_SIZE, NULL);
	else
		XL_CHECK_CONDITION_VAL (len < MAX_BIFF7_RECORD_SIZE, NULL);

#if BIFF_DEBUG > 0
	printf ("Biff put len 0x%x\n", opcode);
#endif

	bp->len_fixed  = +1;
	bp->opcode     = opcode;
	bp->length     = len;
	bp->streamPos  = gsf_output_tell (bp->output);
	if (len > 0) {
		bp->data = g_new (guint8, len);
		bp->data_malloced = TRUE;
	}

	return bp->data;
}

void
ms_biff_put_var_next (BiffPut *bp, guint16 opcode)
{
	guint8 data[4];
	g_return_if_fail (bp != NULL);
	g_return_if_fail (bp->output != NULL);
	g_return_if_fail (bp->len_fixed == -1);

#if BIFF_DEBUG > 0
	printf ("Biff put var 0x%x\n", opcode);
#endif

	bp->len_fixed  = 0;
	bp->opcode     = opcode;
	bp->curpos     = 0;
	bp->length     = 0;
	bp->data       = NULL;
	bp->streamPos  = gsf_output_tell (bp->output);

	GSF_LE_SET_GUINT16 (data,    opcode);
	GSF_LE_SET_GUINT16 (data + 2,0xfaff); /* To be corrected later */
	gsf_output_write (bp->output, 4, data);
}

inline unsigned
ms_biff_max_record_len (BiffPut const *bp)
{
	return (bp->version >= MS_BIFF_V8) ? MAX_BIFF8_RECORD_SIZE : MAX_BIFF7_RECORD_SIZE;
}

void
ms_biff_put_var_write (BiffPut *bp, guint8 const *data, guint32 len)
{
	g_return_if_fail (bp != NULL);
	g_return_if_fail (data != NULL);
	g_return_if_fail (bp->output != NULL);

	g_return_if_fail (!bp->data);
	g_return_if_fail (bp->len_fixed == 0);

	/* Temporary */
	XL_CHECK_CONDITION (bp->length + len < 0xf000);

	if ((bp->curpos + len) > ms_biff_max_record_len (bp)) {

		g_return_if_fail (bp->curpos == bp->length);

		ms_biff_put_commit (bp);
		ms_biff_put_var_next (bp, BIFF_CONTINUE);
	}

	gsf_output_write (bp->output, len, data);
	bp->curpos += len;
	if (bp->curpos > bp->length)
		bp->length = bp->curpos;
}

void
ms_biff_put_var_seekto (BiffPut *bp, int pos)
{
	g_return_if_fail (bp != NULL);
	g_return_if_fail (bp->output != NULL);

	g_return_if_fail (bp->len_fixed == 0);
	g_return_if_fail (!bp->data);

	bp->curpos = pos;
	gsf_output_seek (bp->output, bp->streamPos + bp->curpos + 4, G_SEEK_SET);
}

static void
ms_biff_put_var_commit (BiffPut *bp)
{
	guint8 tmp [4];
	int endpos;

	g_return_if_fail (bp != NULL);
	g_return_if_fail (bp->output != NULL);

	g_return_if_fail (bp->len_fixed == 0);
	g_return_if_fail (!bp->data);

	endpos = bp->streamPos + bp->length + 4;
	gsf_output_seek (bp->output, bp->streamPos, G_SEEK_SET);

	GSF_LE_SET_GUINT16 (tmp, bp->opcode);
	GSF_LE_SET_GUINT16 (tmp+2, bp->length);
	gsf_output_write (bp->output, 4, tmp);

	gsf_output_seek (bp->output, endpos, G_SEEK_SET);
	bp->streamPos  = endpos;
	bp->curpos     = 0;
}

static void
ms_biff_put_len_commit (BiffPut *bp)
{
	guint8 tmp[4];

	g_return_if_fail (bp != NULL);
	g_return_if_fail (bp->output != NULL);
	g_return_if_fail (bp->len_fixed == 1);
	g_return_if_fail (bp->length == 0 || bp->data);
	if (bp->version >= MS_BIFF_V8)
		XL_CHECK_CONDITION (bp->length < MAX_BIFF8_RECORD_SIZE);
	else
		XL_CHECK_CONDITION (bp->length < MAX_BIFF7_RECORD_SIZE);

/*	if (!bp->data_malloced) Unimplemented optimisation
		bp->output->lseek (bp->output, bp->length, G_SEEK_CUR);
		else */
	GSF_LE_SET_GUINT16 (tmp, bp->opcode);
	GSF_LE_SET_GUINT16 (tmp + 2, bp->length);
	gsf_output_write (bp->output, 4, tmp);
	gsf_output_write (bp->output, bp->length, bp->data);

	g_free (bp->data);
	bp->data      = NULL;
	bp->data_malloced = FALSE;
	bp->streamPos = gsf_output_tell (bp->output);
	bp->curpos    = 0;
}

void
ms_biff_put_commit (BiffPut *bp)
{
	switch (bp->len_fixed) {
	case 0:
		ms_biff_put_var_commit (bp);
		break;
	case 1:
		ms_biff_put_len_commit (bp);
		break;
	default:
		g_warning ("Spurious commit");
	}
	bp->len_fixed = -1;

	if (0) {
		/*
		 * This is useful to figure out who writes uninitialized
		 * memory to files.  It causes layers below to flush.
		 */
		gsf_output_seek (bp->output, -1, G_SEEK_CUR);
		gsf_output_seek (bp->output, +1, G_SEEK_CUR);
	}
}

void
ms_biff_put_empty (BiffPut *bp, guint16 opcode)
{
	ms_biff_put_len_next (bp, opcode, 0);
	ms_biff_put_commit (bp);
}

void
ms_biff_put_2byte (BiffPut *bp, guint16 opcode, guint16 content)
{
	guint8 *data = ms_biff_put_len_next (bp, opcode, 2);
	GSF_LE_SET_GUINT16 (data, content);
	ms_biff_put_commit (bp);
}

void
ms_biff_put_abs_write (BiffPut *bp, gsf_off_t pos, gconstpointer buf, gsize size)
{
	gsf_off_t old = gsf_output_tell (bp->output);
	gsf_output_seek (bp->output, pos, G_SEEK_SET);
	gsf_output_write (bp->output, size, buf);
	gsf_output_seek (bp->output, old, G_SEEK_SET);
}