Пример #1
0
GetText::~GetText()
{
    iconv_close(iconv_cd_);
}
Пример #2
0
/* This function returns a newly allocated wide string containing the USB
   device string numbered by the index. The returned string must be freed
   by using free(). */
static wchar_t *get_usb_string(libusb_device_handle *dev, uint8_t idx)
{
	char buf[512];
	int len;
	wchar_t *str = NULL;
	wchar_t wbuf[256];

	/* iconv variables */
	iconv_t ic;
	size_t inbytes;
	size_t outbytes;
	size_t res;
	const char *inptr;
	char *outptr;

	/* Determine which language to use. */
	uint16_t lang;
	lang = get_usb_code_for_current_locale();
	if (!is_language_supported(dev, lang))
		lang = get_first_language(dev);

	/* Get the string from libusb. */
	len = libusb_get_string_descriptor(dev,
			idx,
			lang,
			(unsigned char*)buf,
			sizeof(buf));
	if (len < 0)
		return NULL;

	/* buf does not need to be explicitly NULL-terminated because
	   it is only passed into iconv() which does not need it. */

	/* Initialize iconv. */
	ic = iconv_open("WCHAR_T", "UTF-16LE");
	if (ic == (iconv_t)-1) {
		LOG("iconv_open() failed\n");
		return NULL;
	}

	/* Convert to native wchar_t (UTF-32 on glibc/BSD systems).
	   Skip the first character (2-bytes). */
	inptr = buf+2;
	inbytes = len-2;
	outptr = (char*) wbuf;
	outbytes = sizeof(wbuf);
	res = iconv(ic, &inptr, &inbytes, &outptr, &outbytes);
	if (res == (size_t)-1) {
		LOG("iconv() failed\n");
		goto err;
	}

	/* Write the terminating NULL. */
	wbuf[sizeof(wbuf)/sizeof(wbuf[0])-1] = 0x00000000;
	if (outbytes >= sizeof(wbuf[0]))
		*((wchar_t*)outptr) = 0x00000000;

	/* Allocate and copy the string. */
	str = wcsdup(wbuf);

err:
	iconv_close(ic);

	return str;
}
Пример #3
0
static rc_uint_type
wind_MultiByteToWideChar (rc_uint_type cp, const char *mb,
			  unichar *u, rc_uint_type u_len)
{
  rc_uint_type ret = 0;

#if defined (_WIN32) || defined (__CYGWIN__)
  ret = (rc_uint_type) MultiByteToWideChar (cp, MB_PRECOMPOSED,
					    mb, -1, u, u_len);
  /* Convert to bytes. */
  ret *= sizeof (unichar);

#elif defined (HAVE_ICONV_H)
  int first = 1;
  char tmp[32];
  char *p_tmp;
  const char *iconv_name = wind_iconv_cp (cp);

  if (!mb || !iconv_name)
    return 0;
  iconv_t cd = iconv_open ("UTF-16", iconv_name);

  while (1)
    {
      int iret;
      const char *n_mb;
      char *n_tmp;

      p_tmp = tmp;
      iret = iconv_onechar (cd, (const char *) mb, p_tmp, 32, & n_mb, & n_tmp);
      if (first)
	{
	  first = 0;
	  continue;
	}
      if (!iret)
	{
	  size_t l_tmp = (size_t) (n_tmp - p_tmp);

	  if (u)
	    {
	      if ((size_t) u_len < l_tmp)
		break;
	      memcpy (u, tmp, l_tmp);
	      u += l_tmp/2;
	      u_len -= l_tmp;
	    }
	  ret += l_tmp;
	}
      else
	break;
      if (tmp[0] == 0 && tmp[1] == 0)
	break;
      mb = n_mb;
    }
  iconv_close (cd);
#else
  if (cp)
    ret = 0;
  ret = strlen (mb) + 1;
  ret *= sizeof (unichar);
  if (u != NULL && u_len != 0)
    {
      do
	{
	  *u++ = ((unichar) *mb) & 0xff;
	  --u_len; mb++;
	}
      while (u_len != 0 && mb[-1] != 0);
    }
  if (u != NULL && u_len != 0)
    *u = 0;
#endif
  return ret;
}
Пример #4
0
        /*
         *  convert encode to GBK after parsing.
         */
void html_parser::encode_conv()
{
    char charset_check[200];
    //char charset_conv_str[200];
    //size_t output_allocated = 0;
    //size_t output_length = 0; // buffer_conv_len_;
    //bool success;
    char *outbuff = NULL;

    //printf("charset: %s\n", tree_->charset_);
    if (charset_detector(page_, charset_check) == 0)
    {
        if (strcasecmp(charset_check, tree_->charset_) == 0)
        {
            if (strcasecmp(tree_->charset_, "gbk") == 0 ||
                strcasecmp(tree_->charset_, "gb2312") == 0)
                return;         // no charset_ to be convert.
        } else
        {
            strcpy(tree_->charset_, charset_check);
        }
    } else
    {
        if (strcasecmp(tree_->charset_, "gbk") == 0 ||
            strcasecmp(tree_->charset_, "gb2312") == 0)
            return;             // no charset_ to be convert.
    }

    if (tree_->charset_[0] == '\0')
    {
        title_[0] = 0;
        content_[0] = 0;
        return;
    }

    if (content_[0] == '\0' && title_[0] == '\0' && links_num_ == 0)
        return;

    // 2007-11-12 by huwei
    iconv_t cd;
    size_t bufsize, inlen, oleft;
    char *pin, *pout;
    int tflag = 1, cflag = 1;

    cd = iconv_open((const char *) "gbk//ignore",
                    (const char *) tree_->charset_);
    if (cd == (iconv_t) - 1)
    {
        //printf("iconv_open(%s, %s) failed\n", "gbk", tree_->charset_);
    } else
    {
        bufsize = 512 * 1024;
        outbuff = (char *) malloc(bufsize + 1);
        assert(outbuff);

        if (title_[0])
        {
            bzero(outbuff, bufsize);
            pin = title_;
            pout = outbuff;
            inlen = strlen(title_);
            oleft = bufsize;

            //printf("title start\n");
            if (iconv(cd, &pin, &inlen, &pout, &oleft) != (size_t) (-1))
            {
                tflag = 0;
                //printf("title success\n");
                *(outbuff + (bufsize - oleft)) = 0;
                strcpy(title_, outbuff);
            } else
            {
                *(outbuff + (bufsize - oleft)) = 0;
                strcpy(title_, outbuff);
            }
            //printf("iconv title: %s\n", outbuff);
        }

        if (links_num_)
        {
            for (int i = 0; i < links_num_; ++i)
            {
                bzero(outbuff, bufsize);
                pin = links_[i].text;
                pout = outbuff;
                inlen = strlen(links_[i].text);
                oleft = bufsize - 1;

                if (iconv(cd, &pin, &inlen, &pout, &oleft) !=
                    (size_t) (-1))
                {
                    *(outbuff + (bufsize - oleft)) = 0;
                    strcpy(links_[i].text, outbuff);
                } else
                {
                    *(outbuff + (bufsize - oleft)) = 0;
                    strcpy(links_[i].text, outbuff);
                }
            }
        }

        bzero(outbuff, bufsize);
        if (content_[0])
        {
            pin = content_;
            pout = outbuff;
            inlen = strlen(content_);
            oleft = bufsize - 1;

            //printf("contnet start\n");
            size_t ret;
            if ((ret =
                 iconv(cd, &pin, &inlen, &pout, &oleft)) != (size_t) (-1))
            {
                cflag = 0;
                //printf("content success\n");
                *(outbuff + (bufsize - oleft)) = 0;
                strcpy(content_, outbuff);
            } else
            {
                *(outbuff + (bufsize - oleft)) = 0;
                strcpy(content_, outbuff);
            }
        }

        free(outbuff);
        iconv_close(cd);
    }

/*
	// -------------------------------------------------------
	RECODE_REQUEST request = recode_new_request (outer_);

	sprintf(charset_conv_str,"%s..gbk",tree_->charset_ );

       if ( !recode_scan_request (request, charset_conv_str) )
		goto out;

	if ( title_[0] && tflag ) {
		output_allocated = buffer_conv_len_;

//	printf("CO: %s\n%s\n", (char*)recode_string (request, title_), title_ );	

    	   success = recode_string_to_buffer (request, title_, &buffer_conv_,
		 & output_length, &output_allocated);
	
		if ( success )
			strcpy(title_, buffer_conv_);
	}


	if ( content_[0] && cflag ) {
		output_allocated = buffer_conv_len_;
    	   success = recode_string_to_buffer (request, content_, &buffer_conv_,
		 & output_length, &output_allocated);

		if ( success )
			strcpy(content_, buffer_conv_);

//			UnescapeEntitiesForString(buffer_conv_, strlen(buffer_conv_), content_, max_content_len);
	}


	if ( links_num_ ) {
	        for(int i=0; i < links_num_; ++i)
        	{
			output_allocated = buffer_conv_len_;

    	   		success = recode_string_to_buffer (request, links_[i].text, &buffer_conv_,
		 		& output_length, &output_allocated);

//			printf("LINKS code: %s\n;%s\n", links_[i].text, buffer_conv_ );
			if ( success )
				strcpy( links_[i].text, buffer_conv_);

        	}

	}



out:
	if(outbuff != NULL)
		recode_delete_request (request);
*/

}
Пример #5
0
iconv::~iconv() {
  iconv_close(cd);
}
Пример #6
0
static void iconv_unrefer(void* arg)
{
    iconv_close((iconv_t)arg);
}
Пример #7
0
LIBETPAN_EXPORT
int charconv_buffer(const char * tocode, const char * fromcode,
                    const char * str, size_t length,
                    char ** result, size_t * result_len)
{
#ifdef HAVE_ICONV
    iconv_t conv;
    size_t iconv_r;
    int r;
    char * out;
    char * pout;
    size_t out_size;
    size_t old_out_size;
    size_t count;
#endif
    int res;
    MMAPString * mmapstr;

    if (extended_charconv != NULL) {
        size_t		result_length;
        result_length = length * 6;
        mmapstr = mmap_string_sized_new( result_length + 1);
        *result_len = 0;
        if (mmapstr == NULL) {
            res = MAIL_CHARCONV_ERROR_MEMORY;
        } else {
            res = (*extended_charconv)( tocode, fromcode, str, length, mmapstr->str, &result_length);
            if (res != MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET) {
                if (res == MAIL_CHARCONV_NO_ERROR) {
                    *result = mmapstr->str;
                    res = mmap_string_ref(mmapstr);
                    if (res < 0) {
                        res = MAIL_CHARCONV_ERROR_MEMORY;
                        mmap_string_free(mmapstr);
                    } else {
                        mmap_string_set_size( mmapstr, result_length);	/* can't fail */
                        *result_len = result_length;
                    }
                }
                free( *result);
            }
            return res;
        }
        /* else, let's try with iconv, if available */
    }

#ifndef HAVE_ICONV
    return MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET;
#else

    conv = iconv_open(tocode, fromcode);
    if (conv == (iconv_t) -1) {
        res = MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET;
        goto err;
    }

    out_size = 6 * length; /* UTF-8 can be encoded up to 6 bytes */

    mmapstr = mmap_string_sized_new(out_size + 1);
    if (mmapstr == NULL) {
        res = MAIL_CHARCONV_ERROR_MEMORY;
        goto err;
    }

    out = mmapstr->str;

    pout = out;
    old_out_size = out_size;

    iconv_r = mail_iconv(conv, &str, &length, &pout, &out_size, NULL, "?");

    if (iconv_r == (size_t) -1) {
        res = MAIL_CHARCONV_ERROR_CONV;
        goto free;
    }

    iconv_close(conv);

    * pout = '\0';

    count = old_out_size - out_size;

    r = mmap_string_ref(mmapstr);
    if (r < 0) {
        res = MAIL_CHARCONV_ERROR_MEMORY;
        goto free;
    }

    * result = out;
    * result_len = count;

    return MAIL_CHARCONV_NO_ERROR;

free:
    mmap_string_free(mmapstr);
err:
    return res;
#endif
}
Пример #8
0
Iconv::~Iconv() {
	if (cd != iconv_invalid) iconv_close(cd);
}
Пример #9
0
/*
 * Handle subjects with RFC2047 encoding such as:
 * =?koi8-r?B?78bP0s3Mxc7JxSDXz9rE1dvO2c3JINvB0sHNySDP?=
 */
void utf8ify_rfc822_string(char *buf) {
	char *start, *end, *next, *nextend, *ptr;
	char newbuf[1024];
	char charset[128];
	char encoding[16];
	char istr[1024];
	iconv_t ic = (iconv_t)(-1) ;
	char *ibuf;			/**< Buffer of characters to be converted */
	char *obuf;			/**< Buffer for converted characters */
	size_t ibuflen;			/**< Length of input buffer */
	size_t obuflen;			/**< Length of output buffer */
	char *isav;			/**< Saved pointer to input buffer */
	char *osav;			/**< Saved pointer to output buffer */
	int passes = 0;
	int i, len, delta;
	int illegal_non_rfc2047_encoding = 0;

	/* Sometimes, badly formed messages contain strings which were simply
	 *  written out directly in some foreign character set instead of
	 *  using RFC2047 encoding.  This is illegal but we will attempt to
	 *  handle it anyway by converting from a user-specified default
	 *  charset to UTF-8 if we see any nonprintable characters.
	 */
	len = strlen(buf);
	for (i=0; i<len; ++i) {
		if ((buf[i] < 32) || (buf[i] > 126)) {
			illegal_non_rfc2047_encoding = 1;
			i = len; ///< take a shortcut, it won't be more than one.
		}
	}
	if (illegal_non_rfc2047_encoding) {
		const char *default_header_charset = "iso-8859-1";
		if ( (strcasecmp(default_header_charset, "UTF-8")) && (strcasecmp(default_header_charset, "us-ascii")) ) {
			ctdl_iconv_open("UTF-8", default_header_charset, &ic);
			if (ic != (iconv_t)(-1) ) {
				ibuf = malloc(1024);
				isav = ibuf;
				safestrncpy(ibuf, buf, 1024);
				ibuflen = strlen(ibuf);
				obuflen = 1024;
				obuf = (char *) malloc(obuflen);
				osav = obuf;
				iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
				osav[1024-obuflen] = 0;
				strcpy(buf, osav);
				free(osav);
				iconv_close(ic);
				free(isav);
			}
		}
	}

	/* pre evaluate the first pair */
	nextend = end = NULL;
	len = strlen(buf);
	start = strstr(buf, "=?");
	if (start != NULL) 
		FindNextEnd (start, end);

	while ((start != NULL) && (end != NULL))
	{
		next = strstr(end, "=?");
		if (next != NULL)
			FindNextEnd(next, nextend);
		if (nextend == NULL)
			next = NULL;

		/* did we find two partitions */
		if ((next != NULL) && 
		    ((next - end) > 2))
		{
			ptr = end + 2;
			while ((ptr < next) && 
			       (isspace(*ptr) ||
				(*ptr == '\r') ||
				(*ptr == '\n') || 
				(*ptr == '\t')))
				ptr ++;
			/* did we find a gab just filled with blanks? */
			if (ptr == next)
			{
				memmove (end + 2,
					 next,
					 len - (next - start));

				/* now terminate the gab at the end */
				delta = (next - end) - 2;
				len -= delta;
				buf[len] = '\0';

				/* move next to its new location. */
				next -= delta;
				nextend -= delta;
			}
		}
		/* our next-pair is our new first pair now. */
		start = next;
		end = nextend;
	}

	/* Now we handle foreign character sets properly encoded
	 * in RFC2047 format.
	 */
	start = strstr(buf, "=?");
	FindNextEnd((start != NULL)? start : buf, end);
	while (start != NULL && end != NULL && end > start)
	{
		extract_token(charset, start, 1, '?', sizeof charset);
		extract_token(encoding, start, 2, '?', sizeof encoding);
		extract_token(istr, start, 3, '?', sizeof istr);

		ibuf = malloc(1024);
		isav = ibuf;
		if (!strcasecmp(encoding, "B")) {	/**< base64 */
			ibuflen = CtdlDecodeBase64(ibuf, istr, strlen(istr));
		}
		else if (!strcasecmp(encoding, "Q")) {	/**< quoted-printable */
			size_t len;
			unsigned long pos;
			
			len = strlen(istr);
			pos = 0;
			while (pos < len)
			{
				if (istr[pos] == '_') istr[pos] = ' ';
				pos++;
			}

			ibuflen = CtdlDecodeQuotedPrintable(ibuf, istr, len);
		}
		else {
			strcpy(ibuf, istr);		/**< unknown encoding */
			ibuflen = strlen(istr);
		}

		ctdl_iconv_open("UTF-8", charset, &ic);
		if (ic != (iconv_t)(-1) ) {
			obuflen = 1024;
			obuf = (char *) malloc(obuflen);
			osav = obuf;
			iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
			osav[1024-obuflen] = 0;

			end = start;
			end++;
			strcpy(start, "");
			remove_token(end, 0, '?');
			remove_token(end, 0, '?');
			remove_token(end, 0, '?');
			remove_token(end, 0, '?');
			strcpy(end, &end[1]);

			snprintf(newbuf, sizeof newbuf, "%s%s%s", buf, osav, end);
			strcpy(buf, newbuf);
			free(osav);
			iconv_close(ic);
		}
		else {
			end = start;
			end++;
			strcpy(start, "");
			remove_token(end, 0, '?');
			remove_token(end, 0, '?');
			remove_token(end, 0, '?');
			remove_token(end, 0, '?');
			strcpy(end, &end[1]);

			snprintf(newbuf, sizeof newbuf, "%s(unreadable)%s", buf, end);
			strcpy(buf, newbuf);
		}

		free(isav);

		/*
		 * Since spammers will go to all sorts of absurd lengths to get their
		 * messages through, there are LOTS of corrupt headers out there.
		 * So, prevent a really badly formed RFC2047 header from throwing
		 * this function into an infinite loop.
		 */
		++passes;
		if (passes > 20) return;

		start = strstr(buf, "=?");
		FindNextEnd((start != NULL)? start : buf, end);
	}

}
Пример #10
0
LIBETPAN_EXPORT
int charconv(const char * tocode, const char * fromcode,
             const char * str, size_t length,
             char ** result)
{
#ifdef HAVE_ICONV
    iconv_t conv;
    size_t r;
    char * pout;
    size_t out_size;
    size_t old_out_size;
    size_t count;
#endif
    char * out;
    int res;

    if (extended_charconv != NULL) {
        size_t		result_length;
        result_length = length * 6;
        *result = malloc( length * 6 + 1);
        if (*result == NULL) {
            res = MAIL_CHARCONV_ERROR_MEMORY;
        } else {
            res = (*extended_charconv)( tocode, fromcode, str, length, *result, &result_length);
            if (res != MAIL_CHARCONV_NO_ERROR) {
                free( *result);
            } else {
                out = realloc( *result, result_length + 1);
                if (out != NULL) *result = out;
                /* also a cstring, just in case */
                (*result)[result_length] = '\0';
            }
        }
        if (res != MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET)
            return res;
        /* else, let's try with iconv, if available */
    }

#ifndef HAVE_ICONV
    return MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET;
#else

    if ((strcasecmp(fromcode, "GB2312") == 0) || (strcasecmp(fromcode, "GB_2312-80") == 0)) {
        fromcode = "GBK";
    }
    else if ((strcasecmp(fromcode, "iso-8859-8-i") == 0) || (strcasecmp(fromcode, "iso_8859-8-i") == 0) ||
             (strcasecmp(fromcode, "iso8859-8-i") == 0)) {
        fromcode = "iso-8859-8";
    }
    else if ((strcasecmp(fromcode, "iso-8859-8-e") == 0) || (strcasecmp(fromcode, "iso_8859-8-e") == 0) ||
             (strcasecmp(fromcode, "iso8859-8-e") == 0)) {
        fromcode = "iso-8859-8";
    }
    else if (strcasecmp(fromcode, "ks_c_5601-1987") == 0) {
        fromcode = "euckr";
    }

    conv = iconv_open(tocode, fromcode);
    if (conv == (iconv_t) -1) {
        res = MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET;
        goto err;
    }

    out_size = 6 * length; /* UTF-8 can be encoded up to 6 bytes */

    out = malloc(out_size + 1);
    if (out == NULL) {
        res = MAIL_CHARCONV_ERROR_MEMORY;
        goto close_iconv;
    }

    pout = out;
    old_out_size = out_size;

    r = mail_iconv(conv, &str, &length, &pout, &out_size, NULL, "?");

    if (r == (size_t) -1) {
        res = MAIL_CHARCONV_ERROR_CONV;
        goto free;
    }

    iconv_close(conv);

    * pout = '\0';
    count = old_out_size - out_size;
    pout = realloc(out, count + 1);
    if (pout != NULL)
        out = pout;

    * result = out;

    return MAIL_CHARCONV_NO_ERROR;

free:
    free(out);
close_iconv:
    iconv_close(conv);
err:
    return res;
#endif
}
Пример #11
0
/* MMDFiles_pathdup: convert charset from application to system */
char *MMDFiles_pathdup(const char *str)
{
#ifdef _WIN32
   return MMDFiles_strdup(str);
#endif /* _WIN32 */
#ifdef __APPLE__
   size_t i, size;
   char *inBuff, *outBuff;
   size_t inLen, outLen;
   CFStringRef cfs;

   inLen = MMDFiles_strlen(str);
   if(inLen <= 0)
      return NULL;

   inBuff = MMDFiles_strdup(str);
   if(inBuff == NULL)
      return NULL;

   /* convert directory separator */
   for(i = 0; i < inLen; i += size) {
      size = MMDFiles_getcharsize(&inBuff[i]);
      if(size == 1 && MMDFiles_dirseparator(inBuff[i]) == true)
         inBuff[i] = MMDFILES_DIRSEPARATOR;
   }

   /* convert multi-byte char */
   cfs = CFStringCreateWithCString(NULL, inBuff, kCFStringEncodingDOSJapanese);
   outLen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfs), kCFStringEncodingUTF8) + 1;
   outBuff = (char *) malloc(outLen);
   CFStringGetCString(cfs, outBuff, outLen, kCFStringEncodingUTF8);
   CFRelease(cfs);

   free(inBuff);
   return outBuff;
#endif /* __APPLE__ */
#if !defined(_WIN32) && !defined(__APPLE__)
   iconv_t ic;
   size_t i, size;
   char *inBuff, *outBuff;
   char *inFile, *outFile;
   size_t inLen, outLen;

   inLen = MMDFiles_strlen(str);
   if(inLen <= 0)
      return NULL;
   outLen = inLen * MMDFILESUTILS_MAXCHARBYTE;

   ic = iconv_open(MMDFILES_CHARSET, "SJIS");
   if(ic < 0)
      return NULL;

   inBuff = inFile = MMDFiles_strdup(str);
   outBuff = outFile = (char *) calloc(outLen, sizeof(char));

   /* convert directory separator */
   for(i = 0; i < inLen; i += size) {
      size = MMDFiles_getcharsize(&inFile[i]);
      if(size == 1 && MMDFiles_dirseparator(inFile[i]) == true)
         inFile[i] = MMDFILES_DIRSEPARATOR;
   }

   /* convert muli-byte char */
   if(iconv(ic, &inFile, &inLen, &outFile, &outLen) >= 0) {
      outFile = '\0';
   } else {
      strcpy(outBuff, "");
   }

   iconv_close(ic);

   free(inBuff);
   return outBuff;
#endif /* !_WIN32 && !__APPLE__ */
}
Пример #12
0
java_lexer *
java_new_lexer (FILE *finput, const char *encoding)
{
  java_lexer *lex = xmalloc (sizeof (java_lexer));
  int enc_error = 0;

  lex->finput = finput;
  lex->bs_count = 0;
  lex->unget_value = 0;
  lex->next_unicode = 0;
  lex->avail_unicode = 0;
  lex->next_columns = 1;
  lex->encoding = encoding;
  lex->position.line = 1;
  lex->position.col = 1;
#ifndef JC1_LITE
#ifdef USE_MAPPED_LOCATION
      input_location
	= linemap_line_start (&line_table, 1, 120);
#else
      input_line = 1;
#endif
#endif

#ifdef HAVE_ICONV
  lex->handle = iconv_open ("UCS-2", encoding);
  if (lex->handle != (iconv_t) -1)
    {
      lex->first = -1;
      lex->last = -1;
      lex->out_first = -1;
      lex->out_last = -1;
      lex->read_anything = 0;
      lex->use_fallback = 0;

      /* Work around broken iconv() implementations by doing checking at
	 runtime.  We assume that if the UTF-8 => UCS-2 encoder is broken,
	 then all UCS-2 encoders will be broken.  Perhaps not a valid
	 assumption.  */
      if (! byteswap_init)
	{
	  iconv_t handle;

	  byteswap_init = 1;

	  handle = iconv_open ("UCS-2", "UTF-8");
	  if (handle != (iconv_t) -1)
	    {
	      unicode_t result;
	      unsigned char in[3];
	      char *inp, *outp;
	      size_t inc, outc, r;

	      /* This is the UTF-8 encoding of \ufeff.  */
	      in[0] = 0xef;
	      in[1] = 0xbb;
	      in[2] = 0xbf;

	      inp = (char *) in;
	      inc = 3;
	      outp = (char *) &result;
	      outc = 2;

	      r = iconv (handle, (ICONV_CONST char **) &inp, &inc,
			 &outp, &outc);
	      iconv_close (handle);
	      /* Conversion must be complete for us to use the result.  */
	      if (r != (size_t) -1 && inc == 0 && outc == 0)
		need_byteswap = (result != 0xfeff);
	    }
	}

      lex->byte_swap = need_byteswap;
    }
  else
#endif /* HAVE_ICONV */
    {
      /* If iconv failed, use the internal decoder if the default
	 encoding was requested.  This code is used on platforms where
	 iconv exists but is insufficient for our needs.  For
	 instance, on Solaris 2.5 iconv cannot handle UTF-8 or UCS-2.

	 On Solaris the default encoding, as returned by nl_langinfo(),
	 is `646' (aka ASCII), but the Solaris iconv_open() doesn't
	 understand that.  We work around that by pretending
	 `646' to be the same as UTF-8.   */
      if (strcmp (encoding, DEFAULT_ENCODING) && strcmp (encoding, "646"))
	enc_error = 1;
#ifdef HAVE_ICONV
      else
        {
	  lex->use_fallback = 1;
	  lex->encoding = "UTF-8";
	}
#endif /* HAVE_ICONV */
    }

  if (enc_error)
    fatal_error ("unknown encoding: %qs\nThis might mean that your locale's encoding is not supported\nby your system's iconv(3) implementation.  If you aren't trying\nto use a particular encoding for your input file, try the\n%<--encoding=UTF-8%> option", encoding);

  return lex;
}
Пример #13
0
QByteArray QIconvCodec::convertFromUnicode(const QChar *uc, int len, ConverterState *convState) const
{
    char *inBytes;
    char *outBytes;
    size_t inBytesLeft;

#if defined(GNU_LIBICONV)
    const char **inBytesPtr = const_cast<const char **>(&inBytes);
#else
    char **inBytesPtr = &inBytes;
#endif

    QThreadStorage<QIconvCodec::IconvState *> *ts = fromUnicodeState();
    if (!qt_locale_initialized || !ts) {
        // we're running after the Q_GLOBAL_STATIC has been deleted
        // or before the QCoreApplication initialization
        // bad programmer, no cookie for you
        if (!len)
            // this is a special case - zero-sized string should be
            // translated to empty but not-null QByteArray.
            return QByteArray("");
        return QString::fromRawData(uc, len).toLatin1();
    }
    IconvState *&state = ts->localData();
    if (!state) {
        state = new IconvState(QIconvCodec::createIconv_t(0, UTF16));
        if (state->cd != reinterpret_cast<iconv_t>(-1)) {
            size_t outBytesLeft = len + 3; // +3 for the BOM
            QByteArray ba;
            ba.resize(outBytesLeft);
            outBytes = ba.data();

#if !defined(NO_BOM)
            // give iconv() a BOM
            QChar bom[] = { QChar(QChar::ByteOrderMark) };
            inBytes = reinterpret_cast<char *>(bom);
            inBytesLeft = sizeof(bom);
            if (iconv(state->cd, inBytesPtr, &inBytesLeft, &outBytes, &outBytesLeft) == (size_t) -1) {
                perror("QIconvCodec::convertFromUnicode: using ASCII for conversion, iconv failed for BOM");

                iconv_close(state->cd);
                state->cd = reinterpret_cast<iconv_t>(-1);

                return QString(uc, len).toAscii();
            }
#endif // NO_BOM
        }
    }
    if (state->cd == reinterpret_cast<iconv_t>(-1)) {
        static int reported = 0;
        if (!reported++) {
            fprintf(stderr,
                    "QIconvCodec::convertFromUnicode: using ASCII for conversion, iconv_open failed\n");
        }
        return QString(uc, len).toAscii();
    }
 
    size_t outBytesLeft = len;
    QByteArray ba;
    ba.resize(outBytesLeft);
    outBytes = ba.data();

    // now feed iconv() the real data
    inBytes = const_cast<char *>(reinterpret_cast<const char *>(uc));
    inBytesLeft = len * sizeof(QChar);

    QByteArray in;
    if (convState && convState->remainingChars) {
        // we have one surrogate char to be prepended
        in.resize(sizeof(QChar) + len);
        inBytes = in.data();

        QChar remaining = convState->state_data[0];
        memcpy(in.data(), &remaining, sizeof(QChar));
        memcpy(in.data() + sizeof(QChar), uc, inBytesLeft);

        inBytesLeft += sizeof(QChar);
        convState->remainingChars = 0;
    }

    int invalidCount = 0;
    do {
        if (iconv(state->cd, inBytesPtr, &inBytesLeft, &outBytes, &outBytesLeft) == (size_t) -1) {
            if (errno == EINVAL && convState) {
                // buffer ends in a surrogate
                Q_ASSERT(inBytesLeft == 2);
                convState->remainingChars = 1;
                convState->state_data[0] = uc[len - 1].unicode();
                break;
            }

            switch (errno) {
            case EILSEQ:
                ++invalidCount;
                // fall through
            case EINVAL:
                {
                    inBytes += sizeof(QChar);
                    inBytesLeft -= sizeof(QChar);
                    break;
                }
            case E2BIG:
                {
                    int offset = ba.size() - outBytesLeft;
                    ba.resize(ba.size() * 2);
                    outBytes = ba.data() + offset;
                    outBytesLeft = ba.size() - offset;
                    break;
                }
            default:
                {
                    // note, cannot use qWarning() since we are implementing the codecForLocale :)
                    perror("QIconvCodec::convertFromUnicode: using ASCII for conversion, iconv failed");

                    // reset to initial state
                    iconv(state->cd, 0, &inBytesLeft, 0, &outBytesLeft);

                    return QString(uc, len).toAscii();
                }
            }
        }
    } while (inBytesLeft != 0);

    // reset to initial state
    iconv(state->cd, 0, &inBytesLeft, 0, &outBytesLeft);

    ba.resize(ba.size() - outBytesLeft);

    if (convState)
        convState->invalidChars = invalidCount;

    return ba;
}
Пример #14
0
/* This function returns a newly allocated wide string containing the USB
   device string numbered by the index. The returned string must be freed
   by using free(). */
static wchar_t *get_usb_string(libusb_device_handle *dev, uint8_t idx)
{
	char buf[512];
	int len;
	wchar_t *str = NULL;
	wchar_t wbuf[256];

	/* iconv variables */
	iconv_t ic;
	size_t inbytes;
	size_t outbytes;
	size_t res;
	char *inptr;
	char *outptr;

	/* Determine which language to use. */
	uint16_t lang;
	lang = get_usb_code_for_current_locale();
	if (!is_language_supported(dev, lang))
		lang = get_first_language(dev);
		
	/* Get the string from libusb. */
	len = libusb_get_string_descriptor(dev,
			idx,
			lang,
			(unsigned char*)buf,
			sizeof(buf));
	if (len < 0)
		return NULL;
	
	buf[sizeof(buf)-1] = '\0';
	
	if (len+1 < sizeof(buf))
		buf[len+1] = '\0';
	
	/* Initialize iconv. */
	ic = iconv_open("UTF-32", "UTF-16");
	if (ic == (iconv_t)-1)
		return NULL;
	
	/* Convert to UTF-32 (wchar_t on glibc systems).
	   Skip the first character (2-bytes). */
	inptr = buf+2;
	inbytes = len-2;
	outptr = (char*) wbuf;
	outbytes = sizeof(wbuf);
	res = iconv(ic, &inptr, &inbytes, &outptr, &outbytes);
	if (res == (size_t)-1)
		goto err;

	/* Write the terminating NULL. */
	wbuf[sizeof(wbuf)/sizeof(wbuf[0])-1] = 0x00000000;
	if (outbytes >= sizeof(wbuf[0]))
		*((wchar_t*)outptr) = 0x00000000;
	
	/* Allocate and copy the string. */
	str = wcsdup(wbuf+1);

err:
	iconv_close(ic);
	
	return str;
}
Пример #15
0
/**
 * stringprep_convert:
 * @str: input zero-terminated string.
 * @to_codeset: name of destination character set.
 * @from_codeset: name of origin character set, as used by @str.
 *
 * Convert the string from one character set to another using the
 * system's iconv() function.
 *
 * Return value: Returns newly allocated zero-terminated string which
 *   is @str transcoded into to_codeset.
 **/
char *
stringprep_convert (const char *str,
		    const char *to_codeset, const char *from_codeset)
{
  iconv_t cd;
  char *dest;
  char *outp;
  ICONV_CONST char *p;
  size_t inbytes_remaining;
  size_t outbytes_remaining;
  size_t err;
  size_t outbuf_size;
  int have_error = 0;

  if (strcmp (to_codeset, from_codeset) == 0)
    {
      char *q;
      q = malloc (strlen (str) + 1);
      if (!q)
	return NULL;
      return strcpy (q, str);
    }

  cd = iconv_open (to_codeset, from_codeset);

  if (cd == (iconv_t) - 1)
    return NULL;

  p = (ICONV_CONST char *) str;

  inbytes_remaining = strlen (p);
  /* Guess the maximum length the output string can have.  */
  outbuf_size = (inbytes_remaining + 1) * 5;

  outp = dest = malloc (outbuf_size);
  if (dest == NULL)
    goto out;
  outbytes_remaining = outbuf_size - 1;	/* -1 for NUL */

again:

  err = iconv (cd, (ICONV_CONST char **) &p, &inbytes_remaining,
	       &outp, &outbytes_remaining);

  if (err == (size_t) - 1)
    {
      switch (errno)
	{
	case EINVAL:
	  /* Incomplete text, do not report an error */
	  break;

	case E2BIG:
	  {
	    size_t used = outp - dest;
	    char *newdest;

	    outbuf_size *= 2;
	    newdest = realloc (dest, outbuf_size);
	    if (newdest == NULL)
	      {
		have_error = 1;
		goto out;
	      }
	    dest = newdest;

	    outp = dest + used;
	    outbytes_remaining = outbuf_size - used - 1; /* -1 for NUL */

	    goto again;
	  }
	  break;

	case EILSEQ:
	  have_error = 1;
	  break;

	default:
	  have_error = 1;
	  break;
	}
    }

  *outp = '\0';

  if (*p != '\0')
    have_error = 1;

 out:
  iconv_close (cd);

  if (have_error)
    {
      free (dest);
      dest = NULL;
    }

  return dest;
}
Пример #16
0
int 
XConvertEucTwToUtf8(char* buffer_return, int len) {
  /* FIXME */
#if HAVE_LIBC_ICONV
  iconv_t cd;
  int cdl;
#else
  int i = 0;
#endif
  int l = 0;
  char *buf, *b;

  if (len < 1) return 0;
  b = buf = (char*) malloc((unsigned)len);
  memcpy(buf, buffer_return, (unsigned) len);

#if HAVE_LIBC_ICONV
  l = cdl = len;
  cd = iconv_open("EUC-TW", "UTF-8");
  iconv(cd, &b, &len, &buffer_return, &cdl);
  iconv_close(cd);
  l -= cdl;	
#else
  while (i < len) {
    unsigned int ucs;
    unsigned char c; 
    c = (unsigned char) buf[i];
    if (c < 0x80) {
      ucs = c;	
      i++;
    } else if (c >= 0xa1 && c < 0xff && len - i > 1 ) {
      unsigned char b[2];
      b[0] = (unsigned char) c - 0x80;
      b[1] = (unsigned char) buf[i + 1] - 0x80;
      ucs = ' '; i += 2;
    } else if (c == 0x8e &&  len - i > 3) {
      unsigned char b[2];
      unsigned char c1 =  buf[i + 1];
      unsigned char c2 =  buf[i + 2];
      unsigned char c3 =  buf[i + 3];
      b[0] = (unsigned char)  buf[i + 2] - 0x80;
      b[1] = (unsigned char)  buf[i + 3] - 0x80;
      if (c1 >= 0xa1 && c1 <= 0xb0) {
	if (c2 >= 0xa1 && c2 < 0xff && c3 >= 0xa1 && c3 < 0xff) {
	  ucs = ' '; i += 4;
	} else {
	  ucs = '?'; i++;
	}
      } else {
	ucs = '?'; i++;
      }
    } else {
      ucs = '?';
      i++;
    }
    l += XConvertUcsToUtf8(ucs, buffer_return + l);
  }
#endif
  free(buf);
  return l;
}
Пример #17
0
static char *
convert_ccsid(LIBSSH2_SESSION *session, libssh2_string_cache **cache,
              unsigned short outccsid, unsigned short inccsid,
              const char *instring, ssize_t inlen, size_t *outlen)
{
    char *inp;
    char *outp;
    size_t olen;
    size_t ilen;
    size_t buflen;
    size_t curlen;
    ssize_t termsize;
    int i;
    char *dst;
    libssh2_string_cache *outstring;
    QtqCode_T incode;
    QtqCode_T outcode;
    iconv_t cd;

    if (!instring) {
        if (outlen)
            *outlen = 0;
        return NULL;
    }
    if (outlen)
        *outlen = -1;
    if (!session || !cache)
        return NULL;

    /* Get terminator size. */
    termsize = terminator_size(outccsid);
    if (termsize < 0)
        return NULL;
 
    /* Prepare conversion parameters. */
    memset((void *) &incode, 0, sizeof incode);
    memset((void *) &outcode, 0, sizeof outcode);
    incode.CCSID = inccsid;
    outcode.CCSID = outccsid;
    curlen = OFFSET_OF(libssh2_string_cache, string);
    inp = (char *) instring;
    ilen = inlen;
    buflen = inlen + curlen;
    if (inlen < 0) {
        incode.length_option = 1;
        buflen = STRING_GRANULE;
        ilen = 0;
    }

    /* Allocate output string buffer and open conversion descriptor. */
    dst = LIBSSH2_ALLOC(session, buflen + termsize);
    if (!dst)
        return NULL;
    cd = QtqIconvOpen(&outcode, &incode);
    if (cd.return_value == -1) {
        LIBSSH2_FREE(session, (char *) dst);
        return NULL;
    }

    /* Convert string. */
    for (;;) {
        outp = dst + curlen;
        olen = buflen - curlen;
        i = iconv(cd, &inp, &ilen, &outp, &olen);
        if (inlen < 0 && olen == buflen - curlen) {
            /* Special case: converted 0-length (sub)strings do not store the
               terminator. */
            if (termsize) {
                memset(outp, 0, termsize);
                olen -= termsize;
            }
        }
        curlen = buflen - olen;
        if (i >= 0 || errno != E2BIG)
            break;
        /* Must expand buffer. */
        buflen += STRING_GRANULE;
        outp = LIBSSH2_REALLOC(session, dst, buflen + termsize);
        if (!outp)
            break;
        dst = outp;
    }

    iconv_close(cd);

    /* Check for error. */
    if (i < 0 || !outp) {
        LIBSSH2_FREE(session, dst);
        return NULL;
    }

    /* Process terminator. */
    if (inlen < 0)
        curlen -= termsize;
    else if (termsize)
        memset(dst + curlen, 0, termsize);

    /* Shorten buffer if possible. */
    if (curlen < buflen)
        dst = LIBSSH2_REALLOC(session, dst, curlen + termsize);

    /* Link to cache. */
    outstring = (libssh2_string_cache *) dst;
    outstring->next = *cache;
    *cache = outstring;

    /* Return length if required. */
    if (outlen)
        *outlen = curlen - OFFSET_OF(libssh2_string_cache, string);

    return outstring->string;
}
Пример #18
0
bool IsConversionSupported(const char *src, const char *dst) {
	iconv_t cd = iconv_open(dst, src);
	bool supported = cd != iconv_invalid;
	iconv_close(cd);
	return supported;
}
Пример #19
0
int
main (void)
{
  size_t count = TEST_ROUNDS;
  int result = 0;

  mtrace ();

  /* Just a seed.  */
  srandom (TEST_ROUNDS);

  while (count--)
    {
      int idx = random () % nmodules;

      if (modules[idx].state == unloaded)
	{
	  char outbuf[10000];
	  char *inptr = (char *) inbuf;
	  size_t insize = sizeof (inbuf) - 1;
	  char *outptr = outbuf;
	  size_t outsize = sizeof (outbuf);

	  /* Load the module and do the conversion.  */
	  modules[idx].cd = iconv_open ("UTF-8", modules[idx].name);

	  if (modules[idx].cd == (iconv_t) -1)
	    {
	      printf ("opening of %s failed: %m\n", modules[idx].name);
	      result = 1;
	      break;
	    }

	  modules[idx].state = loaded;

	  /* Now a simple test.  */
	  if (iconv (modules[idx].cd, &inptr, &insize, &outptr, &outsize) != 0
	      || *inptr != '\0')
	    {
	      printf ("conversion with %s failed\n", modules[idx].name);
	      result = 1;
	    }
	}
      else
	{
	  /* Unload the module.  */
	  if (iconv_close (modules[idx].cd) != 0)
	    {
	      printf ("closing of %s failed: %m\n", modules[idx].name);
	      result = 1;
	      break;
	    }

	  modules[idx].state = unloaded;
	}
    }

  for (count = 0; count < nmodules; ++count)
    if (modules[count].state == loaded && iconv_close (modules[count].cd) != 0)
      {
	printf ("closing of %s failed: %m\n", modules[count].name);
	result = 1;
      }

  return result;
}
Пример #20
0
int main( int i_argc, char **pp_argv )
{
    const char *psz_network_name = "DVBlast - http://www.videolan.org/projects/dvblast.html";
    char *p_network_name_tmp = NULL;
    size_t i_network_name_tmp_size;
    char *psz_dup_config = NULL;
    mtime_t i_poll_timeout = MAX_POLL_TIMEOUT;
    struct sched_param param;
    int i_error;
    int c;
    struct sigaction sa;
    sigset_t set;

    int b_enable_syslog = 0;

    if ( i_argc == 1 )
        usage();

    /*
     * The only short options left are: ky0123456789
     * Use them wisely.
     */
    static const struct option long_options[] =
    {
        { "config-file",     required_argument, NULL, 'c' },
        { "remote-socket",   required_argument, NULL, 'r' },
        { "ttl",             required_argument, NULL, 't' },
        { "rtp-output",      required_argument, NULL, 'o' },
        { "priority",        required_argument, NULL, 'i' },
        { "adapter",         required_argument, NULL, 'a' },
        { "frontend-number", required_argument, NULL, 'n' },
        { "frequency",       required_argument, NULL, 'f' },
        { "fec-inner",       required_argument, NULL, 'F' },
        { "rolloff",         required_argument, NULL, 'R' },
        { "symbol-rate",     required_argument, NULL, 's' },
        { "diseqc",          required_argument, NULL, 'S' },
        { "voltage",         required_argument, NULL, 'v' },
        { "force-pulse",     no_argument,       NULL, 'p' },
        { "bandwidth",       required_argument, NULL, 'b' },
        { "inversion",       required_argument, NULL, 'I' },
        { "modulation",      required_argument, NULL, 'm' },
        { "pilot",           required_argument, NULL, 'P' },
        { "fec-lp",          required_argument, NULL, 'K' },
        { "guard",           required_argument, NULL, 'G' },
        { "hierarchy",       required_argument, NULL, 'H' },
        { "transmission",    required_argument, NULL, 'X' },
        { "lock-timeout",    required_argument, NULL, 'O' },
        { "budget-mode",     no_argument,       NULL, 'u' },
        { "select-pmts",     no_argument,       NULL, 'w' },
        { "udp",             no_argument,       NULL, 'U' },
        { "unique-ts-id",    no_argument,       NULL, 'T' },
        { "latency",         required_argument, NULL, 'L' },
        { "retention",       required_argument, NULL, 'E' },
        { "duplicate",       required_argument, NULL, 'd' },
        { "rtp-input",       required_argument, NULL, 'D' },
        { "asi-adapter",     required_argument, NULL, 'A' },
        { "any-type",        no_argument,       NULL, 'z' },
        { "dvb-compliance",  no_argument,       NULL, 'C' },
        { "emm-passthrough", no_argument,       NULL, 'W' },
        { "ecm-passthrough", no_argument,       NULL, 'Y' },
        { "epg-passthrough", no_argument,       NULL, 'e' },
        { "network-name",    no_argument,       NULL, 'M' },
        { "network-id",      no_argument,       NULL, 'N' },
        { "system-charset",  required_argument, NULL, 'j' },
        { "dvb-charset",     required_argument, NULL, 'J' },
        { "provider-name",   required_argument, NULL, 'B' },
        { "logger",          no_argument,       NULL, 'l' },
        { "logger-ident",    required_argument, NULL, 'g' },
        { "print",           required_argument, NULL, 'x' },
        { "quit-timeout",    required_argument, NULL, 'Q' },
        { "quiet",           no_argument,       NULL, 'q' },
        { "help",            no_argument,       NULL, 'h' },
        { "version",         no_argument,       NULL, 'V' },
        { "mrtg-file",       required_argument, NULL, 'Z' },
        { 0, 0, 0, 0 }
    };

    while ( (c = getopt_long(i_argc, pp_argv, "q::c:r:t:o:i:a:n:f:F:R:s:S:v:pb:I:m:P:K:G:H:X:O:uwUTL:E:d:D:A:lg:zCWYeM:N:j:J:B:x:Q:hVZ:", long_options, NULL)) != -1 )
    {
        switch ( c )
        {
        case 'q':
            if ( optarg )
            {
                if ( *optarg == 'q' )  /* e.g. -qqq */
                {
                    i_verbose--;
                    while ( *optarg == 'q' )
                    {
                        i_verbose--;
                        optarg++;
                    }
                }
                else
                {
                    i_verbose -= atoi( optarg );  /* e.g. -q2 */
                }
            }
            else
            {
                i_verbose--;  /* -q */
            }
            break;

        case 'c':
            psz_conf_file = optarg;
            break;

        case 'r':
            psz_srv_socket = optarg;
            break;

        case 't':
            i_ttl_global = strtol( optarg, NULL, 0 );
            break;

        case 'o':
        {
            struct in_addr maddr;
            if ( !inet_aton( optarg, &maddr ) )
                usage();
            memcpy( pi_ssrc_global, &maddr.s_addr, 4 * sizeof(uint8_t) );
            break;
        }

        case 'i':
            i_priority = strtol( optarg, NULL, 0 );
            break;

        case 'a':
            i_adapter = strtol( optarg, NULL, 0 );
            break;

        case 'n':
            i_fenum = strtol( optarg, NULL, 0 );
            break;

        case 'f':
            i_frequency = strtol( optarg, NULL, 0 );
            if ( pf_Open != NULL )
                usage();
            pf_Open = dvb_Open;
            pf_Read = dvb_Read;
            pf_Reset = dvb_Reset;
            pf_SetFilter = dvb_SetFilter;
            pf_UnsetFilter = dvb_UnsetFilter;
            break;

        case 'F':
            i_fec = strtol( optarg, NULL, 0 );
            break;

        case 'R':
            i_rolloff = strtol( optarg, NULL, 0 );
            break;

        case 's':
            i_srate = strtol( optarg, NULL, 0 );
            break;

        case 'S':
            i_satnum = strtol( optarg, NULL, 16 );
            break;

        case 'v':
            i_voltage = strtol( optarg, NULL, 0 );
            break;

        case 'p':
            b_tone = 1;
            break;

        case 'b':
            i_bandwidth = strtol( optarg, NULL, 0 );
            break;

        case 'I':
            i_inversion = strtol( optarg, NULL, 0 );
            break;

        case 'm':
            psz_modulation = optarg;
            break;

        case 'P':
            i_pilot = strtol( optarg, NULL, 0 );
            break;

        case 'K':
            i_fec_lp = strtol( optarg, NULL, 0 );
            break;

        case 'G':
            i_guard = strtol( optarg, NULL, 0 );
            break;

        case 'X':
            i_transmission = strtol( optarg, NULL, 0 );
            break;

        case 'O':
            i_frontend_timeout_duration = strtoll( optarg, NULL, 0 ) * 1000;
            break;

        case 'H':
            i_hierarchy = strtol( optarg, NULL, 0 );
            break;

        case 'u':
            b_budget_mode = 1;
            break;

        case 'w':
            b_select_pmts = 1;
            break;

        case 'U':
            b_udp_global = true;
            break;

        case 'L':
            i_latency_global = strtoll( optarg, NULL, 0 ) * 1000;
            break;

        case 'E':
            i_retention_global = strtoll( optarg, NULL, 0 ) * 1000;
            break;

        case 'd':
            psz_dup_config = optarg;
            break;

        case 'D':
            psz_udp_src = optarg;
            if ( pf_Open != NULL )
                usage();
            pf_Open = udp_Open;
            pf_Read = udp_Read;
            pf_Reset = udp_Reset;
            pf_SetFilter = udp_SetFilter;
            pf_UnsetFilter = udp_UnsetFilter;
            break;

        case 'A':
            i_asi_adapter = strtol( optarg, NULL, 0 );
            if ( pf_Open != NULL )
                usage();
            pf_Open = asi_Open;
            pf_Read = asi_Read;
            pf_Reset = asi_Reset;
            pf_SetFilter = asi_SetFilter;
            pf_UnsetFilter = asi_UnsetFilter;
            break;

        case 'z':
            b_any_type = 1;
            break;

        case 'C':
            b_dvb_global = true;
            break;

        case 'W':
            b_enable_emm = true;
            break;

        case 'Y':
            b_enable_ecm = true;
            b_dvb_global = true;
            break;
 
        case 'e':
            b_epg_global = true;
            break;

        case 'M':
            psz_network_name = optarg;
            break;

        case 'N':
            i_network_id = strtoul( optarg, NULL, 0 );
            break;

        case 'T':
            b_random_tsid = 1;
            break;

        case 'j':
            psz_native_charset = optarg;
            break;

        case 'J':
            psz_dvb_charset = optarg;
            break;

        case 'B':
            psz_provider_name = optarg;
            break;

        case 'l':
            b_enable_syslog = 1;
            break;

        case 'g':
            psz_syslog_ident = optarg;
            break;

        case 'x':
            if ( !strcmp(optarg, "text") )
                i_print_type = PRINT_TEXT;
            else if ( !strcmp(optarg, "xml") )
                i_print_type = PRINT_XML;
            else
                msg_Warn( NULL, "unrecognized print type %s", optarg );
            /* Make stdout line-buffered */
            setvbuf(stdout, NULL, _IOLBF, 0);
            break;

        case 'Q':
            i_quit_timeout_duration = strtoll( optarg, NULL, 0 ) * 1000;
            break;

        case 'V':
            DisplayVersion();
            exit(0);
            break;

        case 'Z':
            psz_mrtg_file = optarg;
            break;

        case 'h':
        default:
            usage();
        }
    }
    if ( optind < i_argc || pf_Open == NULL )
        usage();

    if ( b_enable_syslog )
        msg_Connect( psz_syslog_ident ? psz_syslog_ident : pp_argv[0] );

    if ( i_verbose )
        DisplayVersion();

    msg_Warn( NULL, "restarting" );
    switch (i_print_type) 
    {
        case PRINT_XML:
            printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            printf("<TS>\n");
            break;
        default:
            break;
    }

    if ( b_udp_global )
    {
        msg_Warn( NULL, "raw UDP output is deprecated.  Please consider using RTP." );
        msg_Warn( NULL, "for DVB-IP compliance you should use RTP." );
    }

    if ( b_epg_global && !b_dvb_global )
    {
        msg_Dbg( NULL, "turning on DVB compliance, required by EPG information" );
        b_dvb_global = true;
    }

    memset( &output_dup, 0, sizeof(output_dup) );
    if ( psz_dup_config != NULL )
    {
        output_config_t config;

        config_Defaults( &config );
        if ( !config_ParseHost( &config, psz_dup_config ) )
            msg_Err( NULL, "Invalid target address for -d switch" );
        else
        {
            output_Init( &output_dup, &config );
            output_Change( &output_dup, &config );
        }

        config_Free( &config );
    }

    if ( strcasecmp( psz_native_charset, psz_dvb_charset ) )
    {
#ifdef HAVE_ICONV
        iconv_t iconv_system = iconv_open( psz_dvb_charset,
                                           psz_native_charset );
        if ( iconv_system != (iconv_t)-1 )
        {
            size_t i = strlen( psz_network_name );
            char *p, *psz_string;
            i_network_name_tmp_size = i * 6;
            p = psz_string = malloc(i_network_name_tmp_size);
            if ( iconv( iconv_system, (char **)&psz_network_name, &i, &p,
                        &i_network_name_tmp_size ) == -1 )
                free( psz_string );
            else
            {
                p_network_name_tmp = psz_string;
                i_network_name_tmp_size = p - psz_string;
            }
            iconv_close( iconv_system );
        }
#else
        msg_Warn( NULL,
                  "unable to convert from %s to %s (iconv is not available)",
                  psz_native_charset, psz_dvb_charset );
#endif
    }
    if ( p_network_name_tmp == NULL )
    {
        p_network_name_tmp = strdup(psz_network_name);
        i_network_name_tmp_size = strlen(psz_network_name);
    }
    p_network_name = dvb_string_set( (uint8_t *)p_network_name_tmp,
                                     i_network_name_tmp_size, psz_dvb_charset,
                                     &i_network_name_size );
    free( p_network_name_tmp );

    /* Set signal handlers */
    memset( &sa, 0, sizeof(struct sigaction) );
    sa.sa_handler = SigHandler;
    sigfillset( &set );

    if ( sigaction( SIGHUP, &sa, NULL ) == -1 || sigaction( SIGINT, &sa, NULL ) == -1 )
    {
        msg_Err( NULL, "couldn't set signal handler: %s", strerror(errno) );
        exit(EXIT_FAILURE);
    }

    srand( time(NULL) * getpid() );

    demux_Open();

    // init the mrtg logfile
    mrtgInit(psz_mrtg_file);

    if ( i_priority > 0 )
    {
        memset( &param, 0, sizeof(struct sched_param) );
        param.sched_priority = i_priority;
        if ( (i_error = pthread_setschedparam( pthread_self(), SCHED_RR,
                                               &param )) )
        {
            msg_Warn( NULL, "couldn't set thread priority: %s",
                      strerror(i_error) );
        }
    }

    config_ReadFile( psz_conf_file );

    if ( psz_srv_socket != NULL )
        comm_Open();

    for ( ; ; )
    {
        block_t *p_ts;

        if ( b_exit_now )
        {
            msg_Info( NULL, "Shutdown was requested." );
            break;
        }

        if ( b_conf_reload )
        {
            b_conf_reload = 0;
            msg_Info( NULL, "Configuration reload was requested." );
            config_ReadFile( psz_conf_file );
        }

        if ( i_quit_timeout && i_quit_timeout <= i_wallclock )
        {
            switch (i_print_type)
            {
            case PRINT_XML:
                printf("</TS>\n");
                break;
            default:
                break;
            }
            exit(EXIT_SUCCESS);
        }

        p_ts = pf_Read( i_poll_timeout );
        if ( p_ts != NULL )
        {
            mrtgAnalyse(p_ts);
            demux_Run( p_ts );
        }
        i_poll_timeout = output_Send();
        if ( i_poll_timeout == -1 || i_poll_timeout > MAX_POLL_TIMEOUT )
            i_poll_timeout = MAX_POLL_TIMEOUT;
    }

    mrtgClose();
    outputs_Close( i_nb_outputs );
    demux_Close();
    free( p_network_name );

    if ( b_enable_syslog )
        msg_Disconnect();

    if ( psz_srv_socket && i_comm_fd > -1 )
        unlink( psz_srv_socket );

    return EXIT_SUCCESS;
}
int
main (void)
{
  char name[] = "/tmp/widetext.out.XXXXXX";
  char mbbuf[SIZE];
  char mb2buf[SIZE];
  wchar_t wcbuf[SIZE];
  wchar_t wc2buf[SIZE];
  size_t mbsize;
  size_t wcsize;
  int fd;
  FILE *fp;
  size_t n;
  int res;
  int status = 0;
  wchar_t *wcp;

  setlocale (LC_ALL, "de_DE.UTF-8");
  printf ("locale used: %s\n\n", setlocale (LC_ALL, NULL));

  /* Read the file into memory.  */
  mbsize = fread (mbbuf, 1, SIZE, stdin);
  if (mbsize == 0)
    {
      printf ("%u: cannot read input file from standard input: %m\n",
	      __LINE__);
      exit (1);
    }

   printf ("INFO: input file has %Zd bytes\n", mbsize);

  /* First convert the text to wide characters.  We use iconv here.  */
  {
    iconv_t cd;
    char *inbuf = mbbuf;
    size_t inleft = mbsize;
    char *outbuf = (char *) wcbuf;
    size_t outleft = sizeof (wcbuf);
    size_t nonr;

    cd = iconv_open ("WCHAR_T", "UTF-8");
    if (cd == (iconv_t) -1)
      {
	printf ("%u: cannot get iconv descriptor for conversion to UCS4\n",
		__LINE__);
	exit (1);
      }

    /* We must need only one call and there must be no losses.  */
    nonr = iconv (cd, &inbuf, &inleft, &outbuf, &outleft);
    if (nonr != 0 && nonr != (size_t) -1)
      {
	printf ("%u: iconv performed %Zd nonreversible conversions\n",
		__LINE__, nonr);
	exit (1);
      }

    if  (nonr == (size_t) -1)
      {
	printf ("\
%u: iconv returned with %Zd and errno = %m (inleft: %Zd, outleft: %Zd)\n",
		__LINE__, nonr, inleft, outleft);
	exit (1);
      }

    if (inleft != 0)
      {
	printf ("%u: iconv didn't convert all input\n", __LINE__);
	exit (1);
      }

    iconv_close (cd);

    if ((sizeof (wcbuf) - outleft) % sizeof (wchar_t) != 0)
      {
	printf ("%u: iconv converted not complete wchar_t\n", __LINE__);
	exit (1);
      }

    wcsize = (sizeof (wcbuf) - outleft) / sizeof (wchar_t);
    assert (wcsize + 1 <= SIZE);
  }
Пример #22
0
int main(int argc, char *argv[])
{
	char *argv0 = argv[0];
	SDL_Surface *screen;
	TTF_Font *font;
	SDL_Surface *text, *temp;
	int ptsize;
	int i, done;
	int rdiff, gdiff, bdiff;
	SDL_Color colors[NUM_COLORS];
	SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
	SDL_Color black = { 0x00, 0x00, 0x00, 0 };
	SDL_Color *forecol;
	SDL_Color *backcol;
	SDL_Rect dstrect;
	SDL_Event event;
	int rendersolid;
	int renderstyle;
	int dump;
	enum {
		RENDER_LATIN1,
		RENDER_UTF8,
		RENDER_UNICODE
	} rendertype;
	char *message, string[128];

	/* Look for special execution mode */
	dump = 0;
	/* Look for special rendering types */
	rendersolid = 0;
	renderstyle = TTF_STYLE_NORMAL;
	rendertype = RENDER_LATIN1;
	/* Default is black and white */
	forecol = &black;
	backcol = &white;
	for ( i=1; argv[i] && argv[i][0] == '-'; ++i ) {
		if ( strcmp(argv[i], "-solid") == 0 ) {
			rendersolid = 1;
		} else
		if ( strcmp(argv[i], "-utf8") == 0 ) {
			rendertype = RENDER_UTF8;
		} else
		if ( strcmp(argv[i], "-unicode") == 0 ) {
			rendertype = RENDER_UNICODE;
		} else
		if ( strcmp(argv[i], "-b") == 0 ) {
			renderstyle |= TTF_STYLE_BOLD;
		} else
		if ( strcmp(argv[i], "-i") == 0 ) {
			renderstyle |= TTF_STYLE_ITALIC;
		} else
		if ( strcmp(argv[i], "-u") == 0 ) {
			renderstyle |= TTF_STYLE_UNDERLINE;
		} else
		if ( strcmp(argv[i], "-dump") == 0 ) {
			dump = 1;
		} else
		if ( strcmp(argv[i], "-fgcol") == 0 ) {
			int r, g, b;
			if ( sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3 ) {
				fprintf(stderr, Usage, argv0);
				return(1);
			}
			forecol->r = (Uint8)r;
			forecol->g = (Uint8)g;
			forecol->b = (Uint8)b;
		} else
		if ( strcmp(argv[i], "-bgcol") == 0 ) {
			int r, g, b;
			if ( sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3 ) {
				fprintf(stderr, Usage, argv0);
				return(1);
			}
			backcol->r = (Uint8)r;
			backcol->g = (Uint8)g;
			backcol->b = (Uint8)b;
		} else {
			fprintf(stderr, Usage, argv0);
			return(1);
		}
	}
	argv += i;
	argc -= i;

	/* Check usage */
	if ( ! argv[0] ) {
		fprintf(stderr, Usage, argv0);
		return(1);
	}

	/* Initialize SDL */
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
		return(2);
	}

	/* Initialize the TTF library */
	if ( TTF_Init() < 0 ) {
		fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError());
		SDL_Quit();
		return(2);
	}

	/* Open the font file with the requested point size */
	ptsize = 0;
	if ( argc > 1 ) {
		ptsize = atoi(argv[1]);
	}
	if ( ptsize == 0 ) {
		i = 2;
		ptsize = DEFAULT_PTSIZE;
	} else {
		i = 3;
	}
	font = TTF_OpenFont(argv[0], ptsize);
	if ( font == NULL ) {
		fprintf(stderr, "Couldn't load %d pt font from %s: %s\n",
					ptsize, argv[0], SDL_GetError());
		cleanup(2);
	}
	TTF_SetFontStyle(font, renderstyle);

	if( dump ) {
		for( i = 48; i < 123; i++ ) {
			SDL_Surface* glyph = NULL;

			glyph = TTF_RenderGlyph_Shaded( font, i, *forecol, *backcol );

			if( glyph ) {
				char outname[64];
				sprintf( outname, "glyph-%d.bmp", i );
				SDL_SaveBMP( glyph, outname );
			}

		}
		cleanup(0);
	}

	/* Set a 640x480x8 video mode */
	screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
	if ( screen == NULL ) {
		fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",
							SDL_GetError());
		cleanup(2);
	}

	/* Set a palette that is good for the foreground colored text */
	rdiff = backcol->r - forecol->r;
	gdiff = backcol->g - forecol->g;
	bdiff = backcol->b - forecol->b;
	for ( i=0; i<NUM_COLORS; ++i ) {
		colors[i].r = forecol->r + (i*rdiff)/4;
		colors[i].g = forecol->g + (i*gdiff)/4;
		colors[i].b = forecol->b + (i*bdiff)/4;
	}
	SDL_SetColors(screen, colors, 0, NUM_COLORS);

	/* Clear the background to background color */
	SDL_FillRect(screen, NULL,
			SDL_MapRGB(screen->format, backcol->r, backcol->g, backcol->b));
	SDL_UpdateRect(screen, 0, 0, 0, 0);

	/* Show which font file we're looking at */
	sprintf(string, "Font file: %s", argv[0]);  /* possible overflow */
	if ( rendersolid ) {
		text = TTF_RenderText_Solid(font, string, *forecol);
	} else {
		text = TTF_RenderText_Shaded(font, string, *forecol, *backcol);
	}
	if ( text != NULL ) {
		dstrect.x = 4;
		dstrect.y = 4;
		dstrect.w = text->w;
		dstrect.h = text->h;
		SDL_BlitSurface(text, NULL, screen, &dstrect);
		SDL_FreeSurface(text);
	}
	
	/* Render and center the message */
	if ( argc > 2 ) {
		message = argv[2];
	} else {
		message = DEFAULT_TEXT;
	}
	switch (rendertype) {
	    case RENDER_LATIN1:
		if ( rendersolid ) {
			text = TTF_RenderText_Solid(font,message,*forecol);
		} else {
			text = TTF_RenderText_Shaded(font,message,*forecol,*backcol);
		}
		break;

	    case RENDER_UTF8:
		if ( rendersolid ) {
			text = TTF_RenderUTF8_Solid(font,message,*forecol);
		} else {
			text = TTF_RenderUTF8_Shaded(font,message,*forecol,*backcol);
		}
		break;

	    case RENDER_UNICODE:
		{
			Uint16 unicode_text[BUFSIZ];
			int index;
#ifdef HAVE_ICONV
			/* Use iconv to convert the message into utf-16.
			 * "char" and "" are aliases for the local 8-bit encoding */
			iconv_t cd;
			/*ICONV_CONST*/ char *from_str = message;
			char *to_str = (char*)unicode_text;
			size_t from_sz = strlen(message) + 1;
			size_t to_sz = sizeof(unicode_text);
			size_t res;
			int i;

			if ((cd = iconv_open("UTF-16", "char")) == (iconv_t)-1
			    && (cd = iconv_open("UTF-16", "")) == (iconv_t)-1) {
				perror("Couldn't open iconv");
				exit(1);
			}

			res = iconv(cd, &from_str, &from_sz, &to_str, &to_sz);
			if (res == -1) {
				perror("Couldn't use iconv");
				exit(1);
			}

			iconv_close(cd);
#else
			/* Convert the message from ascii into utf-16.
			 * This is unreliable as a test because it always
			 * gives the local ordering. */
			for (index = 0; message[index]; index++) {
				unicode_text[index] = message[index];
			}
			unicode_text[index] = 0;
#endif

			if ( rendersolid ) {
				text = TTF_RenderUNICODE_Solid(font,
					unicode_text, *forecol);
			} else {
				text = TTF_RenderUNICODE_Shaded(font,
					unicode_text, *forecol, *backcol);
			}
		}
		break;
	    default:
		text = NULL; /* This shouldn't happen */
		break;
	}
	if ( text == NULL ) {
		fprintf(stderr, "Couldn't render text: %s\n", SDL_GetError());
		TTF_CloseFont(font);
		cleanup(2);
	}
	dstrect.x = (screen->w - text->w)/2;
	dstrect.y = (screen->h - text->h)/2;
	dstrect.w = text->w;
	dstrect.h = text->h;
	printf("Font is generally %d big, and string is %hd big\n",
						TTF_FontHeight(font), text->h);

	/* Blit the text surface */
	if ( SDL_BlitSurface(text, NULL, screen, &dstrect) < 0 ) {
		fprintf(stderr, "Couldn't blit text to display: %s\n", 
								SDL_GetError());
		TTF_CloseFont(font);
		cleanup(2);
	}
	SDL_UpdateRect(screen, 0, 0, 0, 0);

	/* Set the text colorkey and convert to display format */
	if ( SDL_SetColorKey(text, SDL_SRCCOLORKEY|SDL_RLEACCEL, 0) < 0 ) {
		fprintf(stderr, "Warning: Couldn't set text colorkey: %s\n",
								SDL_GetError());
	}
	temp = SDL_DisplayFormat(text);
	if ( temp != NULL ) {
		SDL_FreeSurface(text);
		text = temp;
	}

	/* Wait for a keystroke, and blit text on mouse press */
	done = 0;
	while ( ! done ) {
		if ( SDL_WaitEvent(&event) < 0 ) {
			fprintf(stderr, "SDL_PullEvent() error: %s\n",
								SDL_GetError());
			done = 1;
			continue;
		}
		switch (event.type) {
			case SDL_MOUSEBUTTONDOWN:
				dstrect.x = event.button.x - text->w/2;
				dstrect.y = event.button.y - text->h/2;
				dstrect.w = text->w;
				dstrect.h = text->h;
				if ( SDL_BlitSurface(text, NULL, screen,
							&dstrect) == 0 ) {
					SDL_UpdateRects(screen, 1, &dstrect);
				} else {
					fprintf(stderr,
					"Couldn't blit text to display: %s\n", 
								SDL_GetError());
				}
				break;
				
			case SDL_KEYDOWN:
			case SDL_QUIT:
				done = 1;
				break;
			default:
				break;
		}
	}
	SDL_FreeSurface(text);
	TTF_CloseFont(font);
	cleanup(0);

	/* Not reached, but fixes compiler warnings */
	return 0;
}
Пример #23
0
CONF_LIST *create_conf_list()
{
   char buff[BUFSIZ], path[BUFSIZ];
   FILE *fp = NULL;
   CONF_LIST *list;
   XML_Parser p;
   expat_userdata userdata;
   int xml_error = XML_ERROR_NONE;

   if (get_config_dradio_menu_path(path, sizeof(path)) == -1)
      return NULL;

   if (write_config_inputconf() == -1)
      return NULL;

   if ((iconv_cd = iconv_open("ISO-8859-1", "UTF-8")) == (iconv_t) -1)
   {
      perror("iconv_open");
      return NULL;
   }

   p = XML_ParserCreate(NULL);
   if (!p) {
      fprintf(stderr, "Couldn't allocate memory for parser\n");
      return NULL;
   }

   list = conf_list_init();

   userdata.list = list;
   userdata.parser = p;
   XML_SetUserData(p, &userdata);

   XML_SetElementHandler(p, start_element_handler, end_element_handler);

   fp = fopen(path, "r");
   if (fp == NULL)
   {
      perror("fopen");
      return NULL;
   }

   for (;;) {
      int done;
      int len;

      len = (int)fread(buff, sizeof(char), sizeof(buff), fp);
      if (ferror(fp)) {
         perror("fread");
         return NULL;
      }
      done = feof(fp);

      if (XML_Parse(p, buff, len, done) == XML_STATUS_ERROR) {

         xml_error = XML_GetErrorCode(p);

         switch (xml_error)
         {
          case XML_ERROR_ABORTED:
            ; /* This is a XML_StopParser event, error message has already written */
            break;
          default:
            fprintf(stderr, "Parse error in '%s' at line %lu column %lu: %s\n",
                    path,
                    XML_GetCurrentLineNumber(p),
                    XML_GetCurrentColumnNumber(p),
                    XML_ErrorString(xml_error));
            break;
         }
         break;
      }

      if (done)
         break;
   }

   XML_ParserFree(p);
   fclose(fp);

   iconv_close(iconv_cd);

   if (xml_error != XML_ERROR_NONE)
   {
      conf_list_free(list);
      return NULL;
   }

   return list;
}
Пример #24
0
static rc_uint_type
wind_WideCharToMultiByte (rc_uint_type cp, const unichar *u, char *mb, rc_uint_type mb_len)
{
  rc_uint_type ret = 0;
#if defined (_WIN32) || defined (__CYGWIN__)
  WINBOOL used_def = FALSE;

  ret = (rc_uint_type) WideCharToMultiByte (cp, 0, u, -1, mb, mb_len,
				      	    NULL, & used_def);
#elif defined (HAVE_ICONV_H)
  int first = 1;
  char tmp[32];
  char *p_tmp;
  const char *iconv_name = wind_iconv_cp (cp);

  if (!u || !iconv_name)
    return 0;
  iconv_t cd = iconv_open (iconv_name, "UTF-16");

  while (1)
    {
      int iret;
      const char *n_u;
      char *n_tmp;

      p_tmp = tmp;
      iret = iconv_onechar (cd, (const char *) u, p_tmp, 32, &n_u, & n_tmp);
      if (first)
	{
	  first = 0;
	  continue;
	}
      if (!iret)
	{
	  size_t l_tmp = (size_t) (n_tmp - p_tmp);

	  if (mb)
	    {
	      if ((size_t) mb_len < l_tmp)
		break;
	      memcpy (mb, tmp, l_tmp);
	      mb += l_tmp;
	      mb_len -= l_tmp;
	    }
	  ret += l_tmp;
	}
      else
	break;
      if (u[0] == 0)
	break;
      u = (const unichar *) n_u;
    }
  iconv_close (cd);
#else
  if (cp)
    ret = 0;

  while (u[ret] != 0)
    ++ret;

  ++ret;

  if (mb)
    {
      while (*u != 0 && mb_len != 0)
	{
	  if (u[0] == (u[0] & 0x7f))
	    *mb++ = (char) u[0];
	  else
	    *mb++ = '_';
	  ++u; --mb_len;
	}
      if (mb_len != 0)
	*mb = 0;
    }
#endif
  return ret;
}
Пример #25
0
// Convert unicode string to to_enc encoding
char* unicode_decode(const XLSBYTE *s, int len, int *newlen, const char* to_enc)
{
#if HAVE_ICONV
	// Do iconv conversion
    const char *from_enc = "UTF-16LE";
    char* outbuf = 0;
    
    if(s && len && from_enc && to_enc)
    {
        size_t outlenleft = len;
        int outlen = len;
        size_t inlenleft = len;
        iconv_t ic = iconv_open(to_enc, from_enc);
        char* src_ptr = (char*) s;
        char* out_ptr = 0;

        if(ic != (iconv_t)-1)
        {
            size_t st; 
            outbuf = (char*)malloc(outlen + 1);

			if(outbuf)
            {
                out_ptr = (char*)outbuf;
                while(inlenleft)
                {
                    st = iconv(ic, &src_ptr, &inlenleft, &out_ptr,(size_t *) &outlenleft);
                    if(st == (size_t)(-1))
                    {
                        if(errno == E2BIG)
                        {
                            int diff = out_ptr - outbuf;
                            outlen += inlenleft;
                            outlenleft += inlenleft;
                            outbuf = (char*)realloc(outbuf, outlen + 1);
                            if(!outbuf)
                            {
                                break;
                            }
                            out_ptr = outbuf + diff;
                        }
                        else
                        {
                            free(outbuf), outbuf = NULL;
                            break;
                        }
                    }
                }
            }
            iconv_close(ic);
        }
        outlen -= outlenleft;

        if (newlen)
        {
            *newlen = outbuf ? outlen : 0;
        }
        if(outbuf)
        {
            outbuf[outlen] = 0;
        }
    }
    return outbuf;
#else
	// Do wcstombs conversion
  (void)to_enc; // Avoid compiler warning "-Wunused-parameter
	char *converted = NULL;
	int count, count2;

	if (setlocale(LC_CTYPE, "") == NULL) {
		printf("setlocale failed: %d\n", errno);
    return (char*)"*null*";
	}

	count = wcstombs(NULL, (wchar_t*)s, 0);
	if (count <= 0) {
		if (newlen) *newlen = 0;
		return NULL;
	}

  converted = (char*)calloc(count+1, sizeof(char));
	count2 = wcstombs(converted, (wchar_t*)s, count+1);
	if (count2 <= 0) {
		printf("wcstombs failed (%d)\n", len);
		if (newlen) *newlen = 0;
		return converted;
	} else {
		if (newlen) *newlen = count2;
		return converted;
	}
#endif
}
IconvGNUTransService::IconvGNUTransService()
    : IconvGNUWrapper(), fUnicodeCP(0)
{
#if !defined(APP_NO_THREADS)
    // Create global lock object
    if (gIconvMutex == NULL) {
        gIconvMutex = new XMLMutex;
        if (gIconvMutex == NULL)
            XMLPlatformUtils::panic (PanicHandler::Panic_NoTransService);
        IconvGNUMutexCleanup.registerCleanup(reinitIconvGNUMutex);
    }
#endif

    // Try to obtain local (host) characterset from the setlocale
    // and through the environment. Do not call setlocale(LC_*, "")!
    // Using an empty string instead of NULL, will modify the libc
    // behavior.
    //
    char* fLocalCP = setlocale (LC_CTYPE, NULL);
    if (fLocalCP == NULL || *fLocalCP == 0 ||
        strcmp (fLocalCP, "C") == 0 ||
        strcmp (fLocalCP, "POSIX") == 0) {
      fLocalCP = getenv ("LC_ALL");
      if (fLocalCP == NULL) {
        fLocalCP = getenv ("LC_CTYPE");
        if (fLocalCP == NULL)
          fLocalCP = getenv ("LANG");
      }
    }

    if (fLocalCP == NULL || *fLocalCP == 0 ||
        strcmp (fLocalCP, "C") == 0 ||
        strcmp (fLocalCP, "POSIX") == 0)
        fLocalCP = "iso-8859-1";    // fallback locale
    else {
        char    *ptr = strchr (fLocalCP, '.');
        if (ptr == NULL)
            fLocalCP = "iso-8859-1";    // fallback locale
        else
            fLocalCP = ptr + 1;
    }

    // Select the native unicode characters encoding schema
    const IconvGNUEncoding    *eptr;
    // first - try to use the schema with character size, equil to XMLCh
    for (eptr = gIconvGNUEncodings; eptr->fSchema; eptr++) {
        if (eptr->fUChSize != sizeof(XMLCh))
            continue;
        ICONV_LOCK;
        // try to create conversion descriptor
        iconv_t    cd_to = iconv_open(fLocalCP, eptr->fSchema);
        if (cd_to == (iconv_t)-1)
            continue;
        iconv_t    cd_from = iconv_open(eptr->fSchema, fLocalCP);
        if (cd_to == (iconv_t)-1) {
            iconv_close (cd_to);
            continue;
        }
        // got it
        setUChSize(eptr->fUChSize);
        setUBO(eptr->fUBO);
        setCDTo(cd_to);
        setCDFrom(cd_from);
        fUnicodeCP = eptr->fSchema;
        break;
    }
    if (fUnicodeCP == NULL)
        // try to use any known schema
        for (eptr = gIconvGNUEncodings; eptr->fSchema; eptr++) {
            // try to create conversion descriptor
            ICONV_LOCK;
            iconv_t    cd_to = iconv_open(fLocalCP, eptr->fSchema);
            if (cd_to == (iconv_t)-1)
                continue;
            iconv_t    cd_from = iconv_open(eptr->fSchema, fLocalCP);
            if (cd_to == (iconv_t)-1) {
                iconv_close (cd_to);
                continue;
            }
            // got it
            setUChSize(eptr->fUChSize);
            setUBO(eptr->fUBO);
            setCDTo(cd_to);
            setCDFrom(cd_from);
            fUnicodeCP = eptr->fSchema;
            break;
        }

    if (fUnicodeCP == NULL || cdTo() == (iconv_t)-1 || cdFrom() == (iconv_t)-1)
        XMLPlatformUtils::panic (PanicHandler::Panic_NoTransService);
}
Пример #27
0
void play_debug_meta(char* filename){
  ID3_Tag tag(filename);

  ID3_Tag::Iterator* iter = tag.CreateIterator();
  ID3_Frame* frame = NULL;
  char buf[1000];

  // Iconv conversion descriptor: UTF-16 -> UTF-8
  iconv_t cd = iconv_open("UTF8","UTF16BE");

  while (NULL != (frame = iter->GetNext()))
  {
    char* val = NULL;
    field_text(&val, frame); 
    printf("Frame: type %d %s %s:\n", frame->GetID(), frame->GetTextID(), frame->GetDescription());
    ID3_Frame::Iterator* fieldIter = frame->CreateIterator();
    ID3_Field* field = NULL;

    while (NULL != (field = fieldIter->GetNext())) {
      printf("  Field: type ");
      ID3_FieldType type = field->GetType();
      switch(type) {
      case ID3FTY_NONE:
        printf("none"); break;
      case ID3FTY_INTEGER:
        printf("int"); break;
      case ID3FTY_BINARY:
        printf("binary"); break;
      case ID3FTY_TEXTSTRING:
        field->Get(buf, 1000);
        printf("text with %d items, encoding ", field->GetNumTextItems());
        switch(field->GetEncoding()){
        case ID3TE_UTF16: printf("UTF-16"); 

          if ( cd != (iconv_t) -1 ) {
            char* in = (char*) field->GetRawUnicodeText();
            size_t insize = field->Size();

            char* bufptr = buf;
            size_t bufsize = 1000;
            size_t rc = 0;

            // Initialize iconv state
            if( iconv(cd, NULL, NULL, &bufptr, &bufsize) == (size_t) -1 ){
              printf("iconv init Failed\n");
            }
            if ( (rc = iconv(cd, &in, &insize, &bufptr, &bufsize)) != (size_t) -1 ) {
              *bufptr = '\0';
              printf(": '%s' (%d chars)\n", buf, rc);
            } else {
              printf("<conversion using iconv failed>");
              perror("iconv");
            }
          }
          break;
        case ID3TE_UTF16BE: printf("UTF-16BE"); 
          printf(": '%s'", buf);
          break;
        case ID3TE_UTF8: printf("UTF-8");
          printf(": '%s'", buf);
          break;
        case ID3TE_NONE: printf("none");
          printf(": '%s'", buf);
          break;
        case ID3TE_ISO8859_1: printf("ISO-8859-1/ASCII");
          printf(": '%s'", buf);
          break;
        }
        break;
      }
      printf("\n");
    }
    delete fieldIter;
    delete [] val;
  }
  delete iter;

  iconv_close(cd);
}              
Пример #28
0
s32 _ConvertStr(s32 src_code, const void *src, s32 src_len, s32 dst_code, void *dst, s32 *dst_len, bool allowIncomplete)
{
	HostCode srcCode = 0, dstCode = 0;	//OEM code pages
	bool src_page_converted = _L10nCodeParse(src_code, srcCode);	//Check if code is in list.
	bool dst_page_converted = _L10nCodeParse(dst_code, dstCode);

	if (((!src_page_converted) && (srcCode == 0))
		|| ((!dst_page_converted) && (dstCode == 0)))
		return ConverterUnknown;

#ifdef _MSC_VER
	std::string wrapped_source = std::string(static_cast<const char *>(src), src_len);
	std::string target = _OemToOem(srcCode, dstCode, wrapped_source);

	if (dst != NULL)
	{
		if (target.length() > *dst_len) return DSTExhausted;
		memcpy(dst, target.c_str(), target.length());
	}
	*dst_len = target.length();

	return ConversionOK;
#else
	s32 retValue = ConversionOK;
	iconv_t ict = iconv_open(dstCode, srcCode);
	size_t srcLen = src_len;
	if (dst != NULL)
	{
		size_t dstLen = *dst_len;
		size_t ictd = iconv(ict, (char **)&src, &srcLen, (char **)&dst, &dstLen);
		*dst_len -= dstLen;
		if (ictd == -1)
		{
			if (errno == EILSEQ)
				retValue = SRCIllegal;  //Invalid multi-byte sequence
			else if (errno == E2BIG)
				retValue = DSTExhausted;//Not enough space
			else if (errno == EINVAL)
			{
				if (allowIncomplete)
					*dst_len = -1;  // TODO: correct value?
				else
					retValue = SRCIllegal;
			}
		}
	}
	else
	{
		*dst_len = 0;
		char buf[16];
		while (srcLen > 0)
		{
			char *bufPtr = buf;
			size_t bufLeft = sizeof(buf);
			size_t ictd = iconv(ict, (char **)&src, &srcLen, (char **)&bufPtr, &bufLeft);
			*dst_len += sizeof(buf) - bufLeft;
			if (ictd == -1 && errno != E2BIG)
			{
				if (errno == EILSEQ)
					retValue = SRCIllegal;
				else if (errno == EINVAL)
				{
					if (allowIncomplete)
						*dst_len = -1;  // TODO: correct value?
					else
						retValue = SRCIllegal;
				}
				break;
			}
		}
	}
	iconv_close(ict);
	return retValue;
#endif
}
Пример #29
0
Файл: conv.c Проект: fishman/nvi
int
conv_enc (SCR *sp, int option, char *enc)
{
#if defined(USE_WIDECHAR) && defined(USE_ICONV)
    iconv_t id;
    char2wchar_t    *c2w;
    wchar2char_t    *w2c;

    switch (option) {
    case O_FILEENCODING:
	c2w = &sp->conv.file2int;
	w2c = &sp->conv.int2file;
	break;
    case O_INPUTENCODING:
	c2w = &sp->conv.input2int;
	w2c = NULL;
	break;
    }

    if (!*enc) {
	if (c2w) *c2w = raw2int;
	if (w2c) *w2c = int2raw;
	return 0;
    }

    if (!strcmp(enc, "WCHAR_T")) {
	if (c2w) *c2w = CHAR_T_char2int;
	if (w2c) *w2c = CHAR_T_int2char;
	return 0;
    }

    id = iconv_open(enc, nl_langinfo(CODESET));
    if (id == (iconv_t)-1)
	goto err;
    iconv_close(id);
    id = iconv_open(nl_langinfo(CODESET), enc);
    if (id == (iconv_t)-1)
	goto err;
    iconv_close(id);

    switch (option) {
    case O_FILEENCODING:
	*c2w = fe_char2int;
	*w2c = fe_int2char;
	break;
    case O_INPUTENCODING:
	*c2w = ie_char2int;
	break;
    }

    F_CLR(sp, SC_CONV_ERROR);
    F_SET(sp, SC_SCR_REFORMAT);

    return 0;
err:
    switch (option) {
    case O_FILEENCODING:
	msgq(sp, M_ERR,
	    "321|File encoding conversion not supported");
	break;
    case O_INPUTENCODING:
	msgq(sp, M_ERR,
	    "322|Input encoding conversion not supported");
	break;
    }
#endif
    return 1;
}