int CPNameUtil_LinuxConvertToRoot(char const *nameIn, // IN: buf to convert size_t bufOutSize, // IN: size of the output buffer char *bufOut) // OUT: output buffer { const size_t shareNameSize = HGFS_STR_LEN(HGFS_SERVER_POLICY_ROOT_SHARE_NAME); int result; ASSERT(nameIn); ASSERT(bufOut); if (bufOutSize <= shareNameSize) { return -1; } /* Prepend the name of the "root" share directly in the output buffer */ memcpy(bufOut, HGFS_SERVER_POLICY_ROOT_SHARE_NAME, shareNameSize); bufOut[shareNameSize] = '\0'; result = CPName_LinuxConvertTo(nameIn, bufOutSize - shareNameSize - 1, bufOut + shareNameSize + 1); /* Return either the same error code or the correct size */ return (result < 0) ? result : (int)(result + shareNameSize + 1); }
int DnD_LegacyConvertToCPName(const char *nameIn, // IN: Buffer to convert size_t bufOutSize, // IN: Size of output buffer char *bufOut) // OUT: Output buffer { const char partialName[] = HGFS_SERVER_POLICY_ROOT_SHARE_NAME; const size_t partialNameLen = HGFS_STR_LEN(HGFS_SERVER_POLICY_ROOT_SHARE_NAME); const char *partialNameSuffix = ""; size_t partialNameSuffixLen; char *fullName; size_t fullNameSize; size_t nameSize; int result; ASSERT(nameIn); ASSERT(bufOut); /* * Create the full name. Note that Str_Asprintf should not be * used here as it uses FormatMessages which interprets 'data', a UTF-8 * string, as a string in the current locale giving wrong results. */ /* * Is this file path a UNC path? */ if (nameIn[0] == WIN_DIRSEPC && nameIn[1] == WIN_DIRSEPC) { partialNameSuffix = WIN_DIRSEPS HGFS_UNC_DIR_NAME WIN_DIRSEPS; partialNameSuffixLen = HGFS_STR_LEN(WIN_DIRSEPS) + HGFS_STR_LEN(HGFS_UNC_DIR_NAME) + HGFS_STR_LEN(WIN_DIRSEPS); } else { partialNameSuffix = WIN_DIRSEPS HGFS_DRIVE_DIR_NAME WIN_DIRSEPS; partialNameSuffixLen = HGFS_STR_LEN(WIN_DIRSEPS) + HGFS_STR_LEN(HGFS_DRIVE_DIR_NAME) + HGFS_STR_LEN(WIN_DIRSEPS); } /* Skip any path separators at the beginning of the input string */ while (*nameIn == WIN_DIRSEPC) { nameIn++; } nameSize = strlen(nameIn); fullNameSize = partialNameLen + partialNameSuffixLen + nameSize; fullName = Util_SafeMalloc(fullNameSize + 1); memcpy(fullName, partialName, partialNameLen); memcpy(fullName + partialNameLen, partialNameSuffix, partialNameSuffixLen); memcpy(fullName + partialNameLen + partialNameSuffixLen, nameIn, nameSize); fullName[fullNameSize] = '\0'; LOG(4, ("%s: generated name is \"%s\"\n", __FUNCTION__, fullName)); /* * CPName_ConvertTo implementation is performed here without calling any * CPName_ functions. This is safer since those functions might change, but * the legacy behavior we are special casing here will not. */ { char const *winNameIn = fullName; char const *origOut = bufOut; char const *endOut = bufOut + bufOutSize; char const pathSep = WIN_DIRSEPC; char *ignores = ":"; /* Skip any path separators at the beginning of the input string */ while (*winNameIn == pathSep) { winNameIn++; } /* * Copy the string to the output buf, converting all path separators into * '\0' and ignoring the specified characters. */ for (; *winNameIn != '\0' && bufOut < endOut; winNameIn++) { if (ignores) { char *currIgnore = ignores; Bool ignore = FALSE; while (*currIgnore != '\0') { if (*winNameIn == *currIgnore) { ignore = TRUE; break; } currIgnore++; } if (!ignore) { *bufOut = (*winNameIn == pathSep) ? '\0' : *winNameIn; bufOut++; } } else { *bufOut = (*winNameIn == pathSep) ? '\0' : *winNameIn; bufOut++; } } /* * NUL terminate. XXX This should go away. * * When we get rid of NUL termination here, this test should * also change to "if (*winNameIn != '\0')". */ if (bufOut == endOut) { result = -1; goto out; } *bufOut = '\0'; /* Path name size should not require more than 4 bytes. */ ASSERT((bufOut - origOut) <= 0xFFFFFFFF); /* If there were any trailing path separators, dont count them [krishnan] */ result = (int)(bufOut - origOut); while ((result >= 1) && (origOut[result - 1] == 0)) { result--; } /* * Make exception and call CPName_Print() here, since it's only for * logging */ LOG(4, ("%s: CPName is \"%s\"\n", __FUNCTION__, CPName_Print(origOut, result))); } out: free(fullName); return result; }
int CPNameUtil_WindowsConvertToRoot(char const *nameIn, // IN: buf to convert size_t bufOutSize, // IN: size of the output buffer char *bufOut) // OUT: output buffer { const char partialName[] = HGFS_SERVER_POLICY_ROOT_SHARE_NAME; const size_t partialNameLen = HGFS_STR_LEN(HGFS_SERVER_POLICY_ROOT_SHARE_NAME); const char *partialNameSuffix = ""; size_t partialNameSuffixLen; char *fullName; size_t fullNameLen; size_t nameLen; int result; ASSERT(nameIn); ASSERT(bufOut); /* * Create the full name. Note that Str_Asprintf should not be * used here as it uses FormatMessages which interprets 'data', a UTF-8 * string, as a string in the current locale giving wrong results. */ /* * Is this file path a UNC path? */ if (nameIn[0] == WIN_DIRSEPC && nameIn[1] == WIN_DIRSEPC) { partialNameSuffix = WIN_DIRSEPS HGFS_UNC_DIR_NAME WIN_DIRSEPS; partialNameSuffixLen = HGFS_STR_LEN(WIN_DIRSEPS) + HGFS_STR_LEN(HGFS_UNC_DIR_NAME) + HGFS_STR_LEN(WIN_DIRSEPS); } else { partialNameSuffix = WIN_DIRSEPS HGFS_DRIVE_DIR_NAME WIN_DIRSEPS; partialNameSuffixLen = HGFS_STR_LEN(WIN_DIRSEPS) + HGFS_STR_LEN(HGFS_DRIVE_DIR_NAME) + HGFS_STR_LEN(WIN_DIRSEPS); } /* Skip any path separators at the beginning of the input string */ while (*nameIn == WIN_DIRSEPC) { nameIn++; } nameLen = strlen(nameIn); fullNameLen = partialNameLen + partialNameSuffixLen + nameLen; fullName = (char *)Util_SafeMalloc(fullNameLen + 1); memcpy(fullName, partialName, partialNameLen); memcpy(fullName + partialNameLen, partialNameSuffix, partialNameSuffixLen); if (nameIn[1] == ':') { /* * If the name is in format "<drive letter>:" strip out ':' from it * because the rest of the code assumes that driver letter in a * platform independent name is represented by a single character without colon. */ fullName[partialNameLen + partialNameSuffixLen] = nameIn[0]; memcpy(fullName + partialNameLen + partialNameSuffixLen + 1, nameIn + 2, nameLen - 2); fullNameLen--; } else { memcpy(fullName + partialNameLen + partialNameSuffixLen, nameIn, nameLen); } fullName[fullNameLen] = '\0'; /* CPName_ConvertTo strips out the ':' character */ result = CPName_WindowsConvertTo(fullName, bufOutSize, bufOut); free(fullName); return result; }