#include <stdio.h>
#include <stdlib.h>
#define GN_CHAR_UNI_ESCAPE 0x001b
#define bool signed char
# define false 0
# define true 1
int char_def_alphabet_ext_count(unsigned char *input, int length);
bool char_def_alphabet_ext(unsigned int value);
static unsigned char char_def_alphabet_ext_encode(unsigned int value);
unsigned char char_def_alphabet_encode(unsigned int value);
unsigned char SMS_Text2PDU(unsigned char* src,unsigned char* dest,unsigned char size);
unsigned char SMS_PDU2Text(unsigned char* src,unsigned char* dest,unsigned char size);
void encode (unsigned char *msg,unsigned char length);
int /* Returns -1 for success, 0 for failure */
SMS_GSMEncode(
int inSize, /* Number of GSM-7 characters */
char* inText, /* Pointer to the GSM-7 characters. Note: I could
not have used a 0-terminated string, since 0
represents '@' in the GSM-7 character set */
int paddingBits, /* If you use a UDH, you may have to add padding
bits to properly align the GSM-7 septets */
int outSize, /* The number of octets we have available to write */
unsigned char* outBuff, /* A pointer to the available octets */
int *outUsed /* Keeps track of howmany octets actually were used */
);
int /* Returns -1 for success, 0 for failure */
SMS_GSMEncode(
int inSize, /* Number of GSM-7 characters */
char* inText, /* Pointer to the GSM-7 characters. Note: I could
not have used a 0-terminated string, since 0
represents '@' in the GSM-7 character set */
int paddingBits, /* If you use a UDH, you may have to add padding
bits to properly align the GSM-7 septets */
int outSize, /* The number of octets we have available to write */
unsigned char* outBuff, /* A pointer to the available octets */
int *outUsed /* Keeps track of howmany octets actually were used */
)
{
int bits = 0;
int i;
unsigned char octet;
*outUsed = 0;
if( paddingBits )
{
bits = 7 - paddingBits;
*outBuff++ = inText[0] << (7 - bits);
(*outUsed) ++;
bits++;
}
for( i = 0; i < inSize; i++ )
{
if( bits == 7 )
{
bits = 0;
continue;
}
if( *outUsed == outSize )
return 0; /* buffer overflow */
octet = (inText[i] & 0x7f) >> bits;
if( i < inSize - 1 )
octet |= inText[i + 1] << (7 - bits);
*outBuff++ = octet;
(*outUsed)++;
bits++;
}
return -1; /* ok */
}
int main()
{
unsigned char *a = (unsigned char *)malloc(255);
int outlen = 0;
unsigned char aa[200];
unsigned char aaa[] = {0xea,0x37,0x1d,0x9d,0x5e,0xd7,0xdb,0x61,0x39,0x30,0x26,0x9b,0xd1,0x00};
memset(aa,'\0',sizeof(aa));
memset(a,'\0',sizeof(255));
memcpy(a,"ea371d9d5ed7db613930269bd100",14);
//SMS_GSMEncode(strlen("jothikumar@1234"),"jothikumar@1234",0,strlen("jothikumar@1234"),a,&outlen);
//gsm_sms_char_7bit_unpack(0, 14, 15,aaa, aa);
SMS_PDU2Text("E8329BFD06A5E97310BD3CA783A6D35924169101",aa,22);
//char_7bit_pack(0,"hello its test SS3_1_2",a,&outlen);
printf("%s",aa);
getch();
return 0;
}
/*
* FROM GNOKII
* gsm-encoding.c
* gsm-sms.c
*/
#define GN_BYTE_MASK ((1 << bits) - 1)
int gsm_sms_char_7bit_unpack(unsigned int offset, unsigned int in_length, unsigned int out_length,const unsigned char *input, unsigned char *output)
{
unsigned char *out_num = output; /* Current pointer to the output buffer */
const unsigned char *in_num = input; /* Current pointer to the input buffer */
unsigned char rest = 0x00;
int bits;
bits = offset ? offset : 7;
while ((unsigned int)(in_num - input) < in_length)
{
*out_num = ((*in_num & GN_BYTE_MASK) << (7 - bits)) | rest;
rest = *in_num >> bits;
if(*out_num == 0x11)
{
*out_num = '_';
}
if(*out_num == 0x00)
{
*out_num = '@';
}
/* If we don't start from 0th bit, we shouldn't go to the
next char. Under *out_num we have now 0 and under Rest -
_first_ part of the char. */
if ((in_num != input) || (bits == 7)) out_num++;
in_num++;
if ((unsigned int)(out_num - output) >= out_length) break;
/* After reading 7 octets we have read 7 full characters but
we have 7 bits as well. This is the next character */
if (bits == 1)
{
*out_num = rest;
out_num++;
bits = 7;
rest = 0x00;
}
else
{
bits--;
}
}
return (int)(out_num - output);
}
/**
* char_7bit_pack:
* @offset: the bit offset inside the first byte of @output from which to start writing data
* @input: buffer with the string to be converted
* @output: buffer for the converted string, not NUL terminated
* @in_len: length of @input to be set; includes extended alphabet escape char
*
* Returns: the number of bytes used in @output
*
* Converts an array of 8-bit characters from @input into a packed sequence
* of 7-bit characters in @output.
*/
int char_7bit_pack(unsigned int offset, unsigned char *input,
unsigned char *output, unsigned int *in_len)
{
unsigned char *out_num = output; /* Current pointer to the output buffer */
unsigned int in_num;
int bits; /* Number of bits directly copied to output buffer */
unsigned int ucs2len, i = 0, len = strlen(input);
char *ucs2str;
/* FIXME: do it outside this function */
/* FIXME: scheduled for 0.6.31 */
/* First, let's know the encoding. We convert it from something to UCS-2 */
ucs2str = calloc(2 * len, sizeof(unsigned char));
if (!ucs2str)
return 0;
//ucs2len = ucs2_encode(ucs2str, 2 * len, input, len);
bits = (7 + offset) % 8;
/* If we don't begin with 0th bit, we will write only a part of the
first octet */
if (offset) {
*out_num = 0x00;
out_num++;
}
*in_len = 0;
while (i < 22 / 2) {
unsigned char byte;
bool double_char = false;
unsigned int a = 0xff & ucs2str[2 * i], b = 0xff & ucs2str[2 * i + 1];
in_num = 256 * a + b;
if (char_def_alphabet_ext(in_num)) {
byte = GN_CHAR_UNI_ESCAPE;
double_char = true;
goto skip;
next_char:
byte = char_def_alphabet_ext_encode(in_num);
double_char = false;
(*in_len) += 2;
} else {
byte = char_def_alphabet_encode(in_num);
(*in_len)++;
}
skip:
*out_num = byte >> (7 - bits);
/* If we don't write at 0th bit of the octet, we should write
a second part of the previous octet */
if (bits != 7)
*(out_num-1) |= (byte & ((1 << (7-bits)) - 1)) << (bits+1);
bits--;
if (bits == -1)
bits = 7;
else
out_num++;
if (double_char)
goto next_char;
i++;
}
free(ucs2str);
return (out_num - output);
}
/**
* char_def_alphabet_ext:
* @value: the character to test UCS-2 encoded
*
* Returns: non zero if the character can be represented with the Extended GSM Alphabet,
* zero otherwise
*
* Checks if @value is a character defined by the Extended GSM Alphabet.
*
* In GSM specification there are 10 characters in the extension
* of the default alphabet. Their values look a bit random, they are
* only 10, and probably they will never change, so hardcoding them
* here is rather safe.
*/
bool char_def_alphabet_ext(unsigned int value)
{
return (value == 0x0c ||
value == '^' ||
value == '{' ||
value == '}' ||
value == '\\' ||
value =