"checkit_tiff" is an incredibly fast conformance checker for baseline TIFFs (with various extensions), see http://andreas-romeyke.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

222 lines
5.7 KiB

  1. /* rule based checks if given TIFF is a specific baseline TIFF
  2. *
  3. * author: Andreas Romeyke, 2015
  4. * licensed under conditions of libtiff
  5. * (see http://libtiff.maptools.org/misc.html)
  6. *
  7. */
  8. #define _GNU_SOURCE
  9. #include <string.h>
  10. #include "check.h"
  11. #include "check_helper.h"
  12. #ifdef WIN32
  13. static inline char *strndup(const char *s, size_t n)
  14. {
  15. char *result;
  16. size_t len = strlen (s);
  17. if (n < len) len = n;
  18. result = (char *) malloc (len + 1);
  19. if (!result) return 0;
  20. result[len] = '\0';
  21. return (char *) strncpy (result, s, len);
  22. }
  23. #endif
  24. ret_t tif_fails(const char* fail_message) {
  25. ret_t res;
  26. char * str =malloc( sizeof(char) *MAXSTRLEN );
  27. if (NULL==str) {
  28. fprintf(stderr, "could not allocate memory for tif_fails\n");
  29. exit(EXIT_FAILURE);
  30. };
  31. snprintf (str, MAXSTRLEN-1, "%s", fail_message);
  32. res.returnmsg = NULL;
  33. res.returncode=1;
  34. res.returnmsg = malloc( sizeof( retmsg_t ) );
  35. if (NULL==res.returnmsg) {
  36. fprintf(stderr, "could not allocate memory for tif_fails\n");
  37. exit(EXIT_FAILURE);
  38. };
  39. res.returnmsg->rm_type=rm_hard_error;
  40. res.count = 1;
  41. res.returnmsg->rm_msg = str;
  42. printf( "%s", renderer( res) );
  43. // free (str);
  44. // free (res.returnmsg);
  45. return res;
  46. }
  47. ret_t tif_fails_tag(tag_t tag, const char* expected, const char* value) {
  48. ret_t res = tif_returns( tag, expected, value);
  49. // call renderer
  50. printf( "%s", renderer( res) );
  51. // free (str);
  52. // free (res.returnmsg);
  53. return res;
  54. }
  55. ret_t tif_rules_tag(tag_t tag, const char *msg) {
  56. //printf("DEBUG: %s -> %s\n", tag, msg);
  57. ret_t res;
  58. res.returnmsg = NULL;
  59. res.returncode=0;
  60. res.count = 3;
  61. res.returnmsg = malloc( sizeof( retmsg_t ) * res.count );
  62. if (NULL==res.returnmsg) {
  63. fprintf(stderr, "could not allocate memory for tif_rules_tag\n");
  64. exit(EXIT_FAILURE);
  65. };
  66. retmsg_t * p = res.returnmsg;
  67. // header
  68. p->rm_type=rm_rule;
  69. p->rm_msg = "check if ";
  70. // tag
  71. p++;
  72. p->rm_type=rm_tag;
  73. p->rm_msg = malloc( sizeof(char) *MAXSTRLEN );
  74. if (NULL==p->rm_msg) {
  75. fprintf(stderr, "could not allocate memory for 3tif_fails\n");
  76. exit(EXIT_FAILURE);
  77. };
  78. snprintf (p->rm_msg, MAXSTRLEN-1, "tag %s ", tag2str(tag));
  79. // rule
  80. p++;
  81. p->rm_type=rm_rule;
  82. p->rm_msg = malloc( sizeof(char) *MAXSTRLEN );
  83. if (NULL==p->rm_msg) {
  84. fprintf(stderr, "could not allocate memory for 4tif_fails\n");
  85. exit(EXIT_FAILURE);
  86. };
  87. p->rm_msg = strndup( msg, MAXSTRLEN);
  88. printf( "%s", renderer( res) );
  89. // free (str);
  90. // free (res.returnmsg);
  91. return res;
  92. }
  93. ret_t tif_rules(const char *msg) {
  94. ret_t res;
  95. res.returnmsg = NULL;
  96. res.returncode=0;
  97. res.count = 2;
  98. res.returnmsg = malloc( sizeof( retmsg_t ) * res.count );
  99. if (NULL==res.returnmsg) {
  100. fprintf(stderr, "could not allocate memory for tif_rules_tag\n");
  101. exit(EXIT_FAILURE);
  102. };
  103. retmsg_t * p = res.returnmsg;
  104. // header
  105. p->rm_type=rm_default;
  106. p->rm_msg = "check if ";
  107. // rule
  108. p++;
  109. p->rm_type=rm_rule;
  110. p->rm_msg = malloc( sizeof(char) *MAXSTRLEN );
  111. if (NULL==p->rm_msg) {
  112. fprintf(stderr, "could not allocate memory for 4tif_fails\n");
  113. exit(EXIT_FAILURE);
  114. };
  115. p->rm_msg = strndup( msg, MAXSTRLEN);
  116. printf( "%s", renderer( res) );
  117. // free (str);
  118. // free (res.returnmsg);
  119. return res;
  120. }
  121. ret_t tif_files(const char *msg) {
  122. ret_t res;
  123. res.returnmsg = NULL;
  124. res.returncode=0;
  125. res.count = 2;
  126. res.returnmsg = malloc( sizeof( retmsg_t ) * res.count );
  127. if (NULL==res.returnmsg) {
  128. fprintf(stderr, "could not allocate memory for tif_rules_tag\n");
  129. exit(EXIT_FAILURE);
  130. };
  131. retmsg_t * p = res.returnmsg;
  132. // header
  133. p->rm_type=rm_default;
  134. p->rm_msg = "tiff file=";
  135. // rule
  136. p++;
  137. p->rm_type=rm_file;
  138. p->rm_msg = malloc( sizeof(char) *MAXSTRLEN );
  139. if (NULL==p->rm_msg) {
  140. fprintf(stderr, "could not allocate memory for 4tif_fails\n");
  141. exit(EXIT_FAILURE);
  142. };
  143. p->rm_msg = strndup( msg, MAXSTRLEN);
  144. printf( "%s", renderer( res) );
  145. // free (str);
  146. // free (res.returnmsg);
  147. return res;
  148. }
  149. void tifp_check( TIFF * tif) {
  150. if (NULL == tif) { tif_fails("TIFF pointer is empty\n"); };
  151. }
  152. /* TODO: add tif_returns specialized for types */
  153. ret_t tif_returns(tag_t tag, const char* expected, const char* value) {
  154. ret_t res;
  155. /*
  156. char * str =malloc( sizeof(char) *MAXSTRLEN );
  157. if (NULL==str) {
  158. fprintf(stderr, "could not allocate memory for tif_fails\n");
  159. exit(EXIT_FAILURE);
  160. };
  161. snprintf (str, MAXSTRLEN-1, "FAILTAG:tag >>%s<< should have value >>%s<<, but has count/value >>%s<<\n", tag, expected, value);
  162. */
  163. res.returnmsg = NULL;
  164. res.returncode=1;
  165. res.count = 4;
  166. res.returnmsg = malloc( sizeof( retmsg_t) * res.count );
  167. retmsg_t * p = res.returnmsg;
  168. if (NULL==res.returnmsg) {
  169. fprintf(stderr, "could not allocate memory for tif_fails\n");
  170. exit(EXIT_FAILURE);
  171. };
  172. // header
  173. p->rm_type=rm_error;
  174. p->rm_msg = "";
  175. // tag
  176. p++;
  177. p->rm_type=rm_tag;
  178. p->rm_msg = malloc( sizeof(char) *MAXSTRLEN );
  179. if (NULL==p->rm_msg) {
  180. fprintf(stderr, "could not allocate memory for tif_fails\n");
  181. exit(EXIT_FAILURE);
  182. };
  183. snprintf (p->rm_msg, MAXSTRLEN-1, "tag %s", tag2str( tag));
  184. // expected
  185. p++;
  186. p->rm_type=rm_expected;
  187. p->rm_msg = malloc( sizeof(char) *MAXSTRLEN );
  188. if (NULL==p->rm_msg) {
  189. fprintf(stderr, "could not allocate memory for tif_fails\n");
  190. exit(EXIT_FAILURE);
  191. };
  192. snprintf (p->rm_msg, MAXSTRLEN-1, " should have value %s", expected);
  193. // value
  194. p++;
  195. p->rm_type=rm_value;
  196. p->rm_msg = malloc( sizeof(char) *MAXSTRLEN );
  197. if (NULL==p->rm_msg) {
  198. fprintf(stderr, "could not allocate memory for tif_fails\n");
  199. exit(EXIT_FAILURE);
  200. };
  201. snprintf (p->rm_msg, MAXSTRLEN-1, ", but has value (values or count) %s", value);
  202. return res;
  203. }
  204. ret_t tif_fails_by_returns( ret_t ret ) {
  205. printf( "%s", renderer( ret) );
  206. return ret;
  207. }