How to handle nested final result code in AT-command text?
In text mode you can't have &lt;CR>&lt;LF> inside the received message. While sending a text message with the AT command set, the message is already terminated with the first occurence of &lt;CR>&lt;LF>. Hence you haven't to check for this case while receiving messages in text mode.

For sending and receiving formatet messages you must use the PDU mode. In PDU mode the message is packed into a frame which contains also a field for the message length.

I don't know if there's an underlying protocol layer that unsures that you will only get well formated PDU frames.