invalid-coverage.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <stdlib.h>
  2. #include <assert.h>
  3. #include <stdint.h>
  4. #include "qcms.h"
  5. typedef uint32_t __be32;
  6. /* __builtin_bswap isn't available in older gccs
  7. * so open code it for now */
  8. static __be32 cpu_to_be32(int32_t v)
  9. {
  10. #ifdef LITTLE_ENDIAN
  11. return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8) | ((v & 0xff000000) >> 24);
  12. //return __builtin_bswap32(v);
  13. #else
  14. return v;
  15. #endif
  16. }
  17. char *buf;
  18. static void write_u32(size_t offset, uint32_t value)
  19. {
  20. *(__be32*)(buf + offset) = cpu_to_be32(value);
  21. }
  22. #if 0
  23. static uint16_t read_u16(struct mem_source *mem, size_t offset)
  24. {
  25. if (offset + 2 > mem->size) {
  26. invalid_source(mem, "Invalid offset");
  27. return 0;
  28. } else {
  29. return be16_to_cpu(*(__be16*)(mem->buf + offset));
  30. }
  31. }
  32. #endif
  33. static void write_u8(size_t offset, uint8_t value)
  34. {
  35. *(uint8_t*)(buf + offset) = value;
  36. }
  37. int main()
  38. {
  39. qcms_profile_release(qcms_profile_sRGB());
  40. buf = calloc(1500, 1);
  41. assert(!qcms_profile_from_memory(buf, 1500));
  42. // invalid size
  43. write_u32(0, 2500);
  44. assert(!qcms_profile_from_memory(buf, 1500));
  45. // proper size
  46. write_u32(0, 1500);
  47. assert(!qcms_profile_from_memory(buf, 1500));
  48. #define INPUT_DEVICE_PROFILE 0x73636e72 // 'scnr'
  49. write_u32(12, INPUT_DEVICE_PROFILE);
  50. assert(!qcms_profile_from_memory(buf, 1500));
  51. write_u8(8, 0x3); // invalid major revision
  52. assert(!qcms_profile_from_memory(buf, 1500));
  53. write_u8(8, 0x2); // major revision
  54. write_u8(9, 0x55); // invalid minor revision
  55. assert(!qcms_profile_from_memory(buf, 1500));
  56. write_u8(8, 0x2); // major revision
  57. write_u8(9, 0x40); // minor revision
  58. write_u8(10, 1); // reserved 1
  59. write_u8(11, 0); // reserved 2
  60. assert(!qcms_profile_from_memory(buf, 1500));
  61. write_u8(10, 0); // reserved 1
  62. write_u8(64, 0x32); // invalid rendering intent
  63. assert(!qcms_profile_from_memory(buf, 1500));
  64. write_u8(64, 0); // invalid rendering intent
  65. #define RGB_SIGNATURE 0x52474220
  66. #define GRAY_SIGNATURE 0x47524159
  67. write_u32(16, RGB_SIGNATURE);
  68. assert(!qcms_profile_from_memory(buf, 1500));
  69. write_u32(128, 15000); // tag count
  70. assert(!qcms_profile_from_memory(buf, 1500));
  71. write_u32(128, 15); // tag count
  72. assert(!qcms_profile_from_memory(buf, 1500));
  73. #define TAG_bXYZ 0x6258595a
  74. #define TAG_gXYZ 0x6758595a
  75. #define TAG_rXYZ 0x7258595a
  76. #define TAG_rTRC 0x72545243
  77. #define TAG_bTRC 0x62545243
  78. #define TAG_gTRC 0x67545243
  79. #define TAG_kTRC 0x6b545243
  80. #define TAG_A2B0 0x41324230
  81. write_u32(128 + 4, TAG_rXYZ); // tag
  82. assert(!qcms_profile_from_memory(buf, 1500));
  83. write_u32(128 + 4 + 4, 1000); // offset
  84. assert(!qcms_profile_from_memory(buf, 1500));
  85. write_u32(128 + 4 + 4*1*3, TAG_gXYZ); // tag
  86. assert(!qcms_profile_from_memory(buf, 1500));
  87. write_u32(128 + 4 + 4*1*3 + 4, 1000); // offset
  88. assert(!qcms_profile_from_memory(buf, 1500));
  89. write_u32(128 + 4 + 4*2*3, TAG_bXYZ); // tag
  90. assert(!qcms_profile_from_memory(buf, 1500));
  91. write_u32(128 + 4 + 4*2*3 + 4, 1000); // offset
  92. assert(!qcms_profile_from_memory(buf, 1500));
  93. #define XYZ_TYPE 0x58595a20 // 'XYZ '
  94. #define CURVE_TYPE 0x63757276 // 'curv'
  95. #define LUT16_TYPE 0x6d667432 // 'mft2'
  96. #define LUT8_TYPE 0x6d667431 // 'mft1'
  97. write_u32(1000, XYZ_TYPE);
  98. assert(!qcms_profile_from_memory(buf, 1500));
  99. write_u32(128 + 4 + 4*3*3, TAG_rTRC); // tag
  100. assert(!qcms_profile_from_memory(buf, 1500));
  101. write_u32(128 + 4 + 4*3*3 + 4, 1100); // offset
  102. assert(!qcms_profile_from_memory(buf, 1500));
  103. write_u32(128 + 4 + 4*4*3, TAG_gTRC); // tag
  104. assert(!qcms_profile_from_memory(buf, 1500));
  105. write_u32(128 + 4 + 4*4*3 + 4, 1100); // offset
  106. assert(!qcms_profile_from_memory(buf, 1500));
  107. write_u32(128 + 4 + 4*5*3, TAG_bTRC); // tag
  108. assert(!qcms_profile_from_memory(buf, 1500));
  109. write_u32(128 + 4 + 4*5*3 + 4, 1100); // offset
  110. assert(!qcms_profile_from_memory(buf, 1500));
  111. write_u32(1100, CURVE_TYPE);
  112. qcms_profile_release(qcms_profile_from_memory(buf, 1500));
  113. write_u32(1108, 100000); // curve count
  114. assert(!qcms_profile_from_memory(buf, 1500));
  115. write_u32(1108, 1); // curve count
  116. qcms_profile_release(qcms_profile_from_memory(buf, 1500));
  117. /* test out gray profiles */
  118. write_u32(16, GRAY_SIGNATURE);
  119. assert(!qcms_profile_from_memory(buf, 1500));
  120. write_u32(128 + 4 + 4*6*3, TAG_kTRC); // tag
  121. assert(!qcms_profile_from_memory(buf, 1500));
  122. write_u32(128 + 4 + 4*6*3 + 4, 1100); // offset
  123. qcms_profile_release(qcms_profile_from_memory(buf, 1500));
  124. /* test out profiles that are the wrong size */
  125. qcms_profile_from_path("sample-trunc.icc");
  126. return 0;
  127. }