private static libtysila.tydb.Location ReadLocation(Stream s, int locs_offset, int loc_id) { libtysila.tydb.Location l = new libtysila.tydb.Location(); long pos = s.Position; s.Seek((long)(locs_offset + loc_id), SeekOrigin.Begin); int loc_type = ReadInt(s); switch (loc_type) { case 0: l.Type = libtysila.tydb.Location.LocationType.Register; l.RegisterName = ReadString(s); l.Length = s.ReadByte(); break; case 1: l.Type = libtysila.tydb.Location.LocationType.Memory; l.MemoryLocation = ReadULong(s); l.Length = s.ReadByte(); break; case 2: { l.Type = libtysila.tydb.Location.LocationType.ContentsOfLocation; int loc_id_2 = ReadInt(s); l.Offset = (int)ReadULong(s); l.Length = s.ReadByte(); l.ContentsOf = ReadLocation(s, locs_offset, loc_id_2); break; } default: throw new NotSupportedException(); } s.Seek(pos, SeekOrigin.Begin); return(l); }
private int write(List<byte> locations, Dictionary<string, int> location_cache, libtysila.tydb.Location location) { if(location_cache.ContainsKey(location.ToString())) return location_cache[location.ToString()]; int contents_of = -1; if (location.ContentsOf != null) contents_of = write(locations, location_cache, location.ContentsOf); int loc_id = locations.Count; byte[] buf = null; switch (location.Type) { case libtysila.tydb.Location.LocationType.Register: buf = new byte[9]; write(buf, 0, 0); write(buf, 4, location.RegisterName); buf[8] = (byte)location.Length; break; case libtysila.tydb.Location.LocationType.Memory: buf = new byte[13]; write(buf, 0, 1); write(buf, 4, location.MemoryLocation); buf[12] = (byte)location.Length; break; case libtysila.tydb.Location.LocationType.ContentsOfLocation: buf = new byte[17]; write(buf, 0, 2); write(buf, 4, contents_of); write(buf, 8, (ulong)location.Offset); buf[16] = (byte)location.Length; break; default: throw new NotSupportedException(); } location_cache.Add(location.ToString(), loc_id); locations.AddRange(buf); return loc_id; }