<commit_before>﻿using System;
using System.IO;

namespace LibRSync.Core
{
    public class PatchJob : Job
    {
        private readonly Stream delta;
        private readonly IDeltaProcessor deltaProcessor;

        private Opcode cmd;
        private long param1;
        private long param2;

        public PatchJob(Stream @base, Stream delta, Stream @new) : base("patch")
        {
            this.delta = delta;
            this.deltaProcessor = new PatchProcessor(@base, @new);
        }

        protected override StateFunc InitialState()
        {
            return Header;
        }

        protected StateFunc Params()
        {
            NetInt.ReadInt(delta, cmd.Len1, out param1);
            NetInt.ReadInt(delta, cmd.Len2, out param2);

            return PatchRun;
        }

        private StateFunc Literal()
        {
            var buf = new byte[param1];
            delta.Read(buf, 0, buf.Length);
            deltaProcessor.Literal(buf, 0, buf.Length);

            return CommandByte;
        }

        private StateFunc Copy()
        {
            deltaProcessor.Copy(param1, param2);

            return CommandByte;
        }

        private StateFunc PatchRun()
        {
            if (cmd.Kind == OpKind.Literal)
                return Literal;
            else if (cmd.Kind == OpKind.Copy)
                return Copy;
            else if (cmd.Kind == OpKind.End)
                return Completed;
            else
                throw new FileFormatException();
        }

        private StateFunc CommandByte()
        {
            var b = delta.ReadByte();
            if (b == -1)
                return Completed;

            cmd = OpcodePrototypes.Get((byte) b);

            if (cmd.Len1 > 0)
            {
                return Params;
            }
            else
            {
                param1 = cmd.Immediate;
                param2 = 0;

                return PatchRun;
            }
        }

        private StateFunc Header()
        {
            var signature = NetInt.ReadInt(delta);

            if (signature != Const.RS_DELTA_MAGIC)
                throw new Exception();
<commit_msg>Follow the original design of delta file: expect END opcode.<commit_after>﻿using System;
using System.IO;

namespace LibRSync.Core
{
    public class PatchJob : Job
    {
        private readonly Stream delta;
        private readonly IDeltaProcessor deltaProcessor;

        private Opcode cmd;
        private long param1;
        private long param2;

        public PatchJob(Stream @base, Stream delta, Stream @new) : base("patch")
        {
            this.delta = delta;
            this.deltaProcessor = new PatchProcessor(@base, @new);
        }

        protected override StateFunc InitialState()
        {
            return Header;
        }

        protected StateFunc Params()
        {
            NetInt.ReadInt(delta, cmd.Len1, out param1);
            NetInt.ReadInt(delta, cmd.Len2, out param2);

            return PatchRun;
        }

        private StateFunc Literal()
        {
            var buf = new byte[param1];
            delta.Read(buf, 0, buf.Length);
            deltaProcessor.Literal(buf, 0, buf.Length);

            return CommandByte;
        }

        private StateFunc Copy()
        {
            deltaProcessor.Copy(param1, param2);

            return CommandByte;
        }

        private StateFunc PatchRun()
        {
            if (cmd.Kind == OpKind.Literal)
                return Literal;
            else if (cmd.Kind == OpKind.Copy)
                return Copy;
            else if (cmd.Kind == OpKind.End)
                return Completed;
            else
                throw new FileFormatException();
        }

        private StateFunc CommandByte()
        {
            var b = delta.ReadByte();
            if (b == -1)
                throw new FileFormatException("Unexpected end of file");

            cmd = OpcodePrototypes.Get((byte) b);

            if (cmd.Len1 > 0)
            {
                return Params;
            }
            else
            {
                param1 = cmd.Immediate;
                param2 = 0;

                return PatchRun;
            }
        }

        private StateFunc Header()
        {
            var signature = NetInt.ReadInt(delta);

            if (signature != Const.RS_DELTA_MAGIC)
                throw new Exception();

{'fraction_non_alphanumeric': 0.08201178069777978, 'fraction_numerical': 0.007702763932940644, 'mean_word_length': 1.3298153034300793, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7633631', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1092, 'n_words': 301}