Home | History | Annotate | Line # | Download | only in ffs
ffs_bswap.c revision 1.2
      1 /*	$NetBSD: ffs_bswap.c,v 1.2 1998/06/08 17:59:08 ragge Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 Manuel Bouyer.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by the University of
     17  *	California, Berkeley and its contributors.
     18  * 4. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  */
     35 
     36 #include <sys/types.h>
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <ufs/ufs/dinode.h>
     40 #include <ufs/ufs/ufs_bswap.h>
     41 #include <ufs/ffs/fs.h>
     42 #include <ufs/ffs/ffs_extern.h>
     43 
     44 void
     45 ffs_sb_swap(o, n, ns)
     46 	struct fs *o, *n;
     47 	int ns;
     48 {
     49 	int i;
     50 	u_int32_t *o32, *n32;
     51 	u_int16_t *o16, *n16;
     52 
     53 	/* in order to avoid a lot of lines, as the first 52 fields of
     54 	 * the superblock are u_int32_t, we loop here to convert it.
     55 	 */
     56 	o32 = (u_int32_t *)o;
     57 	n32 = (u_int32_t *)n;
     58 	for (i=0; i< 52; i++)
     59 		n32[i] = bswap32(o32[i]);
     60 
     61 	n->fs_cpc = bswap32(o->fs_cpc);
     62 	n->fs_fscktime = bswap32(o->fs_fscktime);
     63 	n->fs_contigsumsize = bswap32(o->fs_contigsumsize);
     64 	n->fs_maxsymlinklen = bswap32(o->fs_maxsymlinklen);
     65 	n->fs_inodefmt = bswap32(o->fs_inodefmt);
     66 	n->fs_maxfilesize = bswap64(o->fs_maxfilesize);
     67 	n->fs_qbmask = bswap64(o->fs_qbmask);
     68 	n->fs_qfmask = bswap64(o->fs_qfmask);
     69 	n->fs_state = bswap32(o->fs_state);
     70 	n->fs_postblformat = bswap32(o->fs_postblformat);
     71 	n->fs_nrpos = bswap32(o->fs_nrpos);
     72 	n->fs_postbloff = bswap32(o->fs_postbloff);
     73 	n->fs_rotbloff = bswap32(o->fs_rotbloff);
     74 	n->fs_magic = bswap32(o->fs_magic);
     75 	/* byteswap the postbl */
     76 	o16 = (ufs_rw32(o->fs_postblformat, ns) == FS_42POSTBLFMT)
     77 		? o->fs_opostbl[0]
     78 		: (int16_t *)((u_int8_t *)o + ufs_rw32(o->fs_postbloff, ns));
     79 	n16 = (ufs_rw32(o->fs_postblformat, ns) == FS_42POSTBLFMT)
     80 		? n->fs_opostbl[0]
     81 		: (int16_t *)((u_int8_t *)n + ufs_rw32(n->fs_postbloff, ns));
     82 	for (i = 0;
     83 		i < ufs_rw32(o->fs_cpc, ns) * ufs_rw32(o->fs_nrpos, ns);
     84 		i++)
     85 		n16[i] = bswap16(o16[i]);
     86 }
     87 
     88 void
     89 ffs_dinode_swap(o, n)
     90 	struct dinode *o, *n;
     91 {
     92 	n->di_mode = bswap16(o->di_mode);
     93 	n->di_nlink = bswap16(o->di_nlink);
     94 	n->di_u.oldids[0] = bswap16(o->di_u.oldids[0]);
     95 	n->di_u.oldids[1] = bswap16(o->di_u.oldids[1]);
     96 	n->di_size = bswap64(o->di_size);
     97 	n->di_atime = bswap32(o->di_atime);
     98 	n->di_atimensec = bswap32(o->di_atimensec);
     99 	n->di_mtime = bswap32(o->di_mtime);
    100 	n->di_mtimensec = bswap32(o->di_mtimensec);
    101 	n->di_ctime = bswap32(o->di_ctime);
    102 	n->di_ctimensec = bswap32(o->di_ctimensec);
    103 	bcopy(o->di_db, n->di_db, (NDADDR + NIADDR) * sizeof(u_int32_t));
    104 	n->di_flags = bswap32(o->di_flags);
    105 	n->di_blocks = bswap32(o->di_blocks);
    106 	n->di_gen = bswap32(o->di_gen);
    107 	n->di_uid = bswap32(o->di_uid);
    108 	n->di_gid = bswap32(o->di_gid);
    109 }
    110 
    111 void
    112 ffs_csum_swap(o, n, size)
    113 	struct csum *o, *n;
    114 	int size;
    115 {
    116 	int i;
    117 	u_int32_t *oint, *nint;
    118 
    119 	oint = (u_int32_t*)o;
    120 	nint = (u_int32_t*)n;
    121 
    122 	for (i = 0; i < size / sizeof(u_int32_t); i++)
    123 		nint[i] = bswap32(oint[i]);
    124 }
    125