Example #1
0
            internal override Command Decode(int prefix, Func <int> nextByte)
            {
                Delta nd = DeltaDecoder.DecodeNd((prefix & 0b11111000) >> 3);
                Delta fd = DeltaDecoder.DecodeFd(nextByte(), nextByte(), nextByte());

                return(Commands.GFill(nd, fd));
            }
Example #2
0
        public override Result <ProcessedPayload> Decode(IPayload payload, DecodingContext context)
        {
            var logger = context.ChannelOptions?.Logger ?? DefaultLogger.LoggerInstance;

            if (payload == null)
            {
                return(Result.Ok(new ProcessedPayload()));
            }

            try
            {
                var payloadBytes = DataHelpers.ConvertToByteArray(payload.Data);

                var previousPayload = context.PreviousPayload?.GetBytes();
                if (previousPayload is null)
                {
                    return(Result.Fail <ProcessedPayload>(new VcDiffErrorInfo("Missing previous payload")));
                }

                var result       = DeltaDecoder.ApplyDelta(previousPayload, payloadBytes);
                var nextEncoding = RemoveCurrentEncodingPart(payload);

                context.PreviousPayload = new PayloadCache(result.AsByteArray(), nextEncoding);
                return(Result.Ok(new ProcessedPayload(
                                     result.AsByteArray(),
                                     RemoveCurrentEncodingPart(payload))));
            }
            catch (Exception ex)
            {
                var error =
                    $"Payload Encoding: {payload.Encoding}. Payload data: {GetPayloadString()}";
                logger.Error("Error decoding vcdiff message: " + error, ex);

                return(Result.Fail <ProcessedPayload>(new VcDiffErrorInfo("Failed to decode vcdiff message", ex)));
            }

            string GetPayloadString()
            {
                try
                {
                    if (payload.Data == null)
                    {
                        return("null");
                    }

                    if (payload.Data is byte[])
                    {
                        return((payload.Data as byte[]).ToBase64());
                    }

                    if (payload.Data is string)
                    {
                        return(payload.Data as string);
                    }

                    return(string.Empty);
                }
                catch
                {
                    return(string.Empty);
                }
            }
        }