Пример #1
0
int sperateCMD(char* in){
	int i=0;
	int len = strlen(in);
	/* flag to determine if we should skip the space in 
	   a string */
	int findQuote = 0;
	/* Stack to valid the quotes pair */
	Stack	stk;
	Stack_Init(&stk);

	for(; i<len; i++){
		if(in[i] == ' ' && !findQuote)
			in[i]='\0';
		if( in[i] == '\'' || in[i] == '\"' ){

			if( stk.size > 0 && Stack_Top(&stk) == in[i])
				Stack_Pop(&stk);
			else
				Stack_Push(&stk,in[i]);
			findQuote++;
			if(stk.size == 0)
				findQuote = 0;
			in[i] = '\0';
		}
	}
	return len;
}
Пример #2
0
void CODE_BYTE_MEM_GET(Stack xxx)
{
	DWORD reg = NULL;
	Stack_Pop(&reg, xxx);
	reg = *(BYTE*)reg;
	Stack_Push(reg, xxx);
}
Пример #3
0
static inline void IRQ()
{
    Stack_Push_Address(CPU.PC);
    Stack_Push(Status_Read());
    CPU.I = 1;
    CPU.PC = (Memory_CPU_Read(0xFFFF) << 8) | Memory_CPU_Read(0xFFFE);
}
Пример #4
0
void CODE_STRLEN_CALL(Stack xxx)
{
	DWORD reg = NULL;
	DWORD STRLEN_RET = 0;
	Stack_Pop(&reg, xxx);
	STRLEN_RET = strlen((char*)(reg));
	Stack_Push(STRLEN_RET, xxx);
}
Пример #5
0
void Marker_markObject(Heap *heap, Stack *stack, Object *object) {
    assert(!Object_IsMarked(&object->header));
    assert(Object_Size(&object->header) != 0);
    Object_Mark(object);
    if (!overflow) {
        overflow = Stack_Push(stack, object);
    }
}
Пример #6
0
void quickSort(int *array, int left, int right)
{
	if(right - left <= 0)
        return;

	int p = quickSortPartition(array, left, right);

	if(right-left <= GRANULARITY )
	{
		quickSort( array , left , p - 1);
		quickSort( array , p+1  , right);
	}
	else
	{
		Stack_Push( &S , left , p - 1);
		Stack_Push( &S , p+1  , right);
	}
}
Пример #7
0
int main()
{
	Stack_Init(&stack);

	Stack_Size(&stack);

	Stack_Push(&stack, 32);
	Stack_Push(&stack, 23);
	Stack_Push(&stack, 49);
	Stack_Push(&stack, 68);

	Stack_Peek(&stack);
	Stack_Pop(&stack);
	Stack_Pop(&stack);
	Stack_Pop(&stack);
	Stack_Pop(&stack);

	_getch();
}
Пример #8
0
/// <summary>
/// Evaluate_next_operator for the next operator.
/// </summary>
/// <param name="operands">The operands.</param>
/// <param name="operators">The operators.</param>
bool evaluate_next_operator(struct Stack* operands, struct Stack* operators) {
    char op = Stack_Top(operators);
    Stack_Pop(operators);

    if (op == '_') { // deal with unary '-' differently
        int val = -Stack_Top(operands);
        Stack_Pop(operands);
        Stack_Push(operands, val);
        return;
    }

    int opr2 = Stack_Top(operands);
    Stack_Pop(operands);
    int opr1 = Stack_Top(operands);
    Stack_Pop(operands);

    switch (op) {
        case '*':
            Stack_Push(operands, opr1 * opr2);
            break;
        case '/':
            if (opr2 == 0) {
                syslog(LOG_ERR, "Cannot divide by 0");
                return DIVIDED_BY_0;
            }
            Stack_Push(operands, opr1 / opr2);
            break;
        case '+':
            Stack_Push(operands, opr1 + opr2);
            break;
        case '-':
            Stack_Push(operands, opr1 - opr2);
            break;
    }
    return false;
}
Пример #9
0
static void execute(void)
{
	struct _Address Destination;
	unsigned int DValue;
	JSR_Instr Instr;
	Memory_RetrWordFromPC(&Instr.Code);
	
	if(!EA_GetFromPC(&Destination, 32, Instr.Bits.EAMode, Instr.Bits.EARegister)) return;
	EA_GetEA(&DValue, &Destination);

	Stack_Push(32,memory_core.pc);

	/* Set the new PC */
	memory_core.pc=DValue;

	/* Condition codes are not affected */

	
	cycle(JSRTime[cycle_EA(Instr.Bits.EARegister,Instr.Bits.EAMode)]);
	
	return;
}
Пример #10
0
void SortArr(int *numbers)
{
	Stack_Init(&S);
	Stack_Push(&S,0,N);
	
	#pragma omp parallel 
	{
		#pragma omp single
		{
			while(Stack_Size(&S)>0)
			{
				struct LR data= Stack_Pop(&S);
				int l= data.L;
				int r= data.R;

				#pragma omp taskwait
				quickSort(numbers,l,r);
			}
			
		}
	}
	
}
Пример #11
0
void CODE_PUSH_NUM(Stack xxx, PVMDATA vm_data)
{
	Stack_Push(*(vm_data->DATA), xxx);
	(vm_data->DATA)++;
}
Пример #12
0
void CODE_PUSH(Stack xxx, DWORD DATA)
{
	Stack_Push(DATA, xxx);
}
Пример #13
0
void Car_StackPush(void) {
	CarListInit();
	Stack_Push(CarList, (void *)Car_Create());
}
Пример #14
0
int Stack_Test()
{
    #define INTERVAL printf ("________________________________________________\n");

    #define LEGEND \
        printf ("Now please choose what to do "\
                "with empty pre-determined non-allocated stack:\n");\
        printf ("Legend:\n");\
        printf ("1 - Construct stack\n");\
        printf ("2 - Push value into stack\n");\
        printf ("3 - Pop value out of the stack\n");\
        printf ("4 - Destruct stack\n");\
        printf ("5 - Print dump info\n");\
        printf ("6 - Reprint the legend\n");\
        printf ("0 - Exit the program\n");

    #define WHATTODO printf ("Enter your choice here: ")

    #define ASSERT( cond )\
    if (!cond)\
    {\
        printf ("Oops! Something's gone wrong within the assertion <%s>\n"\
                "Life <%s>\nLine <%d>\n"\
                "The program will be shut down soon.\n", #cond, __FILE__, __LINE__);\
        printf ("Stack before destruction:\n");\
        Stack_Dump (&stack);\
        Stack_Destruct (&stack);\
        printf ("Stack after destruction:\n");\
        Stack_Dump (&stack);\
        printf ("Shutting down the program.\n"\
                "Please send this error report to google and NASA.\n");\
        return -1;\
    }

    Stack_t stack = {};
    int mode = 0;
    int sloptimization = 0;
    double value_to_push = 0, value_popped = 0;
    int res_input = 0;
    clock_t t_start = 0, t_finish = 0;

    printf ("Do you want to use the optimized version of stack? (1/0): ");
    res_input = scanf ("%d", &sloptimization);

    assert (res_input); assert (sloptimization == 1 || sloptimization == 0);

    INTERVAL;
    LEGEND;

    while (1)
    {
        WHATTODO;
        res_input = scanf ("%d", &mode);

        assert (res_input); assert (mode >= 0); assert (mode <= 6);

        switch (mode)
        {
            case CMD_CONSTRUCT:
                t_start = clock();

                if (Stack_Error_Catch (Stack_Construct (&stack),
                "Constructing the stack..\n") == ERR) return ERR;

                t_finish = clock();
                printf ("Stack is successfully constructed!\n");
                printf ("Time elapsed: %lg\n", (double) ((double) t_finish - (double) t_start)/CLOCKS_PER_SEC);
                break;

            case CMD_PUSH:
                printf ("Enter integer value to push: ");
                res_input = scanf ("%lg", &value_to_push);
                assert (res_input); assert (value_to_push < INT_MAX);

                t_start = clock();

                if (sloptimization)
                {
                    if (Stack_Error_Catch (Stack_Perverted_Push (&stack, value_to_push),
                    "Pushing your value somehow smartly..\n") == ERR) return ERR;
                }
                else
                {
                    if (Stack_Error_Catch (Stack_Push (&stack, value_to_push),
                    "Pushing your value..\n") == ERR) return ERR;
                }

                t_finish = clock();
                printf ("Value pushed!\n");
                printf ("Time elapsed: %lg\n", (double) ((double) t_finish - (double) t_start)/CLOCKS_PER_SEC);
                break;

            case CMD_POP:
                t_start = clock();

                if (sloptimization)
                {
                    if (Stack_Error_Catch (Stack_Perverted_Pop (&stack, &value_popped),
                    "Getting value using innovational algorithms..\n") == ERR) return ERR;
                }
                else
                {
                    if (Stack_Error_Catch (Stack_Pop (&stack, &value_popped),
                    "Getting value..\n") == ERR) return ERR;
                }

                t_finish = clock();
                printf ("Value popped out of the stack: %lg\n", value_popped);
                printf ("Time elapsed: %lg\n", (double) ((double) t_finish - (double) t_start)/CLOCKS_PER_SEC);
                break;

            case CMD_DESTRUCT:
                t_start = clock();

                if (Stack_Error_Catch (Stack_Destruct (&stack),
                "Destroying stack..\n") == ERR) return ERR;

                t_finish = clock();
                printf ("Stack destroyed!\n");
                printf ("Time elapsed: %lg\n", (double) ((double) t_finish - (double) t_start)/CLOCKS_PER_SEC);
                break;

            case CMD_DUMP:
                printf ("Dump info:\n");
                t_start = clock();

                Stack_Dump (&stack);

                t_finish = clock();
                printf ("-----\n");
                printf ("Time elapsed: %lg\n", (double) ((double) t_finish - (double) t_start)/CLOCKS_PER_SEC);
                break;

            case CMD_LEGEND:
                INTERVAL;
                LEGEND;
                break;

            case CMD_END:
                printf ("Good bye!\n");
                if (Stack_Is_OK (&stack))
                {
                    printf ("You've forgot to destruct the stack.\n");
                    printf ("Then I'll do it for you.\n");
                    Stack_Error_Catch (Stack_Destruct (&stack),
                    "Destroying while exiting..\n");
                    printf ("Stack destroyed!\nGood bye!\n");
                }
                return OK;

            default:
                printf ("How you forced the unknown command to reach this point through asserts?!\n");
                break;
        }

        INTERVAL;
    }

    #undef WHATTODO
    #undef LEGEND
    #undef INTERVAL

    return OK;
}
Пример #15
0
void AsyncStack_Push(AsyncStack* self, void* value) {
	assert(self != NULL);
	SAL_Mutex_Acquire(self->Lock);
	Stack_Push(self->BaseStack, value);
	SAL_Mutex_Release(self->Lock);
}
Пример #16
0
static void decode_location(Dwarf_Locdesc *locationList, Dwarf_Signed listLength, long *offset, long *init_val, int *frameRel){

  /*Location Decoding Code from http://ns.dyninst.org/coverage/dyninstAPI/src/parseDwarf.C.gcov.html*/
  Stack *opStack = (Stack*)malloc(sizeof(Stack));
  Stack_Init(opStack);
  assert( listLength > 0 );
 
  if( listLength > 1 ) { fprintf( stderr, "Warning: more than one location, ignoring all but first.\n" ); }
 
  /* Initialize the stack. */
  if( init_val != NULL ) { Stack_Push(opStack, * init_val); }
 
  /* Variable can only become frame-relative by using the frame pointer operand. */
  if( frameRel != NULL ) { * frameRel = false; }
 
  /* Ignore all but the first location, for now. */
  Dwarf_Locdesc location = locationList[0];
  Dwarf_Loc * locations = location.ld_s;
  unsigned int i;
  for( i = 0; i < location.ld_cents; i++ ) {
    /* Handle the literals w/o 32 case statements. */
    if( DW_OP_lit0 <= locations[i].lr_atom && locations[i].lr_atom <= DW_OP_lit31 ) {
      //fprintf( stderr, "Pushing named constant: %d\n", locations[i].lr_atom - DW_OP_lit0 );
      Stack_Push(opStack, locations[i].lr_atom - DW_OP_lit0 );
      continue;
    }
 
    if( (DW_OP_breg0 <= locations[i].lr_atom && locations[i].lr_atom <= DW_OP_breg31) || locations[i].lr_atom == DW_OP_bregx ) {
      /* Offsets from the specified register.  Since we're not
         doing this at runtime, we can't do anything useful here. */
      fprintf( stderr, "Warning: location decode requires run-time information, giving up.\n" );
      *offset = -1;
      return;
    }
 
    if( (DW_OP_reg0 <= locations[i].lr_atom && locations[i].lr_atom <= DW_OP_reg31) || locations[i].lr_atom == DW_OP_regx ) {
      /* The variable resides in the particular register.  We can't do anything
         useful with this information yet. */
      fprintf( stderr, "Warning: location decode indicates register, giving up.\n" );
      *offset = -1;
      return;
    }
 
    switch( locations[i].lr_atom ) {
      case DW_OP_addr:
      case DW_OP_const1u:
      case DW_OP_const2u:
      case DW_OP_const4u:
      case DW_OP_const8u:
      case DW_OP_constu:
        // fprintf( stderr, "Pushing constant %lu\n", (unsigned long)locations[i].lr_number );
        Stack_Push(opStack, (Dwarf_Unsigned)locations[i].lr_number );
        break;
 
      case DW_OP_const1s:
      case DW_OP_const2s:
      case DW_OP_const4s:
      case DW_OP_const8s:
      case DW_OP_consts:
        // fprintf( stderr, "Pushing constant %ld\n", (signed long)(locations[i].lr_number) );
        Stack_Push(opStack, (Dwarf_Signed)(locations[i].lr_number) );
        break;
 
      case DW_OP_fbreg:
        /* We're only interested in offsets, so don't add the FP. */
        // fprintf( stderr, "Pushing FP offset %ld\n", (signed long)(locations[i].lr_number) );
        Stack_Push(opStack, (Dwarf_Signed)(locations[i].lr_number) );
        if( frameRel != NULL ) { * frameRel = true; }
        break;
 
      case DW_OP_dup:
        Stack_Push(opStack, Stack_Top(opStack) );
        break;
 
      case DW_OP_drop:
        Stack_Pop(opStack);
        break;
 
      case DW_OP_over: {
          long int first = Stack_Top(opStack); Stack_Pop(opStack);
          long int second = Stack_Top(opStack); Stack_Pop(opStack);
          Stack_Push(opStack, second ); Stack_Push(opStack, first ); Stack_Push(opStack, second );
        } break;
 
      case DW_OP_pick: {
        /* Duplicate the entry at index locations[i].lr_number. */
        Stack temp;
        Stack_Init(&temp);
        unsigned int i;
        for( i = 0; i < locations[i].lr_number; i++ ) {
          Stack_Push(&temp, Stack_Top(opStack) ); Stack_Pop(opStack);
        }
        long int dup = Stack_Top(opStack);
        for( i = 0; i < locations[i].lr_number; i++ ) {
          Stack_Push(opStack, Stack_Top(&temp) ); Stack_Pop(&temp);
        }
        Stack_Push(opStack, dup );
        } break;
 
      case DW_OP_swap: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first ); Stack_Push(opStack, second );
      } break;
 
      case DW_OP_rot: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        long int third = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first ); Stack_Push(opStack, third ); Stack_Push(opStack, second );
      } break;
 
      case DW_OP_deref:
      case DW_OP_deref_size:
      case DW_OP_xderef:
      case DW_OP_xderef_size:
        // fprintf( stderr, "Warning: location decode requires run-time information, giving up.\n" );
        * offset = -1;
        return;
 
      case DW_OP_abs: {
        long int top = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, abs( top ) );
        } break;
 
      case DW_OP_and: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second & first );
      } break;
                  
      case DW_OP_div: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second / first );
      } break;
 
      case DW_OP_minus: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second - first );
      } break;
 
      case DW_OP_mod: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second % first );
      } break;
 
      case DW_OP_mul: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second * first );
      } break;
 
      case DW_OP_neg: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first * (-1) );
      } break;
 
      case DW_OP_not: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, ~ first );
      } break;
 
      case DW_OP_or: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second | first );
      } break;
 
      case DW_OP_plus: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second + first );
      } break;
 
      case DW_OP_plus_uconst: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first + locations[i].lr_number );
      } break;
 
      case DW_OP_shl: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second << first );
      } break;
 
      case DW_OP_shr: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, (long int)((unsigned long)second >> (unsigned long)first) );
      } break;
 
      case DW_OP_shra: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second >> first );
      } break;
 
      case DW_OP_xor: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, second ^ first );
      } break;
 
      case DW_OP_le: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first <= second ? 1 : 0 );
      } break;
 
      case DW_OP_ge: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first >= second ? 1 : 0 );
      } break;
 
      case DW_OP_eq: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first == second ? 1 : 0 );
      } break;
 
      case DW_OP_lt: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first < second ? 1 : 0 );
      } break;
 
      case DW_OP_gt: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first > second ? 1 : 0 );
      } break;
 
      case DW_OP_ne: {
        long int first = Stack_Top(opStack); Stack_Pop(opStack);
        long int second = Stack_Top(opStack); Stack_Pop(opStack);
        Stack_Push(opStack, first != second ? 1 : 0 );
      } break;
 
      case DW_OP_bra:
        if( Stack_Top(opStack) == 0 ) { break; }
        Stack_Pop(opStack);
        break;/*Right?*/
      case DW_OP_skip: {
        int bytes = (int)(Dwarf_Signed)locations[i].lr_number;
        unsigned int target = locations[i].lr_offset + bytes;
 
        int j = i;
        if( bytes < 0 ) {
          for( j = i - 1; j >= 0; j-- ) {
            if( locations[j].lr_offset == target ) { break; }
          } /* end search backward */
        } else {
          for( j = i + 1; j < location.ld_cents; j ++ ) {
            if( locations[j].lr_offset == target ) { break; }
          } /* end search forward */
        } /* end if positive offset */
 
        /* Because i will be incremented the next time around the loop. */
        i = j - 1;
      } break;
 
      case DW_OP_piece:
        /* For multi-part variables, which we don't handle. */
        break;
 
      case DW_OP_nop:
        break;
 
      default:
        fprintf( stderr, "Unrecognized location opcode 0x%x, returning offset -1.\n", locations[i].lr_atom );
        *offset = -1;
        return;
    } /* end operand switch */
  } /* end iteration over Dwarf_Loc entries. */
 
  /* The top of the stack is the computed location. */
  //fprintf( stderr, "Location decoded: %ld\n", Stack_Top(opStack) );
  *offset = Stack_Top(opStack);
}  
Пример #17
0
/// <summary>
/// Evaluate_exps evaluates the specified expression.
/// </summary>
/// <param name="exp">The expression.</param>
/// <returns>The evaluated result</returns>
struct Result evaluate_expr(char* exp) {
    static const char* ods = "0123456789(";
    static const char* ops = "+-*/_"; // '_' is unary '-'
    struct Stack operands;
    struct Stack operators;
    Stack_Init(&operators);
    Stack_Init(&operands);

    removeSpaces(exp);
    findUnaryOperators(exp);

    //syslog(LOG_INFO, "parsing: %s", exp);

    int i;
    i = 0;
    while (exp[i] != '\0') {
        char c = exp[i++];
        if (strchr(ods, c) != NULL) { // if char == operand
            if (c == '(') {
                Stack_Push(&operators, c);
            } else {
                int iStart = i - 1;
                int iEnd = iStart;
                do {
                    c = exp[i++];
                } while (c != '\n' && isdigit(c));
                iEnd = --i;
                char num[MAX_DIGITS] = "";
                strncat(num, exp + iStart, iEnd - iStart);
                Stack_Push(&operands, atoi(num));
            }
        } else if (strchr(ops, c) != NULL) { // if char == operator
            int currentPres = getPrecendence(c);
            while (Stack_Size(&operators) > 0 &&
                   Stack_Size(&operands)  > 0 &&
                   currentPres < getPrecendence((char)Stack_Top(&operators))) {
                if (evaluate_next_operator(&operands, &operators) == DIVIDED_BY_0) {
                    return inf;
                }
            }
            Stack_Push(&operators, c);
        } else if (c == ')') {
            while (Stack_Top(&operators) != '(') {
                if (evaluate_next_operator(&operands, &operators) == DIVIDED_BY_0) {
                    return inf;
                }
            }
            Stack_Pop(&operators);
        }
    }

    while (Stack_Size(&operators) > 0) {
        if (evaluate_next_operator(&operands, &operators) == DIVIDED_BY_0) {
            return inf;
        }
    }

    int resultInt = Stack_Top(&operands);
    syslog(LOG_INFO, "Result: %d", resultInt);
    struct Result result = {resultInt, false};
    return result;
}
Пример #18
0
err_type archive_addFile(const char *archiveName, const char *fileName, const uint8_t compression) //!!!онкмше хлемю!!!
{
	FILE *arch, *oldArch;
	err_type result = ERR_NO;
	struct stat objInfo;
	Stack *dirs = NULL;
	char *tempArchName = NULL;
	char buffer[_MAX_PATH];
	FileName_FS outFileName;
	File_Header dirHead;

	if ((archiveName == NULL) || (fileName == NULL))
		return ERR_PARAM_INVALID;
	
	if ((result = _checkDirAccess(archiveName)) != ERR_NO)
		return result;

	_getFileDir(archiveName, buffer);
	if ((tempArchName = _tempnam(buffer, "sth")) == NULL)
		return ERR_WRITE_FAILED;

	if ((arch = fopen(tempArchName, "wb")) == NULL)
	{
		free(tempArchName);
		return ERR_WRITE_FAILED;
	}

	if ((oldArch = fopen(archiveName, "rb")) != NULL)
	{
		_splitpath(fileName, NULL, NULL, outFileName.shortName.fName, outFileName.shortName.ext);
		_makepath(buffer, NULL, NULL, outFileName.shortName.fName, outFileName.shortName.ext);
		result = _copyArchWithoutFile(arch, oldArch, buffer);
		fclose(oldArch);
	}
	else
	{
		result = arch_writeMarkerBlock(arch);
	}
		
	if (result != ERR_NO)
		goto finalization;

	/*if ((_fseeki64(arch, 0, SEEK_END)) != 0)
	{
		result = ERR_WRITE_FAILED;
		goto finalization;
	}*/

	if ((dirs = Stack_Create()) == NULL)
	{
		result = ERR_NOMEM;
		goto finalization;
	}

	if ((result = _getStat(fileName, &objInfo)) != ERR_NO)
		goto finalization;

	_splitpath(fileName, outFileName.fullName.drive, outFileName.fullName.dir, outFileName.fullName.fName, outFileName.fullName.ext);
	_splitpath(fileName, NULL, NULL, outFileName.shortName.fName, outFileName.shortName.ext);
	outFileName.shortName.drive[0] = 0;
	outFileName.shortName.dir[0] = 0;

	if ((objInfo.st_mode & S_IFREG) != 0)
	{
		result = _addFile(arch, &outFileName, &objInfo, compression);
	}
	else if ((objInfo.st_mode & S_IFDIR) != 0)
	{
		if ((result = Stack_Push(dirs, &outFileName)) != ERR_NO)
			goto finalization;
	}
	else
	{
		result = ERR_UNKNOWN_FILETYPE;
		goto finalization;
	}

	while (!Stack_IsEmpty(dirs))
	{
		WIN32_FIND_DATAA fileFindData;
		HANDLE hFind = INVALID_HANDLE_VALUE;

		Stack_Pop(dirs, &outFileName);

		_makepath(buffer, outFileName.fullName.drive, outFileName.fullName.dir, outFileName.fullName.fName, outFileName.fullName.ext);
		if ((result = _getStat(buffer, &objInfo)) != ERR_NO)
			goto finalization;
		objInfo.st_size = 0;
		_makepath(buffer, outFileName.shortName.drive, outFileName.shortName.dir, outFileName.shortName.fName, outFileName.shortName.ext);
		if ((result = _File_Header_fill(buffer, compression, &dirHead, &objInfo)) != ERR_NO)
			goto finalization;

		if ((result = File_Header_fwrite(arch, &dirHead)) != ERR_NO)
			goto finalization;

		_makepath(buffer, outFileName.fullName.drive, outFileName.fullName.dir, outFileName.fullName.fName, outFileName.fullName.ext);
		strcat(buffer, "\\*");

		if ((hFind = FindFirstFileA(buffer, &fileFindData)) == INVALID_HANDLE_VALUE)
		{
			result = ERR_UNKNOWN;
			goto finalization;
		}
		if (FindNextFileA(hFind, &fileFindData) == 0)
		{
			continue;
		}
		if (strcmp(fileFindData.cFileName, "..") == 0)
			if (FindNextFileA(hFind, &fileFindData) == 0)
				continue;

		strcat(strcat(strcat(outFileName.fullName.dir, outFileName.fullName.fName), outFileName.fullName.ext), "\\");
		strcat(strcat(strcat(outFileName.shortName.dir, outFileName.shortName.fName), outFileName.shortName.ext), "\\");
		do
		{
			_splitpath(fileFindData.cFileName, NULL, NULL, outFileName.fullName.fName, outFileName.fullName.ext);	
			_splitpath(fileFindData.cFileName, NULL, NULL, outFileName.shortName.fName, outFileName.shortName.ext);

			_makepath(buffer, outFileName.fullName.drive, outFileName.fullName.dir, outFileName.fullName.fName, outFileName.fullName.ext);
			if ((result = _getStat(buffer, &objInfo)) != ERR_NO)
				break;


			if ((objInfo.st_mode & S_IFREG) != 0)
			{
				if ((result = _addFile(arch, &outFileName, &objInfo, compression)) != ERR_NO)
					break;
			}
			else if ((objInfo.st_mode & S_IFDIR) != 0)
			{
				if ((result = Stack_Push(dirs, &outFileName)) != ERR_NO)
					break;
			}
			else
			{
				result = ERR_UNKNOWN_FILETYPE;
				break;
			}
		} 
		while (FindNextFileA(hFind, &fileFindData) != 0);
		FindClose(hFind);

		if (result != ERR_NO)
			goto finalization;
	}

finalization:
	fclose(arch);

	if (result == ERR_NO)
	{
		remove(archiveName);
		rename(tempArchName, archiveName);
	}
	else
	{
		remove(tempArchName);
	}

	free(tempArchName);

	Stack_Destroy(dirs);

	return result;
}
Пример #19
0
static char* gdi_convert_postfix_to_infix(const char* postfix)
{
	int i;
	int length;
	BOOL unary;
	wStack* stack;
	int al, bl, cl, dl;
	char* a, *b, *c, *d;
	bl = cl = dl = 0;
	stack = Stack_New(FALSE);
	length = strlen(postfix);

	for (i = 0; i < length; i++)
	{
		if ((postfix[i] == 'P') || (postfix[i] == 'D') || (postfix[i] == 'S'))
		{
			/* token is an operand, push on the stack */
			a = malloc(2);
			a[0] = postfix[i];
			a[1] = '\0';
			//printf("Operand: %s\n", a);
			Stack_Push(stack, a);
		}
		else
		{
			/* token is an operator */
			unary = FALSE;
			c = malloc(2);
			c[0] = postfix[i];
			c[1] = '\0';

			if (c[0] == 'a')
			{
				c[0] = '&';
			}
			else if (c[0] == 'o')
			{
				c[0] = '|';
			}
			else if (c[0] == 'n')
			{
				c[0] = '~';
				unary = TRUE;
			}
			else if (c[0] == 'x')
			{
				c[0] = '^';
			}
			else
			{
				printf("invalid operator: %c\n", c[0]);
			}

			//printf("Operator: %s\n", c);
			a = (char*) Stack_Pop(stack);

			if (unary)
				b = NULL;
			else
				b = (char*) Stack_Pop(stack);

			al = strlen(a);

			if (b)
				bl = strlen(b);

			cl = 1;
			dl = al + bl + cl + 3;
			d = malloc(dl + 1);
			sprintf_s(d, dl, "(%s%s%s)", b ? b : "", c, a);
			Stack_Push(stack, d);
			free(a);
			free(b);
			free(c);
		}
	}

	d = (char*) Stack_Pop(stack);
	Stack_Free(stack);
	return d;
}
Пример #20
0
NTSTATUS Wdm1DeviceControl(IN PDEVICE_OBJECT fdo,
	IN PIRP Irp)
{
	PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
	NTSTATUS status = STATUS_SUCCESS;
	ULONG BytesTxd = 0;

	ULONG ControlCode = IrpStack->Parameters.DeviceIoControl.IoControlCode;
	ULONG InputLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength;
	ULONG OutputLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength;

	DebugPrint("DeviceIoControl: Control code %x InputLength %d OutputLength %d",
		ControlCode, InputLength, OutputLength);

	// Get access to the shared buffer
	KIRQL irql;
	KeAcquireSpinLock(&BufferLock, &irql);
	switch (ControlCode)
	{
		///////	Zero Buffer
	case IOCTL_WDM1_ZERO_BUFFER:
		// Zero the buffer
		if (Buffer != NULL && BufferSize > 0)
			RtlZeroMemory(Buffer, BufferSize);
		break;

		///////	Remove Buffer
	case IOCTL_WDM1_REMOVE_BUFFER:
		if (Buffer != NULL)
		{
			ExFreePool(Buffer);
			Buffer = NULL;
			BufferSize = 0;
		}
		break;

		///////	Get Buffer Size as ULONG
	case IOCTL_WDM1_GET_BUFFER_SIZE:
		if (OutputLength < sizeof(ULONG))
			status = STATUS_INVALID_PARAMETER;
		else
		{
			BytesTxd = sizeof(ULONG);
			RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer, &BufferSize, sizeof(ULONG));
		}
		break;

		///////	Get Buffer
	case IOCTL_WDM1_GET_BUFFER:
		if (OutputLength > BufferSize)
			status = STATUS_INVALID_PARAMETER;
		else
		{
			BytesTxd = OutputLength;
			RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer, Buffer, BytesTxd);
		}
		break;

		///////	Get DateTime
	case IOCTL_WDM1_GET_BUILDTIME:
	{
		if (OutputLength < dateTimeSize){
			status = STATUS_INVALID_PARAMETER;
		}
		else {
			memset(dateTimeBuffer, 0, dateTimeSize);
			strcpy(dateTimeBuffer, __DATE__);
			strcat(dateTimeBuffer, " ");
			strcat(dateTimeBuffer, __TIME__);
			DebugPrint("DateTime: %s", dateTimeBuffer);
			BytesTxd = dateTimeSize;
			RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer, dateTimeBuffer, dateTimeSize);
		}
	}
		break;

		/////// ------------- RPN STACK --------------------
	case IOCTL_WDM1_RPN_PUSH:
	{
		int value = 0;
		RtlCopyMemory(&value, Irp->AssociatedIrp.SystemBuffer, 4); // 4 byte = 32bit
		BytesTxd = 4;
		if (!Stack_Push(&s, value)){
			status = STATUS_UNSUCCESSFUL;
			BytesTxd = 0;
		}
		DebugPrintStack();
	}
		break;

	case IOCTL_WDM1_RPN_POP:
	{
		if (OutputLength < 4) {
			status = STATUS_INVALID_PARAMETER;
		}
		else {
			int value = 0;
			if (Stack_Pop(&s, value)){
				BytesTxd = 4;
				RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer, &value, BytesTxd);
			}
			else {
				BytesTxd = 0;
				status = STATUS_UNSUCCESSFUL;
			}
		}
	}
		break;

	case IOCTL_WDM1_RPN_ADD:
		if (!RpnCalculator_Add(&s)){
			BytesTxd = 0;
			status = STATUS_UNSUCCESSFUL;
		}
		break;

	case IOCTL_WDM1_RPN_SUB:
		if (!RpnCalculator_Substract(&s)){
			BytesTxd = 0;
			status = STATUS_UNSUCCESSFUL;
		}
		break;

	case IOCTL_WDM1_RPN_MULT:
		if (!RpnCalculator_Multiply(&s)){
			BytesTxd = 0;
			status = STATUS_UNSUCCESSFUL;
		}
		break;

	case IOCTL_WDM1_RPN_DIV:
		if (!RpnCalculator_Divide(&s)){
			BytesTxd = 0;
			status = STATUS_UNSUCCESSFUL;
		}
		break;

	case IOCTL_WDM1_RPN_GETDIVREST:
		if (!RpnCalculator_Modulo(&s)){
			BytesTxd = 0;
			status = STATUS_UNSUCCESSFUL;
		}
		break;

	case IOCTL_WDM1_RPN_DUPLI:
		if (!Stack_Dup(&s)){
			BytesTxd = 0;
			status = STATUS_UNSUCCESSFUL;
		}
		DebugPrintStack();
		break;

		///////	Invalid request
	default:
		status = STATUS_INVALID_DEVICE_REQUEST;
	}
	// Release shared buffer
	KeReleaseSpinLock(&BufferLock, irql);

	DebugPrint("DeviceIoControl: %d bytes written", (int)BytesTxd);

	// Complete IRP
	return CompleteIrp(Irp, status, BytesTxd);
}