Esempio n. 1
0
        public void Write(EndianBinaryWriter writer)
        {
            var decMemStream = new MemoryStream();
            var decWriter    = new EndianBinaryWriter(decMemStream);

            decWriter.Write(Literals.Length);
            foreach (var literal in Literals)
            {
                decWriter.SeekNext(4); //uint size
                var oldPos = decWriter.Position;
                BeamTerm.TermToBinary(decWriter, literal);
                var len = (uint)(decWriter.Position - oldPos);
                decWriter.SeekBack((int)(len + 4));
                decWriter.Write(len);
                decWriter.SeekNext((int)len);
            }

            decMemStream.Position = 0;
            var decBytes = decMemStream.ToArray();

            writer.Write(decBytes.Length);
            var encBytes = ZLibCompressor.Compress(decBytes);

            writer.Write(encBytes);
        }
Esempio n. 2
0
        public void Write(EndianBinaryWriter writer)
        {
            writer.Write(Version);
            writer.Write(Flags);
            writer.Write(LineInstructionsCount);
            writer.Write((uint)FileLineReferences.Sum(x => x.Value.Count));
            writer.Write((uint)FileNames.LongLength);
            foreach (var(fileIdx, refs)  in FileLineReferences)
            {
                BeamTerm.CompactTermToBinary(writer, new CompactTerm(ECompactTermType.Atom, fileIdx));
                foreach (var lineRef in refs)
                {
                    BeamTerm.CompactTermToBinary(writer, new CompactTerm(ECompactTermType.Integer, lineRef));
                }
            }

            var enc = Enc.Enc.Get(FileNamesEncodingType);

            foreach (var fileName in FileNames)
            {
                var bytes = enc.GetBytes(fileName);
                writer.Write((ushort)bytes.Length);
                writer.Write(bytes);
            }
        }
Esempio n. 3
0
        public static BeamLiteralsChunk Read(EndianBinaryReader reader, uint size)
        {
            var decSize  = reader.ReadUInt32();
            var encBytes = reader.ReadBytes((int)size - 4); //-decSize size

            var decBytes = ZLibCompressor.Decompress(encBytes);

            if (decBytes.Length != decSize)
            {
                throw new ReadBytesCountException(decBytes.Length, (int)decSize);
            }

            var decReader = new EndianBinaryReader(new MemoryStream(decBytes));
            var litCount  = decReader.ReadUInt32();
            var literals  = new IExtTerm[litCount];

            for (uint i = 0; i < litCount; i++)
            {
                var litSize = decReader.ReadUInt32();
                var oldPos  = decReader.Position;
                literals[i] = BeamTerm.BinaryToTerm(decReader);
                var newPos = decReader.Position;
                if (oldPos + litSize != newPos)
                {
                    throw new Exception();
                }
            }
            return(new BeamLiteralsChunk()
            {
                Literals = literals
            });
        }
Esempio n. 4
0
        public static BeamCompileInfoChunk Read(EndianBinaryReader reader, uint size)
        {
            var info = BeamTerm.BinaryToTerm(reader);

            return(new BeamCompileInfoChunk()
            {
                Info = info
            });
        }
Esempio n. 5
0
 public void Write(EndianBinaryWriter writer)
 {
     writer.Write((byte)EExtTermType.List);
     writer.Write(Elements.Length);
     foreach (var element in Elements)
     {
         BeamTerm.TermToBinary(writer, element, false);
     }
     BeamTerm.TermToBinary(writer, Tail, false);
 }
Esempio n. 6
0
        public static BeamAttributesChunk Read(EndianBinaryReader reader, uint size)
        {
            //var bytes = reader.ReadBytes((int)size);
            var oldPos = reader.Position;
            var term   = BeamTerm.BinaryToTerm(reader);
            var newPos = reader.Position;

            if (oldPos + size != newPos)
            {
                throw new Exception();
            }
            return(new BeamAttributesChunk()
            {
                Attributes = term
            });
        }
Esempio n. 7
0
        public void Write(EndianBinaryWriter writer)
        {
            if (Elements.Length <= byte.MaxValue)
            {
                writer.Write((byte)EExtTermType.Tuple8);
                writer.Write((byte)Elements.Length);
            }
            else
            {
                writer.Write((byte)EExtTermType.Tuple32);
                writer.Write(Elements.Length);
            }

            foreach (var element in Elements)
            {
                BeamTerm.TermToBinary(writer, element, false);
            }
        }
Esempio n. 8
0
        public static TermList Read(EndianBinaryReader reader)
        {
            var len      = reader.ReadUInt32();
            var elements = new IExtTerm[len];

            for (int i = 0; i < elements.Length; i++)
            {
                elements[i] = BeamTerm.BinaryToTerm(reader, false);
            }

            var tail = BeamTerm.BinaryToTerm(reader, false);

            return(new TermList()
            {
                Elements = elements,
                Tail = tail
            });
        }
Esempio n. 9
0
        public static TermTuple Read(EndianBinaryReader reader, ETermSize size)
        {
            var len = size switch
            {
                ETermSize.Small => reader.ReadByte(),
                ETermSize.Large => reader.ReadUInt32(),
                _ => throw new NotSupportedException(),
            };
            var elements = new IExtTerm[len];

            for (long i = 0; i < elements.LongLength; i++)
            {
                elements[i] = BeamTerm.BinaryToTerm(reader, false);
            }

            return(new TermTuple()
            {
                Elements = elements,
            });
        }
Esempio n. 10
0
        }                                                                                //file_name_idx with line_num[]

        public static BeamLinesChunk Read(EndianBinaryReader reader, uint size)
        {
            var ver            = reader.ReadUInt32();
            var flags          = reader.ReadUInt32();
            var lineInstrCount = reader.ReadUInt32(); //??
            var numLineRefs    = reader.ReadUInt32();
            var numFileNames   = reader.ReadUInt32();

            var        lineRefs      = new Dictionary <BigInteger, List <BigInteger> >(); //file with line[]
            BigInteger fileNameIndex = -1;
            var        fileNames     = new string[numFileNames];

            for (int i = 0; i < numLineRefs + numFileNames; i++)
            {
                var term = BeamTerm.BinaryToCompactTerm(reader);
                if (term.Type == ECompactTermType.Integer)
                {
                    if (fileNameIndex < 0)
                    {
                        if (ThrowOnBadFileId)
                        {
                            throw new FileIndexOverException((int)numFileNames, (int)term.Value);
                        }
                    }
                    //else
                    //    Console.WriteLine("Warn! Bad file id " + fileNameIndex);
                    if (!lineRefs.ContainsKey(fileNameIndex))
                    {
                        lineRefs.Add(fileNameIndex, new List <BigInteger>());
                    }
                    lineRefs[fileNameIndex].Add(term.Value);
                }
                else if (term.Type == ECompactTermType.Atom)
                {
                    if (term.Value > numFileNames)
                    {
                        throw new FileIndexOverException((int)numFileNames, (int)term.Value);
                    }
                    fileNameIndex = term.Value;
                }
                else
                {
                    throw new InvalidOperationException($"Expected only {ECompactTermType.Atom} or {ECompactTermType.Integer} but get {term.Type}");
                }
            }

            for (int i = 0; i < numFileNames; i++)
            {
                var strLen = reader.ReadUInt16();
                fileNames[i] = reader.ReadString(strLen, FileNamesEncodingType);
            }

            return(new BeamLinesChunk()
            {
                Version = ver,
                Flags = flags,
                FileNames = fileNames,
                FileLineReferences = lineRefs,
                LineInstructionsCount = lineInstrCount
            });
        }
Esempio n. 11
0
 public void Write(EndianBinaryWriter writer)
 {
     BeamTerm.TermToBinary(writer, Info);
 }
Esempio n. 12
0
 public void Write(EndianBinaryWriter writer)
 {
     BeamTerm.TermToBinary(writer, Attributes);
 }