<p>laforge has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/osmo-cbc/+/22394">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">charset: Fix padding of USSD messages in 7bit GSM alphabet<br><br>USSD messages are sent in pages of 82 bytes, and there is no way to<br>indicate a shorter payload length inside the page.  Hence, we always<br>must pad up to the end of the page, using <CR> characters.<br><br>Change-Id: I9950431e920579e6c3a0d12348573f51d21739ec<br>---<br>M src/charset.c<br>1 file changed, 30 insertions(+), 3 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/osmo-cbc refs/changes/94/22394/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/src/charset.c b/src/charset.c</span><br><span>index 444743b..efaff8e 100644</span><br><span>--- a/src/charset.c</span><br><span>+++ b/src/charset.c</span><br><span>@@ -26,13 +26,40 @@</span><br><span> </span><br><span> #include "charset.h"</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/* pad the entire "remainder" of a buffer with repeated instances of the given pad character */</span><br><span style="color: hsl(120, 100%, 40%);">+static void pad_with_septets(uint8_t *buf, size_t buf_len, int num_septets, char pad_char)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       unsigned int bit_offset;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    for (bit_offset = num_septets * 7; bit_offset + 7 <= buf_len * 8; bit_offset += 7) {</span><br><span style="color: hsl(120, 100%, 40%);">+               unsigned int byte_offset = bit_offset / 8;</span><br><span style="color: hsl(120, 100%, 40%);">+            unsigned int bits = bit_offset % 8;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+         /* put one more septet */</span><br><span style="color: hsl(120, 100%, 40%);">+             buf[byte_offset] |= ((pad_char << bits) & 0xff);</span><br><span style="color: hsl(120, 100%, 40%);">+            if (bits > 1)</span><br><span style="color: hsl(120, 100%, 40%);">+                      buf[byte_offset+1] = (pad_char) >> (8-bits);</span><br><span style="color: hsl(120, 100%, 40%);">+    }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /* return number of output bytes written */</span><br><span> int charset_utf8_to_gsm7(uint8_t *out, size_t out_len, const char *in, size_t in_len)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-   int octets;</span><br><span style="color: hsl(120, 100%, 40%);">+   int octets, num_septets, num_bits, num_bytes_used;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>         /* FIXME: implement this for 'escape' characters outside 7bit alphabet */</span><br><span style="color: hsl(0, 100%, 40%);">-       gsm_7bit_encode_n_ussd(out, out_len, in, &octets);</span><br><span style="color: hsl(0, 100%, 40%);">-  return octets;</span><br><span style="color: hsl(120, 100%, 40%);">+        num_septets = gsm_7bit_encode_n(out, out_len, in, &octets);</span><br><span style="color: hsl(120, 100%, 40%);">+       num_bits = num_septets * 7;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* we need to pad the entire remainder of the message with <CR> */</span><br><span style="color: hsl(120, 100%, 40%);">+      pad_with_septets(out, out_len, num_septets, '\r');</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  /* return actual number of output octets used, excluding any padding */</span><br><span style="color: hsl(120, 100%, 40%);">+       num_bytes_used = num_bits/8;</span><br><span style="color: hsl(120, 100%, 40%);">+  if (num_bits % 8)</span><br><span style="color: hsl(120, 100%, 40%);">+             num_bytes_used++;</span><br><span style="color: hsl(120, 100%, 40%);">+     return num_bytes_used;</span><br><span> }</span><br><span> </span><br><span> /* return number of output bytes written */</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/osmo-cbc/+/22394">change 22394</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/osmo-cbc/+/22394"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: osmo-cbc </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I9950431e920579e6c3a0d12348573f51d21739ec </div>
<div style="display:none"> Gerrit-Change-Number: 22394 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>