diff --git a/src/detection/physicaldisk/physicaldisk_apple.c b/src/detection/physicaldisk/physicaldisk_apple.c index aba4cf4dd..ce125533d 100644 --- a/src/detection/physicaldisk/physicaldisk_apple.c +++ b/src/detection/physicaldisk/physicaldisk_apple.c @@ -113,9 +113,9 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) if (deviceCharacteristics) { ffCfDictGetString(deviceCharacteristics, CFSTR(kIOPropertyProductSerialNumberKey), &device->serial); - ffStrbufTrim(&device->serial, ' '); + ffStrbufTrimSpace(&device->serial); ffCfDictGetString(deviceCharacteristics, CFSTR(kIOPropertyProductRevisionLevelKey), &device->revision); - ffStrbufTrim(&device->revision, ' '); + ffStrbufTrimRightSpace(&device->revision); CFStringRef mediumType = (CFStringRef) CFDictionaryGetValue(deviceCharacteristics, CFSTR(kIOPropertyMediumTypeKey)); if (mediumType) diff --git a/src/detection/physicaldisk/physicaldisk_bsd.c b/src/detection/physicaldisk/physicaldisk_bsd.c index 562fbb3cf..9bb4669dd 100644 --- a/src/detection/physicaldisk/physicaldisk_bsd.c +++ b/src/detection/physicaldisk/physicaldisk_bsd.c @@ -65,7 +65,7 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) FFPhysicalDiskResult* device = (FFPhysicalDiskResult*) ffListAdd(result); ffStrbufInitF(&device->devPath, "/dev/%s", provider->lg_name); ffStrbufInitMove(&device->serial, &identifier); - ffStrbufTrim(&device->serial, ' '); + ffStrbufTrimSpace(&device->serial); ffStrbufInit(&device->revision); ffStrbufInit(&device->interconnect); switch (snapIter->device_type & DEVSTAT_TYPE_IF_MASK) diff --git a/src/detection/physicaldisk/physicaldisk_linux.c b/src/detection/physicaldisk/physicaldisk_linux.c index 11923f4d4..f4f46a0fe 100644 --- a/src/detection/physicaldisk/physicaldisk_linux.c +++ b/src/detection/physicaldisk/physicaldisk_linux.c @@ -153,7 +153,7 @@ static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOption { ffStrbufInit(&device->serial); if (ffReadFileBufferRelative(devfd, "serial", &device->serial)) - ffStrbufTrim(&device->serial, ' '); + ffStrbufTrimSpace(&device->serial); } { diff --git a/src/detection/physicaldisk/physicaldisk_sunos.c b/src/detection/physicaldisk/physicaldisk_sunos.c index f3b4b3de8..fb14c889e 100644 --- a/src/detection/physicaldisk/physicaldisk_sunos.c +++ b/src/detection/physicaldisk/physicaldisk_sunos.c @@ -36,9 +36,15 @@ static int walkDevTree(di_node_t node, di_minor_t minor, struct FFWalkTreeBundle char* buf; if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "inquiry-serial-no", &buf) > 0) + { ffStrbufSetS(&device->serial, buf); + ffStrbufTrimSpace(&device->serial); + } if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "inquiry-revision-id", &buf) > 0) + { ffStrbufSetS(&device->revision, buf); + ffStrbufTrimRightSpace(&device->revision); + } if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "class", &buf) > 0) ffStrbufSetS(&device->interconnect, buf); diff --git a/src/detection/physicaldisk/physicaldisk_windows.c b/src/detection/physicaldisk/physicaldisk_windows.c index 486f6aa18..0a6e76d58 100644 --- a/src/detection/physicaldisk/physicaldisk_windows.c +++ b/src/detection/physicaldisk/physicaldisk_windows.c @@ -62,14 +62,14 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFPhysic if (sdd->SerialNumberOffset != 0) { ffStrbufSetS(&device->serial, (const char*) sddBuffer + sdd->SerialNumberOffset); - ffStrbufTrim(&device->serial, ' '); + ffStrbufTrimSpace(&device->serial); } ffStrbufInit(&device->revision); if (sdd->ProductRevisionOffset != 0) { ffStrbufSetS(&device->revision, (const char*) sddBuffer + sdd->ProductRevisionOffset); - ffStrbufTrim(&device->revision, ' '); + ffStrbufTrimRightSpace(&device->revision); } device->type |= sdd->RemovableMedia ? FF_PHYSICALDISK_TYPE_REMOVABLE : FF_PHYSICALDISK_TYPE_FIXED; diff --git a/src/util/FFstrbuf.c b/src/util/FFstrbuf.c index 3fcebbd2e..943d0de82 100644 --- a/src/util/FFstrbuf.c +++ b/src/util/FFstrbuf.c @@ -272,6 +272,31 @@ void ffStrbufTrimRight(FFstrbuf* strbuf, char c) strbuf->chars[strbuf->length] = '\0'; } +void ffStrbufTrimLeftSpace(FFstrbuf* strbuf) +{ + if(strbuf->length == 0) + return; + + uint32_t index = 0; + while(index < strbuf->length && isspace(strbuf->chars[index])) + ++index; + + if(index == 0) + return; + + if(strbuf->allocated == 0) + { + //static string + strbuf->length -= index; + strbuf->chars += index; + return; + } + + memmove(strbuf->chars, strbuf->chars + index, strbuf->length - index); + strbuf->length -= index; + strbuf->chars[strbuf->length] = '\0'; +} + void ffStrbufTrimRightSpace(FFstrbuf* strbuf) { if (strbuf->length == 0) diff --git a/src/util/FFstrbuf.h b/src/util/FFstrbuf.h index 841a4634d..d7fdab354 100644 --- a/src/util/FFstrbuf.h +++ b/src/util/FFstrbuf.h @@ -53,6 +53,7 @@ FF_C_PRINTF(2, 3) void ffStrbufSetF(FFstrbuf* strbuf, const char* format, ...); void ffStrbufTrimLeft(FFstrbuf* strbuf, char c); void ffStrbufTrimRight(FFstrbuf* strbuf, char c); +void ffStrbufTrimLeftSpace(FFstrbuf* strbuf); void ffStrbufTrimRightSpace(FFstrbuf* strbuf); bool ffStrbufRemoveSubstr(FFstrbuf* strbuf, uint32_t startIndex, uint32_t endIndex); @@ -473,7 +474,7 @@ static inline FF_C_NODISCARD bool ffStrbufEndsWithS(const FFstrbuf* strbuf, cons return ffStrbufEndsWithNS(strbuf, (uint32_t) strlen(end), end); } -static inline FF_C_NODISCARD bool ffStrbufEndsWithFn(const FFstrbuf* strbuf, int (*fn)(int)) +static inline FF_C_NODISCARD bool ffStrbufEndsWithFn(const FFstrbuf* strbuf, int (*const fn)(int)) { return strbuf->length == 0 ? false : fn(strbuf->chars[strbuf->length - 1]); @@ -507,6 +508,12 @@ static inline void ffStrbufTrim(FFstrbuf* strbuf, char c) ffStrbufTrimLeft(strbuf, c); } +static inline void ffStrbufTrimSpace(FFstrbuf* strbuf) +{ + ffStrbufTrimRightSpace(strbuf); + ffStrbufTrimLeftSpace(strbuf); +} + static inline bool ffStrbufMatchSeparatedS(const FFstrbuf* strbuf, const char* comp, char separator) { return ffStrbufMatchSeparatedNS(strbuf, (uint32_t) strlen(comp), comp, separator); diff --git a/tests/strbuf.c b/tests/strbuf.c index 0b786e18d..8d2c0131c 100644 --- a/tests/strbuf.c +++ b/tests/strbuf.c @@ -371,6 +371,22 @@ int main(void) VERIFY(strbuf.allocated > 0); ffStrbufDestroy(&strbuf); + //ffStrbufCreateStatic / TrimSpace + ffStrbufInitStatic(&strbuf, "\n TEST\n "); + ffStrbufTrimSpace(&strbuf); + VERIFY(strbuf.length == 4); + VERIFY(strbuf.allocated > 0); + VERIFY(ffStrbufEqualS(&strbuf, "TEST")); + ffStrbufDestroy(&strbuf); + + //ffStrbufCreate / TrimSpace + ffStrbufInitS(&strbuf, "\n TEST\n "); + ffStrbufTrimSpace(&strbuf); + VERIFY(strbuf.length == 4); + VERIFY(strbuf.allocated > 0); + VERIFY(ffStrbufEqualS(&strbuf, "TEST")); + ffStrbufDestroy(&strbuf); + //ffStrbufEnsureFixedLengthFree / empty buffer ffStrbufInit(&strbuf); ffStrbufEnsureFixedLengthFree(&strbuf, 10);