BER-TLV解析器和构建器
伯尔特奇是一个Objective-C框架,用于解析和构建BER TLV编码的数据。
功能
- 支持类型:金额、日期、时间、文本、BCD、字节
- 线程安全(提供不可变容器BerTlv)
- 已准备好用于生产(已在App Store发布的几个项目中使用)
- 轻量级(无外部依赖项)
使用依赖项管理器设置
Cocoapods
pod 'BerTlv', '0.2.3'
或者
pod "BerTlv", :git => '[email protected]:evsinev/BerTlv.git', :tag => '0.2.3'
Carthage
github "evsinev/BerTlv" "0.2.3"
示例
解析
NSString * hex =
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f 0123 4567 89ab cdef */
/* 0 */ @"e1 35 9f 1e 08 31 36 30 32 31 34 33 37 ef 12 df" // .5.. .160 2143 7...
/* 10 */ @"0d 08 4d 30 30 30 2d 4d 50 49 df 7f 04 31 2d 32" // ..M0 00-M PI.. .1-2
/* 20 */ @"32 ef 14 df 0d 0b 4d 30 30 30 2d 54 45 53 54 4f" // 2... ..M0 00-T ESTO
/* 30 */ @"53 df 7f 03 36 2d 35 " // S... 6-5;
NSData * data = [HexUtil parse:hex];
BerTlvParser * parser = [[BerTlvParser alloc] init];
BerTlv * tlv = [parser parseConstructed:data];
NSLog(@"%@", [tlv dump:@" "]);
输出结果为
+ [E1]
- [9F1E] 3136303231343337
+ [EF]
- [DF0D] 4D3030302D4D5049
- [DF7F] 312D3232
+ [EF]
- [DF0D] 4D3030302D544553544F53
- [DF7F] 362D35
构建
BerTlvBuilder *builder = [[BerTlvBuilder alloc] initWithTemplate:TAG_E0];
for(int i=0; i<5; i++) {
[builder addHex:@"F9128478E28F860D8424000008514C8F" tag:TAG_86];
}
BerTlvs * tlvs = builder.buildTlvs;
NSLog(@"%@", [builder.buildTlvs dump:@" "]);
输出结果为
+ [E0]
- [86] F9128478E28F860D8424000008514C8F
- [86] F9128478E28F860D8424000008514C8F
- [86] F9128478E28F860D8424000008514C8F
- [86] F9128478E28F860D8424000008514C8F
- [86] F9128478E28F860D8424000008514C8F
许可
BerTlv 框架遵循 Apache License 2.0 许可协议