Exemplo n.º 1
0
		boost::tuple<json_token, node> get_next_token()
		{
			if(pushed_back_tokens.empty() == false) {
				std::pair<json_token, node> pr = pushed_back_tokens.front();
				pushed_back_tokens.pop_front();
				return boost::make_tuple(pr.first, pr.second);
			}

			bool running = true;
			bool in_string = false;
			std::string::iterator start_it = it_;
			std::string new_string;
			while(running) {
				if(it_ == lex_str_.end()) {
					if(in_string) {
						throw parse_error(formatter() << "End of data inside string");
					}
					return boost::make_tuple(DOCUMENT_END, node());
				}
				if(in_string) {
					if(*it_ == '"') {
						++it_;
						node string_node(new_string);
						//if(new_string == "true") {
						//	return boost::make_tuple(LIT_TRUE, node::from_bool(true));
						//} else if(new_string == "false") {
						//	return boost::make_tuple(LIT_FALSE, node::from_bool(false));
						//} else if(new_string == "null") {
						//	return boost::make_tuple(LIT_NULL, node());
						//}
						return boost::make_tuple(STRING_LITERAL, string_node);
					} else if(*it_ == '\\') {
						++it_;
						if(it_ == lex_str_.end()) {
							throw parse_error(formatter() << "End of data in quoted token");
						}
						if(*it_ == '"') {
							new_string += '"';
							++it_;
						} else if(*it_ == '\\') {
							new_string += '\\';
							++it_;
						} else if(*it_ == '/') {
							new_string += '/';
							++it_;
						} else if(*it_ == 'b') {
							new_string += '\b';
							++it_;
						} else if(*it_ == 'f') {
							new_string += '\f';
							++it_;
						} else if(*it_ == 'n') {
							new_string += '\n';
							++it_;
						} else if(*it_ == 'r') {
							new_string += '\r';
							++it_;
						} else if(*it_ == 't') {
							new_string += '\t';
							++it_;
						} else if(*it_ == 'u') {
							++it_;
							if((lex_str_.end() - it_) >= 4) {
								uint16_t value = 0;
								for(int n = 0; n != 4; ++n) {
									value = (value << 4) | decode_hex_nibble(*it_++);
								}
								// Quick and dirty conversion from \uXXXX -> UTF-8
								if(value >= 0 && value <= 127U) {
									new_string += char(value);
								} else if(value >= 128U && value <= 2047U) {
									new_string += char(0xc0 | (value >> 6));
									new_string += char(0x80 | (value & 0x3f));
								} else if(value >= 2048U) {
									new_string += char(0xe0 | (value >> 12));
									new_string += char(0x80 | ((value >> 6) & 0x3f));
									new_string += char(0x80 | (value & 0x3f));
								}
Exemplo n.º 2
0
void decode_hex_line(const char *line, int (*putchar_like_callback)(int))
{
	const unsigned char *p;

	for (p = skip_hd_header((unsigned char *)line); p && *p; p++) {
		if (*p == '|' || *p == ';' || *p == '\n' || *p == '\r') break;

		if (isspace(*p)) continue;
		if (isxdigit(*p)) {
			int h1, h2;

			h1 = decode_hex_nibble(*p++);

			if (!isxdigit(*p)) {
				fprintf(stderr, "Premature hex-nibble is found.\n");
				CONTINUE_OR_BREAK_ON_INVALID_CHAR;
			}
			h2 = decode_hex_nibble(*p);

			putchar(h1 * 16 + h2);

			continue;
		}

#ifdef SUPPORT_STRING_LITERAL_INJECTION
		if (*p == '"') {
			int escape = 0;
			int ch;

			while ((ch = *++p) && ch != '\r' && ch != '\n') {
				if (escape) {
					switch (ch) {
					case 'n': ch = '\n'; break;
					case 'r': ch = '\r'; break;
					case 't': ch = '\t'; break;
					case 'v': ch = '\v'; break;
					case 'a': ch = '\a'; break;
					case 'b': ch = '\b'; break;
					case 'f': ch = '\f'; break;
					case '"': ch = '"';  break;
					case '\\': ch = '\\';  break;
					default:
						fprintf(stderr, "Unknown escape sequence.\n");
						break;
					}
					escape = 0;
				} else {
					switch (ch) {
					case '\\':
						escape = 1;
						break;
					case '"':
						goto out;
					default:
						break;
					}
				}
				if (escape) continue;

				putchar(ch);
			}

			fprintf(stderr, "Premature end of string literal is found.\n");
			break;

		out:
			continue;
		}
#endif

		fprintf(stderr, "Invalid character 0x%02x is found.\n", *p);
		CONTINUE_OR_BREAK_ON_INVALID_CHAR;
	}
}