📄 struct-layout-1_generate.c
字号:
m &= (1ULL << e->len) - 1; l1 &= m; l2 &= m; fprintf (outfile, "%lluU%s,%lluU%s", l1, l1 > 4294967295U ? "LL" : "", l2, l2 > 4294967295U ? "LL" : ""); break; case TYPE_FLOAT: l1 &= 0xffffff; l2 &= 0xffffff; signs = generate_random () & 3; fprintf (outfile, "%s%f,%s%f", (signs & 1) ? "-" : "", ((double) l1) / 64, (signs & 2) ? "-" : "", ((double) l2) / 64); break; case TYPE_CINT: signs = generate_random () & 3; l1 &= e->type->maxval; l2 &= e->type->maxval; fprintf (outfile, "CINT(%s%llu%s,%s%llu%s),", (signs & 1) ? "-" : "", l1, l1 > 2147483647 ? "LL" : "", (signs & 2) ? "-" : "", l2, l2 > 2147483647 ? "LL" : ""); signs = generate_random () & 3; l1 = getrandll (); l2 = getrandll (); l1 &= e->type->maxval; l2 &= e->type->maxval; fprintf (outfile, "CINT(%s%llu%s,%s%llu%s)", (signs & 1) ? "-" : "", l1, l1 > 2147483647 ? "LL" : "", (signs & 2) ? "-" : "", l2, l2 > 2147483647 ? "LL" : ""); break; case TYPE_CUINT: l1 &= e->type->maxval; l2 &= e->type->maxval; fprintf (outfile, "CINT(%lluU%s,%lluU%s),", l1, l1 > 4294967295U ? "LL" : "", l2, l2 > 4294967295U ? "LL" : ""); l1 = getrandll (); l2 = getrandll (); l1 &= e->type->maxval; l2 &= e->type->maxval; fprintf (outfile, "CINT(%lluU%s,%lluU%s)", l1, l1 > 4294967295U ? "LL" : "", l2, l2 > 4294967295U ? "LL" : ""); break; case TYPE_CFLOAT: l1 &= 0xffffff; l2 &= 0xffffff; signs = generate_random () & 3; fprintf (outfile, "CDBL(%s%f,%s%f),", (signs & 1) ? "-" : "", ((double) l1) / 64, (signs & 2) ? "-" : "", ((double) l2) / 64); l1 = getrandll (); l2 = getrandll (); l1 &= 0xffffff; l2 &= 0xffffff; signs = generate_random () & 3; fprintf (outfile, "CDBL(%s%f,%s%f)", (signs & 1) ? "-" : "", ((double) l1) / 64, (signs & 2) ? "-" : "", ((double) l2) / 64); break; case TYPE_UENUM: if (e->type->maxval == 0) fputs ("e0_0,e0_0", outfile); else if (e->type->maxval == 1) fprintf (outfile, "e1_%lld,e1_%lld", l1 & 1, l2 & 1); else { p = strchr (e->type->name, '\0'); while (--p >= e->type->name && *p >= '0' && *p <= '9'); p++; l1 %= 7; l2 %= 7; if (l1 > 3) l1 += e->type->maxval - 6; if (l2 > 3) l2 += e->type->maxval - 6; fprintf (outfile, "e%s_%lld,e%s_%lld", p, l1, p, l2); } break; case TYPE_SENUM: p = strchr (e->type->name, '\0'); while (--p >= e->type->name && *p >= '0' && *p <= '9'); p++; l1 %= 7; l2 %= 7; fprintf (outfile, "e%s_%s%lld,e%s_%s%lld", p, l1 < 3 ? "m" : "", l1 == 3 ? 0LL : e->type->maxval - (l1 & 3), p, l2 < 3 ? "m" : "", l2 == 3 ? 0LL : e->type->maxval - (l2 & 3)); break; case TYPE_PTR: l1 %= 256; l2 %= 256; fprintf (outfile, "(%s)&intarray[%lld],(%s)&intarray[%lld]", e->type->name, l1, e->type->name, l2); break; case TYPE_FNPTR: l1 %= 10; l2 %= 10; fprintf (outfile, "fn%lld,fn%lld", l1, l2); break; default: abort (); } fputs (")", outfile);}intsubvalues (struct entry *e, char *p, char *letter){ int i, j; char *q; if (p >= namebuf + sizeof (namebuf) - 32) abort (); p[0] = *letter; p[1] = '\0'; q = p + 1; switch (e[0].etype) { case ETYPE_STRUCT_ARRAY: case ETYPE_UNION_ARRAY: if (e[0].arr_len == 0 || e[0].arr_len == 255) { *letter += 1 + e[0].len; return 1 + e[0].len; } i = generate_random () % e[0].arr_len; snprintf (p, sizeof (namebuf) - (p - namebuf) - 1, "%c[%d]", *letter, i); q = strchr (p, '\0'); /* FALLTHROUGH */ case ETYPE_STRUCT: case ETYPE_UNION: *q++ = '.'; ++*letter; for (i = 1; i <= e[0].len; ) { i += subvalues (e + i, q, letter); if (e[0].etype == ETYPE_UNION || e[0].etype == ETYPE_UNION_ARRAY) { *letter += e[0].len - i + 1; break; } } return 1 + e[0].len; case ETYPE_TYPE: ++*letter; output_FNB ('F', e); return 1; case ETYPE_ARRAY: if (e[0].arr_len == 0 || e[0].arr_len == 255) { ++*letter; return 1; } i = generate_random () % e[0].arr_len; snprintf (p, sizeof (namebuf) - (p - namebuf), "%c[%d]", *letter, i); output_FNB ('F', e); if ((generate_random () & 7) == 0) { j = generate_random () % e[0].arr_len; if (i != j) { snprintf (p, sizeof (namebuf) - (p - namebuf), "%c[%d]", *letter, j); output_FNB ('F', e); } } ++*letter; return 1; case ETYPE_BITFLD: ++*letter; if (e[0].len != 0) output_FNB ('B', e); return 1; }}/* DERIVED FROM:--------------------------------------------------------------------lookup2.c, by Bob Jenkins, December 1996, Public Domain.hash(), hash2(), hash3, and mix() are externally useful functions.Routines to test the hash are included if SELF_TEST is defined.You can use this free for any purpose. It has no warranty.--------------------------------------------------------------------*//*--------------------------------------------------------------------mix -- mix 3 32-bit values reversibly.For every delta with one or two bit set, and the deltas of all three high bits or all three low bits, whether the original value of a,b,c is almost all zero or is uniformly distributed,* If mix() is run forward or backward, at least 32 bits in a,b,c have at least 1/4 probability of changing.* If mix() is run forward, every bit of c will change between 1/3 and 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)mix() was built out of 36 single-cycle latency instructions in a structure that could supported 2x parallelism, like so: a -= b; a -= c; x = (c>>13); b -= c; a ^= x; b -= a; x = (a<<8); c -= a; b ^= x; c -= b; x = (b>>13); ... Unfortunately, superscalar Pentiums and Sparcs can't take advantage of that parallelism. They've also turned some of those single-cycle latency instructions into multi-cycle latency instructions. Still, this is the fastest good hash I could find. There were about 2^^68 to choose from. I only looked at a billion or so.--------------------------------------------------------------------*//* same, but slower, works on systems that might have 8 byte hashval_t's */#define mix(a,b,c) \{ \ a -= b; a -= c; a ^= (c>>13); \ b -= c; b -= a; b ^= (a<< 8); \ c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \ a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \ b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \ c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \ a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \ b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \ c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \}/*--------------------------------------------------------------------hash() -- hash a variable-length key into a 32-bit value k : the key (the unaligned variable-length array of bytes) len : the length of the key, counting by bytes level : can be any 4-byte valueReturns a 32-bit value. Every bit of the key affects every bit ofthe return value. Every 1-bit and 2-bit delta achieves avalanche.About 36+6len instructions.The best hash table sizes are powers of 2. There is no need to domod a prime (mod is sooo slow!). If you need less than 32 bits,use a bitmask. For example, if you need only 10 bits, do h = (h & hashmask(10));In which case, the hash table should have hashsize(10) elements.If you are hashing n strings (ub1 **)k, do it like this: for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use thiscode any way you wish, private, educational, or commercial. It's free.See http://burtleburtle.net/bob/hash/evahash.htmlUse for hash table lookup, or anything where one collision in 2^32 isacceptable. Do NOT use for cryptographic purposes.--------------------------------------------------------------------*/static hashval_titerative_hash (const void *k_in /* the key */, register size_t length /* the length of the key */, register hashval_t initval /* the previous hash, or an arbitrary value */){ register const unsigned char *k = (const unsigned char *)k_in; register hashval_t a,b,c,len; /* Set up the internal state */ len = length; a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */ c = initval; /* the previous hash value */ /*---------------------------------------- handle most of the key */ while (len >= 12) { a += (k[0] +((hashval_t)k[1]<<8) +((hashval_t)k[2]<<16) +((hashval_t)k[3]<<24)); b += (k[4] +((hashval_t)k[5]<<8) +((hashval_t)k[6]<<16) +((hashval_t)k[7]<<24)); c += (k[8] +((hashval_t)k[9]<<8) +((hashval_t)k[10]<<16)+((hashval_t)k[11]<<24)); mix(a,b,c); k += 12; len -= 12; } /*------------------------------------- handle the last 11 bytes */ c += length; switch(len) /* all the case statements fall through */ { case 11: c+=((hashval_t)k[10]<<24); case 10: c+=((hashval_t)k[9]<<16); case 9 : c+=((hashval_t)k[8]<<8); /* the first byte of c is reserved for the length */ case 8 : b+=((hashval_t)k[7]<<24); case 7 : b+=((hashval_t)k[6]<<16); case 6 : b+=((hashval_t)k[5]<<8); case 5 : b+=k[4]; case 4 : a+=((hashval_t)k[3]<<24); case 3 : a+=((hashval_t)k[2]<<16); case 2 : a+=((hashval_t)k[1]<<8); case 1 : a+=k[0]; /* case 0: nothing left to add */ } mix(a,b,c); /*-------------------------------------------- report the result */ return c;}hashval_te_hash (const void *a){ const struct entry *e = a; hashval_t ret = 0; int i; if (e[0].etype != ETYPE_STRUCT && e[0].etype != ETYPE_UNION) abort (); for (i = 0; i <= e[0].len; ++i) { int attriblen; ret = iterative_hash (&e[i], offsetof (struct entry, attrib), ret); attriblen = e[i].attrib ? strlen (e[i].attrib) : -1; ret = iterative_hash (&attriblen, sizeof (int), ret); if (e[i].attrib) ret = iterative_hash (e[i].attrib, attriblen, ret); } return ret;}inte_eq (const void *a, const void *b){ const struct entry *ea = a, *eb = b; int i; if (ea[0].etype != ETYPE_STRUCT && ea[0].etype != ETYPE_UNION) abort (); if (ea[0].len != eb[0].len) return 0; for (i = 0; i <= ea[0].len; ++i) { if (ea[i].etype != eb[i].etype || ea[i].len != eb[i].len || ea[i].arr_len != eb[i].arr_len || ea[i].type != eb[i].type) return 0; if ((ea[i].attrib == NULL) ^ (eb[i].attrib == NULL)) return 0; if (ea[i].attrib && strcmp (ea[i].attrib, eb[i].attrib) != 0) return 0; } return 1;}static int e_exists (const struct entry *e) { struct entry *h; hashval_t hval; hval = e_hash (e); for (h = hash_table[hval % HASH_SIZE]; h; h = h->next) if (e_eq (e, h)) return 1; return 0;}static voide_insert (struct entry *e){ hashval_t hval; hval = e_hash (e); e->next = hash_table[hval % HASH_SIZE]; hash_table[hval % HASH_SIZE] = e;}voidoutput (struct entry *e){ int i; char c; struct entry *n; const char *skip_cint = ""; if (e[0].etype != ETYPE_STRUCT && e[0].etype != ETYPE_UNION) abort (); if (e_exists (e)) return; n = (struct entry *) malloc ((e[0].len + 1) * sizeof (struct entry)); memcpy (n, e, (e[0].len + 1) * sizeof (struct entry)); e_insert (n); if (idx == limidx) switchfiles (e[0].len); for (i = 1; i <= e[0].len; ++i) if ((e[i].etype == ETYPE_TYPE || e[i].etype == ETYPE_ARRAY) && (e[i].type->type == TYPE_CINT || e[i].type->type == TYPE_CUINT)) break; if (i <= e[0].len) skip_cint = "CI"; if (e[0].attrib) fprintf (outfile, (generate_random () & 1) ? "TX%s(%d,%s %s,," : "TX%s(%d,%s,%s,", skip_cint, idx, e[0].etype == ETYPE_STRUCT ? "struct" : "union", e[0].attrib); else if (e[0].etype == ETYPE_STRUCT) fprintf (outfile, "T%s(%d,", skip_cint, idx); else fprintf (outfile, "U%s(%d,", skip_cint, idx); c = 'a'; for (i = 1; i <= e[0].len; ) i += subfield (e + i, &c); fputs (",", outfile); c = 'a'; for (i = 1; i <= e[0].len; ) { i += subvalues (e + i, namebuf, &c); if (e[0].etype == ETYPE_UNION) break; } fputs (")\n", outfile); if (output_one && idx == limidx) exit (0); ++idx;}enum FEATURE{ FEATURE_VECTOR = 1, FEATURE_COMPLEX = 2, FEATURE_ALIGNEDPACKED = 4, FEATURE_ZEROARRAY = 8, FEATURE_ZEROBITFLD = 16, ALL_FEATURES = FEATURE_COMPLEX | FEATURE_VECTOR | FEATURE_ZEROARRAY | FEATURE_ALIGNEDPACKED | FEATURE_ZEROBITFLD};voidsingles (enum FEATURE features){ struct entry e[2]; int i; memset (e, 0, sizeof (e)); e[0].etype = ETYPE_STRUCT; output (e); e[0].etype = ETYPE_UNION; output (e); for (i = 0; i < ((features & FEATURE_ALIGNEDPACKED) ? NATTRIBS2 : NATTRIBS1); ++i) { e[0].attrib = attributes[i]; e[0].etype = ETYPE_STRUCT; output (e); e[0].etype = ETYPE_UNION; output (e); } e[0].len = 1; e[0].attrib = NULL; for (i = 0; i < NTYPES2; ++i) { e[0].etype = ETYPE_STRUCT; e[1].etype = ETYPE_TYPE; e[1].type = &base_types[i]; output (e); e[0].etype = ETYPE_UNION; output (e); } if (features & FEATURE_COMPLEX) for (i = 0; i < NCTYPES2; ++i) { e[0].etype = ETYPE_STRUCT; e[1].etype = ETYPE_TYPE; e[1].type = &complex_types[i]; output (e); e[0].etype = ETYPE_UNION; output (e); } if (features & FEATURE_VECTOR) for (i = 0; i < NVTYPES2; ++i)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -