コード例 #1
0
ファイル: BinaryReader.cs プロジェクト: hnagata/binariex
        public void GetValue(LeafInfo leafInfo, out object raw, out object decoded, out object output)
        {
            var buffer = new byte[leafInfo.Size];
            var total  = 0;
            var read   = -1;

            while (read != 0 && total < leafInfo.Size)
            {
                read   = this.stream.Read(buffer, total, leafInfo.Size - total);
                total += read;
            }
            if (total == 0)
            {
                throw new EndOfStreamException();
            }
            else if (total < leafInfo.Size)
            {
                throw new BinariexException("reading input file", "Reached EOF before schema end.");
            }
            raw = buffer;

            if (!this.decodeMap.TryGetValue(leafInfo.Type, out var decode))
            {
                throw new BinariexException("reading schema", "Invalid data type: {0}", leafInfo.Type);
            }

            decoded = decode(leafInfo, buffer);
            output  = null;
        }
コード例 #2
0
ファイル: ExcelReader.cs プロジェクト: hnagata/binariex
        public void GetValue(LeafInfo leafInfo, out object raw, out object decoded, out object output)
        {
            var ctx = this.CurrentContext;

            if (ctx == null)
            {
                throw new InvalidOperationException();
            }

            if (ctx.SheetContext.HeaderRowCount + ctx.CursorRowIndex > ctx.Sheet.Dimension.Rows)
            {
                throw new EndOfStreamException();
            }

            var cell     = ctx.Sheet.Cells[ctx.SheetContext.HeaderRowCount + ctx.CursorRowIndex, ctx.CursorColumnIndex];
            var cellText = cell.Text;

            raw = cellText;

            try
            {
                GetDecodedValue(leafInfo, cellText, out decoded, out output);
            }
            catch (BinariexException exc)
            {
                throw exc.AddReaderPosition($@"[{Path.GetFileName(path)}]{cell.FullAddress}");
            }

            base.StepCursor();
        }
コード例 #3
0
ファイル: BinaryWriter.cs プロジェクト: hnagata/binariex
        public void SetValue(LeafInfo leafInfo, object raw, object decoded, object output)
        {
            var encoded = null as byte[];

            if (output != null)
            {
                encoded = output as byte[];
            }
            else if (decoded is byte[])
            {
                encoded = decoded as byte[];
            }
            else
            {
                if (!this.encodeMap.TryGetValue(leafInfo.Type, out var encode))
                {
                    throw new InvalidDataException();
                }
                encoded = encode(leafInfo, decoded);
            }

            if (encoded.Length != leafInfo.Size)
            {
                throw new InvalidDataException();
            }

            this.stream.Write(encoded, 0, encoded.Length);
        }
コード例 #4
0
ファイル: Converter.cs プロジェクト: hnagata/binariex
        void ParseLeaf(XElement elem)
        {
            var leafInfo = new LeafInfo {
                Name     = EvaluateExpr(GetAttr(elem, "name")) as string,
                Type     = EvaluateExpr(GetAttr(elem, "type")) as string,
                Size     = Convert.ToInt32(EvaluateExpr(GetAttr(elem, "size"))),
                Encoding = elem.Attribute("encoding") != null?GetEncoding(EvaluateExpr(elem.Attribute("encoding").Value).ToString()) : this.encoding,
                               Endian = elem.Attribute("endian") != null?GetEndian(EvaluateExpr(elem.Attribute("endian").Value).ToString()) : this.endian,
                                            HasUserCode = elem.Attribute("code") != null
            };

            if (leafInfo.Size <= 0)
            {
                throw new BinariexException("reading schema", "Invalid size: {0}", leafInfo.Size);
            }

            GetValueSafe(leafInfo, out var raw, out var firstObj, out var output);

            var secondObj = null as object;

            if (output == null)
            {
                var codeName = elem.Attribute("code")?.Value;
                secondObj = GetSecondObject(firstObj, codeName);
            }

            SetValueSafe(leafInfo, raw, secondObj, output);

            var label = elem.Attribute("label")?.Value;

            if (label != null)
            {
                this.jsEngine.SetValue(label, raw is byte[] ? secondObj : firstObj);
            }
        }
コード例 #5
0
ファイル: ExcelWriter.cs プロジェクト: hnagata/binariex
        public void SetValue(LeafInfo leafInfo, object raw, object decoded, object output)
        {
            var ctx = this.CurrentContext;

            if (ctx == null)
            {
                throw new InvalidOperationException();
            }

            SetHeaderName(ctx, ctx.HeaderRowIndex + 1, ctx.CursorColumnIndex, leafInfo.Name);

            var rawRepr   = string.Join("", (raw as byte[]).Select(e => e.ToString("X2")));
            var totalRepr = raw == decoded || decoded.ToString() == "" ? $@"<{rawRepr}>" :
                            $@"{decoded.ToString()} <{rawRepr}>";
            var dispRepr = totalRepr.Length > MAX_NUM_CHAR_IN_LINE?totalRepr.Substring(0, MAX_NUM_CHAR_IN_LINE - 3) + "..>" : totalRepr;

            var cell = ctx.Sheet.Cells[ctx.SheetContext.HeaderRowCount + ctx.CursorRowIndex, ctx.CursorColumnIndex];

            cell.Value = dispRepr;

            if (ctx.RepeatEnabled)
            {
                cell.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
            }

            base.StepCursor();
        }
コード例 #6
0
ファイル: BinaryWriter.cs プロジェクト: hnagata/binariex
        byte[] EncodeUnsignedInteger(LeafInfo leafInfo, object decoded)
        {
            var decodedInt = Convert.ToInt64(decoded);
            var buffer     =
                leafInfo.Size == 1 ? new byte[] { (byte)decodedInt } :
            leafInfo.Size == 2 ? BitConverter.GetBytes((ushort)decodedInt) :
            leafInfo.Size == 4 ? BitConverter.GetBytes((uint)decodedInt) :
            leafInfo.Size == 8 ? BitConverter.GetBytes((ulong)decodedInt) :
            throw new InvalidDataException();

            return((BitConverter.IsLittleEndian ^ leafInfo.Endian == "LE") ? buffer.Reverse().ToArray() : buffer);
        }
コード例 #7
0
ファイル: Converter.cs プロジェクト: hnagata/binariex
 void SetValueSafe(LeafInfo leafInfo, object raw, object secondObj, object output)
 {
     try
     {
         this.writer.SetValue(leafInfo, raw, secondObj, output);
     }
     catch (BinariexException)
     {
         throw;
     }
     catch (Exception exc)
     {
         throw new BinariexException(exc, "writing to file");
     }
 }
コード例 #8
0
ファイル: BinaryReader.cs プロジェクト: hnagata/binariex
 object DecodeUnsignedInteger(LeafInfo leafInfo, byte[] raw)
 {
     try
     {
         var ordered = (BitConverter.IsLittleEndian ^ leafInfo.Endian == "LE") ? raw.Reverse().ToArray() : raw;
         var x       =
             raw.Length == 1 ? raw[0] :
             raw.Length == 2 ? BitConverter.ToUInt16(ordered, 0) :
             raw.Length == 4 ? BitConverter.ToUInt32(ordered, 0) :
             raw.Length == 8 ? BitConverter.ToUInt64(ordered, 0) :
             throw new BinariexException("reading schema", "Invalid data length: {0}", raw.Length);
         return(x);
     }
     catch (Exception exc)
     {
         throw new BinariexException(exc, "Invalid data value");
     }
 }
コード例 #9
0
ファイル: BinaryReader.cs プロジェクト: hnagata/binariex
        object DecodePBCD(LeafInfo leafInfo, byte[] raw)
        {
            long x = 0;

            for (var i = 0; i < raw.Length - 1; i++)
            {
                x = x * 100 + (raw[i] >> 4 & 0x0f) * 10 + (raw[i] & 0x0f);
            }
            x = x * 10 + (raw[raw.Length - 1] >> 4 & 0x0f);

            var signCode = raw[raw.Length - 1] & 0x0f;

            if (signCode == 0x0b || signCode == 0x0d)
            {
                x = -x;
            }

            return(x);
        }
コード例 #10
0
ファイル: Converter.cs プロジェクト: hnagata/binariex
 void GetValueSafe(LeafInfo leafInfo, out object raw, out object firstObj, out object output)
 {
     try
     {
         this.reader.GetValue(leafInfo, out raw, out firstObj, out output);
     }
     catch (EndOfStreamException)
     {
         throw;
     }
     catch (BinariexException)
     {
         throw;
     }
     catch (Exception exc)
     {
         throw new BinariexException(exc, "reading input file");
     }
 }
コード例 #11
0
ファイル: BinaryWriter.cs プロジェクト: hnagata/binariex
        byte[] EncodePBCD(LeafInfo leafInfo, object decoded)
        {
            var decodedInt = Convert.ToInt64(decoded);

            byte[] buffer = new byte[leafInfo.Size];

            buffer[leafInfo.Size - 1] = (byte)(decodedInt >= 0 ? 0x0c : 0x0d);
            decodedInt = decodedInt >= 0 ? decodedInt : -decodedInt;

            buffer[leafInfo.Size - 1] |= (byte)((decodedInt % 10) << 4);
            decodedInt /= 10;

            for (var i = buffer.Length - 2; i >= 0 && decodedInt > 0; i--)
            {
                buffer[i] = (byte)(
                    (decodedInt % 10) |
                    (decodedInt / 10 % 10) << 4
                    );
                decodedInt /= 100;
            }

            return(buffer);
        }
コード例 #12
0
ファイル: ExcelReader.cs プロジェクト: hnagata/binariex
        void GetDecodedValue(LeafInfo leafInfo, string cellText, out object decoded, out object output)
        {
            var m = Regex.Match(cellText, @"^(?:(.*) )?<([0-9A-Fa-f]+)(\.\.)?>$");

            if (m.Success)
            {
                decoded = m.Groups[1].Value ?? "";
                if (this.settings["bypassIfRawAvailable"] as string == "true")
                {
                    output = m.Groups[3].Value == "" ?
                             GetByteArrayFromBinString(m.Groups[2].Value) :
                             Enumerable.Repeat(Convert.ToByte(m.Groups[2].Value.Substring(0, 2), 16), leafInfo.Size).ToArray();
                    return;
                }
                else
                {
                    cellText = m.Groups[1].Value ?? "";
                }
            }
            output = null;

            if (leafInfo.HasUserCode)
            {
                decoded = cellText;
                return;
            }

            switch (leafInfo.Type)
            {
            case "bin":
                decoded =
                    !m.Success ? throw new BinariexException("reading input file", "Invalid binary representation: {0}", cellText ?? "''") :
                          m.Groups[3].Value == "" ? GetByteArrayFromBinString(m.Groups[2].Value) :
                          Enumerable.Repeat(Convert.ToByte(m.Groups[2].Value.Substring(0, 2), 16), leafInfo.Size).ToArray();
                break;

            case "char":
                decoded = cellText;
                break;

            case "int":
            case "pbcd":
                try
                {
                    decoded = Int64.Parse(cellText);
                }
                catch (Exception exc)
                {
                    throw new BinariexException(exc, "reading input file", "Invalid number format: {0}", cellText ?? "''");
                }
                break;

            case "uint":
                try
                {
                    decoded = UInt64.Parse(cellText);
                }
                catch (Exception exc)
                {
                    throw new BinariexException(exc, "reading input file", "Invalid number format: {0}", cellText ?? "''");
                }
                break;

            default:
                throw new InvalidDataException();
            }
        }
コード例 #13
0
ファイル: BinaryReader.cs プロジェクト: hnagata/binariex
 object DecodeString(LeafInfo leafInfo, byte[] raw)
 {
     return(leafInfo.Encoding.GetString(raw));
 }
コード例 #14
0
ファイル: BinaryReader.cs プロジェクト: hnagata/binariex
 object DecodeBinary(LeafInfo leafInfo, byte[] raw)
 {
     return(raw);
 }
コード例 #15
0
ファイル: BinaryWriter.cs プロジェクト: hnagata/binariex
 byte[] EncodeBinary(LeafInfo leafInfo, object decoded)
 {
     return(decoded as byte[]);
 }
コード例 #16
0
ファイル: BinaryWriter.cs プロジェクト: hnagata/binariex
 byte[] EncodeString(LeafInfo leafInfo, object decoded)
 {
     return(leafInfo.Encoding.GetBytes(decoded.ToString()));
 }