Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ jobs:
-Wextra
-pedantic
)
CFLAGS="${compile_flags[*]} -std=c89"
CFLAGS="${compile_flags[*]} -std=c99"
CXXFLAGS="${compile_flags[*]} -std=c++98"
LDFLAGS='-g ${{ matrix.ldflags }}'
cmake_args=(
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ project(uriparser
LANGUAGES
C
)
set(CMAKE_C_STANDARD 90) # same as C89, value "89" not supported by CMake
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF) # i.e. -std=c90 rather than default -std=gnu90
set(CMAKE_C_EXTENSIONS OFF) # i.e. -std=c99 rather than default -std=gnu99

# See https://verbump.de/ for what these numbers do
set(URIPARSER_SO_CURRENT 2)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
uriparser is a
strictly [RFC 3986](https://datatracker.ietf.org/doc/html/rfc3986) compliant
URI parsing and handling library
written in C89 ("ANSI C").
written in C99.
uriparser is cross-platform,
fast,
supports both `char` and `wchar_t`, and
Expand Down
65 changes: 29 additions & 36 deletions src/UriCommon.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,35 +671,30 @@ static UriBool URI_FUNC(PrependNewDotSegment)(URI_TYPE(Uri) * uri,
assert(uri != NULL);
assert(memory != NULL);

{
URI_TYPE(PathSegment) * const segment =
memory->malloc(memory, 1 * sizeof(URI_TYPE(PathSegment)));
URI_TYPE(PathSegment) * const segment =
memory->malloc(memory, 1 * sizeof(URI_TYPE(PathSegment)));

if (segment == NULL) {
return URI_FALSE; /* i.e. raise malloc error */
}
if (segment == NULL) {
return URI_FALSE; /* i.e. raise malloc error */
}

segment->next = uri->pathHead;
segment->next = uri->pathHead;

{
URI_TYPE(TextRange) dotRange;
dotRange.first = URI_FUNC(ConstPwd);
dotRange.afterLast = URI_FUNC(ConstPwd) + 1;
URI_TYPE(TextRange) dotRange;
dotRange.first = URI_FUNC(ConstPwd);
dotRange.afterLast = URI_FUNC(ConstPwd) + 1;

if (uri->owner == URI_TRUE) {
if (URI_FUNC(CopyRange)(&(segment->text), &dotRange, memory)
== URI_FALSE) {
memory->free(memory, segment);
return URI_FALSE; /* i.e. raise malloc error */
}
} else {
segment->text = dotRange; /* copies all members */
}
if (uri->owner == URI_TRUE) {
if (URI_FUNC(CopyRange)(&(segment->text), &dotRange, memory) == URI_FALSE) {
memory->free(memory, segment);
return URI_FALSE; /* i.e. raise malloc error */
}

uri->pathHead = segment;
} else {
segment->text = dotRange; /* copies all members */
}

uri->pathHead = segment;

return URI_TRUE;
}

Expand Down Expand Up @@ -730,23 +725,21 @@ UriBool URI_FUNC(FixPathNoScheme)(URI_TYPE(Uri) * uri, UriMemoryManager * memory
}

/* Check for troublesome first path segment containing a colon */
{
UriBool colonFound = URI_FALSE;
const URI_CHAR * walker = uri->pathHead->text.first;

while (walker < uri->pathHead->text.afterLast) {
if (walker[0] == _UT(':')) {
colonFound = URI_TRUE;
break;
}
walker++;
UriBool colonFound = URI_FALSE;
const URI_CHAR * walker = uri->pathHead->text.first;

while (walker < uri->pathHead->text.afterLast) {
if (walker[0] == _UT(':')) {
colonFound = URI_TRUE;
break;
}
walker++;
}

assert((walker == uri->pathHead->text.afterLast) || (colonFound == URI_TRUE));
assert((walker == uri->pathHead->text.afterLast) || (colonFound == URI_TRUE));

if (colonFound == URI_FALSE) {
return URI_TRUE; /* i.e. nothing to do */
}
if (colonFound == URI_FALSE) {
return URI_TRUE; /* i.e. nothing to do */
}

/* Insert "." segment in front */
Expand Down
93 changes: 44 additions & 49 deletions src/UriFile.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,57 +140,52 @@ static URI_INLINE int URI_FUNC(UriStringToFilename)(const URI_CHAR * uriString,
return URI_ERROR_NULL;
}

{
const UriBool file_unknown_slashes =
URI_STRNCMP(uriString, _UT("file:"), URI_STRLEN(_UT("file:"))) == 0;
const UriBool file_one_or_more_slashes =
file_unknown_slashes
&& (URI_STRNCMP(uriString, _UT("file:/"), URI_STRLEN(_UT("file:/"))) == 0);
const UriBool file_two_or_more_slashes =
file_one_or_more_slashes
&& (URI_STRNCMP(uriString, _UT("file://"), URI_STRLEN(_UT("file://"))) == 0);
const UriBool file_three_or_more_slashes =
file_two_or_more_slashes
&& (URI_STRNCMP(uriString, _UT("file:///"), URI_STRLEN(_UT("file:///")))
== 0);

const size_t charsToSkip =
file_two_or_more_slashes
? file_three_or_more_slashes
? toUnix
/* file:///bin/bash */
? URI_STRLEN(_UT("file://"))
/* file:///E:/Documents%20and%20Settings */
: URI_STRLEN(_UT("file:///"))
/* file://Server01/Letter.txt */
: URI_STRLEN(_UT("file://"))
: ((file_one_or_more_slashes && toUnix)
/* file:/bin/bash */
/* https://tools.ietf.org/html/rfc8089#appendix-B */
? URI_STRLEN(_UT("file:"))
: ((!toUnix && file_unknown_slashes && !file_one_or_more_slashes)
/* file:c:/path/to/file */
/* https://tools.ietf.org/html/rfc8089#appendix-E.2 */
? URI_STRLEN(_UT("file:"))
: 0));
const size_t charsToCopy = URI_STRLEN(uriString + charsToSkip) + 1;

const UriBool is_windows_network_with_authority = (toUnix == URI_FALSE)
&& file_two_or_more_slashes
&& !file_three_or_more_slashes;

URI_CHAR * const unescape_target =
is_windows_network_with_authority ? (filename + 2) : filename;

if (is_windows_network_with_authority) {
filename[0] = '\\';
filename[1] = '\\';
}

memcpy(unescape_target, uriString + charsToSkip, charsToCopy * sizeof(URI_CHAR));
URI_FUNC(UnescapeInPlaceEx)(filename, URI_FALSE, URI_BR_DONT_TOUCH);
const UriBool file_unknown_slashes =
URI_STRNCMP(uriString, _UT("file:"), URI_STRLEN(_UT("file:"))) == 0;
const UriBool file_one_or_more_slashes =
file_unknown_slashes
&& (URI_STRNCMP(uriString, _UT("file:/"), URI_STRLEN(_UT("file:/"))) == 0);
const UriBool file_two_or_more_slashes =
file_one_or_more_slashes
&& (URI_STRNCMP(uriString, _UT("file://"), URI_STRLEN(_UT("file://"))) == 0);
const UriBool file_three_or_more_slashes =
file_two_or_more_slashes
&& (URI_STRNCMP(uriString, _UT("file:///"), URI_STRLEN(_UT("file:///"))) == 0);

const size_t charsToSkip =
file_two_or_more_slashes
? file_three_or_more_slashes ? toUnix
/* file:///bin/bash */
? URI_STRLEN(_UT("file://"))
/* file:///E:/Documents%20and%20Settings */
: URI_STRLEN(_UT("file:///"))
/* file://Server01/Letter.txt */
: URI_STRLEN(_UT("file://"))
: ((file_one_or_more_slashes && toUnix)
/* file:/bin/bash */
/* https://tools.ietf.org/html/rfc8089#appendix-B */
? URI_STRLEN(_UT("file:"))
: ((!toUnix && file_unknown_slashes && !file_one_or_more_slashes)
/* file:c:/path/to/file */
/* https://tools.ietf.org/html/rfc8089#appendix-E.2 */
? URI_STRLEN(_UT("file:"))
: 0));
const size_t charsToCopy = URI_STRLEN(uriString + charsToSkip) + 1;

const UriBool is_windows_network_with_authority =
(toUnix == URI_FALSE) && file_two_or_more_slashes && !file_three_or_more_slashes;

URI_CHAR * const unescape_target =
is_windows_network_with_authority ? (filename + 2) : filename;

if (is_windows_network_with_authority) {
filename[0] = '\\';
filename[1] = '\\';
}

memcpy(unescape_target, uriString + charsToSkip, charsToCopy * sizeof(URI_CHAR));
URI_FUNC(UnescapeInPlaceEx)(filename, URI_FALSE, URI_BR_DONT_TOUCH);

/* Convert forward slashes to backslashes */
if (!toUnix) {
URI_CHAR * walker = filename;
Expand Down
18 changes: 8 additions & 10 deletions src/UriMemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,16 +436,14 @@ int uriTestMemoryManagerEx(UriMemoryManager * memory, UriBool challengeAlignment
ptr[2] = 2.2L;
ptr[3] = 3.3L;

{
long double * const ptrNew =
memory->realloc(memory, ptr, 8 * sizeof(long double));
if (ptrNew != NULL) {
ptr = ptrNew;
ptr[4] = 4.4L;
ptr[5] = 5.5L;
ptr[6] = 6.6L;
ptr[7] = 7.7L;
}
long double * const ptrNew =
memory->realloc(memory, ptr, 8 * sizeof(long double));
if (ptrNew != NULL) {
ptr = ptrNew;
ptr[4] = 4.4L;
ptr[5] = 5.5L;
ptr[6] = 6.6L;
ptr[7] = 7.7L;
}

memory->free(memory, ptr);
Expand Down
54 changes: 23 additions & 31 deletions src/UriNormalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,24 +524,22 @@ static const URI_CHAR * URI_FUNC(PastLeadingZeros)(const URI_CHAR * first,
assert(afterLast != NULL);
assert(first != afterLast);

{
/* Find the first non-zero character */
const URI_CHAR * remainderFirst = first;
while ((remainderFirst < afterLast) && (remainderFirst[0] == _UT('0'))) {
remainderFirst++;
}

/* Is the string /all/ zeros? */
if (remainderFirst == afterLast) {
/* Yes, and length is >=1 because we ruled out the empty string earlier;
* pull back onto rightmost zero */
assert(remainderFirst > first);
remainderFirst--;
assert(remainderFirst[0] == _UT('0'));
}
/* Find the first non-zero character */
const URI_CHAR * remainderFirst = first;
while ((remainderFirst < afterLast) && (remainderFirst[0] == _UT('0'))) {
remainderFirst++;
}

return remainderFirst;
/* Is the string /all/ zeros? */
if (remainderFirst == afterLast) {
/* Yes, and length is >=1 because we ruled out the empty string earlier;
* pull back onto rightmost zero */
assert(remainderFirst > first);
remainderFirst--;
assert(remainderFirst[0] == _UT('0'));
}

return remainderFirst;
}

static void URI_FUNC(DropLeadingZerosInplace)(URI_CHAR * first,
Expand All @@ -554,16 +552,13 @@ static void URI_FUNC(DropLeadingZerosInplace)(URI_CHAR * first,
return;
}

{
const URI_CHAR * const remainderFirst =
URI_FUNC(PastLeadingZeros)(first, *afterLast);
const URI_CHAR * const remainderFirst = URI_FUNC(PastLeadingZeros)(first, *afterLast);

if (remainderFirst > first) {
const size_t remainderLen = *afterLast - remainderFirst;
memmove(first, remainderFirst, remainderLen * sizeof(URI_CHAR));
first[remainderLen] = _UT('\0');
*afterLast = first + remainderLen;
}
if (remainderFirst > first) {
const size_t remainderLen = *afterLast - remainderFirst;
memmove(first, remainderFirst, remainderLen * sizeof(URI_CHAR));
first[remainderLen] = _UT('\0');
*afterLast = first + remainderLen;
}
}

Expand All @@ -577,13 +572,10 @@ static void URI_FUNC(AdvancePastLeadingZeros)(const URI_CHAR ** first,
return;
}

{
const URI_CHAR * const remainderFirst =
URI_FUNC(PastLeadingZeros)(*first, afterLast);
const URI_CHAR * const remainderFirst = URI_FUNC(PastLeadingZeros)(*first, afterLast);

/* Cut off leading zeros */
*first = remainderFirst;
}
/* Cut off leading zeros */
*first = remainderFirst;
}

static URI_INLINE int URI_FUNC(NormalizeSyntaxEngine)(URI_TYPE(Uri) * uri,
Expand Down
12 changes: 5 additions & 7 deletions src/UriParse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1361,14 +1361,12 @@ static const URI_CHAR * URI_FUNC(ParseOwnPortUserInfo)(URI_TYPE(ParserState) * s

case _UT('%'):
state->uri->portText.first = NULL; /* Not a port, reset */
{
const URI_CHAR * const afterPct =
URI_FUNC(ParsePctEncoded)(state, first, afterLast, memory);
if (afterPct == NULL) {
return NULL;
}
return URI_FUNC(ParseOwnUserInfo)(state, afterPct, afterLast, memory);
const URI_CHAR * const afterPct =
URI_FUNC(ParsePctEncoded)(state, first, afterLast, memory);
if (afterPct == NULL) {
return NULL;
}
return URI_FUNC(ParseOwnUserInfo)(state, afterPct, afterLast, memory);

case _UT('@'):
state->uri->hostText.afterLast = NULL; /* Not a host, reset */
Expand Down
Loading
Loading