Exemple #1
0
        public void KattisSolver_Should_WorkOnMultipleLinesOfInput()
        {
            // Arrange
            string result;

            using (var helper = new StreamHelper("5 3 8", "10 5 2", "5 15 3"))
            {
                // Act
                var kattisSolver = new KattisSolver(helper.InStream, helper.OutStream);
                kattisSolver.SolveOnStreams();
                result = helper.ReadOut();
            }

            // Assert
            Assert.That(result, Is.EqualTo("5+3=8\r\n10/5=2\r\n5=15/3\r\n"));
        }
Exemple #2
0
        public void KattisSolver_Should_WorkOnStreams()
        {
            // Arrange
            const string data = "5 3 8";
            string result;

            using (var helper = new StreamHelper(data))
            {
                // Act
                var kattisSolver = new KattisSolver(helper.InStream, helper.OutStream);
                kattisSolver.SolveOnStreams();
                result = helper.ReadOut();
            }

            // Assert
            Assert.That(result, Is.EqualTo("5+3=8\r\n"));
        }
        public static Image GetQRCode(byte[] data, ErrorCorrectionLevel level = ErrorCorrectionLevel.M)
        {
            bool requires16BitLength = false;
            int maxBytesInVersion9Code = QRErrorCorrections.GetQRVersionInfo(9).GetCorrectionInfo(level).TotalDataBytes;
            if (maxBytesInVersion9Code - 2 < data.Length)
            {
                // This data requires a version 10 or higher code; will not fit in version 9 or lower.
                // Version 10 and higher codes require 16-bit data lengths.
                requires16BitLength = true;
            }

            StreamHelper sh = new StreamHelper();
            sh.WriteNibble(0x04); // byte mode
            if (requires16BitLength)
            {
                sh.WriteWord((ushort)data.Length);
            }
            else
            {
                sh.WriteByte((byte)data.Length);
            }
            sh.WriteBytes(new ArraySegment<byte>(data));
            sh.WriteNibble(0x00); // terminator
            byte[] binaryData = sh.ToArray();

            int qrCodeVersion;
            ErrorCorrectionLevel errorCorrectionLevel;
            byte[] finalMessageSequence = QRErrorCorrections.GetMessageSequence(binaryData, level, out qrCodeVersion, out errorCorrectionLevel);

            SymbolTemplate template = SymbolTemplate.CreateTemplate(qrCodeVersion);
            template.ErrorCorrectionLevel = errorCorrectionLevel;
            template.PopulateData(finalMessageSequence);
            template.Complete();

            return template.ToImage();
        }
        /// <summary>
        /// Parses headers from message or mime entry.
        /// </summary>
        /// <param name="entryStrm">Stream from where to read headers.</param>
        /// <returns>Returns header lines.</returns>
        public static string ParseHeaders(Stream entryStrm)
        {
            /*3.1.  GENERAL DESCRIPTION
            A message consists of header fields and, optionally, a body.
            The  body  is simply a sequence of lines containing ASCII charac-
            ters.  It is separated from the headers by a null line  (i.e.,  a
            line with nothing preceding the CRLF).
            */

            byte[] crlf = new byte[] { (byte)'\r', (byte)'\n' };
            MemoryStream msHeaders = new MemoryStream();
            StreamHelper r = new StreamHelper(entryStrm, null);
            byte[] lineData = r.ReadLine();
            while (lineData != null)
            {
                if (lineData.Length == 0)
                {
                    break;
                }

                msHeaders.Write(lineData, 0, lineData.Length);
                msHeaders.Write(crlf, 0, crlf.Length);
                lineData = r.ReadLine();
            }

            return System.Text.Encoding.Default.GetString(msHeaders.ToArray());
        }
        /// <summary>
        /// Parses mime entries.
        /// </summary>
        /// <param name="msgStrm"></param>
        /// <param name="pos"></param>
        /// <param name="boundaryID"></param>
        internal ArrayList ParseEntries(MemoryStream msgStrm, int pos, string boundaryID)
        {
            ArrayList entries = null;

            // Entries are already parsed
            if (m_Entries != null)
            {
                return m_Entries;
            }

            entries = new ArrayList();

            // If message doesn't have entries and have 1 entry (simple text message or contains only attachment).
            if (this.ContentType.ToLower().IndexOf("multipart/") == -1)
            {
                entries.Add(new MimeEntry(msgStrm.ToArray(), this));
                m_Entries = entries;

                return m_Entries;
            }

            msgStrm.Position = pos;

            if (boundaryID.Length > 0)
            {
                MemoryStream strmEntry = new MemoryStream();
                StreamHelper reader = new StreamHelper(msgStrm, null);
                byte[] lineData = reader.ReadLine();

                // Search first entry
                while (lineData != null)
                {
                    string line = System.Text.Encoding.Default.GetString(lineData);
                    if (line.StartsWith("--" + boundaryID))
                    {
                        break;
                    }

                    lineData = reader.ReadLine();
                }

                // Start reading entries
                while (lineData != null)
                {
                    // Read entry data
                    string line = System.Text.Encoding.Default.GetString(lineData);
                    // Next boundary
                    if (line.StartsWith("--" + boundaryID) && strmEntry.Length > 0)
                    {
                        // Add Entry
                        entries.Add(new MimeEntry(strmEntry.ToArray(), this));

                        strmEntry.SetLength(0);
                    }
                    else
                    {
                        strmEntry.Write(lineData, 0, lineData.Length);
                        strmEntry.Write(new byte[] { (byte)'\r', (byte)'\n' }, 0, 2);
                    }

                    lineData = reader.ReadLine();
                }
            }

            return entries;
        }
		private static string ConstructNonMultiPart(MimeEntry ent,bool bodystructure)
		{			
			string str =  "(";

			// contentType
			str += "\"" + ent.ContentType.Split('/')[0] + "\" ";

			// conentSubType
			if(ent.ContentType.Split('/').Length == 2){
				str += "\"" + ent.ContentType.Split('/')[1].Replace(";","") + "\" "; 
			}
			else{
				str += "NIL ";
			}

			// conentTypeSubFields
			string longContentType = MimeParser.ParseHeaderField("Content-Type:",ent.Headers);
			if(longContentType.IndexOf(";") > -1){
				str += "(";
				string[] fields = longContentType.Split(';');
				for(int i=1;i<fields.Length;i++){
					string[] nameValue = fields[i].Replace("\"","").Trim().Split(new char[]{'='},2);

					str += "\"" + nameValue[0] + "\" \"" + nameValue[1] + "\"";

					if(i < fields.Length - 1){
						str += " ";
					}
				}
				str += ") ";
			}
			else{
				// if content is attachment and content type name filed is missing, use filename for it
				string fileName = MimeParser.ParseHeaderFiledSubField("Content-Disposition:","filename",ent.Headers);
				if(fileName.Length > 0){
					str += "(\"name\" \"" + fileName + "\") ";
				}
				else{
					str += "NIL ";
				}
			}

			// contentID
			string contentID = MimeParser.ParseHeaderField("Content-ID:",ent.Headers);
			if(contentID.Length > 0){
				str += "\"" + contentID + "\" ";
			}
			else{
				str += "NIL ";
			}

			// contentDescription
			string contentDescription = MimeParser.ParseHeaderField("Content-Description:",ent.Headers);
			if(contentDescription.Length > 0){
				str += "\"" + contentDescription + "\" ";
			}
			else{
				str += "NIL ";
			}

			// contentEncoding
			str += "\"" + ent.ContentEncoding + "\" ";

			// contentSize
			str += ent.DataNonDecoded.Length + " ";

			// envelope NOTE: included only if contentType = "message" !!!

			// contentLines NOTE: included only if contentType = "text" !!!
			if(ent.ContentType.ToLower().IndexOf("text") > -1){
				StreamHelper r = new StreamHelper(new MemoryStream(ent.DataNonDecoded), null);
				int nLines = 0;
				byte[] line = new byte[0];
				while(line != null){
					line = r.ReadLine();
					nLines++;
				}
				str += nLines;
			}
		
			// Need to add extended fields
			if(bodystructure){
				str += " ";

				// md5
				str += "NIL ";

				// contentDisposition
				string contDispos = MimeParser.ParseHeaderField("Content-Disposition:",ent.Headers);
				if(contDispos.Length > 0){
					str += "(";

					string[] fields = contDispos.Split(';');

					str += "\"" + fields[0] + "\" ";

					if(fields.Length > 1){
						str += "(";
						for(int i=1;i<fields.Length;i++){
							string[] nameValue = fields[i].Replace("\"","").Trim().Split(new char[]{'='},2);

							str += "\"" + nameValue[0] + "\" \"" + nameValue[1] + "\"";

							if(i < fields.Length - 1){
								str += " ";
							}
						}
						str += ")";
					}
					else{
						str += "NIL";
					}

					str += ") ";
				}
				else{
					str += "NIL ";
				}

				// contentLanguage
				str += "NIL";
			}

			str += ")";

			return str;
		}