# SMS PDU Decoder
[](https://crates.io/crates/sms-pdu-decoder)
[](https://docs.rs/sms-pdu-decoder)
[](https://opensource.org/licenses/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](https://github.com/syenchuk/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`:
```toml
[dependencies]
sms-pdu-decoder = "0.1.0"
```
## Usage
### Simple API
For quick decoding of incoming SMS messages:
```rust
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:
```rust
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:
```rust
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:
```bash
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:
```bash
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](https://github.com/syenchuk/smspdudecoder) by Sergey Yenchuk
- This Rust port was developed with assistance from [Kiro AI](https://kiro.ai)
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## References
- [GSM 03.40 Specification](https://www.etsi.org/deliver/etsi_gts/03/0340/05.03.00_60/gsmts_0340v050300p.pdf)
- [GSM 03.38 Specification (Character Sets)](https://www.etsi.org/deliver/etsi_i_ets/300900_300999/300900/03_60/ets_300900e03p.pdf)
## 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