2 # Contributing to Proxmark3
4 :+1::tada: First off, thanks for taking the time to contribute! :tada::+1:
6 This guide covers mostly coding style for submitting pull requests, but you can also contribute by [Reporting Bugs](https://github.com/RfidResearchGroup/proxmark3/issues) and [Suggesting Enhancements](https://github.com/RfidResearchGroup/proxmark3/issues) after having carefully checked that a corresponding issue doesn't exist yet.
8 Beware we're all very busy so the best way is by providing yourself some fixes and enhancements via [Pull Requests](https://github.com/RfidResearchGroup/proxmark3/pulls) respecting the following coding style.
10 [Styleguides](#styleguides)
11 * [Overview](#overview)
12 * [Indentation](#indentation)
15 * [Identifiers](#identifiers)
16 * [Data types](#data-types)
17 * [Expressions](#expressions)
18 * [If / for / while etc](#if-for-while-etc)
19 * [Functions](#fonctions)
20 * [Structs / unions / enums](#structs-unions-enums)
22 * [Comments](#comments)
24 * [File headers](#file-headers)
25 * [Header files](#header-files)
26 * [Whitespace](#whitespace)
30 _"Coding styles are like assholes, everyone has one and no one likes anyone elses."_
35 We have established a set of coding style guidelines in order to
36 clean up the code consistently and keep it consistent in the future.
37 Look around and respect the same style.
39 Helper script to get some uniformity in the style:
45 It makes use of `astyle` so be sure to install it first.
50 Don't use tabs, editors are messing them up too easily.
51 Increment unit is four spaces.
53 If you use `make style`, this will be done for you.
57 Try to keep lines to a reasonable length. 80 characters is a good mark; using an
58 editor that shows a vertical line is a great idea. However, don't break a line
59 just because you're slightly over, it's not worth it. No 200-character lines,
64 `#define`, function-like or not, are all UPPERCASE unless you're emulating a
65 well-known function name.
69 Functions, local variables, and arguments are all named using
70 `underscores_as_spaces`. Global variables are Evil and are prepended with `g_` to
71 distinguish them. Avoid them.
73 Single-character variables are a bad idea. Exceptions: loop iterators and maybe
74 simple byte pointers (`*p`) in very obvious places. If you have more than one
75 such pointer, use a real name. If you have more than a couple nested loops,
76 complex logic, or indices that differ in interpretation or purpose, use real
77 names instead of i,j,k.
81 Use `stdint.h` types (`uint32_t` and friends) unless you have a reason not to. Don't
82 use microsoft-style `DWORD` and the like, we're getting rid of those. Avoid char
83 for buffers, `uint8_t` is more obvious when you're not working with strings. Use
84 `const` where things are const. Try to use `size_t` for sizes.
86 Pointers and reference operators are attached to the variable name:
94 otherwise you're tempted to write:
100 `make style` will take care of pointers & reference operators.
104 In general, use whitespace around binary operators - no unspaced blobs of an
105 expression. `make style` will take care of whitespaces around operators.
109 if (5 * a < b && some_bool_var)
113 if (5*a<b&&some_bool_var)
115 For equality with constants, use `i == 0xF00`, not `0xF00 == i`. The compiler warns
116 you about `=` vs `==` anyway, and you shouldn't be screwing that one up by now
119 ## If / for / while etc
121 Put the opening brace on the same line, with a space before it.
122 There should be a space between the construct name (if/for/whatever) and the
123 opening parenthesis, and there should be a space between the closing parenthesis
124 and the opening brace, and no space between parenthesis and expression.
125 `make style` will take care of all that.
127 If you do split the condition, put the binary operators that join the lines at
128 the beginning of the following lines, not at the end of the prior lines.
130 For generic `for()` iterator variables, declare them in-line:
132 for (int i = 0; i < 10; i++) {
136 Note the spaces after the semicolons.
138 if/else should be laid out as follows:
148 You can skip braces around 1-line statements but don't mix braces vs. no braces.
152 Put the return type on the same line.
153 Put a space after a comma in argument lists.
154 Open the brace after the declaration (after a space).
155 `make style` will take care of all that.
157 void foo(int a_thing, int something_else) {
161 Functions with no arguments are declared as `f(void)`, not `f()`.
162 Use static for functions that aren't exported, and put exported functions
163 in a header file (one header file per source file with exported functions
164 usually, no huge headers with all functions).
170 Function names should be `separated_with_underscores()`, except for standard
171 functions (`memcpy`, etc.). It may make sense to break this rule for very common,
172 generic functions that look like library functions (e.g. `dprintf()`).
174 Don't use single-character arguments.
175 Exception: very short functions with one argument that's really obvious:
177 static int ascii(char c) {
178 if (c < 0x20 || c >= 0x7f)
186 static void hexdump(void *buf, size_t len) {
190 As a general guideline, functions shouldn't usually be much more than 30-50
191 lines. Above, the general algorithm won't be easily apparent, and you're
192 probably missing some factoring/restructuring opportunity.
194 ## Structs / unions / enums
196 Use typedefs when defining structs. The type should be named something_t.
202 You can use anonymous enums to replace lots of sequential or mostly-sequential
207 Indent once for the `case:` labels, then again for the body. Like this:
218 `make style` will take care of the indentation.
220 If you fall through into another case, add an explicit comment;
221 otherwise, it can look confusing.
223 If your `switch()` is too long or has too many cases, it should be cleaned up.
224 Split off the cases into functions, break the switch() into parent and children
225 switches (e.g. command and subcommand), or use an array of function pointers or
226 the like. In other words, use common sense and your brain.
228 If you need local scope variables for a case, you can add braces:
238 But at that point you should probably consider using a separate function.
242 Use //, it's shorter:
248 // This does blah blah blah .....
251 `/* */` can be used to comment blocks of code, but you should probably remove
252 them anyway - we have version control, it's easy to fetch old code if needed,
253 so avoid committing commented out chunks of code. The same goes for `#if 0`.
257 Please use common sense and restrain yourself from having a thousands line
258 file. Functions in a file should have something *specific* in common. Over time
259 sub-categories can arise and should therefore yield to file splitting.
261 For these reasons, vague and general filenames (e.g. `util.*`, `global.*`, `misc.*`,
262 `main.*`, and the like) should be very limited, if not prohibited.
266 License/description header first:
268 //-----------------------------------------------------------------------------
269 // YOUR COPYRIGHT LINE GOES HERE
271 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
272 // at your option, any later version. See the LICENSE.txt file for the text of
274 //-----------------------------------------------------------------------------
275 // FILE DESCRIPTION GOES HERE
276 //-----------------------------------------------------------------------------
278 If you modify a file in any non-trivial way (add code, etc.), add your copyright
279 to the top with the current year.
283 Use the following include guard format:
292 Keep in mind that `__FOOBAR_H` would be reserved by the implementation and thus
293 you shouldn't use it (same for `_FOOBAR_H`).
297 Avoid trailing whitespace (no line should end in tab or space).
298 Keep a newline (blank line) at the end of each file.
300 `make style` will take care of both.