Home | History | Annotate | Line # | Download | only in ffs
ffs_bswap.c revision 1.6.2.1
      1 /*	$NetBSD: ffs_bswap.c,v 1.6.2.1 2000/11/20 18:11:44 bouyer 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR 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 USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <ufs/ufs/dinode.h>
     38 #include <ufs/ufs/ufs_bswap.h>
     39 #include <ufs/ffs/fs.h>
     40 #include <ufs/ffs/ffs_extern.h>
     41 
     42 #if !defined(_KERNEL)
     43 #include <string.h>
     44 #endif
     45 
     46 void
     47 ffs_sb_swap(o, n, ns)
     48 	struct fs *o, *n;
     49 	int ns;
     50 {
     51 	int i;
     52 	u_int32_t *o32, *n32;
     53 	u_int16_t *o16, *n16;
     54 
     55 	/* in order to avoid a lot of lines, as the first 52 fields of
     56 	 * the superblock are u_int32_t, we loop here to convert it.
     57 	 */
     58 	o32 = (u_int32_t *)o;
     59 	n32 = (u_int32_t *)n;
     60 	for (i=0; i< 52; i++)
     61 		n32[i] = bswap32(o32[i]);
     62 
     63 	n->fs_cpc = bswap32(o->fs_cpc);
     64 	n->fs_fscktime = bswap32(o->fs_fscktime);
     65 	n->fs_contigsumsize = bswap32(o->fs_contigsumsize);
     66 	n->fs_maxsymlinklen = bswap32(o->fs_maxsymlinklen);
     67 	n->fs_inodefmt = bswap32(o->fs_inodefmt);
     68 	n->fs_maxfilesize = bswap64(o->fs_maxfilesize);
     69 	n->fs_qbmask = bswap64(o->fs_qbmask);
     70 	n->fs_qfmask = bswap64(o->fs_qfmask);
     71 	n->fs_state = bswap32(o->fs_state);
     72 	n->fs_postblformat = bswap32(o->fs_postblformat);
     73 	n->fs_nrpos = bswap32(o->fs_nrpos);
     74 	n->fs_postbloff = bswap32(o->fs_postbloff);
     75 	n->fs_rotbloff = bswap32(o->fs_rotbloff);
     76 	n->fs_magic = bswap32(o->fs_magic);
     77 	/* byteswap the postbl */
     78 	o16 = (ufs_rw32(o->fs_postblformat, ns) == FS_42POSTBLFMT)
     79 	    ? o->fs_opostbl[0]
     80 	    : (int16_t *)((u_int8_t *)o + ufs_rw32(o->fs_postbloff, ns));
     81 	n16 = (ufs_rw32(o->fs_postblformat, ns) == FS_42POSTBLFMT)
     82 	    ? n->fs_opostbl[0]
     83 	    : (int16_t *)((u_int8_t *)n + ufs_rw32(n->fs_postbloff, ns));
     84 	for (i = 0; i < (
     85 	         (ufs_rw32(o->fs_postblformat, ns) == FS_42POSTBLFMT) ?
     86 	         168 : /* fs_opostbl[16][8] */
     87 	         ufs_rw32(o->fs_cpc, ns) * ufs_rw32(o->fs_nrpos, ns));
     88 	     i++)
     89 		n16[i] = bswap16(o16[i]);
     90 }
     91 
     92 void
     93 ffs_dinode_swap(o, n)
     94 	struct dinode *o, *n;
     95 {
     96 	n->di_mode = bswap16(o->di_mode);
     97 	n->di_nlink = bswap16(o->di_nlink);
     98 	n->di_u.oldids[0] = bswap16(o->di_u.oldids[0]);
     99 	n->di_u.oldids[1] = bswap16(o->di_u.oldids[1]);
    100 	n->di_size = bswap64(o->di_size);
    101 	n->di_atime = bswap32(o->di_atime);
    102 	n->di_atimensec = bswap32(o->di_atimensec);
    103 	n->di_mtime = bswap32(o->di_mtime);
    104 	n->di_mtimensec = bswap32(o->di_mtimensec);
    105 	n->di_ctime = bswap32(o->di_ctime);
    106 	n->di_ctimensec = bswap32(o->di_ctimensec);
    107 	memcpy(n->di_db, o->di_db, (NDADDR + NIADDR) * sizeof(u_int32_t));
    108 	n->di_flags = bswap32(o->di_flags);
    109 	n->di_blocks = bswap32(o->di_blocks);
    110 	n->di_gen = bswap32(o->di_gen);
    111 	n->di_uid = bswap32(o->di_uid);
    112 	n->di_gid = bswap32(o->di_gid);
    113 }
    114 
    115 void
    116 ffs_csum_swap(o, n, size)
    117 	struct csum *o, *n;
    118 	int size;
    119 {
    120 	int i;
    121 	u_int32_t *oint, *nint;
    122 
    123 	oint = (u_int32_t*)o;
    124 	nint = (u_int32_t*)n;
    125 
    126 	for (i = 0; i < size / sizeof(u_int32_t); i++)
    127 		nint[i] = bswap32(oint[i]);
    128 }
    129