101a0f853SOlivier Houchard /* $NetBSD: cd9660_conversion.c,v 1.4 2007/03/14 14:11:17 christos Exp $ */
201a0f853SOlivier Houchard
31de7b4b8SPedro F. Giffuni /*-
4*b61a5730SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
51de7b4b8SPedro F. Giffuni *
601a0f853SOlivier Houchard * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
701a0f853SOlivier Houchard * Perez-Rathke and Ram Vedam. All rights reserved.
801a0f853SOlivier Houchard *
901a0f853SOlivier Houchard * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
1001a0f853SOlivier Houchard * Alan Perez-Rathke and Ram Vedam.
1101a0f853SOlivier Houchard *
1201a0f853SOlivier Houchard * Redistribution and use in source and binary forms, with or
1301a0f853SOlivier Houchard * without modification, are permitted provided that the following
1401a0f853SOlivier Houchard * conditions are met:
1501a0f853SOlivier Houchard * 1. Redistributions of source code must retain the above copyright
1601a0f853SOlivier Houchard * notice, this list of conditions and the following disclaimer.
1701a0f853SOlivier Houchard * 2. Redistributions in binary form must reproduce the above
1801a0f853SOlivier Houchard * copyright notice, this list of conditions and the following
1901a0f853SOlivier Houchard * disclaimer in the documentation and/or other materials provided
2001a0f853SOlivier Houchard * with the distribution.
2101a0f853SOlivier Houchard *
2201a0f853SOlivier Houchard * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
2301a0f853SOlivier Houchard * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
2401a0f853SOlivier Houchard * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2501a0f853SOlivier Houchard * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2601a0f853SOlivier Houchard * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
2701a0f853SOlivier Houchard * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
2801a0f853SOlivier Houchard * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2901a0f853SOlivier Houchard * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
3001a0f853SOlivier Houchard * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
3101a0f853SOlivier Houchard * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3201a0f853SOlivier Houchard * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3301a0f853SOlivier Houchard * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
3401a0f853SOlivier Houchard * OF SUCH DAMAGE.
3501a0f853SOlivier Houchard */
3601a0f853SOlivier Houchard #include "cd9660.h"
3701a0f853SOlivier Houchard
3801a0f853SOlivier Houchard #include <sys/cdefs.h>
3901a0f853SOlivier Houchard static char cd9660_compute_gm_offset(time_t);
4001a0f853SOlivier Houchard
4101a0f853SOlivier Houchard #if 0
4201a0f853SOlivier Houchard static inline int
4301a0f853SOlivier Houchard cd9660_pad_even(length)
4401a0f853SOlivier Houchard int length;
4501a0f853SOlivier Houchard {
4601a0f853SOlivier Houchard return length + (length & 0x01);
4701a0f853SOlivier Houchard }
4801a0f853SOlivier Houchard #endif
4901a0f853SOlivier Houchard
5001a0f853SOlivier Houchard /*
5101a0f853SOlivier Houchard * These can probably be implemented using a macro
5201a0f853SOlivier Houchard */
5301a0f853SOlivier Houchard
5401a0f853SOlivier Houchard /* Little endian */
5501a0f853SOlivier Houchard void
cd9660_721(uint16_t w,unsigned char * twochar)5601a0f853SOlivier Houchard cd9660_721(uint16_t w, unsigned char *twochar)
5701a0f853SOlivier Houchard {
5801a0f853SOlivier Houchard #if BYTE_ORDER == BIG_ENDIAN
5901a0f853SOlivier Houchard w = bswap16(w);
6001a0f853SOlivier Houchard #endif
6101a0f853SOlivier Houchard memcpy(twochar,&w,2);
6201a0f853SOlivier Houchard }
6301a0f853SOlivier Houchard
6401a0f853SOlivier Houchard void
cd9660_731(uint32_t w,unsigned char * fourchar)6501a0f853SOlivier Houchard cd9660_731(uint32_t w, unsigned char *fourchar)
6601a0f853SOlivier Houchard {
6701a0f853SOlivier Houchard #if BYTE_ORDER == BIG_ENDIAN
6801a0f853SOlivier Houchard w = bswap32(w);
6901a0f853SOlivier Houchard #endif
7001a0f853SOlivier Houchard memcpy(fourchar,&w,4);
7101a0f853SOlivier Houchard }
7201a0f853SOlivier Houchard
7301a0f853SOlivier Houchard /* Big endian */
7401a0f853SOlivier Houchard void
cd9660_722(uint16_t w,unsigned char * twochar)7501a0f853SOlivier Houchard cd9660_722(uint16_t w, unsigned char *twochar)
7601a0f853SOlivier Houchard {
7701a0f853SOlivier Houchard #if BYTE_ORDER == LITTLE_ENDIAN
7801a0f853SOlivier Houchard w = bswap16(w);
7901a0f853SOlivier Houchard #endif
8001a0f853SOlivier Houchard memcpy(twochar,&w,2);
8101a0f853SOlivier Houchard }
8201a0f853SOlivier Houchard
8301a0f853SOlivier Houchard void
cd9660_732(uint32_t w,unsigned char * fourchar)8401a0f853SOlivier Houchard cd9660_732(uint32_t w, unsigned char *fourchar)
8501a0f853SOlivier Houchard {
8601a0f853SOlivier Houchard #if BYTE_ORDER == LITTLE_ENDIAN
8701a0f853SOlivier Houchard w = bswap32(w);
8801a0f853SOlivier Houchard #endif
8901a0f853SOlivier Houchard memcpy(fourchar,&w,4);
9001a0f853SOlivier Houchard }
9101a0f853SOlivier Houchard
9201a0f853SOlivier Houchard /**
9301a0f853SOlivier Houchard * Convert a dword into a double endian string of eight characters
9401a0f853SOlivier Houchard * @param int The double word to convert
9501a0f853SOlivier Houchard * @param char* The string to write the both endian double word to - It is assumed this is allocated and at least
9601a0f853SOlivier Houchard * eight characters long
9701a0f853SOlivier Houchard */
9801a0f853SOlivier Houchard void
cd9660_bothendian_dword(uint32_t dw,unsigned char * eightchar)9901a0f853SOlivier Houchard cd9660_bothendian_dword(uint32_t dw, unsigned char *eightchar)
10001a0f853SOlivier Houchard {
10101a0f853SOlivier Houchard uint32_t le, be;
10201a0f853SOlivier Houchard #if BYTE_ORDER == LITTLE_ENDIAN
10301a0f853SOlivier Houchard le = dw;
10401a0f853SOlivier Houchard be = bswap32(dw);
10501a0f853SOlivier Houchard #endif
10601a0f853SOlivier Houchard #if BYTE_ORDER == BIG_ENDIAN
10701a0f853SOlivier Houchard be = dw;
10801a0f853SOlivier Houchard le = bswap32(dw);
10901a0f853SOlivier Houchard #endif
11001a0f853SOlivier Houchard memcpy(eightchar, &le, 4);
11101a0f853SOlivier Houchard memcpy((eightchar+4), &be, 4);
11201a0f853SOlivier Houchard }
11301a0f853SOlivier Houchard
11401a0f853SOlivier Houchard /**
11501a0f853SOlivier Houchard * Convert a word into a double endian string of four characters
11601a0f853SOlivier Houchard * @param int The word to convert
11701a0f853SOlivier Houchard * @param char* The string to write the both endian word to - It is assumed this is allocated and at least
11801a0f853SOlivier Houchard * four characters long
11901a0f853SOlivier Houchard */
12001a0f853SOlivier Houchard void
cd9660_bothendian_word(uint16_t dw,unsigned char * fourchar)12101a0f853SOlivier Houchard cd9660_bothendian_word(uint16_t dw, unsigned char *fourchar)
12201a0f853SOlivier Houchard {
12301a0f853SOlivier Houchard uint16_t le, be;
12401a0f853SOlivier Houchard #if BYTE_ORDER == LITTLE_ENDIAN
12501a0f853SOlivier Houchard le = dw;
12601a0f853SOlivier Houchard be = bswap16(dw);
12701a0f853SOlivier Houchard #endif
12801a0f853SOlivier Houchard #if BYTE_ORDER == BIG_ENDIAN
12901a0f853SOlivier Houchard be = dw;
13001a0f853SOlivier Houchard le = bswap16(dw);
13101a0f853SOlivier Houchard #endif
13201a0f853SOlivier Houchard memcpy(fourchar, &le, 2);
13301a0f853SOlivier Houchard memcpy((fourchar+2), &be, 2);
13401a0f853SOlivier Houchard }
13501a0f853SOlivier Houchard
13601a0f853SOlivier Houchard void
cd9660_pad_string_spaces(char * str,int len)13701a0f853SOlivier Houchard cd9660_pad_string_spaces(char *str, int len)
13801a0f853SOlivier Houchard {
13901a0f853SOlivier Houchard int i;
14001a0f853SOlivier Houchard
14101a0f853SOlivier Houchard for (i = 0; i < len; i ++) {
14201a0f853SOlivier Houchard if (str[i] == '\0')
14301a0f853SOlivier Houchard str[i] = 0x20;
14401a0f853SOlivier Houchard }
14501a0f853SOlivier Houchard }
14601a0f853SOlivier Houchard
14701a0f853SOlivier Houchard static char
cd9660_compute_gm_offset(time_t tim)14801a0f853SOlivier Houchard cd9660_compute_gm_offset(time_t tim)
14901a0f853SOlivier Houchard {
15001a0f853SOlivier Houchard struct tm t, gm;
15101a0f853SOlivier Houchard
15201a0f853SOlivier Houchard (void)localtime_r(&tim, &t);
15301a0f853SOlivier Houchard (void)gmtime_r(&tim, &gm);
15401a0f853SOlivier Houchard gm.tm_year -= t.tm_year;
15501a0f853SOlivier Houchard gm.tm_yday -= t.tm_yday;
15601a0f853SOlivier Houchard gm.tm_hour -= t.tm_hour;
15701a0f853SOlivier Houchard gm.tm_min -= t.tm_min;
15801a0f853SOlivier Houchard if (gm.tm_year < 0)
15901a0f853SOlivier Houchard gm.tm_yday = -1;
16001a0f853SOlivier Houchard else if (gm.tm_year > 0)
16101a0f853SOlivier Houchard gm.tm_yday = 1;
16201a0f853SOlivier Houchard
16301a0f853SOlivier Houchard return (char)(-(gm.tm_min + 60* (24 * gm.tm_yday + gm.tm_hour)) / 15);
16401a0f853SOlivier Houchard }
16501a0f853SOlivier Houchard
16601a0f853SOlivier Houchard /* Long dates: 17 characters */
16701a0f853SOlivier Houchard void
cd9660_time_8426(unsigned char * buf,time_t tim)16801a0f853SOlivier Houchard cd9660_time_8426(unsigned char *buf, time_t tim)
16901a0f853SOlivier Houchard {
17001a0f853SOlivier Houchard struct tm t;
17101a0f853SOlivier Houchard char temp[18];
17201a0f853SOlivier Houchard
17301a0f853SOlivier Houchard (void)localtime_r(&tim, &t);
17401a0f853SOlivier Houchard (void)snprintf(temp, sizeof(temp), "%04i%02i%02i%02i%02i%02i%02i",
17501a0f853SOlivier Houchard 1900+(int)t.tm_year,
17601a0f853SOlivier Houchard (int)t.tm_mon+1,
17701a0f853SOlivier Houchard (int)t.tm_mday,
17801a0f853SOlivier Houchard (int)t.tm_hour,
17901a0f853SOlivier Houchard (int)t.tm_min,
18001a0f853SOlivier Houchard (int)t.tm_sec,
18101a0f853SOlivier Houchard 0);
18201a0f853SOlivier Houchard (void)memcpy(buf, temp, 16);
18301a0f853SOlivier Houchard buf[16] = cd9660_compute_gm_offset(tim);
18401a0f853SOlivier Houchard }
18501a0f853SOlivier Houchard
18601a0f853SOlivier Houchard /* Short dates: 7 characters */
18701a0f853SOlivier Houchard void
cd9660_time_915(unsigned char * buf,time_t tim)18801a0f853SOlivier Houchard cd9660_time_915(unsigned char *buf, time_t tim)
18901a0f853SOlivier Houchard {
19001a0f853SOlivier Houchard struct tm t;
19101a0f853SOlivier Houchard
19201a0f853SOlivier Houchard (void)localtime_r(&tim, &t);
19301a0f853SOlivier Houchard buf[0] = t.tm_year;
19401a0f853SOlivier Houchard buf[1] = t.tm_mon+1;
19501a0f853SOlivier Houchard buf[2] = t.tm_mday;
19601a0f853SOlivier Houchard buf[3] = t.tm_hour;
19701a0f853SOlivier Houchard buf[4] = t.tm_min;
19801a0f853SOlivier Houchard buf[5] = t.tm_sec;
19901a0f853SOlivier Houchard buf[6] = cd9660_compute_gm_offset(tim);
20001a0f853SOlivier Houchard }
201