cd9660_conversion.c revision 224762
1193326Sed/*	$NetBSD: cd9660_conversion.c,v 1.4 2007/03/14 14:11:17 christos Exp $	*/
2193326Sed
3193326Sed/*
4193326Sed * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
5193326Sed * Perez-Rathke and Ram Vedam.  All rights reserved.
6193326Sed *
7193326Sed * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
8193326Sed * Alan Perez-Rathke and Ram Vedam.
9193326Sed *
10193326Sed * Redistribution and use in source and binary forms, with or
11193326Sed * without modification, are permitted provided that the following
12193326Sed * conditions are met:
13193326Sed * 1. Redistributions of source code must retain the above copyright
14252723Sdim *    notice, this list of conditions and the following disclaimer.
15193326Sed * 2. Redistributions in binary form must reproduce the above
16226890Sdim *    copyright notice, this list of conditions and the following
17193326Sed *    disclaimer in the documentation and/or other materials provided
18263509Sdim *    with the distribution.
19235633Sdim *
20200583Srdivacky * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
21198092Srdivacky * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
22218893Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23193326Sed * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24193326Sed * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
25193326Sed * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
26193326Sed * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27193326Sed * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28263509Sdim * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29263509Sdim * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30263509Sdim * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31263509Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32263509Sdim * OF SUCH DAMAGE.
33263509Sdim */
34263509Sdim#include "cd9660.h"
35263509Sdim
36263509Sdim#include <sys/cdefs.h>
37263509Sdim__FBSDID("$FreeBSD: head/usr.sbin/makefs/cd9660/cd9660_conversion.c 224762 2011-08-10 19:12:21Z marius $");
38193326Sed
39193326Sedstatic char cd9660_compute_gm_offset(time_t);
40263509Sdim
41193326Sed#if 0
42226890Sdimstatic inline int
43193326Sedcd9660_pad_even(length)
44193326Sedint length;
45193326Sed{
46263509Sdim	return length + (length & 0x01);
47193326Sed}
48193326Sed#endif
49193326Sed
50193326Sed/*
51193326Sed* These can probably be implemented using a macro
52263509Sdim*/
53193326Sed
54226890Sdim/* Little endian */
55193326Sedvoid
56263509Sdimcd9660_721(uint16_t w, unsigned char *twochar)
57263509Sdim{
58263509Sdim#if BYTE_ORDER == BIG_ENDIAN
59263509Sdim	w = bswap16(w);
60263509Sdim#endif
61263509Sdim	memcpy(twochar,&w,2);
62263509Sdim}
63193326Sed
64193326Sedvoid
65193326Sedcd9660_731(uint32_t w, unsigned char *fourchar)
66199482Srdivacky{
67193326Sed#if BYTE_ORDER == BIG_ENDIAN
68193326Sed	w = bswap32(w);
69193326Sed#endif
70193326Sed	memcpy(fourchar,&w,4);
71193326Sed}
72193326Sed
73198092Srdivacky/* Big endian */
74193326Sedvoid
75193326Sedcd9660_722(uint16_t w, unsigned char *twochar)
76193326Sed{
77198092Srdivacky#if BYTE_ORDER == LITTLE_ENDIAN
78210299Sed	w = bswap16(w);
79210299Sed#endif
80210299Sed	memcpy(twochar,&w,2);
81212904Sdim}
82212904Sdim
83212904Sdimvoid
84202879Srdivackycd9660_732(uint32_t w, unsigned char *fourchar)
85202879Srdivacky{
86202879Srdivacky#if BYTE_ORDER == LITTLE_ENDIAN
87206084Srdivacky	w = bswap32(w);
88206084Srdivacky#endif
89206084Srdivacky	memcpy(fourchar,&w,4);
90206084Srdivacky}
91226890Sdim
92218893Sdim/**
93206084Srdivacky* Convert a dword into a double endian string of eight characters
94221345Sdim* @param int The double word to convert
95221345Sdim* @param char* The string to write the both endian double word to - It is assumed this is allocated and at least
96221345Sdim*		eight characters long
97263509Sdim*/
98263509Sdimvoid
99263509Sdimcd9660_bothendian_dword(uint32_t dw, unsigned char *eightchar)
100221345Sdim{
101221345Sdim	uint32_t le, be;
102221345Sdim#if BYTE_ORDER == LITTLE_ENDIAN
103235633Sdim	le = dw;
104235633Sdim	be = bswap32(dw);
105193326Sed#endif
106193326Sed#if BYTE_ORDER == BIG_ENDIAN
107193326Sed	be = dw;
108193326Sed	le = bswap32(dw);
109204643Srdivacky#endif
110204643Srdivacky	memcpy(eightchar, &le, 4);
111204643Srdivacky	memcpy((eightchar+4), &be, 4);
112221345Sdim}
113193326Sed
114193326Sed/**
115205408Srdivacky* Convert a word into a double endian string of four characters
116205408Srdivacky* @param int The word to convert
117205408Srdivacky* @param char* The string to write the both endian word to - It is assumed this is allocated and at least
118218893Sdim*		four characters long
119218893Sdim*/
120218893Sdimvoid
121221345Sdimcd9660_bothendian_word(uint16_t dw, unsigned char *fourchar)
122221345Sdim{
123221345Sdim	uint16_t le, be;
124226890Sdim#if BYTE_ORDER == LITTLE_ENDIAN
125263509Sdim	le = dw;
126263509Sdim	be = bswap16(dw);
127226890Sdim#endif
128193326Sed#if BYTE_ORDER == BIG_ENDIAN
129263509Sdim	be = dw;
130198092Srdivacky	le = bswap16(dw);
131235633Sdim#endif
132263509Sdim	memcpy(fourchar, &le, 2);
133221345Sdim	memcpy((fourchar+2), &be, 2);
134263509Sdim}
135263509Sdim
136193326Sedvoid
137193326Sedcd9660_pad_string_spaces(char *str, int len)
138203955Srdivacky{
139193326Sed	int i;
140205408Srdivacky
141205408Srdivacky	for (i = 0; i < len; i ++) {
142205408Srdivacky		if (str[i] == '\0')
143205408Srdivacky			str[i] = 0x20;
144218893Sdim	}
145218893Sdim}
146218893Sdim
147218893Sdimstatic char
148221345Sdimcd9660_compute_gm_offset(time_t tim)
149221345Sdim{
150221345Sdim	struct tm t, gm;
151221345Sdim
152221345Sdim	(void)localtime_r(&tim, &t);
153226890Sdim	(void)gmtime_r(&tim, &gm);
154226890Sdim	gm.tm_year -= t.tm_year;
155226890Sdim	gm.tm_yday -= t.tm_yday;
156203955Srdivacky	gm.tm_hour -= t.tm_hour;
157221345Sdim	gm.tm_min  -= t.tm_min;
158218893Sdim	if (gm.tm_year < 0)
159218893Sdim		gm.tm_yday = -1;
160203955Srdivacky	else if (gm.tm_year > 0)
161203955Srdivacky		gm.tm_yday = 1;
162203955Srdivacky
163193326Sed	return (char)(-(gm.tm_min + 60* (24 * gm.tm_yday + gm.tm_hour)) / 15);
164193326Sed}
165193326Sed
166203955Srdivacky/* Long dates: 17 characters */
167193326Sedvoid
168193326Sedcd9660_time_8426(unsigned char *buf, time_t tim)
169193326Sed{
170193326Sed	struct tm t;
171198092Srdivacky	char temp[18];
172193326Sed
173193326Sed	(void)localtime_r(&tim, &t);
174193326Sed	(void)snprintf(temp, sizeof(temp), "%04i%02i%02i%02i%02i%02i%02i",
175235633Sdim		1900+(int)t.tm_year,
176235633Sdim		(int)t.tm_mon+1,
177235633Sdim		(int)t.tm_mday,
178245431Sdim		(int)t.tm_hour,
179235633Sdim		(int)t.tm_min,
180235633Sdim		(int)t.tm_sec,
181235633Sdim		0);
182210299Sed	(void)memcpy(buf, temp, 16);
183210299Sed	buf[16] = cd9660_compute_gm_offset(tim);
184210299Sed}
185263509Sdim
186263509Sdim/* Short dates: 7 characters */
187210299Sedvoid
188226890Sdimcd9660_time_915(unsigned char *buf, time_t tim)
189226890Sdim{
190263509Sdim	struct tm t;
191263509Sdim
192226890Sdim	(void)localtime_r(&tim, &t);
193193326Sed	buf[0] = t.tm_year;
194226890Sdim	buf[1] = t.tm_mon+1;
195235633Sdim	buf[2] = t.tm_mday;
196226890Sdim	buf[3] = t.tm_hour;
197226890Sdim	buf[4] = t.tm_min;
198193326Sed	buf[5] = t.tm_sec;
199193326Sed	buf[6] = cd9660_compute_gm_offset(tim);
200193326Sed}
201193326Sed