annotate src/timeconv.c @ 42:7a97f50c39c5 stable-0-5-12 **FUNKY**

security fix for potential buffer overrun in lz decompress
author carl
date Tue, 02 Oct 2007 16:10:36 -0700
parents c508ee15dfca
children b2a7f2e0926a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
1 /***********************************************************************
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
2 *
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
3 * Borrowed from WINE sources!! (http://www.winehq.com)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
4 * Converts a Win32 FILETIME structure to a UNIX time_t value
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
5 */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
6
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
7 /*** WARNING ****
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
8 * This file is not to be incluided in a Visual C++ Project
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
9 * It will make the whole project fail to compile
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
10 * There are functions in libpst.c to handle the dates
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
11 * Do not use this one
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
12 */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
13
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
14 #include <time.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
15 #include "common.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
16 #include "timeconv.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
17
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
18 time_t fileTimeToUnixTime( const FILETIME *filetime, DWORD *remainder );
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
19 char * fileTimeToAscii (const FILETIME *filetime);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
20
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
21 char * fileTimeToAscii (const FILETIME *filetime) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
22 time_t t1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
23
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
24 t1 = fileTimeToUnixTime(filetime,0);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
25 return ctime(&t1);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
26 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
27
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
28 struct tm * fileTimeToStructTM (const FILETIME *filetime) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
29 time_t t1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
30 t1 = fileTimeToUnixTime(filetime, 0);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
31 return gmtime(&t1);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
32 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
33
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
34 /***********************************************************************
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
35 * DOSFS_FileTimeToUnixTime
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
36 *
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
37 * Convert a FILETIME format to Unix time.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
38 * If not NULL, 'remainder' contains the fractional part of the filetime,
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
39 * in the range of [0..9999999] (even if time_t is negative).
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
40 */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
41 time_t fileTimeToUnixTime( const FILETIME *filetime, DWORD *remainder )
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
42 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
43 /* Read the comment in the function DOSFS_UnixTimeToFileTime. */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
44 #if USE_LONG_LONG
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
45
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
46 long long int t = filetime->dwHighDateTime;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
47 t <<= 32;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
48 t += (UINT32)filetime->dwLowDateTime;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
49 t -= 116444736000000000LL;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
50 if (t < 0)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
51 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
52 if (remainder) *remainder = 9999999 - (-t - 1) % 10000000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
53 return -1 - ((-t - 1) / 10000000);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
54 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
55 else
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
56 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
57 if (remainder) *remainder = t % 10000000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
58 return t / 10000000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
59 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
60
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
61 #else /* ISO version */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
62
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
63 UINT32 a0; /* 16 bit, low bits */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
64 UINT32 a1; /* 16 bit, medium bits */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
65 UINT32 a2; /* 32 bit, high bits */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
66 UINT32 r; /* remainder of division */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
67 unsigned int carry; /* carry bit for subtraction */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
68 int negative; /* whether a represents a negative value */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
69
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
70 /* Copy the time values to a2/a1/a0 */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
71 a2 = (UINT32)filetime->dwHighDateTime;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
72 a1 = ((UINT32)filetime->dwLowDateTime ) >> 16;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
73 a0 = ((UINT32)filetime->dwLowDateTime ) & 0xffff;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
74
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
75 /* Subtract the time difference */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
76 if (a0 >= 32768 ) a0 -= 32768 , carry = 0;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
77 else a0 += (1 << 16) - 32768 , carry = 1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
78
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
79 if (a1 >= 54590 + carry) a1 -= 54590 + carry, carry = 0;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
80 else a1 += (1 << 16) - 54590 - carry, carry = 1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
81
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
82 a2 -= 27111902 + carry;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
83
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
84 /* If a is negative, replace a by (-1-a) */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
85 negative = (a2 >= ((UINT32)1) << 31);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
86 if (negative)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
87 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
88 /* Set a to -a - 1 (a is a2/a1/a0) */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
89 a0 = 0xffff - a0;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
90 a1 = 0xffff - a1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
91 a2 = ~a2;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
92 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
93
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
94 /* Divide a by 10000000 (a = a2/a1/a0), put the rest into r.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
95 Split the divisor into 10000 * 1000 which are both less than 0xffff. */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
96 a1 += (a2 % 10000) << 16;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
97 a2 /= 10000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
98 a0 += (a1 % 10000) << 16;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
99 a1 /= 10000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
100 r = a0 % 10000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
101 a0 /= 10000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
102
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
103 a1 += (a2 % 1000) << 16;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
104 a2 /= 1000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
105 a0 += (a1 % 1000) << 16;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
106 a1 /= 1000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
107 r += (a0 % 1000) * 10000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
108 a0 /= 1000;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
109
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
110 /* If a was negative, replace a by (-1-a) and r by (9999999 - r) */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
111 if (negative)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
112 {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
113 /* Set a to -a - 1 (a is a2/a1/a0) */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
114 a0 = 0xffff - a0;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
115 a1 = 0xffff - a1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
116 a2 = ~a2;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
117
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
118 r = 9999999 - r;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
119 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
120
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
121 if (remainder) *remainder = r;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
122
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
123 /* Do not replace this by << 32, it gives a compiler warning and it does
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
124 not work. */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
125 return ((((time_t)a2) << 16) << 16) + (a1 << 16) + a0;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
126 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
127 }