Skip to content

Commit

Permalink
MString:
Browse files Browse the repository at this point in the history
 - Fix issue with not taking zero terminator into account after resizing
  • Loading branch information
Meulengracht committed Feb 2, 2021
1 parent 7071781 commit a42658f
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 119 deletions.
10 changes: 5 additions & 5 deletions librt/libds/include/ds/mstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ struct MString;
typedef struct MString MString_t;

_CODE_BEGIN
DSDECL(MString_t*, MStringCreate(const char* Data, MStringType_t DataType));
DSDECL(MString_t*, MStringCreate(const char * Data, MStringType_t DataType));
DSDECL(MString_t*, MStringClone(MString_t* String));
DSDECL(void, MStringZero(MString_t* String));
DSDECL(void, MStringReset(MString_t* String, const char* NewString, MStringType_t DataType));
DSDECL(void, MStringReset(MString_t* String, const char * NewString, MStringType_t DataType));
DSDECL(void, MStringCopy(MString_t* Destination, MString_t* Source, int DestinationIndex, int SourceIndex, int Length));
DSDECL(void, MStringDestroy(MString_t * string));

// Append Character to a given string the character is assumed to be either ASCII, UTF16 or UTF32
DSDECL(void, MStringAppend(MString_t* Destination, MString_t* String));
DSDECL(void, MStringAppendCharacter(MString_t* String, mchar_t Character));
DSDECL(void, MStringAppendCharacters(MString_t* String, const char* Characters, MStringType_t DataType));
DSDECL(void, MStringAppend(MString_t * destination, MString_t * source));
DSDECL(void, MStringAppendCharacter(MString_t * string, mchar_t character));
DSDECL(void, MStringAppendCharacters(MString_t * string, const char * characters, MStringType_t dataType));
DSDECL(void, MStringAppendInt32(MString_t* String, int32_t Value));
DSDECL(void, MStringAppendUInt32(MString_t* String, uint32_t Value));
DSDECL(void, MStringAppendHex32(MString_t* String, uint32_t Value));
Expand Down
83 changes: 43 additions & 40 deletions librt/libds/mstring/mstringappend.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,62 +26,65 @@

void
MStringAppend(
_In_ MString_t* Destination,
_In_ MString_t* String)
_In_ MString_t* destination,
_In_ MString_t* source)
{
uint8_t *StringPtr;
size_t NewLength;
uint8_t* data;
size_t newLength;

assert(Destination != NULL);
assert(String != NULL);

NewLength = Destination->Length + String->Length;

// Sanitize if destination has enough space for both buffers
if (NewLength >= Destination->MaxLength) {
MStringResize(Destination, NewLength);
if (!destination || !source) {
return;
}

StringPtr = (uint8_t*)Destination->Data;
memcpy(StringPtr + Destination->Length, String->Data, String->Length);
StringPtr[NewLength] = '\0';
Destination->Length = NewLength;
// ensure that we have enough space in destination
newLength = destination->Length + source->Length;
MStringResize(destination, newLength);

data = (uint8_t*)destination->Data;
memcpy(data + destination->Length, source->Data, source->Length);
data[newLength] = '\0';
destination->Length = newLength;
}

void
MStringAppendCharacter(
_In_ MString_t* String,
_In_ mchar_t Character)
_In_ MString_t* string,
_In_ mchar_t character)
{
uint8_t* StringPtr;
size_t NewLength;
size_t ExtraLength = 0;
int i = 0;
uint8_t* data;
size_t newLength;
size_t extraLength = 0;
int i;

assert(String != NULL);

NewLength = String->Length + Utf8ByteSizeOfCharacterInUtf8(Character);
if (NewLength >= String->MaxLength) {
MStringResize(String, NewLength);
if (!string) {
return;
}
StringPtr = (uint8_t*)String->Data;
i = String->Length;

Utf8ConvertCharacterToUtf8(Character, (void*)&StringPtr[i], &ExtraLength);
StringPtr[i + ExtraLength] = '\0';
String->Length += ExtraLength;
newLength = string->Length + Utf8ByteSizeOfCharacterInUtf8(character);
MStringResize(string, newLength);

data = (uint8_t*)string->Data;
i = string->Length;

Utf8ConvertCharacterToUtf8(character, (void*)&data[i], &extraLength);
data[i + extraLength] = '\0';

string->Length += extraLength;
}

void
MStringAppendCharacters(
_In_ MString_t* String,
_In_ const char* Characters,
_In_ MStringType_t DataType)
_In_ MString_t* string,
_In_ const char* characters,
_In_ MStringType_t dataType)
{
assert(String != NULL);
assert(Characters != NULL);
MString_t* proxy;

if (!string || !characters) {
return;
}

MString_t* Proxy = MStringCreate(Characters, DataType);
MStringAppend(String, Proxy);
MStringDestroy(Proxy);
proxy = MStringCreate(characters, dataType);
MStringAppend(string, proxy);
MStringDestroy(proxy);
}
39 changes: 15 additions & 24 deletions librt/libds/mstring/mstringcopy.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* MollenOS
/**
* MollenOS
*
* Copyright 2011, Philip Meulengracht
*
Expand Down Expand Up @@ -36,44 +37,34 @@ MStringCopy(
}

/* If -1, copy all from source */
if (Length == -1)
{
/* Is destination large enough? */
if (Source->Length >= Destination->MaxLength) {
MStringResize(Destination, Source->MaxLength);
}
if (Length == -1) {
// ensure large enough buffer
MStringResize(Destination, Source->MaxLength);

/* Copy */
memcpy(Destination->Data, Source->Data, Source->Length);

/* Update length */
Destination->Length = Source->Length;
}
else
{
else {
/* Calculate byte length to copy */
char *DataPtr = (char*)Source->Data;
char* srcData = (char*)Source->Data;
uint8_t* destData;
int Count = Length, Index = 0;

/* Iterate */
while (DataPtr[Index] && Count) {
mchar_t NextCharacter = Utf8GetNextCharacterInString(DataPtr, &Index);
while (srcData[Index] && Count) {
mchar_t NextCharacter = Utf8GetNextCharacterInString(srcData, &Index);
if (NextCharacter == MSTRING_EOS) {
break;
}
Count--;
}

/* Is destination large enough? */
if ((size_t)Index >= Destination->MaxLength) {
MStringResize(Destination, Index);
}
// ensure space enough for the copy
MStringResize(Destination, Index);

/* Copy */
// copy the data over and then we null terminate
memcpy(Destination->Data, Source->Data, Index);

/* Null Terminate */
uint8_t *NullPtr = (uint8_t*)Destination->Data;
NullPtr[Index] = '\0';
destData = (uint8_t*)Destination->Data;
destData[Index] = '\0';
}
}
67 changes: 36 additions & 31 deletions librt/libds/mstring/mstringcreate.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,34 @@

static int
MStringConvertASCIIToUtf8(
_In_ MString_t* Storage,
_In_ const char* Source)
_In_ MString_t* destination,
_In_ const char* source)
{
char *dPtr = NULL;
char *cPtr = (char*)Source;
size_t DataLength = 0;
char* destData = NULL;
char* srcData = (char*)source;
size_t dataLength;

// Get the length of the data
Storage->Length = 0;
while (*cPtr) {
Storage->Length += Utf8ByteSizeOfCharacterInUtf8((mchar_t)*cPtr);
cPtr++;
destination->Length = 0;
while (*srcData) {
destination->Length += Utf8ByteSizeOfCharacterInUtf8((mchar_t)*srcData);
srcData++;
}

DataLength = DIVUP((Storage->Length + 1), MSTRING_BLOCK_SIZE) * MSTRING_BLOCK_SIZE;
Storage->Data = (void*)dsalloc(DataLength);
Storage->MaxLength = DataLength;
memset(Storage->Data, 0, DataLength);
dataLength = DIVUP((destination->Length + 1), MSTRING_BLOCK_SIZE) * MSTRING_BLOCK_SIZE;
destination->Data = (void*)dsalloc(dataLength);
destination->MaxLength = dataLength;
memset(destination->Data, 0, dataLength);

dPtr = Storage->Data;
cPtr = (char*)Source;
while (*cPtr) {
destData = destination->Data;
srcData = (char*)source;
while (*srcData) {
size_t Bytes = 0;

if (!Utf8ConvertCharacterToUtf8((mchar_t)*cPtr, dPtr, &Bytes)) {
dPtr += Bytes;
if (!Utf8ConvertCharacterToUtf8((mchar_t)*srcData, destData, &Bytes)) {
destData += Bytes;
}
cPtr++;
srcData++;
}
return 0;
}
Expand Down Expand Up @@ -164,28 +164,31 @@ MStringConvertUtf32ToUtf8(

static int
MStringCopyUtf8ToUtf8(
_In_ MString_t* Storage,
_In_ const char* Source)
_In_ MString_t* destination,
_In_ const char* source)
{
size_t DataLength;
assert(Source != NULL);
size_t dataLength;

Storage->Length = strlen(Source);
DataLength = DIVUP((Storage->Length + 1), MSTRING_BLOCK_SIZE) * MSTRING_BLOCK_SIZE;
if (!destination || !source) {
return -1;
}

Storage->Data = (void*)dsalloc(DataLength);
Storage->MaxLength = DataLength;
destination->Length = strlen(source);
dataLength = DIVUP((destination->Length + 1), MSTRING_BLOCK_SIZE) * MSTRING_BLOCK_SIZE;

memset(Storage->Data, 0, DataLength);
memcpy(Storage->Data, (const void*)Source, Storage->Length);
destination->Data = (void*)dsalloc(dataLength);
destination->MaxLength = dataLength;

memset(destination->Data, 0, dataLength);
memcpy(destination->Data, (const void*)source, destination->Length);
return 0;
}

static void
MStringNull(
_In_ MString_t* Storage)
{
if (Storage->Data == NULL) {
if (!Storage->Data) {
Storage->Data = dsalloc(MSTRING_BLOCK_SIZE);
Storage->MaxLength = MSTRING_BLOCK_SIZE;
}
Expand All @@ -199,7 +202,9 @@ MStringReset(
_In_ const char* NewString,
_In_ MStringType_t DataType)
{
assert(String != NULL);
if (!String) {
return;
}

if (String->Data != NULL) {
dsfree(String->Data);
Expand Down
3 changes: 2 additions & 1 deletion librt/libds/mstring/mstringprint.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* MollenOS
/**
* MollenOS
*
* Copyright 2011, Philip Meulengracht
*
Expand Down
2 changes: 1 addition & 1 deletion librt/libds/mstring/mstringprivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,6 @@ CRTDECL(size_t, Utf8ByteCountInString(const char *Str));
/* Helper for internal functions
* to automatically resize the buffer
* of a string to be able to fit a certain size */
CRTDECL(void, MStringResize(MString_t *String, size_t Length));
CRTDECL(void, MStringResize(MString_t * string, size_t size));

#endif //!_MSTRING_PRIV_H_
33 changes: 19 additions & 14 deletions librt/libds/mstring/mstringresize.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,28 @@

#include "mstringprivate.h"

void MStringResize(MString_t *String, size_t Length)
void MStringResize(
_In_ MString_t* string,
_In_ size_t size)
{
/* Calculate the new byte-count we
* need to encompass with blocks */
size_t DataLength = DIVUP(Length, MSTRING_BLOCK_SIZE) * MSTRING_BLOCK_SIZE;
void* data;
size_t dataLength;

/* Expand and reset buffer */
void *Data = dsalloc(DataLength);
memset(Data, 0, DataLength);
// add one to size to account for null terminator
dataLength = DIVUP(size + 1, MSTRING_BLOCK_SIZE) * MSTRING_BLOCK_SIZE;
if (dataLength <= string->MaxLength) {
return;
}

/* Copy old data over */
memcpy(Data, String->Data, String->Length);
data = dsalloc(dataLength);
if (!data) {
return;
}

/* Free the old buffer */
dsfree(String->Data);
memset(data, 0, dataLength);
memcpy(data, string->Data, string->Length);
dsfree(string->Data);

/* Update string to new buffer */
String->MaxLength = DataLength;
String->Data = Data;
string->MaxLength = dataLength;
string->Data = data;
}
3 changes: 3 additions & 0 deletions services/filemanager/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ VfsResolvePath(
if (ProcessGetWorkingDirectory(processId, &basePath[0], _MAXPATH) == OsError) {
if (VfsGuessBasePath(path, &basePath[0]) == OsError) {
ERROR("Failed to guess the base path for path %s", path);
free(basePath);
return NULL;
}
}
Expand All @@ -296,6 +297,8 @@ VfsResolvePath(
}
strcat(basePath, path);
resolvedPath = VfsPathCanonicalize(basePath);

free(basePath);
}
else {
resolvedPath = VfsPathCanonicalize(path);
Expand Down
6 changes: 3 additions & 3 deletions services/filemanager/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ VfsPathCanonicalize(
}

if (previousIndex != MSTRING_NOT_FOUND) {
TRACE("Going back in %s", MStringRaw(absolutePath));
TRACE("[vfs] [path] going back in %s", MStringRaw(absolutePath));
MString_t* subPath = MStringSubString(absolutePath, 0, previousIndex + 1); // Include the '/'
if (subPath) {
MStringDestroy(absolutePath);
Expand All @@ -223,8 +223,8 @@ VfsPathCanonicalize(
else {
// Don't double add '/'
if (IS_SEPERATOR(&path[i])) {
int Index = MStringFindReverse(absolutePath, '/', 0);
if ((Index + 1) != MStringLength(absolutePath)) {
int seperatorIndex = MStringFindReverse(absolutePath, '/', 0);
if ((seperatorIndex + 1) != MStringLength(absolutePath)) {
MStringAppendCharacter(absolutePath, '/');
}
}
Expand Down

0 comments on commit a42658f

Please sign in to comment.