12 namespace FilePathUtils {
17 return measuredLen < bufferLen;
28 if (baseDir ==
nullptr) {
35 if (baseDirLen == 0 || baseDir[0] !=
'/') {
42 const FwSizeType needsSlash = (baseDir[baseDirLen - 1] !=
'/') ? 1 : 0;
43 if ((baseDirLen + needsSlash + pathLen + 1) > resolvedSize) {
46 (void)std::memcpy(resolvedOut, baseDir, baseDirLen);
49 resolvedOut[pos++] =
'/';
51 (void)std::memcpy(&resolvedOut[pos], path, pathLen);
53 resolvedOut[pos] =
'\0';
59 if (pathLen + 1 > resolvedSize) {
62 (void)std::memcpy(resolvedOut, path, pathLen + 1);
88 for (
FwSizeType readPos = 1; readPos <= pathLength;) {
93 for (; readPos < pathLength && resolvedOut[readPos] !=
'/'; readPos++) {
98 if (readPos < pathLength) {
101 readPos = pathLength + 1;
109 if (segLen == 1 && resolvedOut[segStart] ==
'.') {
113 if (segLen == 2 && resolvedOut[segStart] ==
'.' && resolvedOut[segStart + 1] ==
'.') {
116 for (; writePos > 1 && resolvedOut[writePos - 1] !=
'/'; writePos--) {
123 (void)std::memmove(&resolvedOut[writePos], &resolvedOut[segStart], segLen);
125 resolvedOut[writePos++] =
'/';
132 resolvedOut[writePos] =
'\0';
138 if (path ==
nullptr || path[0] ==
'\0') {
143 if (buildStatus !=
VALID) {
153 char* outBuffer =
const_cast<char*
>(resolvedOut.
toChar());
164 if (path[0] !=
'/') {
170 return resolvePath(path, cwdBuffer, resolvedOut, resolvedSize);
172 return resolvePath(path,
"/", resolvedOut, resolvedSize);
188 if (allowedLen == 0 || pathLen == 0) {
191 if (allowedDirectory[allowedLen - 1] !=
'/') {
198 if (pathLen == allowedLen - 1 && std::memcmp(resolvedPath, allowedDirectory, pathLen) == 0) {
203 if (pathLen < allowedLen) {
206 if (std::memcmp(resolvedPath, allowedDirectory, allowedLen) != 0) {
Resolved path falls outside the allowed directory.
static bool isValidCString(FwSizeType measuredLen, FwSizeType bufferLen)
PlatformSizeType FwSizeType
Path is malformed or cannot be resolved.
virtual const CHAR * toChar() const =0
Convert to a C-style char*.
Status checkContainment(const char *resolvedPath, const char *allowedDirectory)
Check whether an already-resolved path is within an allowed directory.
static Status resolveInPlace(char *resolvedOut)
static Status buildAbsolutePath(const char *path, const char *baseDir, char *resolvedOut, FwSizeType resolvedSize)
static constexpr FwSizeType MAX_PATH_LENGTH
Maximum supported path length for resolution buffers.
virtual SizeType getCapacity() const =0
Return the size of the buffer.
static Status getWorkingDirectory(char *path, FwSizeType bufferSize)
Get the current working directory.
Path is valid and within the allowed directory.
Status resolveFromCwd(const char *path, char *resolvedOut, FwSizeType resolvedSize)
Resolve a path using CWD as the base for relative paths.
A read-only abstract superclass for StringBase.
Status resolvePath(const char *path, const char *baseDir, char *resolvedOut, FwSizeType resolvedSize)
Resolve a path by collapsing . and .. segments.
Combined path length exceeds the output buffer size.
FwSizeType string_length(const CHAR *source, FwSizeType buffer_size)
get the length of the source string