static void test_string_repeat() {
	char *string = string_repeat('a', 10);

	CU_ASSERT_STRING_EQUAL(string, "aaaaaaaaaa");
	CU_ASSERT_EQUAL(strlen(string), 10);

	free(string);
}
char* completarBloque(char* bloqueACompletar, int tamanioDeBloque){

	int cantACompletar = tamanioDeBloque - string_length(bloqueACompletar);
	char* aux = string_duplicate(bloqueACompletar);

	string_append(&aux,string_repeat(CARACTER_VACIO,cantACompletar));

	return aux;
}
int main(void){

	unsigned int addr, i, offset = 0;	
	
	char *arg3 = string_repeat(384,"1");
	
	char *params [] = {VULN,"2","30",arg3,NULL};

	char *env [] = {separator,shellcode, NULL};

	execve(params[0],params,env);
	perror("execve");
	exit(1);

}
String StringUtil::Repeat(CStrRef input, int count) {
  if (count < 0) {
    raise_warning("Second argument has to be greater than or equal to 0");
    return String();
  }
  if (count == 0) {
    return "";
  }
  if (!input.empty()) {
    int len = input.size();
    char *ret = string_repeat(input.data(), len, count);
    if (ret) {
      return String(ret, len, AttachString);
    }
  }
  return input;
}
Beispiel #5
0
void logBloqueados(t_list* bloqueados) {

	int i;
	char *s = string_new();

	ITEM_NIVEL * temp = NULL;
	log_info(logFile, "Se detecto interbloque entre %d personajes",
			bloqueados->elements_count);
	for (i = 0; i < bloqueados->elements_count; i++) {
		temp = list_get(bloqueados, i);
		string_append(&s, "-");
		string_append(&s, string_repeat(temp->id, 1));

	}

	log_info(logFile, "Personajes involucharods: %s", s);
	free(s);
}
Beispiel #6
0
int finalizar_en_archivo(int pid) {
	usleep(swap_config->retardo_acceso * 1000);

	int fd = open("swap.data", O_RDWR, S_IWRITE | S_IREAD);

	if (fd == -1)
		return -1;

	struct stat buf;

	if ((stat("swap.data", &buf)) == -1) {
		close(fd);
		return -1;
	}

	char * espacio_swap = mmap((caddr_t) 0, buf.st_size, PROT_READ | PROT_WRITE,
	MAP_SHARED | MAP_NORESERVE, fd, 0);

	if (espacio_swap == MAP_FAILED) {
		close(fd);
		return -1;
	}

	char * hueco = string_repeat('\0', swap_config->pagina_size);

	bool from_process(t_swap * sw) {
		return (sw->pid == pid);
	}
	t_list * swaps_del_pid = list_filter(list_swap, (void *) from_process);

	int direccion = 0;
	void liberar_espacio(t_swap * s) {
		direccion = (s->posicion_en_swap) * swap_config->pagina_size;
		memcpy(espacio_swap + direccion, hueco, swap_config->pagina_size);
		direccion = 0;
	}
Beispiel #7
0
void consola() {

	//char eleccion[30];
	char* eleccion;
	char* comando;
	char* parametro;
	char c = 0;
	int longComando = 0;
	int posInicioParametro = 0;
	int consola = 1;

	while (consola) {
		posInicioParametro = 0;
		eleccion = string_new();
		comando = string_new();
		puts(
				"____________________________________________________________________\n\n"
						"Comandos disponibles: \n"
						"- 1) correr PATH \n"
						"- 2) finalizar PID \n"
						"- 3) ps \n"
						"- 4) cpu \n"
						"Ingrese un comando: ");
		scanf("%c", &c);
		//printf("Escanee %c\n",c);
		longComando++;
		eleccion = (char*) malloc( sizeof(char) * longComando );
		string_append(&eleccion, string_repeat(c,1));
		while(c != '\n'){
			scanf("%c", &c);
			//printf("Escanee %c\n",c);
			longComando++;
			eleccion = (char*) realloc(eleccion, sizeof(char) * longComando );
			string_append(&eleccion, string_repeat(c,1));
		}

		eleccion = (char*) realloc(eleccion, sizeof(char) * (longComando+1) );
		eleccion[longComando] = '\0';

		int i = 0;
		while(eleccion[i] != '\0'){
			if(eleccion[i] == ' ') posInicioParametro = i+1;
			i++;
		}
		//printf("El parametro empieza en la posicion %d\n", posInicioParametro);

		if(posInicioParametro != 0){
			parametro = string_new();
			comando = (char*) malloc( sizeof(char) * strlen(string_substring(eleccion, 0, posInicioParametro-1)));
			comando = string_substring(eleccion, 0, posInicioParametro-1);
			parametro = (char*) malloc( sizeof(char) * strlen(string_substring_from(eleccion, posInicioParametro)));
			parametro = string_substring_from(eleccion, posInicioParametro);
		}
		else{
			//printf("strlen %d \n", strlen(eleccion));
			eleccion[strlen(eleccion)-1] = '\0';
			//printf("strlen %d \n", strlen(eleccion));
			comando = (char*) malloc( sizeof(char) * strlen(eleccion) );
			comando = string_duplicate(eleccion);
		}

		if(posInicioParametro == 0){
			if(strcmp(eleccion, "ps") == 0){
				printf("3Comando a ejecutar: %s\n", comando);
			}
			else{
				if(strcmp(eleccion, "cpu") == 0){
					printf("4Comando a ejecutar: %s\n", comando);
				}
				else{
					printf("El comando (%s) ingresado no es valido \n", comando);
					eleccion[0] = '\0';
					comando[0] = '\0';
				}
			}
		}

		if(posInicioParametro != 0){
			if(strcmp(comando, "correr") == 0){
				printf("1Comando a ejecutar: %s %s\n", comando, parametro);
				Enviar(socketCPU, comando);
				Enviar(socketCPU, parametro);
			}
			else{
				if(strcmp(comando, "finalizar") == 0){
					printf("2Comando a ejecutar: %s %s\n", comando, parametro);
				}
				else{
					printf("El comando (%s %s) ingresado no es valido \n", comando, parametro);
					eleccion[0] = '\0';
					comando[0] = '\0';
					parametro[0] = '\0';
				}
			}
		}

		free(eleccion);
		free(comando);
		if(posInicioParametro != 0){
			free(parametro);
		}
	}
}
int step(context* ctx, avm* vm)
{
	int m, iop1, iop2, iopdest, i, res = ZEN_NO_ERROR;
	int framesize;	/* used for high order functions */
	char *str, *str1, *str2, ibuf[32];
	symbol* s;
	thread* th;
	ctable* table;
	instruction* ins = &vm->instructions[vm->pc];
	iop1 = ins->op1.ival;	iop2 = ins->op2.ival;	iopdest = ins->opdest.ival;
#ifdef ZEN_ENABLE_TRAP
	if (vm->traphook)
		(vm->traphook)(vm);
#endif
	if (ins->op == opHalt)
		return ZEN_VM_HALT;
	switch (ins->op)
	{
	case opLogand:
		seti(&vm->regs[iopdest], (vm->regs[iop1].entity.ival==0||vm->regs[iop2].entity.ival==0)?0:1);
		break;
	case opLogor:
		seti(&vm->regs[iopdest], (vm->regs[iop1].entity.ival==1||vm->regs[iop2].entity.ival==1)?1:0);
		break;
	case opBitcom:
		if (gettypew(&vm->regs[iopdest])==TINTEGER)
			seti(&vm->regs[iopdest], ~vm->regs[iopdest].entity.ival)
		else setvmerror(vm, ZEN_VM_INVALID_OPERATION);
		break;
	case opBitand:
		if (gettypew(&vm->regs[iop1])==TINTEGER && gettypew(&vm->regs[iop2])==TINTEGER)
			seti(&vm->regs[iopdest], (vm->regs[iop1].entity.ival & vm->regs[iop2].entity.ival))
		else setvmerror(vm, ZEN_VM_INVALID_OPERATION);
		break;
	case opBitor:
		if (gettypew(&vm->regs[iop1])==TINTEGER && gettypew(&vm->regs[iop2])==TINTEGER)
			seti(&vm->regs[iopdest], (vm->regs[iop1].entity.ival | vm->regs[iop2].entity.ival))
		else setvmerror(vm, ZEN_VM_INVALID_OPERATION);
		break;
	case opBitxor:
		if (gettypew(&vm->regs[iop1])==TINTEGER && gettypew(&vm->regs[iop2])==TINTEGER)
			seti(&vm->regs[iopdest], vm->regs[iop1].entity.ival ^ vm->regs[iop2].entity.ival)
		else setvmerror(vm, ZEN_VM_INVALID_OPERATION);
		break;
	case opLshift:
		seti(&vm->regs[iopdest], (vm->regs[iop1].entity.ival<<vm->regs[iop2].entity.ival));
		break;
	case opRshift:
		seti(&vm->regs[iopdest], (vm->regs[iop1].entity.ival>>vm->regs[iop2].entity.ival));
		break;
	case opNeg:
		if (gettypew(&vm->regs[iopdest])==TINTEGER)
			seti(&vm->regs[iopdest], -vm->regs[iopdest].entity.ival)
		else
			setf(&vm->regs[iopdest], -vm->regs[iopdest].entity.fval)
		break;
	case opNot:
		vm->regs[iopdest].entity.ival = !vm->regs[iopdest].entity.ival;
		break;
	case opFact:
		vm->regs[iopdest].entity.ival = factorial(vm->regs[iopdest].entity.ival);
		break;
	case opAdd:
		if (gettypew(&vm->regs[iop1])==TINTEGER && gettypew(&vm->regs[iop2])==TINTEGER)
			seti(&vm->regs[iopdest], vm->regs[iop1].entity.ival + vm->regs[iop2].entity.ival)
		else if (gettypew(&vm->regs[iop1])==TINTEGER && gettypew(&vm->regs[iop2])==TFLOAT)
			setf(&vm->regs[iopdest], (float)vm->regs[iop1].entity.ival + vm->regs[iop2].entity.fval)
		else if (gettypew(&vm->regs[iop1])==TINTEGER && gettypew(&vm->regs[iop2])==TSTRING)
		{
			str = (char*)calloc(strlen(getdata(vm->hp.heap, vm->regs[ins->op2.ival].entity.ival))+64, sizeof(char));
			sprintf(str, "%d%s", vm->regs[iop1].entity.ival, getdata(vm->hp.heap, vm->regs[ins->op2.ival].entity.ival));
			sets(&vm->regs[ins->opdest.ival], newstring(vm,str));
			free(str);
		}
		else if (gettypew(&vm->regs[iop1])==TFLOAT && gettypew(&vm->regs[iop2])==TINTEGER)
			setf(&vm->regs[iopdest], vm->regs[iop1].entity.fval + (float)vm->regs[iop2].entity.ival)
		else if (gettypew(&vm->regs[iop1])==TFLOAT && gettypew(&vm->regs[iop2])==TFLOAT)
			setf(&vm->regs[iopdest], vm->regs[iop1].entity.fval + vm->regs[iop2].entity.fval)
		else if (gettypew(&vm->regs[iop1])==TFLOAT && gettypew(&vm->regs[iop2])==TSTRING)
		{
			str = (char*)calloc(strlen(getdata(vm->hp.heap, vm->regs[ins->op2.ival].entity.ival))+64, sizeof(char));
			sprintf(str, "%f%s", vm->regs[iop1].entity.fval, getdata(vm->hp.heap, vm->regs[ins->op2.ival].entity.ival));
			sets(&vm->regs[ins->opdest.ival], newstring(vm,str));
			free(str);
		}
		else if (gettypew(&vm->regs[iop1])==TSTRING && gettypew(&vm->regs[iop2])==TINTEGER)
		{
			str = (char*)calloc(strlen(getdata(vm->hp.heap, vm->regs[ins->op1.ival].entity.ival))+64, sizeof(char));
			sprintf(str, "%s%d", getdata(vm->hp.heap, vm->regs[iop1].entity.ival), vm->regs[ins->op2.ival].entity.ival);
			sets(&vm->regs[ins->opdest.ival], newstring(vm,str));
			free(str);
		}
		else if (gettypew(&vm->regs[iop1])==TSTRING && gettypew(&vm->regs[iop2])==TFLOAT)
		{
			str = (char*)calloc(strlen(getdata(vm->hp.heap, vm->regs[ins->op1.ival].entity.ival))+64, sizeof(char));
			sprintf(str, "%s%f", getdata(vm->hp.heap, vm->regs[iop1].entity.ival), vm->regs[ins->op2.ival].entity.fval);
			sets(&vm->regs[ins->opdest.ival], newstring(vm,str));
			free(str);
		}
		else if (gettypew(&vm->regs[iop1])==TSTRING && gettypew(&vm->regs[iop2])==TSTRING)
		{
			char* s1 = getdata(vm->hp.heap, vm->regs[ins->op1.ival].entity.ival);
			str = (char*)calloc(strlen(getdata(vm->hp.heap, vm->regs[ins->op1.ival].entity.ival))
				+strlen(getdata(vm->hp.heap, vm->regs[ins->op2.ival].entity.ival))+1, sizeof(char));
			strcpy(str, (char*)getdata(vm->hp.heap, vm->regs[ins->op1.ival].entity.ival));
			strcat(str, (char*)getdata(vm->hp.heap, vm->regs[ins->op2.ival].entity.ival));
			sets(&vm->regs[ins->opdest.ival], newstring(vm,str));
			free(str);
		}
		//- added for testing
		else if (gettypew(&vm->regs[iop1])== TTABLE && gettypew(&vm->regs[iop2])==TINTEGER)
		{/*
	        long tindex = TOTinsert(vm, off);
	        ctable* tbl = (ctable*)getdata(vm->hp.heap, ind2off(vm->regs[iop1].entity.ival));
	        word index, source;

	        seti(&index, tbl->nextslot);
            seti(&source, vm->regs[iop2].entity.ival);
	        IA(vm, tbl, tindex, &index, &source);

	        sett(&vm->regs[iopdest], tindex);*/
		}
		else setvmerror(vm, ZEN_VM_INVALID_OPERATION);
		break;

	case opMinus:
		if (gettypew(&vm->regs[iop1])==TINTEGER && gettypew(&vm->regs[iop2])==TINTEGER)
			seti(&vm->regs[iopdest], vm->regs[iop1].entity.ival - vm->regs[iop2].entity.ival)
		else if (gettypew(&vm->regs[iop1])==TINTEGER && gettypew(&vm->regs[iop2])==TFLOAT)
			setf(&vm->regs[iopdest], (float)vm->regs[iop1].entity.ival - vm->regs[iop2].entity.fval)
		else if (gettypew(&vm->regs[iop1])==TFLOAT && gettypew(&vm->regs[iop2])==TINTEGER)
			setf(&vm->regs[iopdest], vm->regs[iop1].entity.fval - (float)vm->regs[iop2].entity.ival)
		else if (gettypew(&vm->regs[iop1])==TFLOAT && gettypew(&vm->regs[iop2])==TFLOAT)
			setf(&vm->regs[iopdest], vm->regs[iop1].entity.fval - vm->regs[iop2].entity.fval)
		else setvmerror(vm, ZEN_VM_INVALID_OPERATION);
		break;
	case opMultiply:
		if (gettypew(&vm->regs[iop1])==TINTEGER && gettypew(&vm->regs[iop2])==TINTEGER)
			seti(&vm->regs[iopdest], vm->regs[iop1].entity.ival * vm->regs[iop2].entity.ival)
		else if (gettypew(&vm->regs[iop1])==TINTEGER && gettypew(&vm->regs[iop2])==TFLOAT)
			setf(&vm->regs[iopdest], (float)vm->regs[iop1].entity.ival * vm->regs[iop2].entity.fval)
		else if (gettypew(&vm->regs[iop1])==TFLOAT && gettypew(&vm->regs[iop2])==TINTEGER)
			setf(&vm->regs[iopdest], vm->regs[iop1].entity.fval * (float)vm->regs[iop2].entity.ival)
		else if (gettypew(&vm->regs[iop1])==TFLOAT && gettypew(&vm->regs[iop2])==TFLOAT)
			setf(&vm->regs[iopdest], vm->regs[iop1].entity.fval * vm->regs[iop2].entity.fval)
        //- String repeat
        else if (gettypew(&vm->regs[iop1])==TSTRING && gettypew(&vm->regs[iop2])==TINTEGER)
        {
			char* s1 = string_repeat(getdata(vm->hp.heap, vm->regs[ins->op1.ival].entity.ival), vm->regs[iop2].entity.ival);
			sets(&vm->regs[iopdest], newstring(vm, s1))
        }
        else if (gettypew(&vm->regs[iop1])==TINTEGER && gettypew(&vm->regs[iop2])==TSTRING)
			seti(&vm->regs[iopdest], vm->regs[iop1].entity.ival * vm->regs[iop2].entity.ival)
		else if (gettypew(&vm->regs[iop1])==TINTEGER && gettypew(&vm->regs[iop2])==TFLOAT)
			setf(&vm->regs[iopdest], (float)vm->regs[iop1].entity.ival * vm->regs[iop2].entity.fval)
		else if (gettypew(&vm->regs[iop1])==TFLOAT && gettypew(&vm->regs[iop2])==TINTEGER)
			setf(&vm->regs[iopdest], vm->regs[iop1].entity.fval * (float)vm->regs[iop2].entity.ival)
		else if (gettypew(&vm->regs[iop1])==TFLOAT && gettypew(&vm->regs[iop2])==TFLOAT)
			setf(&vm->regs[iopdest], vm->regs[iop1].entity.fval * vm->regs[iop2].entity.fval)
        //- String repeat
        else if (gettypew(&vm->regs[iop1])==TSTRING && gettypew(&vm->regs[iop2])==TINTEGER)
        {
			char* s1 = string_repeat(getdata(vm->hp.heap, vm->regs[ins->op1.ival].entity.ival), vm->regs[iop2].entity.ival);
			sets(&vm->regs[iopdest], newstring(vm, s1))
        }
        else if (gettypew(&vm->regs[iop1])==TINTEGER && gettypew(&vm->regs[iop2])==TSTRING)
        {
			char* s1 = string_repeat(getdata(vm->hp.heap, vm->regs[ins->op2.ival].entity.ival), vm->regs[iop1].entity.ival);
			sets(&vm->regs[iopdest], newstring(vm, s1))
        }
		else setvmerror(vm, ZEN_VM_INVALID_OPERATION);
		break;
    //- added for testing
    case opPower:
		if (gettypew(&vm->regs[iop1])==TINTEGER && gettypew(&vm->regs[iop2])==TINTEGER)
			seti(&vm->regs[iopdest], pow(vm->regs[iop1].entity.ival, vm->regs[iop2].entity.ival))
		else if (gettypew(&vm->regs[iop1])==TINTEGER && gettypew(&vm->regs[iop2])==TFLOAT)
			setf(&vm->regs[iopdest], pow((float)vm->regs[iop1].entity.ival, vm->regs[iop2].entity.fval))
		else if (gettypew(&vm->regs[iop1])==TFLOAT && gettypew(&vm->regs[iop2])==TINTEGER)
			setf(&vm->regs[iopdest], pow(vm->regs[iop1].entity.fval, (float)vm->regs[iop2].entity.ival))
		else if (gettypew(&vm->regs[iop1])==TFLOAT && gettypew(&vm->regs[iop2])==TFLOAT)
			setf(&vm->regs[iopdest], pow(vm->regs[iop1].entity.fval, vm->regs[iop2].entity.fval))
		else setvmerror(vm, ZEN_VM_INVALID_OPERATION);