Browse code

modules/pv: Add 7Bit Encoding/Decoding (e.g. for 3GPP-SMS)

Carsten Bock authored on 16/10/2014 18:58:28
Showing 2 changed files
... ...
@@ -179,6 +179,62 @@ static int urldecode_param(str *sin, str *sout) {
179 179
 	return 0;
180 180
 }
181 181
 
182
+/* Encode 7BIT PDU */
183
+static int pdu_7bit_encode(str sin) {
184
+	int i, j;
185
+	unsigned char hex;
186
+	unsigned char nleft;
187
+	unsigned char fill;
188
+	char HexTbl[] = {"0123456789ABCDEF"};
189
+
190
+	nleft = 1;
191
+	j = 0;
192
+	for(i = 0; i < sin.len; i++) {
193
+		hex = *(sin.s) >> (nleft - 1);
194
+		fill = *(sin.s+1) << (8-nleft);
195
+		hex = hex | fill;
196
+		_tr_buffer[j++] = HexTbl[hex >> 4];
197
+		_tr_buffer[j++] = HexTbl[hex & 0x0F];
198
+		nleft++;
199
+		if(nleft == 8) {
200
+			sin.s++;
201
+			i++;
202
+			nleft = 1;
203
+		}	   										 	
204
+		sin.s++;
205
+	}
206
+	_tr_buffer[j] = '\0';
207
+	return j;
208
+}
209
+
210
+/* Decode 7BIT PDU */
211
+static int pdu_7bit_decode(str sin) {
212
+	int i, j;
213
+	unsigned char nleft = 1;
214
+	unsigned char fill = 0;
215
+	unsigned char oldfill = 0;
216
+	j = 0;
217
+	for(i = 0; i < sin.len; i += 2) {
218
+		_tr_buffer[j] = (unsigned char)pv_from_hex(sin.s[i]) << 4;
219
+		_tr_buffer[j] |= (unsigned char)pv_from_hex(sin.s[i+1]);
220
+		fill = (unsigned char)_tr_buffer[j];
221
+		fill >>= (8 - nleft);
222
+		_tr_buffer[j] <<= (nleft -1 );
223
+		_tr_buffer[j] &= 0x7F;
224
+		_tr_buffer[j] |= oldfill;
225
+		oldfill = fill;
226
+		j++;
227
+		nleft++;
228
+		if(nleft == 8) {
229
+			_tr_buffer[j++] = oldfill;
230
+			nleft = 1;
231
+			oldfill = 0;
232
+		}
233
+	}
234
+	_tr_buffer[j] = '\0';
235
+	return j;	
236
+}
237
+
182 238
 /* -- transformations functions */
183 239
 
184 240
 /*!
... ...
@@ -312,6 +368,28 @@ int tr_eval_string(struct sip_msg *msg, tr_param_t *tp, int subtype,
312 368
 			val->rs.s = _tr_buffer;
313 369
 			val->rs.len = i;
314 370
 			break;
371
+		case TR_S_ENCODE7BIT:
372
+			if(!(val->flags&PV_VAL_STR))
373
+				val->rs.s = int2str(val->ri, &val->rs.len);
374
+			if(val->rs.len > (TR_BUFFER_SIZE*7/8) -1)
375
+				return -1;
376
+			i = pdu_7bit_encode(val->rs);
377
+			memset(val, 0, sizeof(pv_value_t));
378
+			val->flags = PV_VAL_STR;
379
+			val->rs.s = _tr_buffer;
380
+			val->rs.len = i;
381
+			break;
382
+		case TR_S_DECODE7BIT:
383
+			if(!(val->flags&PV_VAL_STR))
384
+				val->rs.s = int2str(val->ri, &val->rs.len);
385
+			if(val->rs.len>TR_BUFFER_SIZE/2-1)
386
+				return -1;
387
+			i = pdu_7bit_decode(val->rs);
388
+			memset(val, 0, sizeof(pv_value_t));
389
+			val->flags = PV_VAL_STR;
390
+			val->rs.s = _tr_buffer;
391
+			val->rs.len = i;
392
+			break;
315 393
 		case TR_S_ENCODEBASE64:
316 394
 			if(!(val->flags&PV_VAL_STR))
317 395
 				val->rs.s = int2str(val->ri, &val->rs.len);
... ...
@@ -1960,6 +2038,12 @@ char* tr_parse_string(str* in, trans_t *t)
1960 2038
 	} else if(name.len==11 && strncasecmp(name.s, "decode.hexa", 11)==0) {
1961 2039
 		t->subtype = TR_S_DECODEHEXA;
1962 2040
 		goto done;
2041
+	} else if(name.len==11 && strncasecmp(name.s, "encode.7bit", 11)==0) {
2042
+		t->subtype = TR_S_ENCODE7BIT;
2043
+		goto done;
2044
+	} else if(name.len==11 && strncasecmp(name.s, "decode.7bit", 11)==0) {
2045
+		t->subtype = TR_S_DECODE7BIT;
2046
+		goto done;
1963 2047
 	} else if(name.len==13 && strncasecmp(name.s, "encode.base64", 13)==0) {
1964 2048
 		t->subtype = TR_S_ENCODEBASE64;
1965 2049
 		goto done;
... ...
@@ -37,7 +37,7 @@ enum _tr_type { TR_NONE=0, TR_STRING, TR_URI, TR_PARAMLIST, TR_NAMEADDR,
37 37
 enum _tr_s_subtype { 
38 38
 	TR_S_NONE=0, TR_S_LEN, TR_S_INT, TR_S_MD5, TR_S_SHA256, TR_S_SHA384,
39 39
 	TR_S_SHA512, TR_S_SUBSTR, TR_S_SELECT, TR_S_ENCODEHEXA, TR_S_DECODEHEXA,
40
-	TR_S_ENCODEBASE64, TR_S_DECODEBASE64,
40
+	TR_S_ENCODE7BIT, TR_S_DECODE7BIT, TR_S_ENCODEBASE64, TR_S_DECODEBASE64,
41 41
 	TR_S_ESCAPECOMMON, TR_S_UNESCAPECOMMON, TR_S_ESCAPEUSER, TR_S_UNESCAPEUSER,
42 42
 	TR_S_ESCAPEPARAM, TR_S_UNESCAPEPARAM, TR_S_TOLOWER, TR_S_TOUPPER,
43 43
 	TR_S_STRIP, TR_S_STRIPTAIL, TR_S_PREFIXES, TR_S_PREFIXES_QUOT, TR_S_REPLACE,