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