Commit ac552573 authored by Nick Mathewson's avatar Nick Mathewson 🤹
Browse files

Make router/directory parsing nondestructive and more const-friendly


svn:r890
parent 8bd7c94b
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -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;
+4 −4
Original line number Diff line number Diff line
@@ -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();
+12 −3
Original line number Diff line number Diff line
@@ -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 != '#')
+4 −3
Original line number Diff line number Diff line
@@ -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);
+3 −4
Original line number Diff line number Diff line
@@ -202,7 +202,8 @@ dirserv_add_descriptor(const char **desc)
  routerinfo_t *ri = NULL;
  int i, r;
  char *start, *end;
  char *desc_tmp = NULL, *cp;
  char *desc_tmp = NULL;
  const char *cp;
  size_t desc_len;

  start = strstr(*desc, "router ");
@@ -218,9 +219,7 @@ dirserv_add_descriptor(const char **desc)
    end = start+strlen(start);
  }
  desc_len = end-start;
  cp = desc_tmp = tor_malloc(desc_len+1);
  strncpy(desc_tmp, start, desc_len);
  desc_tmp[desc_len]='\0';
  cp = desc_tmp = tor_strndup(start, desc_len);

  /* Check: is the descriptor syntactically valid? */
  ri = router_get_entry_from_string(&cp);
Loading