/// <summary>
        ///
        /// </summary>
        public static byte[] Encode(byte[] data, DataCoding coding)
        {
            if (coding == DataCoding.Unicode)
            {
                throw new ArgumentException();
            }

            string t = string.Empty;

            for (int i = 0; i < data.Length; i++)
            {
                t = ToBinaryString(data[i]).Substring((int)coding) + t;
            }

            if ((t.Length % 8) != 0)
            {
                t = t.PadLeft(t.Length + (8 - (t.Length % 8)), '0');
            }

            byte[] r = new byte[(t.Length / 8)];
            for (int i = 0; i < r.Length; i++)
            {
                r[i] = BinaryToByte(t.Substring(t.Length - 8 - (8 * i), 8));
            }

            return(r);
        }
        /// <summary>
        ///
        /// </summary>
        public static byte[] Convert(byte[] data, DataCoding sourceCoding, DataCoding targetCoding)
        {
            if ((sourceCoding == DataCoding.Unicode) || (targetCoding == DataCoding.Unicode))
            {
                throw new ArgumentException();
            }

            string t = string.Empty;

            for (int i = 0; i < data.Length; i++)
            {
                t = ToBinaryString(data[i]).Substring((int)sourceCoding) + t;
            }

            int q = 8 - (int)targetCoding;

            if ((t.Length % q) != 0)
            {
                t = t.PadLeft(t.Length + (q - (t.Length % q)), '0');
            }

            byte[] r = new byte[(t.Length / q)];
            for (int i = 0; i < r.Length; i++)
            {
                r[i] = BinaryToByte(t.Substring(t.Length - q - (q * i), q).PadLeft(8, '0'));
            }

            return(r);
        }
        /// <summary>
        ///
        /// </summary>
        public static byte[] Decode(byte[] data, DataCoding coding)
        {
            if (coding == DataCoding.Unicode)
            {
                throw new ArgumentException();
            }

            string t = string.Empty;

            for (int i = 0; i < data.Length; i++)
            {
                t = ToBinaryString(data[i]) + t;
            }

            int m = 8 - (int)coding;

            byte[] r = new byte[(t.Length - (t.Length % m)) / m];
            for (int i = 0; i < r.Length; i++)
            {
                r[i] = BinaryToByte(t.Substring(t.Length - m - (m * i), m).PadLeft(8, '0'));
            }

            if (r[r.Length - 1] == 0)
            {
                byte[] s = new byte[r.Length - 1];
                for (int i = 0; i < s.Length; i++)
                {
                    s[i] = r[i];
                }

                return(s);
            }

            return(r);
        }
Exemple #4
0
        /// <summary>
        /// Inserts the short message into the PDU ArrayList.
        /// </summary>
        /// <param name="pdu">The PDU to put the short message into.</param>
        /// <param name="ShortMessage">The short message to insert.</param>
        /// <returns>The length of the short message.</returns>
        public static byte InsertShortMessage(ArrayList pdu, DataCoding coding, object ShortMessage)
        {
            byte[] msg;

            if (ShortMessage == null)
            {
                msg = null;
            }
            else if (ShortMessage is byte[])
            {
                msg = (byte[])ShortMessage;
            }
            else if (ShortMessage is string)
            {
                msg = GetEncodedText(coding, ShortMessage as string);
            }
            else
            {
                throw new ArgumentException("Short Message must be a string or byte array.");
            }
            //			if(msg.Length >= MessageLcd2.SHORT_MESSAGE_LIMIT)
            //				throw new ArgumentException(
            //					"Short message cannot be longer than " +
            //					MessageLcd2.SHORT_MESSAGE_LIMIT + " octets.");

            byte SmLength = msg == null ? (byte)0 : (byte)msg.Length;

            pdu.Add(SmLength);
            if (msg != null)
            {
                pdu.AddRange(msg);
            }

            return(SmLength);
        }
Exemple #5
0
        private static byte[] EncodeString(DataCoding dataCoding, string str)
        {
            byte[] bytes;
            switch (dataCoding)
            {
            case DataCoding.Ascii:
                bytes = Encoding.ASCII.GetBytes(str);
                break;

            case DataCoding.Latin1:
                bytes = Latin1Encoding.GetBytes(str);
                break;

            case DataCoding.Ucs2:
                bytes = Encoding.BigEndianUnicode.GetBytes(str);
                break;

            case DataCoding.SmscDefault:
                bytes = SmscDefaultEncoding.GetBytes(str);
                break;

            default:
                throw new SmppException(SmppErrorCode.EsmeRunknownerr, "Unsupported encoding");
            }
            return(bytes);
        }
Exemple #6
0
        public static string GetStringFromBytes(byte[] data, DataCoding dataCoding)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            string result = null;

            switch (dataCoding)
            {
            case DataCoding.ASCII:
                result = System.Text.Encoding.ASCII.GetString(data);
                break;

            case DataCoding.Latin1:
                result = Latin1Encoding.GetString(data);
                break;

            case DataCoding.SMSCDefault:
                result = SMSCDefaultEncoding.GetString(data);
                break;

            case DataCoding.UCS2:
                result = UCS2Encoding.GetString(data);
                break;

            default:
                throw new SmppException(SmppErrorCode.ESME_RUNKNOWNERR, "Unsupported encoding");
            }
            //Since a CString may contain a null terminating charactor
            //Replace all occurences of null charactors
            return(result.Replace("\u0000", ""));
        }
Exemple #7
0
        public virtual byte[] GetBytesFromString(string cStr, DataCoding dataCoding)
        {
            if (cStr == null)
            {
                throw new ArgumentNullException("cStr");
            }
            if (cStr.Length == 0)
            {
                return(new byte[] { 0x00 });
            }
            byte[] bytes = null;
            switch (dataCoding)
            {
            case DataCoding.ASCII:
                bytes = System.Text.Encoding.ASCII.GetBytes(cStr);
                break;

            case DataCoding.Latin1:
                bytes = Latin1Encoding.GetBytes(cStr);
                break;

            case DataCoding.UCS2:
                bytes = UCS2Encoding.GetBytes(cStr);
                break;

            case DataCoding.SMSCDefault:
                bytes = SMSCDefaultEncoding.GetBytes(cStr);
                break;

            default:
                throw new SmppException(SmppErrorCode.ESME_RUNKNOWNERR, "Unsupported encoding");
            }
            return(bytes);
        }
Exemple #8
0
 public static byte[] GetBytesFromCString(string cStr, DataCoding dataCoding)
 {
     if (cStr == null) { throw new ArgumentNullException("cStr"); }
     if (cStr.Length == 0) { return new byte[] { 0x00 }; }
     byte[] bytes = null;
     switch (dataCoding)
     {
         case DataCoding.ASCII:
             bytes = System.Text.Encoding.ASCII.GetBytes(cStr);
             break;
         case DataCoding.Latin1:
             bytes = Latin1Encoding.GetBytes(cStr);
             break;
         case DataCoding.UCS2:
             bytes = System.Text.Encoding.Unicode.GetBytes(cStr);
             break;
         case DataCoding.SMSCDefault:
             bytes = SMSCDefaultEncoding.GetBytes(cStr);
             break;
         default:
             throw new SmppException(SmppErrorCode.ESME_RUNKNOWNERR, "Unsupported encoding");
     }
     ByteBuffer buffer = new ByteBuffer(bytes, bytes.Length + 1);
     buffer.Append(new byte[] { 0x00 }); //Append a null charactor a the end
     return buffer.ToBytes();
 }
Exemple #9
0
        private static string DecodeString(byte[] data, DataCoding dataCoding)
        {
            string result;

            switch (dataCoding)
            {
            case DataCoding.Ascii:
                result = Encoding.ASCII.GetString(data);
                break;

            case DataCoding.Latin1:
                result = Latin1Encoding.GetString(data);
                break;

            case DataCoding.Ucs2:
                result = Encoding.BigEndianUnicode.GetString(data);
                break;

            case DataCoding.SmscDefault:
                result = SmscDefaultEncoding.GetString(data);
                break;

            default:
                throw new SmppException(SmppErrorCode.EsmeRunknownerr, "Unsupported encoding");
            }
            return(result);
        }
Exemple #10
0
        // FIXME: This cannot be computed on a fixed way, as it depends on the encodings
        //	  supported as default/unspec-A/unspec-B of the remote server.
        //	  as such, we should me all this logic to SmppCommunicator and add it it
        //	  properties defining the actual value for each 'dinamic' encoding.

        /// <summary>
        /// Gets the maximum length of each segment of a concatenated
        /// message of totalBytes size using the specified data_coding.
        /// </summary>
        /// <param name="coding">The coding.</param>
        /// <param name="totalbytes">The totalbytes.</param>
        /// <returns></returns>
        public static int GetMaxSegmentLength(DataCoding coding, int totalbytes)
        {
            switch (coding)
            {
            case DataCoding.IA5_ASCII:
            case DataCoding.SMSCDefault:
                return(totalbytes <= 160 ? 160 : 153);

            case DataCoding.UCS2:
            //return totalbytes <= 70 ? 70 : 67;
            case DataCoding.Latin1:
            case DataCoding.OctetUnspecifiedA:
            case DataCoding.OctetUnspecifiedB:
            case DataCoding.Cyrillic:
            case DataCoding.ExtendedKanjiJIS:
            case DataCoding.JIS:
            case DataCoding.KS_C:
            case DataCoding.Latin_Hebrew:
            case DataCoding.MusicCodes:
            case DataCoding.Pictogram:
                return(totalbytes <= 140 ? 140 : 134);

            default:
                throw new InvalidOperationException("Invalid or unsuported encoding for text message ");
            }
        }
Exemple #11
0
 /// <summary>
 /// Gets the decoded representation of the specified data.
 /// </summary>
 /// <param name="coding">The coding.</param>
 /// <param name="data">The data.</param>
 /// <returns></returns>
 public static string GetDecodedText(DataCoding coding, byte[] data)
 {
     switch (coding)
     {
         case DataCoding.SMSCDefault:
         //return GSM7BitEncoding.GetBytes(text);
         case DataCoding.OctetUnspecifiedA:
         case DataCoding.OctetUnspecifiedB:
             return new GSMEncoding().GetString(data);
         case DataCoding.IA5_ASCII:
             return Encoding.ASCII.GetString(data);
         case DataCoding.Latin1:
             return Encoding.GetEncoding("iso-8859-1").GetString(data);
         case DataCoding.JIS:
         case DataCoding.ExtendedKanjiJIS:
             return Encoding.GetEncoding("EUC-JP").GetString(data);
         case DataCoding.Cyrillic:
             return Encoding.GetEncoding("iso-8859-5").GetString(data);
         case DataCoding.Latin_Hebrew:
             return Encoding.GetEncoding("iso-8859-8").GetString(data);
         case DataCoding.UCS2:
             // 1201 == Unicode Big Endian (FFFE)!
             return Encoding.GetEncoding(1201).GetString(data);
         case DataCoding.MusicCodes:
             return Encoding.GetEncoding("iso-2022-jp").GetString(data);
         case DataCoding.KS_C:
             return Encoding.GetEncoding("ks_c_5601-1987").GetString(data);
         default:
             throw new ArgumentException("Invalid (or unsupported) DataCoding value.");
     }
 }
Exemple #12
0
 internal SendSmPDU(PDUHeader header)
     : base(header)
 {
     vServiceType        = "";
     vEsmClass           = EsmClass.Default;
     vRegisteredDelivery = RegisteredDelivery.None;
     vDataCoding         = DataCoding.ASCII;
 }
Exemple #13
0
 internal SendSmPdu(PduHeader header)
     : base(header)
 {
     VServiceType        = "";
     VEsmClass           = EsmClass.Default;
     VRegisteredDelivery = RegisteredDelivery.None;
     VDataCoding         = DataCoding.Ascii;
 }
Exemple #14
0
 internal SendSmPDU(PDUHeader header)
     : base(header)
 {
     vServiceType = "";
     vEsmClass = EsmClass.Default;
     vRegisteredDelivery = RegisteredDelivery.None;
     vDataCoding = DataCoding.ASCII;
 }
Exemple #15
0
        public static string GetStringFromBytes(byte[] data, DataCoding dataCoding)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            string result = DecodeString(data, dataCoding);

            //Since a CString may contain a null terminating charactor
            //Replace all occurences of null charactors
            return(result.Replace("\u0000", ""));
        }
Exemple #16
0
 public static byte[] GetBytesFromString(string cStr, DataCoding dataCoding)
 {
     if (cStr == null)
     {
         throw new ArgumentNullException("cStr");
     }
     if (cStr.Length == 0)
     {
         return(new byte[] { 0x00 });
     }
     return(EncodeString(dataCoding, cStr));
 }
Exemple #17
0
        public void Can_Fragment_Using_Udh(int length, DataCoding coding, int segmentsNumber)
        {
            var message = new String(Enumerable.Repeat('A', length).ToArray());

            var submitSm = new SmppSubmitSm()
            {
                DataCoding   = coding,
                ShortMessage = message,
            };

            var segments = SmppUtil.SplitLongMessage(submitSm, SmppSarMethod.UserDataHeader, 0x1);

            Assert.AreEqual(segmentsNumber, segments.Count());
        }
Exemple #18
0
        protected override IEnumerable <SendSmPDU> GetPDUs(DataCoding defaultEncoding)
        {
            SubmitSm sm = CreateSubmitSm();

            sm.SourceAddress.Address      = vSourceAddress;
            sm.DestinationAddress.Address = vDestinatinoAddress; // Urgh, typo :(
            sm.DataCoding = defaultEncoding;
            if (!string.IsNullOrEmpty(UserMessageReference))
            {
                var msgIdBytes = SMPPEncodingUtil.GetBytesFromCString(UserMessageReference);
                sm.Tlv.Add(new Lib.Protocol.Tlv.Tlv(Lib.Protocol.Tlv.Tag.user_message_reference, (ushort)msgIdBytes.Length, msgIdBytes));
            }

            if (vRegisterDeliveryNotification)
            {
                sm.RegisteredDelivery = RegisteredDelivery.DeliveryReceipt;
            }

            vMaxMessageLength = GetMaxMessageLength(defaultEncoding, false);
            byte[] bytes = SMPPEncodingUtil.GetBytesFromString(vText, defaultEncoding);

            string uni   = Encoding.Unicode.GetString(bytes);
            string ascii = Encoding.ASCII.GetString(bytes);
            string utf8  = Encoding.UTF8.GetString(bytes);

            // Unicode encoding return 2 items for 1 char
            // We check vText Length first
            if (vText.Length > vMaxMessageLength && bytes.Length > vMaxMessageLength) // Split into multiple!
            {
                var SegID = new Random().Next(1000, 9999);                            // create random SegmentID
                vMaxMessageLength = GetMaxMessageLength(defaultEncoding, true);
                var messages      = Split(vText, vMaxMessageLength);
                var totalSegments = messages.Count;                   // get the number of (how many) parts
                var udh           = new Udh(SegID, totalSegments, 0); // ID, Total, part

                for (int i = 0; i < totalSegments; i++)
                {
                    udh.MessageSequence = i + 1;                          // seq+1 , - parts of the message
                    sm.SetMessageText(messages[i], defaultEncoding, udh); // send parts of the message + all other UDH settings
                    yield return(sm);
                }
            }
            else
            {
                sm.SetMessageBytes(bytes);
                yield return(sm);
            }
        }
Exemple #19
0
        public virtual void SetMessageText(string message, DataCoding dataCoding, Udh udh)
        {
            ByteBuffer buffer = new ByteBuffer(160);

            if (udh != null)
            {
                buffer.Append(udh.GetBytes());
            }
            buffer.Append(SMPPEncodingUtil.GetBytesFromString(message, dataCoding));
            SetMessageBytes(buffer.ToBytes());
            if (udh != null)
            {
                EsmClass = EsmClass | EsmClass.UdhiIndicator;
            }
            DataCoding = dataCoding;
        }
Exemple #20
0
        public static byte[] GetBytesFromCString(string cStr, DataCoding dataCoding)
        {
            if (cStr == null)
            {
                throw new ArgumentNullException("cStr");
            }
            if (cStr.Length == 0)
            {
                return(new byte[] { 0x00 });
            }
            byte[]     bytes  = EncodeString(dataCoding, cStr);
            ByteBuffer buffer = new ByteBuffer(bytes, bytes.Length + 1);

            buffer.Append(new byte[] { 0x00 }); //Append a null charactor a the end
            return(buffer.ToBytes());
        }
        public virtual byte[] GetBytesFromCString(string cStr, DataCoding dataCoding, bool nullTerminated = true)
        {
            if (cStr == null)
            {
                throw new ArgumentNullException("cStr");
            }
            if (cStr.Length == 0)
            {
                return(new byte[] { 0x00 });
            }
            byte[] bytes = null;
            switch (dataCoding)
            {
            case DataCoding.ASCII:
                bytes = System.Text.Encoding.ASCII.GetBytes(cStr);
                break;

            case DataCoding.Latin1:
                bytes = Latin1Encoding.GetBytes(cStr);
                break;

            case DataCoding.UCS2:
                bytes = UCS2Encoding.GetBytes(cStr);
                break;

            case DataCoding.SMSCDefault:
                bytes = SMSCDefaultEncoding.GetBytes(cStr);
                break;

            default:
                throw new SmppException(SmppErrorCode.ESME_RUNKNOWNERR, "Unsupported encoding");
            }
            ByteBuffer buffer;

            if (nullTerminated)
            {
                buffer = new ByteBuffer(bytes, bytes.Length + 1);
                buffer.Append(new byte[] { 0x00 }); //Append a null charactor a the end
            }
            else
            {
                buffer = new ByteBuffer(bytes, bytes.Length);
            }

            return(buffer.ToBytes());
        }
Exemple #22
0
        public static string GetCStringFromBytes(byte[] data, DataCoding dataCoding)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (data.Length < 1)
            {
                throw new ArgumentException("Array cannot be empty", "data");
            }
            if (data[data.Length - 1] != 0x00)
            {
                throw new ArgumentException("CString must be terminated with a null charactor", "data");
            }
            if (data.Length == 1)
            {
                return("");
            }                                    //The string is empty if it contains a single null charactor
            string result = null;

            switch (dataCoding)
            {
            case DataCoding.ASCII:
                result = System.Text.Encoding.ASCII.GetString(data);
                break;

            case DataCoding.Latin1:
                result = Latin1Encoding.GetString(data);
                break;

            case DataCoding.SMSCDefault:
                result = SMSCDefaultEncoding.GetString(data);
                break;

            case DataCoding.UCS2:
                result = UCS2Encoding.GetString(data);
                break;

            default:
                throw new SmppException(SmppErrorCode.ESME_RUNKNOWNERR, "Unsupported encoding");
            }
            return(result.Replace("\x00", ""));//Replace the terminating null charactor
        }
Exemple #23
0
        private static int GetMaxMessageLength(DataCoding encoding, bool includeUdh)
        {
            switch (encoding)
            {
            case DataCoding.SMSCDefault:
                return(includeUdh ? 153 : 160);

            case DataCoding.Latin1:
                return(includeUdh ? 134 : 140);

            case DataCoding.ASCII:
                return(includeUdh ? 153 : 160);

            case DataCoding.UCS2:
                return(includeUdh ? 67 : 70);

            default:
                throw new InvalidOperationException("Invalid or unsuported encoding for text message ");
            }
        }
Exemple #24
0
        string Read(int Bytes, int TimeOut)
        {
            int    count;
            string str = string.Empty;

            lock (dataIn.SyncRoot) {
                count = dataIn.Count;
                if (((Bytes > 0) && (count < Bytes)) || ((Bytes == 0) && (TimeOut > 0)))
                {
                    if (Bytes == 0)
                    {
                        Bytes = -1;
                    }
                    waitBytes = Bytes;
                    allDone.Reset();
                }
                else
                {
                    allDone.Set();
                }
            }
            allDone.WaitOne(TimeOut, true);
            count = dataIn.Count;
            if (count == 0)
            {
                return(string.Empty);
            }
            if (Bytes <= 0)
            {
                str = DataCoding.GetString(dataIn.ToArray(0, count));
                dataIn.RemoveRange(0, count);
                return(str);
            }
            if ((Bytes > 0) && (count < Bytes))
            {
                return(string.Empty);
            }
            str = DataCoding.GetString(dataIn.ToArray(0, Bytes));
            dataIn.RemoveRange(0, Bytes);
            return(str);
        }
Exemple #25
0
 protected override IEnumerable<SendSmPDU> GetPDUs(DataCoding defaultEncoding)
 {
     //This smpp implementation does not support sending concatenated messages,
     //however, concatenated messages are supported on the receiving side.
     int maxLength = GetMaxMessageLength(defaultEncoding, false);
     byte[] bytes = SMPPEncodingUtil.GetBytesFromString(vText, defaultEncoding);
     //Check message size
     if(bytes.Length > maxLength)
     {
         throw new InvalidOperationException(string.Format(
             "Encoding '{0}' does not support messages of length greater than '{1}' charactors",
             defaultEncoding, maxLength));
     }
     SubmitSm sm = new SubmitSm();
     sm.SetMessageBytes(bytes);
     sm.SourceAddress.Address = vSourceAddress;
     sm.DestinationAddress.Address = vDestinatinoAddress;
     sm.DataCoding = defaultEncoding;
     if (vRegisterDeliveryNotification) { sm.RegisteredDelivery = RegisteredDelivery.DeliveryReceipt; }
     yield return sm;
 }
Exemple #26
0
        /// <summary>
        /// Gets the encoded representation of the specified text.
        /// </summary>
        /// <param name="coding">The coding.</param>
        /// <param name="text">The text.</param>
        /// <returns></returns>
        public static byte[] GetEncodedText(DataCoding coding, string text)
        {
            switch (coding)
            {
            case DataCoding.SMSCDefault:
            //return GSM7BitEncoding.GetBytes(text);
            case DataCoding.OctetUnspecifiedA:
            case DataCoding.OctetUnspecifiedB:
                return(new GSMEncoding(true, false).GetBytes(text));

            case DataCoding.IA5_ASCII:
                return(Encoding.ASCII.GetBytes(text));

            case DataCoding.Latin1:
                return(Encoding.GetEncoding("iso-8859-1").GetBytes(text));

            case DataCoding.JIS:
            case DataCoding.ExtendedKanjiJIS:
                return(Encoding.GetEncoding("EUC-JP").GetBytes(text));

            case DataCoding.Cyrillic:
                return(Encoding.GetEncoding("iso-8859-5").GetBytes(text));

            case DataCoding.Latin_Hebrew:
                return(Encoding.GetEncoding("iso-8859-8").GetBytes(text));

            case DataCoding.UCS2:
                // 1201 == Unicode Big Endian (FFFE)!
                return(Encoding.GetEncoding(1201).GetBytes(text));

            case DataCoding.MusicCodes:
                return(Encoding.GetEncoding("iso-2022-jp").GetBytes(text));

            case DataCoding.KS_C:
                return(Encoding.GetEncoding("ks_c_5601-1987").GetBytes(text));

            default:
                throw new ArgumentException("Invalid (or unsupported) DataCoding value.");
            }
        }
Exemple #27
0
        /// <summary>
        /// Gets the decoded representation of the specified data.
        /// </summary>
        /// <param name="coding">The coding.</param>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public static string GetDecodedText(DataCoding coding, byte[] data)
        {
            switch (coding)
            {
            case DataCoding.SmscDefault:
            //return GSM7BitEncoding.GetBytes(text);
            case DataCoding.OctetUnspecifiedA:
            case DataCoding.OctetUnspecifiedB:
                return(new GsmEncoding().GetString(data));

            case DataCoding.Ia5Ascii:
                return(Encoding.ASCII.GetString(data));

            case DataCoding.Latin1:
                return(Encoding.GetEncoding("iso-8859-1").GetString(data));

            case DataCoding.Jis:
            case DataCoding.ExtendedKanjiJis:
                return(Encoding.GetEncoding("EUC-JP").GetString(data));

            case DataCoding.Cyrillic:
                return(Encoding.GetEncoding("iso-8859-5").GetString(data));

            case DataCoding.LatinHebrew:
                return(Encoding.GetEncoding("iso-8859-8").GetString(data));

            case DataCoding.Ucs2:
                // 1201 == Unicode Big Endian (FFFE)!
                return(Encoding.GetEncoding(1201).GetString(data));

            case DataCoding.MusicCodes:
                return(Encoding.GetEncoding("iso-2022-jp").GetString(data));

            case DataCoding.KsC:
                return(Encoding.GetEncoding("ks_c_5601-1987").GetString(data));

            default:
                throw new ArgumentException("Invalid (or unsupported) DataCoding value.");
            }
        }
Exemple #28
0
        public static string GetCStringFromBytes(byte[] data, DataCoding dataCoding)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (data.Length < 1)
            {
                throw new ArgumentException("Array cannot be empty", "data");
            }
            if (data[data.Length - 1] != 0x00)
            {
                throw new ArgumentException("CString must be terminated with a null charactor", "data");
            }
            if (data.Length == 1)
            {
                return("");
            }                                    //The string is empty if it contains a single null charactor
            string result = DecodeString(data, dataCoding);

            return(result.Replace("\x00", ""));//Replace the terminating null charactor
        }
Exemple #29
0
        protected override IEnumerable <SendSmPdu> GetPdUs(DataCoding defaultEncoding)
        {
            SubmitSm sm = new SubmitSm();

            sm.SourceAddress.Address      = VSourceAddress;
            sm.DestinationAddress.Address = VDestinatinoAddress; // Urgh, typo :(
            sm.DataCoding = defaultEncoding;
            if (VRegisterDeliveryNotification)
            {
                sm.RegisteredDelivery = RegisteredDelivery.DeliveryReceipt;
            }

            _vMaxMessageLength = GetMaxMessageLength(defaultEncoding, false);
            byte[] bytes = SmppEncodingUtil.GetBytesFromString(_vText, defaultEncoding);

            // Unicode encoding return 2 items for 1 char
            // We check vText Length first
            if (_vText.Length > _vMaxMessageLength && bytes.Length > _vMaxMessageLength) // Split into multiple!
            {
                var segId = new Random().Next(1000, 9999);                               // create random SegmentID
                _vMaxMessageLength = GetMaxMessageLength(defaultEncoding, true);
                var messages      = Split(_vText, _vMaxMessageLength);
                var totalSegments = messages.Count;                   // get the number of (how many) parts
                var udh           = new Udh(segId, totalSegments, 0); // ID, Total, part

                for (int i = 0; i < totalSegments; i++)
                {
                    udh.MessageSequence = i + 1;                          // seq+1 , - parts of the message
                    sm.SetMessageText(messages[i], defaultEncoding, udh); // send parts of the message + all other UDH settings
                    yield return(sm);
                }
            }
            else
            {
                sm.SetMessageBytes(bytes);
                yield return(sm);
            }
        }
Exemple #30
0
        /// <summary>
        /// Takes the given PDU and inserts a message payload into its TLV table.
        /// </summary>
        /// <param name="pdu">The PDU to operate on.</param>
        /// <param name="val">The value to insert.</param>
        public static void SetMessagePayload(Pdu pdu, DataCoding coding, object val)
        {
            byte[] encodedValue = null;

            if (val == null)
            {
                pdu.SetOptionalParamBytes(OptionalParamCodes.message_payload, null);
            }
            else if (val is string)
            {
                encodedValue = GetEncodedText(coding, val as string);
            }
            else if (val is byte[])
            {
                encodedValue = (byte[])val;
            }
            else
            {
                throw new ArgumentException("Message Payload must be a string or byte array.");
            }

            if (encodedValue != null)
            {
                const int MAX_PAYLOAD_LENGTH = 64000;
                if (encodedValue.Length < MAX_PAYLOAD_LENGTH)
                {
                    pdu.SetOptionalParamBytes(
                        OptionalParamCodes.message_payload, encodedValue);
                }
                else
                {
                    throw new ArgumentException(
                              "Message Payload must be " + MAX_PAYLOAD_LENGTH + " characters or less in size.");
                }
            }
        }
Exemple #31
0
 // FIXME: This cannot be computed on a fixed way, as it depends on the encodings
 //      supported as default/unspec-A/unspec-B of the remote server.
 //      as such, we should me all this logic to SmppCommunicator and add it it
 //      properties defining the actual value for each 'dinamic' encoding.
 /// <summary>
 /// Gets the maximum length of each segment of a concatenated 
 /// message of totalBytes size using the specified data_coding.
 /// </summary>
 /// <param name="coding">The coding.</param>
 /// <param name="totalbytes">The totalbytes.</param>
 /// <returns></returns>
 public static int GetMaxSegmentLength(DataCoding coding, int totalbytes)
 {
     switch (coding)
     {
         case DataCoding.IA5_ASCII:
         case DataCoding.SMSCDefault:
             return totalbytes <= 160 ? 160 : 153;
         case DataCoding.UCS2:
             //return totalbytes <= 70 ? 70 : 67;
         case DataCoding.Latin1:
         case DataCoding.OctetUnspecifiedA:
         case DataCoding.OctetUnspecifiedB:
         case DataCoding.Cyrillic:
         case DataCoding.ExtendedKanjiJIS:
         case DataCoding.JIS:
         case DataCoding.KS_C:
         case DataCoding.Latin_Hebrew:
         case DataCoding.MusicCodes:
         case DataCoding.Pictogram:
             return totalbytes <= 140 ? 140 : 134;
         default:
             throw new InvalidOperationException("Invalid or unsuported encoding for text message ");
     }
 }
Exemple #32
0
        /// <summary>
        /// Takes the given PDU and inserts a message payload into its TLV table.
        /// </summary>
        /// <param name="pdu">The PDU to operate on.</param>
        /// <param name="val">The value to insert.</param>
        public static void SetMessagePayload(Pdu pdu, DataCoding coding, object val)
        {
            byte[] encodedValue = null;

            if (val == null)
            {
                pdu.SetOptionalParamBytes(OptionalParamCodes.message_payload, null);
            }
            else if(val is string)
            {
                encodedValue = GetEncodedText(coding, val as string);
            }
            else if(val is byte[])
            {
                encodedValue =(byte[])val;
            }
            else
            {
                throw new ArgumentException("Message Payload must be a string or byte array.");
            }

            if (encodedValue != null)
            {
                const int MAX_PAYLOAD_LENGTH = 64000;
                if(encodedValue.Length < MAX_PAYLOAD_LENGTH)
                {
                    pdu.SetOptionalParamBytes(
                        OptionalParamCodes.message_payload, encodedValue);
                }
                else
                {
                    throw new ArgumentException(
                        "Message Payload must be " + MAX_PAYLOAD_LENGTH + " characters or less in size.");
                }
            }
        }
Exemple #33
0
 public static string GetStringFromBytes(byte[] data, DataCoding dataCoding)
 {
     if (data == null) { throw new ArgumentNullException("data"); }
     string result = null;
     switch (dataCoding)
     {
         case DataCoding.ASCII:
             result = System.Text.Encoding.ASCII.GetString(data);
             break;
         case DataCoding.Latin1:
             result = Latin1Encoding.GetString(data);
             break;
         case DataCoding.SMSCDefault:
             result = SMSCDefaultEncoding.GetString(data);
             break;
         case DataCoding.UCS2:
             result = System.Text.Encoding.Unicode.GetString(data);
             break;
         default:
             throw new SmppException(SmppErrorCode.ESME_RUNKNOWNERR, "Unsupported encoding");
     }
     //Since a CString may contain a null terminating charactor
     //Replace all occurences of null charactors
     return result.Replace("\u0000","");
 }
Exemple #34
0
 public static string GetCStringFromBytes(byte[] data, DataCoding dataCoding)
 {
     if (data == null) { throw new ArgumentNullException("data"); }
     if (data.Length < 1) { throw new ArgumentException("Array cannot be empty","data"); }
     if (data[data.Length - 1] != 0x00) { throw new ArgumentException("CString must be terminated with a null charactor","data"); }
     if (data.Length == 1) { return ""; } //The string is empty if it contains a single null charactor
     string result = null;
     switch (dataCoding)
     {
         case DataCoding.ASCII:
             result = System.Text.Encoding.ASCII.GetString(data);
             break;
         case DataCoding.Latin1:
             result = Latin1Encoding.GetString(data);
             break;
         case DataCoding.SMSCDefault:
             result = SMSCDefaultEncoding.GetString(data);
             break;
         case DataCoding.UCS2:
             result = System.Text.Encoding.Unicode.GetString(data);
             break;
         default:
             throw new SmppException(SmppErrorCode.ESME_RUNKNOWNERR, "Unsupported encoding");
     }
     return result.Replace("\x00","");//Replace the terminating null charactor
 }
Exemple #35
0
 protected abstract IEnumerable<SendSmPDU> GetPDUs(DataCoding defaultEncoding);
Exemple #36
0
 public void SetMessageText(string message, DataCoding dataCoding)
 {
     SetMessageText(message, dataCoding, null);
 }
Exemple #37
0
 internal IEnumerable <SendSmPdu> GetMessagePdUs(DataCoding defaultEncoding)
 {
     return(GetPdUs(defaultEncoding));
 }
Exemple #38
0
 internal IEnumerable <SendSmPDU> GetMessagePDUs(DataCoding defaultEncoding, SmppEncodingService smppEncodingService)
 {
     return(GetPDUs(defaultEncoding, smppEncodingService));
 }
Exemple #39
0
 private static int GetMaxMessageLength(DataCoding encoding, bool includeUdh)
 {
     switch (encoding)
     {
         case DataCoding.SMSCDefault:
             return includeUdh ? 153 : 160;
         case DataCoding.Latin1:
             return includeUdh ? 134 : 140;
         case DataCoding.ASCII:
             return includeUdh ? 153 : 160;
         case DataCoding.UCS2:
             return includeUdh ? 67 : 70;
         default:
             throw new InvalidOperationException("Invalid or unsuported encoding for text message ");
     }
 }
Exemple #40
0
 internal IEnumerable<SendSmPDU> GetMessagePDUs(DataCoding defaultEncoding)
 {
     return GetPDUs(defaultEncoding);
 }
Exemple #41
0
 public static byte[] GetBytesFromString(string cStr,DataCoding dataCoding)
 {
     if (cStr == null) { throw new ArgumentNullException("cStr"); }
     if (cStr.Length == 0) { return new byte[] { 0x00 }; }
     byte[] bytes = null;
     switch (dataCoding)
     {
         case DataCoding.ASCII:
             bytes = System.Text.Encoding.ASCII.GetBytes(cStr);
             break;
         case DataCoding.Latin1:
             bytes = Latin1Encoding.GetBytes(cStr);
             break;
         case DataCoding.UCS2:
             bytes = System.Text.Encoding.Unicode.GetBytes(cStr);
             break;
         case DataCoding.SMSCDefault:
             bytes = SMSCDefaultEncoding.GetBytes(cStr);
             break;
         default:
             throw new SmppException(SmppErrorCode.ESME_RUNKNOWNERR, "Unsupported encoding");
     }
     return bytes;
 }
Exemple #42
0
        /// <summary>
        /// Inserts the short message into the PDU ArrayList.
        /// </summary>
        /// <param name="pdu">The PDU to put the short message into.</param>
        /// <param name="ShortMessage">The short message to insert.</param>
        /// <returns>The length of the short message.</returns>
        public static byte InsertShortMessage(ArrayList pdu, DataCoding coding, object ShortMessage)
        {
            byte[] msg;

            if(ShortMessage == null)
            {
                msg = null;
            }
            else if(ShortMessage is byte[])
            {
                msg =(byte[])ShortMessage;
            }
            else if (ShortMessage is string)
            {
                msg = GetEncodedText(coding, ShortMessage as string);
            }
            else
            {
                throw new ArgumentException("Short Message must be a string or byte array.");
            }
            //			if(msg.Length >= MessageLcd2.SHORT_MESSAGE_LIMIT)
            //				throw new ArgumentException(
            //					"Short message cannot be longer than " +
            //					MessageLcd2.SHORT_MESSAGE_LIMIT + " octets.");

            byte SmLength = msg == null ? (byte)0 : (byte)msg.Length;
            pdu.Add(SmLength);
            if (msg != null) pdu.AddRange(msg);

            return SmLength;
        }
Exemple #43
0
 public virtual void SetMessageText(string message, DataCoding dataCoding, Udh udh)
 {
     ByteBuffer buffer = new ByteBuffer(160);
     if (udh != null) { buffer.Append(udh.GetBytes()); }
     buffer.Append(SMPPEncodingUtil.GetBytesFromString(message, dataCoding));
     SetMessageBytes(buffer.ToBytes());
     if (udh != null) { EsmClass = EsmClass | EsmClass.UdhiIndicator; }
     DataCoding = dataCoding;
 }
Exemple #44
0
 protected abstract IEnumerable <SendSmPDU> GetPDUs(DataCoding defaultEncoding, SmppEncodingService smppEncodingService);
Exemple #45
0
 protected abstract IEnumerable <SendSmPdu> GetPdUs(DataCoding defaultEncoding);