sms-pdu-decoder

Crates.iosms-pdu-decoder
lib.rssms-pdu-decoder
version0.1.1
created_at2025-11-28 18:46:24.076656+00
updated_at2025-11-28 18:52:58.769709+00
descriptionA Rust library for decoding SMS PDU (Protocol Data Unit) format as specified in GSM 03.40
homepage
repositoryhttps://github.com/fazalrasel/sms-pdu-decoder
max_upload_size
id1955866
size90,905
Fazal Rasel (fazalrasel)

documentation

README

SMS PDU Decoder

Crates.io Documentation License: MIT

A Rust library for decoding SMS PDU (Protocol Data Unit) format as specified in GSM 03.40.

This is a Rust port of the Python library smspdudecoder by Sergey Yenchuk.

Features

  • 📱 Decode SMS-DELIVER (incoming) messages
  • 📤 Decode SMS-SUBMIT (outgoing) messages
  • 🔤 Support for GSM 7-bit encoding (including extended characters)
  • 🌍 Support for UCS-2 (Unicode) encoding
  • 📦 Support for concatenated (multi-part) messages
  • ⏰ Timezone-aware timestamp parsing
  • 🛡️ Handles truncated PDUs gracefully

Installation

Add this to your Cargo.toml:

[dependencies]
sms-pdu-decoder = "0.1.0"

Usage

Simple API

For quick decoding of incoming SMS messages:

use sms_pdu_decoder::read_incoming_sms;

fn main() {
    let pdu = "07916407058099F9040B916407950303F100008921222140140004D4E2940A";
    
    match read_incoming_sms(pdu) {
        Ok(sms) => {
            println!("From: {}", sms.sender);
            println!("Message: {}", sms.content);
            println!("Date: {}", sms.date);
        }
        Err(e) => eprintln!("Error: {}", e),
    }
}

Detailed API

For more control and access to all PDU fields:

use sms_pdu_decoder::SmsDeliver;

fn main() {
    let pdu = "07916407058099F9040B916407950303F100008921222140140004D4E2940A";
    
    match SmsDeliver::decode(pdu) {
        Ok(sms) => {
            println!("SMSC: {}", sms.smsc.number.unwrap_or_default());
            println!("Sender: {}", sms.sender.number);
            println!("Encoding: {}", sms.dcs.encoding);
            println!("Timestamp: {}", sms.scts);
            println!("Message: {}", sms.user_data.data);
        }
        Err(e) => eprintln!("Error: {}", e),
    }
}

Direct Codec Usage

You can also use the codecs directly:

use sms_pdu_decoder::{Gsm7Bit, Ucs2};

fn main() {
    // GSM 7-bit decoding
    let text = Gsm7Bit::decode("C8F71D14969741F977FD07", false).unwrap();
    println!("GSM 7-bit: {}", text); // "How are you?"
    
    // UCS-2 (Unicode) decoding
    let text = Ucs2::decode("004C006F00720065006D").unwrap();
    println!("UCS-2: {}", text); // "Lorem"
}

Examples

Run the included example:

cargo run --example decode_sms

Supported Encodings

  • GSM 7-bit: Default SMS encoding, supports basic Latin alphabet and some special characters
  • GSM 7-bit Extended: Includes additional characters like {, }, [, ], , etc.
  • UCS-2: Unicode encoding for international characters
  • 8-bit/Binary: Raw binary data

API Documentation

Main Functions

  • read_incoming_sms(pdu: &str) - Simple API for decoding incoming SMS
  • read_outgoing_sms(pdu: &str) - Simple API for decoding outgoing SMS
  • SmsDeliver::decode(pdu: &str) - Detailed API for incoming SMS
  • SmsSubmit::decode(pdu: &str) - Detailed API for outgoing SMS

Codecs

  • Gsm7Bit::decode(data: &str, strip_padding: bool) - Decode GSM 7-bit encoded text
  • Gsm7Bit::encode(data: &str, with_padding: bool) - Encode text to GSM 7-bit
  • Ucs2::decode(data: &str) - Decode UCS-2 (UTF-16BE) encoded text
  • Ucs2::encode(data: &str) - Encode text to UCS-2

Testing

Run the test suite:

cargo test

All tests from the original Python implementation are included and passing.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Acknowledgments

  • Original Python implementation: smspdudecoder by Sergey Yenchuk
  • This Rust port was developed with assistance from Kiro AI

License

This project is licensed under the MIT License - see the LICENSE file for details.

References

Changelog

0.1.0 (2024)

  • Initial release
  • Full support for SMS-DELIVER and SMS-SUBMIT PDU decoding
  • GSM 7-bit and UCS-2 encoding support
  • Concatenated message support
  • Timezone-aware timestamp parsing
Commit count: 0

cargo fmt