Cosmetics: Fix coding style issues

This commit is contained in:
Felipe Zimmerle 2015-10-27 10:21:14 -03:00
parent ffb973700a
commit 93031d93d0
17 changed files with 46 additions and 39 deletions

View File

@ -37,11 +37,12 @@ namespace transformations {
std::string CssDecode::evaluate(std::string value, std::string CssDecode::evaluate(std::string value,
Assay *assay) { Assay *assay) {
char *tmp = (char *) malloc(sizeof(char) * value.size() + 1); char *tmp = reinterpret_cast<char *>(
malloc(sizeof(char) * value.size() + 1));
memcpy(tmp, value.c_str(), value.size() + 1); memcpy(tmp, value.c_str(), value.size() + 1);
tmp[value.size()] = '\0'; tmp[value.size()] = '\0';
css_decode_inplace((unsigned char *)tmp, value.size()); css_decode_inplace(reinterpret_cast<unsigned char *>(tmp), value.size());
std::string ret(tmp, 0, value.size()); std::string ret(tmp, 0, value.size());
free(tmp); free(tmp);
return ret; return ret;

View File

@ -46,7 +46,7 @@ int EscapeSeqDecode::ansi_c_sequences_decode_inplace(unsigned char *input,
if ((input[i] == '\\') && (i + 1 < input_len)) { if ((input[i] == '\\') && (i + 1 < input_len)) {
int c = -1; int c = -1;
switch(input[i + 1]) { switch (input[i + 1]) {
case 'a' : case 'a' :
c = '\a'; c = '\a';
break; break;
@ -86,22 +86,22 @@ int EscapeSeqDecode::ansi_c_sequences_decode_inplace(unsigned char *input,
/* Hexadecimal or octal? */ /* Hexadecimal or octal? */
if (c == -1) { if (c == -1) {
if ((input[i + 1] == 'x')||(input[i + 1] == 'X')) { if ((input[i + 1] == 'x') || (input[i + 1] == 'X')) {
/* Hexadecimal. */ /* Hexadecimal. */
if ((i + 3 < input_len)&&(isxdigit(input[i + 2]))&&(isxdigit(input[i + 3]))) { if ((i + 3 < input_len) && (isxdigit(input[i + 2]))
&& (isxdigit(input[i + 3]))) {
/* Two digits. */ /* Two digits. */
c = x2c(&input[i + 2]); c = x2c(&input[i + 2]);
i += 4; i += 4;
} else { } else {
/* Invalid encoding, do nothing. */ /* Invalid encoding, do nothing. */
} }
} } else {
else
if (ISODIGIT(input[i + 1])) { /* Octal. */ if (ISODIGIT(input[i + 1])) { /* Octal. */
char buf[4]; char buf[4];
int j = 0; int j = 0;
while((i + 1 + j < input_len)&&(j < 3)) { while ((i + 1 + j < input_len) && (j < 3)) {
buf[j] = input[i + 1 + j]; buf[j] = input[i + 1 + j];
j++; j++;
if (!ISODIGIT(input[i + 1 + j])) break; if (!ISODIGIT(input[i + 1 + j])) break;
@ -113,6 +113,7 @@ int EscapeSeqDecode::ansi_c_sequences_decode_inplace(unsigned char *input,
i += 1 + j; i += 1 + j;
} }
} }
}
} }
if (c == -1) { if (c == -1) {
@ -149,8 +150,7 @@ std::string EscapeSeqDecode::evaluate(std::string value,
int size = ansi_c_sequences_decode_inplace(tmp, value.size()); int size = ansi_c_sequences_decode_inplace(tmp, value.size());
std::string ret(""); std::string ret("");
ret.assign((char *)tmp, size); ret.assign(reinterpret_cast<char *>(tmp), size);
//ret.append((const char *)tmp);
free(tmp); free(tmp);
return ret; return ret;

View File

@ -42,9 +42,10 @@ std::string HexDecode::evaluate(std::string value,
int len = value.length(); int len = value.length();
std::string newString; std::string newString;
for(int i=0; i< len; i+=2) { for (int i=0; i< len; i+=2) {
std::string byte = value.substr(i,2); std::string byte = value.substr(i, 2);
char chr = (char) (int)strtol(byte.c_str(), NULL, 16); char chr = static_cast<char>(static_cast<int>(strtol(byte.c_str(),
NULL, 16)));
newString.push_back(chr); newString.push_back(chr);
} }

View File

@ -41,7 +41,7 @@ std::string HexEncode::evaluate(std::string value,
std::stringstream result; std::stringstream result;
for (std::size_t i=0; i < value.length(); i++) { for (std::size_t i=0; i < value.length(); i++) {
int ii = (char)value[i]; int ii = reinterpret_cast<char>(value[i]);
result << std::setw(2) << std::setfill('0') << std::hex << ii; result << std::setw(2) << std::setfill('0') << std::hex << ii;
} }

View File

@ -37,7 +37,8 @@ namespace transformations {
std::string JsDecode::evaluate(std::string value, std::string JsDecode::evaluate(std::string value,
Assay *assay) { Assay *assay) {
char *val = (char *) malloc(sizeof(char) * value.size() + 1); char *val = reinterpret_cast<char *>(
malloc(sizeof(char) * value.size() + 1));
memcpy(val, value.c_str(), value.size() + 1); memcpy(val, value.c_str(), value.size() + 1);
val[value.size()] = '\0'; val[value.size()] = '\0';

View File

@ -42,7 +42,8 @@ std::string NormalisePath::evaluate(std::string value,
Assay *assay) { Assay *assay) {
int changed = 0; int changed = 0;
char *tmp = (char *) malloc(sizeof(char) * value.size() + 1); char *tmp = reinterpret_cast<char *>(
malloc(sizeof(char) * value.size() + 1));
memcpy(tmp, value.c_str(), value.size() + 1); memcpy(tmp, value.c_str(), value.size() + 1);
tmp[value.size()] = '\0'; tmp[value.size()] = '\0';

View File

@ -38,11 +38,12 @@ std::string NormalisePathWin::evaluate(std::string value,
Assay *assay) { Assay *assay) {
int changed; int changed;
char *tmp = (char *) malloc(sizeof(char) * value.size() + 1); char *tmp = reinterpret_cast<char *>(
malloc(sizeof(char) * value.size() + 1));
memcpy(tmp, value.c_str(), value.size() + 1); memcpy(tmp, value.c_str(), value.size() + 1);
tmp[value.size()] = '\0'; tmp[value.size()] = '\0';
int i = normalize_path_inplace((unsigned char *)tmp, int i = normalize_path_inplace(reinterpret_cast<unsigned char *>(tmp),
value.size(), 1, &changed); value.size(), 1, &changed);
std::string ret(""); std::string ret("");

View File

@ -39,15 +39,16 @@ ReplaceComments::ReplaceComments(std::string action)
std::string ReplaceComments::evaluate(std::string value, std::string ReplaceComments::evaluate(std::string value,
Assay *assay) { Assay *assay) {
long int i, j, incomment; uint64_t i, j, incomment;
int changed = 0; int changed = 0;
char *input = (char *) malloc(sizeof(char) * value.size() + 1); char *input = reinterpret_cast<char *>(
malloc(sizeof(char) * value.size() + 1));
memcpy(input, value.c_str(), value.size() + 1); memcpy(input, value.c_str(), value.size() + 1);
input[value.size()] = '\0'; input[value.size()] = '\0';
i = j = incomment = 0; i = j = incomment = 0;
while(i < value.size()) { while (i < value.size()) {
if (incomment == 0) { if (incomment == 0) {
if ((input[i] == '/') && (i + 1 < value.size()) if ((input[i] == '/') && (i + 1 < value.size())
&& (input[i + 1] == '*')) { && (input[i + 1] == '*')) {
@ -78,7 +79,7 @@ std::string ReplaceComments::evaluate(std::string value,
std::string resp; std::string resp;
resp.append((char *)input, j); resp.append(reinterpret_cast<char *>(input), j);
free(input); free(input);

View File

@ -45,7 +45,6 @@
#include "actions/transformations/remove_comments.h" #include "actions/transformations/remove_comments.h"
#include "actions/transformations/remove_nulls.h" #include "actions/transformations/remove_nulls.h"
#include "actions/transformations/remove_whitespace.h" #include "actions/transformations/remove_whitespace.h"
#include "actions/transformations/compress_whitespace.h"
#include "actions/transformations/replace_comments.h" #include "actions/transformations/replace_comments.h"
#include "actions/transformations/replace_nulls.h" #include "actions/transformations/replace_nulls.h"
#include "actions/transformations/sha1.h" #include "actions/transformations/sha1.h"

View File

@ -32,9 +32,9 @@ namespace transformations {
int UrlDecode::urldecode_nonstrict_inplace(unsigned char *input, int UrlDecode::urldecode_nonstrict_inplace(unsigned char *input,
long int input_len, int *invalid_count, int *changed) { uint64_t input_len, int *invalid_count, int *changed) {
unsigned char *d = (unsigned char *)input; unsigned char *d = (unsigned char *)input;
long int i, count; uint64_t i, count;
*changed = 0; *changed = 0;
@ -52,7 +52,7 @@ int UrlDecode::urldecode_nonstrict_inplace(unsigned char *input,
char c1 = input[i + 1]; char c1 = input[i + 1];
char c2 = input[i + 2]; char c2 = input[i + 2];
if (VALID_HEX(c1) && VALID_HEX(c2)) { if (VALID_HEX(c1) && VALID_HEX(c2)) {
unsigned long uni = x2c(&input[i + 1]); uint64_t uni = x2c(&input[i + 1]);
*d++ = (wchar_t)uni; *d++ = (wchar_t)uni;
count++; count++;
@ -61,13 +61,13 @@ int UrlDecode::urldecode_nonstrict_inplace(unsigned char *input,
} else { } else {
/* Not a valid encoding, skip this % */ /* Not a valid encoding, skip this % */
*d++ = input[i++]; *d++ = input[i++];
count ++; count++;
(*invalid_count)++; (*invalid_count)++;
} }
} else { } else {
/* Not enough bytes available, copy the raw bytes. */ /* Not enough bytes available, copy the raw bytes. */
*d++ = input[i++]; *d++ = input[i++];
count ++; count++;
(*invalid_count)++; (*invalid_count)++;
} }
} else { } else {
@ -104,7 +104,8 @@ std::string UrlDecode::evaluate(std::string value,
memcpy(val, value.c_str(), value.size() + 1); memcpy(val, value.c_str(), value.size() + 1);
val[value.size()] = '\0'; val[value.size()] = '\0';
int size = urldecode_nonstrict_inplace(val, value.size(), &invalid_count, &changed); int size = urldecode_nonstrict_inplace(val, value.size(),
&invalid_count, &changed);
std::string out; std::string out;
out.append((const char *)val, size); out.append((const char *)val, size);

View File

@ -34,7 +34,7 @@ class UrlDecode : public Transformation {
std::string evaluate(std::string exp, std::string evaluate(std::string exp,
Assay *assay) override; Assay *assay) override;
int urldecode_nonstrict_inplace(unsigned char *input, long int input_len, int urldecode_nonstrict_inplace(unsigned char *input, uint64_t input_len,
int *invalid_count, int *changed); int *invalid_count, int *changed);
}; };

View File

@ -46,7 +46,7 @@ std::string UrlEncode::url_enc(const char *input,
*changed = 0; *changed = 0;
len = input_len * 3 + 1; len = input_len * 3 + 1;
d = rval = (char *)malloc(len); d = rval = reinterpret_cast<char *>(malloc(len));
if (rval == NULL) { if (rval == NULL) {
return NULL; return NULL;
} }
@ -63,7 +63,7 @@ std::string UrlEncode::url_enc(const char *input,
} else { } else {
if ( (c == 42) || ((c >= 48) && (c <= 57)) if ( (c == 42) || ((c >= 48) && (c <= 57))
|| ((c >= 65) && (c <= 90)) || ((c >= 65) && (c <= 90))
|| ((c >= 97)&&(c <= 122))) { || ((c >= 97) && (c <= 122))) {
*d++ = c; *d++ = c;
count++; count++;
} else { } else {

View File

@ -15,9 +15,10 @@
#include "operators/verify_cc.h" #include "operators/verify_cc.h"
#include <iostream>
#include <pcre.h> #include <pcre.h>
#include <iostream>
#include <cstring> #include <cstring>
#include <vector>
#include "operators/operator.h" #include "operators/operator.h"

View File

@ -16,8 +16,8 @@
#ifndef SRC_OPERATORS_VERIFY_CC_H_ #ifndef SRC_OPERATORS_VERIFY_CC_H_
#define SRC_OPERATORS_VERIFY_CC_H_ #define SRC_OPERATORS_VERIFY_CC_H_
#include <string>
#include <pcre.h> #include <pcre.h>
#include <string>
#include "operators/operator.h" #include "operators/operator.h"

View File

@ -21,7 +21,7 @@
#include "modsecurity/modsecurity.h" #include "modsecurity/modsecurity.h"
#ifndef SRC_UTILS_H_ #ifndef SRC_UTILS_H_
#define SRC_UTILS_H_
#define VALID_HEX(X) (((X >= '0') && (X <= '9')) || \ #define VALID_HEX(X) (((X >= '0') && (X <= '9')) || \
((X >= 'a') && (X <= 'f')) || ((X >= 'A') && (X <= 'F'))) ((X >= 'a') && (X <= 'f')) || ((X >= 'A') && (X <= 'F')))
@ -60,6 +60,5 @@ namespace ModSecurity {
const std::string& param); const std::string& param);
} // namespace ModSecurity } // namespace ModSecurity
#define SRC_UTILS_H_
#endif // SRC_UTILS_H_ #endif // SRC_UTILS_H_

View File

@ -104,7 +104,6 @@ std::string SHA1::final_bin(bool toReset = true) {
std::string SHA1::final() { std::string SHA1::final() {
final_bin(false); final_bin(false);
/* Hex std::string */ /* Hex std::string */

View File

@ -72,8 +72,10 @@ std::string UnitTest::print() {
i << this->obtained << "\"" << std::endl; i << this->obtained << "\"" << std::endl;
} }
if (this->output != this->obtainedOutput) { if (this->output != this->obtainedOutput) {
i << "Expecting: \"" << ModSecurity::toHexIfNeeded(this->output) << "\" - returned: \""; i << "Expecting: \"" << ModSecurity::toHexIfNeeded(this->output);
i << ModSecurity::toHexIfNeeded(this->obtainedOutput) << "\"" << std::endl; i << "\" - returned: \"";
i << ModSecurity::toHexIfNeeded(this->obtainedOutput) << "\"";
i << std::endl;
} }
return i.str(); return i.str();