Ejemplo n.º 1
0
FLAC__bool FLAC__add_metadata_block(const FLAC__StreamMetadata *metadata, FLAC__BitWriter *bw)
{
	unsigned i, j;
	const unsigned vendor_string_length = (unsigned)strlen(FLAC__VENDOR_STRING);

	if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->is_last, FLAC__STREAM_METADATA_IS_LAST_LEN))
		return false;

	if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->type, FLAC__STREAM_METADATA_TYPE_LEN))
		return false;

	/*
     * First, for VORBIS_COMMENTs, adjust the length to reflect our vendor string
     */
	i = metadata->length;
	if(metadata->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
		FLAC__ASSERT(metadata->data.vorbis_comment.vendor_string.length == 0 || 0 != metadata->data.vorbis_comment.vendor_string.entry);
		i -= metadata->data.vorbis_comment.vendor_string.length;
		i += vendor_string_length;
	}
	FLAC__ASSERT(i < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
	if(!FLAC__bitwriter_write_raw_uint32(bw, i, FLAC__STREAM_METADATA_LENGTH_LEN))
		return false;

	switch(metadata->type) {
		case FLAC__METADATA_TYPE_STREAMINFO:
			FLAC__ASSERT(metadata->data.stream_info.min_blocksize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN));
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.min_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN))
				return false;
			FLAC__ASSERT(metadata->data.stream_info.max_blocksize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN));
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.max_blocksize, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
				return false;
			FLAC__ASSERT(metadata->data.stream_info.min_framesize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN));
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.min_framesize, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
				return false;
			FLAC__ASSERT(metadata->data.stream_info.max_framesize < (1u << FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN));
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.max_framesize, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
				return false;
			FLAC__ASSERT(FLAC__format_sample_rate_is_valid(metadata->data.stream_info.sample_rate));
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.sample_rate, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
				return false;
			FLAC__ASSERT(metadata->data.stream_info.channels > 0);
			FLAC__ASSERT(metadata->data.stream_info.channels <= (1u << FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN));
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.channels-1, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
				return false;
			FLAC__ASSERT(metadata->data.stream_info.bits_per_sample > 0);
			FLAC__ASSERT(metadata->data.stream_info.bits_per_sample <= (1u << FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN));
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.stream_info.bits_per_sample-1, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
				return false;
			if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
				return false;
			if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.stream_info.md5sum, 16))
				return false;
			break;
		case FLAC__METADATA_TYPE_PADDING:
			if(!FLAC__bitwriter_write_zeroes(bw, metadata->length * 8))
				return false;
			break;
		case FLAC__METADATA_TYPE_APPLICATION:
			if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8))
				return false;
			if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.application.data, metadata->length - (FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8)))
				return false;
			break;
		case FLAC__METADATA_TYPE_SEEKTABLE:
			for(i = 0; i < metadata->data.seek_table.num_points; i++) {
				if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.seek_table.points[i].sample_number, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.seek_table.points[i].stream_offset, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.seek_table.points[i].frame_samples, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
					return false;
			}
			break;
		case FLAC__METADATA_TYPE_VORBIS_COMMENT:
			if(!FLAC__bitwriter_write_raw_uint32_little_endian(bw, vendor_string_length))
				return false;
			if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)FLAC__VENDOR_STRING, vendor_string_length))
				return false;
			if(!FLAC__bitwriter_write_raw_uint32_little_endian(bw, metadata->data.vorbis_comment.num_comments))
				return false;
			for(i = 0; i < metadata->data.vorbis_comment.num_comments; i++) {
				if(!FLAC__bitwriter_write_raw_uint32_little_endian(bw, metadata->data.vorbis_comment.comments[i].length))
					return false;
				if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.vorbis_comment.comments[i].entry, metadata->data.vorbis_comment.comments[i].length))
					return false;
			}
			break;
		case FLAC__METADATA_TYPE_CUESHEET:
			FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
			if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)metadata->data.cue_sheet.media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8))
				return false;
			if(!FLAC__bitwriter_write_raw_uint64(bw, metadata->data.cue_sheet.lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
				return false;
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.cue_sheet.is_cd? 1 : 0, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
				return false;
			if(!FLAC__bitwriter_write_zeroes(bw, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
				return false;
			if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.cue_sheet.num_tracks, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
				return false;
			for(i = 0; i < metadata->data.cue_sheet.num_tracks; i++) {
				const FLAC__StreamMetadata_CueSheet_Track *track = metadata->data.cue_sheet.tracks + i;

				if(!FLAC__bitwriter_write_raw_uint64(bw, track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, track->number, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
					return false;
				FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
				if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, track->type, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, track->pre_emphasis, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
					return false;
				if(!FLAC__bitwriter_write_zeroes(bw, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, track->num_indices, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
					return false;
				for(j = 0; j < track->num_indices; j++) {
					const FLAC__StreamMetadata_CueSheet_Index *index = track->indices + j;

					if(!FLAC__bitwriter_write_raw_uint64(bw, index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
						return false;
					if(!FLAC__bitwriter_write_raw_uint32(bw, index->number, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
						return false;
					if(!FLAC__bitwriter_write_zeroes(bw, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
						return false;
				}
			}
			break;
		case FLAC__METADATA_TYPE_PICTURE:
			{
				size_t len;
				if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.type, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
					return false;
				len = strlen(metadata->data.picture.mime_type);
				if(!FLAC__bitwriter_write_raw_uint32(bw, len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
					return false;
				if(!FLAC__bitwriter_write_byte_block(bw, (const FLAC__byte*)metadata->data.picture.mime_type, len))
					return false;
				len = strlen((const char *)metadata->data.picture.description);
				if(!FLAC__bitwriter_write_raw_uint32(bw, len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
					return false;
				if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.picture.description, len))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
					return false;
				if(!FLAC__bitwriter_write_raw_uint32(bw, metadata->data.picture.data_length, FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
					return false;
				if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.picture.data, metadata->data.picture.data_length))
					return false;
			}
			break;
		default:
			if(!FLAC__bitwriter_write_byte_block(bw, metadata->data.unknown.data, metadata->length))
				return false;
			break;
	}

	FLAC__ASSERT(FLAC__bitwriter_is_byte_aligned(bw));
	return true;
}
Ejemplo n.º 2
0
FLAC__bool test_bitwriter(void)
{
	FLAC__BitWriter *bw;
	FLAC__bool ok;
	unsigned i, j;
#if WORDS_BIGENDIAN
	static bwword test_pattern1[5] = { 0xaaf0aabe, 0xaaaaaaa8, 0x300aaaaa, 0xaaadeadb, 0x00eeface };
#else
	static bwword test_pattern1[5] = { 0xbeaaf0aa, 0xa8aaaaaa, 0xaaaa0a30, 0xdbeaadaa, 0x00eeface };
#endif
	unsigned words, bits; /* what we think bw->words and bw->bits should be */

	printf("\n+++ libFLAC unit test: bitwriter\n\n");

	/*
	 * test new -> delete
	 */
	printf("testing new... ");
	bw = FLAC__bitwriter_new();
	if(0 == bw) {
		printf("FAILED, returned NULL\n");
		return false;
	}
	printf("OK\n");

	printf("testing delete... ");
	FLAC__bitwriter_delete(bw);
	printf("OK\n");

	/*
	 * test new -> init -> delete
	 */
	printf("testing new... ");
	bw = FLAC__bitwriter_new();
	if(0 == bw) {
		printf("FAILED, returned NULL\n");
		return false;
	}
	printf("OK\n");

	printf("testing init... ");
	FLAC__bitwriter_init(bw);
	if(0 == bw) {
		printf("FAILED, returned NULL\n");
		return false;
	}
	printf("OK\n");

	printf("testing delete... ");
	FLAC__bitwriter_delete(bw);
	printf("OK\n");

	/*
	 * test new -> init -> clear -> delete
	 */
	printf("testing new... ");
	bw = FLAC__bitwriter_new();
	if(0 == bw) {
		printf("FAILED, returned NULL\n");
		return false;
	}
	printf("OK\n");

	printf("testing init... ");
	FLAC__bitwriter_init(bw);
	if(0 == bw) {
		printf("FAILED, returned NULL\n");
		return false;
	}
	printf("OK\n");

	printf("testing clear... ");
	FLAC__bitwriter_clear(bw);
	if(0 == bw) {
		printf("FAILED, returned NULL\n");
		return false;
	}
	printf("OK\n");

	printf("testing delete... ");
	FLAC__bitwriter_delete(bw);
	printf("OK\n");

	/*
	 * test normal usage
	 */
	printf("testing new... ");
	bw = FLAC__bitwriter_new();
	if(0 == bw) {
		printf("FAILED, returned NULL\n");
		return false;
	}
	printf("OK\n");

	printf("testing init... ");
	ok = FLAC__bitwriter_init(bw);
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok)
		return false;

	printf("testing clear... ");
	FLAC__bitwriter_clear(bw);
	printf("OK\n");

	words = bits = 0;

	printf("capacity = %u\n", bw->capacity);

	printf("testing zeroes, raw_uint32*... ");
	ok =
		FLAC__bitwriter_write_raw_uint32(bw, 0x1, 1) &&
		FLAC__bitwriter_write_raw_uint32(bw, 0x1, 2) &&
		FLAC__bitwriter_write_raw_uint32(bw, 0xa, 5) &&
		FLAC__bitwriter_write_raw_uint32(bw, 0xf0, 8) &&
		FLAC__bitwriter_write_raw_uint32(bw, 0x2aa, 10) &&
		FLAC__bitwriter_write_raw_uint32(bw, 0xf, 4) &&
		FLAC__bitwriter_write_raw_uint32(bw, 0xaaaaaaaa, 32) &&
		FLAC__bitwriter_write_zeroes(bw, 4) &&
		FLAC__bitwriter_write_raw_uint32(bw, 0x3, 2) &&
		FLAC__bitwriter_write_zeroes(bw, 8) &&
		FLAC__bitwriter_write_raw_uint64(bw, FLAC__U64L(0xaaaaaaaadeadbeef), 64) &&
		FLAC__bitwriter_write_raw_uint32(bw, 0xace, 12)
	;
	if(!ok) {
		printf("FAILED\n");
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}
	words = 4;
	bits = 24;
	if(bw->words != words) {
		printf("FAILED byte count %u != %u\n", bw->words, words);
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}
	if(bw->bits != bits) {
		printf("FAILED bit count %u != %u\n", bw->bits, bits);
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}
	if(memcmp(bw->buffer, test_pattern1, sizeof(bwword)*words) != 0) {
		printf("FAILED pattern match (buffer)\n");
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}
	if((bw->accum & 0x00ffffff) != test_pattern1[words]) {
		printf("FAILED pattern match (bw->accum=%08X != %08X)\n", bw->accum&0x00ffffff, test_pattern1[words]);
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}
	printf("OK\n");
	FLAC__bitwriter_dump(bw, stdout);

	printf("testing raw_uint32 some more... ");
	ok = FLAC__bitwriter_write_raw_uint32(bw, 0x3d, 6);
	if(!ok) {
		printf("FAILED\n");
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}
	bits += 6;
	test_pattern1[words] <<= 6;
	test_pattern1[words] |= 0x3d;
	if(bw->words != words) {
		printf("FAILED byte count %u != %u\n", bw->words, words);
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}
	if(bw->bits != bits) {
		printf("FAILED bit count %u != %u\n", bw->bits, bits);
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}
	if(memcmp(bw->buffer, test_pattern1, sizeof(bwword)*words) != 0) {
		printf("FAILED pattern match (buffer)\n");
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}
	if((bw->accum & 0x3fffffff) != test_pattern1[words]) {
		printf("FAILED pattern match (bw->accum=%08X != %08X)\n", bw->accum&0x3fffffff, test_pattern1[words]);
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}
	printf("OK\n");
	FLAC__bitwriter_dump(bw, stdout);

	printf("testing utf8_uint32(0x00000000)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint32(bw, 0x00000000);
	ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0;
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint32(0x0000007F)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint32(bw, 0x0000007F);
	ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0x7F;
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint32(0x00000080)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint32(bw, 0x00000080);
	ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xC280;
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint32(0x000007FF)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint32(bw, 0x000007FF);
	ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xDFBF;
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint32(0x00000800)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint32(bw, 0x00000800);
	ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xE0A080;
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint32(0x0000FFFF)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint32(bw, 0x0000FFFF);
	ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xEFBFBF;
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint32(0x00010000)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint32(bw, 0x00010000);
#if WORDS_BIGENDIAN
	ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF0908080;
#else
	ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0x808090F0;
#endif
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint32(0x001FFFFF)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint32(bw, 0x001FFFFF);
#if WORDS_BIGENDIAN
	ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF7BFBFBF;
#else
	ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xBFBFBFF7;
#endif
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint32(0x00200000)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint32(bw, 0x00200000);
#if WORDS_BIGENDIAN
	ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xF8888080 && (bw->accum & 0xff) == 0x80;
#else
	ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0x808088F8 && (bw->accum & 0xff) == 0x80;
#endif
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint32(0x03FFFFFF)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint32(bw, 0x03FFFFFF);
#if WORDS_BIGENDIAN
	ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xFBBFBFBF && (bw->accum & 0xff) == 0xBF;
#else
	ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xBFBFBFFB && (bw->accum & 0xff) == 0xBF;
#endif
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint32(0x04000000)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint32(bw, 0x04000000);
#if WORDS_BIGENDIAN
	ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFC848080 && (bw->accum & 0xffff) == 0x8080;
#else
	ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0x808084FC && (bw->accum & 0xffff) == 0x8080;
#endif
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint32(0x7FFFFFFF)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint32(bw, 0x7FFFFFFF);
#if WORDS_BIGENDIAN
	ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFDBFBFBF && (bw->accum & 0xffff) == 0xBFBF;
#else
	ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xBFBFBFFD && (bw->accum & 0xffff) == 0xBFBF;
#endif
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint64(0x0000000000000000)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000000000000);
	ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0;
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint64(0x000000000000007F)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint64(bw, 0x000000000000007F);
	ok = TOTAL_BITS(bw) == 8 && (bw->accum & 0xff) == 0x7F;
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint64(0x0000000000000080)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000000000080);
	ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xC280;
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint64(0x00000000000007FF)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint64(bw, 0x00000000000007FF);
	ok = TOTAL_BITS(bw) == 16 && (bw->accum & 0xffff) == 0xDFBF;
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint64(0x0000000000000800)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000000000800);
	ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xE0A080;
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint64(0x000000000000FFFF)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint64(bw, 0x000000000000FFFF);
	ok = TOTAL_BITS(bw) == 24 && (bw->accum & 0xffffff) == 0xEFBFBF;
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint64(0x0000000000010000)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000000010000);
#if WORDS_BIGENDIAN
	ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF0908080;
#else
	ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0x808090F0;
#endif
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint64(0x00000000001FFFFF)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint64(bw, 0x00000000001FFFFF);
#if WORDS_BIGENDIAN
	ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xF7BFBFBF;
#else
	ok = TOTAL_BITS(bw) == 32 && bw->buffer[0] == 0xBFBFBFF7;
#endif
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint64(0x0000000000200000)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000000200000);
#if WORDS_BIGENDIAN
	ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xF8888080 && (bw->accum & 0xff) == 0x80;
#else
	ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0x808088F8 && (bw->accum & 0xff) == 0x80;
#endif
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint64(0x0000000003FFFFFF)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000003FFFFFF);
#if WORDS_BIGENDIAN
	ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xFBBFBFBF && (bw->accum & 0xff) == 0xBF;
#else
	ok = TOTAL_BITS(bw) == 40 && bw->buffer[0] == 0xBFBFBFFB && (bw->accum & 0xff) == 0xBF;
#endif
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint64(0x0000000004000000)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000004000000);
#if WORDS_BIGENDIAN
	ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFC848080 && (bw->accum & 0xffff) == 0x8080;
#else
	ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0x808084FC && (bw->accum & 0xffff) == 0x8080;
#endif
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint64(0x000000007FFFFFFF)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint64(bw, 0x000000007FFFFFFF);
#if WORDS_BIGENDIAN
	ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xFDBFBFBF && (bw->accum & 0xffff) == 0xBFBF;
#else
	ok = TOTAL_BITS(bw) == 48 && bw->buffer[0] == 0xBFBFBFFD && (bw->accum & 0xffff) == 0xBFBF;
#endif
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint64(0x0000000080000000)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint64(bw, 0x0000000080000000);
#if WORDS_BIGENDIAN
	ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xFE828080 && (bw->accum & 0xffffff) == 0x808080;
#else
	ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0x808082FE && (bw->accum & 0xffffff) == 0x808080;
#endif
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing utf8_uint64(0x0000000FFFFFFFFF)... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_utf8_uint64(bw, FLAC__U64L(0x0000000FFFFFFFFF));
#if WORDS_BIGENDIAN
	ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xFEBFBFBF && (bw->accum & 0xffffff) == 0xBFBFBF;
#else
	ok = TOTAL_BITS(bw) == 56 && bw->buffer[0] == 0xBFBFBFFE && (bw->accum & 0xffffff) == 0xBFBFBF;
#endif
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}

	printf("testing grow... ");
	FLAC__bitwriter_clear(bw);
	FLAC__bitwriter_write_raw_uint32(bw, 0x5, 4);
	j = bw->capacity;
	for(i = 0; i < j; i++)
		FLAC__bitwriter_write_raw_uint32(bw, 0xaaaaaaaa, 32);
#if WORDS_BIGENDIAN
	ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == 0x5aaaaaaa && (bw->accum & 0xf) == 0xa;
#else
	ok = TOTAL_BITS(bw) == i*32+4 && bw->buffer[0] == 0xaaaaaa5a && (bw->accum & 0xf) == 0xa;
#endif
	printf("%s\n", ok?"OK":"FAILED");
	if(!ok) {
		FLAC__bitwriter_dump(bw, stdout);
		return false;
	}
	printf("capacity = %u\n", bw->capacity);

	printf("testing free... ");
	FLAC__bitwriter_free(bw);
	printf("OK\n");

	printf("testing delete... ");
	FLAC__bitwriter_delete(bw);
	printf("OK\n");

	printf("\nPASSED!\n");
	return true;
}