-
Notifications
You must be signed in to change notification settings - Fork 0
/
ct.c
120 lines (115 loc) · 2.27 KB
/
ct.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* NOTE
* My changes to this code are under the MIT License. As I do not know who made this script initially, and there was no license with it, I am going to assume that the initial code wasn't licensed at all.
* Contributions will all be considered to be a part of the MIT License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef _MSC_VER
#include <sys/utime.h>
#else
#include <utime.h>
#endif
#define PE_SIGNATURE "00004550"
int readFourBytesHex(char *str, FILE *src);
int main(int argc, char *argv[])
{
FILE *fp;
int c;
int i;
int idx;
char temp[9];
unsigned long n;
time_t tm;
struct utimbuf new_times;
char *cTimeString;
if (argc != 2)
{
fprintf(stderr, "usage: %s <DLL/EXE>", argv[0]);
return 1;
}
fp = fopen(argv[1], "r");
if (fp == NULL)
{
fprintf(stderr, "error: failed to open the file.");
return 1;
}
for (i = 1; i <= 60; i++)
{
c = getc(fp);
if (c == EOF)
{
fprintf(stderr, "error: the file is not a windows object file.");
return -1;
}
}
temp[8] = '\0';
readFourBytesHex(temp, fp);
i += 4;
n = strtoul(temp, NULL, 16);
for (; i < n; i++)
{
c = getc(fp);
if (c == EOF)
{
fprintf(stderr, "error: the file is not a windows object file.");
return -1;
}
}
if (readFourBytesHex(temp, fp) != 0)
{
fprintf(stderr, "error: the file is not a windows object file.");
return -1;
}
i += 4;
if (strcmp(temp, PE_SIGNATURE) != 0)
{
fprintf(stderr, "File is not a PE or its header is corrupted.");
return -1;
}
i = 0;
for (; i < 4; i++)
{
c = getc(fp);
if (c == EOF)
{
fprintf(stderr, "error: the file is not a windows object file.");
return -1;
}
}
if (readFourBytesHex(temp, fp) != 0)
{
fprintf(stderr, "error: the file is not a windows object file.");
return -1;
}
n = strtoul(temp, NULL, 16);
tm = n;
cTimeString = ctime(&tm);
printf("Compile Date Time Stamp: %s", cTimeString);
new_times.modtime = n;
utime(argv[1], &new_times);
fclose(fp);
return 0;
}
// Reads four bytes in Hex in Little Endian format.
int readFourBytesHex(char *str, FILE *src)
{
char byteStr[2];
int i;
int c;
str += 6;
for (i = 0; i < 4; i++)
{
c = getc(src);
if (c == EOF)
{
return -1;
}
sprintf(byteStr, "%002x ", c);
strncpy(str, byteStr, 2);
str -= 2;
}
return 0;
}