DPtr<uint8_t> *RIFConst::toUTF8String() const throw(BadAllocException) { DPtr<uint8_t> *esclex; try { esclex = RIFConst::escape(this->lex); } JUST_RETHROW(BadAllocException, "(rethrow)") DPtr<uint8_t> *iristr = this->datatype.getUTF8String(); size_t sz = esclex->size() + iristr->size() + 6; DPtr<uint8_t> *utf8str; try { NEW(utf8str, MPtr<uint8_t>, sz); } catch (BadAllocException &e) { esclex->drop(); iristr->drop(); RETHROW(e, "(rethrow)"); } catch (bad_alloc &e) { esclex->drop(); iristr->drop(); THROW(BadAllocException, sizeof(MPtr<uint8_t>)); } uint8_t *utf8p = utf8str->dptr(); *utf8p = to_ascii('"'); memcpy(++utf8p, esclex->dptr(), esclex->size() * sizeof(uint8_t)); utf8p += esclex->size(); ascii_strcpy(utf8p, "\"^^<"); utf8p += 4; memcpy(utf8p, iristr->dptr(), iristr->size() * sizeof(uint8_t)); utf8p += iristr->size(); *utf8p = to_ascii('>'); esclex->drop(); iristr->drop(); return utf8str; }
int irc_nickcmp_ascii(const char *m, const char *n) { while (*m != '\0' && *n != '\0') { if (to_ascii(*m) != to_ascii(*n)) return -1; m++; n++; } return *m == *n ? 0 : 1; }
DPtr<uint8_t> *RIFActVarBind::toUTF8String() const throw(BadAllocException) { DPtr<uint8_t> *str; DPtr<uint8_t> *varstr; try { varstr = this->var.toUTF8String(); } JUST_RETHROW(BadAllocException, "Cannot stringify RIFActVarBind.") size_t len = 3 + varstr->size(); if (this->frame == NULL) { len += 5; try { NEW(str, MPtr<uint8_t>, len); } catch (bad_alloc &e) { varstr->drop(); THROW(BadAllocException, sizeof(MPtr<uint8_t>)); } catch (BadAllocException &e) { varstr->drop(); RETHROW(e, "Cannot allocate space for stringifying RIFActVarBind."); } uint8_t *write_to = str->dptr(); *write_to = to_ascii('('); memcpy(++write_to, varstr->dptr(), varstr->size()); write_to += varstr->size(); varstr->drop(); ascii_strncpy(write_to, " New())", 7); return str; } DPtr<uint8_t> *framestr; try { framestr = this->frame->toUTF8String(); } catch (BadAllocException &e) { varstr->drop(); RETHROW(e, "Cannot stringify RIFActVarBind."); } len += framestr->size(); try { NEW(str, MPtr<uint8_t>, len); } catch (bad_alloc &e) { varstr->drop(); framestr->drop(); THROW(BadAllocException, sizeof(MPtr<uint8_t>)); } catch (BadAllocException &e) { varstr->drop(); framestr->drop(); RETHROW(e, "Cannot allocate space for stringifying RIFActVarBind."); } uint8_t *write_to = str->dptr(); *write_to = to_ascii('('); memcpy(++write_to, varstr->dptr(), varstr->size()); write_to += varstr->size(); varstr->drop(); *write_to = to_ascii(' '); memcpy(++write_to, framestr->dptr(), framestr->size()); write_to += framestr->size(); framestr->drop(); *write_to = to_ascii(')'); return str; }
int main(int argc, char **argv) { pool p[1] = { pool_create() }; random_init(); /* biases at 16 and 32, so we prefix our 30 byte secret with * two bytes to start with, up to 17 */ byteblock prefix = { (uint8_t *) "AAAAAAAAAAAAAAAAAA", 2 }; assert(argc == 2); byteblock secret = from_base64(p, argv[1]); assert(secret.len == SECRET_LEN); unsigned counts[SECRET_LEN][256]; memset(counts, 0, sizeof(counts)); while (prefix.len < 18) { uint8_t buf[64]; byteblock bb = { buf, 0 }; for (size_t i = 0; i < ROUNDS; i++) { oracle(&prefix, &secret, &bb); if (prefix.len <= 15) { /* bias at 16 towards 240 (full) 0 (half) 16 (half) */ uint8_t b16 = buf[15]; counts[15 - prefix.len][b16 ^ 240] += FULL_WEIGHT; counts[15 - prefix.len][b16 ^ 0] += HALF_WEIGHT; counts[15 - prefix.len][b16 ^ 16] += HALF_WEIGHT; } /* bias at 32 towards 224 (full) 0 (half) 32 (half) */ uint8_t b32 = buf[31]; counts[31 - prefix.len][b32 ^ 224] += FULL_WEIGHT; counts[31 - prefix.len][b32 ^ 0] += HALF_WEIGHT; counts[31 - prefix.len][b32 ^ 32] += HALF_WEIGHT; } prefix.len++; byteblock plaintext = recover(p, counts); printf("guess: %s\n", to_ascii(p, &plaintext)); } byteblock recovered = recover(p, counts); printf("message: %s\n", to_ascii(p, &recovered)); p->finish(p); return 0; }
IRIRef *IRIRef::urify() throw(BadAllocException) { if (this->urified) { return this; } const uint8_t *begin = this->utf8str->dptr(); const uint8_t *end = begin + this->utf8str->size(); const uint8_t *mark = begin; const uint8_t *next = NULL; uint32_t codepoint; while (mark != end) { codepoint = utf8char(mark, &next); if (IRIRef::isIPrivate(codepoint) || IRIRef::isUCSChar(codepoint)) { break; } mark = next; } if (mark == end) { this->urified = true; return this; } vector<uint8_t> urivec; urivec.reserve(this->utf8str->size() + 2); do { urivec.insert(urivec.end(), begin, mark); for (; mark != next; ++mark) { urivec.push_back(to_ascii('%')); uint8_t hi4 = (*mark) >> 4; uint8_t lo4 = (*mark) & UINT8_C(0x0F); urivec.push_back(hi4 <= UINT8_C(9) ? to_ascii('0') + hi4 : to_ascii('A') + (hi4 - 10)); urivec.push_back(lo4 <= UINT8_C(9) ? to_ascii('0') + lo4 : to_ascii('A') + (lo4 - 10)); } begin = mark; while (mark != end) { codepoint = utf8char(mark, &next); if (IRIRef::isIPrivate(codepoint) || IRIRef::isUCSChar(codepoint)) { break; } mark = next; } } while(mark != end); urivec.insert(urivec.end(), begin, mark); MPtr<uint8_t> *uristr; try { NEW(uristr, MPtr<uint8_t>, urivec.size()); } RETHROW_BAD_ALLOC copy(urivec.begin(), urivec.end(), uristr->dptr()); this->utf8str->drop(); this->utf8str = uristr; this->urified = true; return this; }
DPtr<uint8_t> *RIFActionBlock::toUTF8String() const throw(BadAllocException) { deque<void*> subs; try { size_t len = 3 + this->actions->size() + this->actvars.size(); ActVarMap::const_iterator it = this->actvars.begin(); for (; it != this->actvars.end(); ++it) { DPtr<uint8_t> *s = it->second.toUTF8String(); subs.push_back((void*)s); len += s->size(); } RIFAction *act = this->actions->dptr(); RIFAction *end = act + this->actions->size(); for (; act != end; ++act) { DPtr<uint8_t> *s = act->toUTF8String(); subs.push_back((void*)s); len += s->size(); } DPtr<uint8_t> *str; try { NEW(str, MPtr<uint8_t>, len); } RETHROW_BAD_ALLOC uint8_t *write_to = str->dptr(); ascii_strncpy(write_to, "Do(", 3); write_to += 3; deque<void*>::iterator pit = subs.begin(); for (; pit != subs.end(); ++pit) { if (pit != subs.begin()) { *write_to = to_ascii(' '); ++write_to; } DPtr<uint8_t> *p = (DPtr<uint8_t>*) (*pit); memcpy(write_to, p->dptr(), p->size()); write_to += p->size(); p->drop(); *pit = NULL; } *write_to = to_ascii(')'); return str; } catch (BadAllocException &e) { deque<void*>::iterator it = subs.begin(); for (; it != subs.end(); ++it) { if (*it != NULL) { ((DPtr<uint8_t>*)(*it))->drop(); } } RETHROW(e, "Unable to stringify action block."); } }
int main() { string_t * strs; string_t sum; int j; make_zero_str(&sum, 0, STR_LENS); to_binary(&sum); strs = buffer_to_strs(strs_buffer, NUM_STRS, STR_LENS); for(j = 0; j < NUM_STRS; j++) { to_binary(&strs[j]); } for(j = 0; j < NUM_STRS; j++) { add_bin_strs(&sum, &strs[j]); } to_ascii(&sum); fwrite(sum.str, 1, sum.size, stdout); printf("\n"); printf("First 10 digits of the sum: "); fwrite(sum.str, 1, 10, stdout); printf("\n"); free(sum.str); for(j = 0; j < NUM_STRS; j++) { free(strs[j].str); } free(strs); return 0; }
static void dump_bytes(const char * buf, size_t size) { size_t i, ascii_cnt = 0; char ascii[17] = { 0, }; for ( i = 0; i < size; i++ ) { if ( i % 16 == 0 ) { if ( i != 0 ) { printf(" |%s|\n", ascii); ascii[0] = 0; ascii_cnt = 0; } printf("%04X:", (unsigned int)i); } printf(" %02X", buf[i] & 0xFF); ascii[ascii_cnt] = to_ascii(buf[i]); ascii[ascii_cnt + 1] = 0; ascii_cnt++; } if ( ascii[0] ) { if ( size % 16 ) for ( i = 0; i < 16 - (size % 16); i++ ) printf(" "); printf(" |%s|\n", ascii); } }
void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const { OutputParams runparams = runparams_in; if (getParam("set_width") == "auto") { docstring widest = nomenclWidest(buffer(), runparams); // Set the label width via nomencl's command \nomlabelwidth. // This must be output before the command \printnomenclature if (!widest.empty()) { os << "\\settowidth{\\nomlabelwidth}{" << widest << "}\n"; } } else if (getParam("set_width") == "custom") { // custom length as optional arg of \printnomenclature string const width = Length(to_ascii(getParam("width"))).asLatexString(); os << '\\' << from_ascii(getCmdName()) << '[' << from_ascii(width) << "]{}"; return; } // output the command \printnomenclature os << getCommand(runparams); }
void InsetCaption::updateBuffer(ParIterator const & it, UpdateType utype) { Buffer const & master = *buffer().masterBuffer(); DocumentClass const & tclass = master.params().documentClass(); string const & lang = it.paragraph().getParLanguage(master.params())->code(); Counters & cnts = tclass.counters(); string const & type = cnts.current_float(); if (utype == OutputUpdate) { // counters are local to the caption cnts.saveLastCounter(); } // Memorize type for addToToc(). floattype_ = type; if (type.empty() || type == "senseless") full_label_ = master.B_("Senseless!!! "); else { // FIXME: life would be _much_ simpler if listings was // listed in Floating. docstring name; if (type == "listing") name = master.B_("Listing"); else name = master.B_(tclass.floats().getType(type).name()); docstring counter = from_utf8(type); is_subfloat_ = cnts.isSubfloat(); if (is_subfloat_) { // only standard captions allowed in subfloats type_ = "Standard"; counter = "sub-" + from_utf8(type); name = bformat(_("Sub-%1$s"), master.B_(tclass.floats().getType(type).name())); } docstring sec; docstring const lstring = getLayout().labelstring(); docstring const labelstring = isAscii(lstring) ? master.B_(to_ascii(lstring)) : lstring; if (cnts.hasCounter(counter)) { // for longtables, we step the counter upstream if (!cnts.isLongtable()) cnts.step(counter, utype); sec = cnts.theCounter(counter, lang); } if (labelstring != master.B_("standard")) { if (!sec.empty()) sec += from_ascii(" "); sec += bformat(from_ascii("(%1$s)"), labelstring); } if (!sec.empty()) full_label_ = bformat(from_ascii("%1$s %2$s: "), name, sec); else full_label_ = bformat(from_ascii("%1$s #: "), name); } // Do the real work now. InsetText::updateBuffer(it, utype); if (utype == OutputUpdate) cnts.restoreLastCounter(); }
void InsetCaption::setCustomLabel(docstring const & label) { if (!isAscii(label) || label.empty()) // This must be a user defined layout. We cannot translate // this, since gettext accepts only ascii keys. custom_label_ = label; else custom_label_ = _(to_ascii(label)); }
int p(vector<docstring> const & arg) { if (arg.size() < 1) { cerr << "lyxclient: The option -p requires 1 argument." << endl; return -1; } serverPid = to_ascii(arg[0]); return 1; }
docstring const translateIfPossible(docstring const & name, std::string const & language) { if (support::isAscii(name) && !name.empty()) // Probably from a standard configuration file, try to // translate return getMessages(language).get(to_ascii(name)); else // This must be from a user defined configuration file. We // cannot translate this, since gettext accepts only ascii // keys. return name; }
void InsetMathDelim::validate(LaTeXFeatures & features) const { InsetMathNest::validate(features); // The delimiters may be used without \left or \right as well. // Therefore they are listed in lib/symbols, and if they have // requirements, we need to add them here. MathWordList const & words = mathedWordList(); MathWordList::const_iterator it = words.find(left_); if (it != words.end()) { docstring const req = it->second.requires; if (!req.empty()) features.require(to_ascii(req)); } it = words.find(right_); if (it != words.end()) { docstring const req = it->second.requires; if (!req.empty()) features.require(to_ascii(req)); } }
/*-----------------------------------------------------------------------*//** *--------------------------------------------------------------------------*/ void dtm_time_from_secs(char * dtm, uint32_t secs) { #define hh dtm[0] #define mm dtm[1] #define ss dtm[2] uint16_t tmp = (uint16_t)(secs%3600L) ; /* fractional hours */ hh = (uint8_t)(secs/3600L); mm = (uint8_t)(tmp/60); ss = (uint8_t)(tmp%60); /* do format conversion */ to_ascii((uint8_t *)(dtm)); } /* end of dtm_time_from_secs() */
/*-----------------------------------------------------------------------*//** *--------------------------------------------------------------------------*/ void dtm_date_from_days(char * dtm, uint16_t days) { static const uint8_t dtm_DaysInMonth[2][12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } ; #define YY dtm[0] #define MM dtm[1] #define DD dtm[2] const uint8_t * mp ; /* get years in complete 4 year cycles */ YY = 4*(days/1461) ; /* get remaining days; if beyond first */ /* (leap) year, adjust years and get */ /* remaining days in year */ if ((days %= 1461) > 365) { YY += (days-1)/365 ; days = (days-1)%365 ; } /* set up days-in-month array pointer, */ /* subtract days in month, incrementing */ /* month, until run out of days */ for ( mp = &dtm_DaysInMonth[(YY%4)?0:1][0], MM = 1 ; days >= *mp ; days -= *mp++, ++MM ) ; /* store complete days */ DD = days+1 ; /* do format conversion */ to_ascii((uint8_t *)(dtm)) ; } /* end of dtm_date_from_days() */
int main(int argc, char **argv) { pool p[1] = { pool_create() }; assert(argc == 2); byteblock cipher = from_base64(p, argv[1]); int best_score = INT_MAX; byteblock best_key; byteblock best_plaintext; // this is actually quick enough and easier to bruteforce: 40 * 256 * 256 tries for (size_t k = 2; k < MAX_KEYLEN; k++) { byteblock key = { p->alloc(p, k), k }; for (size_t i = 0; i < key.len; i++) { key.buf[i] = find_key_byte(p, &cipher, i, key.len); } byteblock plain = byteblock_xor(p, &cipher, &key); int score = score_english(&plain); if (score < best_score) { best_score = score; best_key = key; best_plaintext = plain; } } printf("key: %s, plain: %s\n", to_hex(p, &best_key), to_ascii(p, &best_plaintext)); p->finish(p); return 0; }
DPtr<uint8_t> *IRIRef::getPart(const enum IRIRefPart part) const throw() { const uint8_t *begin = this->utf8str->dptr(); const uint8_t *end = begin + this->utf8str->size(); const uint8_t *offset = begin; const uint8_t *mark = begin; DPtr<uint8_t> *iripart = NULL; // SCHEME for (; mark != end && *mark != to_ascii(':'); ++mark) { // loop does the work } if (mark == end) { if (part == SCHEME) { return NULL; } // offset unchanged } else { iripart = this->utf8str->sub(0, mark - offset); UTF8Iter begin (iripart); UTF8Iter end (iripart); end.finish(); if (isScheme(begin, end)) { if (part == SCHEME) { return iripart; } offset = mark + 1; } else { if (part == SCHEME) { iripart->drop(); return NULL; } // offset unchanged } iripart->drop(); } // These parts exist only if hierarchy starts with //. if (part == USER_INFO || part == HOST || part == PORT) { if (offset == end || *offset != to_ascii('/') || ++offset == end || *offset != to_ascii('/')) { return NULL; } ++offset; // USER_INFO for (mark = offset; mark != end && *mark != to_ascii('@') && *mark != to_ascii('/'); ++mark) { // loop does the work } if (mark == end || *mark == to_ascii('/')) { if (part == USER_INFO) { return NULL; } // offset unchanged } else { if (part == USER_INFO) { return this->utf8str->sub(offset - begin, mark - offset); } offset = mark + 1; } // HOST if (offset != end && *offset == to_ascii('[')) { for (mark = offset; mark != end && *mark != to_ascii(']'); ++mark) { // loop does the work } ++mark; } else { for (mark = offset; mark != end && *mark != to_ascii(':') && *mark != to_ascii('/') && *mark != to_ascii('?') && *mark != to_ascii('#'); ++mark) { // loop does the work } } if (part == HOST) { return this->utf8str->sub(offset - begin, mark - offset); } offset = mark; // if a colon was found, include it to help distinguish // between no port and empty port // PORT // No need to check part == PORT. It is definitely PORT // by process of elimination. if (offset == end || *offset != to_ascii(':')) { return NULL; } if (*offset == to_ascii(':')) { offset++; for (mark = offset; mark != end && *mark != to_ascii('/') && *mark != to_ascii('?') && *mark != to_ascii('#'); ++mark) { // loop does the work } return this->utf8str->sub(offset - begin, mark - offset); } } // Skip over //authority if necessary. if (end - offset >= 2 && *offset == to_ascii('/') && *(offset + 1) == to_ascii('/')) { for (offset += 2; offset != end && *offset != to_ascii('/') && *offset != to_ascii('?') && *offset != to_ascii('#'); ++offset) { // loop does the work } } // PATH for (mark = offset; mark != end && *mark != to_ascii('?') && *mark != to_ascii('#'); ++mark) { // loop does the work } if (part == PATH) { return this->utf8str->sub(offset - begin, mark - offset); } offset = mark; // leave ? or # for reference // QUERY if (offset == end) { return NULL; } if (*offset == to_ascii('?')) { ++offset; for (mark = offset; mark != end && *mark != to_ascii('#'); ++mark) { // loop does the work } if (part == QUERY) { return this->utf8str->sub(offset - begin, mark - offset); } offset = mark; // leave # for reference } else { if (part == QUERY) { return NULL; } } // FRAGMENT if (offset == end || *offset != to_ascii('#')) { return NULL; } ++offset; return this->utf8str->sub(offset - begin, end - offset); }
IRIRef *IRIRef::normalize() THROWS(BadAllocException, TraceableException) { if (this->normalized && !this->urified) { return this; } uint8_t *normed = NULL; DPtr<uint8_t> *normal = NULL; if (this->utf8str->standable()) { this->utf8str = this->utf8str->stand(); normal = this->utf8str; normal->hold(); } else { NEW(normal, MPtr<uint8_t>, this->utf8str->size()); } normed = normal->dptr(); // Percent encoding; decode if IUnreserved uint8_t bits = UINT8_C(0); uint8_t *begin = this->utf8str->dptr(); uint8_t *end = begin + this->utf8str->size(); uint8_t *marki = begin; uint8_t *markj = begin; uint8_t *markk = normed; for (; marki != end; marki = markj) { for (markj = marki; markj != end && *markj != to_ascii('%'); ++markj) { // loop does the work } size_t sz = markj - marki; memmove(markk, marki, sz * sizeof(uint8_t)); markk += sz; if (markj == end) { break; } uint8_t enc[4] = { UINT8_C(0xFF), UINT8_C(0xFF), UINT8_C(0xFF), UINT8_C(0xFF) }; enc[0] = (((uint8_t) IRI_HEX_VALUE(markj[1])) << 4) | (uint8_t) IRI_HEX_VALUE(markj[2]); if (enc[0] <= UINT8_C(0x7F)) { if (IRIRef::isIUnreserved(enc[0])) { *markk = enc[0]; ++markk; } else { markk[0] = markj[0]; markk[1] = to_upper(markj[1]); markk[2] = to_upper(markj[2]); markk += 3; } markj += 3; continue; } uint8_t *markl = markj + 3; int i = 1; uint8_t bits; for (bits = enc[0] << 1; markl != end && *markl == to_ascii('%') && bits > UINT8_C(0x7F) && i < 4; bits <<= 1) { enc[i] = (((uint8_t) IRI_HEX_VALUE(markl[1])) << 4) | (uint8_t) IRI_HEX_VALUE(markl[2]); markl += 3; ++i; } try { const uint8_t *endenc; uint32_t codepoint = utf8char(enc, &endenc); if (IRIRef::isIUnreserved(codepoint)) { i = endenc - enc; // should already be equal, but just in case memmove(markk, enc, i * sizeof(uint8_t)); markk += i; markj = markl; this->urified = false; } else { for (; markj != markl; markj += 3) { markk[0] = markj[0]; markk[1] = to_upper(markj[1]); markk[2] = to_upper(markj[2]); markk += 3; } } } catch (InvalidEncodingException &e) { markk[0] = markj[0]; markk[1] = to_upper(markj[1]); markk[2] = to_upper(markj[2]); markk += 3; markj += 3; } } if (markk != end) { DPtr<uint8_t> *temp = normal->sub(0, markk - normed); normal->drop(); normal = temp; } if (this->normalized) { this->utf8str->drop(); this->utf8str = normal; return this; } // Character normalization; NFC DPtr<uint32_t> *codepoints = utf8dec(normal); normal->drop(); DPtr<uint32_t> *codepoints2 = nfc_opt(codepoints); codepoints->drop(); normal = utf8enc(codepoints2); codepoints2->drop(); this->utf8str->drop(); this->utf8str = normal; // Path normalization this->resolve(NULL); // Case normalization; scheme and host DPtr<uint8_t> *part = this->getPart(SCHEME); if (part != NULL) { begin = part->dptr(); end = begin + part->size(); for (; begin != end; ++begin) { *begin = to_lower(*begin); } part->drop(); } part = this->getPart(HOST); if (part != NULL) { begin = part->dptr(); end = begin + part->size(); for (; begin != end; ++begin) { *begin = to_lower(*begin); } part->drop(); } this->normalized = true; return this; }
RIFActVarBind RIFActVarBind::parse(DPtr<uint8_t> *utf8str) throw(BadAllocException, SizeUnknownException, InvalidCodepointException, InvalidEncodingException, MalformedIRIRefException, TraceableException) { if (utf8str == NULL) { THROW(TraceableException, "utf8str must not be NULL."); } if (!utf8str->sizeKnown()) { THROWX(SizeUnknownException); } const uint8_t *begin = utf8str->dptr(); const uint8_t *end = begin + utf8str->size(); const uint8_t *left_bound = begin; const uint8_t *right_bound = end; for (; left_bound != end && is_space(*left_bound); ++left_bound) { // find first non-space character } if (left_bound == end || *left_bound != to_ascii('(')) { THROW(TraceableException, "Could not find opening parenthesis."); } for (--right_bound; right_bound != left_bound && is_space(*right_bound); --right_bound) { // find last non-space character } if (right_bound == left_bound || *right_bound != to_ascii(')')) { THROW(TraceableException, "Ends without closing parenthesis."); } for (++left_bound; left_bound != right_bound && is_space(*left_bound); ++left_bound) { // find beginning of action variable } if (left_bound == right_bound || *left_bound != to_ascii('?')) { THROW(TraceableException, "Unable to find action variable."); } for (--right_bound; right_bound != left_bound && is_space(*right_bound); --right_bound) { // find end of binding value } if (right_bound == left_bound) { THROW(TraceableException, "Unable to find binding value."); } ++right_bound; const uint8_t *var = left_bound; while (var != right_bound) { for (++var; var != right_bound && !is_space(*var); ++var) { // find end of variable... maybe } DPtr<uint8_t> *varstr = utf8str->sub(left_bound - begin, var - left_bound); RIFVar v; try { v = RIFVar::parse(varstr); varstr->drop(); } catch (BadAllocException &e) { varstr->drop(); RETHROW(e, "Unable to parse action variable."); } catch (InvalidCodepointException &e) { varstr->drop(); RETHROW(e, "Unable to parse action variable."); } catch (InvalidEncodingException &e) { varstr->drop(); RETHROW(e, "Unable to parse action variable."); } catch (TraceableException &e) { varstr->drop(); continue; } const uint8_t *bindval = var; for (; bindval != right_bound && is_space(*bindval); ++bindval) { // find beginning of binding value } if (bindval == right_bound) { THROW(TraceableException, "Unable to find binding value."); } if (right_bound - bindval == 5 && ascii_strncmp(bindval, "New()", 5) == 0) { return RIFActVarBind(v); } DPtr<uint8_t> *framestr = utf8str->sub(bindval - begin, right_bound - bindval); try { RIFAtomic frame = RIFAtomic::parse(framestr); framestr->drop(); framestr = NULL; return RIFActVarBind(v, frame); } catch (BadAllocException &e) { if (framestr != NULL) { framestr->drop(); } RETHROW(e, "Unable to parse binding value."); } catch (InvalidCodepointException &e) { framestr->drop(); RETHROW(e, "Unable to parse binding value."); } catch (InvalidEncodingException &e) { framestr->drop(); RETHROW(e, "Unable to parse binding value."); } catch (MalformedIRIRefException &e) { framestr->drop(); RETHROW(e, "Unable to parse binding value."); } catch (TraceableException &e) { if (framestr != NULL) { framestr->drop(); } RETHROW(e, "Unable to parse binding value."); } } THROW(TraceableException, "Unable to parse action variable binding."); }
RIFActionBlock RIFActionBlock::parse(DPtr<uint8_t> *utf8str) throw(BadAllocException, SizeUnknownException, InvalidCodepointException, InvalidEncodingException, MalformedIRIRefException, TraceableException) { if (utf8str == NULL) { THROW(TraceableException, "utf8str must not be NULL."); } if (!utf8str->sizeKnown()) { THROWX(SizeUnknownException); } const uint8_t *begin = utf8str->dptr(); const uint8_t *end = begin + utf8str->size(); const uint8_t *mark = begin; for (; mark != end && is_space(*mark); ++mark) { // find beginning of action block } if (end - mark < 4) { THROW(TraceableException, "Could not find action block."); } if (ascii_strncmp(mark, "Do", 2) != 0) { THROW(TraceableException, "Action block must start with \"Do\"."); } mark += 2; for (; mark != end && is_space(*mark); ++mark) { // search for left parenthesis } if (mark == end || *mark != to_ascii('(')) { THROW(TraceableException, "Could not find opening parenthesis."); } for (--end; end != mark && is_space(*end); --end) { // find right parenthesis } if (end == mark || *end != to_ascii(')')) { THROW(TraceableException, "Could not find closing parenthesis."); } for (--end; end != mark && is_space(*end); --end) { // find end of last action } ++end; for (++mark; mark != end && is_space(*mark); ++mark) { // find first binding or action } if (mark == end) { THROW(TraceableException, "Action block is (illegaly) empty."); } deque<RIFAction> actions; ActVarMap bindings(RIFVar::cmplt0); bool find_bindings = (*mark == to_ascii('(')); const uint8_t *bound = mark; while (bound != end) { for (; bound != end && *bound != to_ascii(')'); ++bound) { // find end of action block binding or action... maybe } if (bound != end) { ++bound; } DPtr<uint8_t> *str = utf8str->sub(mark - begin, bound - mark); if (find_bindings) { try { RIFActVarBind bind = RIFActVarBind::parse(str); str->drop(); str = NULL; pair<ActVarMap::iterator, bool> p = bindings.insert( pair<RIFVar, RIFActVarBind>(bind.getActVar(), bind)); if (!p.second) { THROW(TraceableException, "Cannot have multiple bindings for same action variable."); } } catch (BadAllocException &e) { str->drop(); RETHROW(e, "Cannot parse action variable binding."); } catch (InvalidCodepointException &e) { str->drop(); RETHROW(e, "Cannot parse action variable binding."); } catch (InvalidEncodingException &e) { str->drop(); RETHROW(e, "Cannot parse action variable binding."); } catch (MalformedIRIRefException &e) { str->drop(); continue; } catch (TraceableException &e) { if (str == NULL) { RETHROW(e, "Malformed action block."); } str->drop(); continue; } } else { try { RIFAction act = RIFAction::parse(str); str->drop(); actions.push_back(act); } catch (BadAllocException &e) { str->drop(); RETHROW(e, "Cannot parse action."); } catch (InvalidCodepointException &e) { str->drop(); RETHROW(e, "Cannot parse action."); } catch (InvalidEncodingException &e) { str->drop(); RETHROW(e, "Cannot parse action."); } catch (MalformedIRIRefException &e) { str->drop(); continue; } catch (TraceableException &e) { str->drop(); continue; } } for (mark = bound; mark != end && is_space(*mark); ++mark) { // find beginning of next action block binding or action... maybe } bound = mark; find_bindings = find_bindings && (*mark == to_ascii('(')); } DPtr<RIFAction> *pact; try { NEW(pact, APtr<RIFAction>, actions.size()); } RETHROW_BAD_ALLOC copy(actions.begin(), actions.end(), pact->dptr()); try { RIFActionBlock block(pact, bindings); pact->drop(); return block; } catch (TraceableException &e) { pact->drop(); RETHROW(e, "Cannot parse action block."); } }
string time::to_string (string const & format) const { string r; string::const_iterator p = format.cbegin(); string::const_iterator end = format.cend(); bool need_spec = false; // true if conversion specifier character expected while (p < end) { if (*p == '%') { if (need_spec) { r.push_back('%'); need_spec = false; } else { need_spec = true; } } else { if (!need_spec) { r.push_back(*p); } else { switch (to_ascii(*p)) { case 'n': r.push_back('\n'); break; case 't': r.push_back('\t'); break; case 'H': append_prefixed2(r, '0', hour()); break; case 'I': append_prefixed2(r, '0', hour() % 12); break; case 'k': append_prefixed2(r, ' ', hour()); break; case 'l': append_prefixed2(r, ' ', hour() % 12); break; case 'M': append_prefixed2(r, '0', minute()); break; case 'q': r.append(pfs::to_string(millis())); break; case 'Q': append_prefixed3(r, '0', millis()); break; case 'S': append_prefixed2(r, '0', second()); break; case 'R': append_prefixed2(r, '0', hour()); r.push_back(':'); append_prefixed2(r, '0', minute()); break; case 'T': append_prefixed2(r, '0', hour()); r.push_back(':'); append_prefixed2(r, '0', minute()); r.push_back(':'); append_prefixed2(r, '0', second()); break; case 'J': append_prefixed2(r, '0', hour()); r.push_back(':'); append_prefixed2(r, '0', minute()); r.push_back(':'); append_prefixed2(r, '0', second()); r.push_back('.'); append_prefixed3(r, '0', millis()); break; case 'p': r.append(hour() < 12 ? string("AM") : string("PM")); break; default: r.push_back('%'); r.push_back(*p); break; } need_spec = false; } } ++p; } return r; }
RIFAction RIFAction::parse(DPtr<uint8_t> *utf8str) throw(BadAllocException, SizeUnknownException, InvalidCodepointException, InvalidEncodingException, MalformedIRIRefException, TraceableException) { if (utf8str == NULL) { THROW(TraceableException, "utf8str must not be NULL."); } if (!utf8str->sizeKnown()) { THROWX(SizeUnknownException); } const uint8_t *begin = utf8str->dptr(); const uint8_t *end = begin + utf8str->size(); const uint8_t *beginkw = begin; for (; beginkw != end && is_space(*beginkw); ++beginkw) { // find beginning of keyword } if (beginkw == end) { THROW(TraceableException, "No keyword found when parsing RIFAction."); } const uint8_t *endkw = beginkw; for (; endkw != end && is_alnum(*endkw); ++endkw) { // find end of keyword } enum RIFActType type; if (endkw - beginkw == 6) { if (ascii_strncmp(beginkw, "Assert", 6) == 0) { type = ASSERT_FACT; } else if (ascii_strncmp(beginkw, "Modify", 6) == 0) { type = MODIFY; } else { THROW(TraceableException, "Unrecognized RIFAction type."); } } else if (endkw - beginkw == 7) { if (ascii_strncmp(beginkw, "Retract", 7) == 0) { type = RETRACT_FACT; // validate later } else if (ascii_strncmp(beginkw, "Execute", 7) == 0) { type = EXECUTE; } else { THROW(TraceableException, "Unrecognized RIFAction type."); } } else { THROW(TraceableException, "Unrecognized RIFAction type."); } const uint8_t *left_bound = endkw; for (; left_bound != end && *left_bound != to_ascii('('); ++left_bound) { // find left paren enclosing target } if (left_bound == end) { THROW(TraceableException, "Could not find left paren when parsing RIFAction."); } const uint8_t *right_bound = end; for (--right_bound; right_bound != left_bound && *right_bound != to_ascii(')'); --right_bound) { // find right paren enclosing target } if (right_bound == left_bound) { THROW(TraceableException, "Could not find right paren when parsing RIFAction."); } for (++left_bound; left_bound != right_bound && is_space(*left_bound); ++left_bound) { // find left bound of target } if (left_bound == right_bound) { if (type == EXECUTE) { return RIFAction(); } THROW(TraceableException, "No target specified for RIFAction."); } for (--right_bound; right_bound != left_bound && is_space(*right_bound); --right_bound) { // find right bound of target } ++right_bound; DPtr<uint8_t> *targetstr = utf8str->sub(left_bound - begin, right_bound - left_bound); try { RIFAtomic atom = RIFAtomic::parse(targetstr); RIFAction act (type, atom); targetstr->drop(); return act; } catch (BadAllocException &e) { targetstr->drop(); RETHROW(e, "Unable to parse target."); } catch (InvalidCodepointException &e) { targetstr->drop(); RETHROW(e, "Unable to parse target."); } catch (InvalidEncodingException &e) { targetstr->drop(); RETHROW(e, "Unable to parse target."); } catch (MalformedIRIRefException &e) { targetstr->drop(); RETHROW(e, "Unable to parse target."); } catch (BaseException<enum RIFActType> &e) { targetstr->drop(); RETHROW(e, "Unable to parse target."); } catch (TraceableException &e) { if (type != RETRACT_FACT) { targetstr->drop(); RETHROW(e, "Unable to parse target."); } } try { RIFTerm term = RIFTerm::parse(targetstr); RIFAction act (term); targetstr->drop(); return act; } catch (BadAllocException &e) { targetstr->drop(); RETHROW(e, "Unable to parse target."); } catch (InvalidCodepointException &e) { targetstr->drop(); RETHROW(e, "Unable to parse target."); } catch (InvalidEncodingException &e) { targetstr->drop(); RETHROW(e, "Unable to parse target."); } catch (MalformedIRIRefException &e) { targetstr->drop(); RETHROW(e, "Unable to parse target."); } catch (TraceableException &e) { // ignore and check if RETRACT_SLOTS } RIFTerm obj, attr; begin = targetstr->dptr(); end = begin + targetstr->size(); while (begin != end) { for (; begin != end && is_space(*begin); ++begin) { // creep forward to first non-space } for (++begin; begin != end && !is_space(*begin); ++begin) { // search for space between terms } if (begin == end) { targetstr->drop(); THROW(TraceableException, "Invalid RETRACT target."); } DPtr<uint8_t> *str = targetstr->sub(0, begin - targetstr->dptr()); try { obj = RIFTerm::parse(str); str->drop(); } catch (TraceableException &e) { str->drop(); continue; } for (++begin; begin != end && is_space(*begin); ++begin) { // search for second term } str = targetstr->sub(begin - targetstr->dptr(), end - begin); try { attr = RIFTerm::parse(str); str->drop(); } catch (TraceableException &e) { str->drop(); continue; } try { targetstr->drop(); return RIFAction(obj, attr); } RETHROW_BAD_ALLOC } targetstr->drop(); THROW(TraceableException, "Invalid RETRACT target."); }