old_common.c

filter: Statementstmt 36.5% 38 / 104 Branchbran 22.5% 18 / 80 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 30.4% 56 / 184 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/JSON/DWIW/Changes.pm ↑ Summary DWIW.xs »
1 /* Creation date: 2008-04-15T03:00:18Z
2 * Authors: Don
3 */
4  
5 /*
6 Copyright (c) 2007-2010 Don Owens <don@regexguy.com>. All rights reserved.
7  
8 This is free software; you can redistribute it and/or modify it under
9 the Perl Artistic license. You should have received a copy of the
10 Artistic license with this distribution, in the file named
11 "Artistic". You may also obtain a copy from
12 http://regexguy.com/license/Artistic
13  
14 This program is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied
16 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17 PURPOSE.
18 */
19  
20 #include "old_common.h"
21 #include "libjsonevt/utf8.h"
22  
23 /* #define UNLESS(stuff) if (! stuff) */
24 #define SAFE_SET_POINTER_VAL(ptr, val) if (ptr) { *(ptr) = val; }
25  
26 uint32_t
27 0 common_utf8_bytes_to_unicode(const uint8_t *orig_buf, uint32_t buf_len, uint32_t *ret_len) {
28 uint32_t this_octet;
29 0 uint32_t code_point = 0;
30 0 uint32_t expected_len = 0;
31 0 uint32_t len = 0;
32 0 const uint8_t *buf = orig_buf;
33
34 0 ▶ if (buf_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (buf_len == 0) { 0 0 0
35 0 ▶ SAFE_SET_POINTER_VAL(ret_len, 0);
Branch: 1 branch
BranchTrueFalseTotal
SAFE_SET_POINTER_VAL(ret_len, 0); 0 0 0
36 0 return 0;
37 }
38
39 0 this_octet = *buf;
40
41 0 ▶ if (UTF8_BYTE_IS_INVARIANT(this_octet)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UTF8_BYTE_IS_INVARIANT(this_octet)) { 0 0 0
42 0 ▶ SAFE_SET_POINTER_VAL(ret_len, 1);
Branch: 1 branch
BranchTrueFalseTotal
SAFE_SET_POINTER_VAL(ret_len, 1); 0 0 0
43 0 return this_octet;
44 }
45  
46 /* the first byte should not be a continuation byte */
47 0 ▶ if (UTF8_IS_CONTINUATION_BYTE(this_octet)) {
Branch: 2 branches
BranchTrueFalseTotal
if (UTF8_IS_CONTINUATION_BYTE(this_octet)) { 0 0 0
if (UTF8_IS_CONTINUATION_BYTE(this_octet)) { 0 0 0
48 0 ▶ SAFE_SET_POINTER_VAL(ret_len, 0);
Branch: 1 branch
BranchTrueFalseTotal
SAFE_SET_POINTER_VAL(ret_len, 0); 0 0 0
49 0 return 0;
50 }
51  
52 0 ▶ UNLESS (UTF8_IS_START_BYTE(this_octet)) {
Branch: 2 branches
BranchTrueFalseTotal
UNLESS (UTF8_IS_START_BYTE(this_octet)) { 0 0 0
UNLESS (UTF8_IS_START_BYTE(this_octet)) { 0 0 0
53 0 ▶ SAFE_SET_POINTER_VAL(ret_len, 0);
Branch: 1 branch
BranchTrueFalseTotal
SAFE_SET_POINTER_VAL(ret_len, 0); 0 0 0
54 0 return 0;
55 }
56  
57 /* compute the number of expected bytes and pull out the bits
58 contributing to the code point
59 */
60 0 ▶ if ((this_octet & 0xf8) == 0xf0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((this_octet & 0xf8) == 0xf0) { 0 0 0
61 0 expected_len = 4;
62 0 this_octet &= 0x07;
63 }
64 0 ▶ else if ((this_octet & 0xf0) == 0xe0) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((this_octet & 0xf0) == 0xe0) { 0 0 0
65 0 expected_len = 3;
66 0 this_octet &= 0x0f;
67 }
68 0 ▶ else if ((this_octet & 0xe0) == 0xc0) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((this_octet & 0xe0) == 0xc0) { 0 0 0
69 0 expected_len = 2;
70 0 this_octet &= 0x1f;
71 }
72 else {
73 0 ▶ SAFE_SET_POINTER_VAL(ret_len, 0);
Branch: 1 branch
BranchTrueFalseTotal
SAFE_SET_POINTER_VAL(ret_len, 0); 0 0 0
74 0 return 0;
75 }
76  
77 0 ▶ if (buf_len < expected_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (buf_len < expected_len) { 0 0 0
78 0 ▶ SAFE_SET_POINTER_VAL(ret_len, 0);
Branch: 1 branch
BranchTrueFalseTotal
SAFE_SET_POINTER_VAL(ret_len, 0); 0 0 0
79 0 return 0;
80 }
81  
82 /* now need to grab the rest of the bytes */
83 /* grab the bits we want (mask with 0x3f) and OR it with the old value left shifted by 6 */
84
85 0 len = expected_len - 1;
86 0 buf++;
87 0 code_point = this_octet;
88  
89 0 ▶ while (len--) {
Branch: 1 branch
BranchTrueFalseTotal
while (len--) { 0 0 0
90 0 ▶ UNLESS (UTF8_IS_CONTINUATION_BYTE(*buf)) {
Branch: 2 branches
BranchTrueFalseTotal
UNLESS (UTF8_IS_CONTINUATION_BYTE(*buf)) { 0 0 0
UNLESS (UTF8_IS_CONTINUATION_BYTE(*buf)) { 0 0 0
91 0 ▶ SAFE_SET_POINTER_VAL(ret_len, 0);
Branch: 1 branch
BranchTrueFalseTotal
SAFE_SET_POINTER_VAL(ret_len, 0); 0 0 0
92 0 return 0;
93 }
94  
95 0 this_octet = *buf;
96  
97 0 this_octet = (this_octet & 0x3f) | (code_point << 6);
98  
99 /* FIXME: should check here for illegal vals? */
100  
101 0 buf++;
102 0 code_point = this_octet;
103 }
104  
105 0 ▶ SAFE_SET_POINTER_VAL(ret_len, expected_len);
Branch: 1 branch
BranchTrueFalseTotal
SAFE_SET_POINTER_VAL(ret_len, expected_len); 0 0 0
106  
107 0 return code_point;
108 }
109  
110 uint32_t
111 693 common_utf8_unicode_to_bytes(uint32_t cp, uint8_t *out_buf) {
112 693 uint8_t *d = out_buf;
113  
114 693 ▶ if (UNICODE_IS_INVARIANT(cp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNICODE_IS_INVARIANT(cp)) { 688 5 693
115 688 *d = cp;
116 688 return 1;
117 }
118  
119 5 ▶ if (cp < 0x0800) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x0800) { 3 2 5
120 /* 2 bytes */
121 3 *d++ = (cp >> 6) | 0xc0;
122 3 *d++ = (cp & 0x3f) | 0x80;
123 3 return 2;
124 }
125
126 2 ▶ if (cp < 0x010000) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x010000) { 2 0 2
127 /* 3 bytes */
128 2 *d++ = (cp >> 12) | 0xe0;
129 2 *d++ = ((cp >> 6) & 0x3f) | 0x80;
130 2 *d++ = (cp & 0x3f) | 0x80;
131 2 return 3;
132 }
133  
134 0 ▶ if (cp < 0x200000) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x200000) { 0 0 0
135 /* 4 bytes */
136 0 *d++ = (cp >> 18) | 0xf0;
137 0 *d++ = ((cp >> 12) & 0x3f) | 0x80;
138 0 *d++ = ((cp >> 6) & 0x3f) | 0x80;
139 0 *d++ = (cp & 0x3f) | 0x80;
140 0 return 4;
141 }
142  
143 /* invalid */
144 0 *d = 0;
145 0 return 0;
146 }
147  
148  
149 UV
150 33 get_bad_char_policy(HV * self_hash) {
151 33 SV ** ptr = NULL;
152 33 U8 * data_str = NULL;
153 33 STRLEN data_str_len = 0;
154  
155 33 ptr = hv_fetch((HV *)self_hash, "bad_char_policy", 15, 0);
156 33 ▶ if (ptr && SvTRUE(*ptr)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ptr && SvTRUE(*ptr)) { 2 31 33
if (ptr && SvTRUE(*ptr)) { 2 0 2
157 2 data_str = (U8 *)SvPV(*ptr, data_str_len);
158 2 ▶ if (data_str && data_str_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (data_str && data_str_len) { 2 0 2
if (data_str && data_str_len) { 2 0 2
159 2 ▶ if (strnEQ("error", (char *)data_str, data_str_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strnEQ("error", (char *)data_str, data_str_len)) { 0 2 2
160 0 return kBadCharError;
161 }
162 2 ▶ else if (strnEQ("convert", (char *)data_str, data_str_len)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strnEQ("convert", (char *)data_str, data_str_len)) { 2 0 2
163 2 return kBadCharConvert;
164 }
165 0 ▶ else if (strnEQ("pass_through", (char *)data_str, data_str_len)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strnEQ("pass_through", (char *)data_str, data_str_len)) { 0 0 0
166 0 return kBadCharPassThrough;
167 }
168 }
169 }
170  
171 31 return kBadCharError;
172 }
173  
174 static int g_have_big_int = kHaveModuleNotChecked;
175 static int g_have_big_float = kHaveModuleNotChecked;
176  
177 int
178 2 have_bigint() {
179 SV *rv;
180
181 2 ▶ if (g_have_big_int != kHaveModuleNotChecked) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_have_big_int != kHaveModuleNotChecked) { 0 2 2
182 0 ▶ if (g_have_big_int == kHaveModule) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_have_big_int == kHaveModule) { 0 0 0
183 0 return 1;
184 }
185 else {
186 0 return 0;
187 }
188 }
189  
190 2 rv = eval_pv("require Math::BigInt", 0);
191 2 ▶ if (rv && SvTRUE(rv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv && SvTRUE(rv)) { 2 0 2
if (rv && SvTRUE(rv)) { 2 0 2
192 /* module loaded successfully */
193 2 g_have_big_int = kHaveModule;
194 2 return 1;
195 }
196 else {
197 /* we don't have it */
198 0 g_have_big_int = kHaveModuleDontHave;
199 0 return 0;
200 }
201  
202 return 0;
203
204 }
205  
206 int
207 2 have_bigfloat() {
208 SV *rv;
209
210 2 ▶ if (g_have_big_float != kHaveModuleNotChecked) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_have_big_float != kHaveModuleNotChecked) { 0 2 2
211 0 ▶ if (g_have_big_float == kHaveModule) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_have_big_float == kHaveModule) { 0 0 0
212 0 return 1;
213 }
214 else {
215 0 return 0;
216 }
217 }
218  
219 2 rv = eval_pv("require Math::BigFloat", 0);
220 2 ▶ if (rv && SvTRUE(rv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv && SvTRUE(rv)) { 2 0 2
if (rv && SvTRUE(rv)) { 2 0 2
221 /* module loaded successfully */
222 2 g_have_big_float = kHaveModule;
223 2 return 1;
224 }
225 else {
226 /* we don't have it */
227 0 g_have_big_float = kHaveModuleDontHave;
228 0 return 0;
229 }
230  
231 return 0;
232
233 }
234  
« blib/lib/JSON/DWIW/Changes.pm ↑ Summary DWIW.xs »