1 /** ======== stripper_mm ========
2 * Copyright (C) 2005-2006 David "BAILOPAN" Anderson
3 * No warranties of any kind.
4 * Based on the original concept of Stripper2 by botman
6 * License: see LICENSE.TXT
7 * ============================
14 #include "stripper_mm.h"
16 using namespace SourceHook;
20 void f_strncpy_s(char *dest, const char *str, size_t n)
31 * Removes whitespace characters from left side of string
33 void UTIL_TrimLeft(char *buffer)
35 // Let's think of this as our iterator
38 // Make sure the buffer isn't null
41 // Add up number of whitespace characters
47 // If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer
50 memmove(buffer, i, (strlen(i) + 1) * sizeof(char));
56 * Removes whitespace characters from right side of string
58 void UTIL_TrimRight(char *buffer)
60 // Make sure buffer isn't null
63 // Loop through buffer backwards while replacing whitespace chars with null chars
64 for (int i = (int)strlen(buffer) - 1; i >= 0; i--)
66 if (isspace(buffer[i]))
80 const char *pattern = "\"([^\"]+)\"\\s+\"([^\"]+)\"";
83 brk_re = pcre_compile(pattern, 0, &error, &err_no, NULL);
84 brk_re_extra = pcre_study(brk_re, 0, &error);
91 while (!m_StringCache.empty())
93 delete m_StringCache.front();
96 while (!m_PropCache.empty())
98 delete m_PropCache.front();
103 SourceHook::String *Stripper::AllocString()
105 if (m_StringCache.empty())
107 return new SourceHook::String();
109 SourceHook::String *str = m_StringCache.front();
115 void Stripper::FreeString(SourceHook::String *str)
117 m_StringCache.push(str);
120 ent_prop *Stripper::AllocProp()
122 if (m_PropCache.empty())
126 ent_prop *p = m_PropCache.front();
128 assert(p->marked == false);
136 void Stripper::FreeProp(ent_prop *prop)
139 assert(prop->marked == true);
140 prop->marked = false;
142 m_PropCache.push(prop);
145 void Stripper::SetEntityList(const char *ents)
150 m_tostring.assign(ents);
152 size_t len = m_tostring.size();
156 for (size_t i=0; i<len; i++)
158 if (ents[i] == '\n' || ents[i] == '\r')
160 /* check if we should expand the buffer */
161 if (i-pos+1 >= _tmpsize)
165 _tmp = new char[_tmpsize+1];
168 f_strncpy_s(_tmp, &(ents[pos]), i-pos+1);
169 /* make a new string out of it */
170 String *pstr = AllocString();
172 /* push it into the lines table */
173 m_lines.push_back(pstr);
178 /* make sure there is no data we missed */
182 if (len-pos+1 >= _tmpsize)
185 _tmpsize = len-pos+1;
186 _tmp = new char[_tmpsize+1];
189 f_strncpy_s(_tmp, &(ents[pos]), len-pos+1);
191 String *pstr = AllocString();
193 /* push it into the lines table */
194 m_lines.push_back(pstr);
196 /* delete temporary string */
199 /* build the real props list */
203 bool EntPropsMatch(parse_pair *p, ent_prop *e, int *ovector)
206 if (stricmp(p->key.c_str(), e->key.c_str()) == 0)
210 rc = pcre_exec(p->re, NULL, e->val.c_str(), e->val.size(), 0, 0, ovector, 30);
216 if (stricmp(e->val.c_str(), p->val.c_str()) == 0)
226 void ListRecycle(SourceHook::List<parse_pair *> &toread, SourceHook::List<parse_pair *> &towrite)
228 SourceHook::List<parse_pair *>::iterator iter;
229 for (iter=toread.begin(); iter!=toread.end(); iter++)
231 towrite.push_back((*iter));
236 void Stripper::RunReplaceFilter(replace_prop &replace, SourceHook::List<parse_pair *> &props)
238 List<List<ent_prop *> *>::iterator ent_iter;
239 List<ent_prop *>::iterator prop_iter;
240 List<parse_pair *>::iterator parse_iter;
241 List<ent_prop *> *proplist;
244 size_t num_match = 0;
247 for (ent_iter = m_props.begin();
248 ent_iter != m_props.end();
252 proplist = (*ent_iter);
253 /* loop through the list of properties for this block */
254 for (prop_iter = proplist->begin();
255 prop_iter != proplist->end();
259 /* try to match the current property against any we're looking for */
260 for (parse_iter = replace.match.begin();
261 parse_iter != replace.match.end();
265 if (EntPropsMatch(p, e, ovector))
271 /* if we've reached the end of possible matches, we can stop iterating */
272 if (num_match == replace.match.size())
277 /* check if this was a full match */
278 if (num_match == replace.match.size())
280 /* first do deletions */
281 prop_iter = proplist->begin();
283 while (prop_iter != proplist->end())
287 for (parse_iter = replace.to_remove.begin();
288 parse_iter != replace.to_remove.end();
292 if (EntPropsMatch(p, e, ovector))
302 e->last_alloc = __LINE__;
304 prop_iter = proplist->erase(prop_iter);
309 /* do replacements */
310 for (prop_iter = proplist->begin();
311 prop_iter != proplist->end();
315 for (parse_iter = replace.to_replace.begin();
316 parse_iter != replace.to_replace.end();
320 /* check if the keys match */
321 if (stricmp(p->key.c_str(), e->key.c_str()) == 0)
323 e->val.assign(p->val.c_str());
329 for (parse_iter = replace.to_insert.begin();
330 parse_iter != replace.to_insert.end();
337 e->last_alloc = __LINE__;
339 e->key.assign(p->key.c_str());
340 e->val.assign(p->val.c_str());
342 proplist->push_back(e);
347 ListRecycle(replace.match, props);
348 ListRecycle(replace.to_insert, props);
349 ListRecycle(replace.to_remove, props);
350 ListRecycle(replace.to_replace, props);
353 void Stripper::RunRemoveFilter(SourceHook::List<parse_pair *> &filters)
355 List<List<ent_prop *> *>::iterator proplist_iter;
356 List<ent_prop *> *proplist;
357 List<ent_prop *>::iterator ent_iter;
358 List<parse_pair *>::iterator pair_iter, pair_iter_begin, pair_iter_end;
364 size_t num_match = 0;
366 pair_iter_begin = filters.end();
367 pair_iter_end = filters.end();
368 proplist_iter = m_props.begin();
369 while (proplist_iter != m_props.end())
372 proplist = (*proplist_iter);
373 for (ent_iter = proplist->begin();
374 ent_iter != proplist->end();
378 for (pair_iter = pair_iter_begin;
379 pair_iter != pair_iter_end;
383 if (EntPropsMatch(p, e, ovector))
389 if (num_match == filters.size())
394 if (num_match == filters.size())
397 ent_iter = proplist->begin();
398 while (ent_iter != proplist->end())
402 ent_iter = proplist->erase(ent_iter);
404 e->last_free = __LINE__;
409 proplist_iter = m_props.erase(proplist_iter);
416 void Stripper::RunAddFilter(SourceHook::List<parse_pair *> &list)
418 List<parse_pair *>::iterator iter, end=list.end();
421 List<ent_prop *> *cl = new List<ent_prop *>();
427 e->last_alloc = __LINE__;
429 e->key.assign( (*iter)->key.c_str() );
430 e->val.assign( (*iter)->val.c_str() );
434 m_props.push_back(cl);
437 void Stripper::_BuildPropList()
445 bool in_block = false;
446 List<String *>::iterator iter, end=m_lines.end();
448 List<ent_prop *> *cl = NULL;
449 for (iter=m_lines.begin(); iter!=end; iter++)
452 if (*(s->c_str()) == '{' && !in_block)
454 /* if we reached a '{' and we're not in a block, begin a new block */
458 cl = new List<ent_prop *>();
460 } else if (*(s->c_str()) == '}' && in_block) {
461 /* if we reached a '}' ane we're in a block, end the old block
462 * and push it onto the block list
465 m_props.push_back(cl);
468 /* try to match our precompiled expression for "..." "..." */
469 int rc = pcre_exec(brk_re, brk_re_extra, s->c_str(), s->size(), 0, 0, ovector, 30);
472 size_t l = ovector[3] - ovector[2];
477 _key = new char[l+1];
480 f_strncpy_s(_key, s->c_str() + ovector[2], l);
481 l = ovector[5] - ovector[4];
486 _val = new char[l+1];
489 f_strncpy_s(_val, s->c_str() + ovector[4], l);
492 /* add a new ent to the list */
493 ent_prop *e = AllocProp();
495 e->last_alloc = __LINE__;
510 void Stripper::ApplyFileFilter(const char *file)
512 FILE *fp = fopen(file, "rt");
542 bool in_block = false;
543 List<parse_pair *> props;
544 CStack<parse_pair *> fpairs;
545 Mode mode = Mode_Filter;
546 SubMode submode = SubMode_None;
547 const char *error = NULL;
550 List<parse_pair *>::iterator iter, end;
551 replace_prop replace;
556 fgets(buffer, sizeof(buffer)-1, fp);
558 UTIL_TrimRight(buffer);
559 UTIL_TrimLeft(buffer);
561 if (buffer[0] == ';' ||
562 (buffer[0] == '/' && buffer[1] == '/') ||
563 (buffer[0] == '#') ||
566 if (strncmp(buffer, "filter:", 7) == 0)
569 } else if (strncmp(buffer, "add:", 4) == 0) {
571 } else if (strncmp(buffer, "modify:", 8) == 0) {
573 submode = SubMode_None;
574 } else if (!strncmp(buffer, "match:", 6) && (mode == Mode_Replace)) {
575 submode = SubMode_Match;
576 } else if (!strncmp(buffer, "replace:", 8) && (mode == Mode_Replace)) {
577 submode = SubMode_Replace;
578 } else if (!strncmp(buffer, "delete:", 7) && (mode == Mode_Replace)) {
579 submode = SubMode_Remove;
580 } else if (!strncmp(buffer, "insert:", 7) && (mode == Mode_Replace)) {
581 submode = SubMode_Insert;
582 } else if (strcmp(buffer, "{")==0 && !in_block) {
583 /* if we reach a new block and we're not in a block,
584 * dump the current props table and reset.
586 if (mode != Mode_Replace || (mode == Mode_Replace && submode != SubMode_None))
591 } else if (strncmp(buffer, "}", 1) == 0 && in_block) {
592 /* if we reach the end of a block and we're in a block,
593 * mark the end and flush what we've done
597 if (mode == Mode_Replace)
599 assert(submode != SubMode_None);
604 replace.to_insert = props;
610 replace.to_remove = props;
616 replace.match = props;
620 case SubMode_Replace:
622 replace.to_replace = props;
628 /* run the appropriate routine */
629 if (mode == Mode_Filter)
631 RunRemoveFilter(props);
632 } else if (mode == Mode_Add) {
636 /* push each of the unused filters
637 * and then clear the property list
640 for (iter=props.begin(); iter!=end; iter++)
642 fpairs.push((*iter));
646 } else if (strncmp(buffer, "}", 1) == 0 && !in_block && mode == Mode_Replace) {
647 /* run replace filter */
648 RunReplaceFilter(replace, props);
650 for (iter=props.begin(); iter!=end; iter++)
652 fpairs.push((*iter));
655 } else if (in_block) {
656 /* attempt to run our precompiled property match expression */
657 len = strlen(buffer);
658 int rc = pcre_exec(brk_re, brk_re_extra, buffer, len, 0, 0, ovector, 30);
661 size_t len = ovector[3] - ovector[2];
666 _key = new char[len+1];
669 f_strncpy_s(_key, buffer + ovector[2], len);
670 len = ovector[5] - ovector[4];
675 _val = new char[len+1];
678 f_strncpy_s(_val, buffer + ovector[4], len);
680 if (_val[0] == '/' && _val[len-1] == '/')
683 re = pcre_compile(&(_val[1]), PCRE_CASELESS, &error, &err_offs, NULL);
686 META_LOG(g_PLAPI, "File %s parse error (line %d):", file, line);
687 META_LOG(g_PLAPI, "Expression(%s): At pos %d, %s", _val, err_offs, error);
691 /* get a parse pair from the cache */
704 /* assign property information */
713 /* flush anything remaining */
715 for (iter=props.begin(); iter!=props.end(); iter++)
717 fpairs.push( (*iter) );
720 /* delete cached pairs */
721 parse_pair *k = NULL;
722 while (!fpairs.empty())
738 const char *Stripper::ToString()
742 return m_tostring.c_str();
747 List<List<ent_prop *> *>::iterator iter, end=m_props.end(), begin=m_props.begin();
748 List<ent_prop *>::iterator eiter, eend;
751 for (iter=m_props.begin(); iter!=end; iter++)
753 eend = (*iter)->end();
756 m_tostring.append("\n");
760 m_tostring.append("{\n");
761 for (eiter=(*iter)->begin(); eiter!=eend; eiter++)
763 m_tostring.append('"');
764 m_tostring.append( (*eiter)->key.c_str() );
765 m_tostring.append("\" \"");
766 m_tostring.append( (*eiter)->val.c_str() );
767 m_tostring.append("\"\n");
769 m_tostring.append("}");
774 return m_tostring.c_str();
777 void Stripper::Clear()
779 List<String *>::iterator lines_iter;
781 lines_iter = m_lines.begin();
782 while (lines_iter != m_lines.end())
784 FreeString((*lines_iter));
785 lines_iter = m_lines.erase(lines_iter);
789 List<ent_prop *>::iterator iter2, end2;
790 List<List<ent_prop *> *>::iterator iter3, end3;
792 iter3 = m_props.begin();
793 end3 = m_props.end();
794 while (iter3 != end3)
796 iter2 = (*iter3)->begin();
797 end2 = (*iter3)->end();
798 while (iter2 != end2)
802 (*iter2)->last_free = __LINE__;