raop: Move base64 implementation to a util file

Base64 implementation is now in a common file called raop_util.c.
Old Base64 files are removed but copyright is preserved.

Original patch by Martin Blanchard, patch splitted by
Hajime Fujita <crisp.fujita@nifty.com>.
This commit is contained in:
Martin Blanchard 2016-11-06 12:54:04 -06:00 committed by Tanu Kaskinen
parent 31ded701b1
commit bac8a2ba66
5 changed files with 70 additions and 62 deletions

View file

@ -1155,9 +1155,9 @@ librtp_la_LIBADD = $(AM_LIBADD) libpulsecore-@PA_MAJORMINOR@.la libpulsecommon-@
libraop_la_SOURCES = \
modules/raop/raop_client.c modules/raop/raop_client.h \
modules/raop/raop_packet_buffer.h modules/raop/raop_packet_buffer.c \
modules/raop/raop_crypto.c modules/raop/raop_crypto.h \
modules/raop/base64.c modules/raop/base64.h \
modules/raop/raop_packet_buffer.h modules/raop/raop_packet_buffer.c
modules/raop/raop_util.c modules/raop/raop_util.h
libraop_la_CFLAGS = $(AM_CFLAGS) $(OPENSSL_CFLAGS) -I$(top_srcdir)/src/modules/rtp
libraop_la_LDFLAGS = $(AM_LDFLAGS) $(AM_LIBLDFLAGS) -avoid-version
libraop_la_LIBADD = $(AM_LIBADD) $(OPENSSL_LIBS) libpulsecore-@PA_MAJORMINOR@.la librtp.la libpulsecommon-@PA_MAJORMINOR@.la libpulse.la

View file

@ -53,7 +53,7 @@
#include "rtsp_client.h"
#include "raop_packet_buffer.h"
#include "raop_crypto.h"
#include "base64.h"
#include "raop_util.h"
#define JACK_STATUS_DISCONNECTED 0
#define JACK_STATUS_CONNECTED 1
@ -528,7 +528,7 @@ static void do_rtsp_announce(pa_raop_client *c) {
/* UDP protocol does not need "Apple-Challenge" at announce. */
if (c->protocol == RAOP_TCP) {
pa_random(&rand_data, sizeof(rand_data));
pa_base64_encode(&rand_data, sizeof(rand_data), &sac);
pa_raop_base64_encode(&rand_data, 8*sizeof(rand_data), &sac);
rtrimchar(sac, '=');
pa_rtsp_add_header(c->rtsp, "Apple-Challenge", sac);
}
@ -689,7 +689,7 @@ static void udp_rtsp_cb(pa_rtsp_client *rtsp, pa_rtsp_state state, pa_headerlist
/* Set the Apple-Challenge key */
pa_random(&rand, sizeof(rand));
pa_base64_encode(&rand, sizeof(rand), &sac);
pa_raop_base64_encode(&rand, 8*sizeof(rand), &sac);
rtrimchar(sac, '=');
pa_rtsp_add_header(c->rtsp, "Apple-Challenge", sac);

View file

@ -37,7 +37,7 @@
#include <pulsecore/random.h>
#include "raop_crypto.h"
#include "base64.h"
#include "raop_util.h"
#define AES_CHUNK_SIZE 16
@ -85,9 +85,9 @@ static int rsa_encrypt(uint8_t *data, int len, uint8_t *str) {
pa_assert(str);
rsa = RSA_new();
size = pa_base64_decode(rsa_modulus, modules);
size = pa_raop_base64_decode(rsa_modulus, modules);
n_bn = BN_bin2bn(modules, size, NULL);
size = pa_base64_decode(rsa_exponent, exponent);
size = pa_raop_base64_decode(rsa_exponent, exponent);
e_bn = BN_bin2bn(exponent, size, NULL);
RSA_set0_key(rsa, n_bn, e_bn, NULL);
@ -120,7 +120,7 @@ char* pa_raop_secret_get_iv(pa_raop_secret *s) {
pa_assert(s);
pa_base64_encode(s->iv, AES_CHUNK_SIZE, &base64_iv);
pa_raop_base64_encode(s->iv, AES_CHUNK_SIZE, &base64_iv);
return base64_iv;
}
@ -134,7 +134,7 @@ char* pa_raop_secret_get_key(pa_raop_secret *s) {
/* Encrypt our AES public key to send to the device */
size = rsa_encrypt(s->key, AES_CHUNK_SIZE, rsa_key);
pa_base64_encode(rsa_key, size, &base64_key);
pa_raop_base64_encode(rsa_key, size, &base64_key);
return base64_key;
}

View file

@ -1,7 +1,8 @@
/***
This file is part of PulseAudio.
Copyright 2008 Colin Guthrie
Copyright 2013 Martin Blanchard
Copyright Kungliga Tekniska Høgskolan & Colin Guthrie
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
@ -18,8 +19,8 @@
***/
/***
This file was originally inspired by a file developed by
Kungliga Tekniska högskolan
The base64 implementation was originally inspired by a file developed
by Kungliga Tekniska högskolan.
***/
#ifdef HAVE_CONFIG_H
@ -31,12 +32,16 @@
#include <pulse/xmalloc.h>
#include "base64.h"
#include <pulsecore/macro.h>
#include "raop_util.h"
#define BASE64_DECODE_ERROR 0xffffffff
static const char base64_chars[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static int pos(char c) {
static int char_position(char c) {
if (c >= 'A' && c <= 'Z')
return c - 'A' + 0;
if (c >= 'a' && c <= 'z')
@ -51,79 +56,81 @@ static int pos(char c) {
return -1;
}
int pa_base64_encode(const void *data, int size, char **str) {
char *s, *p;
static unsigned int token_decode(const char *token) {
unsigned int val = 0;
int marker = 0;
int i;
int c;
const unsigned char *q;
p = s = pa_xnew(char, size * 4 / 3 + 4);
if (strlen(token) < 4)
return BASE64_DECODE_ERROR;
for (i = 0; i < 4; i++) {
val *= 64;
if (token[i] == '=')
marker++;
else if (marker > 0)
return BASE64_DECODE_ERROR;
else {
int lpos = char_position(token[i]);
if (lpos < 0)
return BASE64_DECODE_ERROR;
val += lpos;
}
}
if (marker > 2)
return BASE64_DECODE_ERROR;
return (marker << 24) | val;
}
int pa_raop_base64_encode(const void *data, int len, char **str) {
const unsigned char *q;
char *p, *s = NULL;
int i, c;
pa_assert(data);
pa_assert(str);
p = s = pa_xnew(char, len * 4 / 3 + 4);
q = (const unsigned char *) data;
for (i = 0; i < size;) {
for (i = 0; i < len;) {
c = q[i++];
c *= 256;
if (i < size)
if (i < len)
c += q[i];
i++;
c *= 256;
if (i < size)
if (i < len)
c += q[i];
i++;
p[0] = base64_chars[(c & 0x00fc0000) >> 18];
p[1] = base64_chars[(c & 0x0003f000) >> 12];
p[2] = base64_chars[(c & 0x00000fc0) >> 6];
p[3] = base64_chars[(c & 0x0000003f) >> 0];
if (i > size)
if (i > len)
p[3] = '=';
if (i > size + 1)
if (i > len + 1)
p[2] = '=';
p += 4;
}
*p = 0;
*str = s;
return strlen(s);
}
#define DECODE_ERROR 0xffffffff
static unsigned int token_decode(const char *token) {
int i;
unsigned int val = 0;
int marker = 0;
if (strlen(token) < 4)
return DECODE_ERROR;
for (i = 0; i < 4; i++) {
val *= 64;
if (token[i] == '=')
marker++;
else if (marker > 0)
return DECODE_ERROR;
else {
int lpos = pos(token[i]);
if (lpos < 0)
return DECODE_ERROR;
val += lpos;
}
}
if (marker > 2)
return DECODE_ERROR;
return (marker << 24) | val;
}
int pa_base64_decode(const char *str, void *data) {
int pa_raop_base64_decode(const char *str, void *data) {
const char *p;
unsigned char *q;
pa_assert(str);
pa_assert(data);
q = data;
for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4) {
unsigned int val = token_decode(p);
unsigned int marker = (val >> 24) & 0xff;
if (val == DECODE_ERROR)
if (val == BASE64_DECODE_ERROR)
return -1;
*q++ = (val >> 16) & 0xff;
if (marker < 2)

View file

@ -1,11 +1,12 @@
#ifndef foobase64hfoo
#define foobase64hfoo
#ifndef fooraoputilfoo
#define fooraoputilfoo
/***
This file is part of PulseAudio.
Copyright 2008 Colin Guthrie
Copyright Kungliga Tekniska högskolan
Copyright 2013 Martin Blanchard
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
@ -23,10 +24,10 @@
/***
This file was originally inspired by a file developed by
Kungliga Tekniska högskolan
Kungliga Tekniska högskolan.
***/
int pa_base64_encode(const void *data, int size, char **str);
int pa_base64_decode(const char *str, void *data);
int pa_raop_base64_encode(const void *data, int len, char **str);
int pa_raop_base64_decode(const char *str, void *data);
#endif