Ejemplo n.º 1
0
static int hexToLong(char **ptr, long *longValue)
{
	int numChars = 0;
	int hexValue;

	*longValue = 0;

	while (**ptr) {
		hexValue = hex(**ptr);
		if (hexValue < 0)
			break;

		*longValue = (*longValue << 4) | hexValue;
		numChars ++;

		(*ptr)++;
	}

	return numChars;
}
Ejemplo n.º 2
0
/*
 * While we find nice hex chars, build an int.
 * Return number of chars processed.
 */
static int hexToInt(char **ptr, int *intValue)
{
	int numChars = 0;
	int hexValue;

	*intValue = 0;

	while (**ptr) {
		hexValue = hex(**ptr);
		if (hexValue < 0)
			break;

		*intValue = (*intValue << 4) | hexValue;
		numChars ++;

		(*ptr)++;
	}

	return (numChars);
}
Ejemplo n.º 3
0
Buffer Buffer::ToHex() const
{
	Buffer hex( len * 2 );
	u8 *hexData = (u8*)hex.Data();
	const u8 *data = (u8*)ptr;
	for( u32 i = 0; i < len; ++i )
	{
		int j = ( data[ i ] >> 4 ) & 0xf;
		if( j <= 9 )
			hexData[ i << 1 ] = ( j + '0' );
		else
			hexData[ i << 1 ] = ( j + 'a' - 10 );
		j = data[ i ] & 0xf;
		if( j <= 9 )
			hexData[ ( i << 1 ) + 1 ] = ( j + '0' );
		else
			hexData[ ( i << 1 ) + 1 ] = ( j + 'a' - 10 );
	}
	return hex;
}
Ejemplo n.º 4
0
static int rush2hash160(unsigned char *pass, size_t pass_sz) {
  unsigned char userpasshash[SHA256_DIGEST_LENGTH*2+1];

  SHA256_Init(sha256_ctx);
  SHA256_Update(sha256_ctx, pass, pass_sz);
  SHA256_Final(hash256, sha256_ctx);

  hex(hash256, sizeof(hash256), userpasshash, sizeof(userpasshash));

  SHA256_Init(sha256_ctx);
  // kdfsalt should be the fragment up to the !
  SHA256_Update(sha256_ctx, kdfsalt, kdfsalt_sz);
  SHA256_Update(sha256_ctx, userpasshash, 64);
  SHA256_Final(priv256, sha256_ctx);

  // early exit if the checksum doesn't match
  if (memcmp(priv256, rushchk, sizeof(rushchk)) != 0) { return -1; }

  return priv2hash160(priv256);
}
Ejemplo n.º 5
0
void AbstractInput::bind() {
  inputList.reset();
  lstring list = mapping.split(",");

  for(auto& mapping : list) {
    lstring values = mapping.split("/");
    if(values.size() == 1) continue;  //skip "None" mapping

    uint64_t id = hex(values[0]);
    string group = values(1, "");
    string input = values(2, "");
    string qualifier = values(3, "");

    Input item;
    for(auto device : inputManager->devices) {
      if(id != device->id) continue;
      if(group == "Rumble") {
        item.device = device;
        item.id = id;
        item.group = 0;
        item.input = 0;
        break;
      }
      if(auto groupID = device->find(group)) {
        if(auto inputID = device->group[groupID()].find(input)) {
          item.device = device;
          item.id = id;
          item.group = groupID();
          item.input = inputID();
          item.qualifier = Input::Qualifier::None;
          if(qualifier == "Lo") item.qualifier = Input::Qualifier::Lo;
          if(qualifier == "Hi") item.qualifier = Input::Qualifier::Hi;
          break;
        }
      }
    }
    if(item.device == nullptr) continue;

    inputList.append(item);
  }
}
Ejemplo n.º 6
0
void lt_check (time_t t, int min_year, int max_year)
{
    if (sizeof (time_t) > 4 && t > 0x7ffffffffffff000LL)
	tmp = NULL;
    else
	tmp = localtime (&t);
    if ( tmp == NULL ||
	/* Check tm_year overflow */
	 tmp->tm_year < min_year || tmp->tm_year > max_year) {
	if (opt_v)
	    fprintf (stderr, "localtime (%ld) failed with errno %d\n", t, errno);
	}
    else {
	if (opt_v)
	    fprintf (stderr, "%3d:%s: %12ld-%02d-%02d %02d:%02d:%02d\n",
		i, hex (t),
		tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
	pt = t;
	}
    } /* lt_check */
Ejemplo n.º 7
0
bool KviRijndaelHexEngine::asciiToBinary(const char * inBuffer, int * len, char ** outBuffer)
{
	KviCString hex(inBuffer);
	char * tmpBuf;
	*len = hex.hexToBuffer(&tmpBuf, false);
	if(*len < 0)
	{
		setLastError(__tr2qs("The message is not a hexadecimal string: this is not my stuff"));
		return false;
	}
	else
	{
		if(len > nullptr)
		{
			*outBuffer = (char *)KviMemory::allocate(*len);
			KviMemory::move(*outBuffer, tmpBuf, *len);
			KviCString::freeBuffer(tmpBuf);
		}
	}
	return true;
}
Ejemplo n.º 8
0
string Ananke::createBsxSatellaviewHeuristic(vector<uint8_t> &buffer) {
  string pathname = {
    libraryPath, "BS-X Satellaview/",
    nall::basename(information.name),
    ".bs/"
  };
  directory::create(pathname);

  file::write({pathname, "manifest.bml"}, {
    "unverified\n",
    "\n",
    "cartridge\n",
    "  rom name=program.rom size=0x", hex(buffer.size()), " type=FlashROM\n",
    "\n",
    "information\n",
    "  title: ", nall::basename(information.name), "\n"
  });
  file::write({pathname, "program.rom"}, buffer);

  return pathname;
}
Ejemplo n.º 9
0
int main()
{
    const my_facet f(1);
    std::ios ios(0);
    unsigned long v = -1;
    {
        const char str[] = "0";
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(v == 0);
    }
    {
        const char str[] = "1";
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(v == 1);
    }
    hex(ios);
    {
        const char str[] = "0xFFFFFFFF";
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(v == 0xFFFFFFFF);
    }
}
Ejemplo n.º 10
0
void dissector_entry_point(uint8_t *packet, struct frame_map *hdr, int linktype,
			      int mode, char **buffer_pkt,
			      uint8_t *switch_filter, uint8_t *stats)
{
	struct protocol *proto_start = NULL;
	struct protocol *proto_end = NULL;
	struct pkt_buff *pkt = NULL;

	if (mode == FNTTYPE_PRINT_NONE)
		return;

	pkt = pkt_alloc(packet, hdr->tp_h.tp_snaplen);

	switch (linktype) {
	case LINKTYPE_EN10MB:
		proto_start = dissector_get_ethernet_entry_point();
		proto_end = dissector_get_ethernet_exit_point();
		break;
	default:
		panic("Linktype not supported!\n");
	};

	dissector_main(pkt, proto_start, proto_end, buffer_pkt, switch_filter,
								    stats);


	switch (mode) {
	case FNTTYPE_PRINT_HEX:
		hex(pkt);
		break;
	case FNTTYPE_PRINT_ASCII:
		ascii(pkt);
		break;
	case FNTTYPE_PRINT_HEX_ASCII:
		hex_ascii(pkt);
		break;
	}
	tprintf_flush();
	pkt_free(pkt);
}
Ejemplo n.º 11
0
Archivo: gdb.c Proyecto: JanmanX/KUDOS
static int hexToNum(char **ptr, int *intValue, int length)
{
  int numChars = 0;
  int hexValue;

  *intValue = 0;

  while (**ptr && length > 0)
    {
      hexValue = hex(**ptr);
      if (hexValue < 0)
	break;

      *intValue = (*intValue << 4) | hexValue;
      numChars ++;

      (*ptr)++;
      length--;
    }

  return (numChars);
}
Ejemplo n.º 12
0
const char* dump(void* bytes, int size) {
    static char buf[51200];
    if (size > 10240) {
        return "<err: too long>";
    } else {
        int i = 0, buf_i = 0;
        for (;i < size; ++i) {
            unsigned char c = ((unsigned char*)bytes)[i];
            if (printable[c]) {
                buf[buf_i++] = c;
            } else if (c == '\\') {
                buf[buf_i++] = '\\';
                buf[buf_i++] = '\\';
            } else {
                hex(c, buf + buf_i);
                buf_i += 4;
            }
        }
        buf[buf_i] = '\x00';
        return buf;
    }
}
Ejemplo n.º 13
0
void mon_fill()
{
   filledframe(6,10,26,5);
   char ln[64]; sprintf(ln, "start: %04X end: %04X", addr, end);
   tprint(6,10, "    fill memory block     ", FRM_HEADER);
   tprint(7,12, "pattern (hex):", FFRAME_INSIDE);
   tprint(7,13, ln, FFRAME_INSIDE);

   static char fillpattern[10] = "00";

   unsigned char pattern[4];
   unsigned fillsize = 0;

   strcpy(str, fillpattern);
   if (!inputhex(22,12,8,true)) return;
   strcpy(fillpattern, str);

   if (!fillpattern[0])
       strcpy(fillpattern, "00");

   for (fillsize = 0; fillpattern[2*fillsize]; fillsize++) {
      if (!fillpattern[2*fillsize+1]) fillpattern[2*fillsize+1] = '0', fillpattern[2*fillsize+2] = 0;
      pattern[fillsize] = hex(fillpattern + 2*fillsize);
   }
   tprint(22,12,"        ", FFRAME_INSIDE);
   tprint(22,12,fillpattern, FFRAME_INSIDE);

   unsigned a1 = input4(14,13,addr); if (a1 == -1) return;
   addr = a1; tprint(14,13,str,FFRAME_INSIDE);
   a1 = input4(24,13,end); if (a1 == -1) return;
   end = a1;

   unsigned pos = 0;
   for (a1 = addr; a1 <= end; a1++) {
      cpu.DirectWm(a1, pattern[pos]);
      if (++pos == fillsize) pos = 0;
   }
}
Ejemplo n.º 14
0
string Ananke::createSufamiTurboHeuristic(vector<uint8_t> &buffer) {
  string pathname = {
    libraryPath, "Sufami Turbo/",
    nall::basename(information.name),
    ".st/"
  };
  directory::create(pathname);

  file::write({pathname, "manifest.bml"}, {
    "unverified\n",
    "\n",
    "cartridge\n",
    "  rom name=program.rom size=0x", hex(buffer.size()), "\n",
    "  ram name=save.ram size=0x2000\n",
    "\n",
    "information\n",
    "  title: ", nall::basename(information.name), "\n"
  });
  file::write({pathname, "program.rom"}, buffer);
  copySufamiTurboSaves(pathname);

  return pathname;
}
Ejemplo n.º 15
0
int hex2int(char **ptr, int *int_value)
{
    int num_chars = 0;
    int hex_value;

    *int_value = 0;

    while (**ptr)
    {
        hex_value = hex(**ptr);
        if (hex_value >=0)
        {
            *int_value = (*int_value <<4) | hex_value;
            num_chars ++;
        }
        else
            break;

        (*ptr)++;
    }

    return (num_chars);
}
Ejemplo n.º 16
0
void CPURegisterEditor::saveRegisters() {
  SFC::cpu.regs.a = hex(regAValue.text());
  SFC::cpu.regs.x = hex(regXValue.text());
  SFC::cpu.regs.y = hex(regYValue.text());
  SFC::cpu.regs.s = hex(regSValue.text());
  SFC::cpu.regs.d = hex(regDValue.text());
  SFC::cpu.regs.db = hex(regDBValue.text());
  SFC::cpu.regs.p.n = flagN.checked();
  SFC::cpu.regs.p.v = flagV.checked();
  SFC::cpu.regs.p.m = flagM.checked();
  SFC::cpu.regs.p.x = flagX.checked();
  SFC::cpu.regs.p.d = flagD.checked();
  SFC::cpu.regs.p.i = flagI.checked();
  SFC::cpu.regs.p.z = flagZ.checked();
  SFC::cpu.regs.p.c = flagC.checked();
  SFC::cpu.regs.e = flagE.checked();
  SFC::cpu.update_table();  //cache E/M/X flags
}
Ejemplo n.º 17
0
static int
hexToInt(char **ptr, long *intValue)
{
    int numChars = 0;
    int hexValue;

    *intValue = 0;

    while (**ptr)
    {
        hexValue = hex(**ptr);
        if (hexValue >=0)
        {
            *intValue = (*intValue <<4) | hexValue;
            numChars ++;
        }
        else
            break;

        (*ptr)++;
    }

    return (numChars);
}
Ejemplo n.º 18
0
QByteArray *Packet::fromHexString(QString string)
{
	QString stripped = string.remove(QRegExp("[^a-zA-Z0-9]")); //Removes everything except a-z (and capital) and 0-9

	//Error, its not an even numbered string (so not in correct form)
	if(stripped.size() % 2 != 0)
		return NULL;

	QByteArray *buffer = new QByteArray(stripped.size() % 2, '\0');
	bool good;
	for(int i = 0; i < stripped.size(); i+=2)
	{
		QStringRef hex(&stripped, i, 2);
		uint8 c = (uint8)hex.toString().toUShort(&good, 16);
		if(!good)
		{
			delete buffer;
			return NULL;
		}

		buffer->push_back(c);
	}
	return buffer;
}
Ejemplo n.º 19
0
QByteArray BasketUtils::toHex(QByteArray rawdata)
{
#if QT_VERSION >= 0x040300
    return rawdata.toHex();
#else
    // This code coping from Qt sources v4.6.3
    QByteArray hex(rawdata.size() * 2, 0);
    char *hexData = hex.data();
    const uchar *data = (const uchar *)rawdata.data();
    for (int i = 0; i < rawdata.size(); ++i) {
        int j = (data[i] >> 4) & 0xf;
        if (j <= 9)
            hexData[i*2] = (j + '0');
         else
            hexData[i*2] = (j + 'a' - 10);
        j = data[i] & 0xf;
        if (j <= 9)
            hexData[i*2+1] = (j + '0');
         else
            hexData[i*2+1] = (j + 'a' - 10);
    }
    return hex;
#endif
}
Ejemplo n.º 20
0
static long
hexToLong(char **ptr, long *longValue)
{
    int numChars = 0;
    int hexValue;

    *longValue = 0;

    while (**ptr)
    {
        hexValue = hex(**ptr);
        if (hexValue >=0)
        {
            *longValue = (*longValue <<4) | hexValue;
            numChars ++;
        }
        else
            break;

        (*ptr)++;
    }

    return (numChars);
}
Ejemplo n.º 21
0
static int nextrune(void)
{
	g.source += chartorune(&g.yychar, g.source);
	if (g.yychar == '\\') {
		g.source += chartorune(&g.yychar, g.source);
		switch (g.yychar) {
		case 0: die("unterminated escape sequence");
		case 'f': g.yychar = '\f'; return 0;
		case 'n': g.yychar = '\n'; return 0;
		case 'r': g.yychar = '\r'; return 0;
		case 't': g.yychar = '\t'; return 0;
		case 'v': g.yychar = '\v'; return 0;
		case 'c':
			g.yychar = (*g.source++) & 31;
			return 0;
		case 'x':
			g.yychar = hex(*g.source++) << 4;
			g.yychar += hex(*g.source++);
			if (g.yychar == 0) {
				g.yychar = '0';
				return 1;
			}
			return 0;
		case 'u':
			g.yychar = hex(*g.source++) << 12;
			g.yychar += hex(*g.source++) << 8;
			g.yychar += hex(*g.source++) << 4;
			g.yychar += hex(*g.source++);
			if (g.yychar == 0) {
				g.yychar = '0';
				return 1;
			}
			return 0;
		}
		if (strchr(ESCAPES, g.yychar))
			return 1;
		if (isalpharune(g.yychar) || g.yychar == '_') /* check identity escape */
			die("invalid escape character");
		return 0;
	}
	return 0;
}
Ejemplo n.º 22
0
static int nextrune(struct cstate *g)
{
	g->source += chartorune(&g->yychar, g->source);
	if (g->yychar == '\\') {
		g->source += chartorune(&g->yychar, g->source);
		switch (g->yychar) {
		case 0: die(g, "unterminated escape sequence");
		case 'f': g->yychar = '\f'; return 0;
		case 'n': g->yychar = '\n'; return 0;
		case 'r': g->yychar = '\r'; return 0;
		case 't': g->yychar = '\t'; return 0;
		case 'v': g->yychar = '\v'; return 0;
		case 'c':
			g->yychar = (*g->source++) & 31;
			return 0;
		case 'x':
			g->yychar = hex(g, *g->source++) << 4;
			g->yychar += hex(g, *g->source++);
			if (g->yychar == 0) {
				g->yychar = '0';
				return 1;
			}
			return 0;
		case 'u':
			g->yychar = hex(g, *g->source++) << 12;
			g->yychar += hex(g, *g->source++) << 8;
			g->yychar += hex(g, *g->source++) << 4;
			g->yychar += hex(g, *g->source++);
			if (g->yychar == 0) {
				g->yychar = '0';
				return 1;
			}
			return 0;
		}
		if (strchr(ESCAPES, g->yychar))
			return 1;
		if (isunicodeletter(g->yychar) || g->yychar == '_') /* check identity escape */
			die(g, "invalid escape character");
		return 0;
	}
	return 0;
}
Ejemplo n.º 23
0
/*
 * scan for the sequence $<data>#<checksum>
 */
static void getpacket(char *buffer)
{
    unsigned char checksum;
    unsigned char xmitcsum;
    int i;
    int count;
    unsigned char ch;

    do {
        /*
         * wait around for the start character,
         * ignore all other characters
         */
        while ((ch = (getDebugChar() & 0x7f)) != '$') ;

        checksum = 0;
        xmitcsum = -1;
        count = 0;

        /*
         * now, read until a # or end of buffer is found
         */
        while (count < BUFMAX) {
            ch = getDebugChar();
            if (ch == '#')
                break;
            checksum = checksum + ch;
            buffer[count] = ch;
            count = count + 1;
        }

        if (count >= BUFMAX)
            continue;

        buffer[count] = 0;

        if (ch == '#') {
            xmitcsum = hex(getDebugChar() & 0x7f) << 4;
            xmitcsum |= hex(getDebugChar() & 0x7f);

            if (checksum != xmitcsum)
                putDebugChar('-');    /* failed checksum */
            else {
                putDebugChar('+'); /* successful transfer */

                /*
                 * if a sequence char is present,
                 * reply the sequence ID
                 */
                if (buffer[2] == ':') {
                    putDebugChar(buffer[0]);
                    putDebugChar(buffer[1]);

                    /*
                     * remove sequence chars from buffer
                     */
                    count = strlen(buffer);
                    for (i=3; i <= count; i++)
                        buffer[i-3] = buffer[i];
                }
            }
        }
    }
    while (checksum != xmitcsum);
}
Ejemplo n.º 24
0
void TestCChannelRobustness()
{
	notes << "Testing CBytestream" << endl;
	CBytestream bsTest;
	bsTest.Test();
	notes << "\n\n\n\nTesting CChannel robustness" << endl;
	int lagMin = 50;
	int lagMax = 400;
	int packetLoss = 15; // In percents
	float packetsPerSecond1 = 10.0f; // One channel sends faster than another
	float packetsPerSecond2 = 0.2f;
	int packetExtraData = 8192; // Extra data in bytes to add to packet to check buffer overflows

	CChannel3 c1, c2;	//CChannel_056b c1, c2;
	SmartPointer<NetworkSocket> s1 = new NetworkSocket(); s1->OpenUnreliable(0);
	SmartPointer<NetworkSocket> s2 = new NetworkSocket(); s2->OpenUnreliable(0);
	SmartPointer<NetworkSocket> s1lag = new NetworkSocket(); s1lag->OpenUnreliable(0);
	SmartPointer<NetworkSocket> s2lag = new NetworkSocket(); s2lag->OpenUnreliable(0);
	NetworkAddr a1, a2, a1lag, a2lag;
	a1 = s1->localAddress();
	a2 = s2->localAddress();
	a1lag = s1lag->localAddress();
	a2lag = s2lag->localAddress();
	c1.Create( a1lag, s1 );
	c2.Create( a2lag, s2 );
	s1lag->setRemoteAddress( a2 );
	s2lag->setRemoteAddress( a1 );

	std::multimap< int, CBytestream > s1buf, s2buf;

	int i1=0, i2=0, i1r=0, i2r=0;
	float packetDelay1 = 10000000;
	if( packetsPerSecond1 > 0 )
		packetDelay1 = 1000.0f / packetsPerSecond1;
	float packetDelay2 = 10000000;
	if( packetsPerSecond2 > 0 )
		packetDelay2 = 1000.0f / packetsPerSecond2;
	float nextPacket1 = 0;
	float nextPacket2 = 0;
	for( int testtime=0; testtime < 100000; testtime+= 10, nextPacket1 += 10, nextPacket2 += 10 )
	{
		tLX->currentTime = AbsTime(testtime);

		// Transmit number sequence and some unreliable info
		CBytestream b1, b2, b1u, b2u;

		if( nextPacket1 >= packetDelay1 )
		{
			nextPacket1 = 0;
			i1++;
			b1.writeInt(i1, 4);
			for( int f=0; f<packetExtraData; f++ )
				b1.writeByte(0xff);
			c1.AddReliablePacketToSend(b1);
		}

		if( nextPacket2 >= packetDelay2 )
		{
			nextPacket2 = 0;
			i2++;
			b2.writeInt(i2, 4);
			for( int f=0; f<packetExtraData; f++ )
				b2.writeByte(0xff);
			c2.AddReliablePacketToSend(b2);
		}


		c1.Transmit( &b1u );
		c2.Transmit( &b2u );

		b1.Clear();
		b2.Clear();

		b1.Read(s1lag.get());
		b2.Read(s2lag.get());
		b1.ResetPosToBegin();
		b2.ResetPosToBegin();

		// Add the lag
		if( b1.GetLength() != 0 )
		{
			if( GetRandomInt(100) + 1 < packetLoss )
				notes << testtime << ": c1 sent packet - lost (" << c1.Messages.size() << 
						" in buf): " << printBinary(b1.readData()) << endl;
			else
			{
				int lag = ((testtime + lagMin + GetRandomInt(lagMax-lagMin)) / 10)*10; // Round to 10
				s1buf.insert( std::make_pair( lag, b1 ) );
				notes<< testtime << ": c1 sent packet - lag " << lag << " size " << b1.GetLength() << " (" << c1.Messages.size() << 
						" in buf): " << printBinary(b1.readData()) << endl;
			}
		}

		for( std::multimap< int, CBytestream > :: iterator it = s1buf.lower_bound(testtime);
				it != s1buf.upper_bound(testtime); it++ )
		{
			it->second.ResetPosToBegin();
			it->second.ResetPosToBegin();
			it->second.Send(s1lag.get());
		}

		if( b2.GetLength() != 0 )
		{
			if( GetRandomInt(100) + 1 < packetLoss )
				notes << testtime << ": c2 sent packet - lost (" << c2.Messages.size() <<
						" in buf): " << printBinary(b2.readData()) << endl;
			else
			{
				int lag = ((testtime + lagMin + GetRandomInt(lagMax-lagMin)) / 10)*10; // Round to 10
				s2buf.insert( std::make_pair( lag, b2 ) );
				notes << testtime << ": c2 sent packet - lag " << lag << " size " << b2.GetLength() << " (" << c2.Messages.size() <<
						" in buf): " << printBinary(b2.readData()) << endl;
			}
		}

		for( std::multimap< int, CBytestream > :: iterator it = s2buf.lower_bound(testtime);
				it != s2buf.upper_bound(testtime); it++ )
		{
			it->second.ResetPosToBegin();
			it->second.ResetPosToBegin();
			it->second.Send(s2lag.get());
		}

		// Receive and check number sequence and unreliable info
		b1.Clear();
		b2.Clear();

		b1.Read(s1.get());
		b2.Read(s2.get());
		b1.ResetPosToBegin();
		b2.ResetPosToBegin();

		if( b1.GetLength() != 0 )
		{
			notes << testtime << ": c1 recv packet (ping " << c1.getPing() << "): " << printBinary(b1.readData()) << endl;
			b1.ResetPosToBegin();
			while( c1.Process( &b1 ) )
			{
				while( b1.GetRestLen() != 0 )
				{
					int i1rr = b1.readInt(4);
					notes << testtime << ": c1 reliable packet, data " << hex(i1rr) << 
							" expected " << i1r+1 << " - " << (i1rr == i1r+1 ? "good" : "ERROR!") << endl;
					i1r = i1rr;
					for( int f=0; f<packetExtraData; f++ )
						b1.readByte();
				}
				b1.Clear();
			}
		}

		if( b2.GetLength() != 0 )
		{
			notes << testtime << ": c2 recv packet (ping " << c2.getPing() << "): " << printBinary(b2.readData()) << endl;
			b2.ResetPosToBegin();
			while( c2.Process( &b2 ) )
			{
				while( b2.GetRestLen() != 0 )
				{
					int i2rr = b2.readInt(4);
					notes << testtime << ": c2 reliable packet, data " << hex(i2rr) << 
							" expected " << i2r+1 << " - " << (i2rr == i2r+1 ? "good" : "ERROR!") << endl;
					i2r = i2rr;
					for( int f=0; f<packetExtraData; f++ )
						b2.readByte();
				}
				b2.Clear();
			}
		}

	}
}
Ejemplo n.º 25
0
int main()
{
    const my_facet f(1);
    std::ios ios(0);
    double v = -1;
    {
        const char str[] = "123";
        assert((ios.flags() & ios.basefield) == ios.dec);
        assert(ios.getloc().name() == "C");
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(v == 123);
    }
    {
        const char str[] = "-123";
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(v == -123);
    }
    {
        const char str[] = "123.5";
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(v == 123.5);
    }
    {
        const char str[] = "125e-1";
        hex(ios);
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(v == 125e-1);
    }
    {
        const char str[] = "0x125p-1";
        hex(ios);
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(v == hexfloat<double>(0x125, 0, -1));
    }
    {
        const char str[] = "inf";
        hex(ios);
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(v == INFINITY);
    }
    {
        const char str[] = "INF";
        hex(ios);
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(v == INFINITY);
    }
    {
        const char str[] = "-inf";
        hex(ios);
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(v == -INFINITY);
    }
    {
        const char str[] = "-INF";
        hex(ios);
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(v == -INFINITY);
    }
    {
        const char str[] = "nan";
        hex(ios);
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(std::isnan(v));
    }
    {
        const char str[] = "NAN";
        hex(ios);
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(std::isnan(v));
    }
    {
        v = -1;
        const char str[] = "123_456_78_9;125";
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+3);
        assert(err == ios.goodbit);
        assert(v == 123);
    }
    {
        v = -1;
        const char str[] = "2-";
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+1);
        assert(err == ios.goodbit);
        assert(v == 2);
    }
    {
        v = -1;
        const char str[] = "1.79779e+309"; // unrepresentable
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.failbit);
        assert(v == HUGE_VAL);
    }
    {
        v = -1;
        const char str[] = "-1.79779e+308"; // unrepresentable
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.failbit);
        assert(v == -HUGE_VAL);
    }
    ios.imbue(std::locale(std::locale(), new my_numpunct));
    {
        v = -1;
        const char str[] = "123_456_78_9;125";
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(v == 123456789.125);
    }
    {
        v = -1;
        const char str[] = "1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_"
                           "1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_"
                           "1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_"
                           "1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_"
                           "1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_"
                           "1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_"
                           "1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_"
                           "1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_"
                           "1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_"
                           "1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_"
                           "1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_1_2_3_4_5_6_7_8_9_0_";
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.failbit);
    }
    {
        v = -1;
        const char str[] = "3;14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651e+10";
        std::ios_base::iostate err = ios.goodbit;
        input_iterator<const char*> iter =
            f.get(input_iterator<const char*>(str),
                  input_iterator<const char*>(str+sizeof(str)),
                  ios, err, v);
        assert(iter.base() == str+sizeof(str)-1);
        assert(err == ios.goodbit);
        assert(std::abs(v - 3.14159265358979e+10)/3.14159265358979e+10 < 1.e-8);
    }
}
Ejemplo n.º 26
0
static int process_ihex(uint8_t *data, ssize_t size)
{
	struct ihex_binrec *record;
	uint32_t offset = 0;
	uint32_t data32;
	uint8_t type, crc = 0, crcbyte = 0;
	int i, j;
	int line = 1;
	int len;

	i = 0;
next_record:
	/* search for the start of record character */
	while (i < size) {
		if (data[i] == '\n') line++;
		if (data[i++] == ':') break;
	}

	/* Minimum record length would be about 10 characters */
	if (i + 10 > size) {
		fprintf(stderr, "Can't find valid record at line %d\n", line);
		return -EINVAL;
	}

	len = hex(data + i, &crc); i += 2;
	if (wide_records) {
		len <<= 8;
		len += hex(data + i, &crc); i += 2;
	}
	record = malloc((sizeof (*record) + len + 3) & ~3);
	if (!record) {
		fprintf(stderr, "out of memory for records\n");
		return -ENOMEM;
	}
	memset(record, 0, (sizeof(*record) + len + 3) & ~3);
	record->len = len;

	/* now check if we have enough data to read everything */
	if (i + 8 + (record->len * 2) > size) {
		fprintf(stderr, "Not enough data to read complete record at line %d\n",
			line);
		return -EINVAL;
	}

	record->addr  = hex(data + i, &crc) << 8; i += 2;
	record->addr |= hex(data + i, &crc); i += 2;
	type = hex(data + i, &crc); i += 2;

	for (j = 0; j < record->len; j++, i += 2)
		record->data[j] = hex(data + i, &crc);

	/* check CRC */
	crcbyte = hex(data + i, &crc); i += 2;
	if (crc != 0) {
		fprintf(stderr, "CRC failure at line %d: got 0x%X, expected 0x%X\n",
			line, crcbyte, (unsigned char)(crcbyte-crc));
		return -EINVAL;
	}

	/* Done reading the record */
	switch (type) {
	case 0:
		/* old style EOF record? */
		if (!record->len)
			break;

		record->addr += offset;
		file_record(record);
		goto next_record;

	case 1: /* End-Of-File Record */
		if (record->addr || record->len) {
			fprintf(stderr, "Bad EOF record (type 01) format at line %d",
				line);
			return -EINVAL;
		}
		break;

	case 2: /* Extended Segment Address Record (HEX86) */
	case 4: /* Extended Linear Address Record (HEX386) */
		if (record->addr || record->len != 2) {
			fprintf(stderr, "Bad HEX86/HEX386 record (type %02X) at line %d\n",
				type, line);
			return -EINVAL;
		}

		/* We shouldn't really be using the offset for HEX86 because
		 * the wraparound case is specified quite differently. */
		offset = record->data[0] << 8 | record->data[1];
		offset <<= (type == 2 ? 4 : 16);
		goto next_record;

	case 3: /* Start Segment Address Record */
	case 5: /* Start Linear Address Record */
		if (record->addr || record->len != 4) {
			fprintf(stderr, "Bad Start Address record (type %02X) at line %d\n",
				type, line);
			return -EINVAL;
		}

		memcpy(&data32, &record->data[0], sizeof(data32));
		data32 = htonl(data32);
		memcpy(&record->data[0], &data32, sizeof(data32));

		/* These records contain the CS/IP or EIP where execution
		 * starts. If requested output this as a record. */
		if (include_jump)
			file_record(record);
		goto next_record;

	default:
		fprintf(stderr, "Unknown record (type %02X)\n", type);
		return -EINVAL;
	}

	return 0;
}
Ejemplo n.º 27
0
int main()
{
    const my_facet f(1);
    {
        std::ios ios(0);
        long v = 0;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "0");
    }
    {
        std::ios ios(0);
        long v = 1;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "1");
    }
    {
        std::ios ios(0);
        long v = -1;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "-1");
    }
    {
        std::ios ios(0);
        long v = -1000;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "-1000");
    }
    {
        std::ios ios(0);
        long v = 1000;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "1000");
    }
    {
        std::ios ios(0);
        showpos(ios);
        long v = 1000;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "+1000");
    }
    {
        std::ios ios(0);
        oct(ios);
        long v = 1000;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "1750");
    }
    {
        std::ios ios(0);
        oct(ios);
        showbase(ios);
        long v = 1000;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "01750");
    }
    {
        std::ios ios(0);
        hex(ios);
        long v = 1000;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "3e8");
    }
    {
        std::ios ios(0);
        hex(ios);
        showbase(ios);
        long v = 1000;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "0x3e8");
    }
    {
        std::ios ios(0);
        hex(ios);
        showbase(ios);
        uppercase(ios);
        long v = 1000;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "0X3E8");
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        hex(ios);
        showbase(ios);
        uppercase(ios);
        long v = 1000;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "0X3E_8");
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        hex(ios);
        showbase(ios);
        long v = 2147483647;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "0x7f_fff_ff_f");
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        oct(ios);
        long v = 0123467;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "123_46_7");
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        oct(ios);
        showbase(ios);
        long v = 0123467;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "0_123_46_7");
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        oct(ios);
        showbase(ios);
        right(ios);
        ios.width(15);
        long v = 0123467;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "*****0_123_46_7");
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        oct(ios);
        showbase(ios);
        left(ios);
        ios.width(15);
        long v = 0123467;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "0_123_46_7*****");
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        oct(ios);
        showbase(ios);
        internal(ios);
        ios.width(15);
        long v = 0123467;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "*****0_123_46_7");
        assert(ios.width() == 0);
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        hex(ios);
        showbase(ios);
        right(ios);
        ios.width(15);
        long v = 2147483647;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "**0x7f_fff_ff_f");
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        hex(ios);
        showbase(ios);
        left(ios);
        ios.width(15);
        long v = 2147483647;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "0x7f_fff_ff_f**");
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        hex(ios);
        showbase(ios);
        internal(ios);
        ios.width(15);
        long v = 2147483647;
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "0x**7f_fff_ff_f");
        assert(ios.width() == 0);
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        showpos(ios);
        long v = 1000;
        right(ios);
        ios.width(10);
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "***+1_00_0");
        assert(ios.width() == 0);
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        showpos(ios);
        long v = 1000;
        left(ios);
        ios.width(10);
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "+1_00_0***");
        assert(ios.width() == 0);
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        showpos(ios);
        long v = 1000;
        internal(ios);
        ios.width(10);
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "+***1_00_0");
        assert(ios.width() == 0);
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        long v = -1000;
        right(ios);
        showpos(ios);
        ios.width(10);
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "***-1_00_0");
        assert(ios.width() == 0);
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        long v = -1000;
        left(ios);
        ios.width(10);
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "-1_00_0***");
        assert(ios.width() == 0);
    }
    {
        std::ios ios(0);
        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
        long v = -1000;
        internal(ios);
        ios.width(10);
        char str[50];
        std::ios_base::iostate err = ios.goodbit;
        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
        std::string ex(str, iter.base());
        assert(ex == "-***1_00_0");
        assert(ios.width() == 0);
    }
}
Ejemplo n.º 28
0
int main(int argc,char **argv)
	{
	if (argc < 2)
		{
		fputs("usage: readtype <input-file> <output-file>",stderr);
		exit(1);
		}

	input_filename = argv[1];
	output_filename = argv[2];

	// Locale support in previous version is deprecated.

	input_file = fopen(input_filename,"r");
	if (!input_file)
		{
		fprintf(stderr,"cannot open input file %s\n",input_filename);
		exit(1);
		}
	output_file = fopen(output_filename,"w");
	if (!output_file)
		{
		fprintf(stderr,"cannot open output file %s\n",output_filename);
		exit(1);
		}

	Data range_info;		// attributes of the current range
	Data unassigned_info;	// attributes used for unassigned characters; the default constructor
							// sets the category to Cn, bidirectional category to L, everything else to 0.
	TBool first = true;
	
	char line[1024];
	const int Fields = 15;
	char *field[Fields];
	TInt prev_code = 0;
	while (fgets(line,sizeof(line),input_file))
		{
		// Strip trailing newline if any.
		int length = strlen(line);
		if (length && line[length - 1] == '\n')
			line[length - 1] = 0;

		// Parse into fields.
		int n = 1;
		field[0] = line;
		for (char *p = line; *p; p++)
			if (*p == ';' && n < Fields)
				{
				*p = 0;
				field[n++] = p + 1;
				}

		// Ignore the line if there is only one field.
		if (n == 1)
			continue;

		// Extract fields of interest.

		// Field 0: Unicode value in hexadecimal.
		int code = hex(field[0]);

		// Field 2: Category.
		Data cur_info;
		cur_info.iCategory = (TUint8)Category(field[2], true);

		// Field 3: Combining class.
		cur_info.iCombiningClass = (TUint8)atoi(field[3]);
		
		// Field 4: Bidirectional category.
		cur_info.iBdCategory = (TUint8)BdCategory(field[4], true);

		// Prepare to determine the folded version (converted to lower case, stripped of accents).
		int folded_code = code;

		// Field 5: Character decomposition.
		if (field[5][0])
			{
			int components = 0;
			const int MaxComponents = 18;		// FDFA; ARABIC LIGATURE SALLALLAHOU ALAYHE WASALLAM has 18 components!
			TUint32 component[MaxComponents];

			// Extract the tag if any.
			char *p = field[5];
			const char *tag = NULL;
			if (field[5][0] == '<')
				{
				tag = ++p;
				while (*p && *p != '>')
					p++;
				if (!*p)
					{
					fprintf(stderr,"syntax error: missing > on the line for code %x\n",code);
					exit(1);
					}
				*p++ = 0;
				}

			// Read the components.
			while (*p)
				{
				while (*p == ' ')
					p++;
				if (components >= MaxComponents)
					{
					fprintf(stderr,"decomposition of %x has too many components: increase MaxComponents\n",code);
					exit(1);
					}
				component[components++] = hex(p);
				while (*p && *p != ' ')
					p++;
				}

			// Store the composition if it has a null tag and is therefore canonical.
			if (tag == NULL)
				{
				// Put its index into the tables.
				if (Compositions >= MaxCompositions)
					{
					fprintf(stderr,"too many compositions (at code %x): increase MaxCompositions\n",code);
					exit(1);
					}
				if (CompositionWords >= 65535)
					{
					fprintf(stderr, "too many compositions (at code %x): need 32 bit!?\n", code);
					exit(1);
					}
				Compose[Compositions] = Decompose[Compositions] = (TInt16)CompositionWords;
				Compositions++;

				// Put it into the composition buffer.
				if (CompositionWords + 2 + components >= MaxCompositionWords)
					{
					fprintf(stderr,"too many compositions (at code %x): increase MaxCompositionWords\n",code);
					exit(1);
					}
				CompositionBuffer[CompositionWords++] = code;
				CompositionBuffer[CompositionWords++] = components;
				for (int i = 0; i < components; i++)
					CompositionBuffer[CompositionWords++] = component[i];
				}
			
			// Store the code used in the ordinary and CJK fold tables.
			if (components > 0)
				{
				if (code < 256)
					{
					if (tag == NULL)
						folded_code = component[0];
					}
				else if (code >= 0xFF00 && code <= 0xFFEE)	// tag will always be <wide> or <narrow>
					folded_code = component[0];
				}
			}

		// Field 8. Numeric value.
		if (field[8][0])
			{
			if (field[8][1] == '/' || field[8][2] == '/')		// fractions
				cur_info.iFlags |= TUnicodeData::EFraction;
			else
				{
				int value = atoi(field[8]);
				if (value >= 0 && value <= 255)
					{
					cur_info.iDigitOffset = (TUint8)((value - (code & 255)) & 255);
					cur_info.iFlags |= TUnicodeData::ESmallNumeric;
					}
				else if (value == 500)
					cur_info.iFlags |= TUnicodeData::EFiveHundred;
				else if (value == 1000)
					cur_info.iFlags |= TUnicodeData::EOneThousand;
				else if (value == 5000)
					cur_info.iFlags |= TUnicodeData::EFiveThousand;
				else if (value == 10000)
					cur_info.iFlags |= TUnicodeData::ETenThousand;
				else if (value == 100000)
					cur_info.iFlags |= TUnicodeData::EHundredThousand;
				else
					fprintf(stderr,"Warning: U+%X has a large numeric property with unrepresentable value %d. Ignored.\n",code,value);
				}
			}

		// Field 9: Mirrored property.
		if (field[9][0] == 'Y')
			cur_info.iFlags |= TUnicodeData::EMirrored;

		// Fields 12, 13, 14: Case variants.
		int uc = code, lc = code, tc = code;
		if (field[12][0])
			{
			uc = hex(field[12]);
			int uc_offset = uc - code;
			if (abs(uc_offset) > 32767)
				{
				fprintf(stderr, "Warning: offset to upper case is too large: code %X, upper case %X, offset %X. Ignored!\n", code, uc, uc_offset);
				}
			else
				{
				cur_info.iFlags |= TUnicodeData::EHasUpperCase;
				cur_info.iCaseOffset = (TInt16)(-uc_offset);
				if (code<0x10000 && uc>0x10000 || code>0x10000 && uc<0x10000)
					fprintf(stderr, "Info: %X and its upper case %X locate at different planes.\n");
				}
			}
		if (field[13][0])
			{
			lc = hex(field[13]);
			int lc_offset = lc - code;
			if (abs(lc_offset) > 32767)
				{
				fprintf(stderr, "Warning: offset to lower case is too large: code %X, lower case %X, offset %X. Ignored!\n", code, lc, lc_offset);
				}
			else
				{
				cur_info.iFlags |= TUnicodeData::EHasLowerCase;
				cur_info.iCaseOffset = (TInt16)lc_offset;
				if (code<0x10000 && lc>0x10000 || code>0x10000 && lc<0x10000)
					fprintf(stderr, "Info: %X and its lower case %X locate at different planes.\n");
				}
			}
		if (field[14][0])
			tc = hex(field[14]);
		if (tc != lc && tc != uc)
			cur_info.iFlags |= TUnicodeData::EHasTitleCase;

		// If this code is < 256 fill in the entries in the special tables.
		if (code < 256)
			{
			LowerCaseTable[code] = (TUint16)lc;
			FoldTable[code] = (TUint16)folded_code;
			}

		// If the code is >= 0xFF00 fill in the entry in the CJK width folding table.
		else if (code >= 0xFF00 && code <= 0xFFFF)
			CjkWidthFoldTable[code & 0xFF] = (TUint16)folded_code;

		/*
		If there was a gap between this code and the previous one, write an 'unassigned' range,
		unless this character is actually the end of a range not fully listed (like the CJK ideographs
		from 4E00 to 9FA5 inclusive), in which case the character name will end in ' Last>'.
		*/
		if (code - prev_code > 1)
			{
			TBool last_in_range = false;
			int name_length = strlen(field[1]);
			if (name_length >= 6 && !strcmp(field[1] + name_length - 6," Last>"))
				last_in_range = TRUE;
			if (!last_in_range)
				{
				add_range(unassigned_info,prev_code + 1);
				range_info = unassigned_info;
				}
			}

		// Write the range.
		if (first || cur_info != range_info)
			{
			add_range(cur_info,code);
			range_info = cur_info;
			}

		first = false;
		prev_code = code;
		}

	/*
	If there was a gap at the end of the encoding (there is at present; FFFE and FFFF are not Unicode characters)
	write an 'unassigned' range.
	*/
	if (prev_code < 0xFFFF)
		add_range(unassigned_info,prev_code + 1);

	// Write an array of indices from Unicode character values to character data sets.
	for (int i = 0; i < Ranges; i++)
		{
		TUint32 end = i < Ranges - 1 ? TheRange[i + 1].iRangeStart : 0x110000;
		for (TUint32 j = TheRange[i].iRangeStart; j < end; j++)
			TheIndex[j] = TheRange[i].iIndex;
		}

	// Write the output file.
	write_output();
	printf("\nDone.\n");

	return 0;
	}
Ejemplo n.º 29
0
/*
 * Writes hex_len hex characters (1/2 byte) to hex from bytes.
 */
std::string bytes_to_hex(const uint8_t* bytes, size_t bytes_len) {
    std::string hex("0x");
    for (size_t i = 0; i < bytes_len; i++)
        android::base::StringAppendF(&hex, "%02x", bytes[i]);
    return hex;
}
Ejemplo n.º 30
0
char* _0f_dis16(unsigned char* arg0, int* arg1) {

	static char buffer[0x100];

	char* o;
	char* p;
	char* r;
	char* r8;
	char* r32;
	char* sreg;
	char* rm;
	char* rm8;
	char* rm32;
	int index;
	int count;

	p = prefix(arg0, &index);
	o = _0f_opcode(&arg0[index+1]);
	r_rm(&arg0[index + 2], &count, &r, &rm, &r8, &r32, &sreg, &rm8, &rm32);

	switch (arg0[index+1])
	{
	case 0x80:
	case 0x81:
	case 0x82:
	case 0x83:
	case 0x84:
	case 0x85:
	case 0x86:
	case 0x87:
	case 0x88:
	case 0x89:
	case 0x8a:
	case 0x8b:
	case 0x8c:
	case 0x8d:
	case 0x8e:
	case 0x8f:
		snprintf(buffer, sizeof(buffer), "%s    %s %s +0x%02x%02x", hex(arg0, index + 2 + 2), p, o, arg0[index + 1 + 1], arg0[index + 1]);
		return buffer;
	case 0x90:
	case 0x91:
	case 0x92:
	case 0x93:
	case 0x94:
	case 0x95:
	case 0x96:
	case 0x97:
	case 0x98:
	case 0x99:
	case 0x9a:
	case 0x9b:
	case 0x9c:
	case 0x9d:
	case 0x9e:
	case 0x9f:
		snprintf(buffer, sizeof(buffer), "%s    %s %s %s", hex(arg0, index + 2 + count), p, o, rm8 );
		return buffer;
	default:
		return "unknown";
	}

}