123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734 |
- /**
- * @file llmessagetemplateparser.cpp
- * @brief LLMessageTemplateParser implementation
- *
- * $LicenseInfo:firstyear=2007&license=viewergpl$
- *
- * Copyright (c) 2007-2009, Linden Research, Inc.
- *
- * Second Life Viewer Source Code
- * The source code in this file ("Source Code") is provided by Linden Lab
- * to you under the terms of the GNU General Public License, version 2.0
- * ("GPL"), unless you have obtained a separate licensing agreement
- * ("Other License"), formally executed by you and Linden Lab. Terms of
- * the GPL can be found in doc/GPL-license.txt in this distribution, or
- * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
- *
- * There are special exceptions to the terms and conditions of the GPL as
- * it is applied to this Source Code. View the full text of the exception
- * in the file doc/FLOSS-exception.txt in this software distribution, or
- * online at
- * http://secondlifegrid.net/programs/open_source/licensing/flossexception
- *
- * By copying, modifying or distributing this software, you acknowledge
- * that you have read and understood your obligations described above,
- * and agree to abide by those obligations.
- *
- * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
- * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
- * COMPLETENESS OR PERFORMANCE.
- * $/LicenseInfo$
- */
- #include "linden_common.h"
- #include "boost/tokenizer.hpp"
- #include "llmessagetemplateparser.h"
- // What follows is a bunch of C functions to do validation.
- // Lets support a small subset of regular expressions here
- // Syntax is a string made up of:
- // a - checks against alphanumeric ([A-Za-z0-9])
- // c - checks against character ([A-Za-z])
- // f - checks against first variable character ([A-Za-z_])
- // v - checks against variable ([A-Za-z0-9_])
- // s - checks against sign of integer ([-0-9])
- // d - checks against integer digit ([0-9])
- // * - repeat last check
- // Checks 'a'
- bool b_return_alphanumeric_ok(char c)
- {
- return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
- (c >= '0' && c <= '9');
- }
- // Checks 'c'
- bool b_return_character_ok(char c)
- {
- return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
- }
- // Checks 'f'
- bool b_return_first_variable_ok(char c)
- {
- return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_';
- }
- // Checks 'v'
- bool b_return_variable_ok(char c)
- {
- return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
- (c >= '0' && c <= '9') || c == '_';
- }
- // Checks 's'
- bool b_return_signed_integer_ok(char c)
- {
- return (c >= '0' && c <= '9') || c == '-';
- }
- // Checks 'd'
- bool b_return_integer_ok(char c)
- {
- return c >= '0' && c <= '9';
- }
- bool (*gParseCheckCharacters[])(char c) =
- {
- b_return_alphanumeric_ok,
- b_return_character_ok,
- b_return_first_variable_ok,
- b_return_variable_ok,
- b_return_signed_integer_ok,
- b_return_integer_ok
- };
- S32 get_checker_number(char checker)
- {
- switch (checker)
- {
- case 'a':
- return 0;
- case 'c':
- return 1;
- case 'f':
- return 2;
- case 'v':
- return 3;
- case 's':
- return 4;
- case 'd':
- return 5;
- case '*':
- return 9999;
- default:
- return -1;
- }
- }
- // Checks token based on passed simplified regular expression
- bool b_check_token(const char* token, const char* regexp)
- {
- S32 tptr, rptr = 0;
- S32 current_checker, next_checker = 0;
- current_checker = get_checker_number(regexp[rptr++]);
- if (current_checker == -1)
- {
- llerrs << "Invalid regular expression value!" << llendl;
- return false;
- }
- if (current_checker == 9999)
- {
- llerrs << "Regular expression can't start with *!" << llendl;
- return false;
- }
- for (tptr = 0; token[tptr]; ++tptr)
- {
- if (current_checker == -1)
- {
- llerrs << "Input exceeds regular expression!\nDid you forget a *?"
- << llendl;
- return false;
- }
- if (!gParseCheckCharacters[current_checker](token[tptr]))
- {
- return false;
- }
- if (next_checker != 9999)
- {
- next_checker = get_checker_number(regexp[rptr++]);
- if (next_checker != 9999)
- {
- current_checker = next_checker;
- }
- }
- }
- return true;
- }
- // C variable can be made up of upper or lower case letters, underscores, or
- // numbers, but can't start with a number
- bool b_variable_ok(const char* token)
- {
- if (b_check_token(token, "fv*"))
- {
- return true;
- }
- llwarns << "Token '" << token << "' is not a variable !" << llendl;
- return false;
- }
- // An integer is made up of the digits 0-9 and may be preceded by a '-'
- bool b_integer_ok(const char* token)
- {
- if (b_check_token(token, "sd*"))
- {
- return true;
- }
- llwarns << "Token is not an integer !" << llendl;
- return false;
- }
- // An integer is made up of the digits 0-9
- bool b_positive_integer_ok(const char* token)
- {
- if (b_check_token(token, "d*"))
- {
- return true;
- }
- llwarns << "Token is not an integer !" << llendl;
- return false;
- }
- // Done with C functions, here's the tokenizer.
- typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
- LLTemplateTokenizer::LLTemplateTokenizer(const std::string& contents)
- : mStarted(false),
- mTokens()
- {
- boost::char_separator<char> newline("\r\n", "", boost::keep_empty_tokens);
- boost::char_separator<char> spaces(" \t");
- U32 line_counter = 1;
-
- tokenizer line_tokens(contents, newline);
- for (tokenizer::iterator line_iter = line_tokens.begin();
- line_iter != line_tokens.end(); ++line_iter, ++line_counter)
- {
- tokenizer word_tokens(*line_iter, spaces);
- for (tokenizer::iterator word_iter = word_tokens.begin();
- word_iter != word_tokens.end(); ++word_iter)
- {
- if ((*word_iter)[0] == '/')
- {
- break; // skip to end of line on comments
- }
- positioned_token pt;// = new positioned_token();
- pt.str = std::string(*word_iter);
- pt.line = line_counter;
- mTokens.push_back(pt);
- }
- }
- mCurrent = mTokens.begin();
- }
- void LLTemplateTokenizer::inc()
- {
- if (atEOF())
- {
- error("trying to increment token of EOF");
- }
- else if (mStarted)
- {
- ++mCurrent;
- }
- else
- {
- mStarted = true;
- mCurrent = mTokens.begin();
- }
- }
- void LLTemplateTokenizer::dec()
- {
- if (mCurrent == mTokens.begin())
- {
- if (mStarted)
- {
- mStarted = false;
- }
- else
- {
- error("trying to decrement past beginning of file");
- }
- }
- else
- {
- --mCurrent;
- }
- }
- std::string LLTemplateTokenizer::get() const
- {
- if (atEOF())
- {
- error("trying to get EOF");
- }
- return mCurrent->str;
- }
- U32 LLTemplateTokenizer::line() const
- {
- return atEOF() ? 0 : mCurrent->line;
- }
- bool LLTemplateTokenizer::atEOF() const
- {
- return mCurrent == mTokens.end();
- }
- std::string LLTemplateTokenizer::next()
- {
- inc();
- return get();
- }
- bool LLTemplateTokenizer::want(const std::string& token)
- {
- if (atEOF())
- {
- return false;
- }
- inc();
- if (atEOF())
- {
- return false;
- }
- if (get() != token)
- {
- dec(); // back up a step
- return false;
- }
- return true;
- }
- bool LLTemplateTokenizer::wantEOF()
- {
- // See if the next token is EOF
- if (atEOF())
- {
- return true;
- }
- inc();
- if (!atEOF())
- {
- dec(); // Back up a step
- return false;
- }
- return true;
- }
- void LLTemplateTokenizer::error(std::string message) const
- {
- if (atEOF())
- {
- llerrs << "Unexpected end of file: " << message << llendl;
- }
- else
- {
- llerrs << "Problem parsing message template at line " << line()
- << ", with token '" << get() << "' : " << message << llendl;
- }
- }
- // Done with tokenizer, next is the parser.
- LLTemplateParser::LLTemplateParser(LLTemplateTokenizer& tokens)
- : mVersion(0.f),
- mMessages()
- {
- // The version number should be the first thing in the file
- if (tokens.want("version"))
- {
- // version number
- std::string vers_string = tokens.next();
- mVersion = (F32)atof(vers_string.c_str());
-
- llinfos << "### Message template version " << mVersion << " ###"
- << llendl;
- }
- else
- {
- llerrs << "Version must be first in the message template, found "
- << tokens.next() << llendl;
- }
- while (LLMessageTemplate* templatep = parseMessage(tokens))
- {
- if (templatep->getDeprecation() != MD_DEPRECATED)
- {
- mMessages.push_back(templatep);
- }
- else
- {
- delete templatep;
- }
- }
- if (!tokens.wantEOF())
- {
- llerrs << "Expected end of template or a message, instead found: "
- << tokens.next() << " at " << tokens.line() << llendl;
- }
- }
- F32 LLTemplateParser::getVersion() const
- {
- return mVersion;
- }
- LLTemplateParser::message_iterator LLTemplateParser::getMessagesBegin() const
- {
- return mMessages.begin();
- }
- LLTemplateParser::message_iterator LLTemplateParser::getMessagesEnd() const
- {
- return mMessages.end();
- }
- // static
- LLMessageTemplate* LLTemplateParser::parseMessage(LLTemplateTokenizer& tokens)
- {
- if (!tokens.want("{"))
- {
- return NULL;
- }
- // Name first
- std::string template_name = tokens.next();
- // Is name a legit C variable name ?
- if (!b_variable_ok(template_name.c_str()))
- {
- llerrs << "Not legit variable name: " << template_name << " at "
- << tokens.line() << llendl;
- }
- LLMessageTemplate* templatep = NULL;
- // OK, now get Frequency ("High", "Medium", or "Low")
- EMsgFrequency frequency = MFT_LOW;
- std::string freq_string = tokens.next();
- if (freq_string == "High")
- {
- frequency = MFT_HIGH;
- }
- else if (freq_string == "Medium")
- {
- frequency = MFT_MEDIUM;
- }
- else if (freq_string == "Low" || freq_string == "Fixed")
- {
- frequency = MFT_LOW;
- }
- else
- {
- llerrs << "Expected frequency, got " << freq_string << " at "
- << tokens.line() << llendl;
- }
- // *TODO: more explicit checking here please
- U32 message_number = strtoul(tokens.next().c_str(), NULL, 0);
- switch (frequency)
- {
- case MFT_HIGH:
- break;
- case MFT_MEDIUM:
- message_number = (255 << 8) | message_number;
- break;
- case MFT_LOW:
- message_number = (255 << 24) | (255 << 16) | message_number;
- break;
- default:
- llerrs << "Unknown frequency enum: " << frequency << llendl;
- }
-
- templatep = new LLMessageTemplate(template_name.c_str(), message_number,
- frequency);
-
- // Now get trust ("Trusted", "NotTrusted")
- std::string trust = tokens.next();
- if (trust == "Trusted")
- {
- templatep->setTrust(MT_TRUST);
- }
- else if (trust == "NotTrusted")
- {
- templatep->setTrust(MT_NOTRUST);
- }
- else
- {
- llerrs << "Bad trust " << trust << " at " << tokens.line() << llendl;
- }
-
- // Get encoding
- std::string encoding = tokens.next();
- if (encoding == "Unencoded")
- {
- templatep->setEncoding(ME_UNENCODED);
- }
- else if (encoding == "Zerocoded")
- {
- templatep->setEncoding(ME_ZEROCODED);
- }
- else
- {
- llerrs << "Bad encoding " << encoding << " at " << tokens.line()
- << llendl;
- }
- // Get deprecation
- if (tokens.want("Deprecated"))
- {
- templatep->setDeprecation(MD_DEPRECATED);
- }
- else if (tokens.want("UDPDeprecated"))
- {
- templatep->setDeprecation(MD_UDPDEPRECATED);
- }
- else if (tokens.want("UDPBlackListed"))
- {
- templatep->setDeprecation(MD_UDPBLACKLISTED);
- }
- else if (tokens.want("NotDeprecated"))
- {
- // This is the default value, but it can't hurt to set it twice
- templatep->setDeprecation(MD_NOTDEPRECATED);
- }
- // else ... it is probably a brace, let's just start block processing
- while (LLMessageBlock * blockp = parseBlock(tokens))
- {
- templatep->addBlock(blockp);
- }
-
- if (!tokens.want("}"))
- {
- llerrs << "Expecting closing } for message " << template_name << " at "
- << tokens.line() << llendl;
- }
- return templatep;
- }
- // static
- LLMessageBlock* LLTemplateParser::parseBlock(LLTemplateTokenizer& tokens)
- {
- if (!tokens.want("{"))
- {
- return NULL;
- }
- // Name first
- std::string block_name = tokens.next();
- // Is name a legit C variable name ?
- if (!b_variable_ok(block_name.c_str()))
- {
- llerrs << "not a legal block name: " << block_name << " at "
- << tokens.line() << llendl;
- }
- LLMessageBlock* blockp = NULL;
- // now, block type ("Single", "Multiple", or "Variable")
- std::string block_type = tokens.next();
- // which one is it?
- if (block_type == "Single")
- {
- // OK, we can create a block
- blockp = new LLMessageBlock(block_name.c_str(), MBT_SINGLE);
- }
- else if (block_type == "Multiple")
- {
- // Need to get the number of repeats
- std::string repeats = tokens.next();
-
- // Is it a legal integer ?
- if (!b_positive_integer_ok(repeats.c_str()))
- {
- llerrs << "not a legal integer for block multiple count: "
- << repeats << " at " << tokens.line() << llendl;
- }
-
- // OK, we can create a block
- blockp = new LLMessageBlock(block_name.c_str(), MBT_MULTIPLE,
- atoi(repeats.c_str()));
- }
- else if (block_type == "Variable")
- {
- // OK, we can create a block
- blockp = new LLMessageBlock(block_name.c_str(), MBT_VARIABLE);
- }
- else
- {
- llerrs << "bad block type: " << block_type
- << " at " << tokens.line() << llendl;
- }
- while (LLMessageVariable* varp = parseVariable(tokens))
- {
- blockp->addVariable(varp->getName(), varp->getType(), varp->getSize());
- delete varp;
- }
- if (!tokens.want("}"))
- {
- llerrs << "Expecting closing } for block " << block_name << " at "
- << tokens.line() << llendl;
- }
- return blockp;
- }
- // static
- LLMessageVariable* LLTemplateParser::parseVariable(LLTemplateTokenizer& tokens)
- {
- if (!tokens.want("{"))
- {
- return NULL;
- }
- std::string var_name = tokens.next();
- if (!b_variable_ok(var_name.c_str()))
- {
- llerrs << "Not a legit variable name: " << var_name << " at "
- << tokens.line() << llendl;
- }
- LLMessageVariable* varp = NULL;
- std::string var_type = tokens.next();
- if (var_type == "U8")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_U8, 1);
- }
- else if (var_type == "U16")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_U16, 2);
- }
- else if (var_type == "U32")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_U32, 4);
- }
- else if (var_type == "U64")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_U64, 8);
- }
- else if (var_type == "S8")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_S8, 1);
- }
- else if (var_type == "S16")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_S16, 2);
- }
- else if (var_type == "S32")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_S32, 4);
- }
- else if (var_type == "S64")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_S64, 8);
- }
- else if (var_type == "F32")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_F32, 4);
- }
- else if (var_type == "F64")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_F64, 8);
- }
- else if (var_type == "LLVector3")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_LLVector3, 12);
- }
- else if (var_type == "LLVector3d")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_LLVector3d, 24);
- }
- else if (var_type == "LLVector4")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_LLVector4, 16);
- }
- else if (var_type == "LLQuaternion")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_LLQuaternion, 12);
- }
- else if (var_type == "LLUUID")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_LLUUID, 16);
- }
- else if (var_type == "BOOL")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_BOOL, 1);
- }
- else if (var_type == "IPADDR")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_IP_ADDR, 4);
- }
- else if (var_type == "IPPORT")
- {
- varp = new LLMessageVariable(var_name.c_str(), MVT_IP_PORT, 2);
- }
- else if (var_type == "Fixed" || var_type == "Variable")
- {
- std::string variable_size = tokens.next();
-
- if (!b_positive_integer_ok(variable_size.c_str()))
- {
- llerrs << "not a legal integer variable size: " << variable_size
- << " at " << tokens.line() << llendl;
- }
- EMsgVariableType type_enum;
- if (var_type == "Variable")
- {
- type_enum = MVT_VARIABLE;
- }
- else if (var_type == "Fixed")
- {
- type_enum = MVT_FIXED;
- }
- else
- {
- type_enum = MVT_FIXED; // removes a warning
- llerrs << "bad variable type: " << var_type << " at "
- << tokens.line() << llendl;
- }
- varp = new LLMessageVariable(var_name.c_str(), type_enum,
- atoi(variable_size.c_str()));
- }
- else
- {
- llerrs << "bad variable type:" << var_type << " at " << tokens.line()
- << llendl;
- }
- if (!tokens.want("}"))
- {
- llerrs << "Expecting closing } for variable " << var_name << " at "
- << tokens.line() << llendl;
- }
- return varp;
- }
|