From 7b84cf6fa63c40a4d0aa9373095e7021eaf708a8 Mon Sep 17 00:00:00 2001
From: Tilghman Lesher <tilghman@meg.abyt.es>
Date: Thu, 26 Jun 2008 17:06:17 +0000
Subject: [PATCH] Convert casts to unions, to fix alignment issues on Solaris
 (closes issue #12932)  Reported by: snuffy  Patches:       
 bug_12932_20080627.diff uploaded by snuffy (license 35)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@125386 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 codecs/codec_a_mu.c          |  4 ++--
 codecs/codec_adpcm.c         |  4 ++--
 codecs/codec_alaw.c          |  4 ++--
 codecs/codec_g722.c          |  4 ++--
 codecs/codec_g726.c          | 10 +++++-----
 codecs/codec_gsm.c           |  4 ++--
 codecs/codec_ilbc.c          |  4 ++--
 codecs/codec_lpc10.c         |  4 ++--
 codecs/codec_resample.c      |  2 +-
 codecs/codec_speex.c         |  4 ++--
 codecs/codec_ulaw.c          |  4 ++--
 include/asterisk/translate.h | 17 +++++++++++------
 main/translate.c             |  6 +++---
 13 files changed, 38 insertions(+), 33 deletions(-)

diff --git a/codecs/codec_a_mu.c b/codecs/codec_a_mu.c
index 23f04f0550..cf03547206 100644
--- a/codecs/codec_a_mu.c
+++ b/codecs/codec_a_mu.c
@@ -47,7 +47,7 @@ static int alawtoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	int x = f->samples;
 	unsigned char *src = f->data.ptr;
-	unsigned char *dst = (unsigned char *)pvt->outbuf + pvt->samples;
+	unsigned char *dst = pvt->outbuf.uc + pvt->samples;
 
 	pvt->samples += x;
 	pvt->datalen += x;
@@ -63,7 +63,7 @@ static int ulawtoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	int x = f->samples;
 	unsigned char *src = f->data.ptr;
-	unsigned char *dst = (unsigned char *)pvt->outbuf + pvt->samples;
+	unsigned char *dst = pvt->outbuf.uc + pvt->samples;
 
 	pvt->samples += x;
 	pvt->datalen += x;
diff --git a/codecs/codec_adpcm.c b/codecs/codec_adpcm.c
index cad4ded750..5cdf541765 100644
--- a/codecs/codec_adpcm.c
+++ b/codecs/codec_adpcm.c
@@ -230,7 +230,7 @@ static int adpcmtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 	struct adpcm_decoder_pvt *tmp = pvt->pvt;
 	int x = f->datalen;
 	unsigned char *src = f->data.ptr;
-	int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
+	int16_t *dst = pvt->outbuf.i16 + pvt->samples;
 
 	while (x--) {
 		*dst++ = decode((*src >> 4) & 0xf, &tmp->state);
@@ -265,7 +265,7 @@ static struct ast_frame *lintoadpcm_frameout(struct ast_trans_pvt *pvt)
 	pvt->samples &= ~1; /* atomic size is 2 samples */
 
 	for (i = 0; i < pvt->samples; i += 2) {
-		pvt->outbuf[i/2] =
+		pvt->outbuf.c[i/2] =
 			(adpcm(tmp->inbuf[i  ], &tmp->state) << 4) |
 			(adpcm(tmp->inbuf[i+1], &tmp->state)     );
 	};
diff --git a/codecs/codec_alaw.c b/codecs/codec_alaw.c
index b2f4f94357..a261dedb7a 100644
--- a/codecs/codec_alaw.c
+++ b/codecs/codec_alaw.c
@@ -45,7 +45,7 @@ static int alawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	int i = f->samples;
 	unsigned char *src = f->data.ptr;
-	int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
+	int16_t *dst = pvt->outbuf.i16 + pvt->samples;
 
 	pvt->samples += i;
 	pvt->datalen += i * 2;	/* 2 bytes/sample */
@@ -60,7 +60,7 @@ static int alawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	int i = f->samples;
-	char *dst = pvt->outbuf + pvt->samples;
+	char *dst = pvt->outbuf.c + pvt->samples;
 	int16_t *src = f->data.ptr;
 
 	pvt->samples += i;
diff --git a/codecs/codec_g722.c b/codecs/codec_g722.c
index a574136e82..19e6b33b1e 100644
--- a/codecs/codec_g722.c
+++ b/codecs/codec_g722.c
@@ -107,7 +107,7 @@ static int g722tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 	/* g722_decode expects the samples to be in the invalid samples / 2 format */
 	in_samples = f->samples / 2;
 
-	out_samples = g722_decode(&tmp->g722, (int16_t *) &pvt->outbuf[pvt->samples * sizeof(int16_t)], 
+	out_samples = g722_decode(&tmp->g722, &pvt->outbuf.i16[pvt->samples * sizeof(int16_t)], 
 		(uint8_t *) f->data.ptr, in_samples);
 
 	pvt->samples += out_samples;
@@ -122,7 +122,7 @@ static int lintog722_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 	struct g722_encoder_pvt *tmp = pvt->pvt;
 	int outlen;
 
-	outlen = g722_encode(&tmp->g722, (uint8_t *) (&pvt->outbuf[pvt->datalen]), 
+	outlen = g722_encode(&tmp->g722, (&pvt->outbuf.ui8[pvt->datalen]), 
 		(int16_t *) f->data.ptr, f->samples);
 
 	pvt->samples += outlen * 2;
diff --git a/codecs/codec_g726.c b/codecs/codec_g726.c
index 8d1346e669..80e0e8ea98 100644
--- a/codecs/codec_g726.c
+++ b/codecs/codec_g726.c
@@ -693,7 +693,7 @@ static int g726aal2tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f
 {
 	struct g726_coder_pvt *tmp = pvt->pvt;
 	unsigned char *src = f->data.ptr;
-	int16_t *dst = (int16_t *) pvt->outbuf + pvt->samples;
+	int16_t *dst = pvt->outbuf.i16 + pvt->samples;
 	unsigned int i;
 
 	for (i = 0; i < f->datalen; i++) {
@@ -718,7 +718,7 @@ static int lintog726aal2_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 		unsigned char d = g726_encode(src[i], &tmp->g726); /* this sample */
 
 		if (tmp->next_flag & 0x80) {	/* merge with leftover sample */
-			pvt->outbuf[pvt->datalen++] = ((tmp->next_flag & 0xf)<< 4) | d;
+			pvt->outbuf.c[pvt->datalen++] = ((tmp->next_flag & 0xf)<< 4) | d;
 			pvt->samples += 2;	/* 2 samples per byte */
 			tmp->next_flag = 0;
 		} else {
@@ -734,7 +734,7 @@ static int g726tolin_framein (struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	struct g726_coder_pvt *tmp = pvt->pvt;
 	unsigned char *src = f->data.ptr;
-	int16_t *dst = (int16_t *) pvt->outbuf + pvt->samples;
+	int16_t *dst = pvt->outbuf.i16 + pvt->samples;
 	unsigned int i;
 
 	for (i = 0; i < f->datalen; i++) {
@@ -759,7 +759,7 @@ static int lintog726_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 		unsigned char d = g726_encode(src[i], &tmp->g726); /* this sample */
 
 		if (tmp->next_flag & 0x80) {	/* merge with leftover sample */
-			pvt->outbuf[pvt->datalen++] = (d << 4) | (tmp->next_flag & 0xf);
+			pvt->outbuf.c[pvt->datalen++] = (d << 4) | (tmp->next_flag & 0xf);
 			pvt->samples += 2;	/* 2 samples per byte */
 			tmp->next_flag = 0;
 		} else {
@@ -774,7 +774,7 @@ static int lintog726_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 static int g726tog726aal2_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	unsigned char *src = f->data.ptr;
-	unsigned char *dst = (unsigned char *) pvt->outbuf + pvt->samples;
+	unsigned char *dst = pvt->outbuf.uc + pvt->samples;
 	unsigned int i;
 
 	for (i = 0; i < f->datalen; i++)
diff --git a/codecs/codec_gsm.c b/codecs/codec_gsm.c
index 805e79b66b..defc1d8988 100644
--- a/codecs/codec_gsm.c
+++ b/codecs/codec_gsm.c
@@ -103,7 +103,7 @@ static int gsmtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	struct gsm_translator_pvt *tmp = pvt->pvt;
 	int x;
-	int16_t *dst = (int16_t *)pvt->outbuf;
+	int16_t *dst = pvt->outbuf.i16;
 	/* guess format from frame len. 65 for MSGSM, 33 for regular GSM */
 	int flen = (f->datalen % MSGSM_FRAME_LEN == 0) ?
 		MSGSM_FRAME_LEN : GSM_FRAME_LEN;
@@ -176,7 +176,7 @@ static struct ast_frame *lintogsm_frameout(struct ast_trans_pvt *pvt)
 		return NULL;
 	while (pvt->samples >= GSM_SAMPLES) {
 		/* Encode a frame of data */
-		gsm_encode(tmp->gsm, tmp->buf + samples, (gsm_byte *) pvt->outbuf + datalen);
+		gsm_encode(tmp->gsm, tmp->buf + samples, (gsm_byte *) pvt->outbuf.c + datalen);
 		datalen += GSM_FRAME_LEN;
 		samples += GSM_SAMPLES;
 		pvt->samples -= GSM_SAMPLES;
diff --git a/codecs/codec_ilbc.c b/codecs/codec_ilbc.c
index 3f9b10edb2..4914392a43 100644
--- a/codecs/codec_ilbc.c
+++ b/codecs/codec_ilbc.c
@@ -114,7 +114,7 @@ static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 	/* Assuming there's space left, decode into the current buffer at
 	   the tail location.  Read in as many frames as there are */
 	int x,i;
-	int16_t *dst = (int16_t *)pvt->outbuf;
+	int16_t *dst = pvt->outbuf.i16;
 	float tmpf[ILBC_SAMPLES];
 
 	if (f->datalen == 0) { /* native PLC, set fake f->datalen and clear plc_mode */
@@ -174,7 +174,7 @@ static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt)
 		/* Encode a frame of data */
 		for (i = 0 ; i < ILBC_SAMPLES ; i++)
 			tmpf[i] = tmp->buf[samples + i];
-		iLBC_encode((unsigned char *) pvt->outbuf + datalen, tmpf, &tmp->enc);
+		iLBC_encode( pvt->outbuf.uc + datalen, tmpf, &tmp->enc);
 
 		datalen += ILBC_FRAME_LEN;
 		samples += ILBC_SAMPLES;
diff --git a/codecs/codec_lpc10.c b/codecs/codec_lpc10.c
index dfcdc79882..abea9d2043 100644
--- a/codecs/codec_lpc10.c
+++ b/codecs/codec_lpc10.c
@@ -140,7 +140,7 @@ static void build_bits(unsigned char *c, INT32 *bits)
 static int lpc10tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	struct lpc10_coder_pvt *tmp = pvt->pvt;
-	int16_t *dst = (int16_t *)pvt->outbuf;
+	int16_t *dst = pvt->outbuf.i16;
 	int len = 0;
 
 	while (len + LPC10_BYTES_IN_COMPRESSED_FRAME <= f->datalen) {
@@ -200,7 +200,7 @@ static struct ast_frame *lintolpc10_frameout(struct ast_trans_pvt *pvt)
 		for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++)
 			tmpbuf[x] = (float)tmp->buf[x + samples] / 32768.0;
 		lpc10_encode(tmpbuf, bits, tmp->lpc10.enc);
-		build_bits((unsigned char *) pvt->outbuf + datalen, bits);
+		build_bits(pvt->outbuf.uc + datalen, bits);
 		datalen += LPC10_BYTES_IN_COMPRESSED_FRAME;
 		samples += LPC10_SAMPLES_PER_FRAME;
 		pvt->samples -= LPC10_SAMPLES_PER_FRAME;
diff --git a/codecs/codec_resample.c b/codecs/codec_resample.c
index bdf9d19f91..f0ae03307f 100644
--- a/codecs/codec_resample.c
+++ b/codecs/codec_resample.c
@@ -103,7 +103,7 @@ static int resample_frame(struct ast_trans_pvt *pvt,
 	int total_in_buf_used = 0;
 	int total_out_buf_used = 0;
 	int16_t *in_buf = (int16_t *) f->data.ptr;
-	int16_t *out_buf = (int16_t *) pvt->outbuf + pvt->samples;
+	int16_t *out_buf = pvt->outbuf.i16 + pvt->samples;
 	float in_buf_f[f->samples];
 	float out_buf_f[2048];
 	int res = 0;
diff --git a/codecs/codec_speex.c b/codecs/codec_speex.c
index b9f8795351..734491c75d 100644
--- a/codecs/codec_speex.c
+++ b/codecs/codec_speex.c
@@ -193,7 +193,7 @@ static int speextolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 	   the tail location.  Read in as many frames as there are */
 	int x;
 	int res;
-	int16_t *dst = (int16_t *)pvt->outbuf;
+	int16_t *dst = pvt->outbuf.i16;
 	/* XXX fout is a temporary buffer, may have different types */
 #ifdef _SPEEX_TYPES_H
 	spx_int16_t fout[1024];
@@ -316,7 +316,7 @@ static struct ast_frame *lintospeex_frameout(struct ast_trans_pvt *pvt)
 
 	/* Terminate bit stream */
 	speex_bits_pack(&tmp->bits, 15, 5);
-	datalen = speex_bits_write(&tmp->bits, pvt->outbuf, pvt->t->buf_size);
+	datalen = speex_bits_write(&tmp->bits, pvt->outbuf.c, pvt->t->buf_size);
 	return ast_trans_frameout(pvt, datalen, samples);
 }
 
diff --git a/codecs/codec_ulaw.c b/codecs/codec_ulaw.c
index 2e0a16467d..d58abe9267 100644
--- a/codecs/codec_ulaw.c
+++ b/codecs/codec_ulaw.c
@@ -45,7 +45,7 @@ static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	int i = f->samples;
 	unsigned char *src = f->data.ptr;
-	int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
+	int16_t *dst = pvt->outbuf.i16 + pvt->samples;
 
 	pvt->samples += i;
 	pvt->datalen += i * 2;	/* 2 bytes/sample */
@@ -61,7 +61,7 @@ static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	int i = f->samples;
-	char *dst = pvt->outbuf + pvt->samples;
+	char *dst = pvt->outbuf.c + pvt->samples;
 	int16_t *src = f->data.ptr;
 
 	pvt->samples += i;
diff --git a/include/asterisk/translate.h b/include/asterisk/translate.h
index 97f10283f3..01841ee017 100644
--- a/include/asterisk/translate.h
+++ b/include/asterisk/translate.h
@@ -136,14 +136,19 @@ struct ast_translator {
  */
 struct ast_trans_pvt {
 	struct ast_translator *t;
-	struct ast_frame f;	/*!< used in frameout */
-	int samples;		/*!< samples available in outbuf */
+	struct ast_frame f;         /*!< used in frameout */
+	int samples;                /*!< samples available in outbuf */
 	/*! \brief actual space used in outbuf */
 	int datalen;
-	void *pvt;		/*!< more private data, if any */
-	char *outbuf;		/*!< the useful portion of the buffer */
-	plc_state_t *plc;	/*!< optional plc pointer */
-	struct ast_trans_pvt *next;	/*!< next in translator chain */
+	void *pvt;                  /*!< more private data, if any */
+	union {
+		char *c;                /*!< the useful portion of the buffer */
+		unsigned char *uc;      /*!< the useful portion of the buffer */
+		int16_t *i16;
+		uint8_t *ui8;
+	} outbuf; 
+	plc_state_t *plc;           /*!< optional plc pointer */
+	struct ast_trans_pvt *next; /*!< next in translator chain */
 	struct timeval nextin;
 	struct timeval nextout;
 	unsigned int destroy:1;
diff --git a/main/translate.c b/main/translate.c
index 9326f4880c..4ee94d940e 100644
--- a/main/translate.c
+++ b/main/translate.c
@@ -119,7 +119,7 @@ static void *newpvt(struct ast_translator *t)
 		ofs += sizeof(plc_state_t);
 	}
 	if (t->buf_size)		/* finally buffer and header */
-		pvt->outbuf = ofs + AST_FRIENDLY_OFFSET;
+		pvt->outbuf.c = ofs + AST_FRIENDLY_OFFSET;
 	/* call local init routine, if present */
 	if (t->newpvt && t->newpvt(pvt)) {
 		ast_free(pvt);
@@ -153,7 +153,7 @@ static void destroy(struct ast_trans_pvt *pvt)
 /*! \brief framein wrapper, deals with plc and bound checks.  */
 static int framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
-	int16_t *dst = (int16_t *)pvt->outbuf;
+	int16_t *dst = pvt->outbuf.i16;
 	int ret;
 	int samples = pvt->samples;	/* initial value */
 	
@@ -235,7 +235,7 @@ struct ast_frame *ast_trans_frameout(struct ast_trans_pvt *pvt,
 	f->mallocd = 0;
 	f->offset = AST_FRIENDLY_OFFSET;
 	f->src = pvt->t->name;
-	f->data.ptr = pvt->outbuf;
+	f->data.ptr = pvt->outbuf.c;
 
 	ast_set_flag(f, AST_FRFLAG_FROM_TRANSLATOR);
 
-- 
GitLab