Annotation of parser3/src/main/pa_charsets.C, revision 1.31
1.1 paf 1: /** @file
2: Parser: sql driver manager implementation.
3:
1.30 moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.26 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.9 paf 6: */
1.1 paf 7:
1.19 moko 8: #include "pa_charsets.h"
1.31 ! moko 9: #include "pa_convert_utf.h"
1.1 paf 10:
1.31 ! moko 11: volatile const char * IDENT_PA_CHARSETS_C="$Id: pa_charsets.C,v 1.30 2026/04/25 13:38:46 moko Exp $" IDENT_PA_CHARSETS_H;
1.12 paf 12:
13: // defines for globals
14:
15: #define CHARSET_UTF8_NAME "UTF-8"
1.31 ! moko 16: #define CHARSET_UTF16_NAME "UTF-16"
1.1 paf 17:
18: // globals
19:
1.28 moko 20: static String charset_utf8_name(CHARSET_UTF8_NAME);
21: Charset pa_UTF8_charset(0, charset_utf8_name, 0/*no file=system*/);
1.1 paf 22:
1.31 ! moko 23: static String charset_latin1_name(CHARSET_LATIN1_NAME);
! 24: static Charset latin1_charset(0, charset_latin1_name, 0/*no file=system*/);
! 25:
! 26: static String charset_utf16_name(CHARSET_UTF16_NAME);
! 27: Charset pa_UTF16_charset(0, charset_utf16_name, 0/*no file=system*/);
! 28:
1.23 moko 29: Charsets pa_charsets;
1.1 paf 30:
1.12 paf 31: // methods
1.1 paf 32:
1.12 paf 33: Charsets::Charsets() {
1.23 moko 34: put(pa_UTF8_charset.NAME(), &pa_UTF8_charset);
1.31 ! moko 35: put(latin1_charset.NAME(), &latin1_charset);
! 36: put("US-ASCII", &latin1_charset);
1.1 paf 37: }
38:
1.22 moko 39: Charset& Charsets::get(String::Body ANAME) {
1.29 moko 40: ANAME=String::Body(str_upper(ANAME.cstr(), ANAME.length()));
1.18 misha 41: if(Charset* result=HashString<value_type>::get(ANAME))
1.2 paf 42: return *result;
43: else
1.22 moko 44: throw Exception(PARSER_RUNTIME, new String(ANAME, String::L_TAINTED), "unknown charset");
45: }
46:
1.31 ! moko 47: /// the charset of the files to load and save: UTF-16 can't be used as request/response charset
! 48: Charset& Charsets::get_file_charset(String::Body ANAME) {
! 49: return strcasecmp(ANAME.cstr(), CHARSET_UTF16_NAME)==0 ? pa_UTF16_charset : get(ANAME);
! 50: }
! 51:
1.22 moko 52: Charset& Charsets::get_direct(const char *ANAME) {
53: if(Charset* result=HashString<value_type>::get(ANAME))
54: return *result;
55: else
56: throw Exception(PARSER_RUNTIME, new String(ANAME, String::L_TAINTED), "unknown charset");
1.2 paf 57: }
1.3 paf 58:
1.31 ! moko 59: static void utf16_to_utf8(char *&body, size_t &body_size, bool big_endian){
! 60: size_t count=body_size/2;
! 61: const UTF16* utf16=(const UTF16*)body;
! 62: #ifdef PA_LITTLE_ENDIAN
! 63: if(big_endian || (size_t)body % sizeof(UTF16)) // otherwise as is
! 64: #endif
! 65: { // copied byte by byte: the content is not to be altered as file body can be used later
! 66: UTF16* copy=(UTF16*)pa_malloc_atomic(count*sizeof(UTF16)+1);
! 67: const unsigned char* src=(const unsigned char*)body;
! 68: for(size_t i=0; i<count; i++, src+=2)
! 69: copy[i]=big_endian ? src[0]<<8 | src[1] : src[1]<<8 | src[0];
! 70: utf16=copy;
! 71: }
! 72:
! 73: char* result=(char*)pa_malloc_atomic(count*3+1); // 3 bytes at most per UTF-16 code unit
! 74: const UTF16* utf16_start=utf16;
! 75: UTF8* result_end=(UTF8*)result;
! 76: pa_convertUTF16toUTF8(&utf16_start, utf16+count, &result_end, (UTF8*)result+count*3, lenientConversion);
! 77: *result_end=0;
! 78:
! 79: body=result;
! 80: body_size=(char*)result_end-result;
! 81: }
! 82:
1.20 moko 83: Charset* Charsets::checkBOM(char *&body,size_t &body_size, Charset* enforced_charset){
1.31 ! moko 84: if(!enforced_charset || enforced_charset->isUTF8()){
! 85: if(body_size>=3 && strncmp(body, "\xEF\xBB\xBF", 3)==0){
! 86: // skip UTF-8 signature (BOM code)
! 87: body+=3;
! 88: body_size-=3;
! 89: return &pa_UTF8_charset;
! 90: }
! 91: if(body_size>=2 && (memcmp(body, "\xFF\xFE", 2)==0 || memcmp(body, "\xFE\xFF", 2)==0)){
! 92: // UTF-16 signature: converted to UTF-8
! 93: bool big_endian=body[0]=='\xFE';
! 94: body+=2;
! 95: body_size-=2;
! 96: utf16_to_utf8(body, body_size, big_endian);
! 97: return &pa_UTF8_charset;
! 98: }
! 99: if(enforced_charset==&pa_UTF16_charset){
! 100: // UTF-16 without signature: little endian
! 101: utf16_to_utf8(body, body_size, false);
! 102: return &pa_UTF8_charset;
! 103: }
1.20 moko 104: }
105: return enforced_charset;
106: }
107:
1.31 ! moko 108: // little endian only
! 109: String::C Charsets::encodeUTF16(const String::C src, bool bom){
! 110: UTF16* result=(UTF16*)pa_malloc_atomic((src.length+1)*sizeof(UTF16)); // the signature and a code unit at most per UTF-8 byte
! 111: UTF16* utf16_end=result;
! 112: if(bom)
! 113: *utf16_end++=0xFEFF;
! 114: const UTF8* src_start=(const UTF8*)src.str;
! 115: int status=pa_convertUTF8toUTF16(&src_start, (const UTF8*)src.str+src.length, &utf16_end, result+src.length+1, lenientConversion);
! 116: if(status!=conversionOK)
! 117: throw Exception(PARSER_RUNTIME, 0, "invalid UTF-8 text can not be saved in UTF-16 (%d)", status);
! 118:
! 119: #ifndef PA_LITTLE_ENDIAN
! 120: for(UTF16* c=result; c<utf16_end; c++) // our own buffer: swapped in place
! 121: *c=(UTF16)(*c<<8 | *c>>8);
! 122: #endif
! 123: return String::C((const char*)result, (char*)utf16_end-(char*)result);
! 124: }
! 125:
1.22 moko 126: void Charsets::load_charset(Request_charsets& charsets, String::Body ANAME, const String& afile_spec) {
1.29 moko 127: ANAME=String::Body(str_upper(ANAME.cstr(), ANAME.length()));
1.3 paf 128: //we know that charset?
1.18 misha 129: if(HashString<value_type>::get(ANAME))
1.3 paf 130: return; // don't load it then
131:
1.12 paf 132: put(ANAME, new Charset(&charsets, ANAME, &afile_spec));
1.4 paf 133: }
E-mail: