Initial Commit
[Projects.git] / pkgbuilds / wiifuse / src / wiifuse-0.2.0 / global.c
blobbe2990eb434c9ca6a1cb43663217a8a0b8353dbd
1 /*
2 * Copyright (C) 2008 dhewg, #wiidev efnet
4 * some functions are taken from
5 * http://git.infradead.org/?p=users/segher/wii.git
6 * Copyright 2007,2008 Segher Boessenkool <segher@kernel.crashing.org>
7 * 21:14:49 <@segher> i'll happily grant you to license the whole as v3, btw
9 * this file is part of wiifuse
10 * http://wiibrew.org/index.php?title=Wiifuse
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "global.h"
29 u8 verbose_level = 0;
31 u16 get_be16 (const u8 *p) {
32 return (p[0] << 8) | p[1];
35 u32 get_be32 (const u8 *p) {
36 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
39 u64 get_be64 (const u8 *p) {
40 return ((u64) get_be32 (p) << 32) | get_be32 (p + 4);
43 void put_be16 (u8 *p, const u16 value) {
44 p[0] = (value >> 8) & 0xff;
45 p[1] = value & 0xff;
48 void put_be32 (u8 *p, const u32 value) {
49 p[0] = (value >> 24) & 0xff;
50 p[1] = (value >> 16) & 0xff;
51 p[2] = (value >> 8) & 0xff;
52 p[3] = value & 0xff;
55 void put_be64 (u8 *p, const u64 value) {
56 put_be32 (p, (value >> 32) & 0xffffffff);
57 put_be32 (p + 4, value & 0xffffffff);
60 size_t g_strnlen (const char *s, size_t size) {
61 size_t i;
63 for (i = 0; i < size; ++i)
64 if (s[i] == 0)
65 return i;
67 return size;
70 u32 get_dol_size (const u8 *header) {
71 u8 i;
72 u32 offset, size;
73 u32 max = 0;
75 // iterate through the 7 code segments
76 for (i = 0; i < 7; ++i) {
77 offset = get_be32 (&header[i * 4]);
78 size = get_be32 (&header[0x90 + i * 4]);
79 if (offset + size > max)
80 max = offset + size;
83 // iterate through the 11 data segments
84 for (i = 0; i < 11; ++i) {
85 offset = get_be32 (&header[0x1c + i * 4]);
86 size = get_be32 (&header[0xac + i * 4]);
87 if (offset + size > max)
88 max = offset + size;
91 return max;