Advertisement
cd62131

Check packed data

Mar 13th, 2017
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.78 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stddef.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5.  
  6. typedef struct {
  7.   uint8_t number, category, version, data;
  8. } value_t;
  9.  
  10. static void test0(uint32_t *, uint32_t *, size_t, size_t);
  11. static void set_value(value_t *, uint32_t);
  12. static bool check_number_lt(value_t *, value_t *);
  13. static bool check_number(value_t *, value_t *);
  14. static bool check_category(value_t *, value_t *);
  15. static bool check_version(value_t *, value_t *);
  16. static void test1(uint32_t *, uint32_t *, size_t, size_t);
  17.  
  18. int main(void) {
  19.   uint32_t a[] = { 0x11223344, };
  20.   uint32_t b[] = { 0x10000000, 0x11000000, 0x11023344, 0x11223344, 0x12000000, };
  21.   test0(a, b, sizeof(a) / sizeof(uint32_t), sizeof(b) / sizeof(uint32_t));
  22.   test1(a, b, sizeof(a) / sizeof(uint32_t), sizeof(b) / sizeof(uint32_t));
  23. }
  24.  
  25. static void test0(uint32_t *a, uint32_t *b, size_t a_size, size_t b_size) {
  26.   for (size_t i = 0; i < a_size; ++i) {
  27.     value_t va; set_value(&va, a[i]);
  28.     for (size_t j = 0; j < b_size; ++j) {
  29.       value_t vb; set_value(&vb, b[j]);
  30.       if (check_number_lt(&va, &vb)) {
  31.         break;
  32.       }
  33.       if (check_number(&va, &vb)) {
  34.         printf("[%d] A=%x, B=%x\n", va.number, va.data, vb.data);
  35.       }
  36.     }
  37.   }
  38. }
  39.  
  40. static void set_value(value_t *v, uint32_t value) {
  41.   v->number = (value >> 24) & 0xff;
  42.   v->category = (value >> 16) & 0xff;
  43.   v->version = (value >> 8) & 0xff;
  44.   v->data = value & 0xff;
  45. }
  46.  
  47. static bool check_number(value_t *va, value_t *vb) {
  48.   if (va->number != vb->number) {
  49.     return false;
  50.   }
  51.   return check_category(va, vb);
  52. }
  53.  
  54. static bool check_number_lt(value_t *va, value_t *vb) {
  55.   return (va->number < vb->number) ? true : false;
  56. }
  57.  
  58. static bool check_category(value_t *va, value_t *vb) {
  59.   if (vb->category != 1 && vb->category != 2) {
  60.     return false;
  61.   }
  62.   return check_version(va, vb);
  63. }
  64.  
  65. static bool check_version(value_t *va, value_t *vb) {
  66.   return (va->version == vb->version) ? true : false;
  67. }
  68.  
  69. #define MASK_NUMBER (0xff000000)
  70. #define MASK_CATEGORY (0xff0000)
  71. #define MASK_CATEGORY1 (0x10000)
  72. #define MASK_CATEGORY2 (0x20000)
  73. #define MASK_VERSION (0xff00)
  74. #define MASK_DATA (0xff)
  75. static void test1(uint32_t *a, uint32_t *b, size_t a_size, size_t b_size) {
  76.   for (size_t i = 0; i < a_size; ++i) {
  77.     for (size_t j = 0; j < b_size; ++j) {
  78.       if (
  79.           !((a[i] & MASK_NUMBER) ^ (b[j] & MASK_NUMBER)) &&
  80.           (
  81.            !((b[j] & MASK_CATEGORY) ^ MASK_CATEGORY1) ||
  82.            !((b[j] & MASK_CATEGORY) ^ MASK_CATEGORY2)
  83.           ) &&
  84.           !((a[i] & MASK_VERSION) ^ (b[j] & MASK_VERSION))
  85.          ) {
  86.         printf(
  87.             "[%d] A=%x, B=%x\n",
  88.             (a[i] & MASK_NUMBER) >> 24,
  89.             a[i] & MASK_DATA,
  90.             b[j] & MASK_DATA
  91.             );
  92.       }
  93.     }
  94.   }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement