void load_integral_impl(Promoted& l) { const std::size_t size = sizeof(Promoted); char* cptr = reinterpret_cast<char *>(&l); //-V206 load_binary(cptr, static_cast<std::size_t>(size)); #ifdef BOOST_BIG_ENDIAN if (endian_little()) reverse_bytes(size, cptr); #else if (endian_big()) reverse_bytes(size, cptr); #endif }
void save_integral_impl(boost::int64_t l) { const std::size_t size = sizeof(boost::int64_t); char* cptr = reinterpret_cast<char *>(&l); #ifdef BOOST_BIG_ENDIAN if(endian_little()) reverse_bytes(size, cptr); #else if(endian_big()) reverse_bytes(size, cptr); #endif save_binary(cptr, size); }
void load_integral_impl(boost::uint64_t & ul) { const std::size_t size = sizeof(boost::uint64_t); char* cptr = reinterpret_cast<char *>(&ul); load_binary(cptr, static_cast<std::size_t>(size)); #ifdef BOOST_BIG_ENDIAN if (endian_little()) reverse_bytes(size, cptr); #else if (endian_big()) reverse_bytes(size, cptr); #endif }
static int jlink_spi_send_command(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr) { uint32_t length; uint8_t *buffer; length = writecnt + readcnt; if (length > JTAG_MAX_TRANSFER_SIZE) return SPI_INVALID_LENGTH; buffer = malloc(length); if (!buffer) { msg_perr("Memory allocation failed.\n"); return SPI_GENERIC_ERROR; } /* Reverse all bytes because the device transfers data LSB first. */ reverse_bytes(buffer, writearr, writecnt); memset(buffer + writecnt, 0x00, readcnt); if (!assert_cs()) { free(buffer); return SPI_PROGRAMMER_ERROR; } int ret; ret = jaylink_jtag_io(jaylink_devh, buffer, buffer, buffer, length * 8, JAYLINK_JTAG_VERSION_2); if (ret != JAYLINK_OK) { msg_perr("jaylink_jag_io() failed: %s.\n", jaylink_strerror(ret)); free(buffer); return SPI_PROGRAMMER_ERROR; } if (!deassert_cs()) { free(buffer); return SPI_PROGRAMMER_ERROR; } /* Reverse all bytes because the device transfers data LSB first. */ reverse_bytes(readarr, buffer + writecnt, readcnt); free(buffer); return 0; }
size_t ntb_base58_encode(const uint8_t *input, size_t length, char *output) { BIGNUM val; BN_ULONG part; char *p = output; BN_init(&val); if (BN_bin2bn(input, length, &val) == NULL) ntb_fatal("A big number operation failed"); while (!BN_is_zero(&val)) { part = BN_div_word(&val, 58); assert(part >= 0 && part < 58); *(p++) = alphabet[part]; } BN_free(&val); /* Make it big-endian */ reverse_bytes(output, p - output); return p - output; }
void portable_binary_iarchive::load_impl(boost::intmax_t & l, char maxsize) { char size; l = 0; this->primitive_base_t::load(size); if(0 == size) { return; } bool negative = (size < 0); if(negative) size = -size; if(size > maxsize) boost::serialization::throw_exception( portable_binary_iarchive_exception() ); char * cptr = reinterpret_cast<char *>(& l); #ifdef BOOST_BIG_ENDIAN cptr += (sizeof(boost::intmax_t) - size); #endif this->primitive_base_t::load_binary(cptr, size); #ifdef BOOST_BIG_ENDIAN if(m_flags & endian_little) #else if(m_flags & endian_big) #endif reverse_bytes(size, cptr); if(negative) l = -l; }
void handle_params(t_info *info, t_command com, int fd) { t_params p; short temp_short; p.i = 0; while (p.i < com.num_params) { if (com.params[p.i][0] == 'r') { p.temp_char = (char)ft_atoi(&com.params[p.i][1]); write(fd, &p.temp_char, sizeof(char)); } else if (com.params[p.i][0] == DIRECT_CHAR) handle_dir_params(info, com, fd, &p); else { p.temp_int = ft_atoi(com.params[p.i]); temp_short = (short)p.temp_int; reverse_bytes(&temp_short, IND_SIZE); write(fd, &temp_short, IND_SIZE); } p.i++; } }
bool bitcoin_txid_to_hex(const struct sha256_double *txid, char *hexstr, size_t hexstr_len) { struct sha256_double rev = *txid; reverse_bytes(rev.sha.u.u8, sizeof(rev.sha.u.u8)); return hex_encode(&rev, sizeof(rev), hexstr, hexstr_len); }
static bool android_pubkey_encode_bignum(const BIGNUM* num, uint8_t* buffer) { if (!BN_bn2bin_padded(buffer, ANDROID_PUBKEY_MODULUS_SIZE, num)) { return false; } reverse_bytes(buffer, ANDROID_PUBKEY_MODULUS_SIZE); return true; }
bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len, struct sha256_double *txid) { if (!hex_decode(hexstr, hexstr_len, txid, sizeof(*txid))) return false; reverse_bytes(txid->sha.u.u8, sizeof(txid->sha.u.u8)); return true; }
void portable_binary_oarchive::save_impl( const boost::intmax_t l, const char maxsize ){ (void)maxsize; char size = 0; if(l == 0){ this->primitive_base_t::save(size); return; } boost::intmax_t ll; bool negative = l < 0; if(negative) ll = -l; else ll = l; do{ ll >>= CHAR_BIT; ++size; }while(ll != 0); this->primitive_base_t::save( static_cast<char>(negative ? -size : size) ); if(negative) ll = -l; else ll = l; char * cptr = reinterpret_cast<char *>(& ll); #ifdef BOOST_BIG_ENDIAN cptr += (sizeof(boost::intmax_t) - size); if(m_flags & endian_little) reverse_bytes(size, cptr); #else if(m_flags & endian_big) reverse_bytes(size, cptr); #endif this->primitive_base_t::save_binary(cptr, size); }
void tablet_parse_data(tablet_t* t, unsigned char* data, xwrap_t* xw) { int i; static const char* keyseq[] = {"f", "f", "Control_L", "Shift_L"}; uint8_t key_mask; if (data[0] == 0x1) { /* No pen */ if ((data[2] & 0xA0) == 0xA0) { /* Buttons */ if (t->prev_scroll != 0x0) { xdo_mouse_up(xw->xdo, CURRENTWINDOW, 2); /* Map middle mouse button to scroll */ t->prev_scroll = 0x0; } for (i = 0; i < 4; ++i) { key_mask = 0x1 << i; if (t->prev_key_state & key_mask) { /* Key was already pressed */ if ((key_mask & data[2]) == 0x0) { /* Now it is released */ xdo_send_keysequence_window_up(xw->xdo, CURRENTWINDOW, keyseq[i], 0); } } else { /* Key was released */ if (key_mask & data[2]) { /* Now it is pressed */ xdo_send_keysequence_window_down(xw->xdo, CURRENTWINDOW, keyseq[i], 0); } } } t->prev_key_state = data[2]; } else { /* Scroll */ if (t->prev_scroll == 0x0) { xdo_mouse_down(xw->xdo, CURRENTWINDOW, 2); /* Map middle mouse button to scroll */ } t->prev_scroll = data[2]; } } else { /* Pen */ if (data[1] != 0x0) { /* Pen is near the drawing plane */ uint16_t x = reverse_bytes(*(uint16_t*)(data + 2)); uint16_t y = reverse_bytes(*(uint16_t*)(data + 4)); float fx = (float)x / t->horizontal_max; float fy = (float)y / t->vertical_max; xdo_move_mouse(xw->xdo, fx * xw->width, fy * xw->height, 0); check_button(0x01, 1, t->prev_point_state, data[1], xw); /* Left mouse button */ check_button(0x02, 3, t->prev_point_state, data[1], xw); /* Right mouse button */ t->prev_point_state = data[1]; } } }
// Assumes block_header allocation is 1024 bits void process_bit_coin_block_inline(block_header* block, uint32* block_digest) { uint8 digest[64]; prime_final_block((uint8*) block, 2, 0, sizeof(block_header)); sha_256_hash((uint32*) block, 2, (uint32*) digest); prime_final_block(digest, 1, 0, 32); sha_256_hash((uint32*) digest, 1, block_digest); reverse_bytes((unsigned char*) block_digest, 32); }
inline void write (S & stream, T val, std::size_t size, wkbByteOrder byte_order) { #ifdef MAPNIK_BIG_ENDIAN bool need_swap = byte_order ? wkbNDR : wkbXDR; #else bool need_swap = byte_order ? wkbXDR : wkbNDR; #endif char* buf = reinterpret_cast<char*>(&val); if (need_swap) { reverse_bytes(size,buf); } stream.write(buf,size); }
bool android_pubkey_decode(const uint8_t* key_buffer, size_t size, RSA** key) { const RSAPublicKey* key_struct = (RSAPublicKey*)key_buffer; bool ret = false; uint8_t modulus_buffer[ANDROID_PUBKEY_MODULUS_SIZE]; RSA* new_key = RSA_new(); if (!new_key) { goto cleanup; } // Check |size| is large enough and the modulus size is correct. if (size < sizeof(RSAPublicKey)) { goto cleanup; } if (key_struct->modulus_size_words != ANDROID_PUBKEY_MODULUS_SIZE_WORDS) { goto cleanup; } // Convert the modulus to big-endian byte order as expected by BN_bin2bn. memcpy(modulus_buffer, key_struct->modulus, sizeof(modulus_buffer)); reverse_bytes(modulus_buffer, sizeof(modulus_buffer)); new_key->n = BN_bin2bn(modulus_buffer, sizeof(modulus_buffer), NULL); if (!new_key->n) { goto cleanup; } // Read the exponent. new_key->e = BN_new(); if (!new_key->e || !BN_set_word(new_key->e, key_struct->exponent)) { goto cleanup; } // Note that we don't extract the montgomery parameters n0inv and rr from // the RSAPublicKey structure. They assume a word size of 32 bits, but // BoringSSL may use a word size of 64 bits internally, so we're lacking the // top 32 bits of n0inv in general. For now, we just ignore the parameters // and have BoringSSL recompute them internally. More sophisticated logic can // be added here if/when we want the additional speedup from using the // pre-computed montgomery parameters. *key = new_key; ret = true; cleanup: if (!ret && new_key) { RSA_free(new_key); } return ret; }
int main(void) { setup_locale(); struct bitcoin_tx *tx; tx = bitcoin_tx_from_hex(NULL, extended_tx, strlen(extended_tx)); assert(tx); /* Canonical results from Nichola Dorier's * http://n.bitcoin.ninja/checktx * With much thanks! */ assert(tx->wtx->num_inputs == 1); assert(tx->wtx->num_outputs == 1); reverse_bytes(tx->wtx->inputs[0].txhash, sizeof(tx->wtx->inputs[0].txhash)); hexeq(tx->wtx->inputs[0].txhash, sizeof(tx->wtx->inputs[0].txhash), "1db36e1306dfc810ea63f0cf866d475cce4e9261a5e8d1581f0d1dc485f4beb5"); assert(tx->wtx->inputs[0].index == 0); /* This is a p2sh-p2wpkh: */ /* ScriptSig is push of "version 0 + hash of pubkey" */ hexeq(tx->wtx->inputs[0].script, tx->wtx->inputs[0].script_len, "16" "00" "144aa38e396e1394fb45cbf83f48d1464fbc9f498f"); /* Witness with 2 items */ assert(tx->wtx->inputs[0].witness); assert(tx->wtx->inputs[0].witness->num_items == 2); hexeq(tx->wtx->inputs[0].witness->items[0].witness, tx->wtx->inputs[0].witness->items[0].witness_len, "3045022100f2abf9e9cf238c66533af93f23937eae8ac01fb6f105a00ab71dbe" "fb9637dc9502205c1ac745829b3f6889607961f5d817dfa0c8f52bdda12e837c" "4f7b162f6db8a701"); hexeq(tx->wtx->inputs[0].witness->items[1].witness, tx->wtx->inputs[0].witness->items[1].witness_len, "0204096eb817f7efb414ef4d3d8be39dd04374256d3b054a322d4a6ee22736d0" "3b"); tal_free(tx); return 0; }
/* this user-defined function is called after the user program receives a data packet. It reverses the content of the packet and then send the result back to the client */ static int udp_server_dispatch_client (MHANDLE conp, void *private_data, DATAPKT *dpkt) { char buffer[1024]; (void) private_data; dpkt->packet_consumed = dpkt->packet_size; memcpy(buffer, dpkt->packet_data, dpkt->packet_size); buffer[dpkt->packet_size] = 0; printf("RECV: %s\n", buffer); /* reverse the received data */ reverse_bytes(buffer, dpkt->packet_size); buffer[dpkt->packet_size] = 0; printf("SEND: %s\n", buffer); /* send data back to the client */ connection_send_data(conp, buffer, dpkt->packet_size); return 0; }
// this doesn't work yet void mine_bit_coin_block(block_header* block, unsigned int start, unsigned int end) { uint32 mined_block[16]; uint32 mined_block_digest[8]; block_header* mined_header = (block_header*) mined_block; uint8 digest[64]; memcpy(block, mined_block, sizeof(block_header)); prime_final_block((uint8*) mined_block, 2, 0, sizeof(block_header)); prime_final_block(digest, 1, 0, 32); end &= 0x00000000FFFFFFFFUL; for(long i=start; i < end; i++) { mined_header->nonce = (unsigned int) i; sha_256_hash(mined_block, 2, (uint32*) digest); // LAMb: This can be optimized to make one call to hash loop sha_256_hash((uint32*) digest, 1, mined_block_digest); reverse_bytes((unsigned char*) mined_block_digest, 32); printf("nonce=%lu\t", i); print_digest(mined_block_digest, true); } }
inline T reverse_bytes_copy_if(T value, Endian const endian) { if (endian != Endian::system) { reverse_bytes(value); } return value; }
void rev_write(int fd, int *temp, int size) { reverse_bytes(temp, size); write(fd, temp, size); }
int main(int argc, char *argv[]) { image_info *info; int i; BYTE *bufp; BYTE *ppm, *ptr; FILE *fp_src, *fp_dest; char *dest; BYTE invert=0; BYTE qvga = 0; int size; float gamma_factor; BYTE gtable[256]; int b; BYTE *buf; BYTE *buf2; int WIDTH=176, HEIGHT=144; BYTE outdoors; GtkWidget *window; GtkWidget *image; GtkWidget *event_box; int current = 1; int offset_correct = 0; if ( !(argc > 1) ){ printf("Syntax: show_sonix_raw sourcefile, or \n"); printf("Syntax: show_sonix_raw -useoffset sourcefile, or \n"); printf("Syntax: show_sonix_raw -useoffset -invert sourcefile\n"); return 0; } if ((strcmp(argv[current], "-qvga") == 0) && (current+1 < argc)) { qvga = 1; offset_correct=8; current += 1; if (argc != 3) { printf("Syntax: show_sonix_raw -qvga sourcefile\n"); return 0; } } if ( !(argc > 2) && (strcmp(argv[current], "-useoffset") == 0) ) { printf("Syntax: show_sonix_raw sourcefile, or \n"); printf("Syntax: show_sonix_raw -useoffset sourcefile \n"); printf("Syntax: show_sonix_raw -useoffset -invert sourcefile\n"); return 0; } printf("argc=%i\n", argc); if ((strcmp(argv[current], "-useoffset") == 0) && (current+1 < argc)) { offset_correct = 8; current += 1; printf("useoffset option used\n"); printf("current=%i\n", current); if ( !(argc > 2) || !(argc < 5) ){ printf("Syntax: show_sonix_raw sourcefile, or \n"); printf("Syntax: show_sonix_raw -useoffset sourcefile, or \n"); printf("Syntax: show_sonix_raw -useoffset -invert sourcefile, or\n"); printf("Syntax: show_sonix_raw -qvga sourcefile\n"); return 0; } } if ( !(argc > 3) && (strcmp(argv[current], "-invert") == 0) ) { printf("Syntax: show_sonix_raw sourcefile, or \n"); printf("Syntax: show_sonix_raw -useoffset sourcefile, or \n"); printf("Syntax: show_sonix_raw -useoffset -invert sourcefile\n"); return 0; } if ((strcmp(argv[current], "-invert") == 0) && (current+1 < argc)) { invert = 1; current += 1; printf("invert option used\n"); printf("current=%i\n", current); if ( !(argc > 3) || !(argc < 5) ){ printf("Syntax: show_sonix_raw sourcefile, or \n"); printf("Syntax: show_sonix_raw -useoffset sourcefile, or \n"); printf("Syntax: show_sonix_raw -useoffset -invert sourcefile \n"); return 0; } } printf("Options: offset=%i invert=%i\n", offset_correct, invert); fprintf (stderr, "Name of source file is %s\n", argv[current]); /* input file is raw_(some characters).raw, so, first thing, we find * the "." in the name. */ dest = malloc(strlen(argv[current])); if (!dest){ free(dest); return -1; } if ((!strchr(argv[current], 0x2e))|| (strncmp(argv[current], "raw", 3))) { fprintf (stderr, "Source file appears not to be a gphoto2 raw file.\n"); fprintf (stderr, "Its name should begin with raw_ and end with .raw\n"); fprintf (stderr, "Exiting.\n"); return 0; } i = strchr(argv[current], 0x2e) - argv[current]; /* and throw away the period (to avoid clobbering any finished images * from gphoto2 which are in the same directory) and throw away the * suffix. */ strncpy(dest, argv[current] + 4, i - 4); /* then affix "ppm" as a suffix (no preceding period) */ strcat (dest, "ppm"); fprintf (stderr, "Destination file will be called %s\n", dest); /* now open the raw file */ if ( (fp_src = fopen(argv[current], "rb") ) == NULL ) { fprintf (stderr, "Error opening source file.\n"); return 0; } /* get the size of the raw file and assign that size to "b" */ fseek(fp_src, 0, 2); b = ftell(fp_src); fseek(fp_src, 0, 0); buf = malloc(b); if (!buf) return -1; /* read the raw file to "buf", and close it */ fread (buf, 1, b, fp_src); fclose (fp_src); info = malloc(512); if (!info) { free(info); return -1; } get_image_info( info, buf, b, qvga); /* If there is a header, then we print it as debug output, then * move past it, to the data. */ bufp = buf+offset_correct; /* show header */ for (i = 0; i < HEADER_LEN; i++) { fprintf(stderr, " %02X", buf[i]); } fprintf(stderr, "\n"); WIDTH = info->width; HEIGHT = info->height; outdoors = info->outdoors; gamma_factor = info->gamma; gp_gamma_fill_table(gtable, gamma_factor); buf2 = malloc(WIDTH * HEIGHT+256); if (!buf2) { free (buf2); return -1; } if((offset_correct)&&!(qvga)) { info->reverse = 1; info->bayer = BAYER_TILE_BGGR; } if (info->compression) { /* non-zero if compression in use */ sonix_decode(buf2, bufp, WIDTH,HEIGHT); } else { memcpy(buf2, bufp, WIDTH*HEIGHT); } free(buf); if (invert) reverse_bytes(buf2, WIDTH*HEIGHT); ppm = malloc (WIDTH * HEIGHT * 3 + 256); /* Data + header */ if (!ppm) { free (buf2); return 0; } memset (ppm, 0, WIDTH * HEIGHT * 3 + 256); sprintf ((char *)ppm, "P6\n" "#CREATOR: My_decoder\n" "%d %d\n" "255\n", WIDTH, HEIGHT); ptr = ppm + strlen ((char *)ppm); size = strlen ((char *)ppm) + (WIDTH * HEIGHT * 3); gp_bayer_decode(buf2, WIDTH, HEIGHT, ptr, info->bayer); free(buf2); white_balance(ptr, WIDTH * HEIGHT, 1.2); if ( (fp_dest = fopen(dest, "wb") ) == NULL ) { fprintf (stderr, "Error opening dest file.\n"); return 0; } fwrite(ppm, 1, size, fp_dest); fclose (fp_dest); free (ppm); /* The file has been saved. The GUI effects follow now. */ gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); event_box = gtk_event_box_new(); gtk_container_add(GTK_CONTAINER (window), event_box); gtk_widget_show(event_box); /* This lets the Window Manager to close the window _and_ sets up * this program to exit when the window is closed. */ g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (delete_event), NULL); /* This displays the image file. */ image = gtk_image_new_from_file(dest); gtk_container_add(GTK_CONTAINER(event_box), image); gtk_widget_show (image); gtk_widget_show (window); gtk_main (); free(info); free(dest); return 0; }
int fst_cipher_init (FSTCipher *cipher, unsigned int seed, unsigned int enc_type) { int i,j; unsigned int temp; unsigned int sortpos; unsigned char c; cipher->enc_type = enc_type; cipher->wrapcount = 0; cipher->add_to_lookup = 0; cipher->seed = seed; FST_HEAVY_DBG_2 ("init_cipher: seed = 0x%08x, enc_type = 0x%02x", seed, enc_type); if (!pad_init (&seed, enc_type, cipher->pad, sizeof (cipher->pad))) return FALSE; /* adjust pad */ c = 0; for (i = 0; i < sizeof (cipher->pad); i++) c = c | cipher->pad[i]; if (!(c & 1)) cipher->pad[0] = cipher->pad[0] | 0x71; /* init cipher->pos */ temp = seed_step (seed); temp = temp >> 16; cipher->pos = ( (temp << 6) - temp) >> 16; /* init cipher->lookup */ for(i = 0; i <sizeof (cipher->lookup); i++) cipher->lookup[i] = (unsigned char)i; if (enc_type & 0x08) { MD5Context ctx; unsigned char md5[MD5_HASH_LEN]; FST_HEAVY_DBG ("init_cipher: enc_type & 0x08"); MD5Init (&ctx); MD5Update (&ctx, cipher->pad, sizeof(cipher->pad)); MD5Final (md5, &ctx); /* correct md5 byte order on big-endian since it's converted to (unsigned int*) below */ reverse_bytes ( (unsigned int*)&md5, 4); /* modify cipher->lookup */ for (i = 0; i < sizeof (cipher->lookup); i++) { if ( (j = calculate_num( (unsigned int*) &md5, 0x100 - i) + i) != i) { unsigned char a = cipher->lookup[j]; unsigned char b = cipher->lookup[i]; cipher->lookup[i] = a; cipher->lookup[j] = b; } } } if(enc_type & 0x10) { FST_HEAVY_DBG ("init_cipher: enc_type & 0x10"); for (seed = cipher->pos, i=0; i < 20; i++) { seed = seed_step (seed); cipher->pad16[i] = seed; } seed = seed_step (seed); /* CHECKME: endianess? */ EncryptionType2::enc_type_2(cipher->pad16, seed); } /* sort cipher->pad */ sortpos = ( (cipher->pos * cipher->pos) + 2) % (sizeof(cipher->pad)-4); qsort (cipher->pad + sortpos, 5, 1, qsort_cmp_func); /* modify every third byte of cipher->pad */ for (i = 5; i < sizeof (cipher->pad); i += 3) { c = cipher->pad[i]; c = ~c + i; cipher->pad[i] = c | 1; } // print_bin_data(cipher->pad, sizeof(cipher->pad)); // print_bin_data(cipher->lookup, sizeof(cipher->lookup)); return TRUE; }
inline void reverse_bytes_if(ArrayRef<T> const& value, Endian const endian) { if (endian != Endian::system) { reverse_bytes(value); } }