void rz3_section_header::flip_endianess() { nrecords = byteswap32(nrecords); CompressedBufferSize = byteswap64(CompressedBufferSize); for (int i=0; i<rstzip3::bitarray_count; i++) { rz3_bitarray_counts[i] = byteswap32(rz3_bitarray_counts[i]); } }
WIN32DLL_DEFINE void _mcrypt_encrypt(WAKE_KEY * wake_key, byte * input, int len) { register word32 r3, r4, r5; word32 r6; int i; if (len == 0) return; r3 = wake_key->r[0]; r4 = wake_key->r[1]; r5 = wake_key->r[2]; r6 = wake_key->r[3]; #ifdef USE_IV if (wake_key->started == 0) { wake_key->started = 1; _mcrypt_encrypt(wake_key, (byte *)& wake_key->iv, wake_key->ivsize); } #endif for (i = 0; i < len; i++) { /* R1 = V[n] = V[n] XOR R6 - here we do it per byte --sloooow */ /* R1 is ignored */ input[i] ^= ((byte *)& r6)[counter]; /* R2 = V[n] = R1 - per byte also */ ((byte *)& r2)[counter] = input[i]; counter++; if (counter == 4) { /* r6 was used - update it! */ counter = 0; #ifdef WORDS_BIGENDIAN /* these swaps are because we do operations per byte */ r2 = byteswap32(r2); r6 = byteswap32(r6); #endif r3 = M(r3, r2); r4 = M(r4, r3); r5 = M(r5, r4); r6 = M(r6, r5); #ifdef WORDS_BIGENDIAN r6 = byteswap32(r6); #endif } } wake_key->r[0] = r3; wake_key->r[1] = r4; wake_key->r[2] = r5; wake_key->r[3] = r6; }
WIN32DLL_DEFINE void _mcrypt_decrypt(cast256_key * key, word32 * blk) { word32 t, u; #ifdef WORDS_BIGENDIAN blk[0] = byteswap32(blk[0]); blk[1] = byteswap32(blk[1]); blk[2] = byteswap32(blk[2]); blk[3] = byteswap32(blk[3]); #endif f_rnd(blk, 88); f_rnd(blk, 80); f_rnd(blk, 72); f_rnd(blk, 64); f_rnd(blk, 56); f_rnd(blk, 48); i_rnd(blk, 40); i_rnd(blk, 32); i_rnd(blk, 24); i_rnd(blk, 16); i_rnd(blk, 8); i_rnd(blk, 0); #ifdef WORDS_BIGENDIAN blk[0] = byteswap32(blk[0]); blk[1] = byteswap32(blk[1]); blk[2] = byteswap32(blk[2]); blk[3] = byteswap32(blk[3]); #endif }
static int dealias_ipid32_inseq(scamper_dealias_probe_t **probes, int probec, uint16_t fudge, int bs) { uint32_t a, b, c; int i; /* * do a preliminary check to see if the ipids could be in sequence with * two samples. */ if(probec == 2) { /* if it is a strict sequence check, we don't actually know */ if(fudge == 0) return 1; a = probes[0]->replies[0]->ipid32; b = probes[1]->replies[0]->ipid32; if(bs != 0) { a = byteswap32(a); b = byteswap32(b); } if(dealias_ipid32_inseq2(a, b, fudge) != 0) return 1; return 0; } for(i=0; i+2<probec; i++) { a = probes[i+0]->replies[0]->ipid32; b = probes[i+1]->replies[0]->ipid32; c = probes[i+2]->replies[0]->ipid32; if(bs != 0) { a = byteswap32(a); b = byteswap32(b); c = byteswap32(c); } if(dealias_ipid32_inseq3(a, b, c, fudge) == 0) return 0; } return 1; }
static int dealias_ipid32_bo(scamper_dealias_probe_t **probes, int probec) { scamper_dealias_probe_t **s = NULL; uint32_t a, b, c = 1, max_bs = 0, max_nobs = 0, u32; int i, rc = 2; if((s = memdup(probes, sizeof(scamper_dealias_probe_t *) * probec)) == NULL) return -1; array_qsort((void **)s, probec, (array_cmp_t)dealias_probe_def_cmp); for(i=0; i<probec-1; i++) { if(s[i]->def != s[i+1]->def) { if(c >= 3) { if(max_nobs < max_bs) rc = 0; else if(max_nobs > max_bs) rc = 1; if(rc == 0) goto done; } c = 1; max_nobs = 0; max_bs = 0; } else { a = s[i]->replies[0]->ipid32; b = s[i+1]->replies[0]->ipid32; u32 = dealias_ipid32_diff(a, b); if(u32 > max_nobs || max_nobs == 0) max_nobs = u32; u32 = dealias_ipid32_diff(byteswap32(a), byteswap32(b)); if(u32 > max_bs || max_bs == 0) max_bs = u32; c++; } } done: if(s != NULL) free(s); return rc; }
static void serpent_decrypt(const uint32_t in_blk[4], uint32_t out_blk[4], const uint32_t *l_key) { uint32_t a,b,c,d,e,f,g,h; uint32_t t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16; #ifdef BLOCK_SWAP a = byteswap32(in_blk[3]); b = byteswap32(in_blk[2]); c = byteswap32(in_blk[1]); d = byteswap32(in_blk[0]); #else a = in_blk[0]; b = in_blk[1]; c = in_blk[2]; d = in_blk[3]; #endif k_xor(32,a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor(31,e,f,g,h); irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor(30,a,b,c,d); irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor(29,e,f,g,h); irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor(28,a,b,c,d); irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor(27,e,f,g,h); irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor(26,a,b,c,d); irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor(25,e,f,g,h); irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor(24,a,b,c,d); irot(a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor(23,e,f,g,h); irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor(22,a,b,c,d); irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor(21,e,f,g,h); irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor(20,a,b,c,d); irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor(19,e,f,g,h); irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor(18,a,b,c,d); irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor(17,e,f,g,h); irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor(16,a,b,c,d); irot(a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor(15,e,f,g,h); irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor(14,a,b,c,d); irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor(13,e,f,g,h); irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor(12,a,b,c,d); irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor(11,e,f,g,h); irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor(10,a,b,c,d); irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor( 9,e,f,g,h); irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor( 8,a,b,c,d); irot(a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor( 7,e,f,g,h); irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor( 6,a,b,c,d); irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor( 5,e,f,g,h); irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor( 4,a,b,c,d); irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor( 3,e,f,g,h); irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor( 2,a,b,c,d); irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor( 1,e,f,g,h); irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor( 0,a,b,c,d); #ifdef BLOCK_SWAP out_blk[3] = byteswap32(a); out_blk[2] = byteswap32(b); out_blk[1] = byteswap32(c); out_blk[0] = byteswap32(d); #else out_blk[0] = a; out_blk[1] = b; out_blk[2] = c; out_blk[3] = d; #endif };
WIN32DLL_DEFINE int _mcrypt_set_key(cast256_key * key, const word32 * in_key, const int key_len) { word32 i, j, t, u, cm, cr, lk[8], tm[8], tr[8]; for (i = 0; i < key_len / sizeof(word32); ++i) #ifdef WORDS_BIGENDIAN lk[i] = byteswap32(in_key[i]); #else lk[i] = in_key[i]; #endif for (; i < 8; ++i) lk[i] = 0; cm = 0x5a827999; cr = 19; for (i = 0; i < 96; i += 8) { for (j = 0; j < 8; ++j) { tm[j] = cm; cm += 0x6ed9eba1; tr[j] = cr; cr += 17; } k_rnd(lk, tr, tm); for (j = 0; j < 8; ++j) { tm[j] = cm; cm += 0x6ed9eba1; tr[j] = cr; cr += 17; } k_rnd(lk, tr, tm); key->l_key[i + 0] = lk[0]; key->l_key[i + 1] = lk[2]; key->l_key[i + 2] = lk[4]; key->l_key[i + 3] = lk[6]; key->l_key[i + 4] = lk[7]; key->l_key[i + 5] = lk[5]; key->l_key[i + 6] = lk[3]; key->l_key[i + 7] = lk[1]; } return 0; }
static int write_file_header(state *s) { BITMAPFILEHEADER b; b.bfType = BF_TYPE; b.bfSize = byteswap32((s->image_width * s->image_height * 3) + 54); b.bfReserved1 = 0; // The offset in the file where RGB image data begins b.bfOffBits = byteswap32(0x36); fwrite(&b.bfType,1,2,s->out_handle); fwrite(&b.bfSize,1,4,s->out_handle); // Write both reserved values at once fwrite(&b.bfReserved1,1,2,s->out_handle); fwrite(&b.bfReserved1,1,2,s->out_handle); fwrite(&b.bfOffBits,sizeof(uint32_t),1,s->out_handle); return FALSE; }
/* x should be a 64 bit integer */ WIN32DLL_DEFINE void _mcrypt_encrypt(blf_ctx * c, word32 * x) { register word32 Xl; register word32 Xr, temp; short i; #ifdef WORDS_BIGENDIAN Xl = x[0]; Xr = x[1]; #else Xl = byteswap32(x[0]); Xr = byteswap32(x[1]); #endif for (i = 0; i < BF_N; ++i) { Xl ^= c->P[i]; Xr ^= F(c, Xl); temp = Xl; Xl = Xr; Xr = temp; } temp = Xl; Xl = Xr; Xr = temp; Xr ^= c->P[BF_N]; Xl ^= c->P[BF_N + 1]; #ifdef WORDS_BIGENDIAN x[0] = Xl; x[1] = Xr; #else x[0] = byteswap32(Xl); x[1] = byteswap32(Xr); #endif }
void GetBoneStructureRoots(RootNode *rootNodes, uint32_t count, BoneStructureRoot *boneStructureRoots, FILE *datFile) { assert(datFile != NULL); for (int i = 0; i < count; ++i) { RootNode *rootNode = &rootNodes[i]; fseek(datFile, rootNode->dataOffset+DATA_BLOCK, SEEK_SET); BoneStructureRoot *boneStructureRoot = &boneStructureRoots[i]; fread(&boneStructureRoot->stringOffset, sizeof(boneStructureRoot->stringOffset), 1, datFile); fread(&boneStructureRoot->unknownFlags, sizeof(boneStructureRoot->unknownFlags), 1, datFile); fread(&boneStructureRoot->childBoneStructOffset, sizeof(boneStructureRoot->childBoneStructOffset), 1, datFile); fread(&boneStructureRoot->nextBoneStructOffset, sizeof(boneStructureRoot->nextBoneStructOffset), 1, datFile); fread(&boneStructureRoot->objectStructOffset, sizeof(boneStructureRoot->objectStructOffset), 1, datFile); fread(&boneStructureRoot->rotation_x, sizeof(boneStructureRoot->rotation_x), 1, datFile); fread(&boneStructureRoot->rotation_y, sizeof(boneStructureRoot->rotation_y), 1, datFile); fread(&boneStructureRoot->rotation_z, sizeof(boneStructureRoot->rotation_z), 1, datFile); fread(&boneStructureRoot->scale_x, sizeof(boneStructureRoot->scale_x), 1, datFile); fread(&boneStructureRoot->scale_y, sizeof(boneStructureRoot->scale_y), 1, datFile); fread(&boneStructureRoot->scale_z, sizeof(boneStructureRoot->scale_z), 1, datFile); fread(&boneStructureRoot->location_x, sizeof(boneStructureRoot->location_x), 1, datFile); fread(&boneStructureRoot->location_y, sizeof(boneStructureRoot->location_y), 1, datFile); fread(&boneStructureRoot->location_z, sizeof(boneStructureRoot->location_z), 1, datFile); fread(&boneStructureRoot->inverseBindMatrixOffset, sizeof(boneStructureRoot->inverseBindMatrixOffset), 1, datFile); fread(&boneStructureRoot->pointerUnknown, sizeof(boneStructureRoot->pointerUnknown), 1, datFile); boneStructureRoot->stringOffset=byteswap32(boneStructureRoot->stringOffset); boneStructureRoot->unknownFlags=byteswap32(boneStructureRoot->unknownFlags); boneStructureRoot->childBoneStructOffset=byteswap32(boneStructureRoot->childBoneStructOffset); boneStructureRoot->nextBoneStructOffset=byteswap32(boneStructureRoot->nextBoneStructOffset); boneStructureRoot->objectStructOffset=byteswap32(boneStructureRoot->objectStructOffset); float tempFloat; tempFloat = boneStructureRoot->rotation_x; boneStructureRoot->rotation_x = floatSwap(tempFloat); tempFloat = boneStructureRoot->rotation_y; boneStructureRoot->rotation_y = floatSwap(tempFloat); tempFloat = boneStructureRoot->rotation_z; boneStructureRoot->rotation_z = floatSwap(tempFloat); tempFloat = boneStructureRoot->scale_x; boneStructureRoot->scale_x = floatSwap(tempFloat); tempFloat = boneStructureRoot->scale_y; boneStructureRoot->scale_y = floatSwap(tempFloat); tempFloat = boneStructureRoot->scale_z; boneStructureRoot->scale_z = floatSwap(tempFloat); tempFloat = boneStructureRoot->location_x; boneStructureRoot->location_x = floatSwap(tempFloat); tempFloat = boneStructureRoot->location_y; boneStructureRoot->location_y = floatSwap(tempFloat); tempFloat = boneStructureRoot->location_z; boneStructureRoot->location_z = floatSwap(tempFloat); boneStructureRoot->inverseBindMatrixOffset=byteswap32(boneStructureRoot->inverseBindMatrixOffset); boneStructureRoot->pointerUnknown=byteswap32(boneStructureRoot->pointerUnknown); } }
static inline uint32_t swapl(libtrace_t *libtrace, uint32_t num) { /* To deal with open_dead traces that might try and use this * if we don't have any per trace data, assume host byte order */ if (!DATA(libtrace)) return num; /* We can use the PCAP magic number to determine the byte order */ if (header_is_backwards_magic(&(DATA(libtrace)->header))) return byteswap32(num); return num; }
WIN32DLL_DEFINE void _mcrypt_decrypt(blf_ctx * c, word32 * x) { word32 Xl, Xr, temp; short i; #ifndef WORDS_BIGENDIAN Xl = x[0]; Xr = x[1]; #else Xl = byteswap32(x[0]); Xr = byteswap32(x[1]); #endif for (i = BF_N + 1; i > 1; --i) { Xl ^= c->P[i]; Xr ^= F(c, Xl); temp = Xl; Xl = Xr; Xr = temp; } temp = Xl; Xl = Xr; Xr = temp; Xr ^= c->P[1]; Xl ^= c->P[0]; #ifndef WORDS_BIGENDIAN x[0] = Xl; x[1] = Xr; #else x[0] = byteswap32(Xl); x[1] = byteswap32(Xr); #endif }
static int write_file_info(state *s) { BITMAPINFOHEADER b; // Size of the this header information b.biSize = byteswap32(HEADERINFOSIZE); if (s->orientation == ORIENTATION_VERTICAL) { b.biWidth = byteswap32(s->image_width); b.biHeight = byteswap32(s->image_height); } else { b.biWidth = byteswap32(s->image_height); b.biHeight = byteswap32(s->image_width); } b.biPlanes = byteswap16(1); b.biBitCount = byteswap16(24); b.biCompression = BI_RGB; // How much RGB data follows this header b.biSizeImage = byteswap32((s->image_width * s->image_height) * 3); b.biXPelsPerMeter = 0; b.biYPelsPerMeter = 0; // These values denote how many values are in the RGB color map // that follows. b.biClrUsed = 0; b.biClrImportant = 0; if (s->verbose) print_status ("%s: Image will be %"PRIu32" x %"PRIu32" x %"PRIu32, __progname, byteswap32(b.biWidth), byteswap32(b.biHeight), byteswap16(b.biBitCount)); fwrite(&b,HEADERINFOSIZE,1,s->out_handle); return FALSE; }
/** * @brief writes 32-bit value to memory at address * @param mem memory to write to * @param address location to write to * @param value value to store in memory * @return true if success, otherwise false */ bool write32Memory(Memory &mem, U32 address, const U32 &value) { bool retval; // Check alignment if ((address & 0x3) == 0) { if (address < MEMORY_SIZE) { U32 *memptr = (U32*)&mem.data[address]; *memptr = byteswap32(value); retval = true; } else { printf("WRITE ERROR: Address 0x%08x >= 0x%08x\n", address, MEMORY_SIZE); retval = false; } } else { printf("WRITE ERROR: Address 0x%08x is not 32-bit aligned\n", address); retval = false; } return retval; }
WIN32DLL_DEFINE int _mcrypt_set_key(WAKE_KEY * wake_key, word32 * key, int len, word32 * IV, int ivlen) { word32 x, z, p; word32 k[4]; /* the key must be exactly 256 bits */ if (len != 32) return -1; #ifdef WORDS_BIGENDIAN k[0] = byteswap32(key[0]); k[1] = byteswap32(key[1]); k[2] = byteswap32(key[2]); k[3] = byteswap32(key[3]); #else k[0] = key[0]; k[1] = key[1]; k[2] = key[2]; k[3] = key[3]; #endif for (p = 0; p < 4; p++) { wake_key->t[p] = k[p]; } for (p = 4; p < 256; p++) { x = wake_key->t[p - 4] + wake_key->t[p - 1]; wake_key->t[p] = x >> 3 ^ tt[x & 7]; } for (p = 0; p < 23; p++) wake_key->t[p] += wake_key->t[p + 89]; x = wake_key->t[33]; z = wake_key->t[59] | 0x01000001; z &= 0xff7fffff; for (p = 0; p < 256; p++) { x = (x & 0xff7fffff) + z; wake_key->t[p] = (wake_key->t[p] & 0x00ffffff) ^ x; } wake_key->t[256] = wake_key->t[0]; x &= 0xff; for (p = 0; p < 256; p++) { wake_key->t[p] = wake_key->t[x = (wake_key->t[p ^ x] ^ x) & 0xff]; wake_key->t[x] = wake_key->t[p + 1]; } wake_key->counter = 0; wake_key->r[0] = k[0]; wake_key->r[1] = k[1]; wake_key->r[2] = k[2]; #ifdef WORDS_BIGENDIAN wake_key->r[3] = byteswap32(k[3]); #else wake_key->r[3] = k[3]; #endif #ifdef USE_IV wake_key->started = 0; if (ivlen > 32) wake_key->ivsize = 32; else wake_key->ivsize = ivlen / 4 * 4; if (IV == NULL) wake_key->ivsize = 0; if (wake_key->ivsize > 0 && IV != NULL) memcpy(&wake_key->iv, IV, wake_key->ivsize); #endif return 0; }
WIN32DLL_DEFINE void _mcrypt_decrypt(WAKE_KEY * wake_key, byte * input, int len) { register word32 r3, r4, r5; word32 r6; int i; if (len == 0) return; r3 = wake_key->r[0]; r4 = wake_key->r[1]; r5 = wake_key->r[2]; r6 = wake_key->r[3]; #ifdef USE_IV if (wake_key->started == 0) { wake_key->started = 1; _mcrypt_encrypt(wake_key, (byte *)& wake_key->iv, wake_key->ivsize); wake_key->r[0] = r3; wake_key->r[1] = r4; wake_key->r[2] = r5; wake_key->r[3] = r6; _mcrypt_decrypt(wake_key, (byte *)& wake_key->iv, wake_key->ivsize); } #endif for (i = 0; i < len; i++) { /* R1 = V[n] */ ((byte *)& r1)[counter] = input[i]; /* R2 = V[n] = V[n] ^ R6 */ /* R2 is ignored */ input[i] ^= ((byte *)& r6)[counter]; counter++; if (counter == 4) { counter = 0; #ifdef WORDS_BIGENDIAN r1 = byteswap32(r1); r6 = byteswap32(r6); #endif r3 = M(r3, r1); r4 = M(r4, r3); r5 = M(r5, r4); r6 = M(r6, r5); #ifdef WORDS_BIGENDIAN r6 = byteswap32(r6); #endif } } wake_key->r[0] = r3; wake_key->r[1] = r4; wake_key->r[2] = r5; wake_key->r[3] = r6; }
#define static_if(cond) detail::call_if<cond>() << [&] template<class Iter> void reverse(Iter first, Iter last) { std::reverse(first, last); } uint32_t byteswap32(uint32_t& x) { return x; } template<typename T> void reverse(T& x) { static_if(sizeof(T) == sizeof(uint32_t)) { std::cout << "fast lane\n"; *reinterpret_cast<uint32_t*>(&x) = byteswap32(*reinterpret_cast<uint32_t*>(&x)); }; static_if(sizeof(T) != sizeof(uint32_t)) { std::cout << "slow lane\n"; auto f = reinterpret_cast<unsigned char*>(&x); auto l = f + sizeof(T); return reverse(f,l); }; } int main(int argc, char **argv) { char x = 0; reverse(x);
static uint32_t _swap32(uint32_t x) { return byteswap32(x); }