Skip to content

Commit

Permalink
Use pointers to const instead of const pointers to const
Browse files Browse the repository at this point in the history
Declaring a `const char *const ptr` means you are declaring a pointer to
"readonly" memory, whose own address value is also immutable. The last
part is not relevant to library users, so remove it.

We want to communicate to the library user the important fact that the
inputs are never changed by a function, because the compiler could use
that information to do some optimization. But the library user doesn't
care whether we ever reassign the pointer internally to a different
location, because that's just an implementation detail.
  • Loading branch information
aklomp committed Aug 18, 2015
1 parent 4fd746e commit f637b70
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 90 deletions.
54 changes: 27 additions & 27 deletions README.md
Expand Up @@ -173,11 +173,11 @@ Set `flags` to `0` for the default behavior, which is runtime feature detection

```c
void base64_encode
( const char *const src
, size_t srclen
, char *const out
, size_t *const outlen
, int flags
( const char *src
, size_t srclen
, char *out
, size_t *outlen
, int flags
) ;
```

Expand All @@ -190,8 +190,8 @@ The buffer in `out` has been allocated by the caller and is at least 4/3 the siz

```c
void base64_stream_encode_init
( struct base64_state * state
, int flags
( struct base64_state *state
, int flags
) ;
```

Expand All @@ -201,11 +201,11 @@ Call this before calling `base64_stream_encode()` to init the state.

```c
void base64_stream_encode
( struct base64_state * state
, const char *const src
, size_t srclen
, char *const out
, size_t *const outlen
( struct base64_state *state
, const char *src
, size_t srclen
, char *out
, size_t *outlen
) ;
```

Expand All @@ -218,9 +218,9 @@ Does not zero-terminate or finalize the output.

```c
void base64_stream_encode_final
( struct base64_state * state
, char *const out
, size_t * outlen
( struct base64_state *state
, char *out
, size_t *outlen
) ;
```

Expand All @@ -234,11 +234,11 @@ Adds the required end-of-stream markers if appropriate.

```c
int base64_decode
( const char *const src
, size_t srclen
, char *const out
, size_t *const outlen
, int flags
( const char *src
, size_t srclen
, char *out
, size_t *outlen
, int flags
) ;
```

Expand All @@ -250,8 +250,8 @@ The buffer in `out` has been allocated by the caller and is at least 3/4 the siz

```c
void base64_stream_decode_init
( struct base64_state * state
, int flags
( struct base64_state *state
, int flags
) ;
```

Expand All @@ -261,11 +261,11 @@ Call this before calling `base64_stream_decode()` to init the state.

```c
int base64_stream_decode
( struct base64_state * state
, const char *const src
, size_t srclen
, char *const out
, size_t *const outlen
( struct base64_state *state
, const char *src
, size_t srclen
, char *out
, size_t *outlen
) ;
```

Expand Down
54 changes: 27 additions & 27 deletions include/libbase64.h
Expand Up @@ -28,18 +28,18 @@ struct base64_state {
* The buffer in `out` has been allocated by the caller and is at least 4/3 the
* size of the input. See above for `flags`; set to 0 for default operation: */
void base64_encode
( const char *const src
, size_t srclen
, char *const out
, size_t *const outlen
, int flags
( const char *src
, size_t srclen
, char *out
, size_t *outlen
, int flags
) ;

/* Call this before calling base64_stream_encode() to init the state. See above
* for `flags`; set to 0 for default operation: */
void base64_stream_encode_init
( struct base64_state * state
, int flags
( struct base64_state *state
, int flags
) ;

/* Encodes the block of data of given length at `src`, into the buffer at
Expand All @@ -48,40 +48,40 @@ void base64_stream_encode_init
* the number of new bytes written into `outlen` (which is set to zero when the
* function starts). Does not zero-terminate or finalize the output. */
void base64_stream_encode
( struct base64_state * state
, const char *const src
, size_t srclen
, char *const out
, size_t *const outlen
( struct base64_state *state
, const char *src
, size_t srclen
, char *out
, size_t *outlen
) ;

/* Finalizes the output begun by previous calls to `base64_stream_encode()`.
* Adds the required end-of-stream markers if appropriate. `outlen` is modified
* and will contain the number of new bytes written at `out` (which will quite
* often be zero). */
void base64_stream_encode_final
( struct base64_state * state
, char *const out
, size_t * outlen
( struct base64_state *state
, char *out
, size_t *outlen
) ;

/* Wrapper function to decode a plain string of given length. Output is written
* to *out without trailing zero. Output length in bytes is written to *outlen.
* The buffer in `out` has been allocated by the caller and is at least 3/4 the
* size of the input. See above for `flags`, set to 0 for default operation: */
int base64_decode
( const char *const src
, size_t srclen
, char *const out
, size_t *const outlen
, int flags
( const char *src
, size_t srclen
, char *out
, size_t *outlen
, int flags
) ;

/* Call this before calling base64_stream_decode() to init the state. See above
* for `flags`; set to 0 for default operation: */
void base64_stream_decode_init
( struct base64_state * state
, int flags
( struct base64_state *state
, int flags
) ;

/* Decodes the block of data of given length at `src`, into the buffer at
Expand All @@ -93,11 +93,11 @@ void base64_stream_decode_init
* Returns -1 if the chosen codec is not included in the current build. Used by
* the test harness to check whether a codec is available for testing. */
int base64_stream_decode
( struct base64_state * state
, const char *const src
, size_t srclen
, char *const out
, size_t *const outlen
( struct base64_state *state
, const char *src
, size_t srclen
, char *out
, size_t *outlen
) ;

#ifdef __cplusplus
Expand Down
20 changes: 10 additions & 10 deletions lib/codecs.h
@@ -1,19 +1,19 @@
/* Function parameters for encoding functions */
#define BASE64_ENC_PARAMS \
( struct base64_state * state \
, const char *const src \
, size_t srclen \
, char *const out \
, size_t *const outlen \
( struct base64_state *state \
, const char *src \
, size_t srclen \
, char *out \
, size_t *outlen \
)

/* Function parameters for decoding functions */
#define BASE64_DEC_PARAMS \
( struct base64_state * state \
, const char *const src \
, size_t srclen \
, char *const out \
, size_t *const outlen \
( struct base64_state *state \
, const char *src \
, size_t srclen \
, char *out \
, size_t *outlen \
)

/* Function signature for encoding functions */
Expand Down
46 changes: 23 additions & 23 deletions lib/lib.c
Expand Up @@ -56,21 +56,21 @@ base64_stream_encode_init (struct base64_state *state, int flags)

void
base64_stream_encode
( struct base64_state * state
, const char *const src
, size_t srclen
, char *const out
, size_t *const outlen
( struct base64_state *state
, const char *src
, size_t srclen
, char *out
, size_t *outlen
)
{
codec.enc(state, src, srclen, out, outlen);
}

void
base64_stream_encode_final
( struct base64_state * state
, char *const out
, size_t *const outlen
( struct base64_state *state
, char *out
, size_t *outlen
)
{
char *o = out;
Expand Down Expand Up @@ -106,23 +106,23 @@ base64_stream_decode_init (struct base64_state *state, int flags)

int
base64_stream_decode
( struct base64_state * state
, const char *const src
, size_t srclen
, char *const out
, size_t *const outlen
( struct base64_state *state
, const char *src
, size_t srclen
, char *out
, size_t *outlen
)
{
return codec.dec(state, src, srclen, out, outlen);
}

void
base64_encode
( const char *const src
, size_t srclen
, char *const out
, size_t *const outlen
, int flags
( const char *src
, size_t srclen
, char *out
, size_t *outlen
, int flags
)
{
size_t s;
Expand All @@ -144,11 +144,11 @@ base64_encode

int
base64_decode
( const char *const src
, size_t srclen
, char *const out
, size_t * outlen
, int flags
( const char *src
, size_t srclen
, char *out
, size_t *outlen
, int flags
)
{
struct base64_state state;
Expand Down
6 changes: 3 additions & 3 deletions test/benchmark.c
Expand Up @@ -54,7 +54,7 @@ timediff_sec (struct timespec *start, struct timespec *end)
}

static void
codec_bench_enc (struct buffers *b, char *const name, unsigned int flags)
codec_bench_enc (struct buffers *b, const char *name, unsigned int flags)
{
int i;
float timediff, fastest = -1.0f;
Expand All @@ -74,7 +74,7 @@ codec_bench_enc (struct buffers *b, char *const name, unsigned int flags)
}

static void
codec_bench_dec (struct buffers *b, char *const name, unsigned int flags)
codec_bench_dec (struct buffers *b, const char *name, unsigned int flags)
{
int i;
float timediff, fastest = -1.0f;
Expand All @@ -94,7 +94,7 @@ codec_bench_dec (struct buffers *b, char *const name, unsigned int flags)
}

static void
codec_bench (struct buffers *b, char *const name, unsigned int flags)
codec_bench (struct buffers *b, const char *name, unsigned int flags)
{
codec_bench_enc(b, name, flags);
codec_bench_dec(b, name, flags);
Expand Down

0 comments on commit f637b70

Please sign in to comment.