--- /tmp/app_sms.c	2004-12-07 17:04:59.000000000 +0000
+++ apps/app_sms.c	2004-12-07 17:04:51.000000000 +0000
@@ -16,26 +16,28 @@
 #include <asterisk/channel.h>
 #include <asterisk/pbx.h>
 #include <asterisk/module.h>
+#include <asterisk/alaw.h>
 #include <asterisk/callerid.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <stdlib.h>
-#include <unistd.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <dirent.h>
 #include <ctype.h>
 #include "../astconf.h"
 
+/* output using Alaw rather than linear */
+/*#define OUTALAW */
+
 /* ToDo */
-/* When acting as SC and answering, should check for messages and send instead of sending EST as first packet */
 /* Add full VP support */
 /* Handle status report messages (generation and reception) */
-/* Log to show oa and da with no spaces to allow parsing */
-/* USC2 coding */
+/* Time zones on time stamps */
+/* user ref field */
 
-static unsigned char message_ref;	/* arbitary message ref */
+static volatile unsigned char message_ref;	/* arbitary message ref */
 
 static char log_file[255];
 static char spool_dir[255];
@@ -58,7 +60,7 @@
   "Argument 'a' means answer, i.e. send initial FSK packet.\n"
   "Argument 's' means act as service centre talking to a phone.\n"
   "Messages are processed as per text file message queues.\n"
-  "Can also call as SMS(name|[s]|number|message) to queue a message.\n";
+  "smsq is a command to generate message queues and send messages.\n";
 
 static signed short wave[] =
   { 0, 392, 782, 1167, 1545, 1913, 2270, 2612, 2939, 3247, 3536, 3802, 4045,
@@ -73,18 +75,20 @@
     -1913, -1545, -1167, -782, -392
 };
 
+#ifdef OUTALAW
+static unsigned char wavea[80];
+#endif
+
 STANDARD_LOCAL_USER;
 
 LOCAL_USER_DECL;
 
-/* SMS 7 bit character mapping */
-/* Note that some greek characters are simply coded as 191 (inverted question mark) as ISO-8859-1 does not do greek */
-/* Note 27 (escape) is to be displayed as a space as per GSM 03.38 */
-static unsigned char sms7to8[] = {
-  '@', 163, '$', 165, 232, 233, 249, 236, 242, 199, 10, 216, 248, 13, 197,
-    229,
-  191, '_', 191, 191, 191, 191, 191, 191, 191, 191, 191, ' ', 198, 230, 223,
-    201,
+/* SMS 7 bit character mapping to UCS-2 */
+static const unsigned short defaultalphabet[] = {
+  0x0040, 0x00A3, 0x0024, 0x00A5, 0x00E8, 0x00E9, 0x00F9, 0x00EC,
+  0x00F2, 0x00E7, 0x000A, 0x00D8, 0x00F8, 0x000D, 0x00C5, 0x00E5,
+  0x0394, 0x005F, 0x03A6, 0x0393, 0x039B, 0x03A9, 0x03A0, 0x03A8,
+  0x03A3, 0x0398, 0x039E, 0x00A0, 0x00C6, 0x00E6, 0x00DF, 0x00C9,
   ' ', '!', '"', '#', 164, '%', '&', 39, '(', ')', '*', '+', ',', '-', '.',
     '/',
   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>',
@@ -98,24 +102,40 @@
   'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 228, 246, 241, 252,
     224,
 };
-unsigned char sms8to7[256];
+
+static const unsigned short escapes[] = {
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x000C, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0x005E, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0x007B, 0x007D, 0, 0, 0, 0, 0, 0x005C,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x005B, 0x007E, 0x005D, 0,
+  0x007C, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0x20AC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+};
+
+#define SMSLEN 160		/* max SMS length */
 
 typedef struct sms_s
 {
   unsigned char hangup;		/* we are done... */
-  unsigned char smsc;		/* we are SMSC */
+  unsigned char smsc:1;		/* we are SMSC */
+  unsigned char rx:1;		/* this is a received message */
   char queue[30];		/* queue name */
   char oa[20];			/* originating address */
   char da[20];			/* destination address */
-  time_t scts;			/* time stamp */
+  time_t scts;			/* time stamp, UTC */
   unsigned char pid;		/* protocol ID */
   unsigned char dcs;		/* data coding scheme */
-  unsigned char mr;		/* message reference */
-  unsigned char udl;		/* user date length */
+  short mr;			/* message reference - actually a byte, but usde -1 for not set */
+  int udl;			/* user data length */
+  int udhl;			/* user data header length */
   unsigned char srr:1;		/* Status Report request */
+  unsigned char udhi:1;		/* User Data Header required, even if length 0 */
   unsigned char rp:1;		/* Reply Path */
   unsigned int vp;		/* validity period in minutes, 0 for not set */
-  unsigned char ud[160];	/* user data (message) */
+  unsigned short ud[SMSLEN];	/* user data (message), UCS-2 coded */
+  unsigned char udh[SMSLEN];	/* user data header */
   unsigned char cli[20];	/* caller ID */
   unsigned char ophase;		/* phase (0-79) for 0 and 1 frequencies (1300Hz and 2100Hz) */
   unsigned char ophasep;	/* phase (0-79) for 1200 bps */
@@ -144,6 +164,11 @@
   /* more to go here */
 } sms_t;
 
+/* different types of encoding */
+#define is7bit(dcs) (((dcs)&0xC0)?(!((dcs)&4)):(!((dcs)&12)))
+#define is8bit(dcs) (((dcs)&0xC0)?(((dcs)&4)):(((dcs)&12)==4))
+#define is16bit(dcs) (((dcs)&0xC0)?0:(((dcs)&12)==8))
+
 static void *
 sms_alloc (struct ast_channel *chan, void *params)
 {
@@ -177,53 +202,273 @@
 isodate (time_t t)
 {				/* static, return a date/time in ISO format */
   static char date[20];
-  strftime (date, sizeof (date), "%Y-%m-%d %H:%M:%S", localtime (&t));
+  strftime (date, sizeof (date), "%Y-%m-%dT%H:%M:%S", localtime (&t));
   return date;
 }
 
-/* pack n bytes from i to o and return number of bytes */
-static unsigned char
-pack7 (unsigned char *o, unsigned char *i, unsigned char n)
+/* reads next UCS character from null terminated UTF-8 string and advanced pointer */
+/* for non valid UTF-8 sequences, returns character as is */
+/* Does not advance pointer for null termination */
+static long
+utf8decode (unsigned char **pp)
+{
+  unsigned char *p = *pp;
+  if (!*p)
+    return 0;			/* null termination of string */
+  (*pp)++;
+  if (*p < 0xC0)
+    return *p;			/* ascii or continuation character */
+  if (*p < 0xE0)
+    {
+      if (*p < 0xC2 || (p[1] & 0xC0) != 0x80)
+	return *p;		/* not valid UTF-8 */
+      (*pp)++;
+      return ((*p & 0x1F) << 6) + (p[1] & 0x3F);
+    }
+  if (*p < 0xF0)
+    {
+      if ((*p == 0xE0 && p[1] < 0xA0) || (p[1] & 0xC0) != 0x80
+	  || (p[2] & 0xC0) != 0x80)
+	return *p;		/* not valid UTF-8 */
+      (*pp) += 2;
+      return ((*p & 0x0F) << 12) + ((p[1] & 0x3F) << 6) + (p[2] & 0x3F);
+    }
+  if (*p < 0xF8)
+    {
+      if ((*p == 0xF0 && p[1] < 0x90) || (p[1] & 0xC0) != 0x80
+	  || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80)
+	return *p;		/* not valid UTF-8 */
+      (*pp) += 3;
+      return ((*p & 0x07) << 18) + ((p[1] & 0x3F) << 12) +
+	((p[2] & 0x3F) << 6) + (p[3] & 0x3F);
+    }
+  if (*p < 0xFC)
+    {
+      if ((*p == 0xF8 && p[1] < 0x88) || (p[1] & 0xC0) != 0x80
+	  || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
+	  || (p[4] & 0xC0) != 0x80)
+	return *p;		/* not valid UTF-8 */
+      (*pp) += 4;
+      return ((*p & 0x03) << 24) + ((p[1] & 0x3F) << 18) +
+	((p[2] & 0x3F) << 12) + ((p[3] & 0x3F) << 6) + (p[4] & 0x3F);
+    }
+  if (*p < 0xFE)
+    {
+      if ((*p == 0xFC && p[1] < 0x84) || (p[1] & 0xC0) != 0x80
+	  || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
+	  || (p[4] & 0xC0) != 0x80 || (p[5] & 0xC0) != 0x80)
+	return *p;		/* not valid UTF-8 */
+      (*pp) += 5;
+      return ((*p & 0x01) << 30) + ((p[1] & 0x3F) << 24) +
+	((p[2] & 0x3F) << 18) + ((p[3] & 0x3F) << 12) + ((p[4] & 0x3F) << 6) +
+	(p[5] & 0x3F);
+    }
+  return *p;			/* not sensible */
+}
+
+/* This takes a binary header (udhl bytes at udh) and UCS-2 message (udl characters at ud) and packs in to o using SMS 7 bit character codes */
+/* The return value is the number of septets packed in to o, which is internally limited to SMSLEN */
+/* o can be null, in which case this is used to validate or count only */
+/* if the input contains invalid characters then the return value is -1 */
+static int
+packsms7 (unsigned char *o, int udhl, unsigned char *udh, int udl,
+	  unsigned short *ud)
 {
-  unsigned char p = 0, b = 0;
-  /* fixup - map character set perhaps... */
-  o[0] = 0;
-  while (n--)
+  unsigned char p = 0, b = 0, n = 0;
+  if (udhl)
+    {				/* header */
+      if (o)
+	o[p++] = udhl;
+      b = 1;
+      n = 1;
+      while (udhl--)
+	{
+	  if (o)
+	    o[p++] = *udh++;
+	  b += 8;
+	  while (b >= 7)
+	    {
+	      b -= 7;
+	      n++;
+	    }
+	  if (n >= SMSLEN)
+	    return n;
+	}
+      if (b)
+	{
+	  b = 7 - b;
+	  if (++n >= SMSLEN)
+	    return n;
+	};			/* filling to septet boundary */
+    }
+  if (o)
+    o[p] = 0;
+  /* message */
+  while (udl--)
     {
-      o[p] |= ((sms8to7[*i] & 0x7F) << b);
+      long u;
+      unsigned char v;
+      u = *ud++;
+      for (v = 0; v < 128 && defaultalphabet[v] != u; v++);
+      if (v == 128 && u && n + 1 < SMSLEN)
+	{
+	  for (v = 0; v < 128 && escapes[v] != u; v++);
+	  if (v < 128)
+	    {			/* escaped sequence */
+	      if (o)
+		o[p] |= (27 << b);
+	      b += 7;
+	      if (b >= 8)
+		{
+		  b -= 8;
+		  p++;
+		  if (o)
+		    o[p] = (27 >> (7 - b));
+		}
+	      n++;
+	    }
+	}
+      if (v == 128)
+	return -1;		/* invalid character */
+      if (o)
+	o[p] |= (v << b);
       b += 7;
       if (b >= 8)
 	{
 	  b -= 8;
 	  p++;
-	  o[p] = ((sms8to7[*i] & 0x7F) >> (7 - b));
+	  if (o)
+	    o[p] = (v >> (7 - b));
 	}
-      i++;
+      if (++n >= SMSLEN)
+	return n;
+    }
+  return n;
+}
+
+/* This takes a binary header (udhl bytes at udh) and UCS-2 message (udl characters at ud) and packs in to o using 8 bit character codes */
+/* The return value is the number of bytes packed in to o, which is internally limited to 140 */
+/* o can be null, in which case this is used to validate or count only */
+/* if the input contains invalid characters then the return value is -1 */
+static int
+packsms8 (unsigned char *o, int udhl, unsigned char *udh, int udl,
+	  unsigned short *ud)
+{
+  unsigned char p = 0;
+  /* header - no encoding */
+  if (udhl)
+    {
+      if (o)
+	o[p++] = udhl;
+      while (udhl--)
+	{
+	  if (o)
+	    o[p++] = *udh++;
+	  if (p >= 140)
+	    return p;
+	}
+    }
+  while (udl--)
+    {
+      long u;
+      u = *ud++;
+      if (u < 0 || u > 0xFF)
+	return -1;		/* not valid */
+      if (o)
+	o[p++] = u;
+      if (p >= 140)
+	return p;
     }
-  if (b)
-    p++;
   return p;
 }
 
-/* check if all characters are valid 7 bit coding characters */
-static unsigned char
-check7 (unsigned char l, unsigned char *p)
+/* This takes a binary header (udhl bytes at udh) and UCS-2 message (udl characters at ud) and packs in to o using 16 bit UCS-2 character codes */
+/* The return value is the number of bytes packed in to o, which is internally limited to 140 */
+/* o can be null, in which case this is used to validate or count only */
+/* if the input contains invalid characters then the return value is -1 */
+static int
+packsms16 (unsigned char *o, int udhl, unsigned char *udh, int udl,
+	   unsigned short *ud)
 {
-  while (l--)
-    if (sms8to7[*p++] & 0x80)
-      return 1;
-  return 0;
+  unsigned char p = 0;
+  /* header - no encoding */
+  if (udhl)
+    {
+      if (o)
+	o[p++] = udhl;
+      while (udhl--)
+	{
+	  if (o)
+	    o[p++] = *udh++;
+	  if (p >= 140)
+	    return p;
+	}
+    }
+  while (udl--)
+    {
+      long u;
+      u = *ud++;
+      if (o)
+	o[p++] = (u >> 8);
+      if (p >= 140)
+	return p - 1;		/* could not fit last character */
+      if (o)
+	o[p++] = u;
+      if (p >= 140)
+	return p;
+    }
+  return p;
 }
 
+/* general pack, with length and data, returns number of bytes of target used */
+static int
+packsms (unsigned char dcs, unsigned char *base, unsigned int udhl,
+	 unsigned char *udh, int udl, unsigned short *ud)
+{
+  unsigned char *p = base;
+  if (udl)
+    {
+      int l = 0;
+      if (is7bit (dcs))
+	{			/* 7 bit */
+	  l = packsms7 (p + 1, udhl, udh, udl, ud);
+	  if (l < 0)
+	    l = 0;
+	  *p++ = l;
+	  p += (l * 7 + 7) / 8;
+	}
+      else if (is8bit (dcs))
+	{			/* 8 bit */
+	  l = packsms8 (p + 1, udhl, udh, udl, ud);
+	  if (l < 0)
+	    l = 0;
+	  *p++ = l;
+	  p += l;
+	}
+      else
+	{			/* UCS-2 */
+	  l = packsms16 (p + 1, udhl, udh, udl, ud);
+	  if (l < 0)
+	    l = 0;
+	  *p++ = l;
+	  p += l;
+	}
+    }
+  else
+    *p++ = 0;			/* no user data */
+  return p - base;
+}
+
+
 /* pack a date and return */
 static void
 packdate (unsigned char *o, time_t w)
 {
   struct tm *t = localtime (&w);
 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__)
-  int z = -t->tm_gmtoff / 3600 / 15;
+  int z = -t->tm_gmtoff / 60 / 15;
 #else
-  int z = timezone / 3600 / 15;
+  int z = timezone / 60 / 15;
 #endif
   *o++ = ((t->tm_year % 10) << 4) + (t->tm_year % 100) / 10;
   *o++ = (((t->tm_mon + 1) % 10) << 4) + (t->tm_mon + 1) / 10;
@@ -256,27 +501,141 @@
   return mktime (&t);
 }
 
-/* unpack bytes from i to o and return number of source bytes. */
-static unsigned char
-unpack7 (unsigned char *o, unsigned char *i, unsigned char l)
+/* unpacks bytes (7 bit encoding) at i, len l septets, and places in udh and ud setting udhl and udl. udh not used if udhi not set */
+static void
+unpacksms7 (unsigned char *i, unsigned char l, unsigned char *udh, int *udhl,
+	    unsigned short *ud, int *udl, char udhi)
 {
   unsigned char b = 0, p = 0;
+  unsigned short *o = ud;
+  *udhl = 0;
+  if (udhi && l)
+    {				/* header */
+      int h = i[p];
+      *udhl = h;
+      if (h)
+	{
+	  b = 1;
+	  p++;
+	  l--;
+	  while (h-- && l)
+	    {
+	      *udh++ = i[p++];
+	      b += 8;
+	      while (b >= 7)
+		{
+		  b -= 7;
+		  l--;
+		  if (!l)
+		    break;
+		}
+	    }
+	  /* adjust for fill, septets */
+	  if (b)
+	    {
+	      b = 7 - b;
+	      l--;
+	    }
+	}
+    }
   while (l--)
     {
+      unsigned char v;
       if (b < 2)
-	*o++ = sms7to8[((i[p] >> b) & 0x7F)];
+	v = ((i[p] >> b) & 0x7F);
       else
-	*o++ = sms7to8[((((i[p] >> b) + (i[p + 1] << (8 - b)))) & 0x7F)];
+	v = ((((i[p] >> b) + (i[p + 1] << (8 - b)))) & 0x7F);
       b += 7;
       if (b >= 8)
 	{
 	  b -= 8;
 	  p++;
 	}
+      if (o > ud && o[-1] == 0x00A0 && escapes[v])
+	o[-1] = escapes[v];
+      else
+	*o++ = defaultalphabet[v];
     }
-  if (b)
-    p++;
-  return p;
+  *udl = (o - ud);
+}
+
+/* unpacks bytes (8 bit encoding) at i, len l septets, and places in udh and ud setting udhl and udl. udh not used if udhi not set */
+static void
+unpacksms8 (unsigned char *i, unsigned char l, unsigned char *udh, int *udhl,
+	    unsigned short *ud, int *udl, char udhi)
+{
+  unsigned short *o = ud;
+  *udhl = 0;
+  if (udhi)
+    {
+      int n = *i;
+      *udhl = n;
+      if (n)
+	{
+	  i++;
+	  l--;
+	  while (l && n)
+	    {
+	      l--;
+	      n--;
+	      *udh++ = *i++;
+	    }
+	}
+    }
+  while (l--)
+    *o++ = *i++;		/* not to UTF-8 as explicitely 8 bit coding in DCS */
+  *udl = (o - ud);
+}
+
+/* unpacks bytes (16 bit encoding) at i, len l septets, and places in udh and ud setting udhl and udl. udh not used if udhi not set */
+static void
+unpacksms16 (unsigned char *i, unsigned char l, unsigned char *udh, int *udhl,
+	     unsigned short *ud, int *udl, char udhi)
+{
+  unsigned short *o = ud;
+  *udhl = 0;
+  if (udhi)
+    {
+      int n = *i;
+      *udhl = n;
+      if (n)
+	{
+	  i++;
+	  l--;
+	  while (l && n)
+	    {
+	      l--;
+	      n--;
+	      *udh++ = *i++;
+	    }
+	}
+    }
+  while (l--)
+    {
+      int v = *i++;
+      if (l--)
+	v = (v << 8) + *i++;
+      *o++ = v;
+    }
+  *udl = (o - ud);
+}
+
+/* general unpack - starts with length byte (octet or septet) and returns number of bytes used, inc length */
+static int
+unpacksms (unsigned char dcs, unsigned char *i, unsigned char *udh, int *udhl,
+	   unsigned short *ud, int *udl, char udhi)
+{
+  int l = *i++;
+  if (is7bit (dcs))
+    {
+      unpacksms7 (i, l, udh, udhl, ud, udl, udhi);
+      l = (l * 7 + 7) / 8;	/* adjust length to return */
+    }
+  else if (is8bit (dcs))
+    unpacksms8 (i, l, udh, udhl, ud, udl, udhi);
+  else
+    unpacksms16 (i, l, udh, udhl, ud, udl, udhi);
+  return l + 1;
 }
 
 /* unpack an address from i, return byte length, unpack to o */
@@ -335,11 +694,14 @@
       int o = open (log_file, O_CREAT | O_APPEND | O_WRONLY, 0666);
       if (o >= 0)
 	{
-	  char line[1000], *p;
+	  char line[1000], mrs[3] = "", *p;
 	  unsigned char n;
-	  snprintf (line, sizeof (line), "%s %c %s %s %s ",
-		    isodate (time (0)), status, h->queue,
-		    *h->oa ? h->oa : "-", *h->da ? h->da : "-");
+	  if (h->mr >= 0)
+	    snprintf (mrs, sizeof (mrs), "%02X", h->mr);
+	  snprintf (line, sizeof (line), "%s %c%c%c%s %s %s %s ",
+		    isodate (time (0)), status, h->rx ? 'I' : 'O',
+		    h->smsc ? 'S' : 'M', mrs, h->queue, *h->oa ? h->oa : "-",
+		    *h->da ? h->da : "-");
 	  p = line + strlen (line);
 	  for (n = 0; n < h->udl; n++)
 	    if (h->ud[n] == '\\')
@@ -378,10 +740,11 @@
   FILE *s;
   char dcsset = 0;		/* if DSC set */
   ast_log (LOG_EVENT, "Sending %s\n", fn);
-  h->udl = *h->oa = *h->da = h->pid = h->srr = h->rp = h->vp = 0;
+  h->rx = h->udl = *h->oa = *h->da = h->pid = h->srr = h->udhi = h->rp =
+    h->vp = h->udhl = 0;
+  h->mr = -1;
   h->dcs = 0xF1;		/* normal messages class 1 */
   h->scts = time (0);
-  h->mr = message_ref++;
   s = fopen (fn, "r");
   if (s)
     {
@@ -392,10 +755,9 @@
 	}
       while (fgets (line, sizeof (line), s))
 	{			/* process line in file */
-	  char *p;
+	  unsigned char *p;
 	  for (p = line; *p && *p != '\n' && *p != '\r'; p++);
 	  *p = 0;		/* strip eoln */
-	  //ast_log (LOG_EVENT, "Line %s\n", line);
 	  p = line;
 	  if (!*p || *p == ';')
 	    continue;		/* blank line or comment, ignore */
@@ -410,29 +772,10 @@
 	    {
 	      *p++ = 0;
 	      if (!strcmp (line, "ud"))
-		{		/* parse message */
+		{		/* parse message (UTF-8) */
 		  unsigned char o = 0;
-		  while (*p && o < 160)
-		    {
-		      if (*p == '\\')
-			{
-			  p++;
-			  if (*p == '\\')
-			    h->ud[o++] = *p++;
-			  else if (*p == 'n')
-			    {
-			      h->ud[o++] = '\n';
-			      p++;
-			    }
-			  else if (*p == 'r')
-			    {
-			      h->ud[o++] = '\r';
-			      p++;
-			    }
-			}
-		      else
-			h->ud[o++] = *p++;
-		    }
+		  while (*p && o < SMSLEN)
+		    h->ud[o++] = utf8decode (&p);
 		  h->udl = o;
 		  if (*p)
 		    ast_log (LOG_WARNING, "UD too long in %s\n", fn);
@@ -465,7 +808,7 @@
 		    {		/* get date/time */
 		      int Y, m, d, H, M, S;
 		      if (sscanf
-			  (p, "%d-%d-%d %d:%d:%d", &Y, &m, &d, &H, &M,
+			  (p, "%d-%d-%dT%d:%d:%d", &Y, &m, &d, &H, &M,
 			   &S) == 6)
 			{
 			  struct tm t;
@@ -490,14 +833,69 @@
 	  else if (*p == '#')
 	    {			/* raw hex format */
 	      *p++ = 0;
-	      if (!strcmp (line, "ud"))
+	      if (*p == '#')
 		{
+		  p++;
+		  if (!strcmp (line, "ud"))
+		    {		/* user data */
+		      int o = 0;
+		      while (*p && o < SMSLEN)
+			{
+			  if (isxdigit (*p) && isxdigit (p[1])
+			      && isxdigit (p[2]) && isxdigit (p[3]))
+			    {
+			      h->ud[o++] =
+				(((isalpha (*p) ? 9 : 0) +
+				  (*p & 0xF)) << 12) +
+				(((isalpha (p[1]) ? 9 : 0) +
+				  (p[1] & 0xF)) << 8) +
+				(((isalpha (p[2]) ? 9 : 0) +
+				  (p[2] & 0xF)) << 4) +
+				((isalpha (p[3]) ? 9 : 0) + (p[3] & 0xF));
+			      p += 4;
+			    }
+			  else
+			    break;
+			}
+		      h->udl = o;
+		      if (*p)
+			ast_log (LOG_WARNING,
+				 "UD too long / invalid UCS-2 hex in %s\n",
+				 fn);
+		    }
+		  else
+		    ast_log (LOG_WARNING, "Only ud can use ## format, %s\n",
+			     fn);
+		}
+	      else if (!strcmp (line, "ud"))
+		{		/* user data */
+		  int o = 0;
+		  while (*p && o < SMSLEN)
+		    {
+		      if (isxdigit (*p) && isxdigit (p[1]))
+			{
+			  h->ud[o++] =
+			    (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 4) +
+			    ((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF));
+			  p += 2;
+			}
+		      else
+			break;
+		    }
+		  h->udl = o;
+		  if (*p)
+		    ast_log (LOG_WARNING,
+			     "UD too long / invalid UCS-1 hex in %s\n", fn);
+		}
+	      else if (!strcmp (line, "udh"))
+		{		/* user data header */
 		  unsigned char o = 0;
-		  while (*p && o < 160)
+		  h->udhi = 1;
+		  while (*p && o < SMSLEN)
 		    {
 		      if (isxdigit (*p) && isxdigit (p[1]))
 			{
-			  h->ud[o] =
+			  h->udh[o] =
 			    (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 4) +
 			    ((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF));
 			  o++;
@@ -506,106 +904,141 @@
 		      else
 			break;
 		    }
-		  h->udl = o;
+		  h->udhl = o;
 		  if (*p)
-		    ast_log (LOG_WARNING, "UD too long / invalid hex in %s\n",
-			     fn);
+		    ast_log (LOG_WARNING,
+			     "UDH too long / invalid hex in %s\n", fn);
 		}
 	      else
 		ast_log (LOG_WARNING,
-			 "Only ud can use 8 bit key format with # instead of =\n");
+			 "Only ud and udh can use # format, %s\n", fn);
 	    }
 	  else
 	    ast_log (LOG_WARNING, "Cannot parse in %s: %s\n", fn, line);
 	}
       fclose (s);
-      if (!dcsset && h->udl <= 140 && check7 (h->udl, h->ud))
-	{
-	  h->dcs = 0xF5;	// default to 8 bit
-	  ast_log (LOG_WARNING,
-		   "Sending in 8 bit format because of illegal characters %s\n",
-		   fn);
-	}
-      if ((h->dcs & 4) && h->udl > 140)
+      if (!dcsset && packsms7 (0, h->udhl, h->udh, h->udl, h->ud) < 0)
 	{
-	  ast_log (LOG_WARNING, "8 bit data too long, truncated %s\n", fn);
-	  h->udl = 140;
+	  if (packsms8 (0, h->udhl, h->udh, h->udl, h->ud) < 0)
+	    {
+	      if (packsms16 (0, h->udhl, h->udh, h->udl, h->ud) < 0)
+		ast_log (LOG_WARNING,
+			 "Invalid UTF-8 message even for UCS-2 (%s)\n", fn);
+	      else
+		{
+		  h->dcs = 0x08;	/* default to 16 bit */
+		  ast_log (LOG_WARNING, "Sending in 16 bit format (%s)\n",
+			   fn);
+		}
+	    }
+	  else
+	    {
+	      h->dcs = 0xF5;	/* default to 8 bit */
+	      ast_log (LOG_WARNING, "Sending in 8 bit format (%s)\n", fn);
+	    }
 	}
-      else if (!(h->dcs & 4) && check7 (h->udl, h->ud))
+      if (is7bit (h->dcs) && packsms7 (0, h->udhl, h->udh, h->udl, h->ud) < 0)
 	ast_log (LOG_WARNING, "Invalid 7 bit GSM data %s\n", fn);
+      if (is8bit (h->dcs) && packsms8 (0, h->udhl, h->udh, h->udl, h->ud) < 0)
+	ast_log (LOG_WARNING, "Invalid 8 bit data %s\n", fn);
+      if (is16bit (h->dcs)
+	  && packsms16 (0, h->udhl, h->udh, h->udl, h->ud) < 0)
+	ast_log (LOG_WARNING, "Invalid 16 bit data %s\n", fn);
     }
-  //ast_log (LOG_EVENT, "Loaded %s\n", fn);
 }
 
 /* white a received text message to a file */
 static void
 sms_writefile (sms_t * h)
 {
-  char fn[200] = "";
-  char fn2[200] = "";
+  char fn[200] = "", fn2[200] = "";
   FILE *o;
-
   strncpy (fn, spool_dir, sizeof (fn) - 1);
   mkdir (fn, 0777);		/* ensure it exists */
-  snprintf (fn + strlen (fn), sizeof (fn) - strlen (fn), "/%s.%s",
-	    h->smsc ? "me-sc" : "sc-me", h->queue);
+  snprintf (fn + strlen (fn), sizeof (fn) - strlen (fn), "/%s",
+	    h->smsc ? h->rx ? "morx" : "mttx" : h->rx ? "mtrx" : "motx");
   mkdir (fn, 0777);		/* ensure it exists */
   strncpy (fn2, fn, sizeof (fn2) - 1);
-  strftime (fn2 + strlen (fn2), sizeof (fn2) - strlen (fn2),
-	    "/%Y-%m-%d_%H:%M:%S", localtime (&h->scts));
-  snprintf (fn2 + strlen (fn2), sizeof (fn2) - strlen (fn2), "-%02X", h->mr);
+  snprintf (fn2 + strlen (fn2), sizeof (fn2) - strlen (fn2), "/%s.%s-%02X",
+	    h->queue, isodate (h->scts), h->mr);
   snprintf (fn + strlen (fn), sizeof (fn) - strlen (fn), "/.%s",
 	    fn2 + strlen (fn) + 1);
   o = fopen (fn, "w");
   if (o)
     {
-      fprintf (o, "mr=%d\n", h->mr);
       if (*h->oa)
 	fprintf (o, "oa=%s\n", h->oa);
       if (*h->da)
 	fprintf (o, "da=%s\n", h->da);
-      if (h->pid)
-	fprintf (o, "pid=%d\n", h->pid);
-      if (h->dcs != 0xF1)
-	fprintf (o, "dcs=%d\n", h->dcs);
-      if (h->vp)
-	fprintf (o, "srr=%d\n", h->vp);
-      if (h->srr)
-	fprintf (o, "srr=1\n");
-      if (h->rp)
-	fprintf (o, "rp=1\n");
-      if (h->scts)
-	fprintf (o, "scts=%s\n", isodate (h->scts));
+      if (h->udhi)
+	{
+	  unsigned int p;
+	  fprintf (o, "udh#");
+	  for (p = 0; p < h->udhl; p++)
+	    fprintf (o, "%02X", h->udh[p]);
+	  fprintf (o, "\n");
+	}
       if (h->udl)
 	{
 	  unsigned int p;
-	  for (p = 0;
-	       p < h->udl && ((h->ud[p] >= 32 && h->ud[p] != 127)
-			      || h->ud[p] == '\n' || h->ud[p] == '\r'); p++);
+	  for (p = 0; p < h->udl && h->ud[p] >= ' '; p++);
 	  if (p < h->udl)
-	    {			// use a hex format as unprintable characters
-	      fprintf (o, "ud#");
-	      for (p = 0; p < h->udl; p++)
-		fprintf (o, "%02X", h->ud[p]);
-	      fprintf (o, "\n;");
-	      /* followed by commented line using printable characters */
-	    }
+	    fputc (';', o);	/* cannot use ud=, but include as a comment for human readable */
 	  fprintf (o, "ud=");
 	  for (p = 0; p < h->udl; p++)
 	    {
-	      if (h->ud[p] == '\\')
-		fprintf (o, "\\\\");
-	      else if (h->ud[p] == '\r')
-		fprintf (o, "\\r");
-	      else if (h->ud[p] == '\n')
-		fprintf (o, "\\n");
-	      else if (h->ud[p] < 32 || h->ud[p] == 127)
+	      unsigned short v = h->ud[p];
+	      if (v < 32)
 		fputc (191, o);
+	      else if (v < 0x80)
+		fputc (v, o);
+	      else if (v < 0x800)
+		{
+		  fputc (0xC0 + (v >> 6), o);
+		  fputc (0x80 + (v & 0x3F), o);
+		}
 	      else
-		fputc (h->ud[p], o);
+		{
+		  fputc (0xE0 + (v >> 12), o);
+		  fputc (0x80 + ((v >> 6) & 0x3F), o);
+		  fputc (0x80 + (v & 0x3F), o);
+		}
 	    }
 	  fprintf (o, "\n");
+	  for (p = 0; p < h->udl && h->ud[p] >= ' '; p++);
+	  if (p < h->udl)
+	    {
+	      for (p = 0; p < h->udl && h->ud[p] < 0x100; p++);
+	      if (p == h->udl)
+		{		/* can write in ucs-1 hex */
+		  fprintf (o, "ud#");
+		  for (p = 0; p < h->udl; p++)
+		    fprintf (o, "%02X", h->ud[p]);
+		  fprintf (o, "\n");
+		}
+	      else
+		{		/* write in UCS-2 */
+		  fprintf (o, "ud##");
+		  for (p = 0; p < h->udl; p++)
+		    fprintf (o, "%04X", h->ud[p]);
+		  fprintf (o, "\n");
+		}
+	    }
 	}
+      if (h->scts)
+	fprintf (o, "scts=%s\n", isodate (h->scts));
+      if (h->pid)
+	fprintf (o, "pid=%d\n", h->pid);
+      if (h->dcs != 0xF1)
+	fprintf (o, "dcs=%d\n", h->dcs);
+      if (h->vp)
+	fprintf (o, "vp=%d\n", h->vp);
+      if (h->srr)
+	fprintf (o, "srr=1\n");
+      if (h->mr >= 0)
+	fprintf (o, "mr=%d\n", h->mr);
+      if (h->rp)
+	fprintf (o, "rp=1\n");
       fclose (o);
       if (rename (fn, fn2))
 	unlink (fn);
@@ -616,14 +1049,16 @@
 
 /* read dir skipping dot files... */
 static struct dirent *
-readdirdot (DIR * d)
+readdirqueue (DIR * d, char *queue)
 {
   struct dirent *f;
   do
     {
       f = readdir (d);
     }
-  while (f && *f->d_name == '.');
+  while (f
+	 && (*f->d_name == '.' || strncmp (f->d_name, queue, strlen (queue))
+	     || f->d_name[strlen (queue)] != '.'));
   return f;
 }
 
@@ -636,8 +1071,10 @@
     {				/* SMSC */
       if ((h->imsg[2] & 3) == 1)
 	{			/* SMS-SUBMIT */
+	  h->udhl = h->udl = 0;
 	  h->vp = 0;
 	  h->srr = ((h->imsg[2] & 0x20) ? 1 : 0);
+	  h->udhi = ((h->imsg[2] & 0x40) ? 1 : 0);
 	  h->rp = ((h->imsg[2] & 0x80) ? 1 : 0);
 	  strncpy (h->oa, h->cli, sizeof (h->oa) - 1);
 	  h->scts = time (0);
@@ -659,20 +1096,17 @@
 	    }
 	  else if (h->imsg[2] & 0x18)
 	    p += 7;		/* ignore enhanced / absolute VP */
-	  h->udl = h->imsg[p++];
-	  if (h->udl)
-	    {
-	      if (h->dcs & 4)
-		{
-		  memcpy (h->ud, h->imsg + p, h->udl);
-		  p += h->udl;
-		}
-	      else
-		p += unpack7 (h->ud, h->imsg + p, h->udl);
-	    }
+	  p +=
+	    unpacksms (h->dcs, h->imsg + p, h->udh, &h->udhl, h->ud, &h->udl,
+		       h->udhi);
+	  h->rx = 1;		/* received message */
 	  sms_writefile (h);	/* write the file */
 	  if (p != h->imsg[1] + 2)
-	    return 0xFF;	/* duh! */
+	    {
+	      ast_log (LOG_WARNING, "Mismatch receive unpacking %d/%d\n", p,
+		       h->imsg[1] + 2);
+	      return 0xFF;	/* duh! */
+	    }
 	}
       else
 	{
@@ -684,27 +1118,27 @@
     {				/* client */
       if (!(h->imsg[2] & 3))
 	{			/* SMS-DELIVER */
-	  *h->da = h->srr = h->rp = h->vp = 0;
-	  h->mr = message_ref++;
+	  *h->da = h->srr = h->rp = h->vp = h->udhi = h->udhl = h->udl = 0;
+	  h->srr = ((h->imsg[2] & 0x20) ? 1 : 0);
+	  h->udhi = ((h->imsg[2] & 0x40) ? 1 : 0);
+	  h->rp = ((h->imsg[2] & 0x80) ? 1 : 0);
+	  h->mr = -1;
 	  p += unpackaddress (h->oa, h->imsg + p);
 	  h->pid = h->imsg[p++];
 	  h->dcs = h->imsg[p++];
 	  h->scts = unpackdate (h->imsg + p);
 	  p += 7;
-	  h->udl = h->imsg[p++];
-	  if (h->udl)
-	    {
-	      if (h->dcs & 4)
-		{
-		  memcpy (h->ud, h->imsg + p, h->udl);
-		  p += h->udl;
-		}
-	      else
-		p += unpack7 (h->ud, h->imsg + p, h->udl);
-	    }
+	  p +=
+	    unpacksms (h->dcs, h->imsg + p, h->udh, &h->udhl, h->ud, &h->udl,
+		       h->udhi);
+	  h->rx = 1;		/* received message */
 	  sms_writefile (h);	/* write the file */
 	  if (p != h->imsg[1] + 2)
-	    return 0xFF;	/* duh! */
+	    {
+	      ast_log (LOG_WARNING, "Mismatch receive unpacking %d/%d\n", p,
+		       h->imsg[1] + 2);
+	      return 0xFF;	/* duh! */
+	    }
 	}
       else
 	{
@@ -721,22 +1155,22 @@
   char fn[100 + NAME_MAX] = "";
   DIR *d;
   char more = 0;
-
   strncpy (fn, spool_dir, sizeof (fn) - 1);
   mkdir (fn, 0777);		/* ensure it exists */
-  snprintf (fn + strlen (fn), sizeof (fn) - strlen (fn), "/%s.%s",
-	    h->smsc ? "sc-me" : "me-sc", h->queue);
+  h->rx = 0;			/* outgoing message */
+  snprintf (fn + strlen (fn), sizeof (fn) - strlen (fn), "/%s",
+	    h->smsc ? "mttx" : "motx");
   mkdir (fn, 0777);		/* ensure it exists */
   d = opendir (fn);
   if (d)
     {
-      struct dirent *f = readdirdot (d);
+      struct dirent *f = readdirqueue (d, h->queue);
       if (f)
 	{
 	  snprintf (fn + strlen (fn), sizeof (fn) - strlen (fn), "/%s",
 		    f->d_name);
 	  sms_readfile (h, fn);
-	  if (readdirdot (d))
+	  if (readdirqueue (d, h->queue))
 	    more = 1;		/* more to send */
 	}
       closedir (d);
@@ -753,23 +1187,15 @@
 	  h->omsg[p++] = h->dcs;
 	  packdate (h->omsg + p, h->scts);
 	  p += 7;
-	  h->omsg[p++] = h->udl;
-	  if (h->udl)
-	    {
-	      if (h->dcs & 4)
-		{
-		  memcpy (h->omsg + p, h->ud, h->udl);
-		  p += h->udl;
-		}
-	      else
-		p += pack7 (h->omsg + p, h->ud, h->udl);
-	    }
+	  p += packsms (h->dcs, h->omsg + p, h->udhl, h->udh, h->udl, h->ud);
 	}
       else
 	{			/* submit */
 	  h->omsg[p++] =
 	    0x01 + (more ? 4 : 0) + (h->srr ? 0x20 : 0) + (h->rp ? 0x80 : 0) +
-	    (h->vp ? 0x10 : 0);
+	    (h->vp ? 0x10 : 0) + (h->udhi ? 0x40 : 0);
+	  if (h->mr < 0)
+	    h->mr = message_ref++;
 	  h->omsg[p++] = h->mr;
 	  p += packaddress (h->omsg + p, h->da);
 	  h->omsg[p++] = h->pid;
@@ -787,17 +1213,7 @@
 	      else
 		h->omsg[p++] = 255;	/* max */
 	    }
-	  h->omsg[p++] = h->udl;
-	  if (h->udl)
-	    {
-	      if (h->dcs & 4)
-		{
-		  memcpy (h->omsg + p, h->ud, h->udl);
-		  p += h->udl;
-		}
-	      else
-		p += pack7 (h->omsg + p, h->ud, h->udl);
-	    }
+	  p += packsms (h->dcs, h->omsg + p, h->udhl, h->udh, h->udl, h->ud);
 	}
       h->omsg[1] = p - 2;
       sms_messagetx (h);
@@ -806,17 +1222,29 @@
     {				/* no message */
       h->omsg[0] = 0x94;	/* SMS_REL */
       h->omsg[1] = 0;
-      h->hangup = 1;
       sms_messagetx (h);
     }
 }
 
 static void
+sms_debug (char *dir, unsigned char *msg)
+{
+  char txt[259 * 3 + 1], *p = txt;	/* always long enough */
+  int n = msg[1] + 3, q = 0;
+  while (q < n && q < 30)
+    {
+      sprintf (p, " %02X", msg[q++]);
+      p += 3;
+    }
+  if (q < n)
+    sprintf (p, "...");
+  ast_verbose (VERBOSE_PREFIX_3 "SMS %s%s\n", dir, txt);
+}
+
+static void
 sms_messagerx (sms_t * h)
 {
-  ast_verbose (VERBOSE_PREFIX_3 "SMS RX %02X %02X %02X %02X %02X %02X...\n",
-	       h->imsg[0], h->imsg[1], h->imsg[2], h->imsg[3], h->imsg[4],
-	       h->imsg[5]);
+  sms_debug ("RX", h->imsg);
   /* testing */
   switch (h->imsg[0])
     {
@@ -876,9 +1304,7 @@
   for (p = 0; p < h->omsg[1] + 2; p++)
     c += h->omsg[p];
   h->omsg[h->omsg[1] + 2] = 0 - c;
-  ast_verbose (VERBOSE_PREFIX_3 "SMS TX %02X %02X %02X %02X %02X %02X...\n",
-	       h->omsg[0], h->omsg[1], h->omsg[2], h->omsg[3], h->omsg[4],
-	       h->omsg[5]);
+  sms_debug ("TX", h->omsg);
   h->obyte = 1;
   h->opause = 200;
   if (h->omsg[0] == 0x93)
@@ -889,41 +1315,61 @@
   h->obyten = h->omsg[1] + 3;
 }
 
-
 static int
 sms_generate (struct ast_channel *chan, void *data, int len, int samples)
 {
-  struct ast_frame f;
+  struct ast_frame f = { 0 };
   unsigned char waste[AST_FRIENDLY_OFFSET];
+#ifdef OUTALAW
+  unsigned char buf[800];
+#else
   signed short buf[800];
+#endif
   sms_t *h = data;
   int i;
 
   if (len > sizeof (buf))
     {
       ast_log (LOG_WARNING, "Only doing %d bytes (%d bytes requested)\n",
-	       (int) (sizeof (buf) / sizeof (signed short)), len);
+	       sizeof (buf) / sizeof (signed short), len);
       len = sizeof (buf);
+#ifdef OUTALAW
+      samples = len;
+#else
       samples = len / 2;
+#endif
     }
   waste[0] = 0;			/* make compiler happy */
   f.frametype = AST_FRAME_VOICE;
+#ifdef OUTALAW
+  f.subclass = AST_FORMAT_ALAW;
+  f.datalen = samples;
+#else
   f.subclass = AST_FORMAT_SLINEAR;
+  f.datalen = samples * 2;
+#endif
   f.offset = AST_FRIENDLY_OFFSET;
   f.mallocd = 0;
   f.data = buf;
-  f.datalen = samples * 2;
   f.samples = samples;
   f.src = "app_sms";
   /* create a buffer containing the digital sms pattern */
   for (i = 0; i < samples; i++)
     {
-      buf[i] = 0;
+#ifdef OUTALAW
+      buf[i] = wavea[0];
+#else
+      buf[i] = wave[0];
+#endif
       if (h->opause)
 	h->opause--;
       else if (h->obyten || h->osync)
 	{			/* sending data */
+#ifdef OUTALAW
+	  buf[i] = wavea[h->ophase];
+#else
 	  buf[i] = wave[h->ophase];
+#endif
 	  if ((h->ophase += ((h->obyte & 1) ? 13 : 21)) >= 80)
 	    h->ophase -= 80;
 	  if ((h->ophasep += 12) >= 80)
@@ -977,8 +1423,8 @@
 	h->imag = h->imag * 7 / 8;
       if (h->imag > 500)
 	{
-	  h->ims0 = (h->ims0 * 6 + *data * wave[h->ips0]) / 7;
 	  h->idle = 0;
+	  h->ims0 = (h->ims0 * 6 + *data * wave[h->ips0]) / 7;
 	  h->imc0 = (h->imc0 * 6 + *data * wave[h->ipc0]) / 7;
 	  h->ims1 = (h->ims1 * 6 + *data * wave[h->ips1]) / 7;
 	  h->imc1 = (h->imc1 * 6 + *data * wave[h->ipc1]) / 7;
@@ -1085,7 +1531,6 @@
   struct localuser *u;
   struct ast_frame *f;
   sms_t h = { 0 };
-
   h.ipc0 = h.ipc1 = 20;		/* phase for cosine */
   h.dcs = 0xF1;			/* default */
   if (!data)
@@ -1098,14 +1543,14 @@
     strncpy (h.cli, chan->cid.cid_num, sizeof (h.cli) - 1);
 
   {
-    char *d = data, *p, answer = 0;
+    unsigned char *d = data, *p, answer = 0;
     if (!*d || *d == '|')
       {
 	ast_log (LOG_ERROR, "Requires queue name\n");
 	return -1;
       }
     for (p = d; *p && *p != '|'; p++);
-    if (p - d + 1 >= sizeof (h.queue))
+    if (p - d >= sizeof (h.queue))
       {
 	ast_log (LOG_ERROR, "Queue name too long\n");
 	return -1;
@@ -1149,6 +1594,7 @@
     if (*d == '|')
       {
 	/* submitting a message, not taking call. */
+	/* depricated, use smsq instead */
 	d++;
 	h.scts = time (0);
 	for (p = d; *p && *p != '|'; p++);
@@ -1170,19 +1616,17 @@
 	if (!h.smsc)
 	  strncpy (h.oa, h.cli, sizeof (h.oa) - 1);
 	d = p;
-	if (!(h.dcs & 4) && check7 (h.udl, h.ud))
-	  ast_log (LOG_WARNING, "Invalid GSM characters in %.*s\n", h.udl,
-		   h.ud);
-	if (strlen (d) > ((h.dcs & 4) ? 140 : 160))
-	  {
-	    ast_log (LOG_ERROR, "Message too long %s\n", d);
-	    h.udl = ((h.dcs & 4) ? 140 : 160);
-	  }
-	else
-	  h.udl = strlen (d);
-	if (h.udl)
-	  memcpy (h.ud, d, h.udl);
-	h.smsc = !h.smsc;	/* file woul go in wrong directory otherwise... */
+	h.udl = 0;
+	while (*p && h.udl < SMSLEN)
+	  h.ud[h.udl++] = utf8decode (&p);
+	if (is7bit (h.dcs) && packsms7 (0, h.udhl, h.udh, h.udl, h.ud) < 0)
+	  ast_log (LOG_WARNING, "Invalid 7 bit GSM data\n");
+	if (is8bit (h.dcs) && packsms8 (0, h.udhl, h.udh, h.udl, h.ud) < 0)
+	  ast_log (LOG_WARNING, "Invalid 8 bit data\n");
+	if (is16bit (h.dcs) && packsms16 (0, h.udhl, h.udh, h.udl, h.ud) < 0)
+	  ast_log (LOG_WARNING, "Invalid 16 bit data\n");
+	h.rx = 0;		/* sent message */
+	h.mr = -1;
 	sms_writefile (&h);
 	return 0;
       }
@@ -1200,7 +1644,11 @@
   if (chan->_state != AST_STATE_UP)
     ast_answer (chan);
 
+#ifdef OUTALAW
+  res = ast_set_write_format (chan, AST_FORMAT_ALAW);
+#else
   res = ast_set_write_format (chan, AST_FORMAT_SLINEAR);
+#endif
   if (res >= 0)
     res = ast_set_read_format (chan, AST_FORMAT_SLINEAR);
   if (res < 0)
@@ -1248,13 +1696,13 @@
 int
 load_module (void)
 {
-  {				/* fill in sms8to7 from sms7to8 */
+#ifdef OUTALAW
+  {
     int p;
-    for (p = 0; p < 256; p++)
-      sms8to7[p] = 0xE0;	/* inverted question mark and invalid */
-    for (p = 0; p < 128; p++)
-      sms8to7[sms7to8[p]] = p;
+    for (p = 0; p < 80; p++)
+      wavea[p] = AST_LIN2A (wave[p]);
   }
+#endif
   snprintf (log_file, sizeof (log_file), "%s/sms", ast_config_AST_LOG_DIR);
   snprintf (spool_dir, sizeof (spool_dir), "%s/sms",
 	    ast_config_AST_SPOOL_DIR);
