assembler: Some r15 fixes
[b43-tools.git] / assembler / util.c
1 /*
2  *   Copyright (C) 2006  Michael Buesch <mb@bu3sch.de>
3  *
4  *   This program is free software; you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License version 2
6  *   as published by the Free Software Foundation.
7  *
8  *   This program is distributed in the hope that it will be useful,
9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *   GNU General Public License for more details.
12  */
13
14 #include "util.h"
15
16 #include <stdio.h>
17 #include <string.h>
18
19
20 void dump(const char *data,
21           size_t size,
22           const char *description)
23 {
24         size_t i;
25         char c;
26
27         fprintf(stderr, "Data dump (%s, %zd bytes):",
28                 description, size);
29         for (i = 0; i < size; i++) {
30                 c = data[i];
31                 if (i % 8 == 0) {
32                         fprintf(stderr, "\n0x%08zx:  0x%02x, ",
33                                 i, c & 0xff);
34                 } else
35                         fprintf(stderr, "0x%02x, ", c & 0xff);
36         }
37         fprintf(stderr, "\n");
38 }
39
40 void * xmalloc(size_t size)
41 {
42         void *p;
43
44         p = malloc(size);
45         if (!p) {
46                 fprintf(stderr, "Out of memory\n");
47                 exit(1);
48         }
49         memset(p, 0, size);
50
51         return p;
52 }
53
54 char * xstrdup(const char *str)
55 {
56         char *c;
57
58         c = strdup(str);
59         if (!c) {
60                 fprintf(stderr, "Out of memory\n");
61                 exit(1);
62         }
63
64         return c;
65 }
66
67 be16_t cpu_to_be16(uint16_t x)
68 {
69         be16_t ret;
70         uint8_t *tmp = (uint8_t *)(&ret);
71
72         tmp[0] = (x & 0xFF00) >> 8;
73         tmp[1] = (x & 0x00FF);
74
75         return ret;
76 }
77
78 be32_t cpu_to_be32(uint32_t x)
79 {
80         be32_t ret;
81         uint8_t *tmp = (uint8_t *)(&ret);
82
83         tmp[0] = (x & 0xFF000000) >> 24;
84         tmp[1] = (x & 0x00FF0000) >> 16;
85         tmp[2] = (x & 0x0000FF00) >> 8;
86         tmp[3] = (x & 0x000000FF);
87
88         return ret;
89 }