Exemple #1
0
static bool
parseConnectConfirmTpdu(CotpConnection* self, uint8_t* buffer, uint8_t len)
{
    if (len < 6)
        return false;

    self->srcRef = getUint16(buffer);
    self->dstRef = getUint16(buffer + 2);
    self->protocolClass = getUint8(buffer + 4);

    return parseOptions(self, buffer + 5, len - 6);
}
// Defined by the WebCrypto spec as:
//
//    dictionary AesCtrParams : Algorithm {
//      CryptoOperationData counter;
//      [EnforceRange] octet length;
//    };
bool parseAesCtrParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, String& errorDetails)
{
    RefPtr<ArrayBufferView> counter;
    if (!getCryptoOperationData(raw, "counter", counter, context, errorDetails))
        return false;

    uint8_t length;
    if (!getUint8(raw, "length", length, context, errorDetails))
        return false;

    params = adoptPtr(new blink::WebCryptoAesCtrParams(length, static_cast<const unsigned char*>(counter->baseAddress()), counter->byteLength()));
    return true;
}
Exemple #3
0
static bool
parseDataTpdu(CotpConnection* self, uint8_t* buffer, uint8_t len)
{
    if (len != 2)
        return false;

    uint8_t flowControl = getUint8(buffer);

    if (flowControl & 0x80)
        self->isLastDataUnit = true;
    else
        self->isLastDataUnit = false;

    return true;
}
Exemple #4
0
QList<Abi> Abi::abisOfBinary(const Utils::FileName &path)
{
    QList<Abi> tmp;
    if (path.isEmpty())
        return tmp;

    QFile f(path.toString());
    if (!f.exists())
        return tmp;

    if (!f.open(QFile::ReadOnly))
        return tmp;

    QByteArray data = f.read(1024);
    if (data.size() >= 67
            && getUint8(data, 0) == '!' && getUint8(data, 1) == '<' && getUint8(data, 2) == 'a'
            && getUint8(data, 3) == 'r' && getUint8(data, 4) == 'c' && getUint8(data, 5) == 'h'
            && getUint8(data, 6) == '>' && getUint8(data, 7) == 0x0a) {
        // We got an ar file: possibly a static lib for ELF, PE or Mach-O

        data = data.mid(8); // Cut of ar file magic
        quint64 offset = 8;

        while (!data.isEmpty()) {
            if ((getUint8(data, 58) != 0x60 || getUint8(data, 59) != 0x0a)) {
                qWarning() << path.toString() << ": Thought it was an ar-file, but it is not!";
                break;
            }

            const QString fileName = QString::fromLocal8Bit(data.mid(0, 16));
            quint64 fileNameOffset = 0;
            if (fileName.startsWith(QLatin1String("#1/")))
                fileNameOffset = fileName.mid(3).toInt();
            const QString fileLength = QString::fromLatin1(data.mid(48, 10));

            int toSkip = 60 + fileNameOffset;
            offset += fileLength.toInt() + 60 /* header */;

            tmp.append(abiOf(data.mid(toSkip)));
            if (tmp.isEmpty() && fileName == QLatin1String("/0              "))
                tmp = parseCoffHeader(data.mid(toSkip, 20)); // This might be windws...

            if (!tmp.isEmpty()
                    && tmp.at(0).binaryFormat() != Abi::MachOFormat)
                break;

            offset += (offset % 2); // ar is 2 byte aligned
            f.seek(offset);
            data = f.read(1024);
        }
    } else {
        tmp = abiOf(data);
    }
    f.close();

    // Remove duplicates:
    QList<Abi> result;
    foreach (const Abi &a, tmp) {
        if (!result.contains(a))
            result.append(a);
    }

    return result;
}
Exemple #5
0
static QList<Abi> abiOf(const QByteArray &data)
{
    QList<Abi> result;
    if (data.size() <= 8)
        return result;

    if (data.size() >= 20
            && getUint8(data, 0) == 0x7f && getUint8(data, 1) == 'E' && getUint8(data, 2) == 'L'
            && getUint8(data, 3) == 'F') {
        // ELF format:
        bool isLE = (getUint8(data, 5) == 1);
        quint16 machine = isLE ? getLEUint16(data, 18) : getBEUint16(data, 18);
        quint8 osAbi = getUint8(data, 7);

        Abi::OS os = Abi::UnixOS;
        Abi::OSFlavor flavor = Abi::GenericUnixFlavor;
        // http://www.sco.com/developers/gabi/latest/ch4.eheader.html#elfid
        switch (osAbi) {
        case 2: // NetBSD:
            os = Abi::BsdOS;
            flavor = Abi::NetBsdFlavor;
            break;
        case 3: // Linux:
        case 0: // no extra info available: Default to Linux:
        case 97: // ARM, also linux most of the time.
            os = Abi::LinuxOS;
            flavor = Abi::GenericLinuxFlavor;
            break;
        case 6: // Solaris:
            os = Abi::UnixOS;
            flavor = Abi::SolarisUnixFlavor;
            break;
        case 9: // FreeBSD:
            os = Abi::BsdOS;
            flavor = Abi::FreeBsdFlavor;
            break;
        case 12: // OpenBSD:
            os = Abi::BsdOS;
            flavor = Abi::OpenBsdFlavor;
        }

        switch (machine) {
        case 3: // EM_386
            result.append(Abi(Abi::X86Architecture, os, flavor, Abi::ElfFormat, 32));
            break;
        case 8: // EM_MIPS
            result.append(Abi(Abi::MipsArchitecture, os, flavor, Abi::ElfFormat, 32));
            break;
        case 20: // EM_PPC
            result.append(Abi(Abi::PowerPCArchitecture, os, flavor, Abi::ElfFormat, 32));
            break;
        case 21: // EM_PPC64
            result.append(Abi(Abi::PowerPCArchitecture, os, flavor, Abi::ElfFormat, 64));
            break;
        case 40: // EM_ARM
            result.append(Abi(Abi::ArmArchitecture, os, flavor, Abi::ElfFormat, 32));
            break;
        case 62: // EM_X86_64
            result.append(Abi(Abi::X86Architecture, os, flavor, Abi::ElfFormat, 64));
            break;
        case 42: // EM_SH
            result.append(Abi(Abi::ShArchitecture, os, flavor, Abi::ElfFormat, 32));
            break;
        case 50: // EM_IA_64
            result.append(Abi(Abi::ItaniumArchitecture, os, flavor, Abi::ElfFormat, 64));
            break;
        default:
            ;
        }
    } else if (((getUint8(data, 0) == 0xce || getUint8(data, 0) == 0xcf)
             && getUint8(data, 1) == 0xfa && getUint8(data, 2) == 0xed && getUint8(data, 3) == 0xfe
            )
            ||
            (getUint8(data, 0) == 0xfe && getUint8(data, 1) == 0xed && getUint8(data, 2) == 0xfa
             && (getUint8(data, 3) == 0xce || getUint8(data, 3) == 0xcf)
            )
           ) {
            // Mach-O format (Mac non-fat binary, 32 and 64bit magic):
            quint32 type = (getUint8(data, 1) ==  0xfa) ? getLEUint32(data, 4) : getBEUint32(data, 4);
            result.append(macAbiForCpu(type));
    } else if ((getUint8(data, 0) == 0xbe && getUint8(data, 1) == 0xba
                && getUint8(data, 2) == 0xfe && getUint8(data, 3) == 0xca)
               ||
               (getUint8(data, 0) == 0xca && getUint8(data, 1) == 0xfe
                && getUint8(data, 2) == 0xba && getUint8(data, 3) == 0xbe)
              ) {
        // Mach-0 format Fat binary header:
        bool isLE = (getUint8(data, 0) == 0xbe);
        quint32 count = isLE ? getLEUint32(data, 4) : getBEUint32(data, 4);
        int pos = 8;
        for (quint32 i = 0; i < count; ++i) {
            if (data.size() <= pos + 4)
                break;

            quint32 type = isLE ? getLEUint32(data, pos) : getBEUint32(data, pos);
            result.append(macAbiForCpu(type));
            pos += 20;
        }
    } else if (data.size() >= 64){
        // Windows PE: values are LE (except for a few exceptions which we will not use here).

        // MZ header first (ZM is also allowed, but rarely used)
        const quint8 firstChar = getUint8(data, 0);
        const quint8 secondChar = getUint8(data, 1);
        if ((firstChar != 'M' || secondChar != 'Z') && (firstChar != 'Z' || secondChar != 'M'))
            return result;

        // Get PE/COFF header position from MZ header:
        qint32 pePos = getLEUint32(data, 60);
        if (pePos <= 0 || data.size() < pePos + 4 + 20) // PE magic bytes plus COFF header
            return result;
        if (getUint8(data, pePos) == 'P' && getUint8(data, pePos + 1) == 'E'
            && getUint8(data, pePos + 2) == 0 && getUint8(data, pePos + 3) == 0)
            result = parseCoffHeader(data.mid(pePos + 4));
    }
    return result;
}
Exemple #6
0
bool 
Babuino::withInt32()
{
	STACKPTR location, index;
	int32_t  rhs, lhs;

	switch (_regs.opCode)
	{
	case OP_ASHIFT:
	case OP_LSHIFT:
		{
			int8_t shift;
			popUint8(_stack, (uint8_t*)&shift);
			popUint32(_stack, (uint32_t*)&lhs);
			if (shift >= 0)
				pushUint32(_stack, (uint32_t)(lhs << shift));
			else
				pushUint32(_stack, (uint32_t)(lhs >> -shift));
			return true;
		}
	case OP_SET:
		//Serial.println("---set---");

		popStackPtr(_stack, &location);
		popUint32(_stack, (uint32_t*)&rhs);

		setUint32(_stack, location, (uint32_t)rhs);
		return true;

	case OP_GET:
		//Serial.println("---get---");
		popStackPtr(_stack, &location);
		getUint32(_stack, location, (uint32_t*)&rhs);
		pushUint32(_stack, (uint32_t)rhs);
		return true;

	case OP_ASET:
		//Serial.println("---aset---");
		popStackPtr(_stack, &location);
		popStackPtr(_stack, &index);
		popUint32(_stack, (uint32_t*)&rhs);
		setUint32(_stack, location + index * sizeof(int32_t), (uint32_t)rhs);
		return true;

	case OP_AGET:
		//Serial.println("---aget---");
		popStackPtr(_stack, &location);
		popStackPtr(_stack, &index);
		getUint32(_stack, location + index * sizeof(int32_t), (uint32_t*)&rhs);
		pushUint32(_stack, (uint32_t)rhs);
		return true;

	case OP_OUTPUT:
		{
			//Serial.print("output ");
			popUint32(_stack, (uint32_t*)&rhs);	// The return value
				// point to where the arguments size is on the stack.
			location = getArgsLocation();
			uint8_t argsSize;
			getUint8(_stack, location - sizeof(uint8_t), &argsSize); // Get size of args. 
				// If the size > 0 then step over the size location too. 
				// Otherwise keep pointing to the same place because the return
				// value should overwrite where the arguments size would be. 
			if (argsSize > 0)
				argsSize += sizeof(uint8_t);
			setUint32(_stack, location - (STACKPTR)argsSize - sizeof(int32_t), (uint32_t)rhs);
		}
		return true;

	case OP_SEND:
		{
			//Serial.println("---send---");
			popUint32(_stack, (uint32_t*)&rhs);
			uint8_t buf[sizeof(int32_t)];
			hton(rhs, buf);
			//_defaultStream->write(nbuf, sizeof(int32_t)); // Replaced for debugging
			_defaultStream->print(rhs);
		}
		return true;

	case OP_SENDN:
		{
			//Serial.println("---sendn---");
			uint8_t items;
			uint8_t port;
			STACKPTR bufLoc;
			popUint8(_stack, &items);
			popUint8(_stack, &port);
			popStackPtr(_stack, &bufLoc); 
			int32_t* buf = (int32_t*)getStackAddress(_stack, bufLoc);
			uint8_t nbuf[sizeof(int32_t)];
			for (int i = 0; i < items; i++)
			{
				rhs = *buf;
				hton(rhs, nbuf);
				//_defaultStream->write(nbuf, sizeof(int32_t)); // Replaced for debugging
				_defaultStream->print(rhs);
				buf++;
			}
		}
		return true;
#ifdef SUPPORT_STRING
	case OP_TOSTR:
		{
			//Serial.println("---tostr---");
			popUint32(_stack, (uint32_t*)&rhs);
				// ***KLUDGE WARNING***
				// Borrowing unused (hopefully) stack space as a buffer!
				// just to avoid having to allocate another buffer.
			
			char* psz = (char *)getTopAddress(_stack);
			itoa((int) rhs, psz, 10);
				// Logically this is wrong because I'm apparently 
				// pushing a string to a location that it already 
				// occupies. However, the stack implementation 
				// pushes strings onto a separate stack, then
				// pushes the location onto the main stack. So
				// the string doesn't get copied over itself, and
				// is already copied before the first characters are
				// overwritten by pushing the said location onto the
				// main stack.
			pushString(_stack, (uint8_t*)psz);
		}
		return true;
#endif
	case OP_FOREACH:
		{	
			//Serial.print("foreach ");
			PROGPTR blockAddr;
			popProgPtr(_stack, &blockAddr);		// Block address
			popStackPtr(_stack, &location);			// Iterator variable location (Variable contains a 'pointer')
			uint8_t itemsRemaining;
			popUint8(_stack, &itemsRemaining);			// Number of items remaining
				// If we've gone through the block then we need to pop the
				// previous list item off the stack
			if (hasBlockExecuted())
			{
				uint32_t tmpUint32;
				popUint32(_stack, &tmpUint32);
			}

			if (itemsRemaining > 0)				// Any items remaining
			{
					// Set the value of the variable to the location on the
					// stack of the next list item (ie the top)
				setStackPtr(_stack, location, getTop(_stack) - sizeof(int32_t));
				_regs.pc = blockAddr;	// Point to the top of the block for the next iteration
				itemsRemaining--;					// Decrement the number of items remaining
				pushUint8(_stack, itemsRemaining);		// Save number of items remaining for next time
				pushStackPtr(_stack, location);		// Save iterator variable location for next time
				pushProgPtr(_stack, blockAddr);	// Save block address for next time
			}
			else
			{
				ascendBlock();			// Leaving. Pop the block.
			}
		}
		return true;
	}

	popUint32(_stack, (uint32_t*)&rhs);

	switch (_regs.opCode)
	{
	case OP_BITNOT:
		pushUint32(_stack, (uint32_t)~rhs);
		return true;

	case OP_ABS:
		pushUint32(_stack, (uint32_t)abs(rhs));
		return true;

	case OP_NEG:
		pushUint32(_stack, (uint32_t)-rhs);
		return true;
	}

	popUint32(_stack, (uint32_t*)&lhs);

	switch (_regs.opCode)
	{
	case OP_ADD:
		pushUint32(_stack, (uint32_t)(lhs + rhs));
		return true;

	case OP_SUB:
		pushUint32(_stack, (uint32_t)(lhs - rhs));
		return true;

	case OP_MUL:
		pushUint32(_stack, (uint32_t)(lhs * rhs));
		return true;

	case OP_DIV:
		pushUint32(_stack, (uint32_t)(lhs / rhs));
		return true;

	case OP_MOD:
		pushUint32(_stack, (uint32_t)(lhs % rhs));
		return true;

	case OP_EQ:
		pushUint8(_stack, (uint8_t)(lhs == rhs));
		return true;


	case OP_GT:
		pushUint8(_stack, (uint8_t)(lhs > rhs));
		return true;

	case OP_LT:
		pushUint8(_stack, (uint8_t)(lhs < rhs));
		return true;

	case OP_LE:
		pushUint8(_stack, (uint8_t)(lhs <= rhs));
		return true;

	case OP_GE:
		pushUint8(_stack, (uint8_t)(lhs >= rhs));
		return true;

	case OP_NE:
		pushUint8(_stack, (uint8_t)(lhs != rhs));
		return true;

	case OP_BITAND:
		pushUint32(_stack, (uint32_t)(lhs & rhs));
		return true;

	case OP_BITOR:
		pushUint32(_stack, (uint32_t)(lhs | rhs));
		return true;

	case OP_BITXOR:
		pushUint32(_stack, (uint32_t)(lhs ^ rhs));
		return true;
	
	case OP_MIN:
		pushUint32(_stack, (uint32_t)min(lhs, rhs));
		return true;	
	case OP_MAX:
		pushUint32(_stack, (uint32_t)max(lhs, rhs));
		return true;
	}
	return false;
}
 uint8_t ClientMessage::get() {
     return getUint8();
 }
Exemple #8
0
static void handlePrintChildMsg(Forwarder_Data_t *fwdata, char *ptr)
{
    Step_t *step = fwdata->userData;
    uint8_t type;
    uint32_t rank, lrank, i;
    size_t len;
    static IO_Msg_Buf_t *lineBuf;
    int32_t myNodeID = step->localNodeId;
    static int initBuf = 0;

    /* read message */
    getUint8(&ptr, &type);
    getUint32(&ptr, &rank);
    char *msg = getDataM(&ptr, &len);

    /* get local rank from rank */
    lrank = getLocalRankID(rank, step, myNodeID);
    if (lrank == (uint32_t )-1) {
	mlog("%s: invalid node rank for rank %i myNodeID %i step %u:%u\n",
		__func__, rank, myNodeID, step->jobid, step->stepid);
	ufree(msg);
	return;
    }

    /* track I/O channels */
    if (!len) {
	if (type == STDOUT && step->outChannels) {
	    if (step->outChannels[lrank] == 0) {
		ufree(msg);
		return;
	    }
	    step->outChannels[lrank] = 0;
	}
	if (type == STDERR && step->errChannels) {
	    if (step->errChannels[lrank] == 0) {
		ufree(msg);
		return;
	    }
	    step->errChannels[lrank] = 0;
	}
    }

    /* handle unbuffered IO */
    if (type == STDERR || (!(step->taskFlags & LAUNCH_LABEL_IO)
	&& !(step->taskFlags & LAUNCH_BUFFERED_IO))
	|| step->taskFlags & LAUNCH_PTY) {
	writeIOmsg(msg, len, rank, type, fwdata, step, lrank);
	ufree(msg);
	return;
    }

    /* handle buffered IO */
    if (!initBuf) {
	lineBuf = umalloc(sizeof(IO_Msg_Buf_t)
			  * step->globalTaskIdsLen[myNodeID]);
	for (i=0; i<step->globalTaskIdsLen[myNodeID]; i++) {
	    lineBuf[i].out.buf = lineBuf[i].err.buf = NULL;
	    lineBuf[i].out.bufUsed = lineBuf[i].err.bufUsed = 0;
	}
	initBuf = 1;
    }

   handleBufferedMsg(msg, len,
		    type == STDOUT ?  &lineBuf[lrank].out : &lineBuf[lrank].err,
		    fwdata, step, rank, type, lrank);

    ufree(msg);
}
Exemple #9
0
static uint8_t readUint8(EventCursor *evcur){
	uint8_t v = getUint8(evcur->ev + evcur->cur);
	evcur->cur += 1;
	return v;
}
Exemple #10
0
luabridge::LuaRef CLuaBinary::getStruct(luabridge::LuaRef objAttr)
{
    assert(objAttr.isTable());

    DataType emType;
    int iSize = Q_INIT_NUMBER;
    std::string strName;
    luabridge::LuaRef objVal = luabridge::newTable(m_pLua);

    for (int i = 1; i <= objAttr.length(); i++)
    {
        luabridge::LuaRef objTmp = objAttr[i];
        if (!objTmp.isTable()
            || STATTRTAB_MINLENS > objTmp.length())
        {
            break;
        }

        luabridge::LuaRef objChildStAttr = getStructAttr(objTmp, strName, emType, iSize);
        switch(emType)
        {
        case DTYPE_SINT8:
            {
                objVal[strName] = getSint8();
            }
            break;
        case DTYPE_UINT8:
            {
                objVal[strName] = getUint8();
            }
            break;
        case DTYPE_BOOL:
            {
                objVal[strName] = getBool();
            }
            break;
        case DTYPE_SINT16:
            {
                objVal[strName] = getSint16();
            }
            break;
        case DTYPE_UINT16:
            {
                objVal[strName] = getUint16();
            }
            break;
        case DTYPE_SINT32:
            {
                objVal[strName] = getSint32();
            }
            break;
        case DTYPE_UINT32:
            {
                objVal[strName] = getUint32();
            }
            break;
        case DTYPE_SINT64:
            {
                objVal[strName] = getSint64();
            }
            break;
        case DTYPE_UINT64:
            {
                objVal[strName] = getUint64();
            }
            break;
        case DTYPE_FLOAT:
            {
                objVal[strName] = getFloat();
            }
            break;
        case DTYPE_DOUBLE:
            {
                objVal[strName] = getDouble();
            }
            break;
        case DTYPE_STRING:
            {
                std::string strVal = getString();
                objVal[strName] = strVal;
                skipRead((unsigned int)(iSize - (strVal.size() + 1)));
            }
            break;
        case DTYPE_BYTE:
            {
                objVal[strName] = getByte(iSize);
            }
            break;
        case DTYPE_STRUCT:
            {
                objVal[strName] = getStruct(objChildStAttr);
            }
            break;
        case DTYPE_SKIP:
            {
                if (iSize > Q_INIT_NUMBER)
                {
                    skipRead(iSize);
                }
            }
            break;

        default:
            break;
        }
    }

    return objVal;
}