Encode the string as per 23.038, packing 8bit chars into 7bit chars packed tightly.
| 191 | |
| 192 | // Encode the string as per 23.038, packing 8bit chars into 7bit chars packed tightly. |
| 193 | static string ussdEncode(string in) |
| 194 | { |
| 195 | unsigned char result[280], *rp = result; |
| 196 | const unsigned char *indata = (const unsigned char*)in.data(); |
| 197 | unsigned indatalen = in.size(); |
| 198 | unsigned offset = 0; |
| 199 | unsigned accum = 0; |
| 200 | for (unsigned i = 0; i < indatalen; i++) { |
| 201 | accum = accum | (encodeGSMChar(indata[i] & 0x7f) << offset); |
| 202 | if (offset == 0) { |
| 203 | offset = 7; |
| 204 | } else { |
| 205 | *rp++ = accum & 0xff; |
| 206 | accum >>= 8; |
| 207 | offset--; |
| 208 | } |
| 209 | } |
| 210 | if (offset == 1) { |
| 211 | // Special case as per 23.038 6.1.2.3 - If there are exactly 7 bits at the end they are padded with a CR. |
| 212 | accum |= ('\r' << 1); |
| 213 | } |
| 214 | if (offset) { |
| 215 | *rp++ = accum & 0xff; |
| 216 | } |
| 217 | return string((char*)result,rp-result); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | // This class is just a wrapper to conveniently contain the ssCodes map. |