static size_t sizeof_elf_from_hdr(void *buf) { struct elf_hdr *elf = (struct elf_hdr*) buf; size_t sz = 0; BUILD_ASSERT(SECURE_BOOT_HEADERS_SIZE > sizeof(struct elf_hdr)); BUILD_ASSERT(SECURE_BOOT_HEADERS_SIZE > sizeof(struct elf64_hdr)); BUILD_ASSERT(SECURE_BOOT_HEADERS_SIZE > sizeof(struct elf32_hdr)); if (elf->ei_ident == ELF_IDENT) { if (elf->ei_class == ELF_CLASS_64) { struct elf64_hdr *elf64 = (struct elf64_hdr*) buf; sz = le64_to_cpu(elf64->e_shoff) + ((uint32_t)le16_to_cpu(elf64->e_shentsize) * (uint32_t)le16_to_cpu(elf64->e_shnum)); } else if (elf->ei_class == ELF_CLASS_32) { struct elf32_hdr *elf32 = (struct elf32_hdr*) buf; sz = le32_to_cpu(elf32->e_shoff) + (le16_to_cpu(elf32->e_shentsize) * le16_to_cpu(elf32->e_shnum)); } } return sz; }
struct coroutine_stack *coroutine_stack_init(void *buf, size_t bufsize, size_t metasize) { struct coroutine_stack *stack; BUILD_ASSERT(COROUTINE_STK_OVERHEAD == sizeof(*stack)); #ifdef MINSIGSTKSZ BUILD_ASSERT(COROUTINE_MIN_STKSZ >= MINSIGSTKSZ); #endif if (bufsize < (COROUTINE_MIN_STKSZ + sizeof(*stack) + metasize)) return NULL; #if HAVE_STACK_GROWS_UPWARDS stack = (char *)buf + metasize; #else stack = (struct coroutine_stack *) ((char *)buf + bufsize - metasize) - 1; #endif stack->magic = COROUTINE_STACK_MAGIC; stack->size = bufsize - sizeof(*stack) - metasize; return stack; }
int elasto_fh_validate(struct elasto_fh *fh) { if (fh == NULL) { dbg(0, "invalid NULL handle\n"); return -EINVAL; } if ((fh->type != ELASTO_FILE_AZURE) && (fh->type != ELASTO_FILE_S3) && (fh->type != ELASTO_FILE_ABB) && (fh->type != ELASTO_FILE_AFS) && (fh->type != ELASTO_FILE_LOCAL)) { dbg(0, "handle has invalid type %x\n", fh->type); return -EINVAL; } BUILD_ASSERT(sizeof(ELASTO_FH_MAGIC) <= ARRAY_SIZE(fh->magic)); if (memcmp(fh->magic, ELASTO_FH_MAGIC, sizeof(ELASTO_FH_MAGIC))) { dbg(0, "handle has invalid magic\n"); return -EINVAL; } return 0; }
void csp_id_set_fill_double(struct csp_id_set *set, csp_id e1, csp_id e2) { /* Ensure that we can initialize the singleton set without having to * allocate anything. Because we always reallocate some space in the set * itself, we can do this at build time. */ BUILD_ASSERT(CSP_ID_SET_INTERNAL_SIZE >= 2); /* Make sure the events are deduplicated! */ if (unlikely(e1 == e2)) { set->hash = CSP_ID_SET_INITIAL_HASH ^ e1; set->count = 1; set->ids[0] = e1; } else { set->hash = CSP_ID_SET_INITIAL_HASH ^ e1 ^ e2; set->count = 2; /* Make sure the events are sorted! */ if (e1 < e2) { set->ids[0] = e1; set->ids[1] = e2; } else { set->ids[0] = e2; set->ids[1] = e1; } } }
static void do_init(void) { uint8_t sha1[SHA1_DIGEST_SIZE]; struct sha1_ctx sha1_ctx; uint8_t random_seed[16]; struct timeval now; /* Get seed data. */ get_entropy_or_die(random_seed, sizeof random_seed); xgettimeofday(&now); /* Convert seed into key. */ sha1_init(&sha1_ctx); sha1_update(&sha1_ctx, random_seed, sizeof random_seed); sha1_update(&sha1_ctx, &now, sizeof now); sha1_update_int(&sha1_ctx, getpid()); #ifndef _WIN32 sha1_update_int(&sha1_ctx, getppid()); sha1_update_int(&sha1_ctx, getuid()); sha1_update_int(&sha1_ctx, getgid()); #endif sha1_final(&sha1_ctx, sha1); /* Generate key. */ BUILD_ASSERT(sizeof sha1 >= 16); aes128_schedule(&key, sha1); /* Generate initial counter. */ get_entropy_or_die(counter, sizeof counter); }
int main(int argc, char *argv[]) { #ifdef FAIL BUILD_ASSERT(1 == 0); #endif return 0; }
int main(void) { #ifdef FAIL BUILD_ASSERT(1 == 0); #endif return 0; }
int main(int argc, char **argv) { int res; int i; struct voxind_t *v; struct sigaction act; #ifdef DEBUG { struct stat buf; while (!stat(VOXIND_DBG, &buf)) { sleep(1); } } #endif ENTER(); BUILD_ASSERT(PIPE_MAX_BLOCK > MIN_MSG_SIZE); memset(&act, 0, sizeof(act)); sigemptyset(&act.sa_mask); act.sa_flags = 0; act.sa_handler = sighandler; for (i = 1; i < NSIG; i++) { sigaction(i, &act, NULL); } my_voxind = calloc(1, sizeof(struct voxind_t)); if (!my_voxind) { res = errno; goto exit0; } my_voxind->msg = calloc(1, PIPE_MAX_BLOCK); if (!my_voxind->msg) { res = errno; goto exit0; } my_voxind->msg_length = PIPE_MAX_BLOCK; res = pipe_restore(&my_voxind->pipe_command, PIPE_COMMAND_FILENO); if (res) goto exit0; atexit(my_exit); do { size_t msg_length = my_voxind->msg_length; if(pipe_read(my_voxind->pipe_command, my_voxind->msg, &msg_length)) goto exit0; if (unserialize(my_voxind->msg, &msg_length)) goto exit0; pipe_write(my_voxind->pipe_command, my_voxind->msg, &msg_length); } while (1); exit0: err("LEAVE, (err=%d)",res); return res; }
static void ivs_from_secret(const unsigned char secret[32], struct iv *iv, struct iv *pad_iv) { struct sha256 sha; sha_with_seed(secret, 2, &sha); BUILD_ASSERT(sizeof(*iv) + sizeof(*pad_iv) == sizeof(sha)); memcpy(iv->iv, sha.u.u8, sizeof(iv->iv)); memcpy(pad_iv->iv, sha.u.u8 + sizeof(iv->iv), sizeof(pad_iv->iv)); }
void csp_id_set_fill_single(struct csp_id_set *set, csp_id event) { /* Ensure that we can initialize the singleton set without having to * allocate anything. Because we always reallocate some space in the set * itself, we can do this at build time. */ BUILD_ASSERT(CSP_ID_SET_INTERNAL_SIZE >= 1); set->hash = CSP_ID_SET_INITIAL_HASH ^ event; set->count = 1; set->ids[0] = event; }
static int queue_prd_msg_hbrt(struct opal_prd_msg *msg, void (*consumed)(void *data)) { uint64_t *buf; BUILD_ASSERT(sizeof(*msg) / sizeof(uint64_t) == 4); buf = (uint64_t *)msg; return _opal_queue_msg(OPAL_MSG_PRD, msg, consumed, 4, buf); }
struct jmap *jmap_new(void) { struct jmap *map; /* Judy uses unsigned long for Word_t, we use unsigned long. */ BUILD_ASSERT(sizeof(Word_t) == sizeof(unsigned long)); /* We also put pointers into Judy, in jmap_types.h */ BUILD_ASSERT(sizeof(Word_t) >= sizeof(void *)); map = malloc(sizeof(*map)); if (map) { map->judy = NULL; memset(&map->err, 0, sizeof(map->err)); map->errstr = NULL; map->num_accesses = 0; map->acc_value = NULL; map->acc_index = 0; map->funcname = NULL; } return map; }
size_t bitmap_count1(const unsigned long int *bitmap, size_t n) { size_t i; size_t count = 0; BUILD_ASSERT(ULONG_MAX <= UINT64_MAX); for (i = 0; i < BITMAP_N_LONGS(n); i++) { count += count_1bits(bitmap[i]); } return count; }
bool shachain_get_secret(const struct shachain *shachain, u64 commit_num, struct secret *preimage) { struct sha256 sha; if (commit_num >= (1ULL << SHACHAIN_BITS)) return false; if (!shachain_get_hash(shachain, shachain_index(commit_num), &sha)) return false; BUILD_ASSERT(sizeof(*preimage) == sizeof(sha)); memcpy(preimage, &sha, sizeof(*preimage)); return true; }
bool per_commit_secret(const struct sha256 *shaseed, struct secret *commit_secret, u64 per_commit_index) { struct sha256 s; if (per_commit_index >= (1ULL << SHACHAIN_BITS)) return false; shachain_from_seed(shaseed, shachain_index(per_commit_index), &s); BUILD_ASSERT(sizeof(s) == sizeof(*commit_secret)); memcpy(commit_secret, &s, sizeof(s)); return true; }
/* Helper for host command to dump controller registers */ void lb_hc_cmd_dump(struct ec_response_lightbar *out) { int i; uint8_t reg; BUILD_ASSERT(ARRAY_SIZE(dump_reglist) == ARRAY_SIZE(out->dump.vals)); for (i = 0; i < ARRAY_SIZE(dump_reglist); i++) { reg = dump_reglist[i]; out->dump.vals[i].reg = reg; out->dump.vals[i].ic0 = controller_read(0, reg); out->dump.vals[i].ic1 = controller_read(1, reg); } }
static int update_bridge(struct starsystem_info *ss) { unsigned char pwdhash[20]; int i, rc; unsigned char buffer[250]; struct packed_buffer pb; struct snis_entity *o; #define bytes_to_read (sizeof(struct update_ship_packet) - 9 + 25 + 5 + \ sizeof(struct power_model_data) + \ sizeof(struct power_model_data) - 1 - 1) fprintf(stderr, "snis_multiverse: update bridge 1\n"); memset(buffer, 0, sizeof(buffer)); memset(pwdhash, 0, sizeof(pwdhash)); rc = read_and_unpack_fixed_size_buffer(ss, buffer, 20, "r", pwdhash, (uint16_t) 20); if (rc != 0) return rc; print_hash("update bridge 2, read 20 bytes: ", pwdhash); BUILD_ASSERT(sizeof(buffer) > bytes_to_read); memset(buffer, 0, sizeof(buffer)); rc = snis_readsocket(ss->socket, buffer, bytes_to_read); if (rc != 0) return rc; fprintf(stderr, "snis_multiverse: update bridge 3\n"); pthread_mutex_lock(&data_mutex); i = lookup_ship_by_hash(pwdhash); if (i < 0) { fprintf(stderr, "snis_multiverse: Unknown ship hash\n"); pthread_mutex_unlock(&data_mutex); return rc; } fprintf(stderr, "snis_multiverse: update bridge 4\n"); o = &ship[i].entity; if (!o->tsd.ship.damcon) { o->tsd.ship.damcon = malloc(sizeof(*o->tsd.ship.damcon)); memset(o->tsd.ship.damcon, 0, sizeof(*o->tsd.ship.damcon)); } packed_buffer_init(&pb, buffer, bytes_to_read); unpack_bridge_update_packet(o, &pb); ship[i].initialized = 1; pthread_mutex_unlock(&data_mutex); rc = 0; fprintf(stderr, "snis_multiverse: update bridge 10\n"); return rc; }
int type_elementary_identifier_p(const struct RFstring *id) { const struct gperf_elementary_type *etype; // assert that the array size is same as enum size BUILD_ASSERT( sizeof(elementary_type_strings)/sizeof(struct RFstring) == ELEMENTARY_TYPE_TYPES_COUNT ); etype = types_string_is_elementary(rf_string_data(id), rf_string_length_bytes(id)); if (!etype) { return -1; } return etype->type; }
char *bitcoin_to_base58(const tal_t *ctx, bool test_net, const struct bitcoin_address *addr) { u8 buf[1 + sizeof(addr->addr) + 4]; char out[BASE58_ADDR_MAX_LEN + 2], *p; buf[0] = test_net ? 111 : 0; BUILD_ASSERT(sizeof(addr->addr) == sizeof(struct ripemd160)); memcpy(buf+1, addr, sizeof(addr->addr)); /* Append checksum */ base58_get_checksum(buf + 1 + sizeof(addr->addr), buf, 1 + sizeof(addr->addr)); p = encode_base58(out, BASE58_ADDR_MAX_LEN, buf, sizeof(buf)); return tal_strdup(ctx, p); }
void elasto_fh_free(struct elasto_fh *fh) { int ret; fh->ops.fh_free(fh->mod_priv); ret = dlclose(fh->mod_dl_h); if (ret != 0) { dbg(0, "failed to unload module (%d): %s\n", fh->type, dlerror()); } free(fh->open_path); BUILD_ASSERT(sizeof(ELASTO_FH_POISON) <= ARRAY_SIZE(fh->magic)); memcpy(fh->magic, ELASTO_FH_POISON, sizeof(ELASTO_FH_POISON)); free(fh); }
static const uint32_t * miniflow_get__(const struct miniflow *flow, unsigned int u32_ofs) { if (!(flow->map[u32_ofs / 32] & (1u << (u32_ofs % 32)))) { static const uint32_t zero = 0; return &zero; } else { const uint32_t *p = flow->values; BUILD_ASSERT(MINI_N_MAPS == 2); if (u32_ofs < 32) { p += popcount(flow->map[0] & ((1u << u32_ofs) - 1)); } else { p += popcount(flow->map[0]); p += popcount(flow->map[1] & ((1u << (u32_ofs - 32)) - 1)); } return p; } }
int main(int argc, char *argv[]) { #ifdef FAIL #if !HAVE_BUILTIN_TYPES_COMPATIBLE_P || !HAVE_TYPEOF #error We need typeof to check isalnum. #endif char #else unsigned char #endif c = argv[0][0]; #ifdef FAIL /* Fake fail on unsigned char platforms. */ BUILD_ASSERT((char)255 < 0); #endif return isalnum(c); }
bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num) { char *end; long l; l = strtol(buffer + tok->start, &end, 0); if (end != buffer + tok->end) return false; BUILD_ASSERT(sizeof(l) >= sizeof(*num)); *num = l; /* Check for overflow/underflow */ if ((l == LONG_MAX || l == LONG_MIN) && errno == ERANGE) return false; /* Check for truncation */ if (*num != l) return false; return true; }
bool json_tok_u64(const char *buffer, const jsmntok_t *tok, uint64_t *num) { char *end; unsigned long l; l = strtoul(buffer + tok->start, &end, 0); if (end != buffer + tok->end) return false; BUILD_ASSERT(sizeof(l) >= sizeof(*num)); *num = l; /* Check for overflow */ if (l == ULONG_MAX && errno == ERANGE) return false; if (*num != l) return false; return true; }
int elasto_fh_init(const struct elasto_fauth *auth, const char *open_path, uint64_t open_flags, struct elasto_fh **_fh) { struct elasto_fh *fh; int ret; const char *mod_path; uint64_t *_mod_vers; int (*mod_fh_init)(const struct elasto_fauth *auth, void **_fh_priv, struct elasto_fh_mod_ops *_ops); if (auth == NULL) { ret = -EINVAL; goto err_out; } if ((auth->type == ELASTO_FILE_AZURE) || (auth->type == ELASTO_FILE_ABB)) { mod_path = "libelasto_file_mod_apb.so"; } else if (auth->type == ELASTO_FILE_S3) { mod_path = "libelasto_file_mod_s3.so"; } else if (auth->type == ELASTO_FILE_AFS) { mod_path = "libelasto_file_mod_afs.so"; } else if (auth->type == ELASTO_FILE_LOCAL) { mod_path = "libelasto_file_mod_local.so"; } else { dbg(0, "unsupported auth type: %d\n", auth->type); ret = -EINVAL; goto err_out; } fh = malloc(sizeof(*fh)); if (fh == NULL) { ret = -ENOMEM; goto err_out; } memset(fh, 0, sizeof(*fh)); fh->type = auth->type; fh->open_path = strdup(open_path); if (fh->open_path == NULL) { goto err_fh_free; } fh->open_flags = open_flags; fh->mod_dl_h = dlopen(mod_path, RTLD_NOW); if (fh->mod_dl_h == NULL) { dbg(0, "failed to load module (%d) at path \"%s\": %s\n", auth->type, mod_path, dlerror()); ret = -EFAULT; goto err_path_free; } _mod_vers = dlsym(fh->mod_dl_h, ELASTO_FILE_MOD_VERS_SYM); if (_mod_vers == NULL) { dbg(0, "failed to find version symbol \"%s\" for module at %s: " "%s\n", ELASTO_FILE_MOD_VERS_SYM, mod_path, dlerror()); ret = -EFAULT; goto err_dl_close; } if (*_mod_vers != ELASTO_FILE_MOD_VERS_VAL) { dbg(0, "Invalid module %s version: %" PRIu64 ", expected " "%llu\n", mod_path, *_mod_vers, ELASTO_FILE_MOD_VERS_VAL); ret = -EFAULT; goto err_dl_close; } mod_fh_init = dlsym(fh->mod_dl_h, ELASTO_FILE_MOD_INIT_FN); if (mod_fh_init == NULL) { dbg(0, "failed to find init fn \"%s\" for module at %s: %s\n", ELASTO_FILE_MOD_INIT_FN, mod_path, dlerror()); ret = -EFAULT; goto err_dl_close; } /* initialise back-end module */ ret = mod_fh_init(auth, &fh->mod_priv, &fh->ops); if (ret < 0) { goto err_dl_close; } BUILD_ASSERT(sizeof(ELASTO_FH_MAGIC) <= ARRAY_SIZE(fh->magic)); memcpy(fh->magic, ELASTO_FH_MAGIC, sizeof(ELASTO_FH_MAGIC)); *_fh = fh; return 0; err_dl_close: dlclose(fh->mod_dl_h); err_path_free: free(fh->open_path); err_fh_free: free(fh); err_out: return ret; }
int main(int argc, char *argv[]) { BUILD_ASSERT(1 == 1); return 0; }
/* * load a resource from FLASH * buf and len shouldn't account for ECC even if partition is ECCed. * * The API here is a bit strange. * If resource has a STB container, buf will contain it * If loading subpartition with STB container, buff will *NOT* contain it * For trusted boot, the whole partition containing the subpart is measured. * * Additionally, the logic to work out how much to read from flash is insane. */ static int flash_load_resource(enum resource_id id, uint32_t subid, void *buf, size_t *len) { int i; int rc = OPAL_RESOURCE; struct ffs_handle *ffs; struct flash *flash; const char *name; bool status = false; bool ecc; bool part_signed = false; void *bufp = buf; size_t bufsz = *len; int ffs_part_num, ffs_part_start, ffs_part_size; int content_size = 0; int offset = 0; lock(&flash_lock); if (!system_flash) { /** * @fwts-label SystemFlashNotFound * @fwts-advice No system flash was found. Check for missing * calls flash_register(...). */ prlog(PR_WARNING, "FLASH: Can't load resource id:%i. " "No system flash found\n", id); goto out_unlock; } flash = system_flash; if (flash->busy) goto out_unlock; for (i = 0, name = NULL; i < ARRAY_SIZE(part_name_map); i++) { if (part_name_map[i].id == id) { name = part_name_map[i].name; break; } } if (!name) { prerror("FLASH: Couldn't find partition for id %d\n", id); goto out_unlock; } /* * If partition doesn't have a subindex but the caller specifies one, * we fail. eg. kernel partition doesn't have a subindex */ if ((part_name_map[i].subid == RESOURCE_SUBID_NONE) && (subid != RESOURCE_SUBID_NONE)) { prerror("PLAT: Partition %s doesn't have subindex\n", name); goto out_unlock; } rc = ffs_init(0, flash->size, flash->bl, &ffs, 1); if (rc) { prerror("FLASH: Can't open ffs handle: %d\n", rc); goto out_unlock; } rc = ffs_lookup_part(ffs, name, &ffs_part_num); if (rc) { /* This is not an error per-se, some partitions * are purposefully absent, don't spam the logs */ prlog(PR_DEBUG, "FLASH: No %s partition\n", name); goto out_free_ffs; } rc = ffs_part_info(ffs, ffs_part_num, NULL, &ffs_part_start, NULL, &ffs_part_size, &ecc); if (rc) { prerror("FLASH: Failed to get %s partition info\n", name); goto out_free_ffs; } prlog(PR_DEBUG,"FLASH: %s partition %s ECC\n", name, ecc ? "has" : "doesn't have"); if (ffs_part_size < SECURE_BOOT_HEADERS_SIZE) { prerror("FLASH: secboot headers bigger than " "partition size 0x%x\n", ffs_part_size); goto out_free_ffs; } rc = blocklevel_read(flash->bl, ffs_part_start, bufp, SECURE_BOOT_HEADERS_SIZE); if (rc) { prerror("FLASH: failed to read the first 0x%x from " "%s partition, rc %d\n", SECURE_BOOT_HEADERS_SIZE, name, rc); goto out_free_ffs; } part_signed = stb_is_container(bufp, SECURE_BOOT_HEADERS_SIZE); prlog(PR_DEBUG, "FLASH: %s partition %s signed\n", name, part_signed ? "is" : "isn't"); /* * part_start/size are raw pointers into the partition. * ie. they will account for ECC if included. */ if (part_signed) { bufp += SECURE_BOOT_HEADERS_SIZE; bufsz -= SECURE_BOOT_HEADERS_SIZE; content_size = stb_sw_payload_size(buf, SECURE_BOOT_HEADERS_SIZE); *len = content_size + SECURE_BOOT_HEADERS_SIZE; if (content_size > bufsz) { prerror("FLASH: content size > buffer size\n"); rc = OPAL_PARAMETER; goto out_free_ffs; } ffs_part_start += SECURE_BOOT_HEADERS_SIZE; rc = blocklevel_read(flash->bl, ffs_part_start, bufp, content_size); if (rc) { prerror("FLASH: failed to read content size %d" " %s partition, rc %d\n", content_size, name, rc); goto out_free_ffs; } if (subid == RESOURCE_SUBID_NONE) goto done_reading; rc = flash_subpart_info(bufp, content_size, ffs_part_size, NULL, subid, &offset, &content_size); if (rc) { prerror("FLASH: Failed to parse subpart info for %s\n", name); goto out_free_ffs; } bufp += offset; goto done_reading; } else /* stb_signed */ { /* * Back to the old way of doing things, no STB header. */ if (subid == RESOURCE_SUBID_NONE) { if (id == RESOURCE_ID_KERNEL || id == RESOURCE_ID_INITRAMFS) { /* * Because actualSize is a lie, we compute the * size of the BOOTKERNEL based on what the ELF * headers say. Otherwise we end up reading more * than we should */ content_size = sizeof_elf_from_hdr(buf); if (!content_size) { prerror("FLASH: Invalid ELF header part" " %s\n", name); rc = OPAL_RESOURCE; goto out_free_ffs; } } else { content_size = ffs_part_size; } if (content_size > bufsz) { prerror("FLASH: %s content size %d > " " buffer size %lu\n", name, content_size, bufsz); rc = OPAL_PARAMETER; goto out_free_ffs; } prlog(PR_DEBUG, "FLASH: computed %s size %u\n", name, content_size); rc = blocklevel_read(flash->bl, ffs_part_start, buf, content_size); if (rc) { prerror("FLASH: failed to read content size %d" " %s partition, rc %d\n", content_size, name, rc); goto out_free_ffs; } *len = content_size; goto done_reading; } BUILD_ASSERT(FLASH_SUBPART_HEADER_SIZE <= SECURE_BOOT_HEADERS_SIZE); rc = flash_subpart_info(bufp, SECURE_BOOT_HEADERS_SIZE, ffs_part_size, &ffs_part_size, subid, &offset, &content_size); if (rc) { prerror("FLASH: FAILED reading subpart info. rc=%d\n", rc); goto out_free_ffs; } *len = ffs_part_size; prlog(PR_DEBUG, "FLASH: Computed %s partition size: %u " "(subpart %u size %u offset %u)\n", name, ffs_part_size, subid, content_size, offset); /* * For a sub partition, we read the whole (computed) * partition, and then measure that. * Afterwards, we memmove() things back into place for * the caller. */ rc = blocklevel_read(flash->bl, ffs_part_start, buf, ffs_part_size); bufp += offset; } done_reading: /* * Verify and measure the retrieved PNOR partition as part of the * secure boot and trusted boot requirements */ secureboot_verify(id, buf, *len); trustedboot_measure(id, buf, *len); /* Find subpartition */ if (subid != RESOURCE_SUBID_NONE) { memmove(buf, bufp, content_size); *len = content_size; } status = true; out_free_ffs: ffs_close(ffs); out_unlock: unlock(&flash_lock); return status ? OPAL_SUCCESS : rc; }
void sng_setup_colors(void *gtk_widget, char *user_color_file) { int i; BUILD_ASSERT(ARRAY_SIZE(gradient_colors) == NGRADIENTS); /* values extracted from gdk_color_parse */ huex[WHITE].red = 65535; huex[WHITE].green = 65535; huex[WHITE].blue = 65535; huex[BLACK].red = 0; huex[BLACK].green = 0; huex[BLACK].blue = 0; huex[LIMEGREEN].red = 12850; huex[LIMEGREEN].green = 52685; huex[LIMEGREEN].blue = 12850; huex[DARKGREEN].red = 0; huex[DARKGREEN].green = 25700; huex[DARKGREEN].blue = 0; huex[YELLOW].red = 65535; huex[YELLOW].green = 65535; huex[YELLOW].blue = 0; huex[RED].red = 65535; huex[RED].green = 0; huex[RED].blue = 0; huex[ORANGE].red = 65535; huex[ORANGE].green = 42405; huex[ORANGE].blue = 0; huex[MAGENTA].red = 65535; huex[MAGENTA].green = 0; huex[MAGENTA].blue = 65535; huex[DARKRED].red = 35723; huex[DARKRED].green = 0; huex[DARKRED].blue = 0; huex[AMBER].red = 65535; huex[AMBER].green = 42405; huex[AMBER].blue = 0; huex[DARKTURQUOISE].red = 0; huex[DARKTURQUOISE].green = 52942; huex[DARKTURQUOISE].blue = 53713; huex[ORANGERED].red = 65535; huex[ORANGERED].green = 17733; huex[ORANGERED].blue = 0; for (i = 0; i < NSHADESOFGRAY; i++) { huex[GRAY + i].red = (i * 32767 * 2) / 256; huex[GRAY + i].green = (i * 32767 * 2) / 256; huex[GRAY + i].blue = (i * 32767 * 2) / 256; } for (i = 1; i <= NSHADECOLORS; i++) { int j, r, g, b; r = snis_randn(32767); g = snis_randn(32767); b = snis_randn(32767); for (j = 0; j < NSHADESOFGRAY / 2; j++) { int index; float f; f = (float) j / (float) (NSHADESOFGRAY / 2.0); index = GRAY + (i * NSHADESOFGRAY) + j; huex[index].red = (f * (float) r); huex[index].green = (f * (float) g); huex[index].blue = (f * (float) b); } for (j = NSHADESOFGRAY / 2; j < NSHADESOFGRAY; j++) { int index; float f; f = (float) (j - NSHADESOFGRAY / 2) / (float) NSHADESOFGRAY / 2.0; index = GRAY + (i * NSHADESOFGRAY) + j; huex[index].red = r + (f * ((32767.0 * 2.0) - (float) r)); huex[index].green = g + (f * ((32767.0 * 2.0) - (float) g)); huex[index].blue = b + (f * ((32767.0 * 2.0) - (float) b)); } } int grad_index = GRADIENTS; for (i=0; i<NGRADIENTS; i++ ) { int j; double h = gradient_colors[i].h; double s = gradient_colors[i].s; double v = gradient_colors[i].v; /* add the shades from black to color */ for (j=0; j<NGRADIENT_SHADES; j++) { double f = j/(double)NGRADIENT_SHADES; double fi = 1.0 - f; hsv2rgb(h, s + (1.0-s)*fi, v * f, &huex[grad_index]); grad_index++; } /* add the pure color */ hsv2rgb(h, s, v, &huex[grad_index]); *gradient_colors[i].color_index = grad_index; grad_index++; /* add the shades from color to white */ for (j=1; j<=NGRADIENT_SHADES; j++) { double f = (NGRADIENT_SHADES-j)/(double)NGRADIENT_SHADES; double fi = 1.0 - f; hsv2rgb(h, s * f, v + (1.0-v)*fi, &huex[grad_index]); grad_index++; } } sng_read_user_colors(user_color_file); fixup_ui_color(BLUE_FIXUP, BLUE); fixup_ui_color(GREEN_FIXUP, GREEN); fixup_ui_color(CYAN_FIXUP, CYAN); graph_dev_setup_colors(gtk_widget, huex, TOTAL_COLORS); }
static int unserialize(struct msg_t *msg, size_t *msg_length) { struct engine_t *engine = NULL; size_t effective_msg_length = 0; size_t allocated_msg_length = 0; ENTER(); if (!msg || !msg_length) { err("LEAVE, args error(%d)",0); return EINVAL; } engine = (struct engine_t*)msg->engine; if ((*msg_length < MIN_MSG_SIZE) || (msg->id != MSG_TO_ECI_ID) || !msg_string(msg->func) || (*msg_length < MSG_HEADER_LENGTH + msg->effective_data_length) || (engine && !check_engine(engine))) { msg("recv erroneous msg"); memset(msg, 0, MIN_MSG_SIZE); msg->id = MSG_TO_APP_ID; msg->func = MSG_UNDEFINED; *msg_length = MIN_MSG_SIZE; msg->res = ECIFalse; dbg("send msg '%s', length=%d, res=0x%x (#%d)", msg_string(msg->func), msg->effective_data_length, msg->res, msg->count); LEAVE(); return 0; } dbg("recv msg '%s', length=%d, engine=%p (#%d)", msg_string(msg->func), msg->effective_data_length, engine, msg->count); msg->id = MSG_TO_APP_ID; msg->effective_data_length = 0; switch(msg->func) { case MSG_ADD_TEXT: if (msg->data[msg->effective_data_length-1] != 0) { err("LEAVE, %s, data error, length=%d, <0x%x 0x%x 0x%x>", msg_string(msg->func), msg->effective_data_length, msg->data[msg->effective_data_length-3], msg->data[msg->effective_data_length-2], msg->data[msg->effective_data_length-1]); msg->res = ECIFalse; } else { dbg("text=%s", (char*)msg->data); dbg("eciAddText: handle=%p, data=%s", engine->handle, msg->data); msg->res = (uint32_t)eciAddText(engine->handle, msg->data); } break; case MSG_CLEAR_ERRORS: dbg("eciClearErrors: handle=%p", engine->handle); eciClearErrors(engine->handle); break; case MSG_CLEAR_INPUT: dbg("eciClearInput: handle=%p", engine->handle); eciClearInput(engine->handle); break; case MSG_COPY_VOICE: dbg("eciCopyVoice: handle=%p, from %d to %d", engine->handle, msg->args.cv.iVoiceFrom, msg->args.cv.iVoiceTo); msg->res = (uint32_t)eciCopyVoice(engine->handle, msg->args.cv.iVoiceFrom, msg->args.cv.iVoiceTo); break; case MSG_DELETE_DICT: dbg("eciDeleteDict: handle=%p, dict=%p", engine->handle, (char*)NULL + msg->args.dd.hDict); msg->res = (uint32_t)eciDeleteDict(engine->handle, (char*)NULL + msg->args.dd.hDict); break; case MSG_ERROR_MESSAGE: BUILD_ASSERT(MSG_HEADER_LENGTH + MAX_ERROR_MESSAGE <= PIPE_MAX_BLOCK); dbg("eciErrorMessage: handle=%p", engine->handle); eciErrorMessage(engine->handle, msg->data); msg->effective_data_length = MAX_ERROR_MESSAGE; msg("error=%s", (char*)msg->data); break; case MSG_GET_AVAILABLE_LANGUAGES: { struct msg_get_available_languages_t *lang = (struct msg_get_available_languages_t *)msg->data; BUILD_ASSERT(MSG_HEADER_LENGTH + sizeof(struct msg_get_available_languages_t) <= PIPE_MAX_BLOCK); lang->nb = sizeof(lang->languages)/sizeof(lang->languages[0]); dbg("eciGetAvailableLanguages"); msg->res = eciGetAvailableLanguages(lang->languages, &lang->nb); msg->effective_data_length = sizeof(struct msg_get_available_languages_t); dbg("nb lang=%d, msg->res=%d", lang->nb, msg->res); } break; case MSG_GET_DEFAULT_PARAM: dbg("eciGetDefaultParam: handle=%p", engine->handle); msg->res = (uint32_t)eciGetDefaultParam(msg->args.gp.Param); break; case MSG_GET_DICT: dbg("eciGetDict: handle=%p", engine->handle); msg->res = (uint32_t)eciGetDict(engine->handle); break; case MSG_GET_PARAM: dbg("eciGetParam: handle=%p, param=%d", engine->handle, msg->args.gp.Param); msg->res = (uint32_t)eciGetParam(engine->handle, msg->args.gp.Param); break; case MSG_GET_VOICE_PARAM: dbg("eciGetVoiceParam: handle=%p, voice=%d, param=%d", engine->handle, msg->args.gvp.iVoice, msg->args.gp.Param); msg->res = (uint32_t)eciGetVoiceParam(engine->handle, msg->args.gvp.iVoice, msg->args.gvp.Param); break; case MSG_INSERT_INDEX: dbg("eciInsertIndex: handle=%p", engine->handle); msg->res = (uint32_t)eciInsertIndex(engine->handle, msg->args.ii.iIndex); break; case MSG_LOAD_DICT: dbg("eciLoadDict: handle=%p, hDict=%p, DictVol=0x%x, filename=%s", engine->handle, (char*)NULL + msg->args.ld.hDict, msg->args.ld.DictVol, msg->data); msg->res = eciLoadDict(engine->handle, (char*)NULL + msg->args.ld.hDict, msg->args.ld.DictVol, msg->data); break; case MSG_NEW: { dbg("eciNew"); ECIHand h = eciNew(); if (h) { engine = calloc(1, sizeof(struct engine_t)); if (engine) { engine->id = ENGINE_ID; engine->handle = h; dbg("MSG_NEW: engine=%p, handle=%p", engine, h); } } msg->res = (uint32_t)engine; } break; case MSG_NEW_DICT: dbg("eciNewDict: handle=%p", engine->handle); msg->res = (uint32_t)eciNewDict(engine->handle); break; case MSG_NEW_EX: { dbg("eciNewEx: value=%d", msg->args.ne.Value); ECIHand h = eciNewEx(msg->args.ne.Value); if (h) { engine = calloc(1, sizeof(struct engine_t)); if (engine) { engine->id = ENGINE_ID; engine->handle = h; } } msg->res = (uint32_t)engine; } break; case MSG_PAUSE: dbg("eciPause: handle=%p", engine->handle); msg->res = (uint32_t)eciPause(engine->handle, msg->args.p.On); break; case MSG_PROG_STATUS: dbg("eciProgStatus: handle=%p", engine->handle); msg->res = eciProgStatus(engine->handle); break; case MSG_REGISTER_CALLBACK: { ECICallback cb = NULL; if (msg->args.rc.Callback) cb = my_callback; dbg("eciRegisterCallback, engine=%p, handle=%p, cb=%p", engine, engine->handle, cb); eciRegisterCallback(engine->handle, cb, engine); } break; case MSG_RESET: dbg("eciReset: handle=%p", engine->handle); eciReset(engine->handle); break; case MSG_SET_DEFAULT_PARAM: dbg("eciSetDefaultParam: handle=%p, p=%d, v=%d", engine->handle, msg->args.sp.Param, msg->args.sp.iValue); msg->res = (uint32_t)eciSetDefaultParam(msg->args.sp.Param, msg->args.sp.iValue); break; case MSG_SET_DICT: dbg("eciSetDict: handle=%p, d=%p", engine->handle, (char*)NULL + msg->args.sd.hDict); msg->res = (uint32_t)eciSetDict(engine->handle, (char*)NULL + msg->args.sd.hDict); break; case MSG_SET_OUTPUT_DEVICE: dbg("eciSetOutputDevice: handle=%p, dev=%d", engine->handle, msg->args.sod.iDevNum); msg->res = (uint32_t)eciSetOutputDevice(engine->handle, msg->args.sod.iDevNum); break; case MSG_SET_PARAM: dbg("eciSetParam: handle=%p, p=%d, v=%d", engine->handle, msg->args.sp.Param, msg->args.sp.iValue); msg->res = (uint32_t)eciSetParam(engine->handle, msg->args.sp.Param, msg->args.sp.iValue); break; case MSG_SET_VOICE_PARAM: dbg("eciSetVoiceParam: handle=%p, p=%d, v=%d", engine->handle, msg->args.svp.iVoice, msg->args.svp.iValue); msg->res = (uint32_t)eciSetVoiceParam(engine->handle, msg->args.svp.iVoice, msg->args.svp.Param, msg->args.svp.iValue); break; case MSG_SET_OUTPUT_BUFFER: set_output_buffer(my_voxind, engine, msg); break; case MSG_SET_OUTPUT_FILENAME: dbg("eciSetOutputFilename: handle=%p, d=%s", engine->handle, msg->data); msg->res = (uint32_t)eciSetOutputFilename(engine->handle, msg->data); break; case MSG_SYNTHESIZE: dbg("eciSynthesize: handle=%p", engine->handle); msg->res = (uint32_t)eciSynthesize(engine->handle); break; case MSG_SYNCHRONIZE: dbg("eciSynchronize: handle=%p", engine->handle); msg->res = (uint32_t)eciSynchronize(engine->handle); break; case MSG_SPEAKING: dbg("eciSpeaking: handle=%p", engine->handle); msg->res = (uint32_t)eciSpeaking(engine->handle); break; case MSG_STOP: dbg("eciStop: handle=%p", engine->handle); msg->res = (uint32_t)eciStop(engine->handle); break; case MSG_VERSION: BUILD_ASSERT(MSG_HEADER_LENGTH + MAX_VERSION <= PIPE_MAX_BLOCK); dbg("eciVersion"); eciVersion(msg->data); msg->effective_data_length = MAX_VERSION; dbg("version=%s", msg->data); break; default: msg->res = ECIFalse; break; } *msg_length = MSG_HEADER_LENGTH + msg->effective_data_length; exit0: dbg("send msg '%s', length=%d, res=0x%x (#%d)", msg_string(msg->func), msg->effective_data_length, msg->res, msg->count); LEAVE(); return 0; }