Branch Coverage

src/pdfmake_parser.c
Criterion Covered Total %
branch 260 538 48.3


line true false branch
24 60 0 if (msg) {
37 1500 117 if (num < p->xref_cap) return 1;
39 0 117 new_cap = p->xref_cap ? p->xref_cap * 2 : 64;
40 0 117 while (new_cap <= num) new_cap *= 2;
43 0 117 if (!new_xref) return 0;
55 80 16 for (i = 0; i < p->prev_count; i++) {
56 32 48 if (p->prev_offsets[i] == offset) return 1;
63 116 112 if (p->prev_count >= p->prev_cap) {
64 0 116 size_t new_cap = p->prev_cap ? p->prev_cap * 2 : 8;
66 0 116 if (!new_arr) return 0;
81 14583 0 if (c >= '0' && c <= '9') return c - '0';
8710 5873 if (c >= '0' && c <= '9') return c - '0';
82 1922 3951 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
1922 0 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
83 3951 0 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
3951 0 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
93 0 122 if (!p) return NULL;
100 0 122 if (!p->doc) {
109 0 122 if (!parser) return;
117 118 0 if (parser) parser->repair = enable ? 1 : 0;
121 0 0 return parser ? parser->err_msg : "NULL parser";
125 0 0 return parser ? parser->err_offset : 0;
141 218 0 for (i = 0; i + 7 < search_len; i++) {
142 218 0 if (memcmp(p->buf + i, needle, 5) == 0) {
148 0 218 if (!found) {
155 218 218 while (isdigit(*ptr)) maj = maj * 10 + (*ptr++ - '0');
156 218 0 if (*ptr == '.') ptr++;
157 218 218 while (isdigit(*ptr)) min = min * 10 + (*ptr++ - '0');
159 218 0 if (major) *major = maj;
160 218 0 if (minor) *minor = min;
183 42240 7002 for (i = 0; i < src_len; i++) {
184 1 42239 if (src[i] == '#') { has_escape = 1; break; }
187 7002 1 if (!has_escape) {
193 0 1 if (!decoded) return pdfmake_null();
196 5 1 for (i = 0; i < src_len; i++) {
197 1 4 if (src[i] == '#' && i + 2 < src_len) {
1 0 if (src[i] == '#' && i + 2 < src_len) {
200 1 0 if (hi >= 0 && lo >= 0) {
1 0 if (hi >= 0 && lo >= 0) {
224 0 47 if (!decoded) return pdfmake_null();
229 1197 47 for (i = 0; i < src_len; i++) {
232 3 1194 if (c == '\\' && i + 1 < src_len) {
3 0 if (c == '\\' && i + 1 < src_len) {
246 0 0 if (i + 1 < src_len && src[i + 1] == '\n') i++;
0 0 if (i + 1 < src_len && src[i + 1] == '\n') i++;
250 0 0 if (next >= '0' && next <= '7') {
0 0 if (next >= '0' && next <= '7') {
253 0 0 if (i + 1 < src_len && src[i + 1] >= '0' && src[i + 1] <= '7') {
0 0 if (i + 1 < src_len && src[i + 1] >= '0' && src[i + 1] <= '7') {
0 0 if (i + 1 < src_len && src[i + 1] >= '0' && src[i + 1] <= '7') {
255 0 0 if (i + 1 < src_len && src[i + 1] >= '0' && src[i + 1] <= '7') {
0 0 if (i + 1 < src_len && src[i + 1] >= '0' && src[i + 1] <= '7') {
0 0 if (i + 1 < src_len && src[i + 1] >= '0' && src[i + 1] <= '7') {
269 0 1194 if (c == '(') paren_depth++;
270 0 1194 else if (c == ')') paren_depth--;
291 0 420 if (!decoded) return pdfmake_null();
296 14581 420 for (i = 0; i < src_len; i++) {
299 0 14581 if (pdfmake_is_whitespace(c)) continue;
302 0 14581 if (digit < 0) continue; /* Invalid hex digit - skip */
304 7291 7290 if (high < 0) {
313 1 419 if (high >= 0) {
326 706 0 if (arr.kind != PDFMAKE_ARRAY) return pdfmake_null();
331 706 5836 if (tok.kind == PDFMAKE_TOK_ARR_CLOSE) {
335 0 5836 if (tok.kind == PDFMAKE_TOK_EOF) {
341 0 5836 if (p->last_err != PDFMAKE_OK) return pdfmake_null();
343 0 5836 if (!pdfmake_array_push(p->doc->arena, &arr, item)) {
368 0 1526 if (dict.kind != PDFMAKE_DICT) return pdfmake_null();
373 1526 5420 if (tok.kind == PDFMAKE_TOK_DICT_CLOSE) {
377 0 5420 if (tok.kind == PDFMAKE_TOK_EOF) {
383 0 5420 if (tok.kind != PDFMAKE_TOK_NAME) {
390 0 5420 if (key_obj.kind != PDFMAKE_NAME) return pdfmake_null();
394 0 5420 if (p->last_err != PDFMAKE_OK) return pdfmake_null();
396 0 5420 if (!pdfmake_dict_set(p->doc->arena, &dict, key_obj.as.name.id, value)) {
405 217 1309 if (next.kind == PDFMAKE_TOK_KW_STREAM) {
410 0 217 if (stream.kind != PDFMAKE_STREAM) return pdfmake_null();
417 596 217 while (pdfmake_dict_iter_next(&iter)) {
425 0 217 if (err != PDFMAKE_OK) return pdfmake_null();
436 431 0 while (endstream_pos < p->buf_len &&
437 125 306 (p->buf[endstream_pos] == '\r' || p->buf[endstream_pos] == '\n')) {
89 217 (p->buf[endstream_pos] == '\r' || p->buf[endstream_pos] == '\n')) {
443 217 0 if (endstream_pos + 9 > p->buf_len ||
444 0 217 memcmp(p->buf + endstream_pos, endstream_kw, 9) != 0) {
478 6561 1547 if (peek1.kind == PDFMAKE_TOK_INT) {
487 1893 4668 if (peek2.kind == PDFMAKE_TOK_KW_R) {
554 217 0 if (length_obj) {
555 217 0 if (length_obj->kind == PDFMAKE_INT) {
558 0 0 } else if (length_obj->kind == PDFMAKE_REF) {
561 0 0 if (resolved && resolved->kind == PDFMAKE_INT) {
0 0 if (resolved && resolved->kind == PDFMAKE_INT) {
575 217 0 if (have_length && body_offset + declared_len + endstream_len <= p->buf_len) {
217 0 if (have_length && body_offset + declared_len + endstream_len <= p->buf_len) {
579 429 0 while (expected_end < p->buf_len &&
580 125 304 (p->buf[expected_end] == '\r' || p->buf[expected_end] == '\n')) {
87 217 (p->buf[expected_end] == '\r' || p->buf[expected_end] == '\n')) {
584 217 0 if (expected_end + endstream_len <= p->buf_len &&
585 215 2 memcmp(p->buf + expected_end, endstream_marker, endstream_len) == 0) {
593 76 0 for (i = scan_start; i + endstream_len <= scan_end; i++) {
594 2 74 if (memcmp(p->buf + i, endstream_marker, endstream_len) == 0) {
599 2 0 if (body_end > body_offset && p->buf[body_end - 1] == '\n') {
2 0 if (body_end > body_offset && p->buf[body_end - 1] == '\n') {
602 2 0 if (body_end > body_offset && p->buf[body_end - 1] == '\r') {
0 2 if (body_end > body_offset && p->buf[body_end - 1] == '\r') {
634 0 911 if (num_tok.kind != PDFMAKE_TOK_INT) {
640 0 911 if (gen_tok.kind != PDFMAKE_TOK_INT) {
646 0 911 if (obj_tok.kind != PDFMAKE_TOK_KW_OBJ) {
656 0 911 if (p->last_err != PDFMAKE_OK) return p->last_err;
660 0 911 if (endobj_tok.kind != PDFMAKE_TOK_KW_ENDOBJ) {
662 0 0 if (endobj_tok.kind != PDFMAKE_TOK_KW_STREAM) {
671 0 911 while (num > p->doc->obj_cap) {
672 0 0 size_t new_cap = p->doc->obj_cap ? p->doc->obj_cap * 2 : PDFMAKE_DOC_INIT_CAP;
674 0 0 while (new_cap < num) new_cap *= 2;
677 0 0 if (!new_objs) {
695 244 667 if (num > p->doc->obj_count) {
700 911 0 if (ensure_xref_cap(p, num)) {
706 0 911 if (num >= p->xref_size) p->xref_size = num + 1;
728 4426 6 for (i = p->buf_len; i > search_start + keyword_len; ) {
730 212 4214 if (memcmp(p->buf + i, keyword, keyword_len) == 0) {
736 6 212 if (!found) {
746 424 0 while (ptr < end && (*ptr == ' ' || *ptr == '\t' || *ptr == '\r' || *ptr == '\n')) {
0 424 while (ptr < end && (*ptr == ' ' || *ptr == '\t' || *ptr == '\r' || *ptr == '\n')) {
0 424 while (ptr < end && (*ptr == ' ' || *ptr == '\t' || *ptr == '\r' || *ptr == '\n')) {
32 392 while (ptr < end && (*ptr == ' ' || *ptr == '\t' || *ptr == '\r' || *ptr == '\n')) {
180 212 while (ptr < end && (*ptr == ' ' || *ptr == '\t' || *ptr == '\r' || *ptr == '\n')) {
752 914 0 while (ptr < end && isdigit(*ptr)) {
702 212 while (ptr < end && isdigit(*ptr)) {
776 0 206 if (first_tok.kind != PDFMAKE_TOK_INT) {
783 0 206 if (count_tok.kind != PDFMAKE_TOK_INT) {
808 0 3046 if (off_tok.kind != PDFMAKE_TOK_INT) {
815 0 3046 if (gen_tok.kind != PDFMAKE_TOK_INT) {
822 3046 3046 while (type_tok.kind == PDFMAKE_TOK_WS || type_tok.kind == PDFMAKE_TOK_COMMENT) {
0 3046 while (type_tok.kind == PDFMAKE_TOK_WS || type_tok.kind == PDFMAKE_TOK_COMMENT) {
827 3046 0 if (type_tok.kind == PDFMAKE_TOK_ERROR && type_tok.length == 1) {
3046 0 if (type_tok.kind == PDFMAKE_TOK_ERROR && type_tok.length == 1) {
831 3046 0 if (!p->xref[obj_num].loaded) {
835 2868 178 if (type_char == 'n') {
844 1333 1713 if (obj_num >= p->xref_size) p->xref_size = obj_num + 1;
855 0 206 if (offset >= p->buf_len) {
863 206 0 if (tok.kind != PDFMAKE_TOK_KW_XREF) {
870 206 206 if (next.kind == PDFMAKE_TOK_KW_TRAILER) break; /* trailer follows */
873 0 206 if (err != PDFMAKE_OK) return err;
875 0 206 if (!ensure_xref_cap(p, first_num + count)) {
880 3046 206 for (i = 0; i < count; i++) {
882 0 3046 if (err != PDFMAKE_OK) return err;
905 0 244 if (tok.kind != PDFMAKE_TOK_KW_TRAILER) {
911 0 244 if (p->last_err != PDFMAKE_OK) return p->last_err;
913 0 244 if (trailer->kind != PDFMAKE_DICT) {
926 228 16 if (root && root->kind == PDFMAKE_REF) {
228 0 if (root && root->kind == PDFMAKE_REF) {
932 172 72 if (info && info->kind == PDFMAKE_REF) {
172 0 if (info && info->kind == PDFMAKE_REF) {
938 66 178 if (encrypt && encrypt->kind == PDFMAKE_REF) {
66 0 if (encrypt && encrypt->kind == PDFMAKE_REF) {
946 192 52 if (id_arr && id_arr->kind == PDFMAKE_ARRAY && pdfmake_array_len(id_arr) >= 1) {
192 0 if (id_arr && id_arr->kind == PDFMAKE_ARRAY && pdfmake_array_len(id_arr) >= 1) {
192 0 if (id_arr && id_arr->kind == PDFMAKE_ARRAY && pdfmake_array_len(id_arr) >= 1) {
948 192 0 if (id0 && id0->kind == PDFMAKE_STR && p->doc_id_len == 0) {
192 0 if (id0 && id0->kind == PDFMAKE_STR && p->doc_id_len == 0) {
82 110 if (id0 && id0->kind == PDFMAKE_STR && p->doc_id_len == 0) {
950 0 82 if (len > sizeof(p->doc_id)) len = sizeof(p->doc_id);
957 244 0 if (size && size->kind == PDFMAKE_INT) {
244 0 if (size && size->kind == PDFMAKE_INT) {
958 0 244 if (!ensure_xref_cap(p, (uint32_t)size->as.i)) {
961 0 244 if ((size_t)size->as.i > p->xref_size) {
968 48 196 if (prev && prev->kind == PDFMAKE_INT) {
48 0 if (prev && prev->kind == PDFMAKE_INT) {
974 32 16 if (prev_visited(p, prev_offset)) {
979 0 16 if (!prev_add(p, prev_offset)) {
985 0 16 if (err != PDFMAKE_OK) return err;
989 0 16 if (err != PDFMAKE_OK) return err;
1021 0 0 if (!w_arr || w_arr->kind != PDFMAKE_ARRAY || pdfmake_array_len(w_arr) < 3) {
0 0 if (!w_arr || w_arr->kind != PDFMAKE_ARRAY || pdfmake_array_len(w_arr) < 3) {
0 0 if (!w_arr || w_arr->kind != PDFMAKE_ARRAY || pdfmake_array_len(w_arr) < 3) {
1036 0 0 out->size = (size_obj && size_obj->kind == PDFMAKE_INT) ? size_obj->as.i : 0;
0 0 out->size = (size_obj && size_obj->kind == PDFMAKE_INT) ? size_obj->as.i : 0;
1056 0 0 if (root && root->kind == PDFMAKE_REF) {
0 0 if (root && root->kind == PDFMAKE_REF) {
1062 0 0 if (info && info->kind == PDFMAKE_REF) {
0 0 if (info && info->kind == PDFMAKE_REF) {
1068 0 0 if (encrypt && encrypt->kind == PDFMAKE_REF) {
0 0 if (encrypt && encrypt->kind == PDFMAKE_REF) {
1089 0 0 for (j = 0; j < w1; j++) field1 = (field1 << 8) | *ptr++;
1090 0 0 for (j = 0; j < w2; j++) field2 = (field2 << 8) | *ptr++;
1091 0 0 for (j = 0; j < w3; j++) field3 = (field3 << 8) | *ptr++;
1095 0 0 type = (w1 == 0) ? 1 : (int)field1;
1097 0 0 if (!p->xref[obj_num].loaded) {
1119 0 0 if (obj_num >= p->xref_size) {
1139 0 20 if (offset >= p->buf_len) {
1149 0 20 if (num_tok.kind != PDFMAKE_TOK_INT) {
1155 0 20 if (gen_tok.kind != PDFMAKE_TOK_INT) {
1161 20 0 if (obj_tok.kind != PDFMAKE_TOK_KW_OBJ) {
1168 0 0 if (p->last_err != PDFMAKE_OK) return p->last_err;
1170 0 0 if (stream.kind != PDFMAKE_STREAM) {
1180 0 0 if (err != PDFMAKE_OK) return err;
1184 0 0 if (err != PDFMAKE_OK) return err;
1190 0 0 if (params.index && params.index->kind == PDFMAKE_ARRAY) {
0 0 if (params.index && params.index->kind == PDFMAKE_ARRAY) {
1194 0 0 for (pair = 0; pair < num_pairs; pair++) {
1200 0 0 if (!first || !count) continue;
0 0 if (!first || !count) continue;
1205 0 0 if (!ensure_xref_cap(p, first_num + entry_count)) return PDFMAKE_ENOMEM;
1207 0 0 for (i = 0; i < entry_count && ptr + params.entry_size <= end; i++) {
0 0 for (i = 0; i < entry_count && ptr + params.entry_size <= end; i++) {
1216 0 0 if (!ensure_xref_cap(p, entry_count)) return PDFMAKE_ENOMEM;
1219 0 0 obj_num < entry_count && ptr + params.entry_size <= end;
0 0 obj_num < entry_count && ptr + params.entry_size <= end;
1231 0 0 if (prev && prev->kind == PDFMAKE_INT) {
0 0 if (prev && prev->kind == PDFMAKE_INT) {
1236 0 0 if (prev_visited(p, prev_offset)) {
1241 0 0 if (!prev_add(p, prev_offset)) {
1249 0 0 if (prev_err != PDFMAKE_OK) {
1252 0 0 if (prev_err == PDFMAKE_OK) {
1257 0 0 if (prev_err != PDFMAKE_OK) return prev_err;
1281 0 181 if (stream->filtered) {
1298 88 93 if (!filter) {
1310 0 93 if (err != PDFMAKE_OK) {
1319 0 93 if (!data) {
1343 44 2912 if (tok.kind == PDFMAKE_TOK_EOF) break;
1345 1820 1092 if (tok.kind == PDFMAKE_TOK_INT) {
1350 1046 774 if (gen_tok.kind == PDFMAKE_TOK_INT) {
1352 256 790 if (obj_tok.kind == PDFMAKE_TOK_KW_OBJ) {
1357 256 0 if (ensure_xref_cap(p, num + 1)) {
1362 52 204 if (num >= p->xref_size) {
1370 5817 240 if (skip.kind == PDFMAKE_TOK_KW_ENDOBJ ||
1371 5801 16 skip.kind == PDFMAKE_TOK_EOF) {
1391 44 17848 if (tok.kind == PDFMAKE_TOK_EOF) break;
1392 38 17810 if (tok.kind == PDFMAKE_TOK_KW_TRAILER) {
1397 38 6 if (trailer_offset > 0) {
1416 1 11364 if (ref.num >= p->xref_size) return NULL;
1419 0 11364 if (entry->type == PDFMAKE_XREF_FREE) return NULL;
1422 0 11364 if (entry->gen != ref.gen) return NULL;
1425 10476 888 if (entry->loaded && ref.num < p->doc->obj_count) {
10453 23 if (entry->loaded && ref.num < p->doc->obj_count) {
1427 10453 0 if (obj) return obj;
1431 911 0 if (entry->type == PDFMAKE_XREF_UNCOMPRESSED) {
1446 911 0 if (err == PDFMAKE_OK) {
1451 0 0 else if (entry->type == PDFMAKE_XREF_COMPRESSED) {
1476 0 0 if (!stm_obj || stm_obj->kind != PDFMAKE_STREAM) return NULL;
0 0 if (!stm_obj || stm_obj->kind != PDFMAKE_STREAM) return NULL;
1479 0 0 if (pdfmake_decode_stream(p, stm_obj->as.stream, &decoded, &decoded_len) != PDFMAKE_OK) {
1494 0 0 if (!n_obj || !first_obj) return NULL;
0 0 if (!n_obj || !first_obj) return NULL;
1505 0 0 for (i = 0; i < n; i++) {
1509 0 0 if (num_tok.kind != PDFMAKE_TOK_INT || off_tok.kind != PDFMAKE_TOK_INT) {
0 0 if (num_tok.kind != PDFMAKE_TOK_INT || off_tok.kind != PDFMAKE_TOK_INT) {
1513 0 0 if ((uint32_t)i == target_index) {
1519 0 0 if (target_offset < 0 || (size_t)target_offset >= decoded_len) {
0 0 if (target_offset < 0 || (size_t)target_offset >= decoded_len) {
1530 0 0 if (p->last_err != PDFMAKE_OK) return NULL;
1533 0 0 while (ref.num > p->doc->obj_cap) {
1534 0 0 size_t new_cap = p->doc->obj_cap ? p->doc->obj_cap * 2 : PDFMAKE_DOC_INIT_CAP;
1536 0 0 while (new_cap < ref.num) new_cap *= 2;
1539 0 0 if (!new_objs) return NULL;
1552 0 0 if (ref.num > p->doc->obj_count) {
1585 0 218 if (err != PDFMAKE_OK) return err;
1589 6 212 if (err != PDFMAKE_OK) {
1590 6 0 if (p->repair) {
1592 0 6 if (err != PDFMAKE_OK) return err;
1598 0 212 if (!prev_add(p, xref_offset)) {
1604 212 0 if (xref_offset < p->buf_len) {
1608 20 192 if (first.kind == PDFMAKE_TOK_INT) {
1611 190 2 } else if (first.kind == PDFMAKE_TOK_KW_XREF) {
1614 190 0 if (err == PDFMAKE_OK) {
1622 38 174 if (err != PDFMAKE_OK) {
1623 38 0 if (p->repair) {
1625 0 38 if (err != PDFMAKE_OK) return err;
1632 0 0 if (p->repair) {
1634 0 0 if (err != PDFMAKE_OK) return err;
1643 212 6 if (p->root_num > 0) {
1646 156 62 if (p->info_num > 0) {