tables.h (3981B)
1 STATIC uint64_t read_unaligned_u64(const char [static sizeof(uint64_t)]); 2 STATIC void write_unaligned_u64(char [static sizeof(uint64_t)], uint64_t); 3 STATIC int64_t readtableinfo(size_t n, const char [n], tableinfo_t [static 1]); 4 STATIC int64_t readtableinfo_n( 5 size_t n, const char [n], uint8_t, tableinfo_t [static 1]); 6 STATIC int64_t writetableinfo( 7 const tableinfo_t [static 1], size_t n, char [n]); 8 9 STATIC uint64_t 10 read_unaligned_u64(const char buf[static sizeof(uint64_t)]) 11 { 12 uint64_t ret; 13 14 memcpy(&ret, buf, sizeof(uint64_t)); 15 16 return ret; 17 } 18 19 STATIC void 20 write_unaligned_u64(char buf[static sizeof(uint64_t)], uint64_t x) 21 { 22 memcpy(buf, &x, sizeof(uint64_t)); 23 } 24 25 STATIC int64_t 26 readtableinfo( 27 size_t buf_size, 28 const char buf[buf_size], 29 tableinfo_t info[static 1] 30 ) 31 { 32 size_t i; 33 34 if (buf == NULL) { 35 LOG("Error reading table: buffer is NULL\n"); 36 return NISSY_ERROR_NULL_POINTER; 37 } 38 39 if (buf_size < INFOSIZE) { 40 LOG("Error reading table: buffer size is too small " 41 "(given size %" PRIu64 " is smaller than INFOSIZE = %" 42 PRId64 ")\n", buf_size, INFOSIZE); 43 return NISSY_ERROR_BUFFER_SIZE; 44 } 45 46 for (i = 0; i < INFO_DISTRIBUTION_LEN; i++) 47 info->distribution[i] = read_unaligned_u64(OFFSET(buf, 48 INFO_OFFSET_DISTRIBUTION + i * sizeof(uint64_t))); 49 50 info->type = read_unaligned_u64(OFFSET(buf, INFO_OFFSET_TYPE)); 51 info->infosize = read_unaligned_u64(OFFSET(buf, INFO_OFFSET_INFOSIZE)); 52 info->fullsize = read_unaligned_u64(OFFSET(buf, INFO_OFFSET_FULLSIZE)); 53 info->hash = read_unaligned_u64(OFFSET(buf, INFO_OFFSET_HASH)); 54 info->entries = read_unaligned_u64(OFFSET(buf, INFO_OFFSET_ENTRIES)); 55 info->classes = read_unaligned_u64(OFFSET(buf, INFO_OFFSET_CLASSES)); 56 info->next = read_unaligned_u64(OFFSET(buf, INFO_OFFSET_NEXT)); 57 58 memcpy(info->solver, OFFSET(buf, INFO_OFFSET_SOLVER), 59 INFO_SOLVER_STRLEN); 60 61 info->h48h = *(uint8_t *)OFFSET(buf, INFO_OFFSET_H48H); 62 info->bits = *(uint8_t *)OFFSET(buf, INFO_OFFSET_BITS); 63 info->base = *(uint8_t *)OFFSET(buf, INFO_OFFSET_BASE); 64 info->maxvalue = *(uint8_t *)OFFSET(buf, INFO_OFFSET_MAXVALUE); 65 66 return NISSY_OK; 67 } 68 69 STATIC int64_t 70 readtableinfo_n( 71 size_t buf_size, 72 const char buf[buf_size], 73 uint8_t n, 74 tableinfo_t info[static 1] 75 ) 76 { 77 int64_t ret; 78 79 for (; n > 0; n--, buf = buf + info->next, buf_size -= info->next) 80 if ((ret = readtableinfo(buf_size, buf, info)) != 0) 81 return ret; 82 83 return NISSY_OK; 84 } 85 86 STATIC int64_t 87 writetableinfo( 88 const tableinfo_t info[static 1], 89 size_t data_size, 90 char buf[data_size] 91 ) 92 { 93 size_t i; 94 bool end; 95 char *c; 96 97 if (data_size < info->fullsize) { 98 LOG("Error writing table: buffer size is too small " 99 "(given %" PRId64 " but table requires %" PRId64 ")\n", 100 data_size, info->fullsize); 101 return NISSY_ERROR_BUFFER_SIZE; 102 } 103 104 for (i = 0; i < INFO_DISTRIBUTION_LEN; i++) 105 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_DISTRIBUTION + 106 i * sizeof(uint64_t)), info->distribution[i]); 107 108 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_TYPE), info->type); 109 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_INFOSIZE), info->infosize); 110 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_FULLSIZE), info->fullsize); 111 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_HASH), info->hash); 112 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_ENTRIES), info->entries); 113 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_CLASSES), info->classes); 114 write_unaligned_u64(OFFSET(buf, INFO_OFFSET_NEXT), info->next); 115 116 memcpy(OFFSET(buf, INFO_OFFSET_SOLVER), info->solver, 117 INFO_SOLVER_STRLEN); 118 119 /* Zeroing all chars after the end of the string, for consistency */ 120 end = false; 121 for (i = 0; i < INFO_SOLVER_STRLEN; i++) { 122 c = OFFSET(buf, INFO_OFFSET_SOLVER + i); 123 end = end || *c == 0; 124 if (end) 125 *c = 0; 126 } 127 128 *(uint8_t *)OFFSET(buf, INFO_OFFSET_H48H) = info->h48h; 129 *(uint8_t *)OFFSET(buf, INFO_OFFSET_BITS) = info->bits; 130 *(uint8_t *)OFFSET(buf, INFO_OFFSET_BASE) = info->base; 131 *(uint8_t *)OFFSET(buf, INFO_OFFSET_MAXVALUE) = info->maxvalue; 132 133 return NISSY_OK; 134 }