1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| int __cdecl main(int argc, const char **argv, const char **envp) { ssignal(14, timeout); alarm(60); puts("=== Welcome to SECPROG calculator ==="); fflush(stdout); calc(); return puts("Merry Christmas!"); } unsigned int calc() { int nums[101]; char expr[1024]; unsigned int canary;
canary = __readgsdword(0x14u); while ( 1 ) { bzero(expr, 0x400u); if ( !get_expr(expr, 1024) ) break; init_pool(nums); if ( parse_expr((int)expr, nums) ) { printf("%d\n", nums[nums[0]]); fflush(stdout); } } return __readgsdword(0x14u) ^ canary; } int __cdecl get_expr(int expr, int _1024) { int v2; char v4; int v5;
v5 = 0; while ( v5 < _1024 && read(0, &v4, 1) != -1 && v4 != 10 ) { if ( v4 == '+' || v4 == '-' || v4 == '*' || v4 == '/' || v4 == '%' || v4 > '/' && v4 <= '9' ) { v2 = v5++; *(_BYTE *)(expr + v2) = v4; } } *(_BYTE *)(v5 + expr) = 0; return v5; } int __cdecl parse_expr(int expr, _DWORD *nums) { int v3; int prev_num_temp; int i; int seqopr; int num_len; char *prev_nums_str; int prev_nums; char operator[100]; unsigned int v11;
v11 = __readgsdword(0x14u); prev_num_temp = expr; seqopr = 0; bzero(operator, 0x64u); for ( i = 0; ; ++i ) { if ( (unsigned int)(*(char *)(i + expr) - 48) > 9 ) { num_len = i + expr - prev_num_temp; prev_nums_str = (char *)malloc(num_len + 1); memcpy(prev_nums_str, prev_num_temp, num_len); prev_nums_str[num_len] = 0; if ( !strcmp(prev_nums_str, "0") ) { puts("prevent division by zero"); fflush(stdout); return 0; } prev_nums = atoi(prev_nums_str); if ( prev_nums > 0 ) { v3 = (*nums)++; nums[v3 + 1] = prev_nums; } if ( *(_BYTE *)(i + expr) && (unsigned int)(*(char *)(i + 1 + expr) - 48) > 9 ) { puts("expression error!"); fflush(stdout); return 0; } prev_num_temp = i + 1 + expr; if ( operator[seqopr] ) { switch ( *(_BYTE *)(i + expr) ) { case '%': case '*': case '/': if ( operator[seqopr] != '+' && operator[seqopr] != '-' ) { eval(nums, operator[seqopr]); operator[seqopr] = *(_BYTE *)(i + expr); } else operator[++seqopr] = *(_BYTE *)(i + expr); break; case '+': case '-': default: eval(nums, operator[seqopr--]); break; } } else { operator[seqopr] = *(_BYTE *)(i + expr); } if ( !*(_BYTE *)(i + expr) ) break; } } while ( seqopr >= 0 ) eval(nums, operator[seqopr--]); return 1; } _DWORD *__cdecl init_pool(_DWORD *nums) { _DWORD *result; int i;
result = nums; *nums = 0; for ( i = 0; i <= 99; ++i ) { result = nums; nums[i + 1] = 0; } return result; } _DWORD *__cdecl eval(_DWORD *a1, char a2) { _DWORD *result;
if ( a2 == '+' ) { a1[*a1 - 1] += a1[*a1]; } else if ( a2 > '+' ) { if ( a2 == 45 ) { a1[*a1 - 1] -= a1[*a1]; } else if ( a2 == '/' ) { a1[*a1 - 1] /= (int)a1[*a1]; } } else if ( a2 == '*' ) { a1[*a1 - 1] *= a1[*a1]; } result = a1; --*a1; return result; }
|