]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/libloc/private.h
2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but 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.
17 #ifndef LIBLOC_PRIVATE_H
18 #define LIBLOC_PRIVATE_H
25 #include <libloc/libloc.h>
27 static inline void __attribute__((always_inline
, format(printf
, 2, 3)))
28 loc_log_null(struct loc_ctx
*ctx
, const char *format
, ...) {}
30 #define loc_log_cond(ctx, prio, arg...) \
32 if (loc_get_log_priority(ctx) >= prio) \
33 loc_log(ctx, prio, __FILE__, __LINE__, __FUNCTION__, ## arg); \
37 # define DEBUG(ctx, arg...) loc_log_cond(ctx, LOG_DEBUG, ## arg)
39 # define DEBUG(ctx, arg...) loc_log_null(ctx, ## arg)
42 #define INFO(ctx, arg...) loc_log_cond(ctx, LOG_INFO, ## arg)
43 #define ERROR(ctx, arg...) loc_log_cond(ctx, LOG_ERR, ## arg)
45 #ifndef HAVE_SECURE_GETENV
46 # ifdef HAVE___SECURE_GETENV
47 # define secure_getenv __secure_getenv
49 # define secure_getenv getenv
53 #define LOC_EXPORT __attribute__ ((visibility("default")))
55 void loc_log(struct loc_ctx
*ctx
,
56 int priority
, const char *file
, int line
, const char *fn
,
57 const char *format
, ...) __attribute__((format(printf
, 6, 7)));
60 static inline void hexdump(struct loc_ctx
* ctx
, const void* addr
, size_t len
) {
61 char buffer_hex
[16 * 3 + 6];
62 char buffer_ascii
[17];
65 unsigned char* p
= (unsigned char*)addr
;
67 DEBUG(ctx
, "Dumping %zu byte(s)\n", len
);
72 // Process every byte in the data
73 for (i
= 0; i
< len
; i
++) {
74 // Multiple of 16 means new line (with line offset)
76 // Just don't print ASCII for the zeroth line
78 DEBUG(ctx
, " %s %s\n", buffer_hex
, buffer_ascii
);
81 sprintf(buffer_hex
, "%04x ", i
);
84 // Now the hex code for the specific character
85 sprintf(buffer_hex
+ 5 + ((i
% 16) * 3), " %02x", p
[i
]);
87 // And store a printable ASCII character for later
88 if ((p
[i
] < 0x20) || (p
[i
] > 0x7e))
89 buffer_ascii
[i
% 16] = '.';
91 buffer_ascii
[i
% 16] = p
[i
];
94 buffer_ascii
[(i
% 16) + 1] = '\0';
97 // Pad out last line if not exactly 16 characters
98 while ((i
% 16) != 0) {
99 sprintf(buffer_hex
+ 5 + ((i
% 16) * 3), " ");
103 // And print the final bit
104 DEBUG(ctx
, " %s %s\n", buffer_hex
, buffer_ascii
);