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