change fmt style
This commit is contained in:
270
.clang-format
Normal file
270
.clang-format
Normal file
@@ -0,0 +1,270 @@
|
||||
---
|
||||
Language: Cpp
|
||||
# BasedOnStyle: Chromium
|
||||
AccessModifierOffset: -2
|
||||
AlignAfterOpenBracket: Align
|
||||
AlignArrayOfStructures: None
|
||||
AlignConsecutiveAssignments:
|
||||
Enabled: false
|
||||
AcrossEmptyLines: false
|
||||
AcrossComments: false
|
||||
AlignCompound: false
|
||||
PadOperators: true
|
||||
AlignConsecutiveBitFields:
|
||||
Enabled: false
|
||||
AcrossEmptyLines: false
|
||||
AcrossComments: false
|
||||
AlignCompound: false
|
||||
PadOperators: false
|
||||
AlignConsecutiveDeclarations:
|
||||
Enabled: false
|
||||
AcrossEmptyLines: false
|
||||
AcrossComments: false
|
||||
AlignCompound: false
|
||||
PadOperators: false
|
||||
AlignConsecutiveMacros:
|
||||
Enabled: false
|
||||
AcrossEmptyLines: false
|
||||
AcrossComments: false
|
||||
AlignCompound: false
|
||||
PadOperators: false
|
||||
AlignConsecutiveShortCaseStatements:
|
||||
Enabled: false
|
||||
AcrossEmptyLines: false
|
||||
AcrossComments: false
|
||||
AlignCaseColons: false
|
||||
AlignEscapedNewlines: Left
|
||||
AlignOperands: Align
|
||||
AlignTrailingComments:
|
||||
Kind: Always
|
||||
OverEmptyLines: 0
|
||||
AllowAllArgumentsOnNextLine: true
|
||||
AllowAllParametersOfDeclarationOnNextLine: false
|
||||
AllowShortBlocksOnASingleLine: Never
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortEnumsOnASingleLine: true
|
||||
AllowShortFunctionsOnASingleLine: Inline
|
||||
AllowShortIfStatementsOnASingleLine: Never
|
||||
AllowShortLambdasOnASingleLine: All
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AlwaysBreakAfterDefinitionReturnType: None
|
||||
AlwaysBreakAfterReturnType: None
|
||||
AlwaysBreakBeforeMultilineStrings: true
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
AttributeMacros:
|
||||
- __capability
|
||||
BinPackArguments: true
|
||||
BinPackParameters: false
|
||||
BitFieldColonSpacing: Both
|
||||
BreakBeforeBraces: Custom
|
||||
BraceWrapping:
|
||||
AfterCaseLabel: false
|
||||
AfterClass: true
|
||||
AfterEnum: true
|
||||
AfterStruct: true
|
||||
AfterUnion: true
|
||||
AfterControlStatement: Never
|
||||
AfterExternBlock: false
|
||||
AfterFunction: true
|
||||
AfterNamespace: true
|
||||
AfterObjCDeclaration: false
|
||||
BeforeCatch: false
|
||||
BeforeElse: false
|
||||
BeforeLambdaBody: false
|
||||
BeforeWhile: false
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: true
|
||||
SplitEmptyRecord: true
|
||||
SplitEmptyNamespace: true
|
||||
BreakAfterAttributes: Never
|
||||
BreakAfterJavaFieldAnnotations: false
|
||||
BreakArrays: true
|
||||
BreakBeforeBinaryOperators: None
|
||||
BreakBeforeConceptDeclarations: Always
|
||||
BreakBeforeInlineASMColon: OnlyMultiline
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakConstructorInitializers: BeforeColon
|
||||
BreakInheritanceList: BeforeColon
|
||||
BreakStringLiterals: true
|
||||
ColumnLimit: 80
|
||||
CommentPragmas: '^ IWYU pragma:'
|
||||
CompactNamespaces: false
|
||||
ConstructorInitializerIndentWidth: 4
|
||||
ContinuationIndentWidth: 4
|
||||
Cpp11BracedListStyle: true
|
||||
DerivePointerAlignment: false
|
||||
DisableFormat: false
|
||||
EmptyLineAfterAccessModifier: Never
|
||||
EmptyLineBeforeAccessModifier: LogicalBlock
|
||||
ExperimentalAutoDetectBinPacking: false
|
||||
FixNamespaceComments: true
|
||||
ForEachMacros:
|
||||
- foreach
|
||||
- Q_FOREACH
|
||||
- BOOST_FOREACH
|
||||
IfMacros:
|
||||
- KJ_IF_MAYBE
|
||||
IncludeBlocks: Preserve
|
||||
IncludeCategories:
|
||||
- Regex: '^<ext/.*\.h>'
|
||||
Priority: 2
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
- Regex: '^<.*\.h>'
|
||||
Priority: 1
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
- Regex: '^<.*'
|
||||
Priority: 2
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
- Regex: '.*'
|
||||
Priority: 3
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
IncludeIsMainRegex: '([-_](test|unittest))?$'
|
||||
IncludeIsMainSourceRegex: ''
|
||||
IndentAccessModifiers: false
|
||||
IndentCaseBlocks: false
|
||||
IndentCaseLabels: true
|
||||
IndentExternBlock: AfterExternBlock
|
||||
IndentGotoLabels: true
|
||||
IndentPPDirectives: None
|
||||
IndentRequiresClause: true
|
||||
IndentWidth: 2
|
||||
IndentWrappedFunctionNames: false
|
||||
InsertBraces: false
|
||||
InsertNewlineAtEOF: false
|
||||
InsertTrailingCommas: None
|
||||
IntegerLiteralSeparator:
|
||||
Binary: 0
|
||||
BinaryMinDigits: 0
|
||||
Decimal: 0
|
||||
DecimalMinDigits: 0
|
||||
Hex: 0
|
||||
HexMinDigits: 0
|
||||
JavaScriptQuotes: Leave
|
||||
JavaScriptWrapImports: true
|
||||
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||
KeepEmptyLinesAtEOF: false
|
||||
LambdaBodyIndentation: Signature
|
||||
LineEnding: DeriveLF
|
||||
MacroBlockBegin: ''
|
||||
MacroBlockEnd: ''
|
||||
MaxEmptyLinesToKeep: 1
|
||||
NamespaceIndentation: None
|
||||
ObjCBinPackProtocolList: Never
|
||||
ObjCBlockIndentWidth: 2
|
||||
ObjCBreakBeforeNestedBlockParam: true
|
||||
ObjCSpaceAfterProperty: false
|
||||
ObjCSpaceBeforeProtocolList: true
|
||||
PackConstructorInitializers: NextLine
|
||||
PenaltyBreakAssignment: 2
|
||||
PenaltyBreakBeforeFirstCallParameter: 1
|
||||
PenaltyBreakComment: 300
|
||||
PenaltyBreakFirstLessLess: 120
|
||||
PenaltyBreakOpenParenthesis: 0
|
||||
PenaltyBreakString: 1000
|
||||
PenaltyBreakTemplateDeclaration: 10
|
||||
PenaltyExcessCharacter: 1000000
|
||||
PenaltyIndentedWhitespace: 0
|
||||
PenaltyReturnTypeOnItsOwnLine: 200
|
||||
PointerAlignment: Left
|
||||
PPIndentWidth: -1
|
||||
QualifierAlignment: Leave
|
||||
RawStringFormats:
|
||||
- Language: Cpp
|
||||
Delimiters:
|
||||
- cc
|
||||
- CC
|
||||
- cpp
|
||||
- Cpp
|
||||
- CPP
|
||||
- 'c++'
|
||||
- 'C++'
|
||||
CanonicalDelimiter: ''
|
||||
BasedOnStyle: google
|
||||
- Language: TextProto
|
||||
Delimiters:
|
||||
- pb
|
||||
- PB
|
||||
- proto
|
||||
- PROTO
|
||||
EnclosingFunctions:
|
||||
- EqualsProto
|
||||
- EquivToProto
|
||||
- PARSE_PARTIAL_TEXT_PROTO
|
||||
- PARSE_TEST_PROTO
|
||||
- PARSE_TEXT_PROTO
|
||||
- ParseTextOrDie
|
||||
- ParseTextProtoOrDie
|
||||
- ParseTestProto
|
||||
- ParsePartialTestProto
|
||||
CanonicalDelimiter: pb
|
||||
BasedOnStyle: google
|
||||
ReferenceAlignment: Pointer
|
||||
ReflowComments: true
|
||||
RemoveBracesLLVM: false
|
||||
RemoveParentheses: Leave
|
||||
RemoveSemicolon: false
|
||||
RequiresClausePosition: OwnLine
|
||||
RequiresExpressionIndentation: OuterScope
|
||||
SeparateDefinitionBlocks: Leave
|
||||
ShortNamespaceLines: 1
|
||||
SortIncludes: CaseSensitive
|
||||
SortJavaStaticImport: Before
|
||||
SortUsingDeclarations: LexicographicNumeric
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceAfterLogicalNot: false
|
||||
SpaceAfterTemplateKeyword: true
|
||||
SpaceAroundPointerQualifiers: Default
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeCaseColon: false
|
||||
SpaceBeforeCpp11BracedList: false
|
||||
SpaceBeforeCtorInitializerColon: true
|
||||
SpaceBeforeInheritanceColon: true
|
||||
SpaceBeforeJsonColon: false
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceBeforeParensOptions:
|
||||
AfterControlStatements: true
|
||||
AfterForeachMacros: true
|
||||
AfterFunctionDefinitionName: false
|
||||
AfterFunctionDeclarationName: false
|
||||
AfterIfMacros: true
|
||||
AfterOverloadedOperator: false
|
||||
AfterRequiresInClause: false
|
||||
AfterRequiresInExpression: false
|
||||
BeforeNonEmptyParentheses: false
|
||||
SpaceBeforeRangeBasedForLoopColon: true
|
||||
SpaceBeforeSquareBrackets: false
|
||||
SpaceInEmptyBlock: false
|
||||
SpacesBeforeTrailingComments: 2
|
||||
SpacesInAngles: Never
|
||||
SpacesInContainerLiterals: true
|
||||
SpacesInLineCommentPrefix:
|
||||
Minimum: 1
|
||||
Maximum: -1
|
||||
SpacesInParens: Never
|
||||
SpacesInParensOptions:
|
||||
InCStyleCasts: false
|
||||
InConditionalStatements: false
|
||||
InEmptyParentheses: false
|
||||
Other: false
|
||||
SpacesInSquareBrackets: false
|
||||
Standard: Auto
|
||||
StatementAttributeLikeMacros:
|
||||
- Q_EMIT
|
||||
StatementMacros:
|
||||
- Q_UNUSED
|
||||
- QT_REQUIRE_VERSION
|
||||
TabWidth: 8
|
||||
UseTab: Never
|
||||
VerilogBreakBetweenInstancePorts: true
|
||||
WhitespaceSensitiveMacros:
|
||||
- BOOST_PP_STRINGIZE
|
||||
- CF_SWIFT_NAME
|
||||
- NS_SWIFT_NAME
|
||||
- PP_STRINGIZE
|
||||
- STRINGIZE
|
||||
...
|
||||
|
@@ -44,3 +44,11 @@ All cryptography libraries list possible side channel attacks in their documenta
|
||||
- TODO: rng
|
||||
- TODO: length extension
|
||||
- TODO: password hashing
|
||||
|
||||
|
||||
## Contribution guide
|
||||
- Please use `clang-format` (and the provided `.clang-format` style)
|
||||
If you do not want to use `clang-format`, try to match the code style of other existing code,
|
||||
and a maintainer will format your code on merge.
|
||||
- Do not break portability of an existing "finished" library
|
||||
- Make new libraries as portable as possible (in a reasonable amount of time)
|
||||
|
211
chacha20.h
211
chacha20.h
@@ -2,15 +2,16 @@
|
||||
* Copyright (c) 2025 Alexander Nutz
|
||||
* MIT licensed, see below documentation
|
||||
*
|
||||
* Latest version can be found at: https://gitea.vxcc.dev/alexander.nutz/slow-libs
|
||||
* Latest version can be found at:
|
||||
* https://gitea.vxcc.dev/alexander.nutz/slow-libs
|
||||
*
|
||||
*
|
||||
* ======== ChaCha20 stream chiper ========
|
||||
* ======== ChaCha20 stream cihper ========
|
||||
*
|
||||
* Security considerations:
|
||||
* - manually zeroize memory (depending on your application)
|
||||
* - length extension attack:
|
||||
* ChaCha20 is only a stream chiper, and, like AES,
|
||||
* ChaCha20 is only a stream cihper, and, like AES,
|
||||
* does NOT prevent against length extension attacks.
|
||||
* Consider using ChaCha20-Poly1305 instead.
|
||||
*
|
||||
@@ -44,10 +45,8 @@
|
||||
* bzero(buf, 64);
|
||||
*
|
||||
*
|
||||
* Usage example 2: CSPRNG (cryptographically secure pseudo random number generator)
|
||||
* slowcrypt_chacha20 state[2];
|
||||
* uint32_t ctr = 1;
|
||||
* char buf[64];
|
||||
* Usage example 2: CSPRNG (cryptographically secure pseudo random number
|
||||
* generator) slowcrypt_chacha20 state[2]; uint32_t ctr = 1; char buf[64];
|
||||
*
|
||||
* while need random numbers {
|
||||
* slowcrypt_chacha20_init(state, key, block_ctr, nonce);
|
||||
@@ -66,21 +65,20 @@
|
||||
|
||||
/*
|
||||
* Copyright (c) 2025 Alexander Nutz
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
* of the Software, and to permit persons to whom the Software is furnished to do
|
||||
* so, subject to the following conditions:
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions: The above copyright
|
||||
* notice and this permission notice shall be included in all copies or
|
||||
* substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS",
|
||||
* WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
|
||||
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SLOWCRYPT_CHACHA20_H
|
||||
@@ -95,7 +93,8 @@
|
||||
#define SLOWCRYPT_CHACHA20_FUNC /**/
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
typedef struct
|
||||
{
|
||||
SLOWCRYPT_CHACHA20_UINT32 state[16];
|
||||
} slowcrypt_chacha20;
|
||||
|
||||
@@ -104,110 +103,134 @@ typedef struct {
|
||||
*
|
||||
* does NOT zeroize states! zeroize manually when done.
|
||||
*/
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_block(slowcrypt_chacha20 state[2], char const key[32],
|
||||
SLOWCRYPT_CHACHA20_UINT32 block_ctr, char const nonce[12],
|
||||
char data[64]);
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_block(
|
||||
slowcrypt_chacha20 state[2],
|
||||
char const key[32],
|
||||
SLOWCRYPT_CHACHA20_UINT32 block_ctr,
|
||||
char const nonce[12],
|
||||
char data[64]);
|
||||
|
||||
/* call this to zero out memory */
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_deinit(slowcrypt_chacha20 *state);
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_deinit(
|
||||
slowcrypt_chacha20* state);
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_init(slowcrypt_chacha20 *state,
|
||||
char const key[32],
|
||||
SLOWCRYPT_CHACHA20_UINT32 block_ctr,
|
||||
char const nonce[12]);
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_init(
|
||||
slowcrypt_chacha20* state,
|
||||
char const key[32],
|
||||
SLOWCRYPT_CHACHA20_UINT32 block_ctr,
|
||||
char const nonce[12]);
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_serialize(char buf[64], slowcrypt_chacha20 const *state);
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_serialize(
|
||||
char buf[64],
|
||||
slowcrypt_chacha20 const* state);
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_serialize_xor(char buf[64], slowcrypt_chacha20 const *state);
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_serialize_xor(
|
||||
char buf[64],
|
||||
slowcrypt_chacha20 const* state);
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_run(slowcrypt_chacha20 *state,
|
||||
slowcrypt_chacha20 *swap,
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_run(slowcrypt_chacha20* state,
|
||||
slowcrypt_chacha20* swap,
|
||||
int num_rounds);
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC SLOWCRYPT_CHACHA20_UINT32 slowcrypt_chacha20_read_ul32(char const *buf);
|
||||
SLOWCRYPT_CHACHA20_FUNC SLOWCRYPT_CHACHA20_UINT32
|
||||
slowcrypt_chacha20_read_ul32(char const* buf);
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_write_ul32(char *buf,
|
||||
SLOWCRYPT_CHACHA20_UINT32 val);
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_write_ul32(
|
||||
char* buf,
|
||||
SLOWCRYPT_CHACHA20_UINT32 val);
|
||||
|
||||
#define SLOWCRYPT_CHACHA20_LAST32(n, bits) (((SLOWCRYPT_CHACHA20_UINT32)(n)) >> (32 - (bits)))
|
||||
#define SLOWCRYPT_CHACHA20_LAST32(n, bits) \
|
||||
(((SLOWCRYPT_CHACHA20_UINT32)(n)) >> (32 - (bits)))
|
||||
|
||||
#define SLOWCRYPT_CHACHA20_ROL32(n, by) \
|
||||
((((SLOWCRYPT_CHACHA20_UINT32)(n)) << (by)) | SLOWCRYPT_CHACHA20_LAST32((n), (by)))
|
||||
#define SLOWCRYPT_CHACHA20_ROL32(n, by) \
|
||||
((((SLOWCRYPT_CHACHA20_UINT32)(n)) << (by)) | \
|
||||
SLOWCRYPT_CHACHA20_LAST32((n), (by)))
|
||||
|
||||
#define SLOWCRYPT_CHACHA20_QROUND(state, a, b, c, d) \
|
||||
do { \
|
||||
state[a] += state[b]; \
|
||||
state[d] ^= state[a]; \
|
||||
state[d] = SLOWCRYPT_CHACHA20_ROL32(state[d], 16); \
|
||||
\
|
||||
state[c] += state[d]; \
|
||||
state[b] ^= state[c]; \
|
||||
state[b] = SLOWCRYPT_CHACHA20_ROL32(state[b], 12); \
|
||||
\
|
||||
state[a] += state[b]; \
|
||||
state[d] ^= state[a]; \
|
||||
state[d] = SLOWCRYPT_CHACHA20_ROL32(state[d], 8); \
|
||||
\
|
||||
state[c] += state[d]; \
|
||||
state[b] ^= state[c]; \
|
||||
state[b] = SLOWCRYPT_CHACHA20_ROL32(state[b], 7); \
|
||||
#define SLOWCRYPT_CHACHA20_QROUND(state, a, b, c, d) \
|
||||
do { \
|
||||
state[a] += state[b]; \
|
||||
state[d] ^= state[a]; \
|
||||
state[d] = SLOWCRYPT_CHACHA20_ROL32(state[d], 16); \
|
||||
\
|
||||
state[c] += state[d]; \
|
||||
state[b] ^= state[c]; \
|
||||
state[b] = SLOWCRYPT_CHACHA20_ROL32(state[b], 12); \
|
||||
\
|
||||
state[a] += state[b]; \
|
||||
state[d] ^= state[a]; \
|
||||
state[d] = SLOWCRYPT_CHACHA20_ROL32(state[d], 8); \
|
||||
\
|
||||
state[c] += state[d]; \
|
||||
state[b] ^= state[c]; \
|
||||
state[b] = SLOWCRYPT_CHACHA20_ROL32(state[b], 7); \
|
||||
} while (0)
|
||||
|
||||
#ifdef SLOWCRYPT_CHACHA20_IMPL
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_deinit(slowcrypt_chacha20 *state) {
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_deinit(
|
||||
slowcrypt_chacha20* state)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < 16; i++)
|
||||
*(volatile int *)&state->state[i] = 0;
|
||||
*(volatile int*)&state->state[i] = 0;
|
||||
}
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC SLOWCRYPT_CHACHA20_UINT32 slowcrypt_chacha20_read_ul32(char const *buf) {
|
||||
SLOWCRYPT_CHACHA20_UINT32 o = (SLOWCRYPT_CHACHA20_UINT32)((uint8_t const *)buf)[0];
|
||||
o |= (SLOWCRYPT_CHACHA20_UINT32)((uint8_t const *)buf)[1] << 8;
|
||||
o |= (SLOWCRYPT_CHACHA20_UINT32)((uint8_t const *)buf)[2] << 16;
|
||||
o |= (SLOWCRYPT_CHACHA20_UINT32)((uint8_t const *)buf)[3] << 24;
|
||||
SLOWCRYPT_CHACHA20_FUNC SLOWCRYPT_CHACHA20_UINT32
|
||||
slowcrypt_chacha20_read_ul32(char const* buf)
|
||||
{
|
||||
SLOWCRYPT_CHACHA20_UINT32 o =
|
||||
(SLOWCRYPT_CHACHA20_UINT32)((uint8_t const*)buf)[0];
|
||||
o |= (SLOWCRYPT_CHACHA20_UINT32)((uint8_t const*)buf)[1] << 8;
|
||||
o |= (SLOWCRYPT_CHACHA20_UINT32)((uint8_t const*)buf)[2] << 16;
|
||||
o |= (SLOWCRYPT_CHACHA20_UINT32)((uint8_t const*)buf)[3] << 24;
|
||||
return o;
|
||||
}
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_write_ul32(char *buf,
|
||||
SLOWCRYPT_CHACHA20_UINT32 val) {
|
||||
((uint8_t *)buf)[0] = (uint8_t)(val & 0xFF);
|
||||
((uint8_t *)buf)[1] = (uint8_t)((val >> 8) & 0xFF);
|
||||
((uint8_t *)buf)[2] = (uint8_t)((val >> 16) & 0xFF);
|
||||
((uint8_t *)buf)[3] = (uint8_t)((val >> 24) & 0xFF);
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_write_ul32(
|
||||
char* buf,
|
||||
SLOWCRYPT_CHACHA20_UINT32 val)
|
||||
{
|
||||
((uint8_t*)buf)[0] = (uint8_t)(val & 0xFF);
|
||||
((uint8_t*)buf)[1] = (uint8_t)((val >> 8) & 0xFF);
|
||||
((uint8_t*)buf)[2] = (uint8_t)((val >> 16) & 0xFF);
|
||||
((uint8_t*)buf)[3] = (uint8_t)((val >> 24) & 0xFF);
|
||||
}
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_init(slowcrypt_chacha20 *state,
|
||||
char const key[32],
|
||||
SLOWCRYPT_CHACHA20_UINT32 block_ctr,
|
||||
char const nonce[12]) {
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_init(
|
||||
slowcrypt_chacha20* state,
|
||||
char const key[32],
|
||||
SLOWCRYPT_CHACHA20_UINT32 block_ctr,
|
||||
char const nonce[12])
|
||||
{
|
||||
int i;
|
||||
|
||||
state->state[0] = 0x61707865;
|
||||
state->state[1] = 0x3320646e;
|
||||
state->state[2] = 0x79622d32;
|
||||
state->state[3] = 0x6b206574;
|
||||
|
||||
for (i = 0; i < 8; i++)
|
||||
state->state[4 + i] = slowcrypt_chacha20_read_ul32(&key[i * 4]);
|
||||
|
||||
state->state[12] = block_ctr;
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
state->state[13 + i] = slowcrypt_chacha20_read_ul32(&nonce[i * 4]);
|
||||
}
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_serialize(char buf[64], slowcrypt_chacha20 const *state) {
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_serialize(
|
||||
char buf[64],
|
||||
slowcrypt_chacha20 const* state)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < 16; i++)
|
||||
slowcrypt_chacha20_write_ul32(&buf[i * 4], state->state[i]);
|
||||
}
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_serialize_xor(char buf[64],
|
||||
slowcrypt_chacha20 const *state) {
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_serialize_xor(
|
||||
char buf[64],
|
||||
slowcrypt_chacha20 const* state)
|
||||
{
|
||||
char swp[4];
|
||||
int i, j;
|
||||
|
||||
@@ -218,9 +241,10 @@ slowcrypt_chacha20_serialize_xor(char buf[64],
|
||||
}
|
||||
}
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_run(slowcrypt_chacha20 *state,
|
||||
slowcrypt_chacha20 *swap,
|
||||
int num_rounds) {
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_run(slowcrypt_chacha20* state,
|
||||
slowcrypt_chacha20* swap,
|
||||
int num_rounds)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 16; i++)
|
||||
@@ -246,10 +270,13 @@ SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_run(slowcrypt_chacha20 *state,
|
||||
state->state[i] += swap->state[i];
|
||||
}
|
||||
|
||||
SLOWCRYPT_CHACHA20_FUNC void
|
||||
slowcrypt_chacha20_block(slowcrypt_chacha20 state[2], char const key[32],
|
||||
SLOWCRYPT_CHACHA20_UINT32 block_ctr, char const nonce[12],
|
||||
char data[64]) {
|
||||
SLOWCRYPT_CHACHA20_FUNC void slowcrypt_chacha20_block(
|
||||
slowcrypt_chacha20 state[2],
|
||||
char const key[32],
|
||||
SLOWCRYPT_CHACHA20_UINT32 block_ctr,
|
||||
char const nonce[12],
|
||||
char data[64])
|
||||
{
|
||||
slowcrypt_chacha20_init(state, key, block_ctr, nonce);
|
||||
slowcrypt_chacha20_run(state, &state[1], 20);
|
||||
slowcrypt_chacha20_serialize_xor(data, state);
|
||||
|
@@ -28,7 +28,8 @@ static char expected[] = {
|
||||
0xF2, 0x78, 0x5E, 0x42, 0x87, 0x4D,
|
||||
};
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
slowcrypt_chacha20 state[2];
|
||||
char buf[64];
|
||||
int i, j, blksz;
|
||||
|
98
slowarr.h
98
slowarr.h
@@ -68,11 +68,11 @@
|
||||
#ifndef SLOWARR_ON_MALLOC_FAIL
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define SLOWARR_ON_MALLOC_FAIL(nb) \
|
||||
do { \
|
||||
fprintf(stderr, "\nmemory allocation of %lu bytes failed!\n", \
|
||||
(unsigned long)nb); \
|
||||
exit(1); \
|
||||
#define SLOWARR_ON_MALLOC_FAIL(nb) \
|
||||
do { \
|
||||
fprintf(stderr, "\nmemory allocation of %lu bytes failed!\n", \
|
||||
(unsigned long)nb); \
|
||||
exit(1); \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
@@ -80,17 +80,37 @@
|
||||
#define SLOWARR__ZEROIZE (1 << 1) /** for cryptography */
|
||||
|
||||
#ifdef __cplusplus
|
||||
template <typename T> struct SLOWARR_CXXT {};
|
||||
template <typename T>
|
||||
struct SLOWARR_CXXT
|
||||
{
|
||||
};
|
||||
|
||||
#define SLOWARR_CXX_HEADER(T) \
|
||||
template <> struct SLOWARR_CXXT<T> { \
|
||||
SLOWARR_MANGLE(T) arr; \
|
||||
\
|
||||
T *begin() { return arr.data; } \
|
||||
T *cbegin() const { return arr.data; } \
|
||||
T *cend() const { return arr.data + arr.len; } \
|
||||
T *end() { return cend(); } \
|
||||
SLOWARR_SZT length() const { return arr.len; } \
|
||||
#define SLOWARR_CXX_HEADER(T) \
|
||||
template <> \
|
||||
struct SLOWARR_CXXT<T> \
|
||||
{ \
|
||||
SLOWARR_MANGLE(T) arr; \
|
||||
\
|
||||
T* begin() \
|
||||
{ \
|
||||
return arr.data; \
|
||||
} \
|
||||
T* cbegin() const \
|
||||
{ \
|
||||
return arr.data; \
|
||||
} \
|
||||
T* cend() const \
|
||||
{ \
|
||||
return arr.data + arr.len; \
|
||||
} \
|
||||
T* end() \
|
||||
{ \
|
||||
return cend(); \
|
||||
} \
|
||||
SLOWARR_SZT length() const \
|
||||
{ \
|
||||
return arr.len; \
|
||||
} \
|
||||
};
|
||||
#else
|
||||
#define SLOWARR_CXX_HEADER(T) /**/
|
||||
@@ -110,9 +130,10 @@ static void SLOWARR___REQUIRE_SEMI(void) {}
|
||||
#define SLOWARR_Header(T) \
|
||||
SLOWARR_BEGINC \
|
||||
/** usage: T(Arr,int) myarr = {0}; */ \
|
||||
typedef struct { \
|
||||
typedef struct \
|
||||
{ \
|
||||
SLOWARR_SZT cap, len; \
|
||||
T *data; \
|
||||
T* data; \
|
||||
unsigned char attr; \
|
||||
} SLOWARR_MANGLE(T); \
|
||||
SLOWARR_ENDC \
|
||||
@@ -135,7 +156,7 @@ static void SLOWARR___REQUIRE_SEMI(void) {}
|
||||
SLOWARR_FUNC void SLOWARR_MANGLE_F(T, reserveTotal)(SLOWARR_MANGLE(T) * arr, \
|
||||
SLOWARR_SZT num); \
|
||||
\
|
||||
SLOWARR_FUNC T *SLOWARR_MANGLE_F(T, pushRef)(SLOWARR_MANGLE(T) * arr); \
|
||||
SLOWARR_FUNC T* SLOWARR_MANGLE_F(T, pushRef)(SLOWARR_MANGLE(T) * arr); \
|
||||
\
|
||||
SLOWARR_FUNC void SLOWARR_MANGLE_F(T, push)(SLOWARR_MANGLE(T) * arr, T val); \
|
||||
\
|
||||
@@ -154,7 +175,8 @@ static void SLOWARR___REQUIRE_SEMI(void) {}
|
||||
\
|
||||
/** result can not be reallocated */ \
|
||||
SLOWARR_FUNC SLOWARR_MANGLE(T) \
|
||||
SLOWARR_MANGLE_F(T, borrow)(T * data, SLOWARR_SZT sz) { \
|
||||
SLOWARR_MANGLE_F(T, borrow)(T * data, SLOWARR_SZT sz) \
|
||||
{ \
|
||||
SLOWARR_MANGLE(T) arr; \
|
||||
arr.data = data; \
|
||||
arr.cap = sz; \
|
||||
@@ -163,18 +185,19 @@ static void SLOWARR___REQUIRE_SEMI(void) {}
|
||||
return arr; \
|
||||
} \
|
||||
\
|
||||
SLOWARR_FUNC void SLOWARR_MANGLE_F(T, \
|
||||
unsafeClear)(SLOWARR_MANGLE(T) * arr) { \
|
||||
SLOWARR_FUNC void SLOWARR_MANGLE_F(T, unsafeClear)(SLOWARR_MANGLE(T) * arr) \
|
||||
{ \
|
||||
if (arr->data) \
|
||||
SLOWARR_FREE(arr->data, arr->cap); \
|
||||
arr->data = (T *)(void *)0; \
|
||||
arr->data = (T*)(void*)0; \
|
||||
arr->cap = 0; \
|
||||
arr->len = 0; \
|
||||
/* don't change attrs */ \
|
||||
} \
|
||||
\
|
||||
/* TODO: could make this align the cap num for better perf */ \
|
||||
SLOWARR_FUNC void SLOWARR_MANGLE_F(T, shrink)(SLOWARR_MANGLE(T) * arr) { \
|
||||
SLOWARR_FUNC void SLOWARR_MANGLE_F(T, shrink)(SLOWARR_MANGLE(T) * arr) \
|
||||
{ \
|
||||
if (arr->attr & SLOWARR__BORROWED) \
|
||||
return; \
|
||||
\
|
||||
@@ -183,15 +206,15 @@ static void SLOWARR___REQUIRE_SEMI(void) {}
|
||||
if (arr->attr & SLOWARR__ZEROIZE) \
|
||||
SLOWARR_MEMZERO(arr->data + arr->len, \
|
||||
(arr->cap - arr->len) * sizeof(T)); \
|
||||
arr->data = (T *)SLOWARR_REALLOC(arr->data, arr->cap * sizeof(T), \
|
||||
arr->len * sizeof(T)); \
|
||||
arr->data = (T*)SLOWARR_REALLOC(arr->data, arr->cap * sizeof(T), \
|
||||
arr->len * sizeof(T)); \
|
||||
arr->cap = arr->len; \
|
||||
} \
|
||||
\
|
||||
SLOWARR_FUNC void SLOWARR_MANGLE_F(T, reserveTotal)(SLOWARR_MANGLE(T) * arr, \
|
||||
SLOWARR_SZT num) { \
|
||||
\
|
||||
void *n; \
|
||||
SLOWARR_SZT num) \
|
||||
{ \
|
||||
void* n; \
|
||||
if (num <= arr->cap) \
|
||||
return; \
|
||||
SLOWARR_ASSERT_USER_ERROR(!(arr->attr & SLOWARR__BORROWED)); \
|
||||
@@ -202,27 +225,29 @@ static void SLOWARR___REQUIRE_SEMI(void) {}
|
||||
SLOWARR_FREE(arr->data, arr->cap * sizeof(T)); \
|
||||
SLOWARR_ON_MALLOC_FAIL(sizeof(T) * num); \
|
||||
} \
|
||||
arr->data = (T *)n; \
|
||||
arr->data = (T*)n; \
|
||||
arr->cap = num; \
|
||||
} \
|
||||
\
|
||||
SLOWARR_FUNC T *SLOWARR_MANGLE_F(T, pushRef)(SLOWARR_MANGLE(T) * arr) { \
|
||||
SLOWARR_FUNC T* SLOWARR_MANGLE_F(T, pushRef)(SLOWARR_MANGLE(T) * arr) \
|
||||
{ \
|
||||
if (arr->cap == 0) { \
|
||||
SLOWARR_MANGLE_F(T, reserveTotal)(arr, SLOWARR_CAP_FOR_FIRST_ELEM(T)); \
|
||||
} else if (arr->len + 1 > arr->cap) { \
|
||||
SLOWARR_MANGLE_F(T, reserveTotal)(arr, \
|
||||
SLOWARR_GROWTH_RATE(T, arr->len)); \
|
||||
SLOWARR_MANGLE_F(T, reserveTotal) \
|
||||
(arr, SLOWARR_GROWTH_RATE(T, arr->len)); \
|
||||
} \
|
||||
return &arr->data[arr->len++]; \
|
||||
} \
|
||||
\
|
||||
SLOWARR_FUNC void SLOWARR_MANGLE_F(T, push)(SLOWARR_MANGLE(T) * arr, \
|
||||
T val) { \
|
||||
SLOWARR_FUNC void SLOWARR_MANGLE_F(T, push)(SLOWARR_MANGLE(T) * arr, T val) \
|
||||
{ \
|
||||
*SLOWARR_MANGLE_F(T, pushRef)(arr) = val; \
|
||||
} \
|
||||
\
|
||||
SLOWARR_FUNC void SLOWARR_MANGLE_F(T, remove)(SLOWARR_MANGLE(T) * arr, \
|
||||
T * out, SLOWARR_SZT i) { \
|
||||
T * out, SLOWARR_SZT i) \
|
||||
{ \
|
||||
SLOWARR_SZT too_much; \
|
||||
SLOWARR_ASSERT_USER_ERROR(i < arr->len); \
|
||||
*out = arr->data[i]; \
|
||||
@@ -235,7 +260,8 @@ static void SLOWARR___REQUIRE_SEMI(void) {}
|
||||
} \
|
||||
} \
|
||||
\
|
||||
SLOWARR_FUNC T SLOWARR_MANGLE_F(T, pop)(SLOWARR_MANGLE(T) * arr) { \
|
||||
SLOWARR_FUNC T SLOWARR_MANGLE_F(T, pop)(SLOWARR_MANGLE(T) * arr) \
|
||||
{ \
|
||||
T temp; \
|
||||
SLOWARR_MANGLE_F(T, remove)(arr, &temp, arr->len - 1); \
|
||||
return temp; \
|
||||
|
@@ -1,5 +1,6 @@
|
||||
void c_memzero(void *ptrin, unsigned len) {
|
||||
char *ptr = ptrin;
|
||||
void c_memzero(void* ptrin, unsigned len)
|
||||
{
|
||||
char* ptr = ptrin;
|
||||
for (; len;) {
|
||||
*ptr = 0;
|
||||
len -= 1;
|
||||
@@ -7,9 +8,10 @@ void c_memzero(void *ptrin, unsigned len) {
|
||||
}
|
||||
}
|
||||
|
||||
void c_memmove(void *destp, void *srcp, unsigned len) {
|
||||
char *dest = destp;
|
||||
char *src = srcp;
|
||||
void c_memmove(void* destp, void* srcp, unsigned len)
|
||||
{
|
||||
char* dest = destp;
|
||||
char* src = srcp;
|
||||
for (; len;) {
|
||||
*dest = *src;
|
||||
len -= 1;
|
||||
@@ -22,17 +24,18 @@ void c_memmove(void *destp, void *srcp, unsigned len) {
|
||||
#define SLOWARR_MEMZERO(ptr, len) c_memzero(ptr, len)
|
||||
#define SLOWARR_MEMMOVE(dest, src, ln) c_memmove(dest, src, ln)
|
||||
#define SLOWARR_SZT unsigned long
|
||||
#define SLOWARR_REALLOC(ptr, old, news) ((void *)0)
|
||||
#define SLOWARR_REALLOC(ptr, old, news) ((void*)0)
|
||||
#define SLOWARR_FREE(ptr, size) /**/
|
||||
#define SLOWARR_ON_MALLOC_FAIL(x) /**/
|
||||
#define SLOW_DEFINE_ACCESS
|
||||
#include "../slowarr.h"
|
||||
|
||||
typedef char const *cstr;
|
||||
typedef char const* cstr;
|
||||
SLOWARR_Header(cstr);
|
||||
SLOWARR_Impl(cstr);
|
||||
|
||||
int main(int argc, char const **argv) {
|
||||
int main(int argc, char const** argv)
|
||||
{
|
||||
T(SLOWARR, cstr) arr = F(SLOWARR, cstr, borrow)(argv, argc);
|
||||
(void)arr;
|
||||
return 0;
|
||||
|
@@ -1,11 +1,12 @@
|
||||
#define SLOW_DEFINE_ACCESS
|
||||
#include "../slowarr.h"
|
||||
|
||||
typedef char const *cstr;
|
||||
typedef char const* cstr;
|
||||
SLOWARR_Header(cstr);
|
||||
SLOWARR_Impl(cstr);
|
||||
|
||||
int main(int argc, char const **argv) {
|
||||
int main(int argc, char const** argv)
|
||||
{
|
||||
T(SLOWARR, cstr) arr = F(SLOWARR, cstr, borrow)(argv, argc);
|
||||
(void)arr;
|
||||
return 0;
|
||||
|
@@ -2,11 +2,12 @@
|
||||
#define SLOW_DEFINE_ACCESS
|
||||
#include "../slowarr.h"
|
||||
|
||||
typedef char const *cstr;
|
||||
typedef char const* cstr;
|
||||
SLOWARR_Header(cstr);
|
||||
SLOWARR_Impl(cstr);
|
||||
|
||||
int main(int argc, char const **argv) {
|
||||
int main(int argc, char const** argv)
|
||||
{
|
||||
T(SLOWARR, cstr) arr = F(SLOWARR, cstr, borrow)(argv, argc);
|
||||
SlowArr<cstr> arrx = {arr};
|
||||
(void)arr;
|
||||
|
176
slowgraph.h
176
slowgraph.h
@@ -20,46 +20,52 @@ typedef struct SlowGraphNode SlowGraphNode;
|
||||
typedef struct SlowGraphEdge SlowGraphEdge;
|
||||
typedef struct SlowGraphAttr SlowGraphAttr;
|
||||
|
||||
struct SlowGraphAttr {
|
||||
SlowGraphAttr *next;
|
||||
struct SlowGraphAttr
|
||||
{
|
||||
SlowGraphAttr* next;
|
||||
unsigned hash;
|
||||
char *key, *val;
|
||||
};
|
||||
|
||||
struct SlowGraphNode {
|
||||
SlowGraphNode *next;
|
||||
struct SlowGraphNode
|
||||
{
|
||||
SlowGraphNode* next;
|
||||
|
||||
unsigned name_hash;
|
||||
char *name;
|
||||
SlowGraphAttr *attr;
|
||||
SlowGraphEdge *children;
|
||||
char* name;
|
||||
SlowGraphAttr* attr;
|
||||
SlowGraphEdge* children;
|
||||
|
||||
unsigned gc_used : 1;
|
||||
};
|
||||
|
||||
struct SlowGraphEdge {
|
||||
SlowGraphEdge *next;
|
||||
SlowGraphAttr *attr;
|
||||
struct SlowGraphEdge
|
||||
{
|
||||
SlowGraphEdge* next;
|
||||
SlowGraphAttr* attr;
|
||||
|
||||
SlowGraphNode *node;
|
||||
SlowGraphNode* node;
|
||||
};
|
||||
|
||||
struct SlowGraph {
|
||||
SlowGraphNode *first;
|
||||
struct SlowGraph
|
||||
{
|
||||
SlowGraphNode* first;
|
||||
};
|
||||
|
||||
SLOWGRAPH_FUNC void SlowGraphAttr_free(SlowGraphAttr *attrs);
|
||||
SLOWGRAPH_FUNC void SlowGraphAttr_free(SlowGraphAttr* attrs);
|
||||
|
||||
SLOWGRAPH_FUNC unsigned slowgraph_hash(char const *buf);
|
||||
SLOWGRAPH_FUNC unsigned slowgraph_hash(char const* buf);
|
||||
|
||||
SLOWGRAPH_FUNC unsigned slowgraph_hashn(void const *bufp, int len);
|
||||
SLOWGRAPH_FUNC unsigned slowgraph_hashn(void const* bufp, int len);
|
||||
|
||||
SLOWGRAPH_FUNC char *
|
||||
slowgraph_next_edge(FILE *inp, char *buf, int bufn,
|
||||
void (*opt_attr_clbk)(char *node, char *key, char *val),
|
||||
void (*opt_last_edge_attr_clbk)(char *key, char *val));
|
||||
SLOWGRAPH_FUNC char* slowgraph_next_edge(
|
||||
FILE* inp,
|
||||
char* buf,
|
||||
int bufn,
|
||||
void (*opt_attr_clbk)(char* node, char* key, char* val),
|
||||
void (*opt_last_edge_attr_clbk)(char* key, char* val));
|
||||
|
||||
SLOWGRAPH_FUNC SlowGraphAttr *SlowGraphAttr_find(SlowGraphAttr *a,
|
||||
SLOWGRAPH_FUNC SlowGraphAttr* SlowGraphAttr_find(SlowGraphAttr* a,
|
||||
unsigned hash);
|
||||
|
||||
/*
|
||||
@@ -70,30 +76,32 @@ SLOWGRAPH_FUNC SlowGraphAttr *SlowGraphAttr_find(SlowGraphAttr *a,
|
||||
Step 3: call SlowGraph_gcUnused(graph) // only deletes nodes not used by
|
||||
manually marked used nodes
|
||||
*/
|
||||
SLOWGRAPH_FUNC void SlowGraph_markAllUnused(SlowGraph *graph);
|
||||
SLOWGRAPH_FUNC void SlowGraph_gcUnused(SlowGraph *graph);
|
||||
SLOWGRAPH_FUNC void SlowGraph_markAllUnused(SlowGraph* graph);
|
||||
SLOWGRAPH_FUNC void SlowGraph_gcUnused(SlowGraph* graph);
|
||||
|
||||
SLOWGRAPH_FUNC SlowGraphNode *SlowGraph_find(SlowGraph *g, unsigned name_hash);
|
||||
SLOWGRAPH_FUNC SlowGraphNode* SlowGraph_find(SlowGraph* g, unsigned name_hash);
|
||||
|
||||
SLOWGRAPH_FUNC SlowGraphEdge *SlowGraphNode_findConnection(SlowGraphNode *from,
|
||||
SlowGraphNode *to);
|
||||
SLOWGRAPH_FUNC SlowGraphEdge* SlowGraphNode_findConnection(SlowGraphNode* from,
|
||||
SlowGraphNode* to);
|
||||
|
||||
/* if edge from->to already exists, return it instead */
|
||||
SLOWGRAPH_FUNC SlowGraphEdge *SlowGraphNode_connect(SlowGraphNode *from,
|
||||
SlowGraphNode *to);
|
||||
SLOWGRAPH_FUNC SlowGraphEdge* SlowGraphNode_connect(SlowGraphNode* from,
|
||||
SlowGraphNode* to);
|
||||
|
||||
SLOWGRAPH_FUNC SlowGraphNode *SlowGraph_getOrCreate(SlowGraph *g,
|
||||
char const *name);
|
||||
SLOWGRAPH_FUNC SlowGraphNode* SlowGraph_getOrCreate(SlowGraph* g,
|
||||
char const* name);
|
||||
|
||||
SLOWGRAPH_FUNC void slowgraph_setAttr(SlowGraphAttr **list, char const *key,
|
||||
char const *val);
|
||||
SLOWGRAPH_FUNC void slowgraph_setAttr(SlowGraphAttr** list,
|
||||
char const* key,
|
||||
char const* val);
|
||||
|
||||
/* returns 1 on failure */
|
||||
SLOWGRAPH_FUNC int SlowGraph_readDGTXT(SlowGraph *g, FILE *f);
|
||||
SLOWGRAPH_FUNC int SlowGraph_readDGTXT(SlowGraph* g, FILE* f);
|
||||
|
||||
#ifdef SLOWGRAPH_IMPL
|
||||
SLOWGRAPH_FUNC void SlowGraphAttr_free(SlowGraphAttr *attrs) {
|
||||
SlowGraphAttr *to_free;
|
||||
SLOWGRAPH_FUNC void SlowGraphAttr_free(SlowGraphAttr* attrs)
|
||||
{
|
||||
SlowGraphAttr* to_free;
|
||||
for (; attrs;) {
|
||||
to_free = attrs;
|
||||
attrs = attrs->next;
|
||||
@@ -103,7 +111,8 @@ SLOWGRAPH_FUNC void SlowGraphAttr_free(SlowGraphAttr *attrs) {
|
||||
}
|
||||
}
|
||||
|
||||
SLOWGRAPH_FUNC unsigned slowgraph_hash(char const *buf) {
|
||||
SLOWGRAPH_FUNC unsigned slowgraph_hash(char const* buf)
|
||||
{
|
||||
unsigned res = 5381;
|
||||
for (; *buf; buf++) {
|
||||
res = (res << 5) + res + *buf;
|
||||
@@ -111,19 +120,23 @@ SLOWGRAPH_FUNC unsigned slowgraph_hash(char const *buf) {
|
||||
return res;
|
||||
}
|
||||
|
||||
SLOWGRAPH_FUNC unsigned slowgraph_hashn(void const *bufp, int len) {
|
||||
char const *buf = (char const *)bufp;
|
||||
SLOWGRAPH_FUNC unsigned slowgraph_hashn(void const* bufp, int len)
|
||||
{
|
||||
char const* buf = (char const*)bufp;
|
||||
unsigned res = 5381;
|
||||
for (; buf != ((char const *)bufp) + len; buf++) {
|
||||
for (; buf != ((char const*)bufp) + len; buf++) {
|
||||
res = (res << 5) + res + *buf;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
SLOWGRAPH_FUNC char *
|
||||
slowgraph_next_edge(FILE *inp, char *buf, int bufn,
|
||||
void (*opt_attr_clbk)(char *node, char *key, char *val),
|
||||
void (*opt_last_edge_attr_clbk)(char *key, char *val)) {
|
||||
SLOWGRAPH_FUNC char* slowgraph_next_edge(
|
||||
FILE* inp,
|
||||
char* buf,
|
||||
int bufn,
|
||||
void (*opt_attr_clbk)(char* node, char* key, char* val),
|
||||
void (*opt_last_edge_attr_clbk)(char* key, char* val))
|
||||
{
|
||||
int c;
|
||||
char *n, *k, *v, *retv = 0;
|
||||
|
||||
@@ -188,21 +201,24 @@ slowgraph_next_edge(FILE *inp, char *buf, int bufn,
|
||||
return retv;
|
||||
}
|
||||
|
||||
SLOWGRAPH_FUNC SlowGraphAttr *SlowGraphAttr_find(SlowGraphAttr *a,
|
||||
unsigned hash) {
|
||||
SLOWGRAPH_FUNC SlowGraphAttr* SlowGraphAttr_find(SlowGraphAttr* a,
|
||||
unsigned hash)
|
||||
{
|
||||
for (; a; a = a->next)
|
||||
if (a->hash == hash)
|
||||
return a;
|
||||
return 0;
|
||||
}
|
||||
|
||||
SLOWGRAPH_FUNC void SlowGraph_markAllUnused(SlowGraph *graph) {
|
||||
SlowGraphNode *n;
|
||||
SLOWGRAPH_FUNC void SlowGraph_markAllUnused(SlowGraph* graph)
|
||||
{
|
||||
SlowGraphNode* n;
|
||||
for (n = graph->first; n; n = n->next)
|
||||
n->gc_used = 0;
|
||||
}
|
||||
|
||||
SLOWGRAPH_FUNC void SlowGraph_gcUnused(SlowGraph *graph) {
|
||||
SLOWGRAPH_FUNC void SlowGraph_gcUnused(SlowGraph* graph)
|
||||
{
|
||||
int changed = 0;
|
||||
SlowGraphNode *n, *o, **prev;
|
||||
SlowGraphEdge *e, *oe;
|
||||
@@ -244,29 +260,32 @@ SLOWGRAPH_FUNC void SlowGraph_gcUnused(SlowGraph *graph) {
|
||||
}
|
||||
}
|
||||
|
||||
SLOWGRAPH_FUNC SlowGraphNode *SlowGraph_find(SlowGraph *g, unsigned name_hash) {
|
||||
SlowGraphNode *n;
|
||||
SLOWGRAPH_FUNC SlowGraphNode* SlowGraph_find(SlowGraph* g, unsigned name_hash)
|
||||
{
|
||||
SlowGraphNode* n;
|
||||
for (n = g->first; n; n = n->next)
|
||||
if (n->name_hash == name_hash)
|
||||
return n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
SLOWGRAPH_FUNC SlowGraphEdge *SlowGraphNode_findConnection(SlowGraphNode *from,
|
||||
SlowGraphNode *to) {
|
||||
SlowGraphEdge *e;
|
||||
SLOWGRAPH_FUNC SlowGraphEdge* SlowGraphNode_findConnection(SlowGraphNode* from,
|
||||
SlowGraphNode* to)
|
||||
{
|
||||
SlowGraphEdge* e;
|
||||
for (e = from->children; e; e = e->next)
|
||||
if (e->node == to)
|
||||
return e;
|
||||
return 0;
|
||||
}
|
||||
|
||||
SLOWGRAPH_FUNC SlowGraphEdge *SlowGraphNode_connect(SlowGraphNode *from,
|
||||
SlowGraphNode *to) {
|
||||
SlowGraphEdge *e = SlowGraphNode_findConnection(from, to);
|
||||
SLOWGRAPH_FUNC SlowGraphEdge* SlowGraphNode_connect(SlowGraphNode* from,
|
||||
SlowGraphNode* to)
|
||||
{
|
||||
SlowGraphEdge* e = SlowGraphNode_findConnection(from, to);
|
||||
if (e)
|
||||
return e;
|
||||
e = (SlowGraphEdge *)calloc(1, sizeof(SlowGraphEdge));
|
||||
e = (SlowGraphEdge*)calloc(1, sizeof(SlowGraphEdge));
|
||||
if (!e)
|
||||
return 0;
|
||||
e->node = to;
|
||||
@@ -275,19 +294,20 @@ SLOWGRAPH_FUNC SlowGraphEdge *SlowGraphNode_connect(SlowGraphNode *from,
|
||||
return e;
|
||||
}
|
||||
|
||||
SLOWGRAPH_FUNC SlowGraphNode *SlowGraph_getOrCreate(SlowGraph *g,
|
||||
char const *name) {
|
||||
SLOWGRAPH_FUNC SlowGraphNode* SlowGraph_getOrCreate(SlowGraph* g,
|
||||
char const* name)
|
||||
{
|
||||
unsigned hash, strln;
|
||||
SlowGraphNode *n;
|
||||
SlowGraphNode* n;
|
||||
|
||||
strln = strlen(name);
|
||||
hash = slowgraph_hashn(name, strln);
|
||||
if ((n = SlowGraph_find(g, hash)))
|
||||
return n;
|
||||
n = (SlowGraphNode *)calloc(1, sizeof(SlowGraphNode));
|
||||
n = (SlowGraphNode*)calloc(1, sizeof(SlowGraphNode));
|
||||
if (!n)
|
||||
return 0;
|
||||
n->name = (char *)calloc(strln + 1, sizeof(char));
|
||||
n->name = (char*)calloc(strln + 1, sizeof(char));
|
||||
if (!n->name) {
|
||||
free(n);
|
||||
return 0;
|
||||
@@ -299,49 +319,54 @@ SLOWGRAPH_FUNC SlowGraphNode *SlowGraph_getOrCreate(SlowGraph *g,
|
||||
return n;
|
||||
}
|
||||
|
||||
SLOWGRAPH_FUNC void slowgraph_setAttr(SlowGraphAttr **list, char const *key,
|
||||
char const *val) {
|
||||
SLOWGRAPH_FUNC void slowgraph_setAttr(SlowGraphAttr** list,
|
||||
char const* key,
|
||||
char const* val)
|
||||
{
|
||||
/* TODO: malloc fail handking */
|
||||
unsigned keylen = strlen(key);
|
||||
unsigned vallen = strlen(val);
|
||||
unsigned hash = slowgraph_hashn(key, keylen);
|
||||
SlowGraphAttr *a = SlowGraphAttr_find(*list, hash);
|
||||
SlowGraphAttr* a = SlowGraphAttr_find(*list, hash);
|
||||
if (a) {
|
||||
free(a->val);
|
||||
} else {
|
||||
a = (SlowGraphAttr *)calloc(1, sizeof(SlowGraphAttr));
|
||||
a = (SlowGraphAttr*)calloc(1, sizeof(SlowGraphAttr));
|
||||
a->next = *list;
|
||||
*list = a;
|
||||
|
||||
a->hash = hash;
|
||||
a->key = (char *)calloc(keylen + 1, sizeof(char));
|
||||
a->key = (char*)calloc(keylen + 1, sizeof(char));
|
||||
strcpy(a->key, key);
|
||||
}
|
||||
|
||||
a->val = (char *)calloc(vallen + 1, sizeof(char));
|
||||
a->val = (char*)calloc(vallen + 1, sizeof(char));
|
||||
strcpy(a->val, val);
|
||||
}
|
||||
|
||||
static SlowGraph *SlowGraph_read__graph;
|
||||
static SlowGraphEdge *SlowGraph_read__lastEdge;
|
||||
static SlowGraph* SlowGraph_read__graph;
|
||||
static SlowGraphEdge* SlowGraph_read__lastEdge;
|
||||
|
||||
static void SlowGraph_read__nodeAttr(char *node, char *key, char *val) {
|
||||
SlowGraphNode *n = SlowGraph_getOrCreate(SlowGraph_read__graph, node);
|
||||
static void SlowGraph_read__nodeAttr(char* node, char* key, char* val)
|
||||
{
|
||||
SlowGraphNode* n = SlowGraph_getOrCreate(SlowGraph_read__graph, node);
|
||||
if (!n)
|
||||
return;
|
||||
slowgraph_setAttr(&n->attr, key, val);
|
||||
}
|
||||
|
||||
static void SlowGraph_read__edgeAttr(char *key, char *val) {
|
||||
static void SlowGraph_read__edgeAttr(char* key, char* val)
|
||||
{
|
||||
if (!SlowGraph_read__lastEdge)
|
||||
return;
|
||||
slowgraph_setAttr(&SlowGraph_read__lastEdge->attr, key, val);
|
||||
}
|
||||
|
||||
/* returns 1 on failure */
|
||||
SLOWGRAPH_FUNC int SlowGraph_readDGTXT(SlowGraph *g, FILE *f) {
|
||||
SLOWGRAPH_FUNC int SlowGraph_readDGTXT(SlowGraph* g, FILE* f)
|
||||
{
|
||||
static char buf[512];
|
||||
char *dest;
|
||||
char* dest;
|
||||
|
||||
SlowGraph_read__graph = g;
|
||||
SlowGraph_read__lastEdge = 0;
|
||||
@@ -349,7 +374,6 @@ SLOWGRAPH_FUNC int SlowGraph_readDGTXT(SlowGraph *g, FILE *f) {
|
||||
while ((dest = slowgraph_next_edge(f, buf, SLOWGRAPH_LENOF(buf),
|
||||
SlowGraph_read__nodeAttr,
|
||||
SlowGraph_read__edgeAttr))) {
|
||||
|
||||
SlowGraph_read__lastEdge = SlowGraphNode_connect(
|
||||
SlowGraph_getOrCreate(g, buf), SlowGraph_getOrCreate(g, dest));
|
||||
if (!SlowGraph_read__lastEdge)
|
||||
|
@@ -1,8 +1,9 @@
|
||||
#define SLOWGRAPH_IMPL
|
||||
#include "../slowgraph.h"
|
||||
|
||||
static void write_csv_field(char const *s) {
|
||||
char const *y;
|
||||
static void write_csv_field(char const* s)
|
||||
{
|
||||
char const* y;
|
||||
int quote = 0;
|
||||
for (y = s; *y; y++) {
|
||||
if (*y == '\n' || *y == '"') {
|
||||
@@ -24,8 +25,9 @@ static void write_csv_field(char const *s) {
|
||||
putchar('"');
|
||||
}
|
||||
|
||||
static void dump_adj_matrix_row(SlowGraph *g, SlowGraphNode *row) {
|
||||
SlowGraphNode *col;
|
||||
static void dump_adj_matrix_row(SlowGraph* g, SlowGraphNode* row)
|
||||
{
|
||||
SlowGraphNode* col;
|
||||
write_csv_field(row->name);
|
||||
printf(",");
|
||||
for (col = g->first; col; col = col->next) {
|
||||
@@ -38,8 +40,9 @@ static void dump_adj_matrix_row(SlowGraph *g, SlowGraphNode *row) {
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void dump_adj_matrix(SlowGraph *g) {
|
||||
SlowGraphNode *n;
|
||||
static void dump_adj_matrix(SlowGraph* g)
|
||||
{
|
||||
SlowGraphNode* n;
|
||||
printf(",");
|
||||
for (n = g->first; n; n = n->next) {
|
||||
if (n != g->first)
|
||||
@@ -52,7 +55,8 @@ static void dump_adj_matrix(SlowGraph *g) {
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
SlowGraph graph = {0};
|
||||
|
||||
if (SlowGraph_readDGTXT(&graph, stdin))
|
||||
|
Reference in New Issue
Block a user