]> infiniteadaptability.org Git - totp/commitdiff
chore: add formatting master v1.0.1
authoralex <[email protected]>
Fri, 12 Dec 2025 07:02:13 +0000 (23:02 -0800)
committeralex <[email protected]>
Fri, 12 Dec 2025 08:04:30 +0000 (00:04 -0800)
Makefile.am
inc/args.h
inc/base32.h
inc/main.h
inc/totp.h
manifest.scm
src/args.c
src/base32.c
src/main.c
src/totp.c
src/usage.c

index 8c4aa60c87f9426b92b550b9162eaa68ff87c26b..e99a8060d45c441fa8dbb1e40232f66993ad7fbd 100644 (file)
@@ -18,3 +18,6 @@ totp_SOURCES += \
                inc/usage.h
 
 SUBDIRS = . test 
+
+format:
+       VERSION_CONTROL=none indent -linux -brf $(totp_SOURCES)
index ebc3cdd92b21a30dc50dfe8ee24451fb537d8f09..1570ea35d24aea2a832d2da6ac87e51ca769d5a2 100644 (file)
@@ -12,6 +12,6 @@
 #include<totp.h>
 #include<usage.h>
 
-int args(int, char**);
+int args(int, char **);
 
 #endif
index c75ccc0e95b019435ffbdc2a521ff339dbbde6d8..234c392dc6d1d292df53e2c99e83d4ca46a8a187 100644 (file)
@@ -4,6 +4,6 @@
 #include<stdio.h>
 #include<stdlib.h>
 
-int from_base32(unsigned char**, ssize_t*);
+int from_base32(unsigned char **, ssize_t *);
 
 #endif
index cf632a262c1ff2fde3354a50900a79db746c86dc..51a565fa65fd1999eb80fc574cd66cd0703cc253 100644 (file)
@@ -9,6 +9,6 @@
 #include<base32.h>
 #include<totp.h>
 
-int main(int,char**);
+int main(int, char **);
 
 #endif
index 69505be91e5bc6a6f98775628b154a178b033dbd..821d140cedc5f30ade4b4074c1341e874ffdeda5 100644 (file)
@@ -12,6 +12,6 @@
 
 extern time_t now;
 
-int totp(unsigned char*, ssize_t, uint32_t*);
+int totp(unsigned char *, ssize_t, uint32_t *);
 
 #endif
index 6c53f863a5e74263e82fba922456c5ddcfb5f2f2..4b2fcf272ba1ac9d3ad291798959b78c5dbd957d 100644 (file)
@@ -1,11 +1,12 @@
 (specifications->manifest (list "autoconf"
                                 "automake"
                                 "coreutils"
-                               "gawk"
+                                "gawk"
                                 "gcc-toolchain"
-                               "gdb"
+                                "gdb"
                                 "grep"
+                                "indent"
                                 "make"
-                               "openssl"
+                                "openssl"
                                 "sed"
-                               "valgrind"))
+                                "valgrind"))
index 1ae55f1a98dbd66cadec5b8e395c9f3fb2184913..3b461e1ed9283cea236993b4fdd17d662c79b70b 100644 (file)
@@ -3,7 +3,7 @@
 static struct option long_options[] = {
        {"help", no_argument, 0, 'h'},
        {"time", required_argument, 0, 't'},
-       {0,0,0,0}
+       {0, 0, 0, 0}
 };
 
 time_t now = 0;
@@ -12,37 +12,44 @@ int args(int argc, char **argv) {
        struct tm tm;
        int c;
 
-       while(1) {
+       while (1) {
                int option_index = 0;
 
-               if((c = getopt_long(argc, argv, "ht:", long_options, &option_index)) == -1) { break; }
-
-               switch(c) {
-                       case 0:
-                               if(long_options[option_index].flag != 0) { break; }
-
-                               fprintf(stderr, "option %s", long_options[option_index].name);
-                               if(optarg) {
-                                       fprintf(stderr, " with arg %s", optarg);
-                               }
-                               fprintf(stderr, "\n");
-                               return -1;
-                               
-                               break;
-                       case 'h':
-                               usage();
-                               return -1;
-                       case 't':
-                               memset(&tm, 0, sizeof(tm));
-
-                               if(strptime(optarg, "%F %T %z", &tm) != NULL) {
-                                       now = mktime(&tm);
-                               }
+               if ((c =
+                    getopt_long(argc, argv, "ht:", long_options,
+                                &option_index)) == -1) {
+                       break;
+               }
 
+               switch (c) {
+               case 0:
+                       if (long_options[option_index].flag != 0) {
                                break;
-                       case '?':
-                       default:
-                               return -1;
+                       }
+
+                       fprintf(stderr, "option %s",
+                               long_options[option_index].name);
+                       if (optarg) {
+                               fprintf(stderr, " with arg %s", optarg);
+                       }
+                       fprintf(stderr, "\n");
+                       return -1;
+
+                       break;
+               case 'h':
+                       usage();
+                       return -1;
+               case 't':
+                       memset(&tm, 0, sizeof(tm));
+
+                       if (strptime(optarg, "%F %T %z", &tm) != NULL) {
+                               now = mktime(&tm);
+                       }
+
+                       break;
+               case '?':
+               default:
+                       return -1;
                }
        }
 
index 500211ea40cd7d2d15a180c0854587336ad3d6d9..f40b92c2224ae29823a5cc8beb452872cbf6aa77 100644 (file)
@@ -5,24 +5,24 @@ int from_base32(unsigned char **raw, ssize_t *n) {
        char c, to_set;
        size_t i, j, offset, len;
 
-       if(NULL == raw || NULL == *raw) {
+       if (NULL == raw || NULL == *raw) {
                fprintf(stderr, "invalid buffer\n");
                return -1;
        }
 
-       if(NULL == n || *n <= 0) {
+       if (NULL == n || *n <= 0) {
                fprintf(stderr, "invalid secret size\n");
                return -1;
        }
 
-       len = ((*n)*5)>>3;
-       if(len == 0) {
+       len = ((*n) * 5) >> 3;
+       if (len == 0) {
                fprintf(stderr, "invalid secret size\n");
                return -1;
        }
-       
+
        buf = calloc(len, sizeof(char));
-       if(NULL == buf) {
+       if (NULL == buf) {
                perror("calloc");
                return -1;
        }
@@ -30,14 +30,14 @@ int from_base32(unsigned char **raw, ssize_t *n) {
        offset = 0;
        p = *raw;
        j = 0;
-       for(i = 0; i < *n; i++) {
+       for (i = 0; i < *n; i++) {
                c = p[i];
 
-               if(c >= 'A' && c <= 'Z') {
+               if (c >= 'A' && c <= 'Z') {
                        to_set = c - 65;
-               } else if(c >= '2' && c <= '7') {
+               } else if (c >= '2' && c <= '7') {
                        to_set = c - 24;
-               } else if(c == '=') {
+               } else if (c == '=') {
                        continue;
                } else {
                        fprintf(stderr, "invalid base32 character %c\n", c);
@@ -47,7 +47,7 @@ int from_base32(unsigned char **raw, ssize_t *n) {
                // No need to mask this value since it will always be <31
                buf[j] += (to_set << 3) >> offset;
 
-               if(((offset + 5) > 7) && (j + 1 < len)) {
+               if (((offset + 5) > 7) && (j + 1 < len)) {
                        j++;
                        buf[j] += ((to_set << (8 - (5 - (8 - offset)))) & 0xff);
                }
index dc228b050c640148fca6cfb1e138926462cc2d38..3827a2dd9428a26796519395f5ebaf641684b476 100644 (file)
@@ -9,26 +9,34 @@ int main(int argc, char **argv) {
        buf = NULL;
        now = 0;
 
-       if(args(argc, argv)<0) { return EXIT_FAILURE; }
+       if (args(argc, argv) < 0) {
+               return EXIT_FAILURE;
+       }
 
-       if((n = getline((char **)&buf, &len, stdin)) <= 0) {
+       if ((n = getline((char **)&buf, &len, stdin)) <= 0) {
                perror("getline failed");
                fprintf(stderr, "No secret provided\n");
                goto clean;
        }
-
        // Trim trailing '\n'
        n--;
 
-       if(from_base32(&buf, &n) < 0) { goto clean; }
-       if(totp((unsigned char *)buf, n, &code) < 0) { goto clean; }
+       if (from_base32(&buf, &n) < 0) {
+               goto clean;
+       }
+
+       if (totp((unsigned char *)buf, n, &code) < 0) {
+               goto clean;
+       }
 
        fprintf(stdout, "%.6u\n", code % 1000000);
 
        free(buf);
 
        return EXIT_SUCCESS;
-       clean:
-               if(buf != NULL) { free(buf); }
-               return EXIT_FAILURE;
+ clean:
+       if (buf != NULL) {
+               free(buf);
+       }
+       return EXIT_FAILURE;
 }
index d8c8c6e6fd45e5a671e53b9a4c2905791a3a85a7..55c07e0ef9d81594aeea803e042e6489580c6ae1 100644 (file)
@@ -6,31 +6,37 @@ int totp(unsigned char *key, ssize_t n, uint32_t *code) {
        size_t offset;
        unsigned int hash_len = SHA_DIGEST_LENGTH;
 
-       if(NULL == key) { return -1; }
-       if(n <= 0) { return -1; }
+       if (NULL == key) {
+               return -1;
+       }
+
+       if (n <= 0) {
+               return -1;
+       }
 
-       if(now == 0) {
+       if (now == 0) {
                now = time(NULL);
        }
 
        now = floor(now / 30);
 
        // Guarantee that the data buffer is in big-endian order
-       for(size_t i = 0; i<sizeof(time_t); i++) {
-               data[sizeof(time_t) - (1 + i)] = now >> (8*i) & 0xff;
+       for (size_t i = 0; i < sizeof(time_t); i++) {
+               data[sizeof(time_t) - (1 + i)] = now >> (8 * i) & 0xff;
        }
 
-       if(NULL == HMAC(EVP_sha1(), key, n, data, sizeof(time_t), hash, &hash_len)) {
+       if (NULL ==
+           HMAC(EVP_sha1(), key, n, data, sizeof(time_t), hash, &hash_len)) {
                fprintf(stderr, "hashing failed\n");
                return -1;
        }
 
        offset = hash[SHA_DIGEST_LENGTH - 1] & 0xf;
 
-       (*code) = ((hash[offset]  & 0x7f) << 24)
-           | ((hash[offset+1] & 0xff) << 16)
-           | ((hash[offset+2] & 0xff) <<  8)
-           | (hash[offset+3] & 0xff);
+       (*code) = ((hash[offset] & 0x7f) << 24)
+           | ((hash[offset + 1] & 0xff) << 16)
+           | ((hash[offset + 2] & 0xff) << 8)
+           | (hash[offset + 3] & 0xff);
 
        return 1;
 }
index 8500a3edbf1aa8e2e7ff51933254d0ead6b95378..6adfca080a324e4abd8148c6ded256e6d8bb0d17 100644 (file)
@@ -4,5 +4,6 @@ void usage() {
        fprintf(stderr, "Usage:\n");
        fprintf(stderr, "\t[SECRET] | totp\n");
        fprintf(stderr, "\n");
-       fprintf(stderr, "Pass a base32 encoded secrets to stdin and generate a totp code at the current time.\n");
+       fprintf(stderr,
+               "Pass a base32 encoded secrets to stdin and generate a totp code at the current time.\n");
 }