156474Sasmodai/*-
2113771Sobrien * Copyright (c) 2005 Ivan Voras <ivoras@freebsd.org>
356474Sasmodai * All rights reserved.
459137Smsmith *
556474Sasmodai * Redistribution and use in source and binary forms, with or without
656474Sasmodai * modification, are permitted provided that the following conditions
756474Sasmodai * are met:
856474Sasmodai * 1. Redistributions of source code must retain the above copyright
956474Sasmodai *    notice, this list of conditions and the following disclaimer.
1056474Sasmodai * 2. Redistributions in binary form must reproduce the above copyright
1156474Sasmodai *    notice, this list of conditions and the following disclaimer in the
1256474Sasmodai *    documentation and/or other materials provided with the distribution.
1356474Sasmodai *
1479727Sschweikh * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1556474Sasmodai * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1656474Sasmodai * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1756474Sasmodai * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
1856474Sasmodai * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1956474Sasmodai * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2056474Sasmodai * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2156474Sasmodai * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2256474Sasmodai * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2356474Sasmodai * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2456474Sasmodai * SUCH DAMAGE.
2556474Sasmodai */
2656474Sasmodai
2756474Sasmodai#include <sys/cdefs.h>
28133452Ssimon__FBSDID("$FreeBSD: releng/11.0/sys/geom/virstor/g_virstor_md.c 172302 2007-09-23 07:34:23Z pjd $");
2956474Sasmodai
3056474Sasmodai#include <sys/param.h>
3156474Sasmodai#include <sys/endian.h>
3256474Sasmodai
3359137Smsmith#include <geom/virstor/g_virstor_md.h>
3456474Sasmodai#include <geom/virstor/binstream.h>
35158762Sbrueffer
36158762Sbrueffer/*
37158762Sbrueffer * Encode data from g_virstor_metadata structure into a endian-independant
38158762Sbrueffer * byte stream.
39158762Sbrueffer */
40158762Sbrueffervoid
41158762Sbrueffervirstor_metadata_encode(struct g_virstor_metadata *md, unsigned char *data)
42158762Sbrueffer{
43158762Sbrueffer	bin_stream_t bs;
44158762Sbrueffer
45158762Sbrueffer	bs_open(&bs, data);
46158762Sbrueffer
47158762Sbrueffer	bs_write_buf(&bs, md->md_magic, sizeof(md->md_magic));
48158762Sbrueffer	bs_write_u32(&bs, md->md_version);
4956474Sasmodai	bs_write_buf(&bs, md->md_name, sizeof(md->md_name));
5056474Sasmodai	bs_write_u64(&bs, md->md_virsize);
5156474Sasmodai	bs_write_u32(&bs, md->md_chunk_size);
5259137Smsmith	bs_write_u32(&bs, md->md_id);
5382234Sdd	bs_write_u16(&bs, md->md_count);
54133452Ssimon
55133452Ssimon	bs_write_buf(&bs, md->provider, sizeof(md->provider));
56133452Ssimon	bs_write_u16(&bs, md->no);
57133452Ssimon	bs_write_u64(&bs, md->provsize);
58133452Ssimon	bs_write_u32(&bs, md->chunk_count);
59133452Ssimon	bs_write_u32(&bs, md->chunk_next);
6079727Sschweikh	bs_write_u16(&bs, md->chunk_reserved);
61133452Ssimon	bs_write_u16(&bs, md->flags);
6279727Sschweikh}
63133452Ssimon
6459137Smsmith
65133452Ssimon/*
66118975Sbmah * Decode data from endian-independant byte stream into g_virstor_metadata
67133452Ssimon * structure.
6859137Smsmith */
69133452Ssimonvoid
7059137Smsmithvirstor_metadata_decode(unsigned char *data, struct g_virstor_metadata *md)
71133452Ssimon{
7259137Smsmith	bin_stream_t bs;
73133452Ssimon
7459137Smsmith	bs_open(&bs, (char *)(data));
75133452Ssimon
7659137Smsmith	bs_read_buf(&bs, md->md_magic, sizeof(md->md_magic));
77133452Ssimon	md->md_version = bs_read_u32(&bs);
78113771Sobrien	bs_read_buf(&bs, md->md_name, sizeof(md->md_name));
79133452Ssimon	md->md_virsize = bs_read_u64(&bs);
80113771Sobrien	md->md_chunk_size = bs_read_u32(&bs);
81133452Ssimon	md->md_id = bs_read_u32(&bs);
82133452Ssimon	md->md_count = bs_read_u16(&bs);
8359137Smsmith
8456474Sasmodai	bs_read_buf(&bs, md->provider, sizeof(md->provider));
8559137Smsmith	md->no = bs_read_u16(&bs);
8665001Ssheldonh	md->provsize = bs_read_u64(&bs);
8765001Ssheldonh	md->chunk_count = bs_read_u32(&bs);
8859137Smsmith	md->chunk_next = bs_read_u32(&bs);
8979727Sschweikh	md->chunk_reserved = bs_read_u16(&bs);
9079727Sschweikh	md->flags = bs_read_u16(&bs);
9159137Smsmith}
9264997Ssheldonh