File Coverage

third_party/modest/source/mycore/incoming.c
Criterion Covered Total %
statement 30 103 29.1
branch 13 44 29.5
condition n/a
subroutine n/a
pod n/a
total 43 147 29.2


line stmt bran cond sub pod time code
1             /*
2             Copyright (C) 2016-2017 Alexander Borisov
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
6             License as published by the Free Software Foundation; either
7             version 2.1 of the License, or (at your option) any later version.
8            
9             This library is distributed in the hope that it will be useful,
10             but 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
17            
18             Author: lex.borisov@gmail.com (Alexander Borisov)
19             */
20              
21             #include "mycore/incoming.h"
22             #include "mycore/utils/resources.h"
23              
24 277           mycore_incoming_buffer_t * mycore_incoming_buffer_add(mycore_incoming_buffer_t *current, mcobject_t *mcobject,
25             const char *html, size_t html_size)
26             {
27 277           mycore_incoming_buffer_t *inc_buf = mcobject_malloc(mcobject, NULL);
28            
29 277           inc_buf->size = html_size;
30 277           inc_buf->length = 0;
31 277           inc_buf->data = html;
32            
33 277 100         if(current) {
34 3           inc_buf->offset = current->offset + current->size;
35 3           current->next = inc_buf;
36             }
37             else {
38 274           inc_buf->offset = 0;
39             }
40            
41 277           inc_buf->prev = current;
42 277           inc_buf->next = NULL;
43            
44 277           return inc_buf;
45             }
46              
47 0           void mycore_incoming_buffer_clean(mycore_incoming_buffer_t *current)
48             {
49 0           memset(current, 0, sizeof(mycore_incoming_buffer_t));
50 0           }
51              
52 0           mycore_incoming_buffer_t * mycore_incoming_buffer_split(mycore_incoming_buffer_t *current, mcobject_t *mcobject, size_t global_pos)
53             {
54 0           size_t relative_pos = global_pos - current->offset;
55 0           mycore_incoming_buffer_t *inc_buf = mcobject_malloc(mcobject, NULL);
56            
57 0           inc_buf->size = current->size - relative_pos;
58 0           inc_buf->length = inc_buf->size;
59 0           inc_buf->data = ¤t->data[relative_pos];
60 0           inc_buf->offset = current->offset + relative_pos;
61 0           inc_buf->next = NULL;
62 0           inc_buf->prev = current;
63            
64 0           current->next = inc_buf;
65 0           current->size = relative_pos;
66 0           current->length = relative_pos;
67            
68 0           return inc_buf;
69             }
70              
71 1453           mycore_incoming_buffer_t * mycore_incoming_buffer_find_by_position(mycore_incoming_buffer_t *inc_buf, size_t begin)
72             {
73 1453 100         if(inc_buf->offset < begin) {
74 1335 50         while(inc_buf && (inc_buf->offset + inc_buf->size) < begin)
    100          
75 3           inc_buf = inc_buf->next;
76             }
77             else {
78 121 50         while(inc_buf && inc_buf->offset > begin)
    50          
79 0           inc_buf = inc_buf->prev;
80             }
81            
82 1453           return inc_buf;
83             }
84              
85 0           const char * mycore_incoming_buffer_data(mycore_incoming_buffer_t *inc_buf)
86             {
87 0           return inc_buf->data;
88             }
89              
90 0           size_t mycore_incoming_buffer_length(mycore_incoming_buffer_t *inc_buf)
91             {
92 0           return inc_buf->length;
93             }
94              
95 0           size_t mycore_incoming_buffer_size(mycore_incoming_buffer_t *inc_buf)
96             {
97 0           return inc_buf->size;
98             }
99              
100 0           size_t mycore_incoming_buffer_offset(mycore_incoming_buffer_t *inc_buf)
101             {
102 0           return inc_buf->offset;
103             }
104              
105 0           size_t mycore_incoming_buffer_relative_begin(mycore_incoming_buffer_t *inc_buf, size_t begin)
106             {
107 0           return (begin - inc_buf->offset);
108             }
109              
110 0           size_t mycore_incoming_buffer_available_length(mycore_incoming_buffer_t *inc_buf, size_t relative_begin, size_t length)
111             {
112 0 0         if((relative_begin + length) > inc_buf->size)
113 0           return (inc_buf->size - relative_begin);
114            
115 0           return length;
116             }
117              
118 0           mycore_incoming_buffer_t * mycore_incoming_buffer_next(mycore_incoming_buffer_t *inc_buf)
119             {
120 0           return inc_buf->next;
121             }
122              
123 0           mycore_incoming_buffer_t * mycore_incoming_buffer_prev(mycore_incoming_buffer_t *inc_buf)
124             {
125 0           return inc_buf->prev;
126             }
127              
128             // // //
129             // convert only one 002345 (\002345) to code point
130             //
131 0           size_t mycore_incoming_buffer_convert_one_escaped_to_code_point(mycore_incoming_buffer_t **inc_buf, size_t *relative_pos)
132             {
133             const unsigned char *u_data;
134 0           mycore_incoming_buffer_t *current = *inc_buf;
135            
136 0 0         if(*relative_pos >= current->size) {
137 0           *relative_pos = 0;
138 0           current = current->next;
139             }
140            
141 0           u_data = (const unsigned char*)current->data;
142            
143 0           unsigned int consume = 0;
144 0           size_t code_point = 0;
145            
146 0 0         while(current)
147             {
148 0 0         if(mycore_string_chars_num_map[ u_data[*relative_pos] ] != 0xff && consume < 6) {
    0          
149 0           code_point <<= 4;
150 0           code_point |= mycore_string_chars_hex_map[ u_data[*relative_pos] ];
151            
152 0           ++consume;
153             }
154             else
155             break;
156            
157 0           *relative_pos += 1;
158            
159 0 0         if(*relative_pos >= current->size)
160             {
161 0 0         if(current->next == NULL)
162 0           break;
163            
164 0           *relative_pos = 0;
165            
166 0           u_data = (const unsigned char*)current->data;
167 0           current = current->next;
168             }
169             }
170            
171 0           *inc_buf = current;
172            
173 0           return code_point;
174             }
175              
176 1           size_t mycore_incoming_buffer_escaped_case_cmp(mycore_incoming_buffer_t **inc_buf, const char *to, size_t to_size, size_t *relative_pos)
177             {
178 1           mycore_incoming_buffer_t *current = *inc_buf;
179            
180 1 50         if(*relative_pos >= current->size) {
181 0 0         if(current->next == 0)
182 0           return to_size;
183            
184 0           *relative_pos = 0;
185 0           current = current->next;
186             }
187            
188 1           const unsigned char *u_to = (const unsigned char*)to;
189 1           const unsigned char *u_data = (const unsigned char*)current->data;
190            
191 1           size_t i = 0;
192            
193 1 50         while(i < to_size)
194             {
195 1 50         if(u_data[*relative_pos] == 0x5C) {
196 0           *relative_pos += 1;
197            
198 0           size_t code_point = mycore_incoming_buffer_convert_one_escaped_to_code_point(¤t, relative_pos);
199            
200 0 0         if(code_point > 255 || mycore_string_chars_lowercase_map[code_point] != mycore_string_chars_lowercase_map[ u_to[i] ]) {
    0          
201             break;
202             }
203            
204 0           u_data = (const unsigned char*)current->data;
205             }
206 1 50         else if(mycore_string_chars_lowercase_map[ u_data[*relative_pos] ] != mycore_string_chars_lowercase_map[ u_to[i] ]) {
207 1           break;
208             }
209             else {
210 0           ++(*relative_pos);
211             }
212            
213 0           i++;
214            
215 0 0         if(*relative_pos >= current->size)
216             {
217 0 0         if(current->next == NULL)
218 0           break;
219            
220 0           current = current->next;
221 0           u_data = (const unsigned char*)current->data;
222 0           *relative_pos = 0;
223             }
224             }
225            
226 1           *inc_buf = current;
227 1           return (to_size - i);
228             }