mirror of
https://github.com/fluencelabs/c-template
synced 2025-04-24 14:22:12 +00:00
26 lines
744 B
C
26 lines
744 B
C
#include "sdk/allocator.h"
|
|
#include "sdk/logger.h"
|
|
#include <string.h>
|
|
|
|
const char *const greeting = "Hello world! From ";
|
|
const int RESPONSE_SIZE_BYTES = 4;
|
|
|
|
char *invoke(const char *str, int length) {
|
|
const size_t greeting_length = strlen(greeting);
|
|
// TODO: add check for overflow
|
|
const size_t response_length = length + RESPONSE_SIZE_BYTES + greeting_length;
|
|
|
|
char *response = (char *)allocate(response_length);
|
|
|
|
wasm_log(str, length);
|
|
|
|
for(int i = 0; i < RESPONSE_SIZE_BYTES; ++i) {
|
|
response[i] = (response_length >> 8*i) & 0xFF;
|
|
}
|
|
|
|
memcpy(response + RESPONSE_SIZE_BYTES, greeting, greeting_length);
|
|
memcpy(response + RESPONSE_SIZE_BYTES + greeting_length, str, length);
|
|
|
|
return response;
|
|
}
|