示例#1
0
文件: stage2.c 项目: pnasrat/thimble
// koffset is the first byte of the kernel on disk
void
stage2main(ulong koffset)
{
    ElfHeader *elf;
    ElfProgHeader *ph, *eph;
    void (*entry)(void);
    uchar *pa;

    cclear();

    has_ahci = ahcidetect();

    elf = (ElfHeader *)0x10000;
    readbytes((uchar *)elf, PGSIZE, koffset);

    if (elf->magic != ELF_MAGIC)
        panic("stage2main - elf magic");

    ph = (ElfProgHeader *)((char *)elf + elf->phoff);
    eph = ph + elf->phnum;

    for (; ph < eph; ph++) {
        pa = (uchar *)ph->paddr;
        readbytes(pa, ph->filesz, koffset + ph->offset);
        if (ph->memsz > ph->filesz)
            stosb(pa + ph->filesz, 0, ph->memsz - ph->filesz);
    }

    entry = (void(*)(void))elf->entry;
    entry();
}
示例#2
0
void read_from_clients(fd_set *fdset)
{
	char buf[48];
	int i;
	int sock;
	int n;
	for (i=0; i < num_clients; i++) {
		sock = clients[i].sock;
		if (!FD_ISSET(sock, fdset))
			continue;
		if (!readbytes(sock, buf, 1) || buf[0] >= '\t' &&
						buf[0] < '\033') {
			i = remove_client(i);
			continue;
		}
		n = 0;
		switch (buf[0]) {
		case 'N':
			if (readbytes(sock, buf+1, 1)) {
				if (buf[1] == '_') {
					send_opponent_name(i);
					continue;
				}
				if (readbytes(sock, buf+2, 15)) {
					memcpy(clients[i].name, buf+1, 16);
					continue;
				}
			}
			i = remove_client(i);
			continue;
		case 'b':
		case 'g':
		case 'h':
		case 'w':
			n = 1;
			break;
		case 'p':
			writebytes(sock, "N_", 2);
		case 'm':
		case 'n':
			n = 2;
			break;
		case 'x':
			n = 4;
		}
		if (n && !readbytes(sock, buf+1, n) ||
		    !fwd_to_opponent(i, buf, n+1))
			i = remove_client(i);
		else if (buf[0] == 'b') {
			n = buf[1];
			if (n > 0 && n <= 12) {
				n *= 4;
				if (!readbytes(sock, buf, n) ||
				    !fwd_to_opponent(i, buf, n))
					i = remove_client(i);
			}
		}
	}
}
示例#3
0
文件: comm.c 项目: gsrr/Python
static void recvnext()
{
	struct tetr next;
	char s[2];
	char *p = tetrom_seq;
	if (!p || !readbytes(s, 2) || s[1] > 6)
		return;
	switch (*s) {
	case '2':
		p += 2;
	case '1':
		break;
	default:
		return;
	}
	if (*p != 0x7F)
		p++;
	else {
		gettetrom(&next, s[1]);
		drawnext(game->player+(*s=='2'), &next);
		if (game->next)
		    *game->next = next;
	}
	*p = s[1];
}
示例#4
0
 inline char* readstring2(){
     //Read a Pascal string with 2 length bytes
     unsigned length = readint16();
     char *string = (char*) malloc(length+1);
     readbytes(string, length);
     string[length] = '\0';
     return string;
 }
示例#5
0
文件: comm.c 项目: gsrr/Python
static int recvboard_read(char *s, int n)
{
	while (n > 0) {
		if (!readbytes(s, 4))
			return 0;
		s += 4;
		n--;
	}
	return 1;
}
示例#6
0
文件: comm.c 项目: gsrr/Python
static void set_level_height(int flags)
{
	char s[2];
	int n;
	if (readbytes(s, 2) && !(flags & IN_GAME) && !game_over) {
		n = s[0]-'0';
		if (n >= 0 && n <= 9)
			player1.startlevel = n;
		n = s[1]-'0';
		if (n >= 0 && n <= 5)
			player1.height = n;
	}
}
示例#7
0
文件: comm.c 项目: gsrr/Python
static void recvplayer()
{
	char s[2];
	if (readbytes(s, 2)) {
		if (s[0] > 9 || s[0] < 0) {
			sock_flags |= CONN_BROKEN;
			return;
		}
		player2.startlevel = s[0];
		player2.rotationsys = s[1];
		writebytes("N_", 2);
	}
}
示例#8
0
int readbytes(int sock, char *dest, int n)
{
	int i;
	if (!waitinput_1sec(sock))
		return 0;
	i = recv(sock, dest, n, 0);
	if (i == n)
		return 1;
	if (i < 0 && errno == EINTR)
		i = 0;
	else if (i <= 0)
		return 0;
	return readbytes(sock, dest+i, n-i);
}
示例#9
0
static  int     FillBuffer( b_file *io )
//======================================
{
    size_t      bytes_read;

    if( FlushBuffer( io ) < 0 )
        return( -1 );
    bytes_read = readbytes( io, io->buffer, io->buff_size );
    if( bytes_read == READ_ERROR )
        return( -1 );
    io->attrs |= READ_AHEAD;
    io->high_water = 0;
    io->read_len = bytes_read;
    return( 0 );
}
示例#10
0
文件: comm.c 项目: gsrr/Python
static void recvboard()
{
	char s[48];
	unsigned char *b = (unsigned char *) s;
	uint_least32_t row;
	int n, m = 0;
	int i = 20;
	int j;
	if (!readbytes(s, 1)) 
		return;
	n = *s;
	if (n < 1 || n > 12) {
		conn_broken(0);
		return;
	}
	if (!recvboard_read(s, n) || !tetrom_seq)
		return;
	if (!game->state)
		m = p1_height_lines();
	if (!player1.height && sock_flags & SAME_HEIGHT) {
		m = n;
		p1_lines_height(n);
	}
	while (n) {
		row = b[3];
		for (j = 2; j >= 0; j--) {
			row <<= 8;
			row |= b[j];
		}
		player2.board[--i] = row;
		b += 4;
		n--;
	}
	n = 20-i;
	if (n == m)
		copy_same_height(n);
	redrawboard(&player2, 19);
	if (!game_running)
		print_press_key();
}
示例#11
0
void load_trace_raw ( char* fname )
{
	unsigned long totalBytes = getFileSize ( fname );
	unsigned long currBytes = 0;

	FILE* fp = fopen ( fname, "rb" );
	char header[2048];
	char buf[2048];

	Call cl;
	Event e;
	char typ, nameID;
	unsigned long long tstart, tstop;
	int fnum, size = 0;	
	int cstart = 0, cnum = 0;
	mMaxSize = 1;

	int num_frame = 0;
	int num_draw = 0;

	Frame f;
	f.clear ();
	frame = 0;

	while ( !feof(fp) && (num_draw < maxDraw || maxDraw==0)) {
			
		readbytes ( header, 18, 1, fp );		// 18 byte header
		parseHeader ( header, typ, nameID, tstart, tstop );
		switch ( typ ) {
		case 'C': {
			readbytes ( buf, 20, 1, fp );			
			if ( num_frame >= startFrame ) {				
				parseTrace ( buf, nameID, cl );
				mCalls.push_back ( cl );
				cnum++;
			}
			} break;
		case 'D': {
			
				currBytes = getFilePos ( fp );
				if ( f.totalDraw % 100 == 0 ) {
					if ( maxDraw == 0 ) {
						app_printf ( "%dk read, %.2f%% of file, %.2f%% done\n", currBytes/1024, currBytes*100.0f/totalBytes, currBytes*100.0f/totalBytes );	
					} else {
						app_printf ( "%dk read, %.2f%% of file, %.2f%% done\n", currBytes/1024, currBytes*100.0f/totalBytes, num_draw*100.0f/maxDraw );	
					}
				}				
				readbytes ( buf, NUM_BIN*9 + 9, 1, fp );
				
				if ( num_frame >= startFrame ) {
					parseTrace ( buf, nameID, e );
				
					e.frame = frame;
					e.call_num = cnum;
					e.call_start = cstart;
					mEvents.push_back ( e );
			
					cstart += cnum;
					cnum = 0;
					if ( e.bin_size[BIN_DRAW] > mMaxSize ) mMaxSize = e.bin_size[BIN_DRAW];
					if ( e.bin_id[BIN_DRAW] > mMaxPrim ) mMaxPrim = e.bin_id[BIN_DRAW];
					num_draw++;
		
					for (int n=0; n < NUM_BIN; n++) {
						f.binChange[n] += (e.bin_change[n]==BIN_CREATE || e.bin_change[n]==BIN_CHANGE) ? 1 : 0;
						f.binSwitch[n] += (e.bin_change[n]==BIN_SWITCH ) ? 1 : 0;
						f.binReuse[n] += (e.bin_change[n]==BIN_REUSE ) ? 1 : 0;
						f.binUnique[n] = (e.bin_id[n] > f.binUnique[n] ) ? e.bin_id[n] : f.binUnique[n];
					}
					f.totalDraw++;
					f.totalTransfer += e.bin_size[BIN_DRAW];
					f.totalPrim += e.bin_id[BIN_DRAW];		
				}
			} break;
		case 'F': {
			
			readbytes ( buf, 8, 1, fp );
			
			if ( num_frame >= startFrame ) {
				mFrames.push_back ( f );		// record frame data
				f.clear ();
				frame++;
				parseTrace ( buf, fnum, size );
				e.name_id = nameID;
				e.name = "Present";
				e.call_num = cnum;
				e.call_start = cstart;			
				e.count = 1;
				for (int n=0; n < NUM_BIN; n++ ) {
					e.bin_id[n] = -1;
					e.bin_change[n] = -1;
				}
				mEvents.push_back ( e );
				cstart += cnum;			
				cnum = 0;				
			}
			num_frame++;

			} break;
		};
	}
	// read may not have gotten to end of frame
	mFrames.push_back ( f );

	if ( mFrames.size() == 0 || mEvents.size() == 0 ) {
		app_printf ( "Error: No frames or events detected.\n" );
		app_printf ( "Try running trace again. \n" );
		_getch();
		exit(-1);
	}

	fclose ( fp );

}
示例#12
0
size_t SysRead( b_file *io, char *b, size_t len )
//===============================================
{
    size_t      bytes_read;
    size_t      amt;
    size_t      offs_in_b;
    size_t      max_valid;

    if( io->attrs & BUFFERED ) {
        // determine the maximum valid position in the buffer
        max_valid = io->read_len;
        if( max_valid < io->high_water ) {
            max_valid = io->high_water;
        }
        // if we're beyond that position then we must fill the buffer
        if( io->b_curs >= max_valid ) {
            if( FillBuffer( io ) < 0 )
                return( READ_ERROR );
            max_valid = io->read_len;
        }
        amt = max_valid - io->b_curs;
        if( amt > len ) {
            amt = len;
        }
        memcpy( b, &io->buffer[io->b_curs], amt );
        offs_in_b = amt;
        io->b_curs += amt;
        len -= amt;
        if( len ) {
            // flush the buffer
            if( FlushBuffer( io ) < 0 )
                return( READ_ERROR );
            if( len > io->buff_size ) {
                // read a multiple of io->buff_size bytes
                amt = len - len % io->buff_size;
                bytes_read = readbytes( io, b + offs_in_b, amt );
                if( bytes_read == READ_ERROR )
                    return( READ_ERROR );
                offs_in_b += bytes_read;
                if( bytes_read < amt )
                    return( offs_in_b );
                len -= amt;
            }
            if( len ) {
                // first fill the buffer
                bytes_read = readbytes( io, io->buffer, io->buff_size );
                if( bytes_read == READ_ERROR )
                    return( READ_ERROR );
                io->attrs |= READ_AHEAD;
                io->read_len = bytes_read;
                // then grab our bytes from it
                if( len > bytes_read ) {
                    len = bytes_read;
                }
                memcpy( b + offs_in_b, io->buffer, len );
                io->b_curs = len;
                offs_in_b += len;
            }
        }
        return( offs_in_b );
    } else {
        return( readbytes( io, b, len ) );
    }
}
示例#13
0
static size_t GetTextRec( b_file *io, char *b, size_t len )
//=========================================================
// Get a record from a TEXT file.
{
    char        ch;
    size_t      read;
    char        rs[2];

    if( io->attrs & SEEK ) { // direct access
        if( SysRead( io, b, len ) == READ_ERROR )
            return( 0 );
        if( SysRead( io, rs, sizeof( char ) ) == READ_ERROR )
            return( 0 );
        if( rs[0] == LF )
            return( len );
#if ! defined( __UNIX__ )
        if( rs[0] == CR ) {
            if( SysRead( io, &rs[1], sizeof( char ) ) == READ_ERROR ) {
                return( 0 );
            }
            if( rs[1] == LF )
                return( len );
            if( ( io->attrs & CARRIAGE_CONTROL ) && ( rs[1] == FF ) ) {
                return( len );
            }
        }
#endif
        FSetErr( IO_BAD_RECORD, io );
        return( 0 );
    } else if( io->attrs & BUFFERED ) {
        char            *ptr;
        char            *stop;
        bool            seen_cr;
        bool            trunc;
        size_t          max_valid;

        // determine maximum valid position in the buffer
        max_valid = io->read_len;
        if( max_valid < io->high_water ) {
            max_valid = io->high_water;
        }
        stop = io->buffer + max_valid;
        ptr = io->buffer + io->b_curs;
        read = 0;
        seen_cr = false;
        trunc = false;
        for( ;; ) {
            if( ptr >= stop ) {
                io->b_curs = ptr - io->buffer;
                if( FillBuffer( io ) < 0 ) {
                    // we have to do this so that io->b_curs is set properly
                    // on end of file
                    ptr = io->buffer + io->b_curs;
                    if( read > 0 && io->stat == IO_EOF ) {
                        IOOk( io );
                    }
                    break;
                }
                stop = io->buffer + io->read_len;
                ptr = io->buffer + io->b_curs;
            }
            ch = *ptr;
            ++ptr;
            if( ch == LF )
                break;
            if( !seen_cr ) {
                if( ch == CTRL_Z ) {
                    --ptr; // give back char so we don't read past EOF
                    if( read == 0 )
                        FSetEof( io );
                    break;
                }
                if( ch == CR ) {
                    seen_cr = true;
                } else if( read < len ) {
                    b[read] = ch;
                    ++read;
                } else {
                    trunc = true;
                }
            } else {
                if( ch == FF && (io->attrs & CARRIAGE_CONTROL) )
                    break;
                --ptr;  // give back the char
                seen_cr = false;
                if( read < len ) {
                    b[read] = CR;
                    ++read;
                } else {
                    trunc = true;
                }
            }
        }
        io->b_curs = ptr - io->buffer;
        if( trunc ) {
            FSetTrunc( io );
        }
        return( read );
    } else {    // device (CON)
        read = 0;
        len = readbytes( io, b, len );
        if( len == READ_ERROR )
            return( 0 );
        for( ;; ) {
            if( read == len )
                break;
#if defined( __UNIX__ ) || defined( __NETWARE__ )
            if( *b == LF )
                return( read );
#else
            if( *b == CR ) {
                ++b;
                if( read == len - 1 )
                    break;
                if( *b == LF )
                    return( read );
                --b;
            } else if( *b == CTRL_Z ) {
                FSetEof( io );
                return( read );
            }
#endif
            ++b;
            ++read;
        }
        FSetTrunc( io );
        return( read );
    }
}
示例#14
0
int     SysSeek( b_file *io, long int new_offset, int seek_mode )
{
    long int    curr_offset;
    long int    new_page;
    long int    page_offset;
    uint        bytes_read;

    curr_offset = CurrFileOffset( io );
    if( seek_mode == SEEK_CUR ) {
        new_offset += curr_offset;
    }
    if( io->attrs & WRITE_ONLY ) {
        if( curr_offset != new_offset ) {
            if( FlushBuffer( io ) < 0 ) {
                FSetSysErr( io );
                return( -1 );
            }
            if( lseek( io->handle, new_offset, SEEK_SET ) == -1 ) {
                FSetSysErr( io );
                return( -1 );
            }
            io->phys_offset = new_offset;
        }
        return( 0 );
    } else if( io->attrs & READ_AHEAD ) {
        page_offset = io->phys_offset - io->read_len;
        if( page_offset <= new_offset ) {
            if( (new_offset < io->phys_offset) ||
                ( (io->attrs & PAST_EOF) &&
                  (new_offset < page_offset + io->buff_size) ) ) {
                // we have the part of file in memory still, or we know we're
                // at the end of the file and the offset we want is on this
                // page as well
                io->b_curs = new_offset - io->phys_offset + io->read_len;
                return( 0 );
            }
        }
    } else if( io->phys_offset <= new_offset &&
                            new_offset < io->phys_offset + io->buff_size ) {
        io->b_curs = new_offset - io->phys_offset;
        if( ( io->attrs & PAST_EOF ) || io->b_curs < io->high_water ) {
            // We already know that the EOF is on this page so don't bother
            // seeking and reading.  Or we already have the part of the
            // file in the buffer.
            return( 0 );
        }
        // we have part of this page in memory already... so read the
        // rest of the page
        if( lseek( io->handle, io->high_water, SEEK_CUR ) < 0 ) {
            FSetSysErr( io );
            return( -1 );
        }
        io->phys_offset += io->high_water;
        bytes_read = readbytes( io, io->buffer + io->high_water,
                                            io->buff_size - io->high_water );
        if( bytes_read == READ_ERROR ) {
            if( io->stat != IO_EOF ) return( -1 );
            IOOk( io );
            io->phys_offset -= io->high_water;  // restore offset
            if( lseek( io->handle, -(long)io->high_water, SEEK_CUR ) < 0 ) {
                FSetSysErr( io );
                return( -1 );
            }
            io->attrs |= PAST_EOF;              // we now know the EOF is here
        } else {
            io->attrs |= READ_AHEAD;
            io->read_len = io->high_water + bytes_read;
            if( bytes_read < io->buff_size - io->high_water ) {
                io->attrs |= PAST_EOF;          // we now know the EOF is here
            }
        }
        return( 0 );
    }
    if( new_offset != curr_offset ) {
        if( FlushBuffer( io ) < 0 ) return( 0 );
        // round down to the nearest multiple of buff_size
        new_page = new_offset - new_offset % io->buff_size;
        if( lseek( io->handle, new_page, SEEK_SET ) < 0 ) {
            FSetSysErr( io );
            return( -1 );
        }
        io->phys_offset = new_page;
        io->b_curs = new_offset - new_page;
        bytes_read = readbytes( io, io->buffer, io->buff_size );
        if( bytes_read == READ_ERROR ) {
            if( io->stat != IO_EOF ) return( -1 );
            IOOk( io );
            io->attrs |= PAST_EOF;
        } else {
            if( bytes_read < io->buff_size ) {
                io->attrs |= PAST_EOF;
            } else {
                io->attrs &= ~PAST_EOF;
            }
            io->attrs |= READ_AHEAD;
            io->read_len = bytes_read;
        }
    }
    return( 0 );
}