void nextPermutation(vector<int>& nums) { int start = (int)nums.size() - 2; while(start >= 0) { if (nums[start] < nums[start + 1]) break; start--; } if(start < 0) { reverse_array(nums, 0, nums.size() - 1); return; } int end = (int)nums.size() - 1; while (end > start) { if (nums[end] > nums[start]) break; end--; } swap(nums, start, end); reverse_array(nums, start + 1, nums.size() - 1); }
int main(){ // Two integers j and k int j = 4; int k = 5; // Display them pre and post swap to show inplace_swap() working printf("j = %d, k = %d\n", j, k); inplace_swap(&j,&k); printf("j = %d, k = %d\n", j, k); int a[4] = {1,2,3,4,}; // array #1 - 4 elements int b[5] = {1,2,3,4,5}; // array #2 - 5 elements, to show an odd number // Print out the array post reversal reverse_array(a, (sizeof(a)/sizeof(*a))); for(int i=0; i < (sizeof(a)/sizeof(*a)); i++){ printf("%d ", a[i]);} printf("\n"); // Print out the array post reversal reverse_array(b, (sizeof(b)/sizeof(*b))); for(int i=0; i < (sizeof(b)/sizeof(*b)); i++){ printf("%d ", b[i]);} printf("\n"); return 0; }
int main () { int a[] = {1,3,3,4}; int b[] = {1,2,3,4,5}; reverse_array(a, 4); reverse_array(b, 5); return 0; }
int main(void) { int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 98, 1024, 1337}; int b[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 98, 1024, 1337, 333}; print_array(a, sizeof(a) / sizeof(int)); reverse_array(a, sizeof(a) / sizeof(int)); print_array(a, sizeof(a) / sizeof(int)); print_array(b, sizeof(b) / sizeof(int)); reverse_array(b, sizeof(b) / sizeof(int)); print_array(b, sizeof(b) / sizeof(int)); return (0); }
int main() { int a[100], i, size; printf("Input the size:"); scanf("%d", &size); for (i = 0; i < size; i++) { printf("Input the number:"); scanf("%d", &a[i]); } printf("\nBefore the array:\n"); for (i = 0; i < size; i++) { printf("%d ", a[i]); } reverse_array(a, size); printf("\nAfter the array:\n"); for (i = 0; i < size; i++) { printf("%d ", a[i]); } printf("\n"); return 0; }
int main(int argc, char *argv[]) { int *a = calloc(argc-1, sizeof(int)); int cnt = argc-1; int i; for (i = 0; i < cnt; i++) { a[i] = atoi(argv[i+1]); } printf("First version:\n"); printf(" Initially: a[0..%d] =", cnt-1); for (i = 0; i < cnt; i++) printf(" %d", a[i]); printf("\n"); reverse_array(a, cnt); printf(" Finally: a[0..%d] =", cnt-1); for (i = 0; i < cnt; i++) printf(" %d", a[i]); printf("\n"); for (i = 0; i < cnt; i++) { a[i] = atoi(argv[i+1]); } printf("Second version:\n"); printf(" Initially: a[0..%d] =", cnt-1); for (i = 0; i < cnt; i++) printf(" %d", a[i]); printf("\n"); fix_reverse_array(a, cnt); printf(" Finally: a[0..%d] =", cnt-1); for (i = 0; i < cnt; i++) printf(" %d", a[i]); printf("\n"); return 0; }
int main(){ int b[]={1,2,3,4,1}; reverse_array(b,5); int i ; for(i = 0 ;i < 5;i ++) printf("%d ",b[i]); return 0 ; }
int main () { int temp[10] = {1,5,4,3,2,6,5,4,3,8}; print_array (temp, 10); printf ("\n"); reverse_array(temp,0,9); print_array (temp, 10); printf ("\n"); return 0; }
void chapter2page35test2dot11() { int *a, *b; int s[] = {1, 3, 5, 7, 9}; // printf("%d%d%d%d%d\n", s[0], s[1], s[2], s[3], s[4]); a = reverse_array(s, 5); printf("%d%d%d%d%d\n", a[0], *(a + 1), a[2], a[3], a[4]); // // int s1[] = {1, 2, 3, 4}; // printf("%d%d%d%d\n", s1[0], s1[1], s1[2], s1[3]); // b = reverse_array(s1, 4); // printf("%d%d%d%d\n", b[0], b[1], b[2], b[3]); }
int main() { double arr_of_dbl[MAX_SZ]; unsigned size = fill_array(arr_of_dbl, MAX_SZ); show_array(arr_of_dbl, size); std::cout << std::endl; reverse_array(arr_of_dbl, size); show_array(arr_of_dbl, size); std::cout << std::endl; return 0; }
int main(int argc, char **argv) { int a[] = {-1, -2, -3, 4, 5, 6, 7}; int cnt = sizeof(a) / sizeof(int); printf("array:"); print_array(a, cnt); reverse_array(a, cnt); printf("reverse:"); print_array(a, cnt); return 0; }
void main () { int a[max],array_length; printf("how long is the array:"); scanf("%d",&array_length); printf("input the array:\n"); for(i=0;i<array_length;i++) scanf("%d",&a[i]); reverse_array(a,array_length); for(i=0;i<array_length;i++) printf("%d ",a[i]); }
int main() { int array1[5] = {10, 20, 30, 40, 50}; int array2[5]; // declare another array of size=5 printf("Printing array1...\n"); print_array(array1, 5); reverse_array(array1, array2, 5); printf("\n\nPrinting array2...\n"); print_array(array2, 5); printf("\n\n"); system("PAUSE"); return 0; }
void in() { const char *s = "abcdef"; showByte((byte_pointer) s, sizeof(s)); int a = 1; int b = 1; int c = a ^ b; printf("%d", c); int arr[] = { 1, 2, 3, 4, 5, 6 }; reverse_array(arr, 6); int i; for (i = 0; i < 6; i++) { printf("%d", arr[i]); } printf("\n--show int--\n"); showInt(100); }
static void check_winding(void) { int r, rings, winding; GArray *ring; rings = gv_areas_num_rings(area); for (r=0; r < rings; ++r) { ring = gv_areas_get_ring(area, r); winding = find_winding(ring); if ((r == 0 && winding == GV_CW) || (r > 0 && winding == GV_CCW)) { reverse_array(ring); } } }
int main(){ printf("Number of numbers: "); unsigned int length; scanf("%u", &length); unsigned int array[length]; printf("Enter the numbers: "); for(unsigned int i=0; i<length; ++i){ scanf("%u", &array[i]); } reverse_array(array, length); for(unsigned int i=0; i<length; ++i){ printf("%u ", array[i]); } return 0; }
/* There is at most one digit per bit. So the maximum buffer needed is equal to * the number of bits in an int. */ int avb_itoa(int n, char *buf, int base, int fill) { static const char digits[] = "0123456789ABCDEF"; int i = 0; while (n > 0) { int next = n / base; int cur = n % base; buf[i] = digits[cur]; i += 1; fill--; n = next; } for (;fill > 0;fill--) { buf[i] = '0'; i++; } reverse_array(buf, i); return i; }
static int itoa(unsigned n, char *buf, unsigned base, int fill) { static const char digits[] = "0123456789ABCDEF"; unsigned i = 0; if (n == 0) fill += 1; while (n > 0) { unsigned next = n / base; unsigned cur = n % base; buf[i] = digits[cur]; i += 1; fill--; n = next; } for (;fill > 0;fill--) { buf[i] = '0'; i++; } reverse_array(buf, i); return i; }
int main() { int action; char **pointer; pointer = malloc(5 * sizeof(char *)); pointer[0] = "asd"; pointer[1] = "123"; pointer[2] = "lald"; pointer[3] = "mnmn"; pointer[4] = END_OF_ARRAY; while(1) { show_menu(); scanf("%d", &action); switch(action) { case 0: exit(0); break; case 1: pointer = allocate_memory(); break; case 2: fill_memory(pointer); break; case 3: replace_memory(pointer); break; case 4: reverse_array(pointer); break; case 5: break; case 6: print_memory(pointer); break; } } }
void AccountMgr::AddAccount(Field* field) { Account * acct = new Account; Sha1Hash hash; string Username = field[1].GetString(); string Password = field[2].GetString(); //string EncryptedPassword = field[3].GetString(); string GMFlags = field[3].GetString(); acct->AccountId = field[0].GetUInt32(); acct->AccountFlags = field[4].GetUInt8(); acct->Banned = field[5].GetUInt32(); if ( (uint32)UNIXTIME > acct->Banned && acct->Banned != 0 && acct->Banned != 1) //1 = perm ban? { //Accounts should be unbanned once the date is past their set expiry date. acct->Banned = 0; //me go boom :( //printf("Account %s's ban has expired.\n",acct->UsernamePtr->c_str()); sLogonSQL->Execute("UPDATE accounts SET banned = 0 WHERE acct=%u",acct->AccountId); } acct->SetGMFlags(GMFlags.c_str()); acct->Locale[0] = 'e'; acct->Locale[1] = 'n'; acct->Locale[2] = 'U'; acct->Locale[3] = 'S'; if(strcmp(field[6].GetString(), "enUS")) { // non-standard language forced memcpy(acct->Locale, field[6].GetString(), 4); acct->forcedLocale = true; } else acct->forcedLocale = false; acct->Muted = field[7].GetUInt32(); if ( (uint32)UNIXTIME > acct->Muted && acct->Muted != 0 && acct->Muted != 1) //1 = perm ban? { //Accounts should be unbanned once the date is past their set expiry date. acct->Muted= 0; DEBUG_LOG("AccountMgr","Account %s's mute has expired.", Username.c_str()); sLogonSQL->Execute("UPDATE accounts SET muted = 0 WHERE acct=%u",acct->AccountId); } // Convert username/password to uppercase. this is needed ;) HEARTHSTONE_TOUPPER(Username); HEARTHSTONE_TOUPPER(Password); if( m_encryptedPasswords ) { // prefer encrypted passwords over nonencrypted BigNumber bn; bn.SetHexStr( Password.c_str() ); if( bn.GetNumBytes() != 20 ) { // Someone probably has non-encrypted passwords in a server that's set to encrypted pws. hash.UpdateData((Username + ":" + Password)); hash.Finalize(); memcpy(acct->SrpHash, hash.GetDigest(), 20); // Make sure this doesn't happen again. BigNumber cnSave; cnSave.SetBinary( acct->SrpHash, 20); string hash = cnSave.AsHexStr(); DEBUG_LOG("AccountMgr", "Found account %s [%u] with invalid password format. Converting to encrypted password.", Username.c_str(), acct->AccountId); sLogonSQL->Execute("UPDATE accounts SET password = SHA1(CONCAT(UPPER(login), ':', UPPER(password))) WHERE acct = %u", acct->AccountId); } else { if ( Password.size() == 40 ) { if( bn.GetNumBytes() < 20 ) { memcpy(acct->SrpHash, bn.AsByteArray(), bn.GetNumBytes()); for (int n=bn.GetNumBytes(); n<=19; n++) acct->SrpHash[n] = (uint8)0; reverse_array(acct->SrpHash, 20); } else { memcpy(acct->SrpHash, bn.AsByteArray(), 20); reverse_array(acct->SrpHash, 20); } } } } else { // Prehash the I value. hash.UpdateData((Username + ":" + Password)); hash.Finalize(); memcpy(acct->SrpHash, hash.GetDigest(), 20); } AccountDatabase[Username] = acct; }
void fs_query_order(fs_query *q) { int conditions; for (conditions = 0; rasqal_query_get_order_condition(q->rq, conditions); conditions++); const int length = q->length; #ifdef DEBUG_ORDER printf("@@ ORDER (%d x %d)\n", conditions, length); #endif /* trap trivial cases */ if (conditions == 0 || length == 0) { return; } /* spot the case where we have ORDER BY ?x, saves evaluating expressions */ if (conditions == 1) { rasqal_expression *oe = rasqal_query_get_order_condition(q->rq, 0); if ((oe->op == RASQAL_EXPR_ORDER_COND_ASC || oe->op == RASQAL_EXPR_ORDER_COND_DESC) && oe->arg1->op == RASQAL_EXPR_LITERAL && oe->arg1->literal->type == RASQAL_LITERAL_VARIABLE) { long int col = (long int)oe->arg1->literal->value.variable->user_data; if (col == 0) { fs_error(LOG_CRIT, "missing column"); return; } int *ordering; if (!fs_sort_column(q, q->bt, col, &ordering)) { if (oe->op == RASQAL_EXPR_ORDER_COND_DESC) { reverse_array(ordering, q->bt[col].vals->length); } q->ordering = ordering; return; } } } struct order_row *orows = malloc(sizeof(struct order_row) * length); fs_value *ordervals = malloc(length * conditions * sizeof(fs_value)); for (int i=0; i<length; i++) { for (int j=0; j<conditions; j++) { ordervals[i * conditions + j] = fs_expression_eval(q, i, 0, rasqal_query_get_order_condition(q->rq, j)); #ifdef DEBUG_ORDER printf("@@ ORDER VAL (%d, %d) = ", i, j); fs_value_print(ordervals[i * conditions + j]); printf("\n"); #endif } orows[i].row = i; orows[i].width = conditions; orows[i].vals = ordervals + (i * conditions); } qsort(orows, length, sizeof(struct order_row), orow_compare); int *ordering = malloc(sizeof(int) * length); for (int i=0; i<length; i++) { ordering[i] = orows[i].row; } #ifdef DEBUG_ORDER printf("Output order:\n"); for (int i=0; i<length; i++) { printf("output row %d row %d\n", i, ordering[i]); } #endif q->ordering = ordering; free(ordervals); free(orows); }
static int aes_config_key( unsigned char * cipher_key) { unsigned int * ptmpkey; #ifdef _REVERSE_BYTE_SEQ_ unsigned char key_char[32]; #endif #ifdef DEBUG unsigned int stavalue; #endif #ifdef _REVERSE_BYTE_SEQ_ if (reverse_array( cipher_key, &key_char[0]) != 0 ) { printf("aes_config_key reverse_array 1 error!\n"); return -1; } if ( reverse_array( cipher_key+8, &key_char[8]) != 0 ) { printf("aes_config_key reverse_array 2 error!\n"); return -1; } ptmpkey=(unsigned int *) key_char; #else ptmpkey=(unsigned int *) cipher_key; #endif if ( -1==require_loop()) { return -1; } #ifndef _REVERSE_BYTE_SEQ_ //not reverse WRITE_REGISTER_ULONG(CIPHER_KEY1_OFFSET,*ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY2_OFFSET,*ptmpkey++); #endif if (KEY_LENGTH_128_1==( ctrl_word & KEY_LENGTH_MASK) || KEY_LENGTH_128_2 ==( ctrl_word & KEY_LENGTH_MASK)) { WRITE_REGISTER_ULONG(CIPHER_KEY3_OFFSET,*ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY4_OFFSET,*ptmpkey++); } else if (KEY_LENGTH_192==( ctrl_word &KEY_LENGTH_MASK)) { #ifdef _REVERSE_BYTE_SEQ_ reverse_array( cipher_key+16, &key_char[16]); WRITE_REGISTER_ULONG(CIPHER_KEY5_OFFSET, *ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY6_OFFSET, *ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY3_OFFSET, *ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY4_OFFSET,*ptmpkey++); #else WRITE_REGISTER_ULONG(CIPHER_KEY3_OFFSET, *ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY4_OFFSET, *ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY5_OFFSET, *ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY6_OFFSET,*ptmpkey++); #endif } else //key length 256 { #ifdef _REVERSE_BYTE_SEQ_ reverse_array( cipher_key+16, &key_char[16]); reverse_array( cipher_key+24, &key_char[24]); WRITE_REGISTER_ULONG(CIPHER_KEY7_OFFSET,*ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY8_OFFSET,*ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY5_OFFSET,*ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY6_OFFSET,*ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY3_OFFSET,*ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY4_OFFSET,*ptmpkey++); #else WRITE_REGISTER_ULONG(CIPHER_KEY3_OFFSET,*ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY4_OFFSET,*ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY5_OFFSET,*ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY6_OFFSET,*ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY7_OFFSET,*ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY8_OFFSET,*ptmpkey); #endif } #ifdef _REVERSE_BYTE_SEQ_ WRITE_REGISTER_ULONG(CIPHER_KEY1_OFFSET,*ptmpkey++); WRITE_REGISTER_ULONG(CIPHER_KEY2_OFFSET,*ptmpkey); #endif #ifdef DEBUG READ_REGISTER_ULONG(CIPHER_KEY1_OFFSET,stavalue); printf("key1:%x\n",stavalue); READ_REGISTER_ULONG(CIPHER_KEY2_OFFSET,stavalue); printf("key2:%x\n",stavalue); READ_REGISTER_ULONG(CIPHER_KEY3_OFFSET,stavalue); printf("key3:%x\n",stavalue); READ_REGISTER_ULONG(CIPHER_KEY4_OFFSET,stavalue); printf("key4:%x\n",stavalue); READ_REGISTER_ULONG(CIPHER_KEY5_OFFSET,stavalue); printf("key5:%x\n",stavalue); READ_REGISTER_ULONG(CIPHER_KEY6_OFFSET,stavalue); printf("key6:%x\n",stavalue); READ_REGISTER_ULONG(CIPHER_KEY7_OFFSET,stavalue); printf("key7:%x\n",stavalue); READ_REGISTER_ULONG(CIPHER_KEY8_OFFSET,stavalue); printf("key8:%x\n",stavalue); #endif return 0; }
void test_odd(){ int a[] = {1, 3, 5, 7, 9}; reverse_array(a, 5); show_array(a, 5); }
void AccountMgr::AddAccount(Field* field) { Account* acct = new Account; Sha1Hash hash; std::string Username = field[1].GetString(); std::string EncryptedPassword = field[2].GetString(); acct->AccountId = field[0].GetUInt32(); acct->AccountFlags = field[3].GetUInt8(); acct->Banned = field[4].GetUInt32(); if((uint32)UNIXTIME > acct->Banned && acct->Banned != 0 && acct->Banned != 1) //1 = perm ban? { acct->Banned = 0; sLogonSQL->Execute("UPDATE accounts SET banned = 0 WHERE id = %u", acct->AccountId); } acct->Locale[0] = 'e'; acct->Locale[1] = 'n'; acct->Locale[2] = 'U'; acct->Locale[3] = 'S'; if(strcmp(field[5].GetString(), "enUS")) { // non-standard language forced memcpy(acct->Locale, field[5].GetString(), 4); acct->forcedLocale = true; } else acct->forcedLocale = false; acct->Muted = field[6].GetUInt32(); if((uint32)UNIXTIME > acct->Muted && acct->Muted != 0 && acct->Muted != 1) //1 = perm ban? { acct->Muted = 0; sLogonSQL->Execute("UPDATE accounts SET muted = 0 WHERE id = %u", acct->AccountId); } // Convert username to uppercase. this is needed ;) Util::StringToUpperCase(Username); // prefer encrypted passwords over nonencrypted if(EncryptedPassword.size() > 0) { if(EncryptedPassword.size() == 40) { BigNumber bn; bn.SetHexStr(EncryptedPassword.c_str()); if(bn.GetNumBytes() < 20) { // Hacky fix memcpy(acct->SrpHash, bn.AsByteArray(), bn.GetNumBytes()); for(int n = bn.GetNumBytes(); n <= 19; n++) acct->SrpHash[n] = (uint8)0; reverse_array(acct->SrpHash, 20); } else { memcpy(acct->SrpHash, bn.AsByteArray(), 20); reverse_array(acct->SrpHash, 20); } } else { LOG_ERROR("Account `%s` has incorrect number of bytes in encrypted password! Disabling.", Username.c_str()); memset(acct->SrpHash, 0, 20); } } else { // This should never happen... LOG_ERROR("Account `%s` has no encrypted password!", Username.c_str()); } AccountDatabase[Username] = acct; }
void AccountMgr::UpdateAccount(Account* acct, Field* field) { uint32 id = field[0].GetUInt32(); Sha1Hash hash; std::string Username = field[1].GetString(); std::string EncryptedPassword = field[2].GetString(); if(id != acct->AccountId) { LOG_ERROR(" >> deleting duplicate account %u [%s]...", id, Username.c_str()); sLogonSQL->Execute("DELETE FROM accounts WHERE id = %u", id); return; } acct->AccountId = field[0].GetUInt32(); acct->AccountFlags = field[3].GetUInt8(); acct->Banned = field[4].GetUInt32(); if((uint32)UNIXTIME > acct->Banned && acct->Banned != 0 && acct->Banned != 1) //1 = perm ban? { //Accounts should be unbanned once the date is past their set expiry date. acct->Banned = 0; LOG_DEBUG("Account %s's ban has expired.", acct->UsernamePtr->c_str()); sLogonSQL->Execute("UPDATE accounts SET banned = 0 WHERE id = %u", acct->AccountId); } if(strcmp(field[5].GetString(), "enUS")) { // non-standard language forced memcpy(acct->Locale, field[5].GetString(), 4); acct->forcedLocale = true; } else acct->forcedLocale = false; acct->Muted = field[6].GetUInt32(); if((uint32)UNIXTIME > acct->Muted && acct->Muted != 0 && acct->Muted != 1) //1 = perm ban? { //Accounts should be unbanned once the date is past their set expiry date. acct->Muted = 0; LOG_DEBUG("Account %s's mute has expired.", acct->UsernamePtr->c_str()); sLogonSQL->Execute("UPDATE accounts SET muted = 0 WHERE id = %u", acct->AccountId); } // Convert username to uppercase. this is needed ;) Util::StringToUpperCase(Username); // prefer encrypted passwords over nonencrypted if(EncryptedPassword.size() > 0) { if(EncryptedPassword.size() == 40) { BigNumber bn; bn.SetHexStr(EncryptedPassword.c_str()); if(bn.GetNumBytes() < 20) { // Hacky fix memcpy(acct->SrpHash, bn.AsByteArray(), bn.GetNumBytes()); for(int n = bn.GetNumBytes(); n <= 19; n++) acct->SrpHash[n] = (uint8)0; reverse_array(acct->SrpHash, 20); } else { memcpy(acct->SrpHash, bn.AsByteArray(), 20); reverse_array(acct->SrpHash, 20); } } else { LOG_ERROR("Account `%s` has incorrect number of bytes in encrypted password! Disabling.", Username.c_str()); memset(acct->SrpHash, 0, 20); } } else { // This should never happen... LOG_ERROR("Account `%s` has no encrypted password!", Username.c_str()); } }
void swap(){ int i; int par[2]; char p; start: printf("switch integer or character to be done,integer input 1 \ and character input 2,and array input 3,exit input 4\n"); scanf("%d",&i); switch(i){ case 1: goto integer; break; case 2: goto character; break; case 3: goto array; break; case 4: goto end; break; default: printf("input error"); goto start; } character: printf("input two characters\n"); i = 0; while(i<2){ p = getchar(); if(p != ',' && p != ' ' && p != 10){ par[i] = p - '\0';//or par[i] = (int)p i++; } } goto do_char; integer: printf("input two integers\n"); scanf("%d",&par[0]); scanf("%d",&par[1]); goto do_int; array: printf("input 1 for int and 2 for char array\n"); int j,length,z; int array[100]; int k; scanf("%d",&j); switch(j){ case 1: printf("enter the length of array\n"); scanf("%d",&length); printf("input for array\n"); for(k=0;k<length;k++){ scanf("%d",&array[k]); //printf("%d\n",array[k]); } goto do_array_int; break; case 2: printf("enter the length of array\n"); scanf("%d",&length); printf("input for array\n"); char q; k=0; while(k<length){ q = getchar(); if(q != ',' && q != ' ' && q != 10){ array[k] = q - '\0';//or par[i] = (int)p k++; } } goto do_array_char; break; default: printf("input error"); goto array; break; } goto array; do_char: inplace_swap(&par[0],&par[1]); printf("%c %c\n",par[0],par[1]); goto start; do_int: inplace_swap(&par[0],&par[1]); printf("%d %d\n",par[0],par[1]); goto start; do_array_int: reverse_array(array,length); for(z=0;z<length;z++) printf("%d ",array[z]); printf("\n"); goto start; do_array_char: reverse_array(array,length); for(z=0;z<length;z++) printf("%c ",array[z]+'\0'); printf("\n"); goto start; end: printf("good bye\n"); }
void test_even(){ int a[] = {1, 3, 5, 7, 9, 11}; reverse_array(a, 6); show_array(a, 6); }
/* *to set the ivin value to the register, no matter it is in des,3des,aes. * ecb and ctr mode don't need to set this value */ static int aes_config_iv(unsigned char * ivin) { unsigned int * ivin_tmp; #ifdef _REVERSE_BYTE_SEQ_ unsigned char ivin_char[16]; #endif #ifdef DEBUG unsigned int stavalue; #endif #ifdef _REVERSE_BYTE_SEQ_ reverse_array( ivin, &ivin_char[0]); //reverse_8char test Ok reverse_array( ivin+8, &ivin_char[8]); ivin_tmp= (unsigned int *) ivin_char; #else ivin_tmp= (unsigned int *) ivin; #endif if(NULL==ivin) { printf("the ivin pointer is null!\n"); return -1; } if(-1==require_loop()) { printf("the busy signal is always 1!\n"); return -1; } if( ECB_MODE==(ctrl_word & AES_MODE_MASK )) //not complete { printf(" ecb mode can't be set ivin!\n"); return -1; } // if the alg is AES, not DES or 3DES #ifdef _REVERSE_BYTE_SEQ_ WRITE_REGISTER_ULONG(CIPHER_IVIN3_OFFSET,*ivin_tmp++); WRITE_REGISTER_ULONG(CIPHER_IVIN4_OFFSET,*ivin_tmp++); WRITE_REGISTER_ULONG(CIPHER_IVIN1_OFFSET,*ivin_tmp++); WRITE_REGISTER_ULONG(CIPHER_IVIN2_OFFSET,*ivin_tmp); #else WRITE_REGISTER_ULONG(CIPHER_IVIN1_OFFSET,*ivin_tmp++); WRITE_REGISTER_ULONG(CIPHER_IVIN2_OFFSET,*ivin_tmp++); WRITE_REGISTER_ULONG(CIPHER_IVIN3_OFFSET,*ivin_tmp++); WRITE_REGISTER_ULONG(CIPHER_IVIN4_OFFSET,*ivin_tmp); #endif #ifdef DEBUG READ_REGISTER_ULONG(CIPHER_IVIN1_OFFSET,stavalue); printf("ivin1:%x\n",stavalue); READ_REGISTER_ULONG(CIPHER_IVIN2_OFFSET,stavalue); printf("ivin2:%x\n",stavalue); READ_REGISTER_ULONG(CIPHER_IVIN3_OFFSET,stavalue); printf("ivin3:%x\n",stavalue); READ_REGISTER_ULONG(CIPHER_IVIN4_OFFSET,stavalue); printf("ivin4:%x\n",stavalue); #endif return 0; }
int get_index_of_tmrca(const char *treestr, const char *name_a, const char *name_b) { int insertion_list_a[MAX_INDEX_LIST_NUM], insertion_list_b[MAX_INDEX_LIST_NUM]; int list_num_a, list_num_b; size_t name_a_index, name_b_index; size_t treelen; int shorter_list_num; int i; int index_of_tmrca; char *blank_str; index_of_tmrca = -1; treelen = strlen(treestr); name_a_index = get_index_of_substring(treestr, name_a); name_b_index = get_index_of_substring(treestr, name_b); // printf("index a: %d, index b: %d\n", name_a_index, name_b_index); // get insertion list for each of the two species get_insertion_list(treestr, insertion_list_a, &list_num_a, name_a_index); get_insertion_list(treestr, insertion_list_b, &list_num_b, name_b_index); // reverse array for finding the first common value in arrays reverse_array(insertion_list_a, 0, list_num_a - 1); reverse_array(insertion_list_b, 0, list_num_b - 1); print_array(insertion_list_a, list_num_a); print_array(insertion_list_b, list_num_b); // find longer insertiton list shorter_list_num = list_num_a < list_num_b ? \ list_num_a : list_num_b; // find last common value in two reversed arrays // [69, 37, 25, 14, 3] // | // [69, 37, 25, 12] for (i = 0; i < shorter_list_num; i++) { if (i == shorter_list_num - 1 && insertion_list_a[i] == insertion_list_b[i]) { index_of_tmrca = insertion_list_a[i]; break; } // printf("%d | %d\n", insertion_list_a[i], insertion_list_b[i]); if (insertion_list_a[i] != insertion_list_b[i]) { index_of_tmrca = insertion_list_a[i - 1]; break; } } printf("[Common]: %d\n", index_of_tmrca); if ((index_of_tmrca < CALI_DISPLAY_HALF_WIDTH) && \ (treelen - index_of_tmrca >= CALI_DISPLAY_HALF_WIDTH)) { gen_blank_str(&blank_str, CALI_DISPLAY_HALF_WIDTH - index_of_tmrca); printf("%s\n", blank_str); printf("\n[Insert]: %s%s%s\n", blank_str, sliced_string(treestr, 0, index_of_tmrca), sliced_string(treestr, index_of_tmrca, index_of_tmrca + CALI_DISPLAY_HALF_WIDTH)); } else if (index_of_tmrca >= CALI_DISPLAY_HALF_WIDTH && \ treelen - index_of_tmrca < CALI_DISPLAY_HALF_WIDTH) { printf("\n[Insert]: %s\n", sliced_string(treestr, index_of_tmrca - CALI_DISPLAY_HALF_WIDTH, treelen)); } else if (index_of_tmrca < CALI_DISPLAY_HALF_WIDTH && \ treelen - index_of_tmrca < CALI_DISPLAY_HALF_WIDTH) { gen_blank_str(&blank_str, CALI_DISPLAY_HALF_WIDTH - index_of_tmrca); printf("\n[Insert]: %s%s\n", blank_str, sliced_string(treestr, 0, treelen)); } else { printf("\n[Insert]: %s\n", sliced_string(treestr, index_of_tmrca - CALI_DISPLAY_HALF_WIDTH, index_of_tmrca + CALI_DISPLAY_HALF_WIDTH)); } gen_blank_str(&blank_str, CALI_DISPLAY_HALF_WIDTH - 3); printf("[Insert]: %s->||<-\n", blank_str); gen_blank_str(&blank_str, CALI_DISPLAY_HALF_WIDTH - 5); printf("[Insert]: %sInsert Here\n", blank_str); // printf("Index of tMRCA: %d\n", index_of_tmrca); return index_of_tmrca; }
/* * we here set the pplaintext to the register */ static int aes_config_plaintext(unsigned char * pplaintext) { unsigned int * pplaintext_tmp; #ifdef _REVERSE_BYTE_SEQ_ unsigned char plain_char[16]; #endif #ifdef DEBUG unsigned int stavalue; #endif if( NULL == pplaintext ) { printf("the pplaintext pointer is null!\n"); return -1; } #ifdef _REVERSE_BYTE_SEQ_ if ( ((ctrl_word & 0x30) == 0) || ((ctrl_word & 0x30) == 0x30) ) { reverse_array( pplaintext, &plain_char[0]); reverse_array( pplaintext+8, &plain_char[8]); pplaintext_tmp = (unsigned int*) (plain_char); } else { pplaintext_tmp = (unsigned int *)pplaintext; } #else pplaintext_tmp = (unsigned int *)pplaintext; #endif if( -1 == require_loop() ) { printf("the busy signal is always true, aes_config_plaintext fail!\n"); return -1; } #ifdef _REVERSE_BYTE_SEQ_ if ( ((ctrl_word & 0x30) == 0) || ((ctrl_word & 0x30) == 0x30) ) { WRITE_REGISTER_ULONG(CIPHER_DIN3_OFFSET, *pplaintext_tmp++ ); WRITE_REGISTER_ULONG(CIPHER_DIN4_OFFSET, *pplaintext_tmp++ ); WRITE_REGISTER_ULONG(CIPHER_DIN1_OFFSET, *pplaintext_tmp++ ); WRITE_REGISTER_ULONG(CIPHER_DIN2_OFFSET, *pplaintext_tmp ); } else { WRITE_REGISTER_ULONG(CIPHER_DIN1_OFFSET, *pplaintext_tmp++ ); WRITE_REGISTER_ULONG(CIPHER_DIN2_OFFSET, *pplaintext_tmp++ ); WRITE_REGISTER_ULONG(CIPHER_DIN3_OFFSET, *pplaintext_tmp++ ); WRITE_REGISTER_ULONG(CIPHER_DIN4_OFFSET, *pplaintext_tmp ); } #else WRITE_REGISTER_ULONG(CIPHER_DIN1_OFFSET, *pplaintext_tmp++ ); WRITE_REGISTER_ULONG(CIPHER_DIN2_OFFSET, *pplaintext_tmp++ ); WRITE_REGISTER_ULONG(CIPHER_DIN3_OFFSET, *pplaintext_tmp++ ); WRITE_REGISTER_ULONG(CIPHER_DIN4_OFFSET, *pplaintext_tmp ); #endif #ifdef DEBUG if(-1==require_loop()) { printf("the busy signal is always 1!\n"); return -1; } READ_REGISTER_ULONG(CIPHER_DIN1_OFFSET,stavalue); printf("pplaintext1:0x%x\n",stavalue); READ_REGISTER_ULONG(CIPHER_DIN2_OFFSET,stavalue); printf("pplaintext2:0x%x\n",stavalue); READ_REGISTER_ULONG(CIPHER_DIN3_OFFSET,stavalue); printf("pplaintext3:0x%x\n",stavalue); READ_REGISTER_ULONG(CIPHER_DIN4_OFFSET,stavalue); printf("pplaintext4:0x%x\n",stavalue); READ_REGISTER_ULONG(CIPHER_CTRL_OFFSET,stavalue); printf("ctrl_word is :0x%x\n", stavalue); #endif return 0; }