Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qurlrecode.cpp File Reference

(e4d0d0b7de436a0fe570963972a7c4795cb5404d)

#include "qurl.h"
#include "private/qurl_p.h"
#include "private/qstringconverter_p.h"
#include "private/qtools_p.h"
#include "private/qsimd_p.h"
Include dependency graph for qurlrecode.cpp:

Go to the source code of this file.

Enumerations

enum  EncodingAction {
  DecodeCharacter = 0 , LeaveCharacter = 1 , EncodeCharacter = 2 , DecodeCharacter = 0 ,
  LeaveCharacter = 1 , EncodeCharacter = 2
}
enum  EncodingAction {
  DecodeCharacter = 0 , LeaveCharacter = 1 , EncodeCharacter = 2 , DecodeCharacter = 0 ,
  LeaveCharacter = 1 , EncodeCharacter = 2
}

Functions

static bool isHex (char16_t c)
static bool isUpperHex (char16_t c)
static char16_t toUpperHex (char16_t c)
static ushort decodeNibble (char16_t c)
static char16_t decodePercentEncoding (const char16_t *input)
static char16_t encodeNibble (ushort c)
static void ensureDetached (QString &result, char16_t *&output, const char16_t *begin, const char16_t *input, const char16_t *end, int add=0)
static bool encodedUtf8ToUtf16 (QString &result, char16_t *&output, const char16_t *begin, const char16_t *&input, const char16_t *end, char16_t decoded)
static void unicodeToEncodedUtf8 (QString &result, char16_t *&output, const char16_t *begin, const char16_t *&input, const char16_t *end, char16_t decoded)
static int recode (QString &result, const char16_t *begin, const char16_t *end, QUrl::ComponentFormattingOptions encoding, const uchar *actionTable, bool retryBadEncoding)
static bool simdCheckNonEncoded (...)
static bool isUnsafeForLocalFile (uchar byte)
static qsizetype decode (QString &appendTo, QStringView in, QUrl::ComponentFormattingOptions encoding)
template<size_t N>
static void maskTable (uchar(&table)[N], const uchar(&mask)[N])
Q_AUTOTEST_EXPORT qsizetype qt_urlRecode (QString &appendTo, QStringView in, QUrl::ComponentFormattingOptions encoding, const ushort *tableModifications)
qsizetype qt_encodeFromUser (QString &appendTo, const QString &in, const ushort *tableModifications)

Variables

static const uchar defaultActionTable [96]
static const uchar reservedMask [96]

Enumeration Type Documentation

◆ EncodingAction [1/2]

Enumerator
DecodeCharacter 
LeaveCharacter 
EncodeCharacter 
DecodeCharacter 
LeaveCharacter 
EncodeCharacter 

Definition at line 14 of file qurlrecode.cpp.

◆ EncodingAction [2/2]

Enumerator
DecodeCharacter 
LeaveCharacter 
EncodeCharacter 
DecodeCharacter 
LeaveCharacter 
EncodeCharacter 

Definition at line 14 of file qurlrecode.cpp.

Function Documentation

◆ decode()

qsizetype decode ( QString & appendTo,
QStringView in,
QUrl::ComponentFormattingOptions encoding )
static
Since
5.0

This function decodes a percent-encoded string located in in by appending each character to appendTo. It returns the number of characters appended. When the encoding mode is QUrl::FullyDecoded (the mode used for most functions), each percent-encoded sequence is decoded as follows:

\list

  • from %00 to %7F: the exact decoded value is appended;
  • from %80 to FF: QChar::ReplacementCharacter is appended;
  • bad encoding: original input is copied to the output, undecoded. \endlist

Given the above, it's important for the input to already have all UTF-8 percent sequences decoded by qt_urlRecode (that is, the input should not have been processed with QUrl::EncodeUnicode).

The input should also be a valid percent-encoded sequence (the output of qt_urlRecode is always valid).

With QUrlDecodeForLocalFile in encoding, the function instead returns -1 when it would decode a byte that is unsafe in a local-file path. The appendTo string is truncated back to its original size, but always returns null if the original was empty.

If no decoding error happened, this function returns the number of characters added to appendTo. That might be 0 if there was nothing to decode (see qt_urlRecode() below for the reason).

Definition at line 573 of file qurlrecode.cpp.

References isHex().

Here is the call graph for this function:

◆ decodeNibble()

ushort decodeNibble ( char16_t c)
inlinestatic

Definition at line 152 of file qurlrecode.cpp.

◆ decodePercentEncoding()

char16_t decodePercentEncoding ( const char16_t * input)
inlinestatic

Definition at line 160 of file qurlrecode.cpp.

References isHex().

Referenced by recode().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ encodedUtf8ToUtf16()

bool encodedUtf8ToUtf16 ( QString & result,
char16_t *& output,
const char16_t * begin,
const char16_t *& input,
const char16_t * end,
char16_t decoded )
static

Definition at line 257 of file qurlrecode.cpp.

◆ encodeNibble()

char16_t encodeNibble ( ushort c)
inlinestatic

Definition at line 169 of file qurlrecode.cpp.

◆ ensureDetached()

void ensureDetached ( QString & result,
char16_t *& output,
const char16_t * begin,
const char16_t * input,
const char16_t * end,
int add = 0 )
static

Definition at line 174 of file qurlrecode.cpp.

◆ isHex()

bool isHex ( char16_t c)
inlinestatic

Definition at line 136 of file qurlrecode.cpp.

Referenced by decode(), and decodePercentEncoding().

Here is the caller graph for this function:

◆ isUnsafeForLocalFile()

bool isUnsafeForLocalFile ( uchar byte)
static

Definition at line 530 of file qurlrecode.cpp.

◆ isUpperHex()

bool isUpperHex ( char16_t c)
inlinestatic

Definition at line 141 of file qurlrecode.cpp.

Referenced by recode(), and toUpperHex().

Here is the caller graph for this function:

◆ maskTable()

template<size_t N>
void maskTable ( uchar(&) table[N],
const uchar(&) mask[N] )
static

Definition at line 646 of file qurlrecode.cpp.

◆ qt_encodeFromUser()

qsizetype qt_encodeFromUser ( QString & appendTo,
const QString & in,
const ushort * tableModifications )

Definition at line 720 of file qurlrecode.cpp.

References defaultActionTable, and EncodeCharacter.

◆ qt_urlRecode()

Q_AUTOTEST_EXPORT qsizetype qt_urlRecode ( QString & appendTo,
QStringView in,
QUrl::ComponentFormattingOptions encoding,
const ushort * tableModifications )

Recodes the string from begin to end. If any transformations are done, append them to appendTo and return the number of characters added. If no transformations were required, return 0.

The encoding option modifies the default behaviour: \list

  • QUrl::DecodeReserved: if set, reserved characters will be decoded; if unset, reserved characters will be encoded
  • QUrl::EncodeSpaces: if set, spaces will be encoded to "%20"; if unset, they will be " "
  • QUrl::EncodeUnicode: if set, characters above U+0080 will be encoded to their UTF-8 percent-encoded form; if unset, they will be decoded to UTF-16
  • QUrl::FullyDecoded: if set, this function will decode all percent-encoded sequences, including that of the percent character. The resulting string will not be percent-encoded anymore. Use with caution! In this mode, the behaviour is undefined if the input string contains any percent-encoding sequences above %80. Also, the function will not correct bad % sequences.
  • QUrlDecodeForLocalFile: if set, this performs the FullyDecoded mode with error checking suitable for local file paths. See decode() above for details. \endlist

Other flags are ignored (including QUrl::EncodeReserved).

The tableModifications argument can be used to supply extra modifications to the tables, to be applied after the flags above are handled. It consists of a sequence of 16-bit values, where the low 8 bits indicate the character in question and the high 8 bits are either EncodeCharacter, LeaveCharacter or DecodeCharacter.

This function corrects percent-encoded errors by interpreting every '' as meaning "%25" (all percents in the same content), except in Decoded modes.

On success, this function returns the number of characters added to appendTo, or -1 on error (errors are currently only detected if encoding is QUrlDecodeForLocalFile). A return value of 0 means this function had no percent-encoded transformations to perform, so the caller must append the in input to appendTo. This lets the caller return a shallow copy of the original QString (avoiding allocation) and keep control of its null-vs-empty state.

Definition at line 697 of file qurlrecode.cpp.

◆ recode()

int recode ( QString & result,
const char16_t * begin,
const char16_t * end,
QUrl::ComponentFormattingOptions encoding,
const uchar * actionTable,
bool retryBadEncoding )
static

Definition at line 340 of file qurlrecode.cpp.

References DecodeCharacter, decodePercentEncoding(), EncodeCharacter, isUpperHex(), LeaveCharacter, and toUpperHex().

Here is the call graph for this function:

◆ simdCheckNonEncoded()

bool simdCheckNonEncoded ( ...)
static

Definition at line 520 of file qurlrecode.cpp.

◆ toUpperHex()

char16_t toUpperHex ( char16_t c)
inlinestatic

Definition at line 147 of file qurlrecode.cpp.

References isUpperHex().

Referenced by recode().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unicodeToEncodedUtf8()

void unicodeToEncodedUtf8 ( QString & result,
char16_t *& output,
const char16_t * begin,
const char16_t *& input,
const char16_t * end,
char16_t decoded )
static

Definition at line 286 of file qurlrecode.cpp.

Variable Documentation

◆ defaultActionTable

const uchar defaultActionTable
static

Definition at line 26 of file qurlrecode.cpp.

Referenced by qt_encodeFromUser().

◆ reservedMask

const uchar reservedMask
static

Definition at line 83 of file qurlrecode.cpp.