Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
robertdavidgraham committed Jan 12, 2014
1 parent 3d6e984 commit 093cbc9
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 28 deletions.
114 changes: 91 additions & 23 deletions src/crypto-base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>

/****************************************************************************
****************************************************************************/
/*****************************************************************************
*****************************************************************************/
size_t
base64_encode(void *vdst, size_t sizeof_dst, const void *vsrc, size_t sizeof_src)
base64_encode(void *vdst, size_t sizeof_dst,
const void *vsrc, size_t sizeof_src)
{
static const char *b64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Expand All @@ -19,14 +21,15 @@ base64_encode(void *vdst, size_t sizeof_dst, const void *vsrc, size_t sizeof_src
unsigned char *dst = (unsigned char *)vdst;
const unsigned char *src = (const unsigned char *)vsrc;

/* encode every 3 bytes of source into 4 bytes of destination text */
while (i + 3 <= sizeof_src) {
unsigned n;

/* make sure there is enough space */
if (d + 4 > sizeof_dst)
return d;

/* conver the chars */
/* convert the chars */
n = src[i]<<16 | src[i+1]<<8 | src[i+2];
dst[d+0] = b64[ (n>>18) & 0x3F ];
dst[d+1] = b64[ (n>>12) & 0x3F ];
Expand All @@ -37,6 +40,8 @@ base64_encode(void *vdst, size_t sizeof_dst, const void *vsrc, size_t sizeof_src
d += 4;
}

/* If the source text isn't an even multiple of 3 characters, then we'll
* have to append a '=' or '==' to the output to compensate */
if (i + 2 <= sizeof_src && d + 4 <= sizeof_dst) {
unsigned n = src[i]<<16 | src[i+1]<<8;
dst[d+0] = b64[ (n>>18) & 0x3F ];
Expand All @@ -57,28 +62,45 @@ base64_encode(void *vdst, size_t sizeof_dst, const void *vsrc, size_t sizeof_src
}


/****************************************************************************
****************************************************************************/
/*****************************************************************************
*****************************************************************************/
size_t
base64_decode(void *vdst, size_t sizeof_dst, const void *vsrc, size_t sizeof_src)
base64_decode(void *vdst, size_t sizeof_dst,
const void *vsrc, size_t sizeof_src)
{
static const unsigned char rstr[] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 62, 0xFF, 0xFF, 0xFF, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 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, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
size_t i = 0;
size_t d = 0;
Expand Down Expand Up @@ -137,14 +159,34 @@ base64_decode(void *vdst, size_t sizeof_dst, const void *vsrc, size_t sizeof_src
return d;
}

/*****************************************************************************
* Provide my own rand() simply to avoid static-analysis warning me that
* 'rand()' is unrandom, when in fact we want the non-random properties of
* rand() for regression testing.
*****************************************************************************/
static unsigned
r_rand(unsigned *seed)
{
static const unsigned a = 214013;
static const unsigned c = 2531011;

*seed = (*seed) * a + c;
return (*seed)>>16 & 0x7fff;
}

/*****************************************************************************
*****************************************************************************/
int
base64_selftest(void)
{
char buf[100];
char buf2[100];
char buf3[100];
size_t buf_len;
size_t buf2_len;
size_t buf3_len;
unsigned i;
unsigned seed = time(0);

buf_len = base64_encode(buf, sizeof(buf), "hello", 5);
buf2_len = base64_decode(buf2, sizeof(buf2), buf, buf_len);
Expand All @@ -153,5 +195,31 @@ base64_selftest(void)
return 1;
}

/*
* Generate a bunch of random strings, encode them, then decode them,
* making sure the final result matches the original string
*/
for (i=0; i<100; i++) {
unsigned j;

/* create a string of random bytes */
buf_len = r_rand(&seed) % 50;
for (j=0; j<buf_len; j++) {
buf[j] = (char)r_rand(&seed);
}

/* encode it */
buf2_len = base64_encode(buf2, sizeof(buf2), buf, buf_len);

/* decode it back again */
buf3_len = base64_decode(buf3, sizeof(buf3), buf2, buf2_len);

/* now make sure result equals original */
if (buf3_len != buf_len && memcmp(buf3, buf, buf_len) != 0) {
fprintf(stderr, "base64: selftest failed\n");
return 1;
}
}

return 0;
}
5 changes: 4 additions & 1 deletion src/proto-http.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ http_hello[] = "GET / HTTP/1.0\r\n"

/*****************************************************************************
*****************************************************************************/
static void
void
field_name(struct BannerOutput *banout, size_t id,
struct Patterns *xhttp_fields);
void
field_name(struct BannerOutput *banout, size_t id,
struct Patterns *xhttp_fields)
{
Expand Down
8 changes: 4 additions & 4 deletions src/proto-x509.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,16 +377,16 @@ x509_decode(struct CertDecode *x, const unsigned char *px, size_t length, struct
break;
case ISSUERNAME_CONTENTS:
//printf("%c", px[i]);
if (x->remainings[0] == 0)
; //printf("\n");
//if (x->remainings[0] == 0)
// printf("\n");
break;
case SUBJECTNAME_CONTENTS:
case EXT_CONTENTS:
//printf("%c", px[i]);
if (x->subject.type == Subject_Common)
banout_append(banout, PROTO_SSL3, px+i, 1);
if (x->remainings[0] == 0)
; //printf("\n");
//if (x->remainings[0] == 0)
// printf("\n");
break;
case VERSION_CONTENTS:
x->u.num <<= 8;
Expand Down
3 changes: 3 additions & 0 deletions src/rawsock-getroute.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ hexdump(const void *v, size_t len)
#define RTA_BRD 0x80 /* for NEWADDR, broadcast or p-p dest addr */
#endif

void
dump_rt_addresses(struct rt_msghdr *rtm);

void
dump_rt_addresses(struct rt_msghdr *rtm)
{
Expand Down
3 changes: 3 additions & 0 deletions src/rawsock-pfring.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ struct PFRING PFRING;
/***************************************************************************
* This checks whether the "pf_ring" driver is installed.
***************************************************************************/
int
PFRING_is_installed(void);

int
PFRING_is_installed(void)
{
Expand Down

0 comments on commit 093cbc9

Please sign in to comment.