Mercurial > trustbridge
changeset 163:8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
The kdelibs / qt coding style is not really suited for plain C
and looks irritiating when combined with standard C functions.
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Tue, 25 Mar 2014 10:29:12 +0000 |
parents | 4a4b5e640d1a |
children | 199878f09bf1 |
files | HACKING cinst/main.c cinst/windowsstore.c |
diffstat | 3 files changed, 229 insertions(+), 170 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HACKING Tue Mar 25 10:29:12 2014 +0000 @@ -0,0 +1,10 @@ +Please keep C code according to GNU coding standards. + +With regards to the style: + astyle --style=gnu --indent=spaces=2 --pad-paren-out + +For the C++ Ui code please use the kdelibs coding style + astyle --indent=spaces=4 --brackets=linux \ + --indent-labels --pad-oper --unpad-paren --pad-header \ + --keep-one-line-statements --convert-tabs \ + --indent-preprocessor
--- a/cinst/main.c Tue Mar 25 10:08:56 2014 +0000 +++ b/cinst/main.c Tue Mar 25 10:29:12 2014 +0000 @@ -59,71 +59,84 @@ * * @returns: 0 on success. An error code otherwise. */ -int readInput(char **certificate_list, char ***to_install, - char ***to_remove, char ***all_certs) +int +readInput (char **certificate_list, char ***to_install, + char ***to_remove, char ***all_certs) { - int lines_read = 0; - int readingList = 0; - size_t list_size = 0; - char buf[MAX_LINE_LENGTH + 2]; + int lines_read = 0; + int readingList = 0; + size_t list_size = 0; + char buf[MAX_LINE_LENGTH + 2]; - if (*certificate_list || *to_install || *to_remove) { - printf("Error invalid parameters\n"); - return -1; + if (*certificate_list || *to_install || *to_remove) + { + printf ("Error invalid parameters\n"); + return -1; } - while (fgets(buf, MAX_LINE_LENGTH + 1, stdin)) { - size_t len = strlen(buf); /* fgets ensures buf is terminated */ - if (len <= 3) { - printf("Line too short.\n"); - return ERR_INVALID_INPUT; + while (fgets (buf, MAX_LINE_LENGTH + 1, stdin) ) + { + size_t len = strlen (buf); /* fgets ensures buf is terminated */ + if (len <= 3) + { + printf ("Line too short.\n"); + return ERR_INVALID_INPUT; } - if (lines_read ++ > MAX_LINES) { - printf("Too many lines\n"); - return ERR_TOO_MUCH_INPUT; + if (lines_read++ > MAX_LINES) + { + printf ("Too many lines\n"); + return ERR_TOO_MUCH_INPUT; } - if (buf[len-2] != '\r') { - if (buf[len-1] != '\n') { - printf("Line too long.\n"); - return ERR_INVALID_INPUT; + if (buf[len - 2] != '\r') + { + if (buf[len - 1] != '\n') + { + printf ("Line too long.\n"); + return ERR_INVALID_INPUT; } - buf[len-1] = '\r'; - buf[len] = '\n'; - buf[len+1] = '\0'; - len++; + buf[len - 1] = '\r'; + buf[len] = '\n'; + buf[len + 1] = '\0'; + len++; } - if (strcmp("-----BEGIN CERTIFICATE LIST-----\r\n", buf) == 0){ - readingList = 1; - continue; - } - if (strcmp("-----END CERTIFICATE LIST-----\r\n", buf) == 0){ - readingList = 0; - continue; + if (strcmp ("-----BEGIN CERTIFICATE LIST-----\r\n", buf) == 0) + { + readingList = 1; + continue; } - if (readingList) { - str_append_str(certificate_list, &list_size, buf, len); - } else if (strcmp("UNINSTALL\r\n", buf) == 0) { - /* Remove trailing \r\n */ - strv_append(to_remove, buf, len - 2); - continue; + if (strcmp ("-----END CERTIFICATE LIST-----\r\n", buf) == 0) + { + readingList = 0; + continue; } - if (*buf == 'I') { - /* Remove leading I: and trailing \r\n */ - strv_append(readingList ? all_certs : to_install, - buf+2, len - 4); - continue; + if (readingList) + { + str_append_str (certificate_list, &list_size, buf, len); } - if (*buf == 'R') { - /* Remove leading R: and trailing \r\n */ - strv_append(readingList ? all_certs : to_remove, - buf+2, len - 4); - continue; + else if (strcmp ("UNINSTALL\r\n", buf) == 0) + { + /* Remove trailing \r\n */ + strv_append (to_remove, buf, len - 2); + continue; + } + if (*buf == 'I') + { + /* Remove leading I: and trailing \r\n */ + strv_append (readingList ? all_certs : to_install, + buf + 2, len - 4); + continue; + } + if (*buf == 'R') + { + /* Remove leading R: and trailing \r\n */ + strv_append (readingList ? all_certs : to_remove, buf + 2, len - 4); + continue; } } - return 0; + return 0; } /** @brief Check that the insturctions match to the list @@ -136,110 +149,133 @@ * * @returns 0 on success, an error otherwise */ -int validate_instructions(char **all_certs, - char **to_validate) +int +validate_instructions (char **all_certs, char **to_validate) { - int i = 0, - j = 0; + int i = 0, j = 0; - if (!all_certs || strv_length(all_certs) < 1) { - /* Invalid parameters */ - return -1; + if (!all_certs || strv_length (all_certs) < 1) + { + /* Invalid parameters */ + return -1; } - if (to_validate == NULL) { - /* Nothing is valid */ - return 0; + if (to_validate == NULL) + { + /* Nothing is valid */ + return 0; } - for (i=0; to_validate[i]; i++) { - bool found = false; - for (j=0; all_certs[j]; j++) { - if (strncmp(to_validate[i], all_certs[j], MAX_LINE_LENGTH - 2) == 0) { - found = true; - break; + for (i = 0; to_validate[i]; i++) + { + bool found = false; + for (j = 0; all_certs[j]; j++) + { + if (strncmp (to_validate[i], all_certs[j], MAX_LINE_LENGTH - 2) == + 0) + { + found = true; + break; } } - if (!found) { - printf("Install instruction with invalid certificate\n."); - return ERR_INVALID_INSTRUCTIONS; + if (!found) + { + printf ("Install instruction with invalid certificate\n."); + return ERR_INVALID_INSTRUCTIONS; } } - return 0; + return 0; } -int main() { - char **to_install = NULL; - char **to_remove = NULL; - char **all_certs = NULL; - char *certificate_list = NULL; - size_t list_len = 0; - int ret = -1; - bool uninstall = false; +int +main () +{ + char **to_install = NULL; + char **to_remove = NULL; + char **all_certs = NULL; + char *certificate_list = NULL; + size_t list_len = 0; + int ret = -1; + bool uninstall = false; - ret = readInput(&certificate_list, &to_install, &to_remove, &all_certs); + ret = readInput (&certificate_list, &to_install, &to_remove, &all_certs); - if (ret) { - return ret; + if (ret) + { + return ret; } - if (!certificate_list) { - return ERR_INVALID_INPUT_NO_LIST; + if (!certificate_list) + { + return ERR_INVALID_INPUT_NO_LIST; } - list_len = strnlen(certificate_list, MAX_INPUT_SIZE); + list_len = strnlen (certificate_list, MAX_INPUT_SIZE); - ret = verify_list(certificate_list, list_len); + ret = verify_list (certificate_list, list_len); - if (ret) { - return ERR_INVALID_SIGNATURE; + if (ret) + { + return ERR_INVALID_SIGNATURE; } - if (!strv_length(to_install) && !strv_length(to_remove)) { - return ERR_NO_INSTRUCTIONS; + if (!strv_length (to_install) && !strv_length (to_remove) ) + { + return ERR_NO_INSTRUCTIONS; } - /* Check that the instructions are ok to execute */ - if (to_install) { - ret = validate_instructions(all_certs, to_install); - if (ret) { - return ret; + /* Check that the instructions are ok to execute */ + if (to_install) + { + ret = validate_instructions (all_certs, to_install); + if (ret) + { + return ret; } } - if (to_remove) { - if (to_remove[0] && strncmp("UNINSTALL", to_remove[0], MAX_LINE_LENGTH) == 0) { - uninstall = true; - strv_free(to_remove); - to_remove = NULL; - } else { - ret = validate_instructions(all_certs, to_remove); - if (ret) { - return ret; + if (to_remove) + { + if (to_remove[0] + && strncmp ("UNINSTALL", to_remove[0], MAX_LINE_LENGTH) == 0) + { + uninstall = true; + strv_free (to_remove); + to_remove = NULL; + } + else + { + ret = validate_instructions (all_certs, to_remove); + if (ret) + { + return ret; } } } - if (uninstall) { - /* To uninstall does not have to be verified as it part of the - * signed list.*/ - to_remove = all_certs; - } else { - strv_free(all_certs); - all_certs = NULL; + if (uninstall) + { + /* To uninstall does not have to be verified as it part of the + * signed list.*/ + to_remove = all_certs; + } + else + { + strv_free (all_certs); + all_certs = NULL; } #ifdef WIN32 - return write_stores_win (to_install, to_remove, true); + return write_stores_win (to_install, to_remove, true); #endif - /* Make valgrind happy */ - strv_free(to_install); - strv_free(to_remove); - free(certificate_list); + /* Make valgrind happy */ + strv_free (to_install); + strv_free (to_remove); + free (certificate_list); - return 0; + return 0; }
--- a/cinst/windowsstore.c Tue Mar 25 10:08:56 2014 +0000 +++ b/cinst/windowsstore.c Tue Mar 25 10:29:12 2014 +0000 @@ -7,82 +7,95 @@ #include "listutil.h" #include "strhelp.h" -static LPWSTR getLastErrorMsg() { - LPWSTR bufPtr = NULL; - DWORD err = GetLastError(); - FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, err, 0, (LPWSTR)&bufPtr, 0, NULL); - if (!bufPtr) { - HMODULE hWinhttp = GetModuleHandleW(L"winhttp"); - if (hWinhttp) { - FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_HMODULE | - FORMAT_MESSAGE_IGNORE_INSERTS, - hWinhttp, HRESULT_CODE(err), 0, - (LPWSTR)&bufPtr, 0, NULL); +static LPWSTR getLastErrorMsg() +{ + LPWSTR bufPtr = NULL; + DWORD err = GetLastError(); + FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, err, 0, (LPWSTR) &bufPtr, 0, NULL); + if (!bufPtr) + { + HMODULE hWinhttp = GetModuleHandleW (L"winhttp"); + if (hWinhttp) + { + FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_HMODULE | + FORMAT_MESSAGE_IGNORE_INSERTS, + hWinhttp, HRESULT_CODE (err), 0, + (LPWSTR) &bufPtr, 0, NULL); } } - if (!bufPtr) - printf("Error getting last error\n"); - return bufPtr; + if (!bufPtr) + printf ("Error getting last error\n"); + return bufPtr; } -int write_stores_win(char **to_install, char **to_remove, bool user_store) +int write_stores_win (char **to_install, char **to_remove, bool user_store) { - int i = 0; - int ret = -1; - HCERTSTORE hStore = NULL; + int i = 0; + int ret = -1; + HCERTSTORE hStore = NULL; - if (user_store) { - hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, - 0, CERT_SYSTEM_STORE_CURRENT_USER, L"Root"); - } else { - hStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, - 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root"); + if (user_store) + { + hStore = CertOpenStore (CERT_STORE_PROV_SYSTEM, 0, + 0, CERT_SYSTEM_STORE_CURRENT_USER, L"Root"); + } + else + { + hStore = CertOpenStore (CERT_STORE_PROV_SYSTEM, 0, + 0, CERT_SYSTEM_STORE_LOCAL_MACHINE, L"Root"); } - if (!hStore) { - return ERR_STORE_ACCESS_DENIED; + if (!hStore) + { + return ERR_STORE_ACCESS_DENIED; } - for (i=0; to_install[i]; i++) { - size_t cert_len = strnlen(to_install[i], MAX_LINE_LENGTH), - buf_size = 0; - char *buf = NULL; + for (i=0; to_install[i]; i++) + { + size_t cert_len = strnlen (to_install[i], MAX_LINE_LENGTH), + buf_size = 0; + char *buf = NULL; - ret = str_base64_decode(&buf, &buf_size, to_install[i], cert_len); + ret = str_base64_decode (&buf, &buf_size, to_install[i], cert_len); - if (ret != 0) { - return ERR_INVALID_INSTRUCTIONS; + if (ret != 0) + { + return ERR_INVALID_INSTRUCTIONS; } - ret = CertAddEncodedCertificateToStore (hStore, - X509_ASN_ENCODING, - (PBYTE)buf, - buf_size, - CERT_STORE_ADD_ALWAYS, - NULL); + ret = CertAddEncodedCertificateToStore (hStore, + X509_ASN_ENCODING, + (PBYTE) buf, + buf_size, + CERT_STORE_ADD_ALWAYS, + NULL); - if (ret == 0) { - LPWSTR error = getLastErrorMsg(); - if (error) { - printf("Failed to add certificate: %S \n", error); - LocalFree(error); + if (ret == 0) + { + LPWSTR error = getLastErrorMsg(); + if (error) + { + printf ("Failed to add certificate: %S \n", error); + LocalFree (error); } } - i++; - free(buf); + i++; + free (buf); } - for (i=0; to_remove[i]; i++) { - // TODO + for (i=0; to_remove[i]; i++) + { + // TODO } - if(hStore) { - CertCloseStore(hStore, 0); + if (hStore) + { + CertCloseStore (hStore, 0); } - return 0; + return 0; } #endif // WIN32