The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1 | /* Copyright (C) 2007-2008 The Android Open Source Project |
| 2 | ** |
| 3 | ** This software is licensed under the terms of the GNU General Public |
| 4 | ** License version 2, as published by the Free Software Foundation, and |
| 5 | ** may be copied, distributed, and modified under those terms. |
| 6 | ** |
| 7 | ** This program is distributed in the hope that it will be useful, |
| 8 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | ** GNU General Public License for more details. |
| 11 | */ |
| 12 | #ifndef _android_gsm_h |
| 13 | #define _android_gsm_h |
| 14 | |
| 15 | /** USEFUL TYPES |
| 16 | **/ |
| 17 | |
| 18 | typedef unsigned char byte_t; |
| 19 | typedef byte_t* bytes_t; |
| 20 | typedef const byte_t* cbytes_t; |
| 21 | |
| 22 | /** BCD |
| 23 | **/ |
| 24 | |
| 25 | /* convert a 8-bit value into the corresponding nibble-bcd byte */ |
| 26 | extern byte_t gsm_int_to_bcdi( int value ); |
| 27 | |
| 28 | /* convert a nibble-bcd byte into an int, invalid nibbles are silently converted to 0 */ |
| 29 | extern int gsm_int_from_bcdi( byte_t value ); |
| 30 | |
| 31 | /** HEX |
| 32 | **/ |
| 33 | |
| 34 | /* try to convert a hex string into a byte string, assumes 'dst' is properly sized, and hexlen is even. |
| 35 | * returns the number of bytes on exit, or -1 in case of badly formatted data */ |
| 36 | extern int gsm_hex_to_bytes ( cbytes_t hex, int hexlen, bytes_t dst ); |
| 37 | |
| 38 | /* convert a hex string into a byte string, assumes 'dst' is properly sized, and hexlen is even. |
| 39 | * no checks are performed */ |
| 40 | extern void gsm_hex_to_bytes0 ( cbytes_t hex, int hexlen, bytes_t dst ); |
| 41 | |
| 42 | /* convert a byte string into a hex string, assumes 'hex' is properly sized */ |
| 43 | extern void gsm_hex_from_bytes( char* hex, cbytes_t src, int srclen ); |
| 44 | |
| 45 | /* convert a hexchar to an int, returns -1 on error */ |
| 46 | extern int gsm_hexchar_to_int( char c ); |
| 47 | |
| 48 | /* convert a hexchar to an int, returns 0 on error */ |
| 49 | extern int gsm_hexchar_to_int0( char c ); |
| 50 | |
| 51 | /* convert a 2-char hex value into an int, returns -1 on error */ |
| 52 | extern int gsm_hex2_to_byte( const char* hex ); |
| 53 | |
| 54 | /* convert a 2-char hex value into an int, returns 0 on error */ |
| 55 | extern int gsm_hex2_to_byte0( const char* hex ); |
| 56 | |
| 57 | /* convert a 4-char hex value into an int, returns -1 on error */ |
| 58 | extern int gsm_hex4_to_short( const char* hex ); |
| 59 | |
| 60 | /* convert a 4-char hex value into an int, returns 0 on error */ |
| 61 | extern int gsm_hex4_to_short0( const char* hex ); |
| 62 | |
| 63 | /* write a byte to a 2-byte hex string */ |
| 64 | extern void gsm_hex_from_byte( char* hex, int val ); |
| 65 | |
| 66 | extern void gsm_hex_from_short( char* hex, int val ); |
| 67 | |
| 68 | /** UTF-8 and GSM Alphabet |
| 69 | **/ |
| 70 | |
| 71 | /* check that a given utf8 string is well-formed, returns 1 on success, 0 otherwise */ |
| 72 | extern int utf8_check( cbytes_t utf8, int utf8len ); |
| 73 | |
| 74 | /* check that all characters in a given utf8 string can be encoded into the GSM alphabet. |
| 75 | returns 1 if TRUE, 0 otherwise */ |
| 76 | extern int utf8_check_gsm7( cbytes_t utf8, int utf8len ); |
| 77 | |
| 78 | /* try to skip enough utf8 characters to generate gsm7len GSM septets */ |
| 79 | extern cbytes_t utf8_skip_gsm7( cbytes_t utf8, cbytes_t utf8end, int gsm7len ); |
| 80 | |
| 81 | /* convert a utf-8 string into a GSM septet string, assumes 'dst' is NULL or is properly sized, |
| 82 | and that all characters are representable. 'offset' is the starting bit offset in 'dst'. |
| 83 | non-representable characters are replaced by spaces. |
| 84 | returns the number of septets, */ |
| 85 | extern int utf8_to_gsm7( cbytes_t utf8, int utf8len, bytes_t dst, int offset ); |
| 86 | |
| 87 | /* convert a utf8 string into an array of 8-bit unpacked GSM septets, |
| 88 | * assumes 'dst' is NULL or is properly sized, returns the number of GSM bytes */ |
| 89 | extern int utf8_to_gsm8( cbytes_t utf8, int utf8len, bytes_t dst ); |
| 90 | |
| 91 | /* convert a GSM septets string into a utf-8 byte string. assumes that 'utf8' is NULL or properly |
| 92 | sized. 'offset' is the starting bit offset in 'src', 'count' is the number of input septets. |
| 93 | return the number of utf8 bytes. */ |
| 94 | extern int utf8_from_gsm7( cbytes_t src, int offset, int count, bytes_t utf8 ); |
| 95 | |
| 96 | /* convert an unpacked 8-bit GSM septets string into a utf-8 byte string. assumes that 'utf8' |
| 97 | is NULL or properly sized. 'count' is the number of input bytes. |
| 98 | returns the number of utf8 bytes */ |
| 99 | extern int utf8_from_gsm8( cbytes_t src, int count, bytes_t utf8 ); |
| 100 | |
| 101 | |
| 102 | /** UCS-2 and GSM Alphabet |
| 103 | ** |
| 104 | ** Note that here, 'ucs2' really refers to non-aligned UCS2-BE, as used by the GSM standard |
| 105 | **/ |
| 106 | |
| 107 | /* check that all characters in a given ucs2 string can be encoded into the GSM alphabet. |
| 108 | returns 1 if TRUE, 0 otherwise */ |
| 109 | extern int ucs2_check_gsm7( cbytes_t ucs2, int ucs2len ); |
| 110 | |
| 111 | /* convert a ucs2 string into a GSM septet string, assumes 'dst' is NULL or properly sized, |
| 112 | 'offset' is the starting bit offset in 'dst'. non-representable characters are replaced |
| 113 | by spaces. returns the number of septets */ |
| 114 | extern int ucs2_to_gsm7( cbytes_t ucs2, int ucs2len, bytes_t dst, int offset ); |
| 115 | |
| 116 | /* convert a ucs2 string into a GSM septet string, assumes 'dst' is NULL or properly sized, |
| 117 | non-representable characters are replaced by spaces. returns the number of bytes */ |
| 118 | extern int ucs2_to_gsm8( cbytes_t ucs2, int ucs2len, bytes_t dst ); |
| 119 | |
| 120 | /* convert a GSM septets string into a ucs2 string. assumes that 'ucs2' is NULL or |
| 121 | properly sized. 'offset' is the starting bit offset in 'src', 'count' is the number |
| 122 | of input septets. return the number of ucs2 characters (not bytes) */ |
| 123 | extern int ucs2_from_gsm7( bytes_t ucs2, cbytes_t src, int offset, int count ); |
| 124 | |
| 125 | /* convert an 8-bit unpacked GSM septets string into a ucs2 string. assumes that 'ucs2' |
| 126 | is NULL or properly sized. 'count' is the number of input septets. return the number |
| 127 | of ucs2 characters (not bytes) */ |
| 128 | extern int ucs2_from_gsm8( bytes_t ucs2, cbytes_t src, int count ); |
| 129 | |
| 130 | |
| 131 | /** UCS2 to/from UTF8 |
| 132 | **/ |
| 133 | |
| 134 | /* convert a ucs2 string into a utf8 byte string, assumes 'utf8' NULL or properly sized. |
| 135 | returns the number of utf8 bytes*/ |
| 136 | extern int ucs2_to_utf8( cbytes_t ucs2, int ucs2len, bytes_t utf8 ); |
| 137 | |
| 138 | /* convert a utf8 byte string into a ucs2 string, assumes 'ucs2' NULL or properly sized. |
| 139 | returns the number of ucs2 chars */ |
| 140 | extern int utf8_to_ucs2( cbytes_t utf8, int utf8len, bytes_t ucs2 ); |
| 141 | |
| 142 | /* try to skip a given number of characters in a utf-8 byte string, return new position */ |
| 143 | extern cbytes_t utf8_skip( cbytes_t utf8, cbytes_t utf8end, int count); |
| 144 | |
| 145 | /** Dial Numbers: TON byte + 'count' bcd numbers |
| 146 | **/ |
| 147 | |
| 148 | /* convert a bcd-coded GSM dial number into an ASCII string (not zero-terminated) |
| 149 | assumes 'dst' is NULL or properly sized, returns 0 in case of success, -1 in case of error. |
| 150 | 'num_digits' is the number of digits, not input bytes. a trailing 0xf0 is ignored automatically |
| 151 | return the number of ASCII chars */ |
| 152 | extern int gsm_bcdnum_to_ascii ( cbytes_t bcd, int num_digits, bytes_t dst ); |
| 153 | |
| 154 | /* convert an ASCII dial-number into a bcd-coded string, returns the number of 4-bit nibbles written, */ |
| 155 | extern int gsm_bcdnum_from_ascii( cbytes_t ascii, int asciilen, bytes_t dst ); |
| 156 | |
| 157 | /** ADN: Abbreviated Dialing Numbers |
| 158 | **/ |
| 159 | #define SIM_ADN_MAX_ALPHA 20 /* maximum number of characters in ADN alpha tag */ |
| 160 | #define SIM_ADN_MAX_NUMBER 20 /* maximum digits in ADN number */ |
| 161 | |
| 162 | typedef struct { |
| 163 | byte_t alpha [ SIM_ADN_MAX_ALPHA*3+1 ]; /* alpha tag in zero-terminated utf-8 */ |
| 164 | char number[ SIM_ADN_MAX_NUMBER+1 ]; /* dialing number in zero-terminated ASCII */ |
| 165 | } |
| 166 | SimAdnRec, *SimAdn; |
| 167 | |
| 168 | typedef struct { |
| 169 | SimAdnRec adn; |
| 170 | byte_t ext_record; /* 0 or 0xFF means no extension */ |
| 171 | } |
| 172 | SimAdnRecordRec, *SimAdnRecord; |
| 173 | |
| 174 | extern int sim_adn_record_from_bytes( SimAdnRecord rec, cbytes_t data, int datalen ); |
| 175 | extern int sim_adn_record_to_bytes ( SimAdnRecord rec, bytes_t data, int datalen ); |
| 176 | |
| 177 | /** ROPES |
| 178 | **/ |
| 179 | |
| 180 | typedef struct { |
| 181 | bytes_t data; |
| 182 | int max; |
| 183 | int pos; |
| 184 | int error; |
| 185 | unsigned char data0[16]; |
| 186 | } GsmRopeRec, *GsmRope; |
| 187 | |
| 188 | extern void gsm_rope_init( GsmRope rope ); |
| 189 | extern void gsm_rope_init_alloc( GsmRope rope, int alloc ); |
| 190 | extern int gsm_rope_done( GsmRope rope ); |
| 191 | extern bytes_t gsm_rope_done_acquire( GsmRope rope, int *psize ); |
| 192 | extern void gsm_rope_add_c( GsmRope rope, char c ); |
| 193 | extern void gsm_rope_add( GsmRope rope, const void* str, int len ); |
| 194 | extern void* gsm_rope_reserve( GsmRope rope, int len ); |
| 195 | |
Marc Petit-Huguenin | a1b379c | 2010-07-14 12:33:15 -0700 | [diff] [blame] | 196 | #define PHONE_PREFIX "1555521" |
| 197 | |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 198 | #endif /* _android_gsm_h */ |