void parse_list(context& ctx) throw (coord) { parse_item(ctx); while (ctx.tag == COMA) { next_token(ctx); parse_item(ctx); } }
// Parse a rule completely, from the first item to the final period (or string). golem_rule *parse_rule(golem_tokenizer *tokenizer) { log("> parse rule [%d]\n", tokenizer->token); golem_rule *rule = new_rule(parse_item(tokenizer)); if (tokenizer->token == ',') { log("- parse rule: target\n"); (void)get_token(tokenizer); rule->target = parse_item(tokenizer); } if (tokenizer->token == ';') { log("- parse rule: others\n"); parse_rule_others(tokenizer, rule); } if (tokenizer->token == ':') { log("- parse rule: effects\n"); if (parse_rule_effects(tokenizer, rule)) { log("< parsed rule [%d]\n", tokenizer->token); return rule; } } if (tokenizer->token == '.') { get_token(tokenizer); log("< parsed rule [%d]\n", tokenizer->token); return rule; } log("Error: unfinished rule.\n"); return NULL; }
struct item* deserialize(char* buffer, int size) /*@ requires [?f0]world(?pub, ?key_clsfy) &*& [?f1]chars(buffer, size, ?cs); @*/ /*@ ensures [f0]world(pub, key_clsfy) &*& [f1]chars(buffer, size, cs) &*& item(result, ?i, pub) &*& [_]pub(i); @*/ { if (size <= MINIMAL_STRING_SIZE) abort_crypto_lib("Found corrupted item during deserialization"); struct item* item = malloc(sizeof(struct item)); if (item == 0){abort_crypto_lib("malloc of item failed");} //@ open [f0]world(pub, key_clsfy); //@ public_chars(buffer, size); //@ close [f0]world(pub, key_clsfy); item->size = size; item->content = malloc_wrapper(item->size); //@ assert item->content |-> ?cont; //@ chars_to_crypto_chars(buffer, size); //@ assert [f1]crypto_chars(normal, buffer, size, ?ccs); memcpy(item->content, buffer, (unsigned int) size); //@ get_forall_t<char>(); //@ get_forall_t<list<char> >(); parse_item(buffer, size); //@ retreive_proof_obligations(); //@ deserialize_item(ccs); //@ leak proof_obligations(pub); //@ assert [_]item_constraints(?i, ccs, pub) &*& [_]pub(i); //@ cs_to_ccs_crypto_chars(cont, cs); //@ cs_to_ccs_crypto_chars(buffer, cs); //@ chars_to_secret_crypto_chars(cont, size); //@ close item(item, i, pub); return item; }
/* parser and renderer for nested frames, single line */ int parse_frame_body_single (const int subslide) { switch (token) { case STRUCT: return parse_structure(); break; case IMAGE: return parse_figure(); break; case ANIMATION: return parse_animation(); break; case LISTING: return parse_listing(); break; case ITEM: return parse_item(1,item_type); break; case URL: return parse_url(); break; case COPY: return parse_copy(); break; default: fprintf(stderr,"[%d] Overlay mode: Illegal token %s\n", yylineno, get_token_name(token)); fprintf(stderr,"Expected commands: structure ('), image, listing, item and URL\n"); return 0; } }
PRIVATE ItemList *parse_item_list ARGS1(FILE *, fp) { ItemList *item_list = HTList_new(); Item *item; LexItem lex_item; for(;;) { if (!(item = parse_item(fp))) { HTList_delete(item_list); /* @@@@ */ item_list = NULL; return NULL; } HTList_addObject(item_list, (void*)item); lex_item = lex(fp); if (lex_item != LEX_ITEM_SEP) { unlex(lex_item); return item_list; } /* ** Here lex_item == LEX_ITEM_SEP; after item separator it ** is ok to have one or more newlines (LEX_REC_SEP) and ** they are ignored (continuation line). */ do { lex_item = lex(fp); } while (lex_item == LEX_REC_SEP); unlex(lex_item); } }
static tree parse_list (string s, int &i) { int item_indent= get_list_depth (s, i); tree r (DOCUMENT); while (can_parse_item (s, i, item_indent)) r << parse_item (s, i, item_indent); return compound ("itemize", r); }
void SchematicSceneParser::parse( sapecng::abstract_builder& builder ) { foreach(Item* item, items_) parse_item(builder, item); builder.flush(); }
bool DescriptionParser::parse_branch(const std::string& branch, const std::vector<std::string>& valid_types) { // split by '!', but can be escaped with '\\!' std::regex rgx("[^\\\\]!"); std::sregex_token_iterator iter(branch.begin(), branch.end(), rgx, -1); for (; iter != std::sregex_token_iterator(); ++iter) { if (static_cast<std::string>(*iter).empty()) continue; if (!parse_item(*iter, valid_types)) return false; } previous_quid_.clear(); return true; }
int parse_section() { match_line(SECTION_HEADER); match_line(SECTION_NAME); match_line(SECTION_NUMBER); parse_item(); output("};\n"); flush_cache(CACHE1); return 0; }
// Parse all child items enclosed in [] and separated by , golem_item *parse_item_children(golem_tokenizer *tokenizer, golem_item *item) { log("> parse children [%d]\n", tokenizer->token); golem_item *first_child = NULL, *child; for (;;) { if (!first_child) { first_child = child = parse_item(tokenizer); } else { child = child->next_sibling = parse_item(tokenizer); } child->parent = item; log("- got child “%s”\n", child->name); if (tokenizer->token == ']') { break; } else if (tokenizer->token != ',') { die("parse item children: expected ,"); } (void)get_token(tokenizer); } log("< parsed children [%d]\n", tokenizer->token); return first_child; }
void linphone_gtk_visibility_set(const char *hiddens, const char *window_name, GtkWidget *w, gboolean show){ char item[64]; const char *i; const char *b; int len; for(b=i=hiddens;*i!='\0';++i){ if (*i==' '){ len=MIN(i-b,sizeof(item)-1); strncpy(item,b,len); item[len]='\0'; b=i+1; parse_item(item,window_name,w,show); } } len=MIN(i-b,sizeof(item)-1); if (len>0){ strncpy(item,b,len); item[len]='\0'; parse_item(item,window_name,w,show); } }
/* parser and renderer for nested frames which use grouping */ int parse_frame_body_loop (const int subslide) { while (token != EOF) { switch (token) { case STRUCT: if (!parse_structure()) return 0; break; case IMAGE: if (!parse_figure()) return 0; break; case ANIMATION: if (!parse_animation()) return 0; break; case LISTING: if (!parse_listing()) return 0; break; case ITEM: if (!parse_item(1,item_type)) return 0; break; case URL: if (!parse_url()) return 0; break; case COLUMN: if (parse_column() == -1) return 0; break; case COLUMN_SEP: if (parse_column_sep() == -1) return 0; break; case END: if (parse_end() == -1) return 0; break; case CURLY_C_BRACE: return 1; case VALUE: // mirror value to output fprintf(ofile,"%s",string); token = yylex(); break; case NEWLINE: // ignore empty newline token = yylex(); break; default: fprintf(stderr, "<%s:%d> [%d] Frame mode: illegal token %s found.\n", __FILE__,__LINE__,yylineno, get_token_name(token)); fprintf(stderr, "Expected commands: uncover (+), overlay (#), only (~), structure ('), image, listing, item or URL\n"); fprintf(stderr, "\tor end frame mode with: New frame, section, title, author, date or an end of file\n"); return 0; } } return 1; }
static heim_array_t parse_array(struct parse_ctx *ctx) { heim_array_t array = heim_array_create(); int ret; heim_assert(*ctx->p == '[', "array doesn't start with ["); ctx->p += 1; while ((ret = parse_item(array, ctx)) > 0) ; if (ret < 0) { heim_release(array); return NULL; } return array; }
// Parse the “others” part of a rule (after ;, before :) void parse_rule_others(golem_tokenizer *tokenizer, golem_rule *rule) { golem_item *item; golem_item *last = NULL; for (;;) { (void)get_token(tokenizer); item = parse_item(tokenizer); if (last) { last->next_sibling = item; } else { rule->others = item; } last = item; if (tokenizer->token == ':') { return; } else if (tokenizer->token != ',') { die("parse rule (others): expected , or :"); } } }
int parse_proc_cmdline(int (*parse_item)(const char *key, const char *value, void *data), void *data, bool strip_prefix) { _cleanup_free_ char *line = NULL; const char *p; int r; assert(parse_item); r = proc_cmdline(&line); if (r < 0) return r; p = line; for (;;) { _cleanup_free_ char *word = NULL; char *value = NULL, *unprefixed; r = extract_first_word(&p, &word, NULL, EXTRACT_QUOTES|EXTRACT_RELAX); if (r < 0) return r; if (r == 0) break; /* Filter out arguments that are intended only for the * initrd */ unprefixed = startswith(word, "rd."); if (unprefixed && !in_initrd()) continue; value = strchr(word, '='); if (value) *(value++) = 0; r = parse_item(strip_prefix && unprefixed ? unprefixed : word, value, data); if (r < 0) return r; } return 0; }
void parse_lib(std::string filename) { int inpfile = open(filename.c_str(), O_RDONLY); if (inpfile == -1) { printf("[%s:%d] unable to open '%s'\n", __FUNCTION__, __LINE__, filename.c_str()); exit(-1); } int len = read(inpfile, buffer, sizeof(buffer)); if (len >= sizeof(buffer) - 1) { printf("[%s:%d] incomplete read of '%s'\n", __FUNCTION__, __LINE__, filename.c_str()); exit(-1); } parsenext(); if (tokval != "library") { printf("[%s:%d] 'library' keyword not found\n", __FUNCTION__, __LINE__); exit(-1); } validate_token(isId(tokval[0]), "name"); parseparam(); parse_item(false, "", nullptr); }
static struct head *parse_file(char *file) { FILE *fp; struct head *phead = NULL; struct item *pitem = NULL; char line[LEN]; char *pline = line; size_t n = LEN; int read; fp = fopen(file, "r+"); if (fp == NULL) { perror("open file failed"); return NULL; } phead = malloc(sizeof(struct head)); if (phead == NULL) { perror("malloc failed"); fclose(fp); return NULL; } phead->head = NULL; phead->tail = NULL; phead->num = 0; while ((read = getline(&pline, &n, fp)) != -1) { line[read-1] = '\0'; if (debug) printf("read [%s]\n", line); pitem = parse_item(line); if (pitem == NULL) break; add_item(pitem, phead); } fclose(fp); return phead; }
file_types_default_t::file_types_default_t(string_t const &, config_t const &config) : file_types_t() { if(config::check) return; string_t string = string_file(config.mime_types_filename.str()); in_t::ptr_t p = string; parse_item(string_t::empty, p, 0); if(!size) return; for( config::list_t<string_t>::ptr_t ptr = config.allow_gzip_list; ptr; ++ptr ) { item_t *item = lookup_item(ptr.val()); if(item) item->allow_gzip = true; } }
json::Value *json::parse(std::istream &s) { std::stack<json::Value *> struct_stack; json::Value *base_object = NULL; eat_whitespace(s); try { struct_stack.push(base_object = read_json_object_or_array(s)); while(!struct_stack.empty()) { parse_item(s, struct_stack); } } catch(...) { delete base_object; throw; } return base_object; }
struct item *asymmetric_decryption(struct item *key, struct item *item, char tag) /*@ requires [?f]world(?pub, ?key_clsfy) &*& true == valid_tag(tag) &*& principal(?principal1, ?count1) &*& item(item, ?enc, pub) &*& item(key, ?k, pub) &*& enc == asymmetric_encrypted_item(?principal2, ?count2, ?pay, ?ent) &*& k == private_key_item(?principal3, ?count3); @*/ /*@ ensures [f]world(pub, key_clsfy) &*& principal(principal1, count1 + 1) &*& item(item, enc, pub) &*& item(key, k, pub) &*& item(result, ?dec, pub) &*& tag_for_item(dec) == tag &*& col ? [_]pub(dec) : principal2 != principal3 || count2 != count3 ? true == key_clsfy(principal3, count3, false) &*& [_]pub(dec) : switch(pay) { case some(dec2): return dec == dec2; case none: return false; }; @*/ { struct item* result = 0; debug_print("DECRYPTING:\n"); print_item(item); check_is_asymmetric_encrypted(item); { pk_context context; unsigned int olen; char output[MAX_PACKAGE_SIZE]; // Key //@ close pk_context(&context); //@ open [f]world(pub, key_clsfy); pk_init(&context); //@ close [f]world(pub, key_clsfy); set_private_key(&context, key); //@ open [f]world(pub, key_clsfy); /*@ assert pk_context_with_key(&context, pk_private, ?principal, ?count, RSA_BIT_KEY_SIZE); @*/ // Decryption //@ open item(item, enc, pub); /*@ assert enc == asymmetric_encrypted_item(principal2, count2, pay, ent); @*/ //@ open [_]item_constraints(enc, ?enc_cs, pub); //@ assert [_]ic_parts(enc)(?enc_tag, ?enc_cont); //@ assert enc_cs == append(enc_tag, enc_cont); //@ open [_]ic_cg(enc)(_, ?enc_cg); //@ assert enc_cg == cg_asym_encrypted(principal2, count2, ?cs_pay, ent); if (item->size - TAG_LENGTH > RSA_KEY_SIZE || item->size - TAG_LENGTH < MINIMAL_STRING_SIZE) abort_crypto_lib("Asymmetric decryption failed: incorrect sizes"); //@ assert item->content |-> ?i_cont &*& item->size |-> ?i_size; //@ crypto_chars_split(i_cont, TAG_LENGTH); //@ drop_append(TAG_LENGTH, enc_tag, enc_cont); //@ assert crypto_chars(secret, i_cont + TAG_LENGTH, i_size - TAG_LENGTH, enc_cont); //@ if (col) enc_cg = chars_for_cg_sur(enc_cont, tag_asym_encrypted); //@ if (col) public_crypto_chars_extract(i_cont + TAG_LENGTH, enc_cg); //@ close cryptogram(i_cont + TAG_LENGTH, i_size - TAG_LENGTH, enc_cont, enc_cg); void *random_state = nonces_expose_state(); //@ close random_state_predicate(havege_state_initialized); /*@ produce_function_pointer_chunk random_function( asym_enc_havege_random_stub) (havege_state_initialized)(state, out, len) { call(); } @*/ //@ open principal(principal1, count1); //@ structure s = known_value(0, full_tag(tag)); //@ close decryption_pre(false, false, principal1, s, enc_cont); if(pk_decrypt(&context, item->content + TAG_LENGTH, (unsigned int) item->size - TAG_LENGTH, output, &olen, MAX_PACKAGE_SIZE, asym_enc_havege_random_stub, random_state) != 0) abort_crypto_lib("Decryption failed"); /*@ open decryption_post(false, ?garbage, principal1, s, ?p_key, ?c_key, ?cs_out); @*/ //@ assert u_integer(&olen, ?size_out); //@ pk_release_context_with_key(&context); //@ open cryptogram(i_cont + TAG_LENGTH, i_size - TAG_LENGTH, enc_cont, enc_cg); pk_free(&context); //@ open pk_context(&context); nonces_hide_state(random_state); //@ assert chars((void*)output + size_out, MAX_PACKAGE_SIZE - size_out, ?cs_rest); result = malloc(sizeof(struct item)); if (result == 0) {abort_crypto_lib("Malloc failed");} result->size = (int) olen; if ((int) olen <= MINIMAL_STRING_SIZE) abort_crypto_lib("Decryption: Incorrect size"); result->content = malloc(result->size); if (result->content == 0) {abort_crypto_lib("Malloc failed");} //@ close [f]world(pub, key_clsfy); //@ assert u_integer(&olen, ?olen_val); //@ assert crypto_chars(_, output, olen_val, cs_out); //@ crypto_chars_split(output, TAG_LENGTH); //@ assert crypto_chars(_, output, TAG_LENGTH, ?cs_tag); //@ assert crypto_chars(_, (void*) output + TAG_LENGTH, olen_val - TAG_LENGTH, ?cs_i); /*@ if (col) { crypto_chars_to_chars(output, TAG_LENGTH); chars_to_crypto_chars(output, TAG_LENGTH); } else if (!garbage) { switch(pay) { case some(pay1): open [_]item_constraints(pay1, cs_out, pub); case none: open [_]ill_formed_item_chars(enc)(cs_out); public_generated_split(polarssl_pub(pub), cs_out, TAG_LENGTH); } public_crypto_chars(output, TAG_LENGTH); } @*/ //@ close check_tag2_ghost_args(false, garbage, p_key, c_key, cs_i); check_tag2(output, tag); //@ if (!garbage) chars_to_secret_crypto_chars(output, TAG_LENGTH); //@ crypto_chars_join(output); memcpy(result->content, output, olen); //@ assert result->content |-> ?cont; //@ assert crypto_chars(?kind, cont, olen_val, cs_out); zeroize(output, (int) olen); //@ close item(item, enc, pub); //@ assert enc == asymmetric_encrypted_item(principal2, count2, pay, ent); //@ assert col || enc_cg == cg_asym_encrypted(principal2, count2, cs_pay, ent); /*@ if (col) { crypto_chars_to_chars(cont, olen_val); public_chars(cont, olen_val); chars_to_crypto_chars(cont, olen_val); } else if (garbage) { assert true == key_clsfy(principal3, count3, false); public_chars(cont, olen_val); chars_to_crypto_chars(cont, olen_val); } else { assert principal2 == principal3; assert count2 == count3; assert cs_out == cs_pay; switch(pay) { case some(pay1): assert [_]item_constraints(pay1, cs_out, pub); case none: open [_]ill_formed_item_chars(enc)(cs_out); assert [_]public_generated(polarssl_pub(pub))(cs_out); public_crypto_chars(cont, olen_val); chars_to_crypto_chars(cont, olen_val); } } @*/ parse_item(result->content, (int) olen); /*@ if (col || garbage) { retreive_proof_obligations(); deserialize_item(cs_out, pub); leak proof_obligations(pub); chars_to_secret_crypto_chars(cont, olen_val); } @*/ //@ open [_]item_constraints(?dec, cs_out, pub); //@ assert [_]ic_parts(dec)(?dec_tag, ?dec_cont); //@ take_append(TAG_LENGTH, dec_tag, dec_cont); //@ drop_append(TAG_LENGTH, dec_tag, dec_cont); //@ assert dec_tag == full_tag(tag); //@ assert tag_for_item(dec) == tag; //@ close item(result, dec, pub); } return result; }
void parse_item(bool capture, std::string pinName, AttributeList *attr) { while (tokval != "}" && !eof) { std::string paramname = tokval; validate_token(isId(tokval[0]), "name"); if (paramname == "default_intrinsic_fall" || paramname == "default_intrinsic_rise") { validate_token(tokval == ":", ":(paramname)"); if (capture) addAttr(attr, pinName, paramname, tokval); validate_token(isdigit(tokval[0]), "number"); } else if (paramname == "bus_type") { validate_token(tokval == ":", ":(bus_type)"); if (capture) addAttr(attr, pinName, paramname, tokval); validate_token(isId(tokval[0]), "name"); } else if (tokval == "(") { while (tokval == "(") { std::string paramstr = parseparam(); bool cell = paramname == "cell" && paramstr == options.cell; int ind = paramstr.find("["); if (capture && (paramname == "pin" || paramname == "bus")) { if (ind > 0 && paramstr[paramstr.length()-1] == ']') { std::string sub = paramstr.substr(ind+1); sub = sub.substr(0, sub.length()-1); paramstr = paramstr.substr(0, ind); capturePins[paramstr].pins[atol(sub.c_str())] = 1; } parse_item(true, paramstr, &capturePins[paramstr].attr); } else if (paramname == "type") { parse_item(true, "", &busInfo[paramstr].attr); } else parse_item(capture || cell, pinName, attr); //if (paramname == "cell") //printf("[%s:%d] paramname %s paramstr %s \n", __FUNCTION__, __LINE__, paramname.c_str(), paramstr.c_str()); if (cell) { processCell(); return; } paramname = tokval; if (!isId(tokval[0])) break; parsenext(); } } else { validate_token(tokval == ":", ":(other)"); if (capture && paramname != "timing_type") addAttr(attr, pinName, paramname, tokval); if (isdigit(tokval[0]) || isId(tokval[0]) || tokval[0] == '"') parsenext(); else validate_token(false, "number or name or string"); if (tokval != "}") validate_token(tokval == ";", ";"); } } validate_token(tokval == "}", "}"); }
struct item *symmetric_decryption(struct item *key, struct item *item) /*@ requires [?f]world(?pub, ?key_clsfy) &*& item(item, ?enc, pub) &*& enc == symmetric_encrypted_item(?principal1, ?count1, ?pay, ?ent) &*& item(key, ?k, pub) &*& k == symmetric_key_item(?principal2, ?count2); @*/ /*@ ensures [f]world(pub, key_clsfy) &*& item(item, enc, pub) &*& item(key, k, pub) &*& item(result, ?dec, pub) &*& col ? [_]pub(dec) : switch(pay) { case some(dec2): return principal1 == principal2 && count1 == count2 && dec == dec2; case none: return false; }; @*/ { debug_print("DECRYPTING:\n"); print_item(item); check_is_symmetric_encrypted(item); //@ open [f]world(pub, key_clsfy); struct item* result; result = malloc(sizeof(struct item)); if (result == 0) abort_crypto_lib("Malloc failed"); { gcm_context gcm_context; char *iv; char iv_buffer[GCM_IV_SIZE]; char *encrypted; //@ open item(key, k, pub); //@ assert key->content |-> ?k_cont &*& key->size |-> ?k_size; check_valid_symmetric_key_item_size(key->size); //@ open [_]item_constraints(k, ?k_cs0, pub); //@ assert [_]ic_parts(k)(?k_tag, ?k_cs); //@ crypto_chars_limits(k_cont); //@ crypto_chars_split(k_cont, TAG_LENGTH); //@ WELL_FORMED(k_tag, k_cs, TAG_SYMMETRIC_KEY) //@ assert crypto_chars(secret, k_cont, TAG_LENGTH, k_tag); //@ assert crypto_chars(secret, k_cont + TAG_LENGTH, GCM_KEY_SIZE, k_cs); //@ cryptogram k_cg = cg_symmetric_key(principal2, count2); //@ if (col) k_cg = chars_for_cg_sur(k_cs, tag_symmetric_key); //@ if (col) crypto_chars_to_chars(k_cont + TAG_LENGTH, GCM_KEY_SIZE); //@ if (col) public_chars_extract(k_cont + TAG_LENGTH, k_cg); //@ if (col) chars_to_secret_crypto_chars(k_cont + TAG_LENGTH, GCM_KEY_SIZE); //@ close cryptogram(k_cont + TAG_LENGTH, GCM_KEY_SIZE, k_cs, k_cg); //@ close gcm_context(&gcm_context); if (gcm_init(&gcm_context, POLARSSL_CIPHER_ID_AES, (key->content + TAG_LENGTH), (unsigned int) GCM_KEY_SIZE * 8) != 0) abort_crypto_lib("Init gcm failed"); //@ assert gcm_context_initialized(&gcm_context, ?p, ?c); //@ assert col || (p == principal2 && c == count2); //@ open cryptogram(k_cont + TAG_LENGTH, GCM_KEY_SIZE, k_cs, k_cg); //@ crypto_chars_join(k_cont); //@ close item(key, k, pub); //@ open item(item, enc, pub); //@ assert item->content |-> ?i_cont &*& item->size |-> ?i_size; //@ open [_]item_constraints(enc, ?cs, pub); //@ open [_]ic_parts(enc)(?enc_tag, ?enc_cont); //@ open [_]ic_cg(enc)(_, ?enc_cg); //@ take_append(TAG_LENGTH, enc_tag, enc_cont); //@ drop_append(TAG_LENGTH, enc_tag, enc_cont); //@ open [_]ic_sym_enc(enc)(?enc_iv, ?cg_cs); //@ assert true == well_formed(cs, nat_length(cs)); //@ close [1/2]hide_crypto_chars(_, i_cont, i_size, cs); check_valid_symmetric_encrypted_item_size(item->size); //@ assert length(cs) > TAG_LENGTH + GCM_IV_SIZE; int size = item->size - TAG_LENGTH - GCM_IV_SIZE - GCM_MAC_SIZE; if (size <= MINIMAL_STRING_SIZE) abort_crypto_lib("Gcm decryption failed"); //@ crypto_chars_limits(i_cont); //@ crypto_chars_split(i_cont, TAG_LENGTH); iv = item->content + TAG_LENGTH; //@ crypto_chars_split(iv, GCM_IV_SIZE); //@ assert [1/2]crypto_chars(secret, iv, GCM_IV_SIZE, ?iv_cs); memcpy(iv_buffer, iv, GCM_IV_SIZE); //@ assert cs == append(enc_tag, enc_cont); //@ assert enc_cont == append(iv_cs, cg_cs); //@ public_crypto_chars(iv, GCM_IV_SIZE); //@ chars_to_secret_crypto_chars(iv, GCM_IV_SIZE); encrypted = iv + GCM_IV_SIZE; //@ crypto_chars_limits(encrypted); //@ crypto_chars_split(encrypted, GCM_MAC_SIZE); //@ assert [1/2]crypto_chars(secret, encrypted, GCM_MAC_SIZE, ?mac_cs); /*@ assert [1/2]crypto_chars(secret, encrypted + GCM_MAC_SIZE, size, ?enc_cs); @*/ //@ assert cg_cs == append(mac_cs, enc_cs); result->size = size; result->content = malloc(size); if (result->content == 0) abort_crypto_lib("Malloc failed"); //@ assert result->content |-> ?r_cont &*& result->size |-> size; //@ if (col) enc_cg = chars_for_cg_sur(cg_cs, tag_auth_encrypted); //@ close exists(enc_cg); if (gcm_auth_decrypt(&gcm_context, (unsigned int) size, iv_buffer, GCM_IV_SIZE, NULL, 0, encrypted, GCM_MAC_SIZE, encrypted + GCM_MAC_SIZE, result->content) != 0) abort_crypto_lib("Gcm decryption failed"); //@ assert crypto_chars(secret, r_cont, size, ?dec_cs); //@ assert col || enc_cg == cg_auth_encrypted(principal1, count1, dec_cs, iv_cs); //@ crypto_chars_join(encrypted); //@ crypto_chars_join(iv); //@ crypto_chars_join(i_cont); //@ open [1/2]hide_crypto_chars(_, i_cont, i_size, cs); //@ close item(item, enc, pub); gcm_free(&gcm_context); //@ open gcm_context(&gcm_context); zeroize(iv_buffer, GCM_IV_SIZE); //@ close [f]world(pub, key_clsfy); /*@ if (col) { crypto_chars_to_chars(r_cont, size); chars_to_crypto_chars(r_cont, size); } else { assert enc == symmetric_encrypted_item(principal1, count1, pay, ent); assert enc_cg == cg_auth_encrypted(principal1, count1, dec_cs, enc_iv); switch(pay) { case some(pay1): assert [_]item_constraints(pay1, dec_cs, pub); case none: open [_]ill_formed_item_chars(enc)(dec_cs); assert [_]public_generated(polarssl_pub(pub))(dec_cs); public_crypto_chars(r_cont, size); chars_to_crypto_chars(r_cont, size); } } @*/ parse_item(result->content, size); /*@ if (col) { public_chars(r_cont, size); chars_to_secret_crypto_chars(r_cont, size); retreive_proof_obligations(); deserialize_item(dec_cs, pub); leak proof_obligations(pub); } @*/ //@ assert crypto_chars(secret, r_cont, size, dec_cs); //@ assert [_]item_constraints(?r, dec_cs, pub); //@ close item(result, r, pub); } debug_print("DECRYPTING RESULT:\n"); print_item(result); return result; }
// parse_message_payload: parse a message payload into a // kssl_operation struct kssl_error_code parse_message_payload(BYTE *payload, // int len, // kssl_operation *operation) { // int offset = 0; kssl_item temp_item; if (payload == NULL || operation == NULL) { return KSSL_ERROR_INTERNAL; } zero_operation(operation); // Count number of items and validate structure while (offset < len) { if (len - offset < (int)(KSSL_ITEM_HEADER_SIZE)) { return KSSL_ERROR_FORMAT; } if (parse_item(payload, &offset, &temp_item) != KSSL_ERROR_NONE || len < offset) { return KSSL_ERROR_FORMAT; } // Iterate through known tags, populating necessary values switch (temp_item.tag) { case KSSL_TAG_OPCODE: { // Skip over malformed tags if (temp_item.length != 1) { continue; } operation->opcode = temp_item.data[0]; operation->is_opcode_set = 1; break; } case KSSL_TAG_DIGEST: { // Skip over malformed tags if (temp_item.length != KSSL_DIGEST_SIZE) continue; operation->digest = temp_item.data; operation->is_digest_set = 1; break; } case KSSL_TAG_PAYLOAD: { operation->payload_len = temp_item.length; operation->payload = temp_item.data; operation->is_payload_set = 1; break; } case KSSL_TAG_CLIENT_IP: { operation->ip_len = temp_item.length; operation->ip = temp_item.data; operation->is_ip_set = 1; break; } case KSSL_TAG_PADDING: { break; } default: break; } } // check to see if opcode and payload are set if (operation->is_opcode_set == 0 || operation->is_payload_set == 0) { return KSSL_ERROR_FORMAT; } return KSSL_ERROR_NONE; }
static void config_value(const char* header, const char* name, char* value, config_ctx* ctx) { char* suffix; if(strcmp(header, CONFIG_GENERAL) == 0) { if(strcmp(name, CONFIG_RRD) == 0) { file_path *p = (file_path*)xcalloc(sizeof(*p)); p->path = value; /* Add the new path to the rrd list */ p->next = ctx->rrdlist; ctx->rrdlist = p; } if(strcmp(name, CONFIG_RAW) == 0) { file_path *p = (file_path*)xcalloc(sizeof(*p)); p->path = value; /* Add the new path to the raw list */ p->next = ctx->rawlist; ctx->rawlist = p; } /* Ignore other [general] options */ return; } if(strcmp(header, CONFIG_POLL) != 0) return; if(strcmp(name, CONFIG_INTERVAL) == 0) { char* t; int i; if(ctx->interval > 0) errx(2, "%s: " CONFIG_INTERVAL " specified twice: %s", ctx->confname, value); i = strtol(value, &t, 10); if(i < 1 || *t) errx(2, "%s: " CONFIG_INTERVAL " must be a number (seconds) greater than zero: %s", ctx->confname, value); ctx->interval = (uint32_t)i; return; } if(strcmp(name, CONFIG_TIMEOUT) == 0) { char* t; int i; if(ctx->timeout > 0) errx(2, "%s: " CONFIG_TIMEOUT " specified twice: %s", ctx->confname, value); i = strtol(value, &t, 10); if(i < 1 || *t) errx(2, "%s: " CONFIG_TIMEOUT " must be a a number (seconds) greater than zero: %s", ctx->confname, value); ctx->timeout = (uint32_t)i; return; } /* Parse out suffix */ suffix = strchr(name, '.'); if(!suffix) /* Ignore unknown options */ return; *suffix = 0; suffix++; /* If it starts with "field.source" */ if(strcmp(suffix, CONFIG_SOURCE) == 0) { const char* t; /* Check the name */ t = name + strspn(name, FIELD_VALID); if(*t) errx(2, "%s: the '%s' field name must only contain characters, digits, underscore and dash", ctx->confname, name); /* Parse out the field */ parse_item(name, value, ctx); } /* If it starts with "field.reference" */ if(strcmp(suffix, CONFIG_REFERENCE) == 0) { /* Parse out the field */ parse_item_reference(name, value, ctx); } }
// Parse an effect from the list of effects. golem_effect *parse_effect(golem_tokenizer *tokenizer) { log("> parse effect [%d]\n", tokenizer->token); int token; golem_effect *effect = mallocd(sizeof(golem_effect)); effect->reference = 0; effect->param.reference = 0; effect->next = NULL; switch (tokenizer->token) { case token_name: effect->type = effect_item; effect->param.item = parse_item(tokenizer); log("- item effect (%s)\n", effect->param.item->name); break; case token_string: effect->type = effect_string; effect->param.string = get_token_string(tokenizer); log("- string effect (%s)\n", effect->param.string); break; case token_ref: effect->reference = get_token_ref(tokenizer); token = get_token(tokenizer); switch (token) { case '+': case '-': effect->type = effect_tag; effect->param.tag = parse_tag(tokenizer); log("- tag effect (%d/%c%s)\n", effect->reference, effect->param.tag->sign ? '+' : '-', effect->param.tag->name); break; case '[': token = get_token(tokenizer); switch (token) { case token_ref: effect->type = effect_move_ref_ref; effect->param.reference = get_token_ref(tokenizer); log("- move ref/ref effect (%d/%d)\n", effect->reference, effect->param.reference); break; case token_name: effect->type = effect_move_ref_item; effect->param.item = parse_item(tokenizer); log("- move ref/item effect (%d/%s)\n", effect->reference, effect->param.item->name); break; default: die("parse effect: error parsing move effect"); } token = get_token(tokenizer); if (token != ']') { die("parse effect: error parsing move effect, expected ]"); } break; default: die("parse effect: syntax error."); } break; case '-': token = get_token(tokenizer); if (token != token_ref) { die("parse effect: expected reference after -"); } effect->type = effect_remove; effect->reference = get_token_ref(tokenizer); log("- remove effect (%d)\n", effect->reference); break; default: die("parse effect: syntax error"); } if (effect->type != effect_item) { (void)get_token(tokenizer); } log("< parsed effect (%d) [%d]\n", effect->type, tokenizer->token); return effect; }
int parse_item(int present_indent, const char *mode) { fprintf(ofile,"\\begin{%s}\n",mode); if (!parse_item_value(mode)) return 0; while (token != EOF) { switch (token) { case INDENT: if (present_indent==int_val) { // same indent if ((token = yylex()) == ITEM) { if (!parse_item_value(mode)) return 0; } else { fprintf(stderr, "<%s:%d> [%d] Item expected, but %s \"%s\" found.\n", __FILE__,__LINE__,yylineno,get_token_name(token),yytext); return 0; } } else if (present_indent>int_val) { // less indent go back fprintf(ofile,"\\end{%s}\n",mode); return 1; } else if (present_indent<int_val) { // new indent go sub if ((token = yylex()) == ITEM) { if (!parse_item(int_val,item_type)) return 0; } else { fprintf(stderr, "<%s:%d> [%d] Item expected, but %s \"%s\" found.\n", __FILE__,__LINE__,yylineno,get_token_name(token),yytext); return 0; } } break; case ITEM: // this means zero new_indent if (present_indent>1) { fprintf(ofile,"\\end{%s}\n",mode); return 1; } else if (!parse_item_value(mode)) return 0; break; case VALUE: case SECTION: case FRAME: case IMAGE: case STRUCT: case NEWLINE: case EOF: case COLUMN: case COLUMN_SEP: case END: case CURLY_C_BRACE: case ZERO: // leave frame => close all item list fprintf(ofile,"\\end{%s}\n",mode); return 1; default: fprintf(stderr, "<%s:%d> [%d] Top mode: %s \"%s\" not allowed in top mode.\n", __FILE__,__LINE__, yylineno, get_token_name(token), yytext); fprintf(stderr, "<%s:%d> Expected: item, indent, section, frame, image, structure ('), newline, or end of file\n", __FILE__,__LINE__); return 0; } } fprintf(stderr, "<%s:%d> [%d] Item mode: Unhandled EOF\n",__FILE__,__LINE__, yylineno); return 0; }
/* * parse the content of a frame */ int parse_frame() { int subslide = 1; structure_count = 0; fprintf(ofile,"%% FRAME\n"); fprintf(ofile,"\\begin{frame}[fragile]\n"); if ((token = yylex()) == VALUE) { fprintf(ofile,"\\frametitle{%s}\n", string); token = yylex(); while (token != EOF) { switch (token) { case SUBTITLE: if (!parse_subtitle()) return 0; break; case UNCOVER: if (!parse_frame_body("uncover",subslide++)) return 0; break; case OVERLAY: if (!parse_frame_body("overlay",subslide++)) return 0; break; case ONLY: if (!parse_frame_body("only",subslide++)) return 0; break; case STRUCT: if (!parse_structure()) return 0; break; case IMAGE: if (!parse_figure()) return 0; break; case ANIMATION: if (!parse_animation()) return 0; break; case LISTING: if (!parse_listing()) return 0; break; case ITEM: if (!parse_item(1,item_type)) return 0; break; case URL: if (!parse_url()) return 0; break; case COPY: if (!parse_copy()) return 0; break; case COLUMN: if (parse_column() == -1) return 0; break; case COLUMN_SEP: if (parse_column_sep() == -1) return 0; break; case END: if (parse_end() == -1) return 0; break; case VALUE: // mirror value to output fprintf(ofile,"%s",string); token = yylex(); break; case FRAME: case SECTION: case TITLE: case AUTHOR: case DATE: case ZERO: case PART: fprintf(ofile,"\\end{frame}\n\n"); return 1; case NEWLINE: // ignore empty newline token = yylex(); break; default: fprintf(stderr, "<%s:%d> [%d] Frame mode: illegal token %s found.\n", __FILE__,__LINE__,yylineno, get_token_name(token)); fprintf(stderr, "Expected commands: uncover (+), overlay (#), only (~), structure ('), image, listing, item or URL\n"); fprintf(stderr, "\tor end frame mode with: New frame, section, title, author, date or an end of file\n"); return 0; } } return 1; } else { fprintf(stderr, "<%s:%d> [%d] Frame mode: expected value, but %s found.\n", __FILE__,__LINE__, yylineno, get_token_name(token)); return 0; } }
void parse_pair_item(char* message, int size) /*@ requires FORALLP_C &*& FORALLP_CS &*& [?f1]world(?pub, ?key_clsfy) &*& exists(?ccs_tag) &*& ccs_tag == full_ctag(c_to_cc(TAG_PAIR)) &*& TAG_LENGTH == length(ccs_tag) &*& size <= INT_MAX - TAG_LENGTH &*& head(ccs_tag) == c_to_cc(TAG_PAIR) &*& [?f2]crypto_chars(?kind, message, size, ?ccs_cont) &*& switch(kind) { case normal: return true; case secret: return [_]item_constraints(pair_item(_, _), append(ccs_tag, ccs_cont), pub); }; @*/ /*@ ensures [f1]world(pub, key_clsfy) &*& [f2]crypto_chars(kind, message, size, ccs_cont) &*& true == well_formed_ccs(forallc, forallcs, nat_length(append(ccs_tag, ccs_cont)), append(ccs_tag, ccs_cont)); @*/ { //@ open [f1]world(pub, key_clsfy); //@ close [f1]world(pub, key_clsfy); if (size <= (int) sizeof(int)) abort_crypto_lib("Incorrect size for pair item"); //@ list<crypto_char> ccs = append(ccs_tag, ccs_cont); //@ take_append(TAG_LENGTH, ccs_tag, ccs_cont); //@ drop_append(TAG_LENGTH, ccs_tag, ccs_cont); //@ crypto_chars_limits(message); //@ crypto_chars_split(message, sizeof(int)); //@ assert [f2]crypto_chars(kind, message, sizeof(int), ?size_f_ccs); /*@ assert [f2]crypto_chars(kind, message + sizeof(int), size - sizeof(int), ?ccs_p); @*/ //@ take_append(sizeof(int), size_f_ccs, ccs_p); //@ drop_append(sizeof(int), size_f_ccs, ccs_p); //@ assert ccs_cont == append(size_f_ccs, ccs_p); /*@ if (kind == secret) { assert [_]item_constraints(?p, ccs, pub); OPEN_ITEM_CONSTRAINTS(p, ccs, pub); public_crypto_chars(message, sizeof(int)); } else { crypto_chars_to_chars(message, sizeof(int)); } @*/ //@ assert [f2]chars(message, sizeof(int), ?size_f_cs); //@ assert cs_to_ccs(size_f_cs) == size_f_ccs; //@ chars_to_integer(message); int size_f = *((int*) ((void*) message)); //@ assert size_f == int_of_chars(size_f_cs); //@ integer_to_chars(message); if (size_f < MINIMAL_STRING_SIZE || size_f > size - (int) sizeof(int)) abort_crypto_lib("Incorrect size for pair item"); int size_s = size - (int) sizeof(int) - size_f; //@ crypto_chars_limits(message + sizeof(int)); //@ crypto_chars_split(message + sizeof(int), size_f); /*@ assert [f2]crypto_chars(kind, message + (int) sizeof(int), size_f, ?ccs_f); @*/ /*@ assert [f2]crypto_chars(kind, message + (int) sizeof(int) + size_f, size_s, ?ccs_s); @*/ //@ take_append(size_f, ccs_f, ccs_s); //@ drop_append(size_f, ccs_f, ccs_s); //@ assert ccs_p == append(ccs_f, ccs_s); if (size_f <= TAG_LENGTH || size_s <= TAG_LENGTH) abort_crypto_lib("Incorrect size for pair item"); /*@ assert ccs_cont == append(cs_to_ccs(chars_of_unbounded_int(length(ccs_f))), append(ccs_f, ccs_s)); @*/ /*@ if (kind == secret) { assert [_]item_constraints(?p, ccs, pub); OPEN_ITEM_CONSTRAINTS(p, ccs, pub); assert [_]ic_pair(p)(?ccs_f0, ?ccs_s0); assert [_]item_constraints(_, ccs_f0, pub); assert [_]item_constraints(_, ccs_s0, pub); cs_to_ccs_inj(chars_of_int(length(ccs_f)), chars_of_int(length(ccs_f0))); take_append(size_f, ccs_f0, ccs_s0); drop_append(size_f, ccs_f0, ccs_s0); } @*/ parse_item(message + (int) sizeof(int), size_f); parse_item(message + (int) sizeof(int) + size_f, size_s); //@ crypto_chars_join(message + sizeof(int)); //@ if (kind == normal) chars_to_crypto_chars(message, sizeof(int)); //@ if (kind == secret) chars_to_secret_crypto_chars(message, sizeof(int)); //@ crypto_chars_join(message); //@ length_equals_nat_length(ccs); //@ length_equals_nat_length(ccs_cont); /*@ switch(nat_length(ccs)) { case succ(n): well_formed_upper_bound(nat_length(ccs_f), nat_length(ccs_cont), ccs_f); well_formed_upper_bound(nat_length(ccs_s), nat_length(ccs_cont), ccs_s); assert length(ccs) > 0; assert length(ccs) <= INT_MAX; assert true == well_formed_ccs(forallc, forallcs, nat_length(ccs_f), ccs_f); assert true == well_formed_ccs(forallc, forallcs, nat_length(ccs_s), ccs_s); head_append(ccs_tag, ccs_cont); well_formed_pair_item(ccs, size_f, ccs_f, ccs_s); case zero: assert false; } @*/ }
void SchematicSceneParser::parse_item( sapecng::abstract_builder& builder, Item* item ) { SchematicScene::SupportedItemType type = SchematicScene::itemType(item); std::map<std::string, std::string> props; storeItemData(props, type, item); QtProperty* properties = SchematicScene::itemProperties(item); QHash<QString, QString> subproperties; if(properties) { subproperties.insert("__NAME", properties->valueText()); foreach(QtProperty* prop, properties->subProperties()) subproperties.insert(prop->propertyName(), prop->valueText()); } switch(type) { case SchematicScene::GroundItemType: case SchematicScene::PortItemType: { // add as unknow Component* component = static_cast<Component*>(item); props["node"] = QString::number(component->nodes().front()).toStdString(); builder.add_unknow_component(props); break; } case SchematicScene::LabelItemType: { // add as unknow Label* label = static_cast<Label*>(item); props["text"] = label->text().toStdString(); builder.add_unknow_component(props); break; } case SchematicScene::WireItemType: { Wire* wire = static_cast<Wire*>(item); props["orientation"] = QString::number(wire->orientation()).toStdString(); props["to_x"] = QString::number(wire->toPoint().x()).toStdString(); props["to_y"] = QString::number(wire->toPoint().y()).toStdString(); props["conn"] = QString::number(wire->isJunctionsConnected()).toStdString(); builder.add_wire_component(props); break; } case SchematicScene::OutItemType: { if(!discard_) { Component* out = static_cast<Component*>(item); builder.add_out_component(out->nodes().front(), props); } break; } case SchematicScene::VoltmeterItemType: case SchematicScene::AmmeterItemType: { Component* component = static_cast<Component*>(item); QVector<int> nodes = component->nodes(); builder.add_dual_component( dualMap_[type], "", 1., true, nodes.at(1), nodes.at(0), props ); break; } case SchematicScene::CapacitorItemType: case SchematicScene::ConductanceItemType: case SchematicScene::InductorItemType: case SchematicScene::ResistorItemType: case SchematicScene::CurrentSourceItemType: case SchematicScene::VoltageSourceItemType: { Component* component = static_cast<Component*>(item); QVector<int> nodes = component->nodes(); storeLabel(props, component); builder.add_dual_component( dualMap_[type], subproperties.value("__NAME").toStdString(), subproperties.value("Value").toDouble(), QVariant(subproperties.value("Symbolic")).toBool(), nodes.at(1), nodes.at(0), props ); break; } case SchematicScene::CCCSItemType: case SchematicScene::CCVSItemType: case SchematicScene::VCCSItemType: case SchematicScene::VCVSItemType: { Component* component = static_cast<Component*>(item); QVector<int> nodes = component->nodes(); storeLabel(props, component); builder.add_quad_component( quadMap_[type], subproperties.value("__NAME").toStdString(), subproperties.value("Value").toDouble(), QVariant(subproperties.value("Symbolic")).toBool(), nodes.at(1), nodes.at(0), nodes.at(3), nodes.at(2), props ); break; } case SchematicScene::OpAmplItemType: { Component* component = static_cast<Component*>(item); QVector<int> nodes = component->nodes(); storeLabel(props, component); builder.add_quad_component( quadMap_[type], subproperties.value("__NAME").toStdString(), 1., false, SchematicScene::Ground, nodes.at(2), nodes.at(1), nodes.at(0), props ); break; } case SchematicScene::TransformerItemType: { Component* component = static_cast<Component*>(item); QVector<int> nodes = component->nodes(); storeLabel(props, component); builder.add_quad_component( quadMap_[type], subproperties.value("__NAME").toStdString(), subproperties.value("Value").toDouble(), QVariant(subproperties.value("Symbolic")).toBool(), nodes.at(3), nodes.at(2), nodes.at(1), nodes.at(0), props ); break; } case SchematicScene::MutualInductanceItemType: { Component* component = static_cast<Component*>(item); QVector<int> nodes = component->nodes(); storeLabel(props, component); props["lp:name"] = subproperties.value("lp:name").toStdString(); props["lp:value"] = subproperties.value("lp:value").toStdString(); props["ls:name"] = subproperties.value("ls:name").toStdString(); props["ls:value"] = subproperties.value("ls:value").toStdString(); builder.add_quad_component( quadMap_[type], subproperties.value("__NAME").toStdString(), subproperties.value("Value").toDouble(), QVariant(subproperties.value("Symbolic")).toBool(), nodes.at(3), nodes.at(2), nodes.at(1), nodes.at(0), props ); break; } case SchematicScene::UserDefItemType: { Component* component = static_cast<Component*>(item); storeLabel(props, component); builder.begin_userdef_component( subproperties.value("__NAME").toStdString(), props ); ++discard_; QPointer<qsapecng::SchematicScene> scene = item->data(101).value< QPointer<qsapecng::SchematicScene> >(); foreach(Item* item, scene->activeItems()) parse_item(builder, item); --discard_; builder.end_userdef_component( subproperties.value("__NAME").toStdString(), props ); // outer-to-inner port buffer resistors QVector<int> nodes = component->nodes(); QVector<int> ports = scene->ports(); if(nodes.size() == ports.size()) { for(int i = 0; i < nodes.size(); ++i) builder.add_dual_component( dualMap_[SchematicScene::ResistorItemType], "", .0, false, nodes.at(i), ports.at(i), boost::assign::map_list_of("discard", "1") ); } else { // something strange happens - port-to-ground short circuits foreach(int node, nodes) builder.add_dual_component( dualMap_[SchematicScene::ResistorItemType], "", .0, false, node, SchematicScene::Ground, boost::assign::map_list_of("discard", "1") ); } break; } default: break; } }
// API: ParseItem void ParseItem(const char* szItem, StyleItem *item) { parse_item(szItem, item); }