1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*  Fluent Bit
4  *  ==========
5  *  Copyright (C) 2019-2021 The Fluent Bit Authors
6  *  Copyright (C) 2015-2018 Treasure Data Inc.
7  *
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  */
20 
21 /*
22  * The following SDS interface is a clone/strip-down version of the original
23  * SDS library created by Antirez at https://github.com/antirez/sds.
24  */
25 
26 #ifndef FLB_SDS_H
27 #define FLB_SDS_H
28 
29 #include <fluent-bit/flb_info.h>
30 #include <fluent-bit/flb_macros.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <inttypes.h>
34 
35 #define FLB_SDS_HEADER_SIZE (sizeof(uint64_t) + sizeof(uint64_t))
36 
37 typedef char *flb_sds_t;
38 
39 #pragma pack(push, 1)
40 struct flb_sds {
41     uint64_t len;        /* used */
42     uint64_t alloc;      /* excluding the header and null terminator */
43     char buf[];
44 };
45 #pragma pack(pop)
46 
47 #define FLB_SDS_HEADER(s)  ((struct flb_sds *) (s - FLB_SDS_HEADER_SIZE))
48 
flb_sds_len(flb_sds_t s)49 static inline size_t flb_sds_len(flb_sds_t s)
50 {
51     return (size_t) FLB_SDS_HEADER(s)->len;
52 }
53 
flb_sds_is_empty(flb_sds_t s)54 static inline int flb_sds_is_empty(flb_sds_t s)
55 {
56     if (flb_sds_len(s) == 0) {
57         return FLB_TRUE;
58     }
59 
60     return FLB_FALSE;
61 }
62 
flb_sds_len_set(flb_sds_t s,size_t len)63 static inline void flb_sds_len_set(flb_sds_t s, size_t len)
64 {
65     FLB_SDS_HEADER(s)->len = len;
66 }
67 
flb_sds_alloc(flb_sds_t s)68 static inline size_t flb_sds_alloc(flb_sds_t s)
69 {
70     return (size_t) FLB_SDS_HEADER(s)->alloc;
71 }
72 
flb_sds_avail(flb_sds_t s)73 static inline size_t flb_sds_avail(flb_sds_t s)
74 {
75     struct flb_sds *h;
76 
77     h = FLB_SDS_HEADER(s);
78     return (size_t) (h->alloc - h->len);
79 }
80 
flb_sds_cmp(flb_sds_t s,const char * str,int len)81 static inline int flb_sds_cmp(flb_sds_t s, const char *str, int len)
82 {
83     if (flb_sds_len(s) != len) {
84         return -1;
85     }
86 
87     return strncmp(s, str, len);
88 }
89 
flb_sds_casecmp(flb_sds_t s,const char * str,int len)90 static inline int flb_sds_casecmp(flb_sds_t s, const char *str, int len)
91 {
92     if (flb_sds_len(s) != len) {
93         return -1;
94     }
95 
96     return strncasecmp(s, str, len);
97 }
98 
99 flb_sds_t flb_sds_create(const char *str);
100 flb_sds_t flb_sds_create_len(const char *str, int len);
101 flb_sds_t flb_sds_create_size(size_t size);
102 flb_sds_t flb_sds_cat(flb_sds_t s, const char *str, int len);
103 flb_sds_t flb_sds_cat_esc(flb_sds_t s, const char *str, int len,
104                                        char *esc, size_t esc_size);
105 flb_sds_t flb_sds_cat_utf8(flb_sds_t *sds, const char *str, int len);
106 int flb_sds_cat_safe(flb_sds_t *buf, const char *str, int len);
107 flb_sds_t flb_sds_increase(flb_sds_t s, size_t len);
108 flb_sds_t flb_sds_copy(flb_sds_t s, const char *str, int len);
109 void flb_sds_destroy(flb_sds_t s);
110 flb_sds_t flb_sds_printf(flb_sds_t *sds, const char *fmt, ...);
111 
112 #endif
113