/* * Helper Methods */ private string readHelper() { bool foundSeperator = false; List <string> data = new List <String>(); while (!foundSeperator) { byte[] buffer = new byte[1]; inputRIF.Read(buffer, 0, 1); string raw = new ASCIIEncoding().GetString(buffer); if (!raw.Equals("|")) { data.Add(raw); } else { foundSeperator = true; } } string returnData = null; foreach (string v in data) { returnData += v; } return(returnData); }
/// <summary> /// An archive had an empty rsmp, so f**k trying to read it /// and read all the chunkheaders instead. /// </summary> /// <param name="files">A list to fill with typetags (resourcename) and offsets.</param> private void FuckThisShit(ref Dictionary <string, List <uint> > files) { //IFF header is always 64 bytes - make absolutely sure we're at the right position in the file! m_Reader.BaseStream.Position = 64; List <KeyValuePair <string, uint> > offsets = new List <KeyValuePair <string, uint> >(); while (true) { uint offset = (uint)m_Reader.BaseStream.Position; byte[] TagBytes = m_Reader.ReadBytes(4); Array.Reverse(TagBytes); string tag = new ASCIIEncoding().GetString(TagBytes); byte[] bytes = m_Reader.ReadBytes(4); if (bytes.Length == 0) { break; } uint size = Endian.SwapUInt32(BitConverter.ToUInt32(bytes, 0)); m_Reader.BaseStream.Position += (size - 8); if (!tag.Equals("XXXX")) { offsets.Add(new KeyValuePair <string, uint>(tag, offset)); } //76 bytes is the size of a chunkheader, so don't bother reading the next one //the stream has less than 76 bytes left. if (m_Reader.BaseStream.Position == m_Reader.BaseStream.Length || (m_Reader.BaseStream.Length - m_Reader.BaseStream.Position) < 76) { break; } } List <string> typesFound = new List <string>(); foreach (KeyValuePair <string, uint> kvp in offsets) { if (!typesFound.Exists(delegate(string s) { return(s.CompareTo(kvp.Key) == 0); })) { List <KeyValuePair <string, uint> > theseChunks = offsets.FindAll(delegate(KeyValuePair <string, uint> pair) { return(pair.Key.CompareTo(kvp.Key) == 0); }); List <uint> offsetValues = new List <uint>(); foreach (KeyValuePair <string, uint> kvp2 in theseChunks) { offsetValues.Add(kvp2.Value); } files.Add(kvp.Key, offsetValues); typesFound.Add(kvp.Key); } } }
public void Equals(ASCIIEncoding encoding, object value, bool expected) { Assert.Equal(expected, encoding.Equals(value)); if (value is ASCIIEncoding) { Assert.Equal(expected, encoding.GetHashCode().Equals(value.GetHashCode())); } }
public void MessageTransfer(IMessage m) { BinaryReader reader = new BinaryReader(m.Body, Encoding.UTF8); byte[] buffer = new byte[m.Body.Length - m.Body.Position]; reader.Read(buffer, 0, buffer.Length); string str = new ASCIIEncoding().GetString(buffer); Console.WriteLine("Message: " + str); this._range.Add(m.Id); if (str.Equals("That's all, folks!")) { this._session.MessageAccept(this._range, new Option[0]); IClientSession session = this._session; lock (session) { Monitor.Pulse(this._session); } } }
private void button1_Click(object sender, EventArgs e) { string _rName, _numberOfP, _numberOfQ, _timeForQ; _rName = roomName.Text; _numberOfP = numberOfPlayers.Text; _numberOfQ = numberOfQ.Text; _timeForQ = timeForQ.Text; string msg = ClientCodes.CreateRoom + (_rName.Length.ToString()).PadLeft(2, '0') + _rName + _numberOfP + _numberOfQ.PadLeft(2, '0') + _timeForQ.PadLeft(2, '0'); byte[] buffer = new ASCIIEncoding().GetBytes(msg); Program.sock.Write(buffer, 0, msg.Length); byte[] buffer2 = new byte[4096]; int bytesRead = Program.sock.Read(buffer2, 0, 4096); string input = new ASCIIEncoding().GetString(buffer2); // getting data from server. if (input.Equals(ServerCodes.CreateRoomSuccess)) { Program._roomName = _rName; Program.max_number_players = _numberOfP; Program.number_of_questions = _numberOfQ; Program.time_per_questions = _timeForQ; Lobby l = new Lobby(); this.Hide(); l.ShowDialog(); l.Show(); //should open wait tab. } else { label6.Text = "Error: couldn't create a room."; } }
/// <summary> /// A PALT (palette) chunk was not found when searching through this archive's rsmp, /// so find it manually. /// </summary> private void FindPALT() { m_Reader.BaseStream.Position = 64; List <KeyValuePair <string, uint> > PALTOffsets = new List <KeyValuePair <string, uint> >(); while (true) { uint offset = (uint)m_Reader.BaseStream.Position; byte[] TagBytes = m_Reader.ReadBytes(4); Array.Reverse(TagBytes); string tag = new ASCIIEncoding().GetString(TagBytes); byte[] bytes = m_Reader.ReadBytes(4); if (bytes.Length == 0) { break; } uint size = Endian.SwapUInt32(BitConverter.ToUInt32(bytes, 0)); m_Reader.BaseStream.Position += (size - 8); if (tag.Equals("PALT")) { PALTOffsets.Add(new KeyValuePair <string, uint>(tag, offset)); } //76 bytes is the size of a chunkheader, so don't bother reading the next one //the stream has less than 76 bytes left. if (m_Reader.BaseStream.Position == m_Reader.BaseStream.Length || (m_Reader.BaseStream.Length - m_Reader.BaseStream.Position) < 76) { break; } } foreach (KeyValuePair <string, uint> KVP in PALTOffsets) { m_Reader.BaseStream.Position = KVP.Value; IffChunk Chunk = new IffChunk(KVP.Key); Chunk.Length = Endian.SwapUInt32(m_Reader.ReadUInt32()) - 76; Chunk.ID = Endian.SwapUInt16(m_Reader.ReadUInt16()); ushort Flags = Endian.SwapUInt16(m_Reader.ReadUInt16()); Chunk.NameString = GetNameString(); if ((m_Reader.BaseStream.Length - m_Reader.BaseStream.Position) >= Chunk.Length) { m_Reader.BaseStream.Position = KVP.Value + 76; Chunk.Data = m_Reader.ReadBytes((int)Chunk.Length); } else { Chunk.Data = new byte[Chunk.Length]; } m_PMaps.Add(new PaletteMap(Chunk)); } }
/// <summary> /// Reads the chunks of the IFF-archive by looking for the RSMP. /// </summary> private void ReadChunks() { string Identifier = new string(m_Reader.ReadChars(60)).Replace("\0", ""); if (Identifier != "IFF FILE 2.5:TYPE FOLLOWED BY SIZE JAMIE DOORNBOS & MAXIS 1") { throw new Exception("Invalid iff file!"); } uint resMapOffset = Endian.SwapUInt32(m_Reader.ReadUInt32()); Dictionary <string, List <uint> > files = new Dictionary <string, List <uint> >(); if (resMapOffset != 0) { long pos = m_Reader.BaseStream.Position; m_Reader.BaseStream.Position = resMapOffset; m_Reader.BaseStream.Position += 76; //Skip the header. m_Reader.ReadInt32(); //Reserved uint version = m_Reader.ReadUInt32(); m_Reader.ReadInt32(); //pmsr m_Reader.ReadInt32(); //Size uint typeCount = m_Reader.ReadUInt32(); //How many types are present in this *.iff... for (uint i = 0; i < typeCount; i++) { //NOTE: For some types in some files this is empty... string typeCode = new ASCIIEncoding().GetString(m_Reader.ReadBytes(4)); if (version == 0) { //Empty RSMP... //numEntries + 1 entry without label = 13 bytes. if ((m_Reader.BaseStream.Length - m_Reader.BaseStream.Position) < 13) { files.Clear(); FuckThisShit(ref files); break; } } else if (version == 1) { //Empty RSMP... //numEntries + 1 entry without label = 16 bytes. if ((m_Reader.BaseStream.Length - m_Reader.BaseStream.Position) < 16) { files.Clear(); FuckThisShit(ref files); break; } } //How many entries there are... uint numEntries = m_Reader.ReadUInt32(); List <uint> offsets = new List <uint>(); for (uint j = 0; j < numEntries; j++) { if (version == 0) { //Empty RSMP... //Minimum size for an entry without a label is 9 bytes. if ((m_Reader.BaseStream.Length - m_Reader.BaseStream.Position) < ((numEntries - j) * 9)) { files.Clear(); FuckThisShit(ref files); break; } } else if (version == 1) { //Empty RSMP... //Minimum size for an entry without a label is 12 bytes. if ((m_Reader.BaseStream.Length - m_Reader.BaseStream.Position) < ((numEntries - j) * 12)) { files.Clear(); FuckThisShit(ref files); break; } } uint offset = m_Reader.ReadUInt32(); m_Reader.ReadInt16(); //ChunkID if (version == 1) { m_Reader.ReadInt16(); } //ChunkID m_Reader.ReadInt16(); //Flags if (version == 1) { byte Length = m_Reader.ReadByte(); if (Length > 0) { m_Reader.ReadBytes(Length); } } else { GetNameString(); } offsets.Add(offset); } if (!files.ContainsKey(typeCode)) { files.Add(typeCode, offsets); } } } else //There was no offset to the resourcemap, meaning that an RSMP probably doesn't exist. { List <KeyValuePair <string, uint> > offsets = new List <KeyValuePair <string, uint> >(); while (true) { uint offset = (uint)m_Reader.BaseStream.Position; byte[] TagBytes = m_Reader.ReadBytes(4); Array.Reverse(TagBytes); string tag = new ASCIIEncoding().GetString(TagBytes); byte[] bytes = m_Reader.ReadBytes(4); if (bytes.Length == 0) { break; } uint size = Endian.SwapUInt32(BitConverter.ToUInt32(bytes, 0)); m_Reader.BaseStream.Position += (size - 8); if (!tag.Equals("XXXX")) { offsets.Add(new KeyValuePair <string, uint>(tag, offset)); } //76 bytes is the size of a chunkheader, so don't bother reading the next one //the stream has less than 76 bytes left. if (m_Reader.BaseStream.Position == m_Reader.BaseStream.Length || (m_Reader.BaseStream.Length - m_Reader.BaseStream.Position) < 76) { break; } } List <string> typesFound = new List <string>(); foreach (KeyValuePair <string, uint> kvp in offsets) { if (!typesFound.Exists(delegate(string s) { return(s.CompareTo(kvp.Key) == 0); })) { List <KeyValuePair <string, uint> > theseChunks = offsets.FindAll(delegate(KeyValuePair <string, uint> pair) { return(pair.Key.CompareTo(kvp.Key) == 0); }); List <uint> offsetValues = new List <uint>(); foreach (KeyValuePair <string, uint> kvp2 in theseChunks) { offsetValues.Add(kvp2.Value); } if (!files.ContainsKey(kvp.Key)) { files.Add(kvp.Key, offsetValues); } typesFound.Add(kvp.Key); } } } foreach (KeyValuePair <string, List <uint> > file in files) { foreach (int offset in file.Value) { if (offset > 0) { m_Reader.BaseStream.Position = offset; byte[] Buf = m_Reader.ReadBytes(4); string StrResource = Encoding.ASCII.GetString(Buf); if (StrResource == "SPR#" || StrResource == "SPR2" || StrResource == "rsmp" || StrResource == "PALT" || StrResource == "DGRP" || StrResource == "STR#" || StrResource == "BHAV" || StrResource == "FWAV" || StrResource == "CTSS" || StrResource == "TTAB" || StrResource == "TTAs" || StrResource == "OBJf" || StrResource == "BCON" || StrResource == "TPRP" || StrResource == "TMPL" || StrResource == "TRCN" || StrResource == "Optn" || StrResource == "SLOT" || StrResource == "GLOB" || StrResource == "FBMP" || StrResource == "BMP_" || StrResource == "FCNS") { //MessageBox.Show(StrResource); IffChunk Chunk = ToChunk(StrResource, offset); //i += (int)Chunk.Length; m_Chunks.Add(Chunk); } } } } }
public void Equals(ASCIIEncoding encoding, object value, bool expected) { Assert.Equal(expected, encoding.Equals(value)); Assert.Equal(expected, encoding.GetHashCode().Equals(value?.GetHashCode())); }
public void EqualsTest(ASCIIEncoding encoding, object value, bool expected) { Assert.Equal(expected, encoding.Equals(value)); Assert.Equal(expected, encoding.GetHashCode().Equals(value?.GetHashCode())); }
private void sign_in_btn_Click(object sender, EventArgs e) { string message = ""; string username = this.username_textBox.Text; string password = this.password_textBox.Text; string username_length = "", password_length = ""; if (username.Length < 10) { username_length = "0" + username.Length.ToString(); } if (password.Length < 10) { password_length = "0" + password.Length.ToString(); } try { byte[] buffer = new ASCIIEncoding().GetBytes("200" + username_length + username + password_length + password); this._clientStream.Write(buffer, 0, buffer.Length); this._clientStream.Flush(); this._bytesRead = this._clientStream.Read(this._bufferIn, 0, 4); if (this._bytesRead == 0) { //the client has disconnected from the server this._tcpClient.Close(); } message = new ASCIIEncoding().GetString(this._bufferIn); } catch { } if (message.Equals("1020")) { this.username.Hide(); this.password.Hide(); this.username_textBox.Hide(); this.password_textBox.Hide(); this.sign_in_btn.Hide(); this.problem_in_sign_in_label.Hide(); this.welcome_label.ForeColor = Color.White; this.welcome_label.Text = "Hello " + this.username_textBox.Text; this.welcome_label.TextAlign = ContentAlignment.MiddleCenter; this.welcome_label.Show(); this.sign_up_btn.Text = "Sign out"; this.join_room_btn.Show(); this.create_room_btn.Show(); this.my_status_btn.Show(); this.best_scores_btn.Show(); } else { if (message.Equals("1021")) { this.problem_in_sign_in_label.Text = "wrong username or password"; } else { this.problem_in_sign_in_label.Text = "user has already connected"; } this.problem_in_sign_in_label.Show(); } }