Escape Encoding and Decoding

This tutorial describes the steps to escape encoding and decoding.

Introduction

Reserved, unsafe and unreserved characters present a possibility of being interpreted incorrectly when used in the URIs. To avoid this, these characters must be escape encoded and decoded.

Escape encoding

URI encoding of a character consists of a "%" symbol, followed by two hexadecimal digits representing the octet code. For example, Space = decimal code point 32 in the ISO-Latin set. 32 decimal = 20 in hexadecimal. The URI encoded representation will be "%20"

EscapeUtils::EscapeEncodeL() escape encodes the invalid and reserved characters in the data as escape triples. The reserved characters and the set of excluded characters specified by RFC 2396 (refer to the above table) form the entire set of excluded data.

The following code fragment checks for the invalid and reserved characters in the authority component of an URI and returns the string with these characters escape encoded. For other modes as defined in TEscapeMode, refer to the table in Reservered characters section.

HBufC16* encode = EscapeUtils::EscapeEncodeL(*decode,  EscapeUtils::EEscapeAuth);
CleanupStack::PushL(encode);//encode contains %20%3C%3E%23%25%22%7B%7D%7C%5C%5E%60

......//use encode here

CleanupStack::PopAndDestroy(encode);

Escape encoding is ideal during creation of URI from the components.

Escape decoding

EscapeUtils::EscapeDecodeL() escape decodes the data. This code escape decodes the data '%20%3C%3E%23%25%22%7B%7D%7C%5C%5E%60' to '<>#%\"{}|\\^'.

_LIT(KEscapeEncoded, %20%3C%3E%23%25%22%7B%7D%7C%5C%5E%60);        //data to decode
HBufC16* decode = EscapeUtils::EscapeDecodeL(KEscapeEncoded); //contains <>#%\"{}|\\^`
CleanupStack::PushL(decode);
...........................
CleanupStack::PopAndDestroy(decode);
          

The URI must be split into its components before the escaped characters within the components are safely decoded.

Related concepts