void xs_i2c_readBlock(xsMachine *the) { FskErr err; xsI2C i2c = xsGetHostData(xsThis); int argc = xsToInteger(xsArgc), i; int format = 2; SInt32 dataSize = xsToInteger(xsArg(0)), readCount; UInt8 data[32]; xsThrowIfNULL(i2c); DBG_I2C("xs_i2c_readBlock\n"); if ((dataSize > 32) || (dataSize <= 0)) xsThrowDiagnosticIfFskErr(kFskErrInvalidParameter, "I2C readBlock invalid size %d. %s", (int)dataSize, i2c->diagnosticID); FskPinI2CSetAddress(i2c->pin, i2c->address); err = FskPinI2CReadBytes(i2c->pin, dataSize, &readCount, data); if (err) { xsTraceDiagnostic("I2C readBlock failed with error %s %s.", FskInstrumentationGetErrorString(err), i2c->diagnosticID); goto bail; } if (argc > 1) { int t = xsTypeOf(xsArg(1)); if ((xsNumberType == t) || (t == xsIntegerType)) format = xsToInteger(xsArg(1)); else { char *formatString = xsToString(xsArg(1)); if (0 == FskStrCompare(formatString, "Buffer")) format = 2; else if (0 == FskStrCompare(formatString, "Chunk")) format = 0; else if (0 == FskStrCompare(formatString, "Array")) format = 1; } } if (2 == format) { xsResult = xsArrayBuffer(data, readCount); } else if (0 == format) { xsResult = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(readCount)); FskMemMove(xsGetHostData(xsResult), data, readCount); } else if (1 == format) { xsResult = xsNew1(xsGlobal, xsID("Array"), xsInteger(readCount)); for (i = 0; i < readCount; i++) xsSet(xsResult, i, xsInteger(data[i])); } bail: if (err) xsError(err); }
FskErr FskSSLWrite(void *a, const void *buf, int *bufLen) { FskSSL *fssl = a; FskErr err = kFskErrNone; int len = *bufLen; if (fssl->skt == NULL) return kFskErrOperationFailed; if (buf == NULL || len == 0) return kFskErrNone; xsBeginHost(fssl->vm->the); xsTry { xsVars(1); xsCall1_noResult(fssl->socket, xsID("attachData"), xsHostData(fssl->skt)); xsVar(0) = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(len)); FskMemCopy(xsGetHostData(xsVar(0)), buf, len); xsResult = xsCall2(fssl->ssl, xsID("write"), fssl->socket, xsVar(0)); *bufLen = xsToInteger(xsResult); if (*bufLen == 0) err = kFskErrNoData; xsCall0_noResult(xsVar(0), xsID("free")); } xsCatch { if (xsHas(xsException, xsID("code"))) err = xsToInteger(xsGet(xsException, xsID("code"))); if (err == kFskErrNone) err = kFskErrOperationFailed; } xsEndHost(fssl->vm->the); return err; }
void KPR_message_invoke(xsMachine* the) { KprMessage self = kprGetHostData(xsThis, this, message); xsVars(1); xsVar(0) = xsNewHostFunction(KPR_Message_invoke_executor, 2); xsSet(xsVar(0), xsID_message, xsThis); if (xsToInteger(xsArgc) > 0) KprMessageScriptTargetSet(self, the, &xsArg(0)); xsResult = xsNew1(xsGlobal, xsID_Promise, xsVar(0)); }
FskErr FskSSLLoadCerts(void *a, FskSocketCertificateRecord *cert) { FskSSL *fssl = a; FskErr err = kFskErrNone; xsBeginHost(fssl->vm->the); xsTry { xsVars(2); xsVar(0) = xsUndefined; xsVar(1) = xsUndefined; if (cert != NULL) { if (cert->certificates != NULL && cert->certificatesSize > 0) { xsVar(0) = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(cert->certificatesSize)); FskMemCopy(xsGetHostData(xsVar(0)), cert->certificates, cert->certificatesSize); } if (cert->policies != NULL) { if (cert->certificates == NULL) xsVar(0) = xsNew0(xsGlobal, xsID("Chunk")); // create a null chunk just for setting the polices xsSet(xsVar(0), xsID("policies"), xsString(cert->policies)); } if (cert->hostname != NULL) { xsSet(fssl->socket, xsID("hostname"), xsString(cert->hostname)); // too easy but should work... } if (cert->key != NULL && cert->keySize > 0) { xsVar(1) = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(cert->keySize)); FskMemCopy(xsGetHostData(xsVar(1)), cert->key, cert->keySize); } } xsCall2_noResult(fssl->ssl, xsID("loadCerts"), xsVar(0), xsVar(1)); } xsCatch { if (xsHas(xsException, xsID("code"))) err = xsToInteger(xsGet(xsException, xsID("code"))); if (err == kFskErrNone) err = kFskErrOperationFailed; } xsEndHost(fssl->vm->the); return err; }
void KPR_message_get_responseBuffer(xsMachine *the) { KprMessage self = xsGetHostData(xsThis); void* data; UInt32 size; KprMessageGetResponseBody(self, &data, &size); if (data && size) { xsResult = xsNew1(xsGlobal, xsID_ArrayBuffer, xsInteger(size)); FskMemMove(xsToArrayBuffer(xsResult), data, size); } }
void KPR_message_get_requestChunk(xsMachine *the) { KprMessage self = xsGetHostData(xsThis); void* data; UInt32 size; KprMessageGetRequestBody(self, &data, &size); if (data && size) { xsResult = xsNew1(xsGlobal, xsID_Chunk, xsInteger(size)); FskMemCopy(xsGetHostData(xsResult), data, size); } }
void xs_i2c_readBlockDataSMB(xsMachine* the) { xsI2C i2c = xsGetHostData(xsThis); FskErr err; char* formatString; int format = 2, i; SInt32 dataSize = 0; UInt8 command = (UInt8)xsToInteger(xsArg(0)); UInt8 data[34]; //needs to be 34 because we're using I2C_SMBUS_I2C_BLOCK_BROKEN in i2cdev.c //@@ WTF - not at this layer, at least SInt32 length = (SInt32)xsToInteger(xsArg(1)); DBG_I2C("xs_i2c_readBlockDataSMB\n"); xsThrowIfNULL(i2c); if ((length < 0) || (length > 32)) xsThrowDiagnosticIfFskErr(kFskErrInvalidParameter, "I2C readBlockDataSMB bad length %d %s.", length, i2c->diagnosticID); FskPinI2CSetAddress(i2c->pin, i2c->address); formatString = xsToString(xsArg(2)); if (!FskStrCompare(formatString, "Buffer")) format = 2; else if (!FskStrCompare(formatString, "Chunk")) format = 0; else if (!FskStrCompare(formatString, "Array")) format = 1; err = FskPinI2CReadDataBytes(i2c->pin, command, length, &dataSize, data); xsThrowDiagnosticIfFskErr(err, "I2C readBlockDataSMB register %d failed %s.", (int)command, i2c->diagnosticID); if (2 == format) { xsResult = xsArrayBuffer(data, dataSize); } else if (0 == format) { xsResult = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(dataSize)); FskMemMove(xsGetHostData(xsResult), data, dataSize); } else if (1 == format) { xsResult = xsNew1(xsGlobal, xsID("Array"), xsInteger(dataSize)); for (i = 0; i < dataSize; i++) xsSet(xsResult, i, xsInteger(data[i])); } }
void KPR_message_get_responseChunk(xsMachine *the) { KprMessage self = xsGetHostData(xsThis); void* data; UInt32 size; // xsTrace("message.responseChunk deprecated, use responseBuffer\n"); KprMessageGetResponseBody(self, &data, &size); if (data && size) { xsResult = xsNew1(xsGlobal, xsID_Chunk, xsInteger(size)); FskMemCopy(xsGetHostData(xsResult), data, size); } }
void KPR_canvasRenderingContext2D_getLineDash(xsMachine *the) { FskCanvas2dContext ctx = xsGetHostData(xsThis); double *dash = NULL; FskErr err; UInt32 len, i; bailIfError(FskCanvas2dGetLineDash(ctx, &len, &dash)); len *= 2; /* Convert from cycles to length */ xsResult = xsNew1(xsGlobal, xsID("Array"), xsInteger(len)); for (i = 0; i < len; i++) xsSetAt(xsResult, xsInteger(i), xsNumber(dash[i])); bail: (void)FskMemPtrDispose(dash); return; }
void xs_curve25519_dh(xsMachine *the) { void *secret, *basepoint; xsIntegerValue sz; xsResult = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(32)); getChunkData(the, &xsArg(0), &secret, &sz); if (sz != 32) cryptThrowFSK(kFskErrInvalidParameter); if (xsToInteger(xsArgc) > 1) { getChunkData(the, &xsArg(1), &basepoint, &sz); if (sz != 32) cryptThrowFSK(kFskErrInvalidParameter); } else basepoint = (void *)c25519_base_x; c25519_prepare(secret); c25519_smult(xsGetHostData(xsResult), basepoint, secret); }
FskErr FskSSLAttach(void **fsslp, FskSocket skt) { FskSSL *fssl; FskErr err; if ((err = FskMemPtrNewClear(sizeof(FskSSL), (FskMemPtr*)(void*)&fssl)) != kFskErrNone) return err; if ((err = newSSLVM(&fssl->vm)) != kFskErrNone) { FskMemPtrDispose(fssl); return err; } xsBeginHost(fssl->vm->the); xsTry { xsVars(2); xsVar(0) = xsNew1(xsGet(xsGlobal, xsID("Stream")), xsID("Socket"), xsHostData(skt)); xsVar(1) = xsNewInstanceOf(xsObjectPrototype); (void)xsSet(xsVar(1), xsID("raw"), xsTrue); xsCall1_noResult(xsVar(0), xsID("setProperties"), xsVar(1)); fssl->socket = xsVar(0); xsRemember(fssl->socket); xsVar(0) = xsNew0(xsGet(xsGlobal, xsID("FskSSL")), xsID("Session")); fssl->ssl = xsVar(0); xsRemember(fssl->ssl); } xsCatch { if (xsHas(xsException, xsID("code"))) err = xsToInteger(xsGet(xsException, xsID("code"))); if (err == kFskErrNone) err = kFskErrOperationFailed; } xsEndHost(fssl->vm->the); if (err == kFskErrNone) { if (fsslp != NULL) *fsslp = fssl; } else { disposeSSLVM(fssl->vm); FskMemPtrDispose(fssl); } return err; }
void xs_x509_decodeSubjectKeyId(xsMachine *the) { UInt32 sz = xsToInteger(xsGet(xsArg(0), xsID("length"))); unsigned char *p = xsGetHostData(xsArg(0)), *endp = p + sz, *endTBS, *endEXT, *extnID, *spki, *spk; int l, extnIDLen, spkLen; static UInt8 id_ce_ski[] = {2 * 40 + 5, 29, 14}; /* [2, 5, 29, 14] */ #define getTag() (p < endp ? (int)*p++ : -1) #define getBerLen() _getBerLen(&p, endp) #define nextTag() (getTag(), l = getBerLen(), p += l) if (getTag() != 0x30) return; if ((l = getBerLen()) < 0) return; if (p + l > endp) return; /* TBSCertficate */ if (getTag() != 0x30) return; if ((l = getBerLen()) < 0) return; if ((endTBS = p + l) > endp) return; if (*p & 0x80) { /* EXPLICT Version */ p++; nextTag(); } nextTag(); /* serialNumber */ nextTag(); /* signature */ nextTag(); /* issuer */ nextTag(); /* validity */ nextTag(); /* subject */ spki = p; /* subjectPublicKeyInfo */ nextTag(); /* OPTIONAL */ while (p < endTBS) { int tag = getTag(); if ((l = getBerLen()) < 0) return; switch (tag & 0x1f) { case 1: /* issuerUniqueID */ case 2: /* subjectUniqueID */ p += l; continue; /* goto the next tag */ case 3: /* extensions */ break; /* fall thru */ default: return; } /* must be a SEQUENCE of [1..MAX] */ if (getTag() != 0x30) return; if ((l = getBerLen()) < 0) return; endEXT = p + l; while (p < endEXT) { /* must be a SEQUENCE of {extnID, critical, extnValue} */ if (getTag() != 0x30) return; if ((l = getBerLen()) < 0) return; /* extnID: OBJECT ID */ if (getTag() != 0x06) return; if ((extnIDLen = getBerLen()) < 0) return; extnID = p; p += extnIDLen; /* critical: BOOLEAN */ if (*p == 0x01) nextTag(); /* extnValue: OCTET STRING */ if (getTag() != 0x04) return; if ((l = getBerLen()) < 0) return; if (extnIDLen == sizeof(id_ce_ski) && FskMemCompare(extnID, id_ce_ski, extnIDLen) == 0) { /* SKI: OCTET STRING */ if (getTag() != 0x04) return; l = getBerLen(); xsResult = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(l)); FskMemCopy(xsGetHostData(xsResult), p, l); return; } p += l; } } { /* * Couldn't find Subject Key Identifier. Make up the ID from the Subject Public Key */ struct sha1 sha1; p = spki; if (getTag() != 0x30) return; /* should be a SEQUENCE */ l = getBerLen(); /* skip AlgorithmIdentifier */ nextTag(); if (getTag() != 0x03) return; /* should be a BIT STRING */ spkLen = getBerLen(); spk = p; xsResult = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(SHA1_DGSTSIZE)); sha1_create(&sha1); sha1_update(&sha1, spk, spkLen); sha1_fin(&sha1, xsGetHostData(xsResult)); } }
static void KPR_canvasRenderingContext2D_getStyle(xsMachine *the, xsBooleanValue stroke) { FskCanvas2dContext ctx; const FskColorSourceUnion* csu; UInt32 c, i; FskGradientStop *stop; xsVars(3); ctx = xsGetHostData(xsThis); if (stroke) csu = (const FskColorSourceUnion *)FskCanvas2dGetStrokeStyle(ctx); else csu = (const FskColorSourceUnion *)FskCanvas2dGetFillStyle(ctx); switch (csu->so.type) { case kFskColorSourceTypeConstant: KprSerializeColor(the, &csu->cn.color, &xsResult); break; case kFskColorSourceTypeLinearGradient: xsResult = xsNewInstanceOf(xsGet(xsGet(xsGlobal, xsID("KPR")), xsID("canvasLinearGradient"))); xsSet(xsResult, xsID("x0"), xsNumber(FskFixedToFloat(csu->lg.gradientVector[0].x))); xsSet(xsResult, xsID("y0"), xsNumber(FskFixedToFloat(csu->lg.gradientVector[0].y))); xsSet(xsResult, xsID("x1"), xsNumber(FskFixedToFloat(csu->lg.gradientVector[1].x))); xsSet(xsResult, xsID("y1"), xsNumber(FskFixedToFloat(csu->lg.gradientVector[1].y))); c = csu->lg.numStops; stop = csu->lg.gradientStops; goto getStops; case kFskColorSourceTypeRadialGradient: xsResult = xsNewInstanceOf(xsGet(xsGet(xsGlobal, xsID("KPR")), xsID("canvasRadialGradient"))); xsSet(xsResult, xsID("x0"), xsNumber(FskFixedToFloat(csu->rg.focus.x))); xsSet(xsResult, xsID("y0"), xsNumber(FskFixedToFloat(csu->rg.focus.y))); xsSet(xsResult, xsID("r0"), xsNumber(0)); xsSet(xsResult, xsID("x1"), xsNumber(FskFixedToFloat(csu->rg.center.x))); xsSet(xsResult, xsID("y1"), xsNumber(FskFixedToFloat(csu->rg.center.y))); xsSet(xsResult, xsID("r1"), xsNumber(FskFixedToFloat(csu->rg.radius))); c = csu->rg.numStops; stop = csu->rg.gradientStops; getStops: xsVar(0) = xsNew1(xsGlobal, xsID("Array"), xsInteger(c)); for (i = 0; i < c; i++) { xsVar(1) = xsNewInstanceOf(xsGet(xsGet(xsGlobal, xsID("KPR")), xsID("canvasGradientStop"))); xsSet(xsVar(1), xsID("offset"), xsNumber(FskFractToFloat(stop->offset))); KprSerializeColor(the, &stop->color, &xsVar(2)); xsSet(xsVar(1), xsID("color"), xsVar(2)); xsSetAt(xsVar(0), xsInteger(i), xsVar(1)); stop++; } xsSet(xsResult, xsID("stops"), xsVar(0)); break; case kFskColorSourceTypeTexture: xsResult = xsNewInstanceOf(xsGet(xsGet(xsGlobal, xsID("KPR")), xsID("canvasPattern"))); // @@ fskBitmapToXSBitmap(the, (FskBitmap)csu->tx.texture, false, &xsVar(0)); xsSet(xsResult, xsID("image"), xsVar(0)); c = csu->tx.spreadMethod & kFskCanvas2dPatternRepeatX; i = csu->tx.spreadMethod & kFskCanvas2dPatternRepeatY; if (c && i) xsSet(xsResult, xsID("repetition"), xsString("repeat")); else if (c) xsSet(xsResult, xsID("repetition"), xsString("repeat-x")); else if (i) xsSet(xsResult, xsID("repetition"), xsString("repeat-y")); else xsSet(xsResult, xsID("repetition"), xsString("no-repeat")); break; } }
void cryptThrowFSK_(xsMachine *the, int fskcode) { cryptThrow_(the, "kCryptSystemError", GET_XS_FNAME(), xsNew1(xsGet(xsGet(xsGlobal, xsID("Fsk")), xsID("Error")), xsID("Native"), xsInteger(fskcode))); }