Skip to content

Commit 29eedf9

Browse files
olszomalmtrojnar
authored andcommitted
Fixed DIFAT sectors writing
1 parent d6f94d7 commit 29eedf9

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

msi.c

+20-13
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,16 @@ typedef struct {
186186
char *ministream;
187187
char *minifat;
188188
char *fat;
189+
char *difat;
189190
uint32_t dirtreeLen;
190191
uint32_t miniStreamLen;
191192
uint32_t minifatLen;
192193
uint32_t fatLen;
194+
uint32_t difatLen;
193195
uint32_t ministreamsMemallocCount;
194196
uint32_t minifatMemallocCount;
195197
uint32_t fatMemallocCount;
198+
uint32_t difatMemallocCount;
196199
uint32_t dirtreeSectorsCount;
197200
uint32_t minifatSectorsCount;
198201
uint32_t fatSectorsCount;
@@ -1542,6 +1545,16 @@ static void fat_append(MSI_OUT *out, char *buf, uint32_t len)
15421545
out->fatLen += len;
15431546
}
15441547

1548+
static void difat_append(MSI_OUT *out, char *buf, uint32_t len)
1549+
{
1550+
if (out->difatLen == (uint64_t)out->difatMemallocCount * out->sectorSize) {
1551+
out->difatMemallocCount += 1;
1552+
out->difat = OPENSSL_realloc(out->difat, (size_t)(out->difatMemallocCount * out->sectorSize));
1553+
}
1554+
memcpy(out->difat + out->difatLen, buf, (size_t)len);
1555+
out->difatLen += len;
1556+
}
1557+
15451558
static int msi_dirent_delete(MSI_DIRENT *dirent, const u_char *name, uint16_t nameLen)
15461559
{
15471560
int i;
@@ -1938,15 +1951,6 @@ static void dirtree_save(MSI_DIRENT *dirent, BIO *outdata, MSI_OUT *out)
19381951
out->sectorNum += out->dirtreeSectorsCount;
19391952
}
19401953

1941-
static void fat_pad_last_sector(MSI_OUT *out, int padValue, char *buf)
1942-
{
1943-
if (out->fatLen % out->sectorSize > 0) {
1944-
uint32_t remain = out->sectorSize - out->fatLen % out->sectorSize;
1945-
memset(buf, padValue, (size_t)remain);
1946-
fat_append(out, buf, remain);
1947-
}
1948-
}
1949-
19501954
static int fat_save(BIO *outdata, MSI_OUT *out)
19511955
{
19521956
char buf[MAX_SECTOR_SIZE];
@@ -1955,8 +1959,6 @@ static int fat_save(BIO *outdata, MSI_OUT *out)
19551959
remain = (out->fatLen + out->sectorSize - 1) / out->sectorSize;
19561960
out->fatSectorsCount = (out->fatLen + remain * 4 + out->sectorSize - 1) / out->sectorSize;
19571961

1958-
fat_pad_last_sector(out, 0, buf);
1959-
19601962
if (out->fatSectorsCount > DIFAT_IN_HEADER) {
19611963
difatEntriesPerSector = (out->sectorSize / 4) - 1;
19621964
difatSectors = (out->fatSectorsCount - DIFAT_IN_HEADER + difatEntriesPerSector - 1) / difatEntriesPerSector;
@@ -2001,7 +2003,7 @@ static int fat_save(BIO *outdata, MSI_OUT *out)
20012003
PUT_UINT32_LE(out->sectorNum + 1, buf + out->sectorSize - 4);
20022004
}
20032005

2004-
fat_append(out, buf, out->sectorSize);
2006+
difat_append(out, buf, out->sectorSize);
20052007
out->sectorNum++;
20062008
}
20072009
}
@@ -2019,9 +2021,14 @@ static int fat_save(BIO *outdata, MSI_OUT *out)
20192021
}
20202022

20212023
/* empty unallocated free sectors in the last FAT sector */
2022-
fat_pad_last_sector(out, (int)FREESECT, buf);
2024+
if (out->fatLen % out->sectorSize > 0) {
2025+
remain = out->sectorSize - out->fatLen % out->sectorSize;
2026+
memset(buf, (int)FREESECT, (size_t)remain);
2027+
fat_append(out, buf, remain);
2028+
}
20232029

20242030
BIO_write(outdata, out->fat, (int)out->fatLen);
2031+
BIO_write(outdata, out->difat, (int)out->difatLen);
20252032
return 1; /* OK */
20262033
}
20272034

0 commit comments

Comments
 (0)