<commit_before>package hxd.fmt.tiff;
import hxd.fmt.tiff.Data;

class Writer {

	var f : haxe.io.Output;

	public function new(f) {
		this.f = f;
	}

	public function write( tif : TifFile ) {
		f.writeString("II");
		f.writeUInt16(42);
		var pos = 4;
		var offsets = [], lengths = [];
		for( d in tif.data ) {
			offsets.push(pos);
			lengths.push(d.length);
			pos += d.length;
		}
		f.writeInt32(pos + 4);
		pos += 4;

		var tags = tif.tags.copy();
		for( d in tif.data )
			f.write(d);
		tags.push({ tag : StripOffsets, type : Long, value : VArray([for( o in offsets ) VInt(o)]) });
		tags.push({ tag : StripByteCounts, type : Long, value : VArray([for( o in lengths ) VInt(o)]) });
		tags.sort((t1,t2) -> t1.tag.toInt() - t2.tag.toInt());

		f.writeUInt16(tags.length);
		pos += 2;

		var endTags = pos + tags.length * 12 + 4;
		var tagExtraValues = [];

		for( t in tags ) {
			f.writeUInt16(t.tag.toInt());
			f.writeUInt16(t.type.toInt());
			var count = switch( t.value ) {
			case VArray(arr): arr.length;
			case VString(s): s.length;
			default: 1;
			}
			f.writeInt32(count);
			var size = count * t.type.getSize();
			if( size > 4 ) {
				f.writeInt32(endTags);
				endTags += size;<commit_msg>Fix StripOffsets off by 4 bytes in TIFF writer (#869)<commit_after>package hxd.fmt.tiff;
import hxd.fmt.tiff.Data;

class Writer {

	var f : haxe.io.Output;

	public function new(f) {
		this.f = f;
	}

	public function write( tif : TifFile ) {
		f.writeString("II");
		f.writeUInt16(42);
		var pos = 8;
		var offsets = [], lengths = [];
		for( d in tif.data ) {
			offsets.push(pos);
			lengths.push(d.length);
			pos += d.length;
		}
		f.writeInt32(pos);

		var tags = tif.tags.copy();
		for( d in tif.data )
			f.write(d);
		tags.push({ tag : StripOffsets, type : Long, value : VArray([for( o in offsets ) VInt(o)]) });
		tags.push({ tag : StripByteCounts, type : Long, value : VArray([for( o in lengths ) VInt(o)]) });
		tags.sort((t1,t2) -> t1.tag.toInt() - t2.tag.toInt());

		f.writeUInt16(tags.length);
		pos += 2;

		var endTags = pos + tags.length * 12 + 4;
		var tagExtraValues = [];

		for( t in tags ) {
			f.writeUInt16(t.tag.toInt());
			f.writeUInt16(t.type.toInt());
			var count = switch( t.value ) {
			case VArray(arr): arr.length;
			case VString(s): s.length;
			default: 1;
			}
			f.writeInt32(count);
			var size = count * t.type.getSize();
			if( size > 4 ) {
				f.writeInt32(endTags);
				endTags += size;
{'fraction_non_alphanumeric': 0.17827868852459017, 'fraction_numerical': 0.02459016393442623, 'mean_word_length': 2.9434571890145396, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1320706', 'n_tokens_mistral': 1140, 'n_tokens_neox': 971, 'n_words': 229}