00001 /*This file has been prepared for Doxygen automatic documentation generation.*/ 00013 /* Copyright (c) 2009 Atmel Corporation. All rights reserved. 00014 * 00015 * Redistribution and use in source and binary forms, with or without 00016 * modification, are permitted provided that the following conditions are met: 00017 * 00018 * 1. Redistributions of source code must retain the above copyright notice, this 00019 * list of conditions and the following disclaimer. 00020 * 00021 * 2. Redistributions in binary form must reproduce the above copyright notice, 00022 * this list of conditions and the following disclaimer in the documentation 00023 * and/or other materials provided with the distribution. 00024 * 00025 * 3. The name of Atmel may not be used to endorse or promote products derived 00026 * from this software without specific prior written permission. 00027 * 00028 * 4. This software may only be redistributed and used in connection with an Atmel 00029 * AVR product. 00030 * 00031 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 00032 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00033 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00034 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR 00035 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00036 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00037 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00038 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00039 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00040 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE 00041 * 00042 */ 00043 00044 //_____ I N C L U D E S ___________________________________________________ 00045 00046 #include "compiler.h" 00047 #include "unicode.h" 00048 00049 00050 //_____ D E F I N I T I O N S ______________________________________________ 00051 00052 00059 U8 unicode_header_scan( U8* header ) 00060 { 00061 if( (header[0] == 0xEF) 00062 && (header[1] == 0xBB) 00063 && (header[2] == 0xBF) ) 00064 { 00065 return UNI_TYPE_UTF8; 00066 } 00067 if( (header[0] == 0xFE) 00068 && (header[1] == 0xFF) ) 00069 { 00070 return UNI_TYPE_UTF16BE; 00071 } 00072 if( (header[0] == 0xFF) 00073 && (header[1] == 0xFE) ) 00074 { 00075 return UNI_TYPE_UTF16LE; 00076 } 00077 return UNI_TYPE_ASCII; 00078 } 00079 00080 00088 U8 unicode_header_get( U8* header, U8 txt_format ) 00089 { 00090 switch( txt_format ) 00091 { 00092 case UNI_TYPE_UTF8: 00093 header[0] = 0xEF; 00094 header[1] = 0xBB; 00095 header[2] = 0xBF; 00096 return 3; 00097 00098 case UNI_TYPE_UTF16BE: 00099 header[0] = 0xFE; 00100 header[1] = 0xFF; 00101 return 2; 00102 00103 case UNI_TYPE_UTF16LE: 00104 header[0] = 0xFF; 00105 header[1] = 0xFE; 00106 return 2; 00107 } 00108 return 0; 00109 } 00110 00118 U8 utf8_to_unicode( U8* utf8, U16* unicode ) 00119 { 00120 U8 c0,c1,c2; 00121 00122 // Take 3 bytes 00123 c0 = utf8[0]; 00124 c1 = utf8[1]; 00125 c2 = utf8[2]; 00126 00127 if( 0x00 == (c0 & 0x80) ) 00128 { 00129 *unicode = c0; 00130 return 1; 00131 } 00132 if( 0xC0 == (c0 & 0xE0) ) 00133 { 00134 *unicode = ((U16)(c0 & 0x1F)<<6) | ((U16)(c1 & 0x3F)); 00135 return 2; 00136 } 00137 if( 0xE0 == (c0 & 0xF0) ) 00138 { 00139 *unicode = ((U16)(c0 & 0x0F)<<(6+6)) | ((U16)(c1 & 0x3F)<<(6)) | ((U16)(c2 & 0x3F)); 00140 return 3; 00141 } 00142 // Error ! 00143 *unicode = c0; 00144 return 1; 00145 } 00146