예제 #1
0
int main(int argc, char* argv[])
{
    int i;
    int r;                  /* row in table */
    int m;                  /* step */
    int strlength;
    FILE* in = stdin;
    if (argc > 2) {
        printf("Usage: $> av inputfile\n");
        return 0;
    }
    if (argc == 2) {
        in = fopen(argv[1], "r");
        if (NULL == in) {
            return -1;
        }
    }
    (void)read_to_buf(buf, BUF_LEN, in);
    strlength = strlen(buf);
    for (m = 1; m <= MAX_M; ++m) {
        for (r = 0; r < m; ++r) {
            ic[m-1][r] = calc_Ic(m, r, buf, strlength);
        }
    }
    calc_avg_Ic();
    m = find_key_len();
    printf("key length = %d\n", m);
    calc_MIc(m, buf, strlength);
    find_g(m);
    calc_key_equation(m);
    try_keys(m);

    fclose(in);
    return 0;
}
예제 #2
0
void handle_client(int cfd){
    char name[1024];
    bzero(name, 1024);
    read (cfd, &name, 1024);
    printf("%s\n", name);
    int fret = open(name, O_RDONLY);
    if (fret < 0){
        //perror("file error");
        write(cfd, "ER", 2);
        char* err = strerror(errno);
        write(cfd, err, strlen(err));
        printf("%s", err);

        close(cfd);
        return;
    }
    printf("asdfasdfsaf\n");
    write(cfd, "OK", 2);
    struct stat stat_src;
    fstat(fret, &stat_src);
    size_t num = htons(stat_src.st_size);
    printf("sending file size: %d\n", stat_src.st_size);
    write(cfd, &num, sizeof(num));
    s_buffer* buf = alloc_buffer(256, fret, cfd);
    int bufret = 1;
    while(bufret > 0){
        bufret = read_to_buf(buf, fret);
        bufret = read_from_buffer(buf, cfd);
    }
    dealloc_buffer(buf);
    close(cfd);
}
예제 #3
0
파일: npost.c 프로젝트: PietjeBell88/npost
void crc32_file( diskfile_t *df )
{
    char buf[1L << 15];
    df->crc = 0;
    size_t bytes_read;
    size_t offset;

    while( (bytes_read = read_to_buf( df, offset, sizeof(buf), buf )) > 0 )
    {
        crc32( buf, bytes_read, &df->crc );
        offset += bytes_read;
    }
}
예제 #4
0
파일: npost.c 프로젝트: PietjeBell88/npost
size_t npost_get_part( npost_param_t *param, int filenum, int partnum, char **outbuf )
{
    // Alias
    diskfile_t *df = &param->split_files[filenum];
    size_t blocksize = YENC_LINE_LENGTH * param->lines;
    int parts = (df->filesize + blocksize - 1) / blocksize;

    // Allocate buffers
    char *inbuf = malloc( blocksize );
    char *buffer = malloc( yenc_buffer_size( param->lines ) + 1024 ); // message-id also takes up some space
    char *buffer_orig = buffer;

    // Generate the subject
    char subject[256];

    sprintf( subject, "%s - [%d/%d] - \"%s\" yEnc (%d/%d)",
             param->comment, filenum+1, param->n_split_files, df->filename_out, partnum+1, parts);

    // Generate the message id
    char message_id[1024];
    srand( time( NULL ) );
    sprintf( message_id, "*****@*****.**",
             partnum+1, parts, rand() % 0xffffffff, rand() % 0xffffffff, rand() % 0xffffffff, (int) time( NULL ) );

    buffer += sprintf( buffer, "From: %s (%s)\r\nNewsgroups: %s\r\nSubject: %s\r\nMessage-ID: <%s>\r\n\r\n",
                       param->email, param->name, param->newsgroups, subject, message_id );


    size_t psize = read_to_buf( df, partnum * blocksize, blocksize, inbuf );

    buffer += yenc_encode( df->filename_out, df->filesize, df->crc, inbuf,
                           partnum+1, parts, param->lines, psize, buffer );

    buffer += sprintf( buffer, "\r\n.\r\n" );

    *outbuf = buffer_orig;

    free( inbuf );

    return buffer - buffer_orig;
}
예제 #5
0
procps_status_t * procps_scan(int save_user_arg0)
{
	static DIR *dir;
	struct dirent *entry;
	static procps_status_t ret_status;
	char *name;
	int n;
	char status[32];
	char *status_tail;
	char buf[PROCPS_BUFSIZE];
	procps_status_t curstatus;
	int pid;
	long tasknice;
	struct stat sb;

	if (!dir) {
		dir = opendir("/proc");
		if(!dir)
			bb_error_msg_and_die("Can't open /proc");
	}
	for(;;) {
		if((entry = readdir(dir)) == NULL) {
			closedir(dir);
			dir = 0;
			return 0;
		}
		name = entry->d_name;
		if (!(*name >= '0' && *name <= '9'))
			continue;

		memset(&curstatus, 0, sizeof(procps_status_t));
		pid = atoi(name);
		curstatus.pid = pid;

		status_tail = status + sprintf(status, "/proc/%d", pid);
		if(stat(status, &sb))
			continue;
		bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));

		strcpy(status_tail, "/stat");
		n = read_to_buf(status, buf);
		if(n < 0)
			continue;
		name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
		if(name == 0 || name[1] != ' ')
			continue;
		*name = 0;
		sscanf(buf, "%*s (%15c", curstatus.short_cmd);
		n = sscanf(name+2,
		"%c %d "
		"%*s %*s %*s %*s "     /* pgrp, session, tty, tpgid */
		"%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
		"%lu %lu "
#else
		"%*s %*s "
#endif
		"%*s %*s %*s "         /* cutime, cstime, priority */
		"%ld "
		"%*s %*s %*s "         /* timeout, it_real_value, start_time */
		"%*s "                 /* vsize */
		"%ld",
		curstatus.state, &curstatus.ppid,
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
		&curstatus.utime, &curstatus.stime,
#endif
		&tasknice,
		&curstatus.rss);
#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
		if(n != 6)
#else
		if(n != 4)
#endif
			continue;

		if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
			curstatus.state[1] = 'W';
		else
			curstatus.state[1] = ' ';
		if (tasknice < 0)
			curstatus.state[2] = '<';
		else if (tasknice > 0)
			curstatus.state[2] = 'N';
		else
			curstatus.state[2] = ' ';

#ifdef PAGE_SHIFT
		curstatus.rss <<= (PAGE_SHIFT - 10);     /* 2**10 = 1kb */
#else
		curstatus.rss *= (getpagesize() >> 10);     /* 2**10 = 1kb */
#endif

		if(save_user_arg0) {
			strcpy(status_tail, "/cmdline");
			n = read_to_buf(status, buf);
			if(n > 0) {
				if(buf[n-1]=='\n')
					buf[--n] = 0;
				name = buf;
				while(n) {
					if(((unsigned char)*name) < ' ')
						*name = ' ';
					name++;
					n--;
				}
				*name = 0;
				if(buf[0])
					curstatus.cmd = strdup(buf);
				/* if NULL it work true also */
			}
		}
		return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
	}
}
예제 #6
0
procps_status_t* procps_scan(procps_status_t* sp, int flags)
{
	struct dirent *entry;
	char buf[PROCPS_BUFSIZE];
	char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
	char *filename_tail;
	long tasknice;
	unsigned pid;
	int n;
	struct stat sb;

	if (!sp)
		sp = alloc_procps_scan(flags);

	for (;;) {
		entry = readdir(sp->dir);
		if (entry == NULL) {
			free_procps_scan(sp);
			return NULL;
		}
		pid = bb_strtou(entry->d_name, NULL, 10);
		if (errno)
			continue;

		/* After this point we have to break, not continue
		 * ("continue" would mean that current /proc/NNN
		 * is not a valid process info) */

		memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz));

		sp->pid = pid;
		if (!(flags & ~PSSCAN_PID)) break;

		filename_tail = filename + sprintf(filename, "/proc/%d", pid);

		if (flags & PSSCAN_UIDGID) {
			if (stat(filename, &sb))
				break;
			/* Need comment - is this effective or real UID/GID? */
			sp->uid = sb.st_uid;
			sp->gid = sb.st_gid;
		}

		if (flags & PSSCAN_STAT) {
			char *cp;
			/* see proc(5) for some details on this */
			strcpy(filename_tail, "/stat");
			n = read_to_buf(filename, buf);
			if (n < 0)
				break;
			cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
			if (!cp || cp[1] != ' ')
				break;
			cp[0] = '\0';
			if (sizeof(sp->comm) < 16)
				BUG_comm_size();
			sscanf(buf, "%*s (%15c", sp->comm);
			n = sscanf(cp+2,
				"%c %u "               /* state, ppid */
				"%u %u %*s %*s "       /* pgid, sid, tty, tpgid */
				"%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
				"%lu %lu "             /* utime, stime */
				"%*s %*s %*s "         /* cutime, cstime, priority */
				"%ld "                 /* nice */
				"%*s %*s %*s "         /* timeout, it_real_value, start_time */
				"%lu ",                /* vsize */
				sp->state, &sp->ppid,
				&sp->pgid, &sp->sid,
				&sp->utime, &sp->stime,
				&tasknice,
				&sp->vsz);
			if (n != 8)
				break;

			if (sp->vsz == 0 && sp->state[0] != 'Z')
				sp->state[1] = 'W';
			else
				sp->state[1] = ' ';
			if (tasknice < 0)
				sp->state[2] = '<';
			else if (tasknice > 0)
				sp->state[2] = 'N';
			else
				sp->state[2] = ' ';

			sp->vsz >>= 10; /* vsize is in bytes and we want kb */
		}

		if (flags & PSSCAN_CMD) {
			free(sp->cmd);
			sp->cmd = NULL;
			strcpy(filename_tail, "/cmdline");
			n = read_to_buf(filename, buf);
			if (n <= 0)
				break;
			if (buf[n-1] == '\n') {
				if (!--n)
					break;
				buf[n] = '\0';
			}
			do {
				n--;
				if ((unsigned char)(buf[n]) < ' ')
					buf[n] = ' ';
			} while (n);
			sp->cmd = xstrdup(buf);
		}
		break;
	}