bool isNext() { ushort magic[2]; int rc; rc = fread(&magic[0],1,2,zipfp); rc = fread(&magic[1],1,2,zipfp); return ( (magic[0] == SWAP_BYTES(0x4B50)) && (magic[1] == SWAP_BYTES(0x0403)) ); }
uint8_t emb_srv_mask_reg(struct emb_super_server_t* _ssrv, struct emb_server_t* _srv) { struct emb_srv_regs_t* r; uint8_t* rx_data = _ssrv->rx_pdu->data; uint8_t* tx_data = _ssrv->tx_pdu->data; uint8_t res; uint16_t tmp; const uint16_t addr = GET_BIG_END16(rx_data + 0), and_mask = GET_BIG_END16(rx_data + 2), or_mask = GET_BIG_END16(rx_data + 4); if(!_srv->get_holding_regs) return MBE_SLAVE_FAILURE; r = _srv->get_holding_regs(_srv, addr); if(!r) return MBE_ILLEGAL_DATA_ADDR; if(!r->read_regs || !r->write_regs) return MBE_ILLEGAL_DATA_ADDR; res = r->read_regs(r, addr - r->start, 1, &tmp); if(res) return res; tmp = (tmp & and_mask) | (or_mask & ~and_mask); res = r->write_regs(r, addr - r->start, 1, &tmp); if(res) return res; if(MASK_REGISTER_ANS_SIZE() > _ssrv->tx_pdu->max_size) return MBE_SLAVE_FAILURE; ((uint16_t*)tx_data)[0] = SWAP_BYTES(addr); ((uint16_t*)tx_data)[1] = SWAP_BYTES(and_mask); ((uint16_t*)tx_data)[2] = SWAP_BYTES(or_mask); _ssrv->tx_pdu->function = 0x16; _ssrv->tx_pdu->data_size = MASK_REGISTER_ANS_SIZE(); return 0; }
uint8_t emb_srv_write_coil(struct emb_super_server_t* _ssrv, struct emb_server_t* _srv) { uint8_t* rx_data = _ssrv->rx_pdu->data; uint8_t* tx_data = _ssrv->tx_pdu->data; uint8_t coil_value; struct emb_srv_bits_t* coils; const uint16_t addr = GET_BIG_END16(rx_data + 0), value = GET_BIG_END16(rx_data + 2); switch(value) { case 0x0000: coil_value = 0; break; case 0xFF00: coil_value = 1; break; default: return MBE_ILLEGAL_DATA_VALUE; } if(!_srv->get_coils) return MBE_SLAVE_FAILURE; coils = _srv->get_coils(_srv, addr); if(!coils) return MBE_ILLEGAL_DATA_ADDR; if(!coils->write_bits) return MBE_ILLEGAL_DATA_ADDR; _ssrv->tx_pdu->function = 0x05; _ssrv->tx_pdu->data_size = WRITE_COIL_ANS_SIZE(); if(_ssrv->tx_pdu->max_size < _ssrv->tx_pdu->data_size) return MBE_SLAVE_FAILURE; ((uint16_t*)(tx_data))[0] = SWAP_BYTES(addr); ((uint16_t*)(tx_data))[1] = SWAP_BYTES(value); return coils->write_bits(coils, addr - coils->start, 1, &coil_value); }
/* readInteger () ** ** Read and return an integer. */ int readInteger () { int i, numBytesRead; numBytesRead = fread (&i, 4, 1, inputFile); if (numBytesRead != 1) { fatalError ("Problem reading from input file"); } return SWAP_BYTES (i); }
/** * @brief Send a PDU. * * (Transport interface) This function will send packet (CRC suffix automatically added). * * @param [in] _mbt TCP context * @param [in] _slave_addr Address of slave * @param [in] _pdu PDU, that will be sent. * * @return Zero on success, error on fail. */ static int modbus_tcp_send_packet(void *_mbt, int _slave_addr, emb_const_pdu_t *_pdu) { struct emb_tcp_t* mbt = (struct emb_tcp_t*)_mbt; if(_pdu->data_size <= MAX_PDU_DATA_SIZE) { struct emb_tcp_mbap_t* mbap = (struct emb_tcp_mbap_t*)mbt->tx_buf; const uint16_t length = _pdu->data_size + 2; // If we are server, then take a transaction-id from request, // if we are client, then transaction-id is a number, increased by 1 per transaction. if(mbt->transport.flags & EMB_TRANSPORT_FLAG_IS_SERVER) { mbap->transact_id = ((struct emb_tcp_mbap_t*)mbt->rx_buf)->transact_id; } else { uint16_t prev_transaction_id = SWAP_BYTES(mbap->transact_id); ++prev_transaction_id; mbap->transact_id = SWAP_BYTES(prev_transaction_id); } mbap->proto_id = SWAP_BYTES(0); mbap->length = SWAP_BYTES(length); mbap->unit_id = _slave_addr; mbt->tx_buf[emb_tcp_mbap_size] = _pdu->function; memcpy((mbt->tx_buf + emb_tcp_mbap_size+1), _pdu->data, _pdu->data_size); mbt->tx_pkt_size = length + 6; mbt->tx_pkt_counter = 0; #if EMODBUS_PACKETS_DUMPING if(mbt->transport.flags & EMB_TRANSPORT_FLAG_DUMD_PAKETS) if(emb_dump_tx_data) emb_dump_tx_data(mbt->tx_buf, mbt->tx_pkt_size); #endif // EMODBUS_PACKETS_DUMPING write_data_to_port(mbt); return 0; } else return -modbus_buffer_overflow; }
void emb_tcp_initialize(struct emb_tcp_t* _mbt) { if(_mbt) { struct emb_tcp_mbap_t* mbap = (struct emb_tcp_mbap_t*)_mbt->tx_buf; // Set first transaction_id to zero. mbap->transact_id = SWAP_BYTES(0); _mbt->transport.send_packet = modbus_tcp_send_packet; _mbt->transport.transport_context = _mbt; _mbt->curr_rx_length = -1; } }
/* writeInteger (x) ** ** Write an integer (4-bytes, binary) to the BLITZ DISK file. */ void writeInteger (int x) { int i, x2; x2 = SWAP_BYTES (x); errno = 0; i = fwrite (&x2, 1, 4, diskFile); if (i != 4) { if (errno) perror ("Error writing to BLITZ DISK file"); fatalError ("Problems writing to BLITZ DISK file"); } }
/* readInteger () ** ** Read an integer (4-bytes, binary) from the BLITZ DISK file and return it. */ int readInteger () { int i, numItemsRead; errno = 0; numItemsRead = fread (&i, 4, 1, diskFile); if (numItemsRead != 1) { if (errno) perror ("Error when reading from file"); fatalError ("Problem reading from file"); } return SWAP_BYTES (i); }
static int parse_packet(struct emb_tcp_t* _mbt) { _mbt->curr_rx_length = -1; // TODO: Maybe I need to loop-based searching in receive buffer ? if(_mbt->rx_pkt_counter >= (emb_tcp_mbap_size + 2)) { struct emb_tcp_mbap_t* mbap = (struct emb_tcp_mbap_t*)_mbt->rx_buf; const uint16_t pkt_length = SWAP_BYTES(mbap->length) - 1; _mbt->curr_rx_length = (emb_tcp_mbap_size + pkt_length) - _mbt->rx_pkt_counter; // If we are received not all packet, then skip processing, and // wait for data remainder. if(_mbt->curr_rx_length > 0) return 0; #if EMODBUS_PACKETS_DUMPING if(_mbt->transport.flags & EMB_TRANSPORT_FLAG_DUMD_PAKETS) if(emb_dump_rx_data) emb_dump_rx_data(_mbt->rx_buf, _mbt->rx_pkt_counter); #endif // EMODBUS_PACKETS_DUMPING do { // If we are client's side, then we need to check a transaction id. // The transaction id must be the same as in sent packet. if(!(_mbt->transport.flags & EMB_TRANSPORT_FLAG_IS_SERVER)) { struct emb_tcp_mbap_t* tx_mbap = (struct emb_tcp_mbap_t*)_mbt->tx_buf; if(tx_mbap->transact_id != mbap->transact_id) { emb_transport_error(&_mbt->transport, -modbus_resp_wrong_transaction_id); break; } } if(_mbt->transport.rx_pdu) { emb_pdu_t* const rx_pdu = _mbt->transport.rx_pdu; const int pkt_data_length = pkt_length - 1; if(rx_pdu->max_size < pkt_data_length) { emb_transport_error(&_mbt->transport, -modbus_resp_buffer_ovf); break; } rx_pdu->function = _mbt->rx_buf[emb_tcp_mbap_size]; rx_pdu->data_size = pkt_data_length; memcpy(rx_pdu->data, _mbt->rx_buf + (emb_tcp_mbap_size+1), pkt_data_length); emb_transport_recv_packet(&_mbt->transport, mbap->unit_id, MB_CONST_PDU(rx_pdu)); } } while(0); return 1; } return 0; }
int main(int argc, char **argv) { short val; char *p_val; p_val = (char *) &val; p_val[0] = 0x12; p_val[1] = 0x34; if (!is_big_endian()) { val = SWAP_BYTES(val); } printf("%x\n", val); return 0; }
uint8_t emb_srv_write_file(struct emb_super_server_t* _ssrv, struct emb_server_t* _srv) { enum { reference_type = 0x06 }; uint8_t* rx_data = _ssrv->rx_pdu->data; const uint8_t byte_count = rx_data[0]; const uint8_t* const rx_data_end = rx_data + _ssrv->rx_pdu->data_size; if(byte_count <= 0x09 || byte_count >= 0xFB) { return MBE_ILLEGAL_DATA_VALUE; } _ssrv->tx_pdu->function = 0x15; if(_ssrv->tx_pdu->max_size < _ssrv->rx_pdu->data_size) return MBE_SLAVE_FAILURE; // Copy all request data to response. _ssrv->tx_pdu->data_size = _ssrv->rx_pdu->data_size; memcpy(_ssrv->tx_pdu->data, rx_data, _ssrv->rx_pdu->data_size); ++rx_data; while(rx_data < rx_data_end) { struct emb_srv_file_t* file; if(*rx_data != EMB_FILE_REF_TYPE) return MBE_ILLEGAL_DATA_ADDR; if(!_srv->get_file) return MBE_SLAVE_FAILURE; file = _srv->get_file(_srv, GET_BIG_END16(rx_data + 1)/*, start_addr*/); if((file) && (file->write_file) /*&& ((file->start + file->size) >= (start_addr + reg_count))*/) { uint8_t res; uint16_t j; const uint16_t start_addr = GET_BIG_END16(rx_data + 3); const uint16_t reg_count = GET_BIG_END16(rx_data + 5); // Skip ref-type, fileno, start_addr and reg_count. rx_data += (sizeof(uint8_t) + 3*sizeof(uint16_t)); if((rx_data + (reg_count*2)) > rx_data_end) return MBE_ILLEGAL_DATA_VALUE; for(j=0; j<reg_count; ++j) { const uint16_t tmp = ((uint16_t*)rx_data)[j]; ((uint16_t*)rx_data)[j] = SWAP_BYTES(tmp); } res = file->write_file(file, start_addr, reg_count, (uint16_t*)rx_data); if(res) return res; // skip data rx_data += reg_count*2; } else { return MBE_ILLEGAL_DATA_ADDR; } } return 0; }
// checkHostCompatibility () // // This routine checks that the host implementation of C++ meets certain // requirements. // // (1) This routine checks that integers are represented using exactly 4 // bytes. // // (2) This routine checks that integers are stored in the expected // Big or Little Endian order. // // (3) This routine checks that integer overflow behavior is as expected // with two's complement arithmetic. // // (4) This routine checks that doubles are implemented using 8-bytes in // the IEEE standard, with the bytes in correct Big/Little Endian order. // // (5) This routine checks that the double->int conversion works as // expected. If this is not the case, then truncateToInt() will need to // be changed. // void checkHostCompatibility () { union fourBytes { char chars [4]; unsigned int i; } fourBytes; double d; char * p, * q; int i, i1, i2, i3; // Check that ints are in the expected Big/Little Endian order. fourBytes.chars[0] = 0x12; fourBytes.chars[1] = 0x34; fourBytes.chars[2] = 0x56; fourBytes.chars[3] = 0x78; if (SWAP_BYTES(fourBytes.i) != 0x12345678) { fatalError ("There is a big/little endian byte ordering problem."); } // Check that we have at least 4 bytes of precision. i = 0x00000001; i <<= 20; i <<= 10; i >>= 20; i >>= 10; if (i != 0x00000001) { fatalError ("This program only runs on computers with 4 byte integers - 1"); } // Check that we have no more than 4 bytes of precision. i = 0x00000001; i <<= 20; i <<= 13; // Some compilers treat <<33 as a nop! i >>= 20; i >>= 13; if (i != 0x00000000) { fatalError ("This program only runs on computers with 4 byte integers - 2"); } // Check that we have the expected overflow behavior for ints. i = -2147483647; i = i - 2; if (i != 2147483647) { fatalError ("This program only runs on computers with 4 byte integers - 3"); } // Check that doubles are represented as we expect. d = 123.456e37; p = (char *) &d; q = p; // If doubles are stored in Big Endian byte order.... if ((*p++ == '\x48') && (*p++ == '\x0d') && (*p++ == '\x06') && (*p++ == '\x3c') && (*p++ == '\xdb') && (*p++ == '\x93') && (*p++ == '\x27') && (*p++ == '\xcf')) { #ifdef BLITZ_HOST_IS_LITTLE_ENDIAN fatalError ("There is a big/little endian byte ordering problem with doubles - 1."); #endif // Else, if doubles are stored in Little Endian byte order... } else if ((*q++ == '\xcf') && (*q++ == '\x27') && (*q++ == '\x93') && (*q++ == '\xdb') && (*q++ == '\x3c') && (*q++ == '\x06') && (*q++ == '\x0d') && (*q++ == '\x48')) { #ifdef BLITZ_HOST_IS_LITTLE_ENDIAN #else fatalError ("There is a big/little endian byte ordering problem with doubles - 2."); #endif // Else, if doubles are stored in some other way... } else { fatalError ("The host implementation of 'double' is not what I expect."); } // There is variation in the way different hosts handle double->int conversion // when the double is too large to represent as an integer. When checking // we must do the conversion in two steps, since some compilers perform the // conversion at compile time, and will do the conversion differently than // the host machine. Truly appalling, isn't it! // On PPC, (int) 9e99 is 0x7fffffff // On PPC, (int) d is 0x7fffffff // On Intel, (int) 9e99 is 0x7fffffff // On Intel, (int) d is 0x80000000 // i = (int) 9e99; // printf ("(int) 9e99 is 0x%08x\n", i); d = 9e99; i = (int) d; // Note: ((int) 9e99 == 0 while ((int) d) == 2147483647)!!! // printf ("(int) d is 0x%08x\n", i); // Check that double->int conversion works as expected. d = 4.9; i1 = (int) d; d = -4.9; i2 = (int) d; d = -9e99; i3 = (int) d; if ((i1 != 4) || (i2 != -4) || (i3 != 0x80000000)) { printf ("%d %d %d\n", i1, i2, i3); fatalError ("The host implementation of double->int casting is not what I expect."); } }
static void USPiKeyStatusHandlerRaw (unsigned char ucModifiers, const unsigned char RawKeys[6]) { static int keydown[MAX_KEYS] = { 0, 0, 0, 0, 0, 0 }; static int capsLock = 1; int i, index, c; short key; for (index = 0; index < MAX_KEYS; index++) { key = RawKeys[index]; if (key < 4 || key > KEY_MAX_CODE) { // Invalid key code continue; } for (i = 0; i < MAX_KEYS; i++) { if (key == keydown[i]) { break; } } if (i >= MAX_KEYS) { for (int i = 0; i < MAX_KEYS; i++) { if (keydown[i] == 0) { if (key == 57) { capsLock = capsLock ? 0 : 1; } else if ((ucModifiers & (LSHIFT | RSHIFT)) != 0 && (ucModifiers & ALTGR) != 0) { c = keyMap[key][KEY_SHIFT_ALTGR]; put_key(c != 0 ? c : SWAP_BYTES(key)); } else if ((ucModifiers & ALTGR) != 0) { c = keyMap[key][KEY_ALTGR]; put_key(c != 0 ? c : SWAP_BYTES(key)); } else if ((ucModifiers & (LSHIFT | RSHIFT)) != 0) { c = keyMap[key][KEY_SHIFT]; if (capsLock) { c = tolower(c); } put_key(c != 0 ? c : SWAP_BYTES(key)); } else { c = keyMap[key][KEY_NORMAL]; if (c >= 'a' && c <= 'z' && (ucModifiers & (LCTRL | RCTRL)) != 0) { c = (c - 'a') + 1; } else if (capsLock) { c = toupper(c); } put_key(c != 0 ? c : SWAP_BYTES(key)); } keydown[i] = key; kbd_status[key] = 1; break; } } } } for (i = 0; i < MAX_KEYS; i++) { key = keydown[i]; if (key != 0) { for (index = 0; index < MAX_KEYS; index++) { if (RawKeys[index] == key) break; } if (index >= MAX_KEYS) { keydown[i] = 0; kbd_status[key] = 0; } } } }
len -= rc; } } static void add_to_zip_directory(char *fname, int len){ uint fname_length = strlen(fname); ushort header[500]; if ((central_directory_ptr + 400) > central_directory_limit) { sprintf(message, "Ran out of ZIP central directory space\n \ after creating %d entries.\n", central_directory_count); l_abort(message); } header[0] = SWAP_BYTES(0x4B50); header[1] = SWAP_BYTES(0x0201); header[2] = SWAP_BYTES(0xA); // required version header[3] = SWAP_BYTES(0xA); // flags => not compressed header[4] = 0; // Compression method => none header[5] = 0; // Last modified date and time. header[6] = 0; header[7] = 0; // CRC header[8] = 0; header[9] = 0;