laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/35134?usp=email )
Change subject: rlp: Add support for 576bit RLP frames ......................................................................
rlp: Add support for 576bit RLP frames
The code so far only supported 240bit RLP frames; Add support for 576bit in this patch. We still only support versions 0+1 and not version 2.
Change-Id: Idfdcabb19fe8733fb9c5ee76a39b0bf4cdf60c2c --- M src/gsm/rlp.c 1 file changed, 39 insertions(+), 21 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/34/35134/1
diff --git a/src/gsm/rlp.c b/src/gsm/rlp.c index 9ef0c85..a9903f8 100644 --- a/src/gsm/rlp.c +++ b/src/gsm/rlp.c @@ -57,19 +57,22 @@ { 0, NULL } };
+/* number of bytes used up by FCS */ +#define FCS_SIZE_BYTES 3
/*! decode a RLP frame into its abstract representation. Doesn't check FCS correctness. * \param[out] out caller-allocated memory for output of decoded frame * \param[in] version RLP version number to use when decoding * \param[in] data raw RLP frame input data - * \param[in] data_len length of data (in octets) + * \param[in] data_len length of data (in octets); must be 30 (240bit) or 72 (576bit) * \returns 0 in case of success; negative on error */ int osmo_rlp_decode(struct osmo_rlp_frame_decoded *out, uint8_t version, const uint8_t *data, size_t data_len) { + const uint8_t hdr_len = 2; /* will become a variable when we introduce v2 support */ uint8_t n_s, n_r;
/* we only support 240 bit so far */ - if (data_len != 240/8) + if (data_len != 240/8 && data_len != 576/8) return -EINVAL;
/* we only support version 0+1 so far */ @@ -82,16 +85,15 @@ out->c_r = data[0] & 1; n_s = (data[0] >> 3) | (data[1] & 1) << 5; n_r = (data[1] >> 2); - //out->fcs = (data[240/8-3] << 16) | (data[240/8-2]) << 8 | (data[240/8-1] << 0); - out->fcs = (data[240/8-1] << 16) | (data[240/8-2]) << 8 | (data[240/8-3] << 0); + out->fcs = (data[data_len-1] << 16) | (data[data_len-2]) << 8 | (data[data_len-3] << 0); out->p_f = (data[1] >> 1) & 1;
if (n_s == 0x3f) { out->ftype = OSMO_RLP_FT_U; out->u_ftype = n_r & 0x1f; if (out->u_ftype == OSMO_RLP_U_FT_XID) { - memcpy(out->info, data+2, 240/8 - 5); - out->info_len = 240/8 - 5; + memcpy(out->info, data + hdr_len, data_len - (hdr_len + FCS_SIZE_BYTES)); + out->info_len = data_len - (hdr_len + FCS_SIZE_BYTES); } } else if (n_s == 0x3e) { out->ftype = OSMO_RLP_FT_S; @@ -102,8 +104,8 @@ out->s_ftype = (data[0] >> 1) & 3; out->n_s = n_s; out->n_r = n_r; - memcpy(out->info, data+2, 240/8 - 5); - out->info_len = 240/8 - 5; + memcpy(out->info, data + hdr_len, data_len - (hdr_len + FCS_SIZE_BYTES)); + out->info_len = data_len - (2 + 3); }
return 0; @@ -111,12 +113,12 @@
/*! encode a RLP frame from its abstract representation. Generates FCS. * \param[out] out caller-allocated output buffer - * \param[in] out_size size of output buffer (in octets) + * \param[in] out_size size of output buffer (in octets); must be 30 (240bit) or 72 (576bit) * \param[in] in decoded RLP frame which is to be encoded * \returns number of output bytes used; negative on error */ int osmo_rlp_encode(uint8_t *out, size_t out_size, const struct osmo_rlp_frame_decoded *in) { - uint8_t out_len = 240/8; + const uint8_t hdr_len = 2; /* will become a variable when we introduce v2 support */ uint8_t n_s, n_r, s_bits; uint32_t fcs;
@@ -124,10 +126,10 @@ if (in->version >= 2) return -ENOTSUP;
- if (out_size < out_len) + if (out_size != 240/8 && out_size != 576/8) return -EINVAL;
- memset(out, 0, out_len); + memset(out, 0, out_size);
if (in->c_r) out[0] |= 0x01; @@ -139,8 +141,11 @@ n_s = 0x3f; n_r = in->u_ftype; s_bits = 0; - if (in->u_ftype == OSMO_RLP_U_FT_XID) - memcpy(out+2, in->info, in->info_len); + if (in->u_ftype == OSMO_RLP_U_FT_XID) { + if (in->info_len > out_size - (hdr_len + FCS_SIZE_BYTES)) + return -EINVAL; + memcpy(out+hdr_len, in->info, in->info_len); + } break; case OSMO_RLP_FT_S: n_s = 0x3e; @@ -149,12 +154,12 @@ break; case OSMO_RLP_FT_IS: /* we only support 240 bit so far */ - if (in->info_len != 240/8 - 5) + if (in->info_len > out_size - (hdr_len + FCS_SIZE_BYTES)) return -EINVAL; n_s = in->n_s; n_r = in->n_r; s_bits = in->s_ftype; - memcpy(out+2, in->info, in->info_len); + memcpy(out+hdr_len, in->info, in->info_len); break; default: return -EINVAL; @@ -171,12 +176,12 @@ out[0] |= (s_bits & 3) << 1;
/* compute FCS + add it to end of frame */ - fcs = osmo_rlp_fcs_compute(out, out_len - 3); - out[out_len - 3] = (fcs >> 0) & 0xff; - out[out_len - 2] = (fcs >> 8) & 0xff; - out[out_len - 1] = (fcs >> 16) & 0xff; + fcs = osmo_rlp_fcs_compute(out, out_size - FCS_SIZE_BYTES); + out[out_size - 3] = (fcs >> 0) & 0xff; + out[out_size - 2] = (fcs >> 8) & 0xff; + out[out_size - 1] = (fcs >> 16) & 0xff;
- return out_len; + return out_size; }