Exemplo n.º 1
0
/*
 * initialize the index table: read it from language.idx or create
 * a new one and write it to the index file. A new index file is
 * created if the language set in config.pgp doesn't match the one
 * in language.idx or if the size of language.txt has changed.
 */
static void
init_lang()
{
	char indexfile[MAX_PATH];
	char subtitles_file[MAX_PATH];
	FILE *indexf;

	if (strcmp(language, "en") == 0) {
		subtitles_available = -1;
		return;		/* use default messages */
	}

	buildfilename (subtitles_file, langfile);
	if ((langf = fopen(subtitles_file, "rb")) == NULL) {
		subtitles_available = -1;
		return;
	}
	init_crc();
	if ((strbuf = (char *) malloc(STRBUFSIZE)) == NULL) {
		fprintf(stderr, "Not enough memory for foreign subtitles\n");
		fclose(langf);
		subtitles_available = -1;
		return;
	}
	buildfilename(indexfile, LANG_INDEXFILE);
	if ((indexf = fopen(indexfile, "rb")) != NULL) {
		if (fread(&indx_hdr, sizeof(indx_hdr), 1, indexf) == 1 &&
			indx_hdr.lang_fsize == fsize(langf) &&
			strcmp(indx_hdr.lang, language) == 0)
		{
			nmsg = indx_hdr.nmsg;
			indx_tbl = (struct indx_ent *) malloc(nmsg * sizeof(struct indx_ent));
			if (indx_tbl == NULL) {
				fprintf(stderr, "Not enough memory for foreign subtitles\n");
				fclose(indexf);
				fclose(langf);
				subtitles_available = -1;
				return;
			}
			if (fread(indx_tbl, sizeof(struct indx_ent), nmsg, indexf) != nmsg)
			{
				free(indx_tbl);	/* create a new one */
				indx_tbl = NULL;
			}
		}
		fclose(indexf);
	}
	if (indx_tbl == NULL && make_indexfile(indexfile) < 0) {
		fclose(langf);
		subtitles_available = -1;
	} else {
		subtitles_available = 1;
	}
}
Exemplo n.º 2
0
main(int argc, char **argv)
{
	int opt, cmd = 0, rc = 0;
	char *langIDs[16];
	char *outfile = NULL;

	init_crc();
	if ((strbuf = (char *) malloc(STRBUFSIZE)) == NULL) {
		perror(argv[0]);
		exit(1);
	}
	while ((opt = getopt(argc, argv, "cxmo:")) != EOF) {
		switch (opt) {
		  case 'c': cmd = CMD_CHECK; break;
		  case 'x': cmd = CMD_EXTRACT; break;
		  case 'm': cmd = CMD_MERGE; break;
		  case 'o': outfile = optarg; break;
		  default: usage();
		}
	}
	argc -= optind; argv += optind;
	switch (cmd) {
		case CMD_EXTRACT:
			if (argc < 2)
				usage();
			rc = extract(argv[0], outfile, &argv[1]);
			break;
		case CMD_MERGE:
			if (argc < 2)
				usage();
			rc = merge(argv[0], argv[1], outfile, argv[2]);
			break;
		case CMD_CHECK:
			verbose = 1;
			if (argc == 0)
				checkfile("language.txt");
			else
				while (--argc >= 0)
					checkfile(*argv++);
			break;
		default: usage();
	}
	exit(rc);
}
Exemplo n.º 3
0
/*
 * build the index table in memory, and if indexfile is not NULL,
 * write it to this file
 */
static int
make_indexfile(char *indexfile)
{
	FILE *indexf;
	long filepos;
	int total_msgs = 0;
	char *res;

	rewind(langf);
	indx_hdr.lang_fsize = fsize(langf);
	strncpy(indx_hdr.lang, language, 15);
	init_crc();
	line = 1;
	nmsg = 0;
	while (readstr(langf, strbuf, 0)) {
		if (nmsg == max_msgs) {
			if (max_msgs) {
				max_msgs *= 2;
				indx_tbl = (struct indx_ent *) realloc(indx_tbl, max_msgs *
							sizeof(struct indx_ent));
			} else {
				max_msgs = 400;
				indx_tbl = (struct indx_ent *) malloc(max_msgs *
							sizeof(struct indx_ent));
			}
			if (indx_tbl == NULL) {
				fprintf(stderr, "Not enough memory for foreign subtitles\n");
				return(-1);
			}
		}
		++total_msgs;
		indx_tbl[nmsg].crc = message_crc(strbuf);
		if (lookup_offset(indx_tbl[nmsg].crc) != -1)
			error("message CRC not unique.\n");
		do {
			filepos = ftell(langf);
			res = readstr (langf, strbuf, 1);		/* Abort if find newline first */
			if (*language == '\0')		/* use first language found */
				strcpy(language, lang);
		} while (res && strbuf[0] != '\0' && strcmp(language, lang) != 0);

		if (res == NULL)
			break;
		if (strbuf[0] == '\0')	/* No translation */
			continue;

		indx_tbl[nmsg].offset = filepos;
		++nmsg;
		do
			res = readstr (langf, strbuf, 1);		/* Abort if find newline first */
		while (res && strbuf[0] != '\0');
	}
	line = 0;
	indx_hdr.nmsg = nmsg;
	if (verbose)
		fprintf(stderr, "%s: %d messages, %d translations for language \"%s\"\n",
				langfile, total_msgs, nmsg, language);
	if (nmsg == 0) {
		fprintf(stderr, "No translations available for language \"%s\"\n\n",
				language);
		return(-1);
	}

	if (indexfile) {
		if ((indexf = fopen(indexfile, "wb")) == NULL)
			fprintf(stderr, "Cannot create %s\n", indexfile);
		else {
			fwrite(&indx_hdr, 1, sizeof(indx_hdr), indexf);
			fwrite(indx_tbl, sizeof(struct indx_ent), nmsg, indexf);
			if (ferror(indexf) || fclose(indexf))
				fprintf(stderr, "error writing %s\n", indexfile);
		}
	}
	return(0);
}
Exemplo n.º 4
0
Arquivo: uftp.c Projeto: smakonin/UFTP
int __cdecl main(int argc, char **argv) 
{
	WSADATA			wsa_data;
    struct addrinfo *result					= NULL,
                    *ptr					= NULL,
                    hints;
	SOCKET			conn_socket				= INVALID_SOCKET;
	char			input[MAX_INPUT_LENGTH],
					msg_buf[MAX_INPUT_LENGTH],
					*command,
					*filename,
					*next_token				= NULL,
					control_message[MAX_INPUT_LENGTH];
	int				quit					= FALSE,
					return_code;

	init_crc();

	printf("UFTP Client v0.8\n");
	printf("--------------------------\n\n");

	if(argc != 2)
	{
		printf("Usage: uftp.exe [ip address of the UFTP Server]\n\n");
		return 1;
	}

	printf("Connecting to UFTP Server %s:%s.", argv[1], CONNECT_PORT);

    // Initialize Winsock
    return_code = WSAStartup(MAKEWORD(2,2), &wsa_data);
    if(return_code != 0) 
	{
        printf("WSAStartup failed: %d\n", return_code);
        return 1;
    }
	printf(".");

    // Resolve the server address and port
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    return_code = getaddrinfo(argv[1], CONNECT_PORT, &hints, &result);
    if(return_code != 0) 
	{
        printf("getaddrinfo failed: %d\n", return_code);
        WSACleanup();
        return 1;
    }
	printf(".");

    // Attempt to connect to an address until one succeeds
    for(ptr = result; ptr != NULL; ptr = ptr->ai_next) 
	{
        // Create a conn_socket for connecting to server
        conn_socket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
        if(conn_socket == INVALID_SOCKET) 
		{
            printf("Error at conn_socket(): %ld\n", WSAGetLastError());
            freeaddrinfo(result);
            WSACleanup();
            return 1;
        }
		printf(".");

        // Connect to server.
        return_code = connect(conn_socket, ptr->ai_addr, (int)ptr->ai_addrlen);
        if(return_code == SOCKET_ERROR) 
		{
            closesocket(conn_socket);
            conn_socket = INVALID_SOCKET;
            continue;
        }
		printf(".");
        break;
    }
    freeaddrinfo(result);
    if(conn_socket == INVALID_SOCKET) 
	{
        printf("Unable to connect to server!\n");
        WSACleanup();
        return 1;
    }	
	printf(".DONE!\n\n");
	printf("COMMANDS:\n");
	printf("\t DIR  - Display directory listing.\n");
	printf("\t GET  - Download remote file.\n");
	printf("\t QUIT - Disconnect from server and exit.\n\n");

	memset(control_message, 0, sizeof(MAX_INPUT_LENGTH));
	control_message[0] = CONTROL_MESSAGE_SEND_HELLO;
	return_code = send(conn_socket, control_message, (int)strlen(control_message), 0);
	if(return_code == SOCKET_ERROR)
	{
		printf("failed to send hello: %d\n", WSAGetLastError());
		quit = TRUE;
	}
	else
	{
		memset(control_message, 0, sizeof(MAX_INPUT_LENGTH));
		return_code = recv(conn_socket, control_message, MAX_INPUT_LENGTH-1, 0);
		if(return_code == SOCKET_ERROR)
		{
			printf("failed to receive hello: %d\n", WSAGetLastError());
			quit = TRUE;
		}
	}

	switch(control_message[0])
	{
	case CONTROL_MESSAGE_HELLO_RESPONSE:
		printf("UFTP Server ready for commands:\n\n");
		quit = FALSE;
		break;

	case CONTROL_MESSAGE_SERVER_BUSY:
		printf("Server is too bust, try again later...\n\n");
		quit = TRUE;
		break;

	default:
		printf("Unknown server, may not be a UFTP Server.\n\n");
		quit = TRUE;
		break;
	}

	// interact with the server
	while(!quit)
	{
		memset(input, 0, sizeof(MAX_INPUT_LENGTH));
		printf(">");
		gets_s(input, MAX_INPUT_LENGTH);

		//process input
		command = strtok_s(input, DELIMIT_TOKENS, &next_token);
		filename = strtok_s(NULL, DELIMIT_TOKENS, &next_token);

		if(_strcmpi(command, "get") == 0)
		{
			sprintf_s(control_message, MAX_INPUT_LENGTH, "  %s", filename);
			control_message[0] = CONTROL_MESSAGE_GET_FILE;
			return_code = send(conn_socket, control_message, (int)strlen(control_message)+1, 0);
			if(return_code == SOCKET_ERROR)
			{
				printf("failed to send command: %d\n", WSAGetLastError());
				quit = TRUE;
				break;
			}

			if(!recv_file(conn_socket, filename))
			{
				printf("failed to get file: %d\n", WSAGetLastError());
				quit = TRUE;
				continue;
			}
		}
		else if(_strcmpi(command, "dir") == 0)
		{
			memset(control_message, 0, sizeof(MAX_INPUT_LENGTH));
			control_message[0] = CONTROL_MESSAGE_DISPLAY_DIR;
			return_code = send(conn_socket, control_message, (int)strlen(control_message), 0);
			if(return_code == SOCKET_ERROR)
			{
				printf("failed to send command: %d\n", WSAGetLastError());
				quit = TRUE;
				break;
			}

			do
			{				
				memset(msg_buf, 0, sizeof(MAX_INPUT_LENGTH));
				return_code = recv(conn_socket, msg_buf, MAX_INPUT_LENGTH-1, 0);
				if(return_code == SOCKET_ERROR)
				{
					printf("failed to list dir: %d\n", WSAGetLastError());
					quit = TRUE;
					break;
				}				

				printf("%s", msg_buf);

				if(strstr(msg_buf, END_OF_DYNAMIC_DATA) != NULL)
					break;

			} while(_strcmpi(msg_buf, END_OF_DYNAMIC_DATA) != 0);

			printf("\n");
		}
		else if(_strcmpi(command, "quit") == 0)
		{
			quit = TRUE;
		}
		else
		{
			printf("unknown command: %s\n", command);
		}
	}

    // shutdown the connection since no more data will be sent
    return_code = shutdown(conn_socket, SD_SEND);
    if(return_code == SOCKET_ERROR) 
	{
        printf("shutdown failed: %d\n", WSAGetLastError());
        closesocket(conn_socket);
        WSACleanup();
        return 1;
    }

    // Receive until the peer closes the connection
    do 
	{
        return_code = recv(conn_socket, msg_buf, MAX_INPUT_LENGTH-1, 0);
    } while( return_code > 0 );

    // cleanup
    closesocket(conn_socket);
    WSACleanup();

    return 0;
}
Exemplo n.º 5
0
int main(int argc, char **argv)
{
	WSADATA				wsa_data;
	SOCKET				listening_socket			= INVALID_SOCKET,
						client_socket				= INVALID_SOCKET;
    struct addrinfo		*result						= NULL, 
						hints;
	struct sockaddr		address;
	int					return_code,
						index,
						address_length,
						shutdown_signal				= 0;
	char				control_message[MAX_INPUT_LENGTH];
	HTHREAD	threads[MAX_CONNECTIONS];

	init_crc();

	printf("UFTP Daemon v0.8\n");
	printf("--------------------------\n\n");
	printf("On fixed port: %s\n", CONNECT_PORT);
	printf("Max. Connurent Connections: %d\n", MAX_CONNECTIONS);
	printf("\nServer staring up.");

	// Clean up thread lookup
	memset(threads, 0, sizeof(HTHREAD) * MAX_CONNECTIONS);
	printf(".");
    
    // Initialize Winsock sub-system
    return_code = WSAStartup(MAKEWORD(2,2), &wsa_data);
    if(return_code != 0) 
	{
        printf("WSAStartup failed: %d\n", return_code);
        return 1;
    }
	printf(".");

    // Resolve the server address and port
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;
    return_code = getaddrinfo(NULL, CONNECT_PORT, &hints, &result);
    if(return_code != 0) 
	{
        printf("getaddrinfo failed: %d\n", return_code);
        WSACleanup();
        return 1;
    }
	printf(".");

    // Create a SOCKET for connecting to server
    listening_socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if(listening_socket == INVALID_SOCKET) 
	{
        printf("socket failed: %ld\n", WSAGetLastError());
        freeaddrinfo(result);
        WSACleanup();
        return 1;
    }
	printf(".");

    // Setup the TCP listening socket
    return_code = bind(listening_socket, result->ai_addr, (int)result->ai_addrlen);
    if(return_code == SOCKET_ERROR) 
	{
        printf("bind failed: %d\n", WSAGetLastError());
        freeaddrinfo(result);
        closesocket(listening_socket);
        WSACleanup();
        return 1;
    }
    freeaddrinfo(result);
	printf(".");

    return_code = listen(listening_socket, SOMAXCONN);
    if(return_code == SOCKET_ERROR) 
	{
        printf("listen failed: %d\n", WSAGetLastError());
        closesocket(listening_socket);
        WSACleanup();
        return 1;
    }
	printf(".DONE!\n\n");

	// Loop until shutdown
	while(!shutdown_signal)
	{
		// Accept a client socket
		memset(&address, 0, sizeof(address));
		client_socket = INVALID_SOCKET;
		address_length = sizeof(address);
		client_socket = accept(listening_socket, &address, &address_length);
		if(client_socket == INVALID_SOCKET) 
			printf("accept failed: %d\n", WSAGetLastError());

		memset(control_message, 0, sizeof(MAX_INPUT_LENGTH));
		return_code = recv(client_socket, control_message, MAX_INPUT_LENGTH-1, 0);
		if(control_message[0] != CONTROL_MESSAGE_SEND_HELLO)
		{
			return_code = shutdown(client_socket, SD_BOTH);
			closesocket(client_socket);
			continue;
		}

		// Client socket valid start tread to handle client
		index = find_next_free_thread(threads);
		if(index == -1)
		{
			printf("max connections reached: %d\n", MAX_CONNECTIONS);
			
			memset(control_message, 0, sizeof(MAX_INPUT_LENGTH));
			control_message[0] = CONTROL_MESSAGE_SERVER_BUSY;
			return_code = send(client_socket, control_message, (int)strlen(control_message), 0);
			return_code = shutdown(client_socket, SD_BOTH);
			closesocket(client_socket);
			continue;
		}

		threads[index].in_use = 1;
		threads[index].socket = client_socket;
		memcpy(&threads[index].address, &address, sizeof(address));
		threads[index].handle = CreateThread(NULL, 0, handle_client_thread, &threads[index], 0, &threads[index].id);

        if(threads[index].handle == NULL) 
			printf("unable to create thread: %d\n", index);

		// TODO: Add clean way to close down server
		//if(tolower(getchar()) == (int)'q')
		//	shutdown_signal = 1;
	}

    // Close to stop new connections
	closesocket(listening_socket);

    // Make sure all threads are complete and exited
	for(index = 0; index < MAX_CONNECTIONS; index++)
	{
		WaitForSingleObject(threads[index].handle, INFINITE);
		CloseHandle(threads[index].handle);
	}
	
	// Close down socket sub-system
    WSACleanup();

    return 0;
}