diff options
author | Nick Mathewson <nickm@torproject.org> | 2003-12-08 23:45:37 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2003-12-08 23:45:37 +0000 |
commit | ac552573dddbd661685382cdd89841a206dc0b00 (patch) | |
tree | 80ed7a762c4e939f8481820c1719be9351b7afa2 /src/common | |
parent | 8bd7c94bf6ca5ef0ece02b165ed0102eb39e0d78 (diff) | |
download | tor-ac552573dddbd661685382cdd89841a206dc0b00.tar.gz tor-ac552573dddbd661685382cdd89841a206dc0b00.zip |
Make router/directory parsing nondestructive and more const-friendly
svn:r890
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/crypto.c | 12 | ||||
-rw-r--r-- | src/common/crypto.h | 8 | ||||
-rw-r--r-- | src/common/util.c | 15 | ||||
-rw-r--r-- | src/common/util.h | 7 |
4 files changed, 26 insertions, 16 deletions
diff --git a/src/common/crypto.c b/src/common/crypto.c index 6b7f0c29f7..070a2a0cbc 100644 --- a/src/common/crypto.c +++ b/src/common/crypto.c @@ -441,7 +441,7 @@ int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env, char **dest, int return 0; } -int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env, char *src, int len) { +int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env, const char *src, int len) { BIO *b; assert(env && src); @@ -820,7 +820,7 @@ crypto_cipher_advance(crypto_cipher_env_t *env, long delta) /* SHA-1 */ -int crypto_SHA_digest(unsigned char *m, int len, unsigned char *digest) +int crypto_SHA_digest(const unsigned char *m, int len, unsigned char *digest) { assert(m && digest); return (SHA1(m,len,digest) == NULL); @@ -1034,7 +1034,7 @@ char *crypto_perror() } int -base64_encode(char *dest, int destlen, char *src, int srclen) +base64_encode(char *dest, int destlen, const char *src, int srclen) { EVP_ENCODE_CTX ctx; int len, ret; @@ -1046,13 +1046,13 @@ base64_encode(char *dest, int destlen, char *src, int srclen) return -1; EVP_EncodeInit(&ctx); - EVP_EncodeUpdate(&ctx, dest, &len, src, srclen); + EVP_EncodeUpdate(&ctx, dest, &len, (char*) src, srclen); EVP_EncodeFinal(&ctx, dest+len, &ret); ret += len; return ret; } int -base64_decode(char *dest, int destlen, char *src, int srclen) +base64_decode(char *dest, int destlen, const char *src, int srclen) { EVP_ENCODE_CTX ctx; int len, ret; @@ -1063,7 +1063,7 @@ base64_decode(char *dest, int destlen, char *src, int srclen) return -1; EVP_DecodeInit(&ctx); - EVP_DecodeUpdate(&ctx, dest, &len, src, srclen); + EVP_DecodeUpdate(&ctx, dest, &len, (char*) src, srclen); EVP_DecodeFinal(&ctx, dest, &ret); ret += len; return ret; diff --git a/src/common/crypto.h b/src/common/crypto.h index 0d8257fd9c..31cb41b6ec 100644 --- a/src/common/crypto.h +++ b/src/common/crypto.h @@ -38,7 +38,7 @@ int crypto_pk_generate_key(crypto_pk_env_t *env); int crypto_pk_read_private_key_from_file(crypto_pk_env_t *env, FILE *src); int crypto_pk_read_public_key_from_file(crypto_pk_env_t *env, FILE *src); int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env, char **dest, int *len); -int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env, char *src, int len); +int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env, const char *src, int len); int crypto_pk_write_private_key_to_file(crypto_pk_env_t *env, FILE *dest); int crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env, const char *fname); int crypto_pk_write_public_key_to_file(crypto_pk_env_t *env, FILE *dest); @@ -58,8 +58,8 @@ int crypto_pk_public_checksig(crypto_pk_env_t *env, unsigned char *from, int fro int crypto_pk_get_fingerprint(crypto_pk_env_t *pk, char *fp_out); int crypto_pk_check_fingerprint_syntax(const char *s); -int base64_encode(char *dest, int destlen, char *src, int srclen); -int base64_decode(char *dest, int destlen, char *src, int srclen); +int base64_encode(char *dest, int destlen, const char *src, int srclen); +int base64_decode(char *dest, int destlen, const char *src, int srclen); /* Key negotiation */ typedef struct crypto_dh_env_st { @@ -95,7 +95,7 @@ int crypto_cipher_advance(crypto_cipher_env_t *env, long delta); crypto_cipher_env_t *crypto_create_init_cipher(int cipher_type, char *key, char *iv, int encrypt_mode); /* SHA-1 */ -int crypto_SHA_digest(unsigned char *m, int len, unsigned char *digest); +int crypto_SHA_digest(const unsigned char *m, int len, unsigned char *digest); /* random numbers */ int crypto_seed_rng(); diff --git a/src/common/util.c b/src/common/util.c index b28eb6d5e2..c8ec2de230 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -54,12 +54,21 @@ char *tor_strdup(const char *s) { return dup; } +char *tor_strndup(const char *s, size_t n) { + char *dup; + assert(s); + dup = tor_malloc(n+1); + strncpy(dup, s, n); + dup[n] = 0; + return dup; +} + /* * String manipulation */ /* return the first char of s that is not whitespace and not a comment */ -char *eat_whitespace(char *s) { +const char *eat_whitespace(const char *s) { assert(s); while(isspace(*s) || *s == '#') { @@ -75,14 +84,14 @@ char *eat_whitespace(char *s) { return s; } -char *eat_whitespace_no_nl(char *s) { +const char *eat_whitespace_no_nl(const char *s) { while(*s == ' ' || *s == '\t') ++s; return s; } /* return the first char of s that is whitespace or '#' or '\0 */ -char *find_whitespace(char *s) { +const char *find_whitespace(const char *s) { assert(s); while(*s && !isspace(*s) && *s != '#') diff --git a/src/common/util.h b/src/common/util.h index 8e91532ba0..bc8ee06c6f 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -36,11 +36,12 @@ void *tor_malloc(size_t size); void *tor_malloc_zero(size_t size); void *tor_realloc(void *ptr, size_t size); char *tor_strdup(const char *s); +char *tor_strndup(const char *s, size_t n); #define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0) -char *eat_whitespace(char *s); -char *eat_whitespace_no_nl(char *s); -char *find_whitespace(char *s); +const char *eat_whitespace(const char *s); +const char *eat_whitespace_no_nl(const char *s); +const char *find_whitespace(const char *s); void tor_gettimeofday(struct timeval *timeval); long tv_udiff(struct timeval *start, struct timeval *end); |