1 |
|
|
/* $OpenBSD: cd9660_conversion.c,v 1.2 2016/10/16 20:26:56 natano Exp $ */ |
2 |
|
|
/* $NetBSD: cd9660_conversion.c,v 1.4 2007/03/14 14:11:17 christos Exp $ */ |
3 |
|
|
|
4 |
|
|
/* |
5 |
|
|
* Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan |
6 |
|
|
* Perez-Rathke and Ram Vedam. All rights reserved. |
7 |
|
|
* |
8 |
|
|
* This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys, |
9 |
|
|
* Alan Perez-Rathke and Ram Vedam. |
10 |
|
|
* |
11 |
|
|
* Redistribution and use in source and binary forms, with or |
12 |
|
|
* without modification, are permitted provided that the following |
13 |
|
|
* conditions are met: |
14 |
|
|
* 1. Redistributions of source code must retain the above copyright |
15 |
|
|
* notice, this list of conditions and the following disclaimer. |
16 |
|
|
* 2. Redistributions in binary form must reproduce the above |
17 |
|
|
* copyright notice, this list of conditions and the following |
18 |
|
|
* disclaimer in the documentation and/or other materials provided |
19 |
|
|
* with the distribution. |
20 |
|
|
* |
21 |
|
|
* THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN |
22 |
|
|
* GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR |
23 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
24 |
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
25 |
|
|
* DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN |
26 |
|
|
* GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT, |
27 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
28 |
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
29 |
|
|
* USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
30 |
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
31 |
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
32 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
33 |
|
|
* OF SUCH DAMAGE. |
34 |
|
|
*/ |
35 |
|
|
#include "cd9660.h" |
36 |
|
|
|
37 |
|
|
#define bswap16 swap16 |
38 |
|
|
#define bswap32 swap32 |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
static char cd9660_compute_gm_offset(time_t); |
42 |
|
|
|
43 |
|
|
#if 0 |
44 |
|
|
static inline int |
45 |
|
|
cd9660_pad_even(length) |
46 |
|
|
int length; |
47 |
|
|
{ |
48 |
|
|
return length + (length & 0x01); |
49 |
|
|
} |
50 |
|
|
#endif |
51 |
|
|
|
52 |
|
|
/* |
53 |
|
|
* These can probably be implemented using a macro |
54 |
|
|
*/ |
55 |
|
|
|
56 |
|
|
/* Little endian */ |
57 |
|
|
void |
58 |
|
|
cd9660_721(uint16_t w, unsigned char *twochar) |
59 |
|
|
{ |
60 |
|
|
#if BYTE_ORDER == BIG_ENDIAN |
61 |
|
|
w = bswap16(w); |
62 |
|
|
#endif |
63 |
|
|
memcpy(twochar,&w,2); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
void |
67 |
|
|
cd9660_731(uint32_t w, unsigned char *fourchar) |
68 |
|
|
{ |
69 |
|
|
#if BYTE_ORDER == BIG_ENDIAN |
70 |
|
|
w = bswap32(w); |
71 |
|
|
#endif |
72 |
|
|
memcpy(fourchar,&w,4); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
/* Big endian */ |
76 |
|
|
void |
77 |
|
|
cd9660_722(uint16_t w, unsigned char *twochar) |
78 |
|
|
{ |
79 |
|
|
#if BYTE_ORDER == LITTLE_ENDIAN |
80 |
|
|
w = bswap16(w); |
81 |
|
|
#endif |
82 |
|
|
memcpy(twochar,&w,2); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
void |
86 |
|
|
cd9660_732(uint32_t w, unsigned char *fourchar) |
87 |
|
|
{ |
88 |
|
|
#if BYTE_ORDER == LITTLE_ENDIAN |
89 |
|
|
w = bswap32(w); |
90 |
|
|
#endif |
91 |
|
|
memcpy(fourchar,&w,4); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
/** |
95 |
|
|
* Convert a dword into a double endian string of eight characters |
96 |
|
|
* @param int The double word to convert |
97 |
|
|
* @param char* The string to write the both endian double word to - It is assumed this is allocated and at least |
98 |
|
|
* eight characters long |
99 |
|
|
*/ |
100 |
|
|
void |
101 |
|
|
cd9660_bothendian_dword(uint32_t dw, unsigned char *eightchar) |
102 |
|
|
{ |
103 |
|
|
uint32_t le, be; |
104 |
|
|
#if BYTE_ORDER == LITTLE_ENDIAN |
105 |
|
|
le = dw; |
106 |
|
|
be = bswap32(dw); |
107 |
|
|
#endif |
108 |
|
|
#if BYTE_ORDER == BIG_ENDIAN |
109 |
|
|
be = dw; |
110 |
|
|
le = bswap32(dw); |
111 |
|
|
#endif |
112 |
|
|
memcpy(eightchar, &le, 4); |
113 |
|
|
memcpy((eightchar+4), &be, 4); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
/** |
117 |
|
|
* Convert a word into a double endian string of four characters |
118 |
|
|
* @param int The word to convert |
119 |
|
|
* @param char* The string to write the both endian word to - It is assumed this is allocated and at least |
120 |
|
|
* four characters long |
121 |
|
|
*/ |
122 |
|
|
void |
123 |
|
|
cd9660_bothendian_word(uint16_t dw, unsigned char *fourchar) |
124 |
|
|
{ |
125 |
|
|
uint16_t le, be; |
126 |
|
|
#if BYTE_ORDER == LITTLE_ENDIAN |
127 |
|
|
le = dw; |
128 |
|
|
be = bswap16(dw); |
129 |
|
|
#endif |
130 |
|
|
#if BYTE_ORDER == BIG_ENDIAN |
131 |
|
|
be = dw; |
132 |
|
|
le = bswap16(dw); |
133 |
|
|
#endif |
134 |
|
|
memcpy(fourchar, &le, 2); |
135 |
|
|
memcpy((fourchar+2), &be, 2); |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
void |
139 |
|
|
cd9660_pad_string_spaces(char *str, int len) |
140 |
|
|
{ |
141 |
|
|
int i; |
142 |
|
|
|
143 |
|
|
for (i = 0; i < len; i ++) { |
144 |
|
|
if (str[i] == '\0') |
145 |
|
|
str[i] = 0x20; |
146 |
|
|
} |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
static char |
150 |
|
|
cd9660_compute_gm_offset(time_t tim) |
151 |
|
|
{ |
152 |
|
|
struct tm t, gm; |
153 |
|
|
|
154 |
|
|
(void)localtime_r(&tim, &t); |
155 |
|
|
(void)gmtime_r(&tim, &gm); |
156 |
|
|
gm.tm_year -= t.tm_year; |
157 |
|
|
gm.tm_yday -= t.tm_yday; |
158 |
|
|
gm.tm_hour -= t.tm_hour; |
159 |
|
|
gm.tm_min -= t.tm_min; |
160 |
|
|
if (gm.tm_year < 0) |
161 |
|
|
gm.tm_yday = -1; |
162 |
|
|
else if (gm.tm_year > 0) |
163 |
|
|
gm.tm_yday = 1; |
164 |
|
|
|
165 |
|
|
return (char)(-(gm.tm_min + 60* (24 * gm.tm_yday + gm.tm_hour)) / 15); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
/* Long dates: 17 characters */ |
169 |
|
|
void |
170 |
|
|
cd9660_time_8426(unsigned char *buf, time_t tim) |
171 |
|
|
{ |
172 |
|
|
struct tm t; |
173 |
|
|
char temp[18]; |
174 |
|
|
|
175 |
|
|
(void)localtime_r(&tim, &t); |
176 |
|
|
(void)snprintf(temp, sizeof(temp), "%04i%02i%02i%02i%02i%02i%02i", |
177 |
|
|
1900+(int)t.tm_year, |
178 |
|
|
(int)t.tm_mon+1, |
179 |
|
|
(int)t.tm_mday, |
180 |
|
|
(int)t.tm_hour, |
181 |
|
|
(int)t.tm_min, |
182 |
|
|
(int)t.tm_sec, |
183 |
|
|
0); |
184 |
|
|
(void)memcpy(buf, temp, 16); |
185 |
|
|
buf[16] = cd9660_compute_gm_offset(tim); |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
/* Short dates: 7 characters */ |
189 |
|
|
void |
190 |
|
|
cd9660_time_915(unsigned char *buf, time_t tim) |
191 |
|
|
{ |
192 |
|
|
struct tm t; |
193 |
|
|
|
194 |
|
|
(void)localtime_r(&tim, &t); |
195 |
|
|
buf[0] = t.tm_year; |
196 |
|
|
buf[1] = t.tm_mon+1; |
197 |
|
|
buf[2] = t.tm_mday; |
198 |
|
|
buf[3] = t.tm_hour; |
199 |
|
|
buf[4] = t.tm_min; |
200 |
|
|
buf[5] = t.tm_sec; |
201 |
|
|
buf[6] = cd9660_compute_gm_offset(tim); |
202 |
|
|
} |