third_party/modest/source/myurl/serialization.c

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 62 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 171 SCAR 0
CC
0
Cov
0%
CRAP 0
« third_party/modest/source/myurl/scheme.c ↑ Summary third_party/modest/source/myurl/url.c »
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 "myurl/url.h"
22 #include "myhtml/serialization.h"
23  
24 0 void myurl_serialization_host(myurl_entry_t* url_entry, mycore_callback_serialize_f callback, void* ctx)
25 {
26 0 switch (url_entry->host.type) {
27 0 case MyURL_HOST_TYPE_DOMAIN:
28 0 callback(url_entry->host.value.domain.value, url_entry->host.value.domain.length, ctx);
29 0 break;
30
31 0 case MyURL_HOST_TYPE_IPv4: {
32 char digit_buffer[6];
33 0 char buffer[17] = {0};
34
35 0 size_t ipv4_length = 12 + 4;
36 0 size_t digit_size = 0;
37
38 0 unsigned int n = url_entry->host.value.ipv.pieces[0];
39
40 0 ▶ for(size_t i = 0; i < 4; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(size_t i = 0; i < 4; i++) 0 0 0
41 {
42 0 digit_size = myurl_convert_integer_to_data_without_check_buffer(n % 256, digit_buffer);
43
44 0 ▶ if(digit_size <= ipv4_length) {
Branch: 1 branch
BranchTrueFalseTotal
if(digit_size <= ipv4_length) { 0 0 0
45 0 ipv4_length -= digit_size;
46
47 0 memcpy(&buffer[ipv4_length], digit_buffer, sizeof(char) * digit_size);
48 }
49
50 0 ▶ if(ipv4_length && i != 3) {
Branch: 2 branches
BranchTrueFalseTotal
if(ipv4_length && i != 3) { 0 0 0
if(ipv4_length && i != 3) { 0 0 0
51 0 ipv4_length--;
52 0 buffer[ipv4_length] = '.';
53 }
54
55 0 n = (unsigned int)floor((double)n / 256.0f);
56 }
57
58 0 callback(&buffer[ipv4_length], (16 - ipv4_length), ctx);
59 0 break;
60 }
61
62 0 case MyURL_HOST_TYPE_IPv6: {
63 0 callback("[", 1, ctx);
64
65 /* 1 */
66 0 unsigned int *pieces = url_entry->host.value.ipv.pieces;
67 0 unsigned int *compress_pointer = NULL;
68
69 0 char buffer[128] = {0};
70
71 /* 2, 3 */
72 0 ▶ for(size_t i = 0; i < 7; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(size_t i = 0; i < 7; i++) { 0 0 0
73 0 ▶ if(pieces[i] == 0 && pieces[(i + 1)] == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if(pieces[i] == 0 && pieces[(i + 1)] == 0) { 0 0 0
if(pieces[i] == 0 && pieces[(i + 1)] == 0) { 0 0 0
74 0 compress_pointer = &pieces[i];
75 0 break;
76 }
77 }
78
79 /* 4 */
80 0 ▶ for(size_t i = 0; i < 8; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(size_t i = 0; i < 8; i++) { 0 0 0
81 /* 4.1 */
82 0 ▶ if(compress_pointer == &pieces[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if(compress_pointer == &pieces[i]) { 0 0 0
83
84
85 0 ▶ if(i == 0)
Branch: 1 branch
BranchTrueFalseTotal
if(i == 0) 0 0 0
86 0 callback("::", 2, ctx);
87 else
88 0 callback(":", 1, ctx);
89
90 0 i++;
91 0 ▶ while(i < 8) {
Branch: 1 branch
BranchTrueFalseTotal
while(i < 8) { 0 0 0
92 0 ▶ if(pieces[i] != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(pieces[i] != 0) { 0 0 0
93 0 i--;
94 0 break;
95 }
96
97 0 i++;
98 }
99 }
100 else {
101 /* 4.2 */
102 0 size_t len = myurl_convert_integer_to_hex_data_without_check_buffer((long)pieces[i], buffer);
103 0 callback(buffer, len, ctx);
104
105 /* 4.3 */
106 0 ▶ if(i != 7)
Branch: 1 branch
BranchTrueFalseTotal
if(i != 7) 0 0 0
107 0 callback(":", 1, ctx);
108 }
109 }
110
111 0 callback("]", 1, ctx);
112
113 0 break;
114 }
115
116 0 case MyURL_HOST_TYPE_OPAQUE:
117 0 callback(url_entry->host.value.opaque.value, url_entry->host.value.opaque.length, ctx);
118 0 break;
119
120 0 default:
121 0 break;
122 }
123 0 }
124  
125 0 void myurl_serialization_authority(myurl_entry_t* url_entry, mycore_callback_serialize_f callback, void* ctx)
126 {
127 0 ▶ if(url_entry->username_length || url_entry->password_length) {
Branch: 2 branches
BranchTrueFalseTotal
if(url_entry->username_length || url_entry->password_length) { 0 0 0
if(url_entry->username_length || url_entry->password_length) { 0 0 0
128 0 callback(url_entry->username, url_entry->username_length, ctx);
129
130 0 ▶ if(url_entry->password_length) {
Branch: 1 branch
BranchTrueFalseTotal
if(url_entry->password_length) { 0 0 0
131 0 callback(":", 1, ctx);
132 0 callback(url_entry->password, url_entry->password_length, ctx);
133 }
134 }
135 0 }
136  
137 0 void myurl_serialization_auth_host_port(myurl_entry_t* url_entry, mycore_callback_serialize_f callback, void* ctx)
138 {
139 0 callback("//", 2, ctx);
140
141 0 ▶ if(url_entry->username_length || url_entry->password_length) {
Branch: 2 branches
BranchTrueFalseTotal
if(url_entry->username_length || url_entry->password_length) { 0 0 0
if(url_entry->username_length || url_entry->password_length) { 0 0 0
142 0 myurl_serialization_authority(url_entry, callback, ctx);
143 0 callback("@", 1, ctx);
144 }
145
146 0 myurl_serialization_host(url_entry, callback, ctx);
147
148 0 ▶ if(url_entry->port_is_set) {
Branch: 1 branch
BranchTrueFalseTotal
if(url_entry->port_is_set) { 0 0 0
149 0 callback(":", 1, ctx);
150
151 0 char return_str[128] = {0};
152 0 size_t length = myurl_convert_integer_to_data_without_check_buffer((long)url_entry->port, return_str);
153
154 0 callback(return_str, length, ctx);
155 }
156 0 }
157  
158 0 void myurl_serialization_path(myurl_entry_t* url_entry, mycore_callback_serialize_f callback, void* ctx)
159 {
160 0 ▶ if(url_entry->flags & MyURL_FLAGS_CANNOT_BE_BASE_URL) {
Branch: 1 branch
BranchTrueFalseTotal
if(url_entry->flags & MyURL_FLAGS_CANNOT_BE_BASE_URL) { 0 0 0
161 0 callback(url_entry->path.list[0].data, url_entry->path.list[0].length, ctx);
162 }
163 else {
164 0 ▶ for(size_t i = 0; i < url_entry->path.length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(size_t i = 0; i < url_entry->path.length; i++) { 0 0 0
165 0 callback("/", 1, ctx);
166 0 callback(url_entry->path.list[i].data, url_entry->path.list[i].length, ctx);
167 }
168 }
169 0 }
170  
171 0 void myurl_serialization_fragment(myurl_entry_t* url_entry, mycore_callback_serialize_f callback, void* ctx)
172 {
173 0 ▶ if(url_entry->fragment && url_entry->fragment_length)
Branch: 2 branches
BranchTrueFalseTotal
if(url_entry->fragment && url_entry->fragment_length) 0 0 0
if(url_entry->fragment && url_entry->fragment_length) 0 0 0
174 0 callback(url_entry->fragment, url_entry->fragment_length, ctx);
175 0 }
176  
177 0 void myurl_serialization_without_fragment(myurl_entry_t* url_entry, mycore_callback_serialize_f callback, void* ctx)
178 {
179 0 ▶ if(url_entry->scheme.name)
Branch: 1 branch
BranchTrueFalseTotal
if(url_entry->scheme.name) 0 0 0
180 0 callback(url_entry->scheme.name, url_entry->scheme.length, ctx);
181
182 0 callback(":", 1, ctx);
183
184 0 ▶ if(url_entry->host.type) {
Branch: 1 branch
BranchTrueFalseTotal
if(url_entry->host.type) { 0 0 0
185 0 myurl_serialization_auth_host_port(url_entry, callback, ctx);
186 }
187 0 ▶ else if(url_entry->scheme.sid == MyURL_SCHEME_ID_FILE) {
Branch: 1 branch
BranchTrueFalseTotal
else if(url_entry->scheme.sid == MyURL_SCHEME_ID_FILE) { 0 0 0
188 0 callback("//", 2, ctx);
189 }
190
191 0 myurl_serialization_path(url_entry, callback, ctx);
192
193 0 ▶ if(url_entry->query != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(url_entry->query != NULL) { 0 0 0
194 0 callback("?", 1, ctx);
195
196 0 ▶ if(url_entry->query_length)
Branch: 1 branch
BranchTrueFalseTotal
if(url_entry->query_length) 0 0 0
197 0 callback(url_entry->query, url_entry->query_length, ctx);
198 }
199 0 }
200  
201 0 void myurl_serialization_with_fragment(myurl_entry_t* url_entry, mycore_callback_serialize_f callback, void* ctx)
202 {
203 0 myurl_serialization_without_fragment(url_entry, callback, ctx);
204
205 0 ▶ if(url_entry->fragment != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(url_entry->fragment != NULL) { 0 0 0
206 0 callback("#", 1, ctx);
207
208 0 ▶ if(url_entry->fragment_length)
Branch: 1 branch
BranchTrueFalseTotal
if(url_entry->fragment_length) 0 0 0
209 0 callback(url_entry->fragment, url_entry->fragment_length, ctx);
210 }
211 0 }
212  
213 0 void myurl_serialization(myurl_entry_t* url_entry, bool exclude_fragment_flag, mycore_callback_serialize_f callback, void* ctx)
214 {
215 0 ▶ if(exclude_fragment_flag)
Branch: 1 branch
BranchTrueFalseTotal
if(exclude_fragment_flag) 0 0 0
216 0 myurl_serialization_without_fragment(url_entry, callback, ctx);
217 else
218 0 myurl_serialization_with_fragment(url_entry, callback, ctx);
219 0 }
220  
221  
« third_party/modest/source/myurl/scheme.c ↑ Summary third_party/modest/source/myurl/url.c »