Skip to content
Permalink
Browse files

Added .clang-format style rules, clam-format script to automate forma…

…tting of ClamAV code, and preparing select files so that clang-format does not alter carefully formatted sections.
  • Loading branch information...
micah-at-talos committed Dec 3, 2018
1 parent 9404d47 commit 27948a034c5136081eed981ac0491a8f6f345408
@@ -0,0 +1,117 @@
---
Language: Cpp
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: true
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Linux
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 0
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 8
UseTab: Never
...

@@ -0,0 +1,73 @@
#!/bin/bash

clang-format -style='{ Language: Cpp, UseTab: Never, IndentWidth: 4, AlignTrailingComments: true, AlignConsecutiveAssignments: true, AlignAfterOpenBracket: true, AlignEscapedNewlines: Left, AlignOperands: true, AllowShortFunctionsOnASingleLine: Empty, AllowShortIfStatementsOnASingleLine: true, AllowShortLoopsOnASingleLine: true, BreakBeforeBraces: Linux, BreakBeforeTernaryOperators: true, ColumnLimit: 0, FixNamespaceComments: true, SortIncludes: false, MaxEmptyLinesToKeep: 1, SpaceBeforeParens: ControlStatements, IndentCaseLabels: true, DerivePointerAlignment: true }' -dump-config > .clang-format

clang-format -i -verbose clamav-milter/*.c
clang-format -i -verbose clamav-milter/*.h
clang-format -i -verbose clambc/*.c
clang-format -i -verbose clambc/*.h
clang-format -i -verbose clamconf/*.c
clang-format -i -verbose clamconf/*.h
clang-format -i -verbose clamd/*.c
clang-format -i -verbose clamd/*.h
clang-format -i -verbose clamdscan/*.c
clang-format -i -verbose clamdscan/*.h
clang-format -i -verbose clamdtop/*.c
clang-format -i -verbose clamdtop/*.h
clang-format -i -verbose clamscan/*.c
clang-format -i -verbose clamscan/*.h
clang-format -i -verbose clamsubmit/*.c
clang-format -i -verbose clamsubmit/*.h
clang-format -i -verbose examples/*.c
clang-format -i -verbose examples/*.h
clang-format -i -verbose examples/fileprop_analysis/*.c
clang-format -i -verbose examples/fileprop_analysis/old/*.c
clang-format -i -verbose freshclam/*.c
clang-format -i -verbose freshclam/*.h
clang-format -i -verbose libclamav/*.c
clang-format -i -verbose libclamav/*.h
clang-format -i -verbose libclamav/jsparse/*.c
clang-format -i -verbose libclamav/jsparse/*.h
clang-format -i -verbose libclamav/lzw/*.c
clang-format -i -verbose libclamav/lzw/*.h
clang-format -i -verbose libclamav/nsis/nulsft.*
clang-format -i -verbose libclamunrar_iface/*.c
clang-format -i -verbose libclamunrar_iface/*.h
clang-format -i -verbose libfreshclam/*.c
clang-format -i -verbose libfreshclam/*.h
clang-format -i -verbose shared/*.c
clang-format -i -verbose shared/*.h
clang-format -i -verbose sigtool/*.c
clang-format -i -verbose sigtool/*.h
clang-format -i -verbose unit_tests/*.c
clang-format -i -verbose unit_tests/*.h
clang-format -i -verbose win32/compat/*.c
clang-format -i -verbose win32/compat/*.h
clang-format -i -verbose win32/*.h

# Undo changes to specific files (whitelist)
git checkout clamd/fts.c
git checkout clamd/priv_fts.h
git checkout libclamav/iana_cctld.h
git checkout libclamav/bytecode_api_decl.c
git checkout libclamav/bytecode_api_impl.h
git checkout libclamav/bytecode_hooks.h
git checkout libclamav/rijndael.c
git checkout libclamav/rijndael.h
git checkout libclamav/yara_lexer.c
git checkout libclamav/yara_grammar.c
git checkout libclamav/yara_grammar.h
git checkout libclamav/yara_exec.c
git checkout libclamav/yara_exec.h
git checkout libclamav/yara_compiler.h
git checkout libclamav/yara_compiler.h
git checkout libclamav/yara_parser.h
git checkout libclamav/yara_hash.c
git checkout libclamav/yara_hash.h
git checkout libclamav/yara_arena.c
git checkout libclamav/yara_arena.h
git checkout libclamav/inffixed64.h
git checkout libclamav/inflate64.h
git checkout libclamav/inflate64.c
git checkout libclamav/inflate64_priv.h
git checkout shared/queue.h
@@ -446,7 +446,7 @@ sfsistat clamfi_eom(SMFICTX *ctx) {
} else if(pid > 0) {
int wret;
pthread_mutex_unlock(&virusaction_lock);
while((wret = waitpid(pid, &ret, 0)) == -1 && errno == EINTR);
while ((wret = waitpid(pid, &ret, 0)) == -1 && errno == EINTR) continue;
if(wret<0)
logg("!VirusEvent: waitpid() failed: %s\n", cli_strerror(errno, er, sizeof(er)));
else {
@@ -175,7 +175,7 @@ virusaction (const char *filename, const char *virname,
else if (pid > 0)
{ /* parent */
pthread_mutex_unlock (&virusaction_lock);
while (waitpid (pid, NULL, 0) == -1 && errno == EINTR);
while (waitpid (pid, NULL, 0) == -1 && errno == EINTR) continue;
}
else
{
@@ -669,7 +669,7 @@ int parallel_client_scan(char *file, int scantype, int *infected, int *err, int
}

sendln(cdata.sockd, "zEND", 5);
while(cdata.ids && !dspresult(&cdata));
while (cdata.ids && !dspresult(&cdata)) continue;
closesocket(cdata.sockd);

*infected += cdata.infected;
@@ -860,7 +860,7 @@ int scanmanager(const struct optstruct *opts)
}

if((ret = cl_engine_compile(engine)) != 0) {
logg("!Database initialization error: %s\n", cl_strerror(ret));;
logg("!Database initialization error: %s\n", cl_strerror(ret));

cl_engine_free(engine);
return 2;
@@ -440,7 +440,7 @@ int main(int argc, char *argv[])
}
curl_easy_setopt(clam_curl, CURLOPT_COOKIE, str);
free(str);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "utf8", CURLFORM_COPYCONTENTS, "\x27" "\x13", CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "utf8", CURLFORM_COPYCONTENTS, "\x27\x13", CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "authenticity_token", CURLFORM_COPYCONTENTS, authenticity_token, CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "submissionID", CURLFORM_COPYCONTENTS, submissionID, CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "type", CURLFORM_COPYCONTENTS, malware?"malware":"fp", CURLFORM_END);
@@ -82,7 +82,7 @@ int main(int argc, char **argv)

/* build engine */
if((ret = cl_engine_compile(engine)) != CL_SUCCESS) {
printf("Database initialization error: %s\n", cl_strerror(ret));;
printf("Database initialization error: %s\n", cl_strerror(ret));
cl_engine_free(engine);
close(fd);
return 2;
@@ -187,7 +187,7 @@ static int
download (const struct optstruct *opts, const char *cfgfile)
{
time_t currtime;
int ret = 0, try = 1, maxattempts = 0;
int ret = 0, attempt = 1, maxattempts = 0;
const struct optstruct *opt;

time(&currtime);
@@ -207,17 +207,17 @@ download (const struct optstruct *opts, const char *cfgfile)
{
while (opt)
{
ret = downloadmanager (opts, opt->strarg, try);
ret = downloadmanager (opts, opt->strarg, attempt);
#ifndef _WIN32
alarm (0);
#endif
if (ret == FCE_CONNECTION || ret == FCE_BADCVD
|| ret == FCE_FAILEDGET || ret == FCE_MIRRORNOTSYNC)
{
if (try < maxattempts)
if (attempt < maxattempts)
{
logg ("Trying again in 5 secs...\n");
try++;
attempt++;
sleep (5);
continue;
}
@@ -229,7 +229,7 @@ download (const struct optstruct *opts, const char *cfgfile)
{
logg ("Update failed. Your network may be down or none of the mirrors listed in %s is working. Check https://www.clamav.net/documents/official-mirror-faq for possible reasons.\n", cfgfile);
}
try = 1;
attempt = 1;
}

}
@@ -1597,7 +1597,7 @@ test_database_wrap (const char *file, const char *newdb, int bytecode)
}
fclose (f);

while ((ret = waitpid (pid, &status, 0)) == -1 && errno == EINTR);
while ((ret = waitpid (pid, &status, 0)) == -1 && errno == EINTR) continue;
if (ret == -1 && errno != ECHILD)
logg ("^waitpid() failed: %s\n", strerror (errno));
cli_chomp (firstline);
@@ -48,9 +48,9 @@

#ifndef timercmp
#define timercmp(a, b, cmp) \
(((a)->tv_sec == (b)->tv_sec) ? \
((a)->tv_usec cmp (b)->tv_usec) : \
((a)->tv_sec cmp (b)->tv_sec))
(((a)->tv_sec == (b)->tv_sec) \
? ((a)->tv_usec cmp (b)->tv_usec) \
: ((a)->tv_sec cmp (b)->tv_sec))
#endif /* timercmp */

#ifndef timersub
@@ -204,7 +204,7 @@ int cli_scanapm(cli_ctx *ctx)
!strncmp((char*)apentry.type, "Apple_Driver_ATAPI", 32) ||
!strncmp((char*)apentry.type, "Apple_Patches", 32)) {

partsize = apentry.pBlockCount * 2048;;
partsize = apentry.pBlockCount * 2048;
}
}

@@ -281,7 +281,7 @@ static int apm_prtn_intxn(cli_ctx *ctx, struct apm_partition_info *aptable, size
!strncmp((char*)apentry.type, "Apple_Driver_ATAPI", 32) ||
!strncmp((char*)apentry.type, "Apple_Patches", 32)) {

apentry.pBlockCount = apentry.pBlockCount * 4;;
apentry.pBlockCount = apentry.pBlockCount * 4;
}
}

@@ -2170,7 +2170,7 @@ int asn1_load_mscat(fmap_t *map, struct cl_engine *engine) {
}
if(!engine->hm_fp) {
if(!(engine->hm_fp = mpool_calloc(engine->mempool, 1, sizeof(*db)))) {
tag.size = 1;;
tag.size = 1;
return 1;
}
#ifdef USE_MPOOL
@@ -34,6 +34,7 @@
#include "binhex.h"


// clang-format off
static const uint8_t hqxtbl[] = {
/* 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f */
/* 00-0f */ 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
@@ -45,6 +46,7 @@ static const uint8_t hqxtbl[] = {
/* 60-6f */ 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0xff,0x37,0x38,0x39,0x3a,0x3b,0x3c,0xff,0xff,
/* 70-7f */ 0x3d,0x3e,0x3f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
};
// clang-format on

#define BH_FLUSH_SZ (BUFSIZ - 256)

@@ -30,7 +30,8 @@
* fallback.
* Usually bytecode.cvd will contain this bytecode */

static const char* builtin_bc_startup = "ClamBCafhhbfkjmld|afefdfggifnf```aa```|biacflfafmfbfcfmb`cnbacacmbachcccmbgfbfcc`ccchcbfdf``bgcaap`clamcoincidencejb:4096\n"
static const char* builtin_bc_startup =
"ClamBCafhhbfkjmld|afefdfggifnf```aa```|biacflfafmfbfcfmb`cnbacacmbachcccmbgfbfcc`ccchcbfdf``bgcaap`clamcoincidencejb:4096\n"
"\n"
"Teddaaahdabahdacahdadahdaeahdafahdagahebfgebidebegebdgebgdebkdebcgebbgebageb`gebofebnfebmfebedeblfebkfebjfebadcbgab`bb`bb`bb`bb`bb`bb`bbifbifbifbifbifbifbifahahahahahahahahahebneebifaaaaaaaab`baabb`bb`baacb`bbadb`baacb`bboeb`baacb`bb`bb`baadb`bbadb`bb`baadb`bbadbadb`bdbadahdbkaahdbbcahdbibahdb`eahdbddahdbodahdbdaahdaiahdakahdamahdahahdbncahdbnbah\n"
"Ebjdaibcdbbf|bcaefnfgfifnfefoedfcfofnfffoelfeffgeflf``bbdbbf|bkaefnfgfifnfefoeffegnfcfdgifofnfaflfifdgigoelfeffgeflf``agbcf|baadfefbfeggfoe`gbgifnfdgoeegifnfdg``bcabcf|afdgefcgdgbc``afbdf|b`adfefbfeggfoe`gbgifnfdgoecgdgbg``bhdbef|b`agfefdgoeefnffgifbgofnfmfefnfdg``aabff|afdgefcgdgac``bidbgf|bdadfifcgafbflfefoebfigdgefcfofdfefoeifff``bjdbgf|aodfifcgafbflfefoejfifdgoeifff``\n"
Oops, something went wrong.

0 comments on commit 27948a0

Please sign in to comment.
You can’t perform that action at this time.