tutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  copyright : (C) 2013 by Tsuda Kageyu
3  email : tsuda.kageyu@gmail.com
4  ***************************************************************************/
5 
6 /***************************************************************************
7  * This library is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU Lesser General Public License version *
9  * 2.1 as published by the Free Software Foundation. *
10  * *
11  * This library is distributed in the hope that it will be useful, but *
12  * WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the Free Software *
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
19  * 02110-1301 USA *
20  * *
21  * Alternatively, this file is available under the Mozilla Public *
22  * License Version 1.1. You may obtain a copy of the License at *
23  * http://www.mozilla.org/MPL/ *
24  ***************************************************************************/
25 
26 #ifndef TAGLIB_TUTILS_H
27 #define TAGLIB_TUTILS_H
28 
29 // THIS FILE IS NOT A PART OF THE TAGLIB API
30 
31 #ifndef DO_NOT_DOCUMENT // tell Doxygen not to document this header
32 
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36 
37 #if defined(HAVE_MSC_BYTESWAP)
38 # include <stdlib.h>
39 #elif defined(HAVE_GLIBC_BYTESWAP)
40 # include <byteswap.h>
41 #elif defined(HAVE_MAC_BYTESWAP)
42 # include <libkern/OSByteOrder.h>
43 #elif defined(HAVE_OPENBSD_BYTESWAP)
44 # include <sys/endian.h>
45 #endif
46 
47 #include <tstring.h>
48 #include <cstdio>
49 #include <cstdarg>
50 #include <cstring>
51 
52 namespace TagLib
53 {
54  namespace Utils
55  {
56 
60  inline ushort byteSwap(ushort x)
61  {
62 #if defined(HAVE_GCC_BYTESWAP)
63 
64  return __builtin_bswap16(x);
65 
66 #elif defined(HAVE_MSC_BYTESWAP)
67 
68  return _byteswap_ushort(x);
69 
70 #elif defined(HAVE_GLIBC_BYTESWAP)
71 
72  return __bswap_16(x);
73 
74 #elif defined(HAVE_MAC_BYTESWAP)
75 
76  return OSSwapInt16(x);
77 
78 #elif defined(HAVE_OPENBSD_BYTESWAP)
79 
80  return swap16(x);
81 
82 #else
83 
84  return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
85 
86 #endif
87  }
88 
92  inline uint byteSwap(uint x)
93  {
94 #if defined(HAVE_GCC_BYTESWAP)
95 
96  return __builtin_bswap32(x);
97 
98 #elif defined(HAVE_MSC_BYTESWAP)
99 
100  return _byteswap_ulong(x);
101 
102 #elif defined(HAVE_GLIBC_BYTESWAP)
103 
104  return __bswap_32(x);
105 
106 #elif defined(HAVE_MAC_BYTESWAP)
107 
108  return OSSwapInt32(x);
109 
110 #elif defined(HAVE_OPENBSD_BYTESWAP)
111 
112  return swap32(x);
113 
114 #else
115 
116  return ((x & 0xff000000u) >> 24)
117  | ((x & 0x00ff0000u) >> 8)
118  | ((x & 0x0000ff00u) << 8)
119  | ((x & 0x000000ffu) << 24);
120 
121 #endif
122  }
123 
127  inline ulonglong byteSwap(ulonglong x)
128  {
129 #if defined(HAVE_GCC_BYTESWAP)
130 
131  return __builtin_bswap64(x);
132 
133 #elif defined(HAVE_MSC_BYTESWAP)
134 
135  return _byteswap_uint64(x);
136 
137 #elif defined(HAVE_GLIBC_BYTESWAP)
138 
139  return __bswap_64(x);
140 
141 #elif defined(HAVE_MAC_BYTESWAP)
142 
143  return OSSwapInt64(x);
144 
145 #elif defined(HAVE_OPENBSD_BYTESWAP)
146 
147  return swap64(x);
148 
149 #else
150 
151  return ((x & 0xff00000000000000ull) >> 56)
152  | ((x & 0x00ff000000000000ull) >> 40)
153  | ((x & 0x0000ff0000000000ull) >> 24)
154  | ((x & 0x000000ff00000000ull) >> 8)
155  | ((x & 0x00000000ff000000ull) << 8)
156  | ((x & 0x0000000000ff0000ull) << 24)
157  | ((x & 0x000000000000ff00ull) << 40)
158  | ((x & 0x00000000000000ffull) << 56);
159 
160 #endif
161  }
162 
167  inline String formatString(const char *format, ...)
168  {
169  // Sufficient buffer size for the current internal uses.
170  // Consider changing this value when you use this function.
171 
172  static const size_t BufferSize = 128;
173 
174  va_list args;
175  va_start(args, format);
176 
177  char buf[BufferSize];
178  int length;
179 
180 #if defined(HAVE_VSNPRINTF)
181 
182  length = vsnprintf(buf, BufferSize, format, args);
183 
184 #elif defined(HAVE_VSPRINTF_S)
185 
186  length = vsprintf_s(buf, format, args);
187 
188 #else
189 
190  // The last resort. May cause a buffer overflow.
191 
192  length = vsprintf(buf, format, args);
193  if(length >= BufferSize) {
194  debug("Utils::formatString() - Buffer overflow! Returning an empty string.");
195  length = -1;
196  }
197 
198 #endif
199 
200  va_end(args);
201 
202  if(length != -1)
203  return String(buf);
204  else
205  return String::null;
206  }
207 
211  enum ByteOrder
212  {
214  LittleEndian,
216  BigEndian
217  };
218 
222  inline ByteOrder systemByteOrder()
223  {
224  union {
225  int i;
226  char c;
227  } u;
228 
229  u.i = 1;
230  if(u.c == 1)
231  return LittleEndian;
232  else
233  return BigEndian;
234  }
235 
239  inline ByteOrder floatByteOrder()
240  {
241  union {
242  double d;
243  char c;
244  } u;
245 
246  // 1.0 is stored in memory like 0x3FF0000000000000 in canonical form.
247  // So the first byte is zero if little endian.
248 
249  u.d = 1.0;
250  if(u.c == 0)
251  return LittleEndian;
252  else
253  return BigEndian;
254  }
255 
256  }
257 }
258 
259 #endif
260 
261 #endif
static String null
Definition: tstring.h:506
unsigned int uint
Definition: taglib.h:66
unsigned short ushort
Definition: taglib.h:65
unsigned long long ulonglong
Definition: taglib.h:67
A namespace for all TagLib related classes and functions.
Definition: apefile.h:41