Esempio n. 1
0
static void
dprintstr(char *str, const Char *f, const Char *t)
{
    dprintf("%s:\"", str);
    while (f < t) {
	if (ASC(*f) & ~ASCII)
	  dprintf("[%x]", *f++);
	else
	  dprintf("%c", CTL_ESC(ASCII & ASC(*f++)));
    }
    dprintf("\"\r\n");
}
Esempio n. 2
0
int
eject_media(cd_device *dev)
{
	if (vol_running) {
		/* If there is a media, try using DKIOCEJECT 1st */
		if (check_device(dev, CHECK_NO_MEDIA) == 0) {
			if (ioctl(dev->d_fd, DKIOCEJECT, 0) == 0) {
				return (1);
			}
		}
	}
	if (load_unload(dev->d_fd, 0) == 0) {
		/* if eject fails */
		if ((uscsi_status == 2) && (ASC(rqbuf) == 0x53)) {
			/*
			 * check that eject is not blocked on the device
			 */
			if (!prevent_allow_mr(dev->d_fd, 1))
				return (0);
			return (load_unload(dev->d_fd, 0));
		}
		return (0);
	}
	return (1);
}
Esempio n. 3
0
int main() {
	int arr[26] = {0};
	int i, m1, m2, idx;
	char c, *p = input;
	scanf("%s", p);

	while (c = *p++)  {
		int idx = ASC(c);
		arr[idx]++;
	}

	m1 = m2 = 0;
	for(i = 0; i < 26; i++) {
		if (arr[i] >= m1) {
			m2 = m1;
			m1 = arr[i];
			idx = i;
		}
	}

	if (m1 == m2)
		printf("?\n");
	else
		printf("%c\n", CHR(idx));
	return 0;
}
Esempio n. 4
0
static void info_scsi_cmd_err(const char *cmd, int err) {
        if (err == -1) {
                log_debug("%s failed", cmd);
                return;
        }
        log_debug("%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh", cmd, SK(err), ASC(err), ASCQ(err));
}
Esempio n. 5
0
static void info_scsi_cmd_err(struct udev *udev, char *cmd, int err)
{
	if (err == -1) {
		info(udev, "%s failed\n", cmd);
		return;
	}
	info(udev, "%s failed with SK=%Xh/ASC=%02Xh/ACQ=%02Xh\n", cmd, SK(err), ASC(err), ASCQ(err));
}
Esempio n. 6
0
void testsymbit()
{
	std::string const fn = "mem://tmp";

	uint64_t const n = 16*1024+37;
	libmaus2::autoarray::AutoArray<libmaus2::huffman::SymBit> ASC(n);

	for ( uint64_t i = 0; i < n; ++i )
	{
		ASC[i].sbit = libmaus2::random::Random::rand64() % 2;
	}
	for ( uint64_t i = 0; i < n; )
	{
		uint64_t const z = libmaus2::random::Random::rand64() % 64;
		uint64_t const zz = std::min(n-i,z);

		// ASC[i].cnt = zz;

		i += zz;
	}
	for ( uint64_t i = 0; i < n; )
	{
		uint64_t const z = libmaus2::random::Random::rand64() % 64;
		uint64_t const zz = std::min(n-i,z);
		uint64_t const sym = libmaus2::random::Random::rand64() % 31;

		for ( uint64_t j = 0; j < zz; ++j )
			ASC[i++].sym = sym;
	}

	libmaus2::huffman::SymBitEncoderStd::unique_ptr_type senc(new libmaus2::huffman::SymBitEncoderStd(fn,512));
	for ( uint64_t i = 0; i < n; ++i )
		senc->encode(ASC[i]);
	senc.reset();

	assert ( libmaus2::huffman::SymBitDecoder::getLength(fn) == n );

	for ( uint64_t j = 0; j < n; ++j )
	{
		libmaus2::huffman::SymBitDecoder sdec(std::vector<std::string>(1,fn),j,1 /* numthreads */);
		libmaus2::huffman::SymBit SC;
		uint64_t i = 0;
		for ( ; sdec.decode(SC); ++i )
		{
			// std::cerr << SC.sym << "," << SC.cnt << "," << SC.sbit << std::endl;
			assert ( SC == ASC[i+j] );
		}
		assert ( i+j == n );
	}
}
Esempio n. 7
0
/*
 * Descriptor format sense data.  See spc3 p 31.  Returns length of
 * buffer used so far.
 */
int _sense_header_build(uint8_t *data, int len, uint8_t key, uint16_t code,
		       uint8_t additional_len, const char *file, int line)
{
	if (len < 8)
		return 0;
	data[0] = 0x72;  /* current, not deferred */
	data[1] = key;
	data[2] = ASC(code);
	data[3] = ASCQ(code);
	data[7] = additional_len;  /* additional length, beyond these 8 bytes */
	osd_warning("%s:%d: _sense_header_build key=%d code=%x additional_len=%d",
		 file, line, key, code, additional_len);

	return 8;
}
Esempio n. 8
0
int main()
{
  _10: LET A=3;
  _20: LET B=2;
  _30: DIM F(A,B);
  _40: LET I = 0;
  _50: LET J = 0;
  _60: FOR I = 1 TO A;
  _70: FOR J = 1 TO B;
  _80: F(I,J)=CHR$(I+5*J+ASC("A"));
  _90: NEXT;
 _100: NEXT;
 _110: LET LINE$="";
 _120: FOR I = 1 TO A;
 _130: FOR J = 1 TO B;
 _140: LINE$=LINE$+F(I,J);
 _150: NEXT;
 _160: PRINT LINE$;
 _170: LINE$="";
 _180: NEXT;
}
Esempio n. 9
0
// Returns true if a given apply site should be skipped during the
// early inlining pass.
//
// NOTE: Add here the checks for any specific @_semantics/@_effects
// attributes causing a given callee to be excluded from the inlining
// during the early inlining pass.
static bool shouldSkipApplyDuringEarlyInlining(FullApplySite AI) {
  // Add here the checks for any specific @_semantics attributes that need
  // to be skipped during the early inlining pass.
  ArraySemanticsCall ASC(AI.getInstruction());
  if (ASC && !ASC.canInlineEarly())
    return true;

  SILFunction *Callee = AI.getReferencedFunction();
  if (!Callee)
    return false;

  if (Callee->hasSemanticsAttr("self_no_escaping_closure") ||
      Callee->hasSemanticsAttr("pair_no_escaping_closure"))
    return true;

  // Add here the checks for any specific @_effects attributes that need
  // to be skipped during the early inlining pass.
  if (Callee->hasEffectsKind())
    return true;

  return false;
}
Esempio n. 10
0
int
eject_media(cd_device *dev)
{
	if (vol_running) {
		/* If there is a media, try using DKIOCEJECT 1st */
		if (check_device(dev, CHECK_NO_MEDIA) == 0) {
			/*
			 * The check_device() call will issue
			 * a TEST UNIT READY (TUR) and retry many
			 * times when a DVD-R is present. The DKIOCEJECT
			 * ioctl will subsequently fail causing us to
			 * issue the LOAD/UNLOAD SCSI command to the device
			 * with out ejecting the media. Insted of letting
			 * this happen, issue a reset to the device before
			 * issuing the DKIOCEJCET ioctl.
			 */
			if (device_type == DVD_MINUS)
				reset_dev(dev->d_fd);

			if (ioctl(dev->d_fd, DKIOCEJECT, 0) == 0) {
				return (1);
			}
		}
	}
	if (load_unload(dev->d_fd, 0) == 0) {
		/* if eject fails */
		if ((uscsi_status == 2) && (ASC(rqbuf) == 0x53)) {
			/*
			 * check that eject is not blocked on the device
			 */
			if (!prevent_allow_mr(dev->d_fd, 1))
				return (0);
			return (load_unload(dev->d_fd, 0));
		}
		return (0);
	}
	return (1);
}
Esempio n. 11
0
void
write_fini(void)
{
	print_n_flush(gettext("Finalizing (Can take several minutes)..."));
	/* Some drives don't like this while in test write mode */
	if (!simulation) {
		if (!finalize(target)) {
			/*
			 * It is possible that the drive is busy writing the
			 * buffered portion. So do not get upset yet.
			 */
			(void) sleep(10);
			if (!finalize(target)) {
				if (debug) {
					(void) printf("status %x, %x/%x/%x\n",
					    uscsi_status, SENSE_KEY(rqbuf),
					    ASC(rqbuf), ASCQ(rqbuf));
				}

				/*
				 * Different vendor drives return different
				 * sense error info for CLOSE SESSION command.
				 * The Panasonic drive that we are using is
				 * one such drive.
				 */
				if (device_type == DVD_MINUS) {
					if (verbose) {
						(void) printf(
						    "skipping finalizing\n");
					}
				} else {

			/* l10n_NOTE : 'failed' as in finishing up...failed  */
					(void) printf(gettext("failed.\n"));

					err_msg(gettext(
					    "Could not finalize the disc.\n"));
					exit(1);
				}


			}
		}
		if (vol_running) {
			(void) eject_media(target);
		}
	} else if (check_device(target, CHECK_MEDIA_IS_NOT_BLANK)) {
		/*
		 * Some drives such as the pioneer A04 will retain a
		 * ghost TOC after a simulation write is done. The
		 * media will actually be blank, but the drive will
		 * report a TOC. There is currently no other way to
		 * re-initialize the media other than ejecting or
		 * to ask the drive to clear the leadout. The laser
		 * is currently off so nothing is written to the
		 * media (on a good behaving drive).
		 * NOTE that a device reset does not work to make
		 * the drive re-initialize the media.
		 */

		blanking_type = "clear_ghost";
		blank();

	}
	/* l10n_NOTE : 'done' as in "Finishing up...done"  */
	(void) printf(gettext("done.\n"));
}
Esempio n. 12
0
/*
 * Close session. This will write TOC.
 */
int
finalize(cd_device *dev)
{
	uchar_t *di;
	int count, ret, err;
	int immediate;
	int finalize_max;

	/*
	 * For ATAPI devices we will use the immediate mode and will
	 * poll the command for completion so that this command may
	 * not hog the channel. But for SCSI, we will use the treditional
	 * way of issuing the command with a large enough timeout. This
	 * is done because immediate mode was designed for ATAPI and some
	 * SCSI RW drives might not be even tested with it.
	 */
	if ((dev->d_inq[2] & 7) != 0) {
		/* SCSI device */
		immediate = 0;
	} else {
		/* non-SCSI (e.g ATAPI) device */
		immediate = 1;
	}

	/* We need to close track before close session */
	if (device_type == DVD_PLUS) {
		if (!close_track(dev->d_fd, 0, 0, immediate))
			return (0);
	}

	if (!close_track(dev->d_fd, 0, 1, immediate)) {
		/*
		 * For DAO mode which we use for DVD-RW, the latest MMC
		 * specification does not mention close_track. Some
		 * newer drives will return an ILLEGAL INSTRUCTION
		 * which we will ignore. We have also found a Panasonic
		 * drive which will return a MEDIA ERROR. It is safe
		 * to ignore both errors as this is not needed for
		 * these drives.
		 * This is kept for older drives which had needed
		 * us to issue close_track to flush the cache fully.
		 * once we are certain these drives have cleared the
		 * market, this can be removed.
		 */
		if (device_type == DVD_MINUS) {
			return (0);
		}
	} else {
		if (!immediate)
			return (1);
	}
	if (immediate) {
		(void) sleep(10);

		di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
		err = 0;

		if (device_type == CD_RW) {
			/* Finalization should not take more than 6 minutes */
			finalize_max = FINALIZE_TIMEOUT;
		} else {
			/* some DVD-RW drives take longer than 6 minutes */
			finalize_max = FINALIZE_TIMEOUT*2;
		}

		for (count = 0; count < finalize_max; count++) {
			ret = read_disc_info(dev->d_fd, di);
			if (ret != 0)
				break;
			if (uscsi_status != 2)
				err = 1;
			if (SENSE_KEY(rqbuf) == 2) {
				/* not ready but not becoming ready */
				if (ASC(rqbuf) != 4)
					err = 1;
			} else if (SENSE_KEY(rqbuf) == 5) {
				/* illegal mode for this track */
				if (ASC(rqbuf) != 0x64)
					err = 1;
			} else {
				err = 1;
			}
			if (err == 1) {
				if (debug) {
					(void) printf("Finalization failed\n");
					(void) printf("%x %x %x %x\n",
					    uscsi_status, SENSE_KEY(rqbuf),
					    ASC(rqbuf), ASCQ(rqbuf));
				}
				free(di);
				return (0);
			}
			if (uscsi_status == 2) {
				int i;
				/* illegal field in command packet */
				if (ASC(rqbuf) == 0x24) {
					/* print it out! */
					(void) printf("\n");
					for (i = 0; i < 18; i++)
						(void) printf("%x ",
						    (unsigned)(rqbuf[i]));
					(void) printf("\n");
				}
			}
			(void) sleep(5);
		}
		free(di);
	}
	return (ret);
}
Esempio n. 13
0
/*!
  \param Func
*/
xbShort xbExpn::ProcessFunction( char * Func )
{
/* 1 - pop function from stack
   2 - verify function name and get no of parms needed 
   3 - verify no of parms >= remainder of stack
   4 - pop parms off stack
   5 - execute function
   6 - push result back on stack
*/


  char   *buf = 0;
  xbExpNode *p1, *p2, *p3, *WorkNode, *FuncNode;
  xbShort  ParmsNeeded,len;
  char   ptype = 0;  /* process type s=string, l=logical, d=double */
  xbDouble DoubResult = 0;
  xbLong   IntResult = 0;
  FuncNode = (xbExpNode *) Pop();

  ParmsNeeded = GetFuncInfo( Func, 1 );

  if( ParmsNeeded == -1 ) {
    return XB_INVALID_FUNCTION;
  }
  else {
    ParmsNeeded = 0;
    if( FuncNode->Sibling1 ) ParmsNeeded++;
    if( FuncNode->Sibling2 ) ParmsNeeded++;
    if( FuncNode->Sibling3 ) ParmsNeeded++;
  }

  if( ParmsNeeded > GetStackDepth())
    return XB_INSUFFICIENT_PARMS;

  p1 = p2 = p3 = NULL;
  if( ParmsNeeded > 2 ) p3 = (xbExpNode *) Pop(); 
  if( ParmsNeeded > 1 ) p2 = (xbExpNode *) Pop(); 
  if( ParmsNeeded > 0 ) p1 = (xbExpNode *) Pop(); 
  memset( WorkBuf, 0x00, WorkBufMaxLen+1);

  if( strncmp( Func, "ABS", 3 ) == 0 ) {  
    ptype = 'd';
    DoubResult = ABS( GetDoub( p1 ));
  }
  else if( strncmp( Func, "ASC", 3 ) == 0 ) {  
    ptype = 'd';
    DoubResult = ASC( p1->StringResult );
  }
  else if( strncmp( Func, "AT", 2 ) == 0 ) {  
    ptype = 'd';
    DoubResult = AT( p1->StringResult, p2->StringResult );
  }
  else if( strncmp( Func, "CDOW", 4 ) == 0 ) {  
    ptype = 's';
    buf = CDOW( p1->StringResult );
  }
  else if( strncmp( Func, "CHR", 3 ) == 0 ) {  
    ptype = 's';
    buf = CHR( GetInt( p1 ));
  }
  else if( strncmp( Func, "CMONTH", 6 ) == 0 ) {  
    ptype = 's';
    buf = CMONTH( p1->StringResult );
  }
  else if( strncmp( Func, "CTOD", 4 ) == 0 ) {  
    ptype = 's';
    buf = CTOD( p1->StringResult );
  }
  else if( strncmp( Func, "DATE", 4 ) == 0 ) {  
    ptype = 's';
    buf = DATE();
  }
  else if( strncmp( Func, "DAY", 3 ) == 0 ) {  
    ptype = 'd';
    DoubResult = DAY( p1->StringResult );
  }
  else if( strncmp( Func, "DESCEND", 7 ) == 0 && p1->ExpressionType == 'C' ) {  
    ptype = 's';
    buf = DESCEND( p1->StringResult.c_str() );
  }
  else if( strncmp( Func, "DESCEND", 7 ) == 0 && p1->ExpressionType == 'N' ) {  
    ptype = 'd';
    DoubResult = DESCEND( GetDoub( p1 ));
  }
  else if( strncmp( Func, "DESCEND", 7 ) == 0 && p1->ExpressionType == 'D' ) {  
    xbDate d( p1->StringResult );
    ptype = 'd';
    DoubResult = DESCEND( d );
  }
  else if( strncmp( Func, "DOW", 3 ) == 0 ) {
    ptype = 'd';
    DoubResult = DOW( p1->StringResult );
  }
  else if( strncmp( Func, "DTOC", 4 ) == 0 ) {  
    ptype = 's';
    buf = DTOC( p1->StringResult );
  }
  else if( strncmp( Func, "DTOS", 4 ) == 0 ) {  
    ptype = 's';
    buf = DTOS( p1->StringResult );
  }
  else if( strncmp( Func, "EXP", 3 ) == 0 ) {  
    ptype = 'd';
    DoubResult = EXP( GetDoub( p1 ));
  }
  else if( strncmp( Func, "IIF", 3 ) == 0 ){
    ptype = 's';
    buf = IIF( p1->IntResult, p2->StringResult, p3->StringResult );
  }
  else if( strncmp( Func, "INT", 3 ) == 0 ) {  
    ptype = 'd';
    DoubResult = INT( GetDoub( p1 ));
  }
  else if( strncmp( Func, "ISALPHA", 7 ) == 0 ) {  
    ptype = 'l';
    IntResult = ISALPHA( p1->StringResult );
  }
  else if( strncmp( Func, "ISLOWER", 7 ) == 0 ) {  
    ptype = 'l';
    IntResult = ISLOWER( p1->StringResult );
  }
  else if( strncmp( Func, "ISUPPER", 7 ) == 0 ) {  
    ptype = 'l';
    IntResult = ISUPPER( p1->StringResult );
  }
  else if( strncmp( Func, "LEN", 3 ) == 0 ) {  
    ptype = 'd';
    DoubResult = LEN( p1->StringResult );
  }
  else if( strncmp( Func, "LEFT", 4 ) == 0 ) {  
    ptype = 's';
    buf = LEFT( p1->StringResult, INT( p2->DoubResult ));
  }
  else if( strncmp( Func, "LTRIM", 5 ) == 0 ) {  
    ptype = 's';
    buf = LTRIM( p1->StringResult );
  }  
  else if( strncmp( Func, "LOG", 3 ) == 0 ) {  
    ptype = 'd';
    DoubResult = LOG( GetDoub( p1 ));
  }  
  else if( strncmp( Func, "LOWER", 5 ) == 0 ) {  
    ptype = 's';
    buf = LOWER( p1->StringResult );
  }  
  else if( strncmp( Func, "MAX", 3 ) == 0 ) {  
    ptype = 'd';
    DoubResult = MAX( GetDoub( p1 ), GetDoub( p2 ));
  }  
  else if( strncmp( Func, "MIN", 3 ) == 0 ) {  
    ptype = 'd';
    DoubResult = MIN( GetDoub( p1 ), GetDoub( p2 ));
  }  
  else if( strncmp( Func, "MONTH", 5 ) == 0 ) {  
    ptype = 'd';
    DoubResult = MONTH( p1->StringResult );
  } 

  else if( strncmp( Func, "RECNO", 5 ) == 0 )
  {
    ptype = 'd';
    DoubResult = RECNO( FuncNode->dbf );
  }

  else if( strncmp( Func, "REPLICATE", 9 ) == 0 ) {
    ptype = 's';
    buf = REPLICATE( p1->StringResult, GetInt( p2 ));
  }
  else if( strncmp( Func, "RIGHT", 5 ) == 0 ) {
    ptype = 's';
    buf = RIGHT( p1->StringResult, GetInt( p2 ));
  }
  else if( strncmp( Func, "RTRIM", 5 ) == 0 ) {  
    ptype = 's';
    buf = RTRIM( p1->StringResult );
  }  
  else if( strncmp( Func, "SPACE", 5 ) == 0 ) {  
    ptype = 's';
    buf = SPACE( INT( GetDoub( p1 )));
  }
  else if( strncmp( Func, "SQRT", 4 ) == 0 ) {  
    ptype = 'd';
    DoubResult = SQRT( GetDoub( p1 ));
  }
  else if( strncmp( Func, "STRZERO", 7 ) == 0 && ParmsNeeded == 1 ) {
    ptype = 's';
    buf = STRZERO( p1->StringResult );
  }   
  else if( strncmp( Func, "STRZERO", 7 ) == 0 && ParmsNeeded == 2 ) {
    ptype = 's';
    buf = STRZERO( p1->StringResult, GetInt( p2 ));
  }   
  else if( strncmp( Func, "STRZERO", 7 ) == 0 && ParmsNeeded == 3 ) {
    ptype = 's';
    buf = STRZERO( p1->StringResult, GetInt( p2 ), GetInt( p3 ));
  }   

  else if( strncmp( Func, "STR", 3 ) == 0 && p3 ) {
    ptype = 's';
    if(p1->ExpressionType == 'N')
      buf = STR( p1->DoubResult, GetInt( p2 ), GetInt( p3 ));
    else
      buf = STR( p1->StringResult, GetInt( p2 ), GetInt( p3 ));
  }   

  else if( strncmp( Func, "STR", 3 ) == 0 && p2 ) {
    ptype = 's';
    buf = STR( p1->StringResult, GetInt( p2 ));
  }

  else if( strncmp( Func, "STR", 3 ) == 0 && p1 ) {
    ptype = 's';
    buf = STR( p1->StringResult );
  }
   
  else if( strncmp( Func, "SUBSTR", 6 ) == 0 ) {
    ptype = 's';
    buf = SUBSTR( p1->StringResult, GetInt( p2 ), GetInt( p3 )); 
  }
  else if( strncmp( Func, "TRIM", 4 ) == 0 ) {  
    ptype = 's';
    buf = TRIM( p1->StringResult );
  }  
  else if( strncmp( Func, "UPPER", 5 ) == 0 ) {  
    ptype = 's';
    buf = UPPER( p1->StringResult );
  }  
  else if( strncmp( Func, "VAL", 3 ) == 0 ) {  
    ptype = 'd';
    DoubResult = VAL( p1->StringResult );
  }  
  else if( strncmp( Func, "YEAR", 4 ) == 0 ) {  
    ptype = 'd';
    DoubResult = YEAR( p1->StringResult );
  }  
  if( p1 && !p1->InTree ) delete p1;
  if( p2 && !p2->InTree ) delete p2;
  if( p3 && !p3->InTree ) delete p3;
  if( !FuncNode->InTree ) delete FuncNode;
  if( buf ){
    len = strlen( buf );
    WorkNode = new xbExpNode;
    if( !WorkNode )
      return XB_NO_MEMORY;
    WorkNode->ResultLen = len + 1;
    
  } else {    
    len = 0;
    WorkNode = new xbExpNode;
    if( !WorkNode )
      return XB_NO_MEMORY;
    WorkNode->ResultLen = 0;
  }

  switch( ptype ){
   case 's':                               /* string or char result */
    WorkNode->DataLen = len;
    WorkNode->ExpressionType = 'C';
    WorkNode->Type = 's';
    WorkNode->StringResult = buf;
    break;
   case 'd':                               /* numeric result */
    WorkNode->DataLen = 0;
    WorkNode->ExpressionType = 'N';
    WorkNode->Type = 'd';
    WorkNode->DoubResult = DoubResult;
    break;
   case 'l':                               /* logical result */
    WorkNode->DataLen = 0;
    WorkNode->ExpressionType = 'L';
    WorkNode->Type = 'l';
    WorkNode->IntResult = IntResult;
    break;
   default:
    std::cout << "\nInternal error. " << ptype;
    break;
  }
  Push(WorkNode);
  return XB_NO_ERROR;
}
Esempio n. 14
0
static int
gdTcl_UtfToUniChar(char *str, Tcl_UniChar *chPtr)
/* str is the UTF8 next character pointer */
/* chPtr is the int for the result */
{
    int byte;

    /* HTML4.0 entities in decimal form, e.g. &#197; */
    byte = *((unsigned char *) str);
    if (byte == '&') {
        int i, n=0;

        byte = *((unsigned char *) (str+1));
        if (byte == '#') {
            for (i = 2; i < 8; i++) {
                byte = *((unsigned char *) (str+i));
                if (byte >= '0' && byte <= '9') {
                    n = (n * 10) + (byte - '0');
                }
                else
                    break;
            }
            if (byte == ';') {
                *chPtr = (Tcl_UniChar) n;
                return ++i;
            }
        }
    }

    /*
     * Unroll 1 to 3 byte UTF-8 sequences, use loop to handle longer ones.
     */

    byte = ASC(*((unsigned char *) str));
    if (byte < 0xC0) {
        /*
         * Handles properly formed UTF-8 characters between 0x01 and 0x7F.
         * Also treats \0 and naked trail bytes 0x80 to 0xBF as valid
         * characters representing themselves.
         */

        *chPtr = (Tcl_UniChar) byte;
        return 1;
    } else if (byte < 0xE0) {
        if ((ASC(str[1]) & 0xC0) == 0x80) {
            /*
             * Two-byte-character lead-byte followed by a trail-byte.
             */

            *chPtr = (Tcl_UniChar) (((byte & 0x1F) << 6) | (ASC(str[1]) & 0x3F));
            return 2;
        }
        /*
         * A two-byte-character lead-byte not followed by trail-byte
         * represents itself.
         */

        *chPtr = (Tcl_UniChar) byte;
        return 1;
    } else if (byte < 0xF0) {
        if (((ASC(str[1]) & 0xC0) == 0x80) && ((ASC(str[2]) & 0xC0) == 0x80)) {
            /*
             * Three-byte-character lead byte followed by two trail bytes.
             */

            *chPtr = (Tcl_UniChar) (((byte & 0x0F) << 12)
                                    | ((ASC(str[1]) & 0x3F) << 6) | (ASC(str[2]) & 0x3F));
            return 3;
        }
        /*
         * A three-byte-character lead-byte not followed by two trail-bytes
         * represents itself.
         */

        *chPtr = (Tcl_UniChar) byte;
        return 1;
    }
#if TCL_UTF_MAX > 3
    else {
        int ch, total, trail;

        total = totalBytes[byte];
        trail = total - 1;
        if (trail > 0) {
            ch = byte & (0x3F >> trail);
            do {
                str++;
                if ((ASC(*str) & 0xC0) != 0x80) {
                    *chPtr = byte;
                    return 1;
                }
                ch <<= 6;
                ch |= (ASC(*str) & 0x3F);
                trail--;
            } while (trail > 0);
            *chPtr = ch;
            return total;
        }
    }
Esempio n. 15
0
int
uscsi(int fd, struct uscsi_cmd *scmd)
{
	char		rqbuf[RQLEN];
	int		ret;
	int		i, retries, total_retries;
	int		max_retries = 20;

	scmd->uscsi_flags |= USCSI_RQENABLE;
	scmd->uscsi_rqlen = RQLEN;
	scmd->uscsi_rqbuf = rqbuf;

	for (retries = 0; retries < max_retries; retries++) {
		scmd->uscsi_status = 0;
		memset(rqbuf, 0, RQLEN);

		ret = ioctl(fd, USCSICMD, scmd);

		if ((ret == 0) && (scmd->uscsi_status == 2)) {
			ret = -1;
			errno = EIO;
		}
		if ((ret < 0) && (scmd->uscsi_status == 2)) {
			/*
			 * The drive is not ready to recieve commands but
			 * may be in the process of becoming ready.
			 * sleep for a short time then retry command.
			 * SENSE/ASC = 2/4 : not ready
			 * ASCQ = 0  Not Reportable.
			 * ASCQ = 1  Becoming ready.
			 * ASCQ = 4  FORMAT in progress.
			 * ASCQ = 7  Operation in progress.
			 */
			if ((SENSE_KEY(rqbuf) == 2) && (ASC(rqbuf) == 4) &&
			    ((ASCQ(rqbuf) == 0) || (ASCQ(rqbuf) == 1) ||
			    (ASCQ(rqbuf) == 4)) || (ASCQ(rqbuf) == 7)) {
				total_retries++;
				sleep(1);
				continue;
			}

			/*
			 * Device is not ready to transmit or a device reset
			 * has occurred. wait for a short period of time then
			 * retry the command.
			 */
			if ((SENSE_KEY(rqbuf) == 6) && ((ASC(rqbuf) == 0x28) ||
			    (ASC(rqbuf) == 0x29))) {
				sleep(1);
				total_retries++;
				continue;
			}
			/*
			 * Blank Sense, we don't know what the error is or if
			 * the command succeeded, Hope for the best. Some
			 * drives return blank sense periodically and will
			 * fail if this is removed.
			 */
			if ((SENSE_KEY(rqbuf) == 0) && (ASC(rqbuf) == 0) &&
			    (ASCQ(rqbuf) == 0)) {
				ret = 0;
				break;
			}

			HAL_DEBUG (("cmd: 0x%02x ret:%i status:%02x "
			    " sense: %02x ASC: %02x ASCQ:%02x\n",
			    (uchar_t)scmd->uscsi_cdb[0], ret,
			    scmd->uscsi_status,
			    (uchar_t)SENSE_KEY(rqbuf),
			    (uchar_t)ASC(rqbuf), (uchar_t)ASCQ(rqbuf)));
		}

		break;
	}

	if (retries) {
		HAL_DEBUG (("total retries: %d\n", total_retries));
	}

	return (ret);
}
Esempio n. 16
0
/* returns 0 if media was detected */
static int cd_profiles(struct udev *udev, int fd)
{
        struct scsi_cmd sc;
        unsigned char features[65530];
        unsigned int cur_profile = 0;
        unsigned int len;
        unsigned int i;
        int err;
        int ret;

        ret = -1;

        /* First query the current profile */
        scsi_cmd_init(udev, &sc);
        scsi_cmd_set(udev, &sc, 0, 0x46);
        scsi_cmd_set(udev, &sc, 8, 8);
        scsi_cmd_set(udev, &sc, 9, 0);
        err = scsi_cmd_run(udev, &sc, fd, features, 8);
        if ((err != 0)) {
                info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
                /* handle pre-MMC2 drives which do not support GET CONFIGURATION */
                if (SK(err) == 0x5 && IN_SET(ASC(err), 0x20, 0x24)) {
                        log_debug("drive is pre-MMC2 and does not support 46h get configuration command");
                        log_debug("trying to work around the problem");
                        ret = cd_profiles_old_mmc(udev, fd);
                }
                goto out;
        }

        cur_profile = features[6] << 8 | features[7];
        if (cur_profile > 0) {
                log_debug("current profile 0x%02x", cur_profile);
                feature_profile_media (udev, cur_profile);
                ret = 0; /* we have media */
        } else {
                log_debug("no current profile, assuming no media");
        }

        len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
        log_debug("GET CONFIGURATION: size of features buffer 0x%04x", len);

        if (len > sizeof(features)) {
                log_debug("cannot get features in a single query, truncating");
                len = sizeof(features);
        } else if (len <= 8)
                len = sizeof(features);

        /* Now get the full feature buffer */
        scsi_cmd_init(udev, &sc);
        scsi_cmd_set(udev, &sc, 0, 0x46);
        scsi_cmd_set(udev, &sc, 7, ( len >> 8 ) & 0xff);
        scsi_cmd_set(udev, &sc, 8, len & 0xff);
        scsi_cmd_set(udev, &sc, 9, 0);
        err = scsi_cmd_run(udev, &sc, fd, features, len);
        if ((err != 0)) {
                info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
                return -1;
        }

        /* parse the length once more, in case the drive decided to have other features suddenly :) */
        len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
        log_debug("GET CONFIGURATION: size of features buffer 0x%04x", len);

        if (len > sizeof(features)) {
                log_debug("cannot get features in a single query, truncating");
                len = sizeof(features);
        }

        /* device features */
        for (i = 8; i+4 < len; i += (4 + features[i+3])) {
                unsigned int feature;

                feature = features[i] << 8 | features[i+1];

                switch (feature) {
                case 0x00:
                        log_debug("GET CONFIGURATION: feature 'profiles', with %i entries", features[i+3] / 4);
                        feature_profiles(udev, &features[i]+4, MIN(features[i+3], len - i - 4));
                        break;
                default:
                        log_debug("GET CONFIGURATION: feature 0x%04x <ignored>, with 0x%02x bytes", feature, features[i+3]);
                        break;
                }
        }
out:
        return ret;
}
Esempio n. 17
0
static int cd_profiles(struct udev *udev, int fd)
{
	struct scsi_cmd sc;
	unsigned char features[65530];
	unsigned int cur_profile = 0;
	unsigned int len;
	unsigned int i;
	int err;

	/* First query the current profile */
	scsi_cmd_init(udev, &sc, features, sizeof(features));
	scsi_cmd_set(udev, &sc, 0, 0x46);
	scsi_cmd_set(udev, &sc, 8, 8);
	scsi_cmd_set(udev, &sc, 9, 0);
	err = scsi_cmd_run(udev, &sc, fd, features, 8);
	if ((err != 0)) {
		info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
		/* handle pre-MMC2 drives which do not support GET CONFIGURATION */
		if (SK(err) == 0x5 && ASC(err) == 0x20) {
			info(udev, "drive is pre-MMC2 and does not support 46h get configuration command\n");
			info(udev, "trying to work around the problem\n");
			return cd_profiles_old_mmc(udev, fd);
		}
		return -1;
	}

	cur_profile = features[6] << 8 | features[7];
	if (cur_profile > 0) {
		info(udev, "current profile 0x%02x\n", cur_profile);
	} else {
		info(udev, "no current profile, assuming no media\n");
		return -1;
	}

	switch (cur_profile) {
	case 0x03:
	case 0x04:
	case 0x05:
		info(udev, "profile 0x%02x \n", cur_profile);
		cd_media = 1;
		cd_media_mo = 1;
		break;
	case 0x08:
		info(udev, "profile 0x%02x media_cd_rom\n", cur_profile);
		cd_media = 1;
		cd_media_cd_rom = 1;
		break;
	case 0x09:
		info(udev, "profile 0x%02x media_cd_r\n", cur_profile);
		cd_media = 1;
		cd_media_cd_r = 1;
		break;
	case 0x0a:
		info(udev, "profile 0x%02x media_cd_rw\n", cur_profile);
		cd_media = 1;
		cd_media_cd_rw = 1;
		break;
	case 0x10:
		info(udev, "profile 0x%02x media_dvd_ro\n", cur_profile);
		cd_media = 1;
		cd_media_dvd_rom = 1;
		break;
	case 0x11:
		info(udev, "profile 0x%02x media_dvd_r\n", cur_profile);
		cd_media = 1;
		cd_media_dvd_r = 1;
		break;
	case 0x12:
		info(udev, "profile 0x%02x media_dvd_ram\n", cur_profile);
		cd_media = 1;
		cd_media_dvd_ram = 1;
		break;
	case 0x13:
		info(udev, "profile 0x%02x media_dvd_rw_ro\n", cur_profile);
		cd_media = 1;
		cd_media_dvd_rw = 1;
		cd_media_dvd_rw_ro = 1;
		break;
	case 0x14:
		info(udev, "profile 0x%02x media_dvd_rw_seq\n", cur_profile);
		cd_media = 1;
		cd_media_dvd_rw = 1;
		cd_media_dvd_rw_seq = 1;
		break;
	case 0x1B:
		info(udev, "profile 0x%02x media_dvd_plus_r\n", cur_profile);
		cd_media = 1;
		cd_media_dvd_plus_r = 1;
		break;
	case 0x1A:
		info(udev, "profile 0x%02x media_dvd_plus_rw\n", cur_profile);
		cd_media = 1;
		cd_media_dvd_plus_rw = 1;
		break;
	case 0x2A:
		info(udev, "profile 0x%02x media_dvd_plus_rw_dl\n", cur_profile);
		cd_media = 1;
		cd_media_dvd_plus_rw_dl = 1;
		break;
	case 0x2B:
		info(udev, "profile 0x%02x media_dvd_plus_r_dl\n", cur_profile);
		cd_media = 1;
		cd_media_dvd_plus_r_dl = 1;
		break;
	case 0x40:
		info(udev, "profile 0x%02x media_bd\n", cur_profile);
		cd_media = 1;
		cd_media_bd = 1;
		break;
	case 0x41:
	case 0x42:
		info(udev, "profile 0x%02x media_bd_r\n", cur_profile);
		cd_media = 1;
		cd_media_bd_r = 1;
		break;
	case 0x43:
		info(udev, "profile 0x%02x media_bd_re\n", cur_profile);
		cd_media = 1;
		cd_media_bd_re = 1;
		break;
	case 0x50:
		info(udev, "profile 0x%02x media_hddvd\n", cur_profile);
		cd_media = 1;
		cd_media_hddvd = 1;
		break;
	case 0x51:
		info(udev, "profile 0x%02x media_hddvd_r\n", cur_profile);
		cd_media = 1;
		cd_media_hddvd_r = 1;
		break;
	case 0x52:
		info(udev, "profile 0x%02x media_hddvd_rw\n", cur_profile);
		cd_media = 1;
		cd_media_hddvd_rw = 1;
		break;
	default:
		info(udev, "profile 0x%02x <ignored>\n", cur_profile);
		break;
	}


	len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
	info(udev, "GET CONFIGURATION: size of features buffer 0x%04x\n", len);

	if (len > sizeof(features)) {
		info(udev, "can not get features in a single query, truncating\n");
		len = sizeof(features);
	} else if (len <= 8) {
		len = sizeof(features);
	}

	/* Now get the full feature buffer */
	scsi_cmd_init(udev, &sc, features,  len);
	scsi_cmd_set(udev, &sc, 0, 0x46);
	scsi_cmd_set(udev, &sc, 7, ( len >> 8 ) & 0xff);
	scsi_cmd_set(udev, &sc, 8, len & 0xff);
	scsi_cmd_set(udev, &sc, 9, 0);
	err = scsi_cmd_run(udev, &sc, fd, features, len);
	if ((err != 0)) {
		info_scsi_cmd_err(udev, "GET CONFIGURATION", err);
		return -1;
	}

	/* parse the length once more, in case the drive decided to have other features suddenly :) */
	len = features[0] << 24 | features[1] << 16 | features[2] << 8 | features[3];
	info(udev, "GET CONFIGURATION: size of features buffer 0x%04x\n", len);

	if (len > sizeof(features)) {
		info(udev, "can not get features in a single query, truncating\n");
		len = sizeof(features);
	}

	/* device features */
	for (i = 8; i+4 < len; i += (4 + features[i+3])) {
		unsigned int feature;

		feature = features[i] << 8 | features[i+1];

		switch (feature) {
		case 0x00:
			info(udev, "GET CONFIGURATION: feature 'profiles', with %i entries\n", features[i+3] / 4);
			feature_profiles(udev, &features[i]+4, features[i+3]);
			break;
		default:
			info(udev, "GET CONFIGURATION: feature 0x%04x <ignored>, with 0x%02x bytes\n", feature, features[i+3]);
			break;
		}
	}

	return 0;
}
Esempio n. 18
0
static int
xenon_atapi_read_sectors(sec_t start_sector, sec_t sector_size, void *buf) {
	int maxretries = 20;
	//struct xenon_ata_device *dev = bdev->ctx;
	struct xenon_ata_device *dev = &atapi;
	struct xenon_atapi_read readcmd;

	readcmd.code = 0x28;
	readcmd.lba = start_sector;
	readcmd.length = sector_size;
	
#ifndef USE_DMA
	unsigned int sect = 0;
	void * bufpos = buf;
retry:
	xenon_atapi_packet(dev, (char *) &readcmd, 0);
	xenon_ata_wait_ready(dev);
	for (sect = 0; sect < sector_size; sect++) {
		if (xenon_ata_pio_read(dev, bufpos, XENON_CDROM_SECTOR_SIZE)) {
			int sense = xenon_atapi_request_sense(dev);

			// no media
			if (SK(sense) == 0x02 && ASC(sense) == 0x3a) {
				return DISKIO_ERROR_NO_MEDIA;
			}

			// becoming ready
			if ((SK(sense) == 0x02 && ASC(sense) == 0x4) || SK(sense) == 0x6) {
				if (!maxretries) return -1;
				mdelay(500);
				maxretries--;
				goto retry;
			}

			printf("ATAPI read error\n");
			return -1;
		}
		bufpos += XENON_CDROM_SECTOR_SIZE;
	}
#else
retry:
	xenon_atapi_packet(dev, (char *) &readcmd, 1);
	xenon_ata_wait_ready(dev);

	if (xenon_ata_dma_read(dev, buf, sector_size * XENON_CDROM_SECTOR_SIZE)) {
		int sense = xenon_atapi_request_sense(dev);

		// no media
		if (SK(sense) == 0x02 && ASC(sense) == 0x3a) {
			return DISKIO_ERROR_NO_MEDIA;
		}

		// becoming ready
		if ((SK(sense) == 0x02 && ASC(sense) == 0x4) || SK(sense) == 0x6) {
			if (!maxretries) return -1;
			mdelay(500);
			maxretries--;
			goto retry;
		}

		printf("ATAPI DMA read error\n");
		return -1;
	}
#endif

	return sector_size;
}