Skip to content

Commit 3252f46

Browse files
eduard-sukharevTracy Spiva
authored andcommitted
🐛 Fix long filename read/report (MarlinFirmware#25509)
1 parent c1d0b18 commit 3252f46

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

Marlin/src/sd/SdBaseFile.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,8 +1013,7 @@ bool SdBaseFile::openNext(SdBaseFile *dirFile, uint8_t oflag) {
10131013
* \return false if the dirname is a short file name 8.3 (SFN)
10141014
*/
10151015
bool SdBaseFile::isDirNameLFN(const char *dirname) {
1016-
uint8_t length = strlen(dirname);
1017-
uint8_t idx = length;
1016+
uint8_t length = strlen(dirname), idx = length;
10181017
bool dotFound = false;
10191018
if (idx > 12) return true; // LFN due to filename length > 12 ("filename.ext")
10201019
// Check dot(s) position
@@ -1508,31 +1507,31 @@ int8_t SdBaseFile::readDir(dir_t *dir, char *longFilename) {
15081507
if (DIR_IS_FILE_OR_SUBDIR(dir)) {
15091508
#if ENABLED(UTF_FILENAME_SUPPORT)
15101509
#if LONG_FILENAME_CHARSIZE > 2
1511-
// Add warning for developers for currently not supported 3-byte cases (Conversion series of 2-byte
1512-
// codepoints to 3-byte in-place will break the rest of filename)
1510+
// Add warning for developers for unsupported 3-byte cases.
1511+
// (Converting 2-byte codepoints to 3-byte in-place would break the rest of filename.)
15131512
#error "Currently filename re-encoding is done in-place. It may break the remaining chars to use 3-byte codepoints."
15141513
#endif
15151514

15161515
// Is there a long filename to decode?
15171516
if (longFilename) {
15181517
// Reset n to the start of the long name
15191518
n = 0;
1520-
for (uint16_t idx = 0; idx < (LONG_FILENAME_LENGTH) / 2; idx += 2) { // idx is fixed since FAT LFN always contains UTF-16LE encoding
1519+
for (uint16_t idx = 0; idx < (LONG_FILENAME_LENGTH); idx += 2) { // idx is fixed since FAT LFN always contains UTF-16LE encoding
15211520
const uint16_t utf16_ch = longFilename[idx] | (longFilename[idx + 1] << 8);
1522-
if (0xD800 == (utf16_ch & 0xF800)) // Surrogate pair - encode as '_'
1521+
if (0xD800 == (utf16_ch & 0xF800)) // Surrogate pair - encode as '_'
15231522
longFilename[n++] = '_';
1524-
else if (0 == (utf16_ch & 0xFF80)) // Encode as 1-byte UTF-8 char
1523+
else if (0 == (utf16_ch & 0xFF80)) // Encode as 1-byte UTF-8 char
15251524
longFilename[n++] = utf16_ch & 0x007F;
1526-
else if (0 == (utf16_ch & 0xF800)) { // Encode as 2-byte UTF-8 char
1525+
else if (0 == (utf16_ch & 0xF800)) { // Encode as 2-byte UTF-8 char
15271526
longFilename[n++] = 0xC0 | ((utf16_ch >> 6) & 0x1F);
15281527
longFilename[n++] = 0x80 | ( utf16_ch & 0x3F);
15291528
}
15301529
else {
1531-
#if LONG_FILENAME_CHARSIZE > 2 // Encode as 3-byte UTF-8 char
1530+
#if LONG_FILENAME_CHARSIZE > 2 // Encode as 3-byte UTF-8 char
15321531
longFilename[n++] = 0xE0 | ((utf16_ch >> 12) & 0x0F);
15331532
longFilename[n++] = 0xC0 | ((utf16_ch >> 6) & 0x3F);
15341533
longFilename[n++] = 0xC0 | ( utf16_ch & 0x3F);
1535-
#else // Encode as '_'
1534+
#else // Encode as '_'
15361535
longFilename[n++] = '_';
15371536
#endif
15381537
}

0 commit comments

Comments
 (0)