Mercurial > trustbridge
diff common/listutil.c @ 905:698b6a9bd75e
Fix coding style for C code
astyle --style=gnu --indent=spaces=2
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Wed, 13 Aug 2014 15:49:47 +0200 |
parents | 7861950f7637 |
children | edbf5e5e88f4 |
line wrap: on
line diff
--- a/common/listutil.c Wed Aug 13 15:44:35 2014 +0200 +++ b/common/listutil.c Wed Aug 13 15:49:47 2014 +0200 @@ -41,181 +41,205 @@ #define READ_FILE_INVALID_CALL -5 int read_file(const char *file_name, char **data, size_t *size, - const size_t max_size) + const size_t max_size) { - FILE *f; - long file_size; - - if (!file_name || !data || !size || !max_size) { - return READ_FILE_INVALID_CALL; - } + FILE *f; + long file_size; - f = fopen(file_name, "rb"); - if (f == NULL) - return READ_FILE_UNREADABLE; - - fseek(f, 0, SEEK_END); - file_size = ftell(f); - if (file_size <= 0){ - fclose(f); - return READ_FILE_UNREADABLE; + if (!file_name || !data || !size || !max_size) + { + return READ_FILE_INVALID_CALL; } - fseek(f, 0, SEEK_SET); + f = fopen(file_name, "rb"); + if (f == NULL) + return READ_FILE_UNREADABLE; - if (file_size + 1 == 0) { - fclose(f); - return READ_FILE_TOO_LARGE; - } - *size = (size_t) file_size; - - if (*size > max_size) { - fclose(f); - return READ_FILE_TOO_LARGE; + fseek(f, 0, SEEK_END); + file_size = ftell(f); + if (file_size <= 0) + { + fclose(f); + return READ_FILE_UNREADABLE; } - *data = (char *) malloc( *size + 1 ); - if (*data == NULL) { - fclose(f); - return READ_FILE_NO_MEMORY; + fseek(f, 0, SEEK_SET); + + if (file_size + 1 == 0) + { + fclose(f); + return READ_FILE_TOO_LARGE; + } + *size = (size_t) file_size; + + if (*size > max_size) + { + fclose(f); + return READ_FILE_TOO_LARGE; } - if (fread(*data, 1, *size, f) != *size) { - free(*data); - fclose(f); - return READ_FILE_READ_FAILED; + *data = (char *) malloc( *size + 1 ); + if (*data == NULL) + { + fclose(f); + return READ_FILE_NO_MEMORY; } - fclose(f); + if (fread(*data, 1, *size, f) != *size) + { + free(*data); + fclose(f); + return READ_FILE_READ_FAILED; + } - (*data)[*size] = '\0'; + fclose(f); - return 0; + (*data)[*size] = '\0'; + + return 0; } int verify_list(const char *data, const size_t size) { - int ret = -1; - pk_context pub_key_ctx; - char *p; + int ret = -1; + pk_context pub_key_ctx; + char *p; - /* Modulus / 8 are the necessary bytes. */ + /* Modulus / 8 are the necessary bytes. */ #ifndef TRUSTBRIDGE_RSA_KEY_SIZE # error "Key size undefined" #endif - const size_t sig_b64_size = TRUSTBRIDGE_RSA_KEY_SIZE / 8 * 4 / 3; - size_t sig_size = TRUSTBRIDGE_RSA_KEY_SIZE / 8; + const size_t sig_b64_size = TRUSTBRIDGE_RSA_KEY_SIZE / 8 * 4 / 3; + size_t sig_size = TRUSTBRIDGE_RSA_KEY_SIZE / 8; - char signature_b64[sig_b64_size + 1]; - unsigned char signature[sig_size]; - /* Hash algroithm is sha256 */ - unsigned char hash[32]; + char signature_b64[sig_b64_size + 1]; + unsigned char signature[sig_size]; + /* Hash algroithm is sha256 */ + unsigned char hash[32]; - if (!data || !size) { - return -1; + if (!data || !size) + { + return -1; } - /* Fetch the signature from the first line od data */ - p = strchr(data, '\r'); - if (p == 0 || (unsigned int)(p - (data + 2)) != sig_b64_size) { -/* printf("Invalid data. Signature might be too long.\n"); */ - return -1; + /* Fetch the signature from the first line od data */ + p = strchr(data, '\r'); + if (p == 0 || (unsigned int)(p - (data + 2)) != sig_b64_size) + { + /* printf("Invalid data. Signature might be too long.\n"); */ + return -1; } - strncpy(signature_b64, data + 2, sig_b64_size); - signature_b64[sig_b64_size] = '\0'; + strncpy(signature_b64, data + 2, sig_b64_size); + signature_b64[sig_b64_size] = '\0'; - ret = base64_decode(signature, &sig_size, - (unsigned char *)signature_b64, sig_b64_size); + ret = base64_decode(signature, &sig_size, + (unsigned char *)signature_b64, sig_b64_size); - if (ret != 0 || sig_size != TRUSTBRIDGE_RSA_KEY_SIZE / 8) { -/* printf("failed to decode signature\n"); */ - return -1; + if (ret != 0 || sig_size != TRUSTBRIDGE_RSA_KEY_SIZE / 8) + { + /* printf("failed to decode signature\n"); */ + return -1; } - /* Hash is calculated over the data without the first line. - * linebreaks are \r\n so the first char of the new line is - * p+2 */ - p += 2; - /* Size of the data to hash is the size - signature line - * signature line is sig_b64_size - "S:" and - "\r\n" so -4*/ - sha256((unsigned char *)p, size - sig_b64_size - 4, hash, 0); + /* Hash is calculated over the data without the first line. + * linebreaks are \r\n so the first char of the new line is + * p+2 */ + p += 2; + /* Size of the data to hash is the size - signature line + * signature line is sig_b64_size - "S:" and - "\r\n" so -4*/ + sha256((unsigned char *)p, size - sig_b64_size - 4, hash, 0); - pk_init(&pub_key_ctx); + pk_init(&pub_key_ctx); - ret = pk_parse_public_key(&pub_key_ctx, public_key_pem, - public_key_pem_size); - if (ret != 0) { - ERRORPRINTF ("pk_parse_public_key failed with -0x%04x\n\n", -ret); - pk_free(&pub_key_ctx); - return ret; + ret = pk_parse_public_key(&pub_key_ctx, public_key_pem, + public_key_pem_size); + if (ret != 0) + { + ERRORPRINTF ("pk_parse_public_key failed with -0x%04x\n\n", -ret); + pk_free(&pub_key_ctx); + return ret; } - ret = pk_verify(&pub_key_ctx, POLARSSL_MD_SHA256, hash, 0, - signature, sig_size); + ret = pk_verify(&pub_key_ctx, POLARSSL_MD_SHA256, hash, 0, + signature, sig_size); - if (ret != 0) { - ERRORPRINTF ("pk_verify failed with -0x%04x\n\n", -ret); + if (ret != 0) + { + ERRORPRINTF ("pk_verify failed with -0x%04x\n\n", -ret); } - pk_free(&pub_key_ctx); + pk_free(&pub_key_ctx); - return ret; + return ret; } list_status_t read_and_verify_list(const char *file_name, char **data, size_t *size) { - list_status_t retval = UnknownError; - *data = NULL; - *size = 0; - int ret = 0; - - ret = read_file(file_name, data, size, MAX_FILESIZE); + list_status_t retval = UnknownError; + *data = NULL; + *size = 0; + int ret = 0; - /* printf ("Ret: %i \n", ret); */ - if (ret != 0) { - if (ret == READ_FILE_TOO_LARGE) { - return TooLarge; + ret = read_file(file_name, data, size, MAX_FILESIZE); + + /* printf ("Ret: %i \n", ret); */ + if (ret != 0) + { + if (ret == READ_FILE_TOO_LARGE) + { + return TooLarge; } - if (ret == READ_FILE_UNREADABLE) { - /* TODO: work with errno ? */ - /* errsv = errno; */ - /* perror("read_and_verify_list(), READ_FILE_UNREADABLE:"); */ - return SeekFailed; + if (ret == READ_FILE_UNREADABLE) + { + /* TODO: work with errno ? */ + /* errsv = errno; */ + /* perror("read_and_verify_list(), READ_FILE_UNREADABLE:"); */ + return SeekFailed; } - if (ret == READ_FILE_READ_FAILED) { - /* TODO: work with ferror() or feof() ? */ - return ReadFailed; + if (ret == READ_FILE_READ_FAILED) + { + /* TODO: work with ferror() or feof() ? */ + return ReadFailed; } - return UnknownError; + return UnknownError; } - if (!*data || !*size) { - /* File is probably empty */ - return UnknownError; + if (!*data || !*size) + { + /* File is probably empty */ + return UnknownError; } - if (**data != 'S') { - retval = InvalidFormat; - } else { - ret = verify_list (*data, *size); - if (ret == 0) { - /* Hooray */ - return Valid; + if (**data != 'S') + { + retval = InvalidFormat; + } + else + { + ret = verify_list (*data, *size); + if (ret == 0) + { + /* Hooray */ + return Valid; } - if (ret == -1) { - /* our error */ - retval = InvalidFormat; - } else { - retval = InvalidSignature; + if (ret == -1) + { + /* our error */ + retval = InvalidFormat; + } + else + { + retval = InvalidSignature; } } - if (retval != Valid && *data) { - free(*data); - *data = NULL; - *size = 0; + if (retval != Valid && *data) + { + free(*data); + *data = NULL; + *size = 0; } - return retval; + return retval; } char **