Skip to content

Commit

Permalink
refactor: More flexible field copying macro
Browse files Browse the repository at this point in the history
  • Loading branch information
abelcheung committed Dec 23, 2023
1 parent 5d334f6 commit 656140a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
12 changes: 6 additions & 6 deletions src/rifiuti-vista.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ _validate_index_file (const char *filename,
goto validate_fail;
}

copy_field (ver, VERSION_OFFSET, FILESIZE_OFFSET);
copy_field (*ver, buf, VERSION_OFFSET, FILESIZE_OFFSET);
*ver = GUINT64_FROM_LE (*ver);
g_debug ("version = %" PRIu64, *ver);

Expand All @@ -86,7 +86,7 @@ _validate_index_file (const char *filename,

// Version 2 adds a uint32 file name strlen before file name.
// This presumably breaks the 260 char barrier in version 1.
copy_field (&pathlen, VERSION1_FILENAME_OFFSET, VERSION2_FILENAME_OFFSET);
copy_field (pathlen, buf, VERSION1_FILENAME_OFFSET, VERSION2_FILENAME_OFFSET);
pathlen = GUINT32_FROM_LE (pathlen);

/* Header length + strlen in UTF-16 encoding */
Expand Down Expand Up @@ -163,8 +163,8 @@ _populate_record_data (void *buf,
record = g_malloc0 (sizeof (rbin_struct));
record->version = version;

memcpy (&record->filesize, buf + FILESIZE_OFFSET,
FILETIME_OFFSET - FILESIZE_OFFSET - (int) erraneous);
copy_field (record->filesize, buf, FILESIZE_OFFSET,
FILETIME_OFFSET - (int) erraneous);
if (erraneous)
{
g_debug ("filesize field broken, 56 bit only, val=0x%" PRIX64,
Expand All @@ -179,8 +179,8 @@ _populate_record_data (void *buf,
}

/* File deletion time */
memcpy (&record->winfiletime, buf - (int) erraneous + FILETIME_OFFSET,
VERSION1_FILENAME_OFFSET - FILETIME_OFFSET);
copy_field (record->winfiletime, buf - (int) erraneous,
FILETIME_OFFSET, VERSION1_FILENAME_OFFSET);
record->winfiletime = GINT64_FROM_LE (record->winfiletime);
record->deltime = win_filetime_to_gdatetime (record->winfiletime);

Expand Down
21 changes: 12 additions & 9 deletions src/rifiuti.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ _validate_index_file (const char *filename,
goto validation_fail;
}

copy_field (&ver, VERSION_OFFSET, KEPT_ENTRY_OFFSET);
copy_field (ver, buf, VERSION_OFFSET, KEPT_ENTRY_OFFSET);
ver = GUINT32_FROM_LE (ver);

// total_entry only meaningful for 95 and NT4, on other versions
// it's junk memory data, don't bother copying
if ( ( ver == VERSION_NT4 ) || ( ver == VERSION_WIN95 ) ) {
copy_field (&meta->total_entry, TOTAL_ENTRY_OFFSET, RECORD_SIZE_OFFSET);
copy_field (meta->total_entry, buf, TOTAL_ENTRY_OFFSET, RECORD_SIZE_OFFSET);
meta->total_entry = GUINT32_FROM_LE (meta->total_entry);
}

copy_field (&meta->recordsize, RECORD_SIZE_OFFSET, FILESIZE_SUM_OFFSET);
copy_field (meta->recordsize, buf, RECORD_SIZE_OFFSET, FILESIZE_SUM_OFFSET);
meta->recordsize = GUINT32_FROM_LE (meta->recordsize);

g_free (buf);
Expand Down Expand Up @@ -150,15 +150,16 @@ _populate_record_data (void *buf,

// Verbatim path in ANSI code page
record->raw_legacy_path = g_malloc0 (RECORD_INDEX_OFFSET - LEGACY_FILENAME_OFFSET);
copy_field (record->raw_legacy_path, LEGACY_FILENAME_OFFSET, RECORD_INDEX_OFFSET);
copy_field (*(record->raw_legacy_path), buf,
LEGACY_FILENAME_OFFSET, RECORD_INDEX_OFFSET);

/* Index number associated with the record */
copy_field (&record->index_n, RECORD_INDEX_OFFSET, DRIVE_LETTER_OFFSET);
copy_field (record->index_n, buf, RECORD_INDEX_OFFSET, DRIVE_LETTER_OFFSET);
record->index_n = GUINT32_FROM_LE (record->index_n);
g_debug ("index=%u", record->index_n);

/* Number representing drive letter, 'A:' = 0, etc */
copy_field (&drivenum, DRIVE_LETTER_OFFSET, FILETIME_OFFSET);
copy_field (drivenum, buf, DRIVE_LETTER_OFFSET, FILETIME_OFFSET);
drivenum = GUINT32_FROM_LE (drivenum);
g_debug ("drive=%u", drivenum);
if (drivenum >= sizeof (driveletters) - 1) {
Expand All @@ -179,13 +180,14 @@ _populate_record_data (void *buf,
}

/* File deletion time */
copy_field (&record->winfiletime, FILETIME_OFFSET, FILESIZE_OFFSET);
copy_field (record->winfiletime, buf, FILETIME_OFFSET, FILESIZE_OFFSET);
record->winfiletime = GINT64_FROM_LE (record->winfiletime);
record->deltime = win_filetime_to_gdatetime (record->winfiletime);

/* File size or occupied cluster size */
/* BEWARE! This is 32bit data casted to 64bit struct member */
copy_field (&record->filesize, FILESIZE_OFFSET, UNICODE_FILENAME_OFFSET);
copy_field (record->filesize, buf,
FILESIZE_OFFSET, UNICODE_FILENAME_OFFSET);
record->filesize = GUINT64_FROM_LE (record->filesize);
g_debug ("filesize=%" PRIu64, record->filesize);

Expand All @@ -210,7 +212,8 @@ _populate_record_data (void *buf,

uni_buf_sz = UNICODE_RECORD_SIZE - UNICODE_FILENAME_OFFSET;
record->raw_uni_path = g_malloc (uni_buf_sz);
copy_field (record->raw_uni_path, UNICODE_FILENAME_OFFSET, UNICODE_RECORD_SIZE);
copy_field (*(record->raw_uni_path), buf,
UNICODE_FILENAME_OFFSET, UNICODE_RECORD_SIZE);
null_terminator_offset = ucs2_strnlen (
record->raw_uni_path, WIN_PATH_MAX) * sizeof (gunichar2);

Expand Down
4 changes: 2 additions & 2 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ typedef struct _rbin_struct
} rbin_struct;

/* convenience macro */
#define copy_field(field, off1, off2) \
memcpy((field), buf + (off1), (off2) - (off1))
#define copy_field(field, buf, off1, off2) \
memcpy(&(field), (buf) + (off1), (off2) - (off1))

/*! Every Windows use this GUID in recycle bin desktop.ini */
#define RECYCLE_BIN_CLSID "645FF040-5081-101B-9F08-00AA002F954E"
Expand Down

0 comments on commit 656140a

Please sign in to comment.