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