/// <summary> /// Does period handling. /// </summary> /// <param name="strm">Input stream.</param> /// <param name="add_Remove">If true add periods, else removes periods.</param> /// <param name="setStrmPosTo0">If true sets stream position to 0.</param> /// <returns></returns> public static MemoryStream DoPeriodHandling(Stream strm, bool add_Remove, bool setStrmPosTo0) { MemoryStream replyData = new MemoryStream(); byte[] crlf = new byte[] { (byte)'\r', (byte)'\n' }; if (setStrmPosTo0) { strm.Position = 0; } StreamLineReader r = new StreamLineReader(strm); byte[] line = r.ReadLine(); // Loop through all lines while (line != null) { if (line.Length > 0) { if (line[0] == (byte)'.') { /* Add period Rfc 2821 4.5.2 * - Before sending a line of mail text, the SMTP client checks the * first character of the line. If it is a period, one additional * period is inserted at the beginning of the line. */ if (add_Remove) { replyData.WriteByte((byte)'.'); replyData.Write(line, 0, line.Length); } /* Remove period Rfc 2821 4.5.2 * If the first character is a period , the first characteris deleted. */ else { replyData.Write(line, 1, line.Length - 1); } } else { replyData.Write(line, 0, line.Length); } } replyData.Write(crlf, 0, crlf.Length); // Read next line line = r.ReadLine(); } replyData.Position = 0; return(replyData); }
/// <summary> /// Parses relay info from stream. /// </summary> /// <param name="relayMsgStrm"></param> private void ReadRelayInfo(Stream relayMsgStrm) { StreamLineReader reader = new StreamLineReader(relayMsgStrm); string relayHead = System.Text.Encoding.ASCII.GetString(reader.ReadLine()); if(relayHead != null && relayHead.StartsWith("RelayInfo:")){ relayHead = relayHead.Replace("RelayInfo:",""); relayHead = relayHead.Trim(); string[] param = relayHead.Split(new char[]{'\t'}); if(param.Length == 4){ m_IsWSent = Convert.ToBoolean(Convert.ToInt32(param[0])); m_To = param[1]; m_From = param[2]; m_MsgDate = DateTime.ParseExact(param[3],"r",System.Globalization.DateTimeFormatInfo.InvariantInfo); } m_MsgStartPos = (int)relayMsgStrm.Position; } }
/// <summary> /// Does period handling. /// </summary> /// <param name="strm">Input stream.</param> /// <param name="add_Remove">If true add periods, else removes periods.</param> /// <param name="setStrmPosTo0">If true sets stream position to 0.</param> /// <returns></returns> public static MemoryStream DoPeriodHandling(Stream strm,bool add_Remove,bool setStrmPosTo0) { MemoryStream replyData = new MemoryStream(); byte[] crlf = new byte[]{(byte)'\r',(byte)'\n'}; if(setStrmPosTo0){ strm.Position = 0; } StreamLineReader r = new StreamLineReader(strm); byte[] line = r.ReadLine(); // Loop through all lines while(line != null){ if(line.Length > 0){ if(line[0] == (byte)'.'){ /* Add period Rfc 281 4.5.2 - Before sending a line of mail text, the SMTP client checks the first character of the line. If it is a period, one additional period is inserted at the beginning of the line. */ if(add_Remove){ replyData.WriteByte((byte)'.'); replyData.Write(line,0,line.Length); } /* Remove period Rfc 281 4.5.2 If the first character is a period , the first characteris deleted. */ else{ replyData.Write(line,1,line.Length-1); } } else{ replyData.Write(line,0,line.Length); } } // write enc stuff here miceli replyData.Write(crlf,0,crlf.Length); // Read next line line = r.ReadLine(); } replyData.Position = 0; return replyData; }
/// <summary> /// Scans invalid CR or LF combination in stream. Returns true if contains invalid CR or LF combination. /// </summary> /// <param name="strm">Stream which to check.</param> /// <returns>Returns true if contains invalid CR or LF combination.</returns> public static bool ScanInvalid_CR_or_LF(Stream strm) { StreamLineReader lineReader = new StreamLineReader(strm); byte[] line = lineReader.ReadLine(); while(line != null){ foreach(byte b in line){ // Contains CR or LF. It cannot conatian such sumbols, because CR must be paired with LF // and we currently reading lines with CRLF combination. if(b == 10 || b == 13){ return true; } } line = lineReader.ReadLine(); } return false; }
/// <summary> /// Adds column to db file. /// </summary> /// <param name="column"></param> internal void AddColumn(LDB_DataColumn column) { // Find free column data area // Set position over version, free data pages count and data page data area size m_pDbFile.Position = 68; long freeColumnPosition = -1; StreamLineReader r = new StreamLineReader(m_pDbFile); // Loop all columns data areas, see it there any free left for(int i=0;i<100;i++){ byte[] columnInfo = r.ReadLine(); if(columnInfo == null){ throw new Exception("Invalid columns data area length !"); } // We found unused column data area if(columnInfo[0] == '\0'){ freeColumnPosition = m_pDbFile.Position; break; } } m_FilePosition = m_pDbFile.Position; if(freeColumnPosition != -1){ // TODO: If there is data ??? // Move to row start SetFilePosition(GetFilePosition() - 102); // Store column byte[] columnData = column.ToColumnInfo(); WriteToFile(columnData,0,columnData.Length); } else{ throw new Exception("Couldn't find free column space ! "); } }
/// <summary> /// Opens specified data file. /// </summary> /// <param name="fileName">File name.</param> /// <param name="waitTime">If data base file is exclusively locked, then how many seconds to wait file to unlock before raising a error.</param> public void Open(string fileName,int waitTime) { DateTime lockExpireTime = DateTime.Now.AddSeconds(waitTime); while(true){ try{ m_pDbFile = File.Open(fileName,FileMode.Open,FileAccess.ReadWrite,FileShare.ReadWrite); break; } catch(IOException x){ // Make this because to get rid of "The variable 'x' is declared but never used" string dummy = x.Message; System.Threading.Thread.Sleep(15); // Lock wait time timed out if(DateTime.Now > lockExpireTime){ throw new Exception("Database file is locked and lock wait time expired !"); } } } /* Table structure: 50 bytes - version 2 bytes - CRLF 8 bytes - free datapages count 2 bytes - CRLF 4 bytes - datapage data area size 2 bytes - CRLF 100 x 500 bytes - 100 columns info store 2 bytes - CRLF ... data pages */ m_DbFileName = fileName; StreamLineReader r = new StreamLineReader(m_pDbFile); // TODO: check if LDB file // Read version line (50 bytes + CRLF) byte[] version = r.ReadLine(); // Skip free data pages count byte[] freeDataPagesCount = new byte[10]; m_pDbFile.Read(freeDataPagesCount,0,freeDataPagesCount.Length); // 4 bytes datapage data area size + CRLF byte[] dataPageDataAreaSize = new byte[6]; m_pDbFile.Read(dataPageDataAreaSize,0,dataPageDataAreaSize.Length); m_DataPageDataAreaSize = ldb_Utils.ByteToInt(dataPageDataAreaSize,0); // Read 100 column lines (500 + CRLF bytes each) for(int i=0;i<100;i++){ byte[] columnInfo = r.ReadLine(); if(columnInfo == null){ throw new Exception("Invalid columns data area length !"); } if(columnInfo[0] != '\0'){ m_pColumns.Parse(columnInfo); } } // Header terminator \0 m_pDbFile.Position++; // No we have rows start offset m_DatapagesStartOffset = m_pDbFile.Position; // Store file length and position m_FileLength = m_pDbFile.Length; m_FilePosition = m_pDbFile.Position; }