introduce and use helpers lms_string_size_strndup() and lms_string_size_dup()
[platform/upstream/lightmediascanner.git] / src / plugins / shared / util.h
1 /**
2  * Copyright (C) 2013  Intel Corporation. All rights reserved.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1 of
7  * the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  *
19  * @author Lucas De Marchi <lucas.demarchi@intel.com>
20  */
21
22 #include <byteswap.h>
23 #include <endian.h>
24 #include <inttypes.h>
25 #include <stddef.h>
26
27 #include <lightmediascanner_utils.h>
28
29 #define NSEC100_PER_SEC  10000000ULL
30 #define MSEC_PER_SEC  1000ULL
31
32 #define get_unaligned(ptr)                      \
33     ({                                          \
34         struct __attribute__((packed)) {        \
35             typeof(*(ptr)) __v;                 \
36         } *__p = (typeof(__p)) (ptr);           \
37         __p->__v;                               \
38     })
39
40 #if __BYTE_ORDER == __LITTLE_ENDIAN
41 static inline uint64_t get_le64(const void *ptr)
42 {
43         return get_unaligned((const uint64_t *) ptr);
44 }
45
46 static inline uint64_t get_be64(const void *ptr)
47 {
48         return bswap_64(get_unaligned((const uint64_t *) ptr));
49 }
50
51 static inline uint32_t get_le32(const void *ptr)
52 {
53         return get_unaligned((const uint32_t *) ptr);
54 }
55
56 static inline uint32_t get_be32(const void *ptr)
57 {
58         return bswap_32(get_unaligned((const uint32_t *) ptr));
59 }
60
61 static inline uint16_t get_le16(const void *ptr)
62 {
63     return get_unaligned((const uint16_t *) ptr);
64 }
65
66 static inline uint16_t get_be16(const void *ptr)
67 {
68     return bswap_16(get_unaligned((const uint16_t *) ptr));
69 }
70
71 #elif __BYTE_ORDER == __BIG_ENDIAN
72 static inline uint64_t get_le64(const void *ptr)
73 {
74         return bswap_64(get_unaligned((const uint64_t *) ptr));
75 }
76
77 static inline uint64_t get_be64(const void *ptr)
78 {
79         return get_unaligned((const uint64_t *) ptr);
80 }
81
82 static inline uint32_t get_le32(const void *ptr)
83 {
84         return bswap_32(get_unaligned((const uint32_t *) ptr));
85 }
86
87 static inline uint32_t get_be32(const void *ptr)
88 {
89         return get_unaligned((const uint32_t *) ptr);
90 }
91
92 static inline uint16_t get_le16(const void *ptr)
93 {
94         return bswap_16(get_unaligned((const uint16_t *) ptr));
95 }
96
97 static inline uint16_t get_be16(const void *ptr)
98 {
99         return get_unaligned((const uint16_t *) ptr);
100 }
101
102 #else
103 #error "Unknown byte order"
104 #endif