Debian Codegraph @

src/lib/helper/simple_string.c

raw
1 /* 'checkit_tiff' is a conformance checker for baseline TIFFs
2 *
3 * author: Andreas Romeyke, 2015-2022
4 * licensed under conditions of libtiff
5 * (see http://libtiff.maptools.org/misc.html)
6 *
7 */
8
9 #include <assert.h>
10 #include "simple_string.h"
11
12 #include <ctype.h>
13 #include <stdio.h>
14 #include <string.h>
15
16 #include "strcopy.h"
17
18 unsigned long atoul (const char * s) {
19 /*
20 errno = 0;
21 char *endptr;
22 unsigned long val = strtoul(s, &endptr, 10);
23 if (errno != 0) {
24 perror("strtoul");
25 exit(EXIT_FAILURE);
26 }
27 if (endptr == s) {
28 fprintf(stderr, "No digits were found\n");
29 exit(EXIT_FAILURE);
30 }
31 printf("ATOUL: '%s' -> %lu\n", s, val);
32 return val;
33 */
34 long long int tmp = atoll(s);
35 if (tmp < 0) {
36 perror("negative numbers not supported");
37 exit(EXIT_FAILURE);
38 }
39 unsigned long v = (unsigned long) tmp & 0xffffffff;
40 return v;
41 }
42
43 unsigned int count_multiple_zero_bytes(const char *val, uint32 count) {
44 unsigned int r = 0;
45 for (uint32 i=0; i<count-1; i++) {
46 if (val[i] == '\0' && val[i+1] =='\0') {
47 r = i+1;
48 break;
49 }
50 }
51 return r;
52 }
53
54 void sprint_string_as_hexdump(char *hexdump_str, size_t hexdump_len, const char *val, uint32 len) {
55 /* largest possible value is:
56 * 12 -> for string constants
57 * strlen(val) * 2 + 2 -> for worst case of string
58 * strlen(val) * 14 -> for worst case of hexdump
59 * + some extra space -> reserved + \0
60 */
61 assert(NULL != hexdump_str);
62 assert(hexdump_len > 100);
63 if (hexdump_len < (len * 16 + 64) ) {
64 sprintf(hexdump_str, "(hexdump exceeds limit of useful view size, len=%zu > %zu)\n", (size_t) (len*16+64), hexdump_len);
65 return;
66 }
67 unsigned int res_pos = 0;
68 const char * str = "str='";
69 const char * undef_s = "�";
70 const char * hex = "' hex=";
71 const size_t str_len=strlen(str);
72 const size_t undef_len=strlen(undef_s);
73 const size_t hex_len = strlen(hex);
74 cit_strcopy(hexdump_str+res_pos, hexdump_len-res_pos, str, str_len);
75 //strncpy(hexdump_str + res_pos, str , hexdump_len - 1);
76 res_pos+=strlen(str);
77 for (unsigned int i = 0; i<= len-1; i++) {
78 if (isprint(val[i]) ) {
79 hexdump_str[res_pos] = val[i];
80 } else {
81 //strncpy(hexdump_str + res_pos, "�", hexdump_len - res_pos - 1);
82 cit_strcopy(hexdump_str + res_pos, hexdump_len-res_pos,undef_s, undef_len);
83 res_pos+=2;
84 }
85 res_pos++;
86 }
87
88 cit_strcopy(hexdump_str + res_pos, hexdump_len-res_pos, hex, hex_len);
89 res_pos+=hex_len;
90
91 /* hex output */
92 for (unsigned int i = 0; i<= len-1; i++) {
93 if (isprint(val[i])) {
94 snprintf(hexdump_str + res_pos, hexdump_len - res_pos - 1, " %02x", val[i]);
95 res_pos += 3;
96 } else {
97 /* problem here ist to determine the size of output */
98 char tmp[hexdump_len];
99 snprintf(tmp, hexdump_len - 1, " ---->%02x<----", (unsigned char) val[i]);
100 unsigned int tmp_len = strlen( tmp);
101 //strncpy(hexdump_str + res_pos, tmp, hexdump_len - res_pos - 1);
102 cit_strcopy(hexdump_str+res_pos, hexdump_len-res_pos, tmp, tmp_len);
103 res_pos += tmp_len;
104 }
105 if (res_pos >= hexdump_len - 1 ) {
106 hexdump_str[hexdump_len - 1] = '\0';
107 break;
108 }
109 }
110 hexdump_str[res_pos] = '\0';
111 //printf("respos=%u (hexdump_len=%lu)", res_pos, hexdump_len);
112 }