[or-cvs] Make router/directory parsing nondestructive and more const...

Nick Mathewson nickm at seul.org
Mon Dec 8 23:45:39 UTC 2003


Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv8227/src/common

Modified Files:
	crypto.c crypto.h util.c util.h 
Log Message:
Make router/directory parsing nondestructive and more const-friendly

Index: crypto.c
===================================================================
RCS file: /home/or/cvsroot/src/common/crypto.c,v
retrieving revision 1.44
retrieving revision 1.45
diff -u -d -r1.44 -r1.45
--- crypto.c	3 Dec 2003 08:08:07 -0000	1.44
+++ crypto.c	8 Dec 2003 23:45:37 -0000	1.45
@@ -441,7 +441,7 @@
   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 @@
 
 
 /* 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 @@
 }
 
 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 @@
     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 @@
     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;

Index: crypto.h
===================================================================
RCS file: /home/or/cvsroot/src/common/crypto.h,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -d -r1.22 -r1.23
--- crypto.h	12 Nov 2003 04:28:30 -0000	1.22
+++ crypto.h	8 Dec 2003 23:45:37 -0000	1.23
@@ -38,7 +38,7 @@
 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_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 @@
 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();

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.42
retrieving revision 1.43
diff -u -d -r1.42 -r1.43
--- util.c	21 Nov 2003 05:55:03 -0000	1.42
+++ util.c	8 Dec 2003 23:45:37 -0000	1.43
@@ -54,12 +54,21 @@
   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 @@
   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 != '#')

Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -d -r1.23 -r1.24
--- util.h	18 Nov 2003 08:20:09 -0000	1.23
+++ util.h	8 Dec 2003 23:45:37 -0000	1.24
@@ -36,11 +36,12 @@
 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);



More information about the tor-commits mailing list