int MSufSort::CompareStrings(SYMBOL_TYPE * stringA, SYMBOL_TYPE * stringB, int len) { #ifdef SORT_16_BIT_SYMBOLS while (len) { unsigned short valA = ENDIAN_SWAP_16(stringA[0]); unsigned short valB = ENDIAN_SWAP_16(stringB[0]); if (valA > valB) return 1; if (valA < valB) return -1; stringA++; stringB++; len--; } #else while (len) { if (stringA[0] > stringB[0]) return 1; if (stringA[0] < stringB[0]) return -1; stringA++; stringB++; len--; } #endif return 0; }
inline void read(U8* item) { instream->getBytes(swapped, 20); ENDIAN_SWAP_32(&swapped[ 0], &item[ 0]); // x ENDIAN_SWAP_32(&swapped[ 4], &item[ 4]); // y ENDIAN_SWAP_32(&swapped[ 8], &item[ 8]); // z ENDIAN_SWAP_16(&swapped[12], &item[12]); // intensity *((U32*)&item[14]) = *((U32*)&swapped[14]); // bitfield, classification, scan_angle_rank, user_data ENDIAN_SWAP_16(&swapped[18], &item[18]); // point_source_ID };
inline BOOL write(const U8* item) { ENDIAN_SWAP_32(&item[ 0], &swapped[ 0]); // x ENDIAN_SWAP_32(&item[ 4], &swapped[ 4]); // y ENDIAN_SWAP_32(&item[ 8], &swapped[ 8]); // z ENDIAN_SWAP_16(&item[12], &swapped[12]); // intensity *((U32*)&swapped[14]) = *((U32*)&item[14]); // bitfield, classification, scan_angle_rank, user_data ENDIAN_SWAP_16(&item[18], &swapped[18]); // point_source_ID return outstream->putBytes(swapped, 20); };
uint16 CFileDataIO::ReadUInt16() const { uint16 value = 0; Read(&value, sizeof(uint16)); return ENDIAN_SWAP_16(value); }
bool SoundBuffer::LoadWAV(const std::string & filename, const SoundInfo & sound_device_info, std::ostream & error_output) { if (loaded) Unload(); name = filename; FILE *fp; unsigned int size; fp = fopen(filename.c_str(), "rb"); if (fp) { char id[5]; //four bytes to hold 'RIFF' if (fread(id,sizeof(char),4,fp) != 4) return false; //read in first four bytes id[4] = '\0'; if (!strcmp(id,"RIFF")) { //we had 'RIFF' let's continue if (fread(&size,sizeof(unsigned int),1,fp) != 1) return false; //read in 32bit size value size = ENDIAN_SWAP_32(size); if (fread(id,sizeof(char),4,fp)!= 4) return false; //read in 4 byte string now if (!strcmp(id,"WAVE")) { //this is probably a wave file since it contained "WAVE" if (fread(id,sizeof(char),4,fp)!= 4) return false; //read in 4 bytes "fmt "; if (!strcmp(id,"fmt ")) { unsigned int format_length, sample_rate, avg_bytes_sec; short format_tag, channels, block_align, bits_per_sample; if (fread(&format_length, sizeof(unsigned int),1,fp) != 1) return false; format_length = ENDIAN_SWAP_32(format_length); if (fread(&format_tag, sizeof(short), 1, fp) != 1) return false; format_tag = ENDIAN_SWAP_16(format_tag); if (fread(&channels, sizeof(short),1,fp) != 1) return false; channels = ENDIAN_SWAP_16(channels); if (fread(&sample_rate, sizeof(unsigned int), 1, fp) != 1) return false; sample_rate = ENDIAN_SWAP_32(sample_rate); if (fread(&avg_bytes_sec, sizeof(unsigned int), 1, fp) != 1) return false; avg_bytes_sec = ENDIAN_SWAP_32(avg_bytes_sec); if (fread(&block_align, sizeof(short), 1, fp) != 1) return false; block_align = ENDIAN_SWAP_16(block_align); if (fread(&bits_per_sample, sizeof(short), 1, fp) != 1) return false; bits_per_sample = ENDIAN_SWAP_16(bits_per_sample); //new wave seeking code //find data chunk bool found_data_chunk = false; long filepos = format_length + 4 + 4 + 4 + 4 + 4; int chunknum = 0; while (!found_data_chunk && chunknum < 10) { fseek(fp, filepos, SEEK_SET); //seek to the next chunk if (fread(id, sizeof(char), 4, fp) != 4) return false; //read in 'data' if (fread(&size, sizeof(unsigned int), 1, fp) != 1) return false; //how many bytes of sound data we have size = ENDIAN_SWAP_32(size); if (!strcmp(id,"data")) { found_data_chunk = true; } else { filepos += size + 4 + 4; } chunknum++; } if (chunknum >= 10) { //cerr << __FILE__ << "," << __LINE__ << ": Sound file contains more than 10 chunks before the data chunk: " + filename << std::endl; error_output << "Couldn't find wave data in first 10 chunks of " << filename << std::endl; return false; } sound_buffer = new char[size]; if (fread(sound_buffer, sizeof(char), size, fp) != size) return false; //read in our whole sound data chunk #if SDL_BYTEORDER == SDL_BIG_ENDIAN if (bits_per_sample == 16) { for (unsigned int i = 0; i < size/2; i++) { //cout << "preswap i: " << sound_buffer[i] << "preswap i+1: " << sound_buffer[i+1] << std::endl; //short preswap = ((short *)sound_buffer)[i]; ((short *)sound_buffer)[i] = ENDIAN_SWAP_16(((short *)sound_buffer)[i]); //cout << "postswap i: " << sound_buffer[i] << "postswap i+1: " << sound_buffer[i+1] << std::endl; //cout << (int) i << "/" << (int) size << std::endl; //short postswap = ((short *)sound_buffer)[i]; //cout << preswap << "/" << postswap << std::endl; } } //else if (bits_per_sample != 8) else { error_output << "Sound file with " << bits_per_sample << " bits per sample not supported" << std::endl; return false; } #endif info = SoundInfo(size/(bits_per_sample/8), sample_rate, channels, bits_per_sample/8); SoundInfo original_info(size/(bits_per_sample/8), sample_rate, channels, bits_per_sample/8); loaded = true; SoundInfo desired_info(original_info.samples, sound_device_info.frequency, original_info.channels, sound_device_info.bytespersample); //ConvertTo(desired_info); if (desired_info == original_info) { } else { error_output << "SOUND FORMAT:" << std::endl; original_info.DebugPrint(error_output); error_output << "DESIRED FORMAT:" << std::endl; desired_info.DebugPrint(error_output); //throw EXCEPTION(__FILE__, __LINE__, "Sound file isn't in desired format: " + filename); //cerr << __FILE__ << "," << __LINE__ << ": Sound file isn't in desired format: " + filename << std::endl; error_output << "Sound file isn't in desired format: "+filename << std::endl; return false; } } else { //throw EXCEPTION(__FILE__, __LINE__, "Sound file doesn't have \"fmt \" header: " + filename); //cerr << __FILE__ << "," << __LINE__ << ": Sound file doesn't have \"fmt \" header: " + filename << std::endl; error_output << "Sound file doesn't have \"fmt\" header: "+filename << std::endl; return false; } } else { //throw EXCEPTION(__FILE__, __LINE__, "Sound file doesn't have WAVE header: " + filename); //cerr << __FILE__ << "," << __LINE__ << ": Sound file doesn't have WAVE header: " + filename << std::endl; error_output << "Sound file doesn't have WAVE header: "+filename << std::endl; return false; } } else { //throw EXCEPTION(__FILE__, __LINE__, "Sound file doesn't have RIFF header: " + filename); //cerr << __FILE__ << "," << __LINE__ << ": Sound file doesn't have WAVE header: " + filename << std::endl; error_output << "Sound file doesn't have RIFF header: "+filename << std::endl; return false; } fclose(fp); } else { //throw EXCEPTION(__FILE__, __LINE__, "Can't open sound file: " + filename); //cerr << __FILE__ << "," << __LINE__ << ": Can't open sound file: " + filename << std::endl; error_output << "Can't open sound file: "+filename << std::endl; return false; } //cout << size << std::endl; return true; }
int _yubi_verify_otp_passcode_helper(char *user, char *otp_passcode, int debug, const char* trans) { int i; yk_ticket tkt; ykdb_entry entry; ykdb_h *handle; char *pch; char otp[65] = ""; // max 12-char public-id, 32-char otp char passcode[65] = ""; uint8_t tkt_private_uid_hash[32]; uint8_t ticket_enc_key[256]; uint8_t ticket_enc_hash[32]; uint8_t public_uid_bin[PUBLIC_UID_BYTE_SIZE]; uint8_t public_uid_bin_size = 0; uint32_t crc; int delta_session; int delta_button; int otp_len = 0; int passcode_len = 0; if (debug) syslog(LOG_DEBUG, "received OTP/Passcode: %s", otp_passcode ? otp_passcode:""); /* set additional default values for the entry after parsing */ getSHA256((uint8_t *)user, strlen(user), (uint8_t *)&entry.user_hash); /* everything upto the first "|" is otp, everything after is passcode */ if ( NULL != (pch=strchr(otp_passcode, '|')) ) { otp_len = pch-otp_passcode; passcode_len = strlen(otp_passcode) - otp_len - 1; if ( otp_len > 0 ) { strncpy(otp, otp_passcode, otp_len); otp[otp_len] = '\0'; } if ( passcode_len > 0 ) strncpy(passcode, pch+1, passcode_len); } else { syslog(LOG_AUTH, "invalid otp/passcode received: %s", otp_passcode ? otp_passcode:""); return YK_FAILURE; } if (debug) syslog(LOG_DEBUG, "OTP: %s (%d), Passcode: %s (%d)", otp, otp_len, passcode, passcode_len); /* perform initial parse to grab public UID */ parseOTP(&tkt, public_uid_bin, &public_uid_bin_size, (uint8_t *)otp, NULL, trans); /* OTP needs the public UID for lookup */ if (public_uid_bin_size <= 0) { if (debug) syslog(LOG_DEBUG, "public_uid has no length, OTP is invalid"); return YK_FAILURE; } /* set additional default values for the entry after parsing */ getSHA256(public_uid_bin, public_uid_bin_size, (uint8_t *)&entry.public_uid_hash); /* open the db or create if empty */ handle = ykdbDatabaseOpen(CONFIG_AUTH_DB_DEFAULT); if (handle == NULL) { if (debug) syslog(LOG_DEBUG, "couldn't access database: %s", CONFIG_AUTH_DB_DEFAULT); return YK_FAILURE; } /* seek to public UID if it exists */ if ( ykdbEntrySeekOnUserPublicHash(handle, (uint8_t *)&entry.user_hash, (uint8_t *)&entry.public_uid_hash, YKDB_SEEK_START) != YKDB_SUCCESS ) { ykdbDatabaseClose(handle); if (debug) syslog(LOG_DEBUG, "no entry for user (with that token): %s", user); free(handle); return YK_FAILURE; } /* grab the entry */ if ( ykdbEntryGet(handle, &entry) != YKDB_SUCCESS ) { ykdbDatabaseClose(handle); free(handle); return YK_FAILURE; } /* start building decryption entry as required */ safeSnprintf((char *)ticket_enc_key, 256, "TICKET_ENC_KEY_BEGIN"); /* add hex string format of public uid */ if ( entry.flags & YKDB_TOKEN_ENC_PUBLIC_UID ) { safeSnprintfAppend((char *)ticket_enc_key, 256, "|"); for(i=0; i<public_uid_bin_size; i++) safeSnprintfAppend((char *)ticket_enc_key, 256, "%02x", public_uid_bin[i]); } /* add passcode as appropriate */ if ( (entry.flags & YKDB_TOKEN_ENC_PASSCODE) ) { if (passcode_len > 0) { safeSnprintfAppend((char *)ticket_enc_key, 256, "|%s", passcode); } else { /* passcode needed but not given */ ykdbDatabaseClose(handle); free(handle); return YK_PASSCODE; } } /* close off decryption key text and generate encryption hash */ safeSnprintfAppend((char *)ticket_enc_key, 256, "|TICKET_ENC_KEY_END"); if (debug) syslog(LOG_DEBUG, "Encryption Key: %s", ticket_enc_key); getSHA256(ticket_enc_key, strlen((char *)ticket_enc_key), ticket_enc_hash); /* decrypt if flags indicate so */ if ( entry.flags & YKDB_TOKEN_ENC_PUBLIC_UID || entry.flags & YKDB_TOKEN_ENC_PASSCODE ) { aesDecryptCBC((uint8_t *)&entry.ticket, sizeof(ykdb_entry_ticket), ticket_enc_hash, ticket_enc_hash+16); } /* perform real parse to grab real ticket, using the now unecrypted key */ parseOTP(&tkt, public_uid_bin, &public_uid_bin_size, (uint8_t *)otp, (uint8_t *)&entry.ticket.key, trans); /* check CRC matches */ crc = getCRC((uint8_t *)&tkt, sizeof(yk_ticket)); ENDIAN_SWAP_16(crc); /* no use continuing if the decoded OTP failed */ if ( crc != CRC_OK_RESIDUE ) { ykdbDatabaseClose(handle); if (debug) syslog(LOG_DEBUG, "crc invalid: 0x%04x", crc); free(handle); return YK_FAILURE; } /* hash decrypted private uid */ getSHA256(tkt.private_uid, PRIVATE_UID_BYTE_SIZE, (uint8_t *)&tkt_private_uid_hash); /* match private uid hashes */ if ( memcmp(&tkt_private_uid_hash, &entry.ticket.private_uid_hash, 32) ) { ykdbDatabaseClose(handle); if (debug) syslog(LOG_DEBUG, "private uid mismatch"); free(handle); return YK_FAILURE; } /* check counter deltas */ delta_session = tkt.session_counter - entry.ticket.last_session; delta_button = tkt.button_counter - entry.ticket.last_button; if ( delta_session < 0 ) { ykdbDatabaseClose(handle); if (debug) syslog(LOG_DEBUG, "OTP is INVALID. Session delta: %d. Possible replay!!!", delta_session); free(handle); return YK_FAILURE; } if ( delta_session == 0 && delta_button <= 0 ) { ykdbDatabaseClose(handle); if (debug) syslog(LOG_DEBUG, "OTP is INVALID. Session delta: %d. Button delta: %d. Possible replay!!!", delta_session, delta_button); free(handle); return YK_FAILURE; } /* update the database entry with the latest counters */ entry.ticket.last_timestamp_lo = tkt.timestamp_lo; entry.ticket.last_timestamp_hi = tkt.timestamp_hi; entry.ticket.last_session = tkt.session_counter; entry.ticket.last_button = tkt.button_counter; /* re-encrypt and write to database */ if ( entry.flags & YKDB_TOKEN_ENC_PUBLIC_UID || entry.flags & YKDB_TOKEN_ENC_PASSCODE ) { aesEncryptCBC((uint8_t *)&entry.ticket, sizeof(ykdb_entry_ticket), ticket_enc_hash, ticket_enc_hash+16); } /* re-encrypt and write to database */ if ( ykdbEntryWrite(handle, &entry) != YKDB_SUCCESS ) { ykdbDatabaseClose(handle); free(handle); return YK_FAILURE; } free(handle); return YK_SUCCESS; }
void MSufSort::InitialSort() { // This is the first sorting pass which makes the initial suffix // chains from the given source string. Pushes these chains onto // the stack for further sorting. #ifndef SORT_16_BIT_SYMBOLS #ifdef USE_ALT_SORT_ORDER for (unsigned int suffixIndex = 0; suffixIndex < m_sourceLength; suffixIndex++) m_source[suffixIndex] = m_forwardAltSortOrder[m_source[suffixIndex]]; #endif #endif #ifdef USE_ENHANCED_INDUCTION_SORTING m_ISA[m_sourceLength - 1] = m_ISA[m_sourceLength - 2] = SORTED_BY_ENHANCED_INDUCTION; m_firstSortedPosition[Value16(m_sourceLength - 1)]++; m_firstSortedPosition[Value16(m_sourceLength - 2)]++; for (int suffixIndex = m_sourceLength - 3; suffixIndex >= 0; suffixIndex--) { unsigned short symbol = Value16(suffixIndex); m_firstSortedPosition[symbol]++; #ifdef SORT_16_BIT_SYMBOLS unsigned short valA = ENDIAN_SWAP_16(m_source[suffixIndex]); unsigned short valB = ENDIAN_SWAP_16(m_source[suffixIndex + 1]); if ((suffixIndex == m_sourceLengthMinusOne) || (valA > valB)) m_ISA[suffixIndex] = SORTED_BY_ENHANCED_INDUCTION; else AddToSuffixChain(suffixIndex, symbol); #else bool useEIS = false; if ((m_source[suffixIndex] > m_source[suffixIndex + 1]) || ((m_source[suffixIndex] < m_source[suffixIndex + 1]) && (m_source[suffixIndex] > m_source[suffixIndex + 2]))) useEIS = true; if (!useEIS) { if (m_endOfSuffixChain[symbol] == END_OF_CHAIN) { m_endOfSuffixChain[symbol] = m_startOfSuffixChain[symbol] = suffixIndex; m_newChainIds[m_numNewChains++] = ENDIAN_SWAP_16(symbol); } else { m_ISA[suffixIndex] = m_startOfSuffixChain[symbol]; m_startOfSuffixChain[symbol] = suffixIndex; } } else m_ISA[suffixIndex] = SORTED_BY_ENHANCED_INDUCTION; #endif } #else for (unsigned int suffixIndex = 0; suffixIndex < m_sourceLength; suffixIndex++) { unsigned short symbol = Value16(suffixIndex); AddToSuffixChain(suffixIndex, symbol); } #endif #ifdef USE_ENHANCED_INDUCTION_SORTING unsigned int n = 1; for (unsigned int i = 0; i < 0x10000; i++) { unsigned short p = ENDIAN_SWAP_16(i); unsigned int temp = m_firstSortedPosition[p]; if (temp) { m_firstSortedPosition[p] = n; n += temp; } } #endif MarkSuffixAsSorted(m_sourceLength, m_nextSortedSuffixValue); PushNewChainsOntoStack(true); }
bool SoundBuffer::LoadWAV(const std::string & filename, const SoundInfo & sound_device_info, std::ostream & error_output) { if (loaded) Unload(); name = filename; std::ifstream file(filename.c_str(), std::ifstream::binary); if (!file) { error_output << "Failed to open sound file: " << filename << std::endl; return false; } // read in first four bytes char id[5] = {'\0'}; file.read(id, 4); if (!file) { error_output << "Failed to read sound file RIFF header: " << filename << std::endl; return false; } if (strcmp(id, "RIFF")) { error_output << "Sound file doesn't have RIFF header: " << filename << std::endl; return false; } // read in 32bit size value unsigned int size = 0; file.read((char*)&size, sizeof(unsigned int)); size = ENDIAN_SWAP_32(size); if (!file) { error_output << "Failed to read sound file size: " << filename << std::endl; return false; } // read in 4 bytes "WAVE" file.read(id, 4); if (!file) { error_output << "Failed to read WAVE header: " << filename << std::endl; return false; } if (strcmp(id, "WAVE")) { error_output << "Sound file doesn't have WAVE header: " << filename << std::endl; return false; } // read in 4 bytes "fmt "; file.read(id, 4); if (!file) { error_output << "Failed to read \"fmt\" header: " << filename << std::endl; return false; } if (strcmp(id, "fmt ")) { error_output << "Sound file doesn't have \"fmt\" header: " << filename << std::endl; return false; } // read sound format unsigned int format_length, sample_rate, avg_bytes_sec; short format_tag, channels, block_align, bits_per_sample; file.read((char*)&format_length, sizeof(unsigned int)); file.read((char*)&format_tag, sizeof(short)); file.read((char*)&channels, sizeof(short)); file.read((char*)&sample_rate, sizeof(unsigned int)); file.read((char*)&avg_bytes_sec, sizeof(unsigned int)); file.read((char*)&block_align, sizeof(short)); file.read((char*)&bits_per_sample, sizeof(short)); if (!file) { error_output << "Failed to read sound format: " << filename << std::endl; return false; } format_length = ENDIAN_SWAP_32(format_length); format_tag = ENDIAN_SWAP_16(format_tag); channels = ENDIAN_SWAP_16(channels); sample_rate = ENDIAN_SWAP_32(sample_rate); avg_bytes_sec = ENDIAN_SWAP_32(avg_bytes_sec); block_align = ENDIAN_SWAP_16(block_align); bits_per_sample = ENDIAN_SWAP_16(bits_per_sample); // find data chunk bool found_data_chunk = false; long filepos = format_length + 4 + 4 + 4 + 4 + 4; int chunknum = 0; while (!found_data_chunk && chunknum < 10) { // seek to the next chunk file.seekg(filepos); // read in 'data' file.read(id, 4); if (!file) return false; // how many bytes of sound data we have file.read((char*)&size, sizeof(unsigned int)); if (!file) return false; size = ENDIAN_SWAP_32(size); if (!strcmp(id, "data")) found_data_chunk = true; else filepos += size + 4 + 4; chunknum++; } if (chunknum >= 10) { error_output << "Couldn't find wave data in first 10 chunks of " << filename << std::endl; return false; } // read in our whole sound data chunk sound_buffer = new char[size]; file.read(sound_buffer, size); if (!file) { error_output << "Failed to read wave data " << filename << std::endl; return false; } #if SDL_BYTEORDER == SDL_BIG_ENDIAN if (bits_per_sample == 16) { for (unsigned int i = 0; i < size / 2; i++) { ((short *)sound_buffer)[i] = ENDIAN_SWAP_16(((short *)sound_buffer)[i]); } } else { error_output << "Sound file with " << bits_per_sample << " bits per sample not supported" << std::endl; return false; } #endif info = SoundInfo(size/(bits_per_sample/8), sample_rate, channels, bits_per_sample/8); loaded = true; SoundInfo original_info(size/(bits_per_sample/8), sample_rate, channels, bits_per_sample/8); SoundInfo desired_info(original_info.samples, sound_device_info.frequency, original_info.channels, sound_device_info.bytespersample); if (!(desired_info == original_info)) { error_output << "SOUND FORMAT:" << std::endl; original_info.DebugPrint(error_output); error_output << "DESIRED FORMAT:" << std::endl; desired_info.DebugPrint(error_output); error_output << "Sound file isn't in desired format: " << filename << std::endl; return false; } return true; }