TEST(plankton, env_construction) { CREATE_RUNTIME(); // Environment references resolve correctly to ast factories. { byte_buffer_t buf; byte_buffer_init(&buf); write_ast_factory(&buf, "Literal"); value_t value = deserialize(runtime, &buf); ASSERT_FAMILY(ofFactory, value); byte_buffer_dispose(&buf); } // Objects with ast factory headers produce asts. { byte_buffer_t buf; byte_buffer_init(&buf); byte_buffer_append(&buf, pObject); write_ast_factory(&buf, "Literal"); byte_buffer_append(&buf, pMap); plankton_wire_encode_uint32(&buf, 1); write_string(&buf, "value"); byte_buffer_append(&buf, pTrue); value_t value = deserialize(runtime, &buf); ASSERT_FAMILY(ofLiteralAst, value); ASSERT_VALEQ(yes(), get_literal_ast_value(value)); byte_buffer_dispose(&buf); } DISPOSE_RUNTIME(); }
// Writes an ast factory reference with the given ast type to the given buffer. static value_t write_ast_factory(byte_buffer_t *buf, const char *ast_type) { byte_buffer_append(buf, pEnvironment); byte_buffer_append(buf, pArray); TRY(plankton_wire_encode_uint32(buf, 2)); TRY(write_string(buf, "ast")); TRY(write_string(buf, ast_type)); return success(); }
value_t plankton_wire_encode_uint32(byte_buffer_t *buf, uint32_t value) { while (value > 0x7F) { // As long as the value doesn't fit in 7 bits chop off 7 bits and mark // them with a high 1 to indicate that there's more coming. byte_t part = value & 0x7F; byte_buffer_append(buf, part | 0x80); value >>= 7; } byte_buffer_append(buf, value); return success(); }
value_t read_handle_to_blob(runtime_t *runtime, FILE *handle) { // Read the complete file into a byte buffer. byte_buffer_t buffer; byte_buffer_init(&buffer); while (true) { static const size_t kBufSize = 1024; byte_t raw_buffer[kBufSize]; size_t was_read = fread(raw_buffer, 1, kBufSize, handle); if (was_read <= 0) break; for (size_t i = 0; i < was_read; i++) byte_buffer_append(&buffer, raw_buffer[i]); } blob_t data_blob; byte_buffer_flush(&buffer, &data_blob); // Create a blob to hold the result and copy the data into it. value_t result = new_heap_blob_with_data(runtime, &data_blob); byte_buffer_dispose(&buffer); return result; }
static BOOL __stdcall CryptSetHashParam_done(BOOL retval, HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData, DWORD dwFlags) { DWORD err = GetLastError(); int ret_addr = *((DWORD *) ((DWORD) &retval - 4)); if (retval && !called_internally(ret_addr)) { HashMap::iterator iter; LOCK(); iter = hash_map.find(hHash); if (iter != hash_map.end()) { HashContext *ctx = iter->second; const TCHAR *param_str; HMAC_INFO *hmi; ByteBuffer *buf = NULL; const char *data = NULL; int data_len = 0; switch (dwParam) { case HP_HMAC_INFO: param_str = _T("HMAC_INFO"); hmi = (HMAC_INFO *) pbData; buf = byte_buffer_sized_new(4 + 4 + hmi->cbInnerString + 4 + hmi->cbOuterString); byte_buffer_append(buf, &hmi->HashAlgid, sizeof(ALG_ID)); byte_buffer_append(buf, &hmi->cbInnerString, sizeof(DWORD)); byte_buffer_append(buf, hmi->pbInnerString, hmi->cbInnerString); byte_buffer_append(buf, &hmi->cbOuterString, sizeof(DWORD)); byte_buffer_append(buf, hmi->pbOuterString, hmi->cbOuterString); data = (const char *) buf->buf; data_len = (int) buf->offset; break; case HP_HASHVAL: param_str = _T("HASHVAL"); break; default: param_str = _T("UNKNOWN"); break; } message_logger_log(_T("CryptSetHashParam"), (char *) &retval - 4, ctx->get_id(), MESSAGE_TYPE_PACKET, MESSAGE_CTX_INFO, PACKET_DIRECTION_INVALID, NULL, NULL, data, data_len, _T("hHash=0x%p, Algid=%s, dwParam=%s"), hHash, ctx->get_alg_id_as_string(), param_str); if (buf != NULL) byte_buffer_free(buf); } UNLOCK(); } SetLastError(err); return retval; }
// Writes a tagged plankton string to the given buffer. static value_t write_string(byte_buffer_t *buf, const char *c_str) { string_t str; string_init(&str, c_str); byte_buffer_append(buf, pString); return plankton_wire_encode_string(buf, &str); }