Skip to content

Commit 7666a6b

Browse files
committed
Add C89 support
1 parent c21fdcf commit 7666a6b

24 files changed

+390
-288
lines changed

configure.ac

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ AH_TOP([#ifndef LIBLOGDB_CONFIG_H])
1010
AH_TOP([#define LIBLOGDB_CONFIG_H])
1111
AH_BOTTOM([#endif /*LIBLOGDB_CONFIG_H*/])
1212
AM_INIT_AUTOMAKE([no-define subdir-objects foreign])
13-
AC_HEADER_STDBOOL
1413
LT_INIT
1514

1615
PKG_PROG_PKG_CONFIG
1716
AC_PATH_TOOL(AR, ar)
1817
AC_PATH_TOOL(RANLIB, ranlib)
1918
AC_PATH_TOOL(STRIP, strip)
2019
AM_PROG_CC_C_O
21-
AC_PROG_CC_C99
20+
AC_PROG_CC_C89
21+
if test x"$ac_cv_prog_cc_c89" = x"no"; then
22+
AC_MSG_ERROR([c89 compiler support required])
23+
fi
2224

2325
CFLAGS="$CFLAGS -W"
2426

25-
warn_CFLAGS="-std=gnu99 -pedantic -Wno-unused-function -Wno-long-long -Wno-overlength-strings -Wno-pointer-arith"
27+
warn_CFLAGS="-pedantic -Wall -Wextra -Wno-unused-function -Wno-long-long -Wno-overlength-strings -Wno-pointer-arith"
2628
saved_CFLAGS="$CFLAGS"
2729
CFLAGS="$CFLAGS $warn_CFLAGS"
2830
AC_MSG_CHECKING([if ${CC} supports ${warn_CFLAGS}])

include/logdb/buffer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ LIBLOGDB_API struct buffer* buffer_copy(const void* data, size_t data_len);
5656
}
5757
#endif
5858

59-
#endif //__LIBLOGDB_BUFFER_H__
59+
#endif /* __LIBLOGDB_BUFFER_H__ */

include/logdb/cstr.h

+2-5
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,10 @@ LIBLOGDB_API int cstr_erase(cstring* s, size_t pos, ssize_t len);
5656
LIBLOGDB_API int cstr_append_buf(cstring* s, const void* buf, size_t sz);
5757
LIBLOGDB_API int cstr_append_cstr(cstring* s, cstring *append);
5858

59-
LIBLOGDB_API static inline int cstr_append_c(cstring* s, char ch)
60-
{
61-
return cstr_append_buf(s, &ch, 1);
62-
}
59+
LIBLOGDB_API int cstr_append_c(cstring* s, char ch);
6360

6461
#ifdef __cplusplus
6562
}
6663
#endif
6764

68-
#endif //__LIBLOGDB_CSTR_H__
65+
#endif /* __LIBLOGDB_CSTR_H__ */

include/logdb/logdb.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#ifndef __LIBLOGDB_H__
3131
#define __LIBLOGDB_H__
3232

33-
typedef uint8_t logdb_bool; //!serialize, c/c++ save bool
33+
typedef uint8_t logdb_bool; /*!serialize, c/c++ save bool*/
3434

3535
#ifndef true
3636
#define true 1
@@ -40,6 +40,10 @@ typedef uint8_t logdb_bool; //!serialize, c/c++ save bool
4040
#define false 0
4141
#endif
4242

43+
#ifndef NULL
44+
#define NULL 0
45+
#endif
46+
4347
#ifdef __cplusplus
4448
extern "C" {
4549
#endif
@@ -62,4 +66,4 @@ extern "C" {
6266
}
6367
#endif
6468

65-
#endif //__LIBLOGDB_H__
69+
#endif /* __LIBLOGDB_H__ */

include/logdb/logdb_file.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,24 @@ enum logdb_logdb_error {
6868
LOGDB_ERROR_WRONG_FILE_FORMAT = 300,
6969
LOGDB_ERROR_DATASTREAM_ERROR = 400,
7070
LOGDB_ERROR_CHECKSUM = 500,
71-
LOGDB_ERROR_FILE_ALREADY_OPEN = 600,
71+
LOGDB_ERROR_FILE_ALREADY_OPEN = 600
7272
};
7373

7474
/** logdb handle */
7575
typedef struct logdb_log_db {
7676
FILE *file;
77-
void (*mem_map_cb)(void*, logdb_logdb_record *); //callback for memory mapping
78-
void *cb_ctx; //callback context
79-
logdb_logdb_record *memdb_head; //optional non-schematic memory database
77+
void (*mem_map_cb)(void*, logdb_logdb_record *); /* callback for memory mapping */
78+
void *cb_ctx; /* callback context */
79+
logdb_logdb_record *memdb_head; /* optional non-schematic memory database */
8080
logdb_logdb_record *cache_head;
8181
SHA256_CTX hashctx;
8282
uint8_t hashlen;
8383
uint32_t version;
8484
uint32_t support_flags;
8585
} logdb_log_db;
8686

87-
/////////// DB HANDLING
88-
//////////////////////////////////
87+
/* DB HANDLING
88+
////////////////////////////////// */
8989
/** creates new logdb handle, sets default values */
9090
LIBLOGDB_API logdb_log_db* logdb_logdb_new();
9191

@@ -130,4 +130,4 @@ logdb_bool logdb_logdb_remove_existing_records(logdb_logdb_record *usehead, cstr
130130
}
131131
#endif
132132

133-
#endif //__LIBLOGDB_LOGDB_H__
133+
#endif /* __LIBLOGDB_LOGDB_H__ */

include/logdb/logdb_memdb.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ LIBLOGDB_API size_t logdb_memdb_size(logdb_log_db* db);
6060
}
6161
#endif
6262

63-
#endif //__LIBLOGDB_LOGDB_MEMDB_H__
63+
#endif /* __LIBLOGDB_LOGDB_MEMDB_H__ */

include/logdb/logdb_rec.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,13 @@ enum logdb_logdb_record_type {
4949
typedef struct logdb_logdb_record {
5050
cstring* key;
5151
cstring* value;
52-
struct logdb_logdb_record* next; //linked list -> next node (NULL if end)
53-
struct logdb_logdb_record* prev; //linked list -> prev node (NULL if end)
54-
int written; //! 0 = not written to databse, 1 = written
55-
uint8_t mode; // record mode, 0 = WRITE, 1 = ERASE
52+
struct logdb_logdb_record* next; /* linked list -> next node (NULL if end) */
53+
struct logdb_logdb_record* prev; /* linked list -> prev node (NULL if end) */
54+
int written; /* 0 = not written to databse, 1 = written */
55+
uint8_t mode; /* record mode, 0 = WRITE, 1 = ERASE */
5656
} logdb_logdb_record;
5757

58-
/////////// RECORD HANDLING
59-
//////////////////////////////////
58+
/* RECORD HANDLING */
6059
/** creates new logdb key/value record */
6160
LIBLOGDB_API logdb_logdb_record* logdb_logdb_record_new();
6261

@@ -85,4 +84,4 @@ LIBLOGDB_API logdb_logdb_record* logdb_logdb_record_rm_desc(logdb_logdb_record *
8584
}
8685
#endif
8786

88-
#endif //__LIBLOGDB_LOGDB_REC_H__
87+
#endif /* __LIBLOGDB_LOGDB_REC_H__ */

include/logdb/portable_endian.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// "License": Public Domain
1+
/* "License": Public Domain
22
// I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
33
// In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
44
// be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
55
// an example on how to get the endian conversion functions on different platforms.
6-
6+
*/
77
#ifndef PORTABLE_ENDIAN_H__
88
#define PORTABLE_ENDIAN_H__
99

include/logdb/serialize.h

+6-25
Original file line numberDiff line numberDiff line change
@@ -45,48 +45,29 @@ LIBLOGDB_API void ser_u16(cstring* s, uint16_t v_);
4545
LIBLOGDB_API void ser_u32(cstring* s, uint32_t v_);
4646
LIBLOGDB_API void ser_i32(cstring* s, int32_t v_);
4747
LIBLOGDB_API void ser_u64(cstring* s, uint64_t v_);
48-
static inline void ser_u256(cstring* s, const unsigned char* v_)
49-
{
50-
ser_bytes(s, v_, 32);
51-
}
52-
48+
LIBLOGDB_API void ser_u256(cstring* s, const unsigned char* v_);
5349
LIBLOGDB_API void ser_varlen(cstring* s, uint32_t vlen);
5450
LIBLOGDB_API void ser_str(cstring* s, const char* s_in, size_t maxlen);
5551
LIBLOGDB_API void ser_varstr(cstring* s, cstring* s_in);
5652

57-
static inline void ser_s32(cstring* s, int32_t v_)
58-
{
59-
ser_u32(s, (uint32_t)v_);
60-
}
53+
LIBLOGDB_API void ser_s32(cstring* s, int32_t v_);
6154

62-
static inline void ser_s64(cstring* s, int64_t v_)
63-
{
64-
ser_u64(s, (uint64_t)v_);
65-
}
55+
LIBLOGDB_API void ser_s64(cstring* s, int64_t v_);
6656

6757
LIBLOGDB_API int deser_skip(struct const_buffer* buf, size_t len);
6858
LIBLOGDB_API int deser_bytes(void* po, struct const_buffer* buf, size_t len);
6959
LIBLOGDB_API int deser_u16(uint16_t* vo, struct const_buffer* buf);
7060
LIBLOGDB_API int deser_u32(uint32_t* vo, struct const_buffer* buf);
71-
LIBLOGDB_API int deser_u64(uint64_t* vo, struct const_buffer* buf);
72-
7361
LIBLOGDB_API int deser_i32(int32_t* vo, struct const_buffer* buf);
74-
75-
76-
static inline int deser_u256(uint8_t* vo, struct const_buffer* buf)
77-
{
78-
return deser_bytes(vo, buf, 32);
79-
}
62+
LIBLOGDB_API int deser_u64(uint64_t* vo, struct const_buffer* buf);
63+
LIBLOGDB_API int deser_u256(uint8_t* vo, struct const_buffer* buf);
8064

8165
LIBLOGDB_API int deser_varlen(uint32_t* lo, struct const_buffer* buf);
8266
LIBLOGDB_API int deser_varlen_file(uint32_t* lo, FILE *file, uint8_t *rawdata, size_t *buflen_inout);
8367
LIBLOGDB_API int deser_str(char* so, struct const_buffer* buf, size_t maxlen);
8468
LIBLOGDB_API int deser_varstr(cstring** so, struct const_buffer* buf);
8569

86-
static inline int deser_s64(int64_t* vo, struct const_buffer* buf)
87-
{
88-
return deser_u64((uint64_t*)vo, buf);
89-
}
70+
LIBLOGDB_API int deser_s64(int64_t* vo, struct const_buffer* buf);
9071

9172
#ifdef __cplusplus
9273
}

include/logdb/sha2.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@ LIBLOGDB_API void hmac_sha512(const uint8_t* key, const uint32_t keylen, const u
7676
}
7777
#endif
7878

79-
#endif //__LOGDB_SHA2_H__
79+
#endif /* __LOGDB_SHA2_H__ */

include/logdb/utils.h

+10-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include <stdint.h>
3333
#include <stddef.h>
3434

35+
#include <logdb/logdb.h>
36+
3537
#ifdef __cplusplus
3638
extern "C" {
3739
#endif
@@ -41,16 +43,17 @@ extern "C" {
4143

4244
#define strlens(s) (s == NULL ? 0 : strlen(s))
4345

46+
LIBLOGDB_API void utils_clear_buffers(void);
47+
LIBLOGDB_API void utils_hex_to_bin(const char* str, unsigned char* out, int inLen, int* outLen);
48+
LIBLOGDB_API void utils_bin_to_hex(unsigned char* bin_in, size_t inlen, char* hex_out);
49+
LIBLOGDB_API uint8_t* utils_hex_to_uint8(const char* str);
50+
LIBLOGDB_API char* utils_uint8_to_hex(const uint8_t* bin, size_t l);
51+
LIBLOGDB_API void utils_reverse_hex(char* h, int len);
4452

45-
void utils_clear_buffers(void);
46-
void utils_hex_to_bin(const char* str, unsigned char* out, int inLen, int* outLen);
47-
void utils_bin_to_hex(unsigned char* bin_in, size_t inlen, char* hex_out);
48-
uint8_t* utils_hex_to_uint8(const char* str);
49-
char* utils_uint8_to_hex(const uint8_t* bin, size_t l);
50-
void utils_reverse_hex(char* h, int len);
53+
LIBLOGDB_API void * safe_malloc(size_t size);
5154

5255
#ifdef __cplusplus
5356
}
5457
#endif
5558

56-
#endif //LIBBTC_UTILS_H__
59+
#endif /* LIBBTC_UTILS_H__*/

src/cstr.c

+24-10
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@
99

1010
static int cstr_alloc_min_sz(cstring* s, size_t sz)
1111
{
12-
sz++; // NUL overhead
12+
unsigned int shift;
13+
unsigned int al_sz;
14+
char* new_s;
15+
16+
sz++; /* NULL overhead */
1317

1418
if (s->alloc && (s->alloc >= sz))
1519
return 1;
1620

17-
unsigned int shift = 3;
18-
unsigned int al_sz;
21+
shift = 3;
1922
while ((al_sz = (1 << shift)) < sz)
2023
shift++;
2124

22-
char* new_s = realloc(s->str, al_sz);
25+
new_s = realloc(s->str, al_sz);
2326
if (!new_s)
2427
return 0;
2528

@@ -59,10 +62,12 @@ cstring* cstr_new_buf(const void* buf, size_t sz)
5962

6063
cstring* cstr_new(const char* init_str)
6164
{
65+
size_t slen;
66+
6267
if (!init_str || !*init_str)
6368
return cstr_new_sz(0);
6469

65-
size_t slen = strlen(init_str);
70+
slen = strlen(init_str);
6671
return cstr_new_buf(init_str, slen);
6772
}
6873

@@ -80,22 +85,22 @@ void cstr_free(cstring* s, int free_buf)
8085

8186
int cstr_resize(cstring* s, size_t new_sz)
8287
{
83-
// no change
88+
/* no change */
8489
if (new_sz == s->len)
8590
return 1;
8691

87-
// truncate string
92+
/* truncate string */
8893
if (new_sz <= s->len) {
8994
s->len = new_sz;
9095
s->str[s->len] = 0;
9196
return 1;
9297
}
9398

94-
// increase string size
99+
/* increase string size */
95100
if (!cstr_alloc_min_sz(s, new_sz))
96101
return 0;
97102

98-
// contents of string tail undefined
103+
/* contents of string tail undefined */
99104

100105
s->len = new_sz;
101106
s->str[s->len] = 0;
@@ -120,6 +125,13 @@ int cstr_append_cstr(cstring* s, cstring *append)
120125
return cstr_append_buf(s, append->str, append->len);
121126
}
122127

128+
129+
int cstr_append_c(cstring* s, char ch)
130+
{
131+
return cstr_append_buf(s, &ch, 1);
132+
}
133+
134+
123135
int cstr_equal(const cstring* a, const cstring* b)
124136
{
125137
if (a == b)
@@ -133,12 +145,14 @@ int cstr_equal(const cstring* a, const cstring* b)
133145

134146
int cstr_erase(cstring* s, size_t pos, ssize_t len)
135147
{
148+
ssize_t old_tail;
149+
136150
if (pos == s->len && len == 0)
137151
return 1;
138152
if (pos >= s->len)
139153
return 0;
140154

141-
ssize_t old_tail = s->len - pos;
155+
old_tail = s->len - pos;
142156
if ((len >= 0) && (len > old_tail))
143157
return 0;
144158

0 commit comments

Comments
 (0)