Пример #1
0
bool
FileReadWrite::Next(BString& string)
{
	// Fill up the buffer with the first chunk of data
	if (fPositionInBuffer == 0)
		fAmtRead = fFile->Read(&fBuffer, sizeof(fBuffer));
	while (fAmtRead > 0) {
		while (fPositionInBuffer < fAmtRead) {
			// Return true if we hit a newline or the end of the file
			if (fBuffer[fPositionInBuffer] == '\n') {
				fPositionInBuffer++;
				// Convert begin
				int32 state = 0;
				int32 bufferLen = string.Length();
				int32 destBufferLen = bufferLen;
				char destination[destBufferLen];
				if (fSourceEncoding) {
					convert_to_utf8(fSourceEncoding, string.String(),
						&bufferLen, destination, &destBufferLen, &state);
				}
				string = destination;
				return true;
			}
			string += fBuffer[fPositionInBuffer]; 
			fPositionInBuffer++;
		} 

		// Once the buffer runs out, grab some more and start again
		fAmtRead = fFile->Read(&fBuffer, sizeof(fBuffer)); 
		fPositionInBuffer = 0;
	}	
	return false;
}
Пример #2
0
static int handle_commit_msg(char *line, unsigned linesize)
{
	static int still_looking = 1;
	char *endline = line + linesize;

	if (!cmitmsg)
		return 0;

	if (still_looking) {
		char *cp = line;
		if (isspace(*line)) {
			for (cp = line + 1; *cp; cp++) {
				if (!isspace(*cp))
					break;
			}
			if (!*cp)
				return 0;
		}
		if ((still_looking = check_header(cp, endline - cp, s_hdr_data, 0)) != 0)
			return 0;
	}

	/* normalize the log message to UTF-8. */
	if (metainfo_charset)
		convert_to_utf8(line, endline - line, charset);

	if (patchbreak(line)) {
		fclose(cmitmsg);
		cmitmsg = NULL;
		return 1;
	}

	fputs(line, cmitmsg);
	return 0;
}
Пример #3
0
inline T getpath() {
#if _WIN32
    // Handle Unicode, just remove if you don't want/need this. convert_to_utf8
    // uses WideCharToMultiByte in the Win32 API
    return convert_to_utf8( _wgetenv(L"PATH") );
#else
    return ::getenv("PATH");
#endif
}
Пример #4
0
void eb_update_status(eb_account *remote, const char *message)
{
	char *buff = convert_to_utf8(message);

	eb_chat_window_display_status(remote, buff);
	ay_conversation_display_status(remote, buff);

	g_free(buff);
}
Пример #5
0
static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
{
	assert(!mi->filter_stage);

	if (mi->header_stage) {
		if (!line->len || (line->len == 1 && line->buf[0] == '\n')) {
			if (mi->inbody_header_accum.len) {
				flush_inbody_header_accum(mi);
				mi->header_stage = 0;
			}
			return 0;
		}
	}

	if (mi->use_inbody_headers && mi->header_stage) {
		mi->header_stage = check_inbody_header(mi, line);
		if (mi->header_stage)
			return 0;
	} else
		/* Only trim the first (blank) line of the commit message
		 * when ignoring in-body headers.
		 */
		mi->header_stage = 0;

	/* normalize the log message to UTF-8. */
	if (convert_to_utf8(mi, line, mi->charset.buf))
		return 0; /* mi->input_error already set */

	if (mi->use_scissors && is_scissors_line(line->buf)) {
		int i;

		strbuf_setlen(&mi->log_message, 0);
		mi->header_stage = 1;

		/*
		 * We may have already read "secondary headers"; purge
		 * them to give ourselves a clean restart.
		 */
		for (i = 0; header[i]; i++) {
			if (mi->s_hdr_data[i])
				strbuf_release(mi->s_hdr_data[i]);
			mi->s_hdr_data[i] = NULL;
		}
		return 0;
	}

	if (patchbreak(line)) {
		if (mi->message_id)
			strbuf_addf(&mi->log_message,
				    "Message-Id: %s\n", mi->message_id);
		return 1;
	}

	strbuf_addbuf(&mi->log_message, line);
	return 0;
}
Пример #6
0
static void decode_header(struct strbuf *it)
{
	if (decode_header_bq(it))
		return;
	/* otherwise "it" is a straight copy of the input.
	 * This can be binary guck but there is no charset specified.
	 */
	if (metainfo_charset)
		convert_to_utf8(it, "");
}
Пример #7
0
static int handle_non_rfc2047_encoding(char **buffer,size_t *bufferLength,size_t *sourceLength)
{
	char *string = *buffer;
	int32 length = *sourceLength;
	int32 i;

	// check for 8-bit characters
	for (i = 0;i < length;i++)
		if (string[i] & 0x80)
			break;
	if (i == length)
		return false;

	// check for groups of 8-bit characters - this code is not very smart;
	// it just can detect some sort of single-byte encoded stuff, the rest
	// is regarded as UTF-8

	int32 singletons = 0,doubles = 0;

	for (i = 0;i < length;i++)
	{
		if (string[i] & 0x80)
		{
			if ((string[i + 1] & 0x80) == 0)
				singletons++;
			else doubles++;
			i++;
		}
	}

	if (singletons != 0)	// can't be valid UTF-8 anymore, so we assume ISO-Latin-1
	{
		int32 state = 0;
		// just to be sure
		int32 destLength = length * 4 + 1;
		int32 destBufferLength = destLength;
		char *dest = (char *)malloc(destLength);
		if (dest == NULL)
			return 0;

		if (convert_to_utf8(B_ISO1_CONVERSION,string,&length,dest,&destLength,&state) == B_OK)
		{
			free(*buffer);
			*buffer = dest;
			*bufferLength = destBufferLength;
			*sourceLength = destLength;
			return true;
		}
		free(dest);
		return false;
	}

	// we assume a valid UTF-8 string here, but yes, we don't check it
	return true;
}
Пример #8
0
static int os_getenv (lua_State *L) {
  #define MAX_VALUE_LEN 32767 /* according to MSDN */
  const char *name = luaL_checkstring(L, 1);
  wchar_t *utf16_name;
  wchar_t *utf16_value;
  char *utf8_value;
  int error, result;
  
  utf16_name = convert_to_utf16(name,&error);
  if(utf16_name == NULL){
    if(error == ERROR_COMMITMENT_LIMIT || error == ERROR_NOT_ENOUGH_MEMORY){
      return luaL_error(L, "not enough memory");
    } else {
      return luaL_error(L, "unable to convert %s to "
        "utf-16: error code = 0x%p", name, (void *)(ULONG_PTR)error);
    }
  }
  
  utf16_value = malloc(MAX_VALUE_LEN * sizeof(wchar_t));
  if(utf16_value == NULL){
    free(utf16_name);
    return luaL_error(L, "not enough memory");
  }
  
  result = GetEnvironmentVariableW(utf16_name,utf16_value,MAX_VALUE_LEN);
  error = GetLastError();
  free(utf16_name);
  if(result == 0){
    free(utf16_value);
    if(error == ERROR_ENVVAR_NOT_FOUND){
      lua_pushnil(L);
      return 1;
    }
    return luaL_error(L, "GetEnvironmentVariableW "
      "failed: error code = 0x%p", (void *)(ULONG_PTR)error);
  }
  
  utf8_value = convert_to_utf8(utf16_value,&error);
  free(utf16_value);
  if(utf8_value == NULL){
    if(error == ERROR_COMMITMENT_LIMIT || error == ERROR_NOT_ENOUGH_MEMORY){
      return luaL_error(L, "not enough memory");
    } else {
      return luaL_error(L, "unable to convert value to "
        "utf-8: error code = 0x%p", (void *)(ULONG_PTR)error);
    }
  }
  
  lua_pushstring(L, utf8_value);
  free(utf8_value);
  return 1;
}
Пример #9
0
/******************************************************************************
 *                                                                            *
 * Function: process_log                                                      *
 *                                                                            *
 * Purpose: Get message from logfile WITHOUT rotation                         *
 *                                                                            *
 * Parameters: filename - logfile name                                        *
 *             lastlogsize - offset for message                               *
 *             value - pointer for logged message                             *
 *                                                                            *
 * Return value: returns SUCCEED on successful reading,                       *
 *               FAIL on other cases                                          *
 *                                                                            *
 * Author: Eugene Grigorjev                                                   *
 *                                                                            *
 * Comments:                                                                  *
 *    This function allocates memory for 'value', because use zbx_free.       *
 *    Return SUCCEED and NULL value if end of file received.                  *
 *                                                                            *
 ******************************************************************************/
int	process_log(char *filename, long *lastlogsize, char **value, const char *encoding)
{
	int		f;
	struct stat	buf;
	int		nbytes, ret = FAIL;
	char		buffer[MAX_BUFFER_LEN];

	assert(filename);
	assert(lastlogsize);
	assert(value);
	assert(encoding);

	zabbix_log(LOG_LEVEL_DEBUG, "In process_log() filename:'%s' lastlogsize:%li", filename, *lastlogsize);

	/* handling of file shrinking */
	if (0 != zbx_stat(filename, &buf))
	{
		zabbix_log(LOG_LEVEL_WARNING, "cannot stat [%s]: %s", filename, zbx_strerror(errno));
		return ret;
	}

	if (buf.st_size < *lastlogsize)
		*lastlogsize = 0;

	if (-1 == (f = zbx_open(filename, O_RDONLY)))
	{
		zabbix_log(LOG_LEVEL_WARNING, "cannot open [%s]: %s", filename, zbx_strerror(errno));
		return ret;
	}

	if ((off_t)-1 != lseek(f, (off_t)*lastlogsize, SEEK_SET))
	{
		if (-1 != (nbytes = zbx_read(f, buffer, sizeof(buffer), encoding)))
		{
			if (0 != nbytes)
			{
				*lastlogsize += nbytes;
				*value = convert_to_utf8(buffer, nbytes, encoding);
				zbx_rtrim(*value, "\r\n ");
			}
			ret = SUCCEED;
		}
		else
			zabbix_log(LOG_LEVEL_WARNING, "cannot read from [%s]: %s", filename, zbx_strerror(errno));
	}
	else
		zabbix_log(LOG_LEVEL_WARNING, "cannot set position to [%li] for [%s]: %s", *lastlogsize, filename, zbx_strerror(errno));

	close(f);

	return ret;
}
Пример #10
0
void qr_code_generation ( char *input, char *output )
{
	int i;
	int version;
	int size;
	char *str_source_bin;
	char *data;
	char **blocks;
	char **pattern;
	int  **correction_blocks;

	str_source_bin = convert_to_utf8( input ); //convert string to utf8 ascii

	version = optimal_version( strlen( str_source_bin ) ); //optimal version

	str_source_bin = add_service_inf(str_source_bin, &version); //add service information into string

	blocks = create_blocks( str_source_bin, version ); //create blocks
	
	correction_blocks = create_correction_block( blocks, version ); //create correction blocks
	
	data = create_data ( blocks, correction_blocks, version ); //create data for qr code
	
	pattern = create_canvas_pattern ( data, version ); //create pattern for bmp
	
	create_bmp( pattern, output, version ); //create bmp

	//free str_source_bin
	free ( str_source_bin );

	//free blocks
	//free correction_blocks
	for ( i = 0; i < number_of_blocks[version]; i++ )
	{
			free( blocks[i] );
			free( correction_blocks[i] );
	}
	free( blocks );
	free( correction_blocks ); 

	//free data
	free( data );

	//free pattern
	size = ( ( ( version - 1 ) * 4 ) + 21 );
	size+=8; //white border
	for ( i = 0; i < size; i++ )
		free( pattern[i] );
	free( pattern );

}
Пример #11
0
void CharView::Draw(BRect urect)
{
	BPoint		point;
	font_height	fheight;
	char		utf8Char[3];
	uint16		uniChar[1];

	urect = Bounds();

//	SetLowColor(def_viewcolor);
	SetDrawingMode(B_OP_COPY);
	SetHighColor(strokeColor);
	SetPenSize(1);
	StrokeRect(urect);

	urect.InsetBy(1, 1);
	SetHighColor(bgColor);
	FillRect(urect);
	SetLowColor(bgColor);
	SetHighColor(displayColor);

	if (drawmode) {
		font.SetSize(urect.Width() * 0.6);
		font.GetHeight(&fheight);

		//  Unicode to UTF8 Character encoding
		uniChar[0] = B_HOST_TO_BENDIAN_INT16(((mutf == 0) || (mutf == 65535)) ? 1 : mutf);
		int32 state = 0;
		int32 srcLen = 2;
		int32 destLen = sizeof(utf8Char);
		convert_to_utf8(B_UNICODE_CONVERSION, (const char*)uniChar, &srcLen,
						utf8Char, &destLen, &state);

		SetFont(&font);
	
		bool hasGlyph[1];
		font.GetHasGlyphs(utf8Char, 1, hasGlyph);

		if (hasGlyph[0]) {
			float choffset = (urect.right - urect.left - StringWidth(utf8Char, destLen)) / 2;
			float cvoffset = (urect.Height() - fheight.ascent - fheight.descent) / 2 + fheight.ascent;
			point.Set(choffset, cvoffset);
			DrawString(utf8Char, destLen, point);
		}
	}

/*	printf("\nCharView!\n");
	printf("utf8Char[0]: %x\n", utf8Char[0]);
	printf("utf8Char[1]: %x\n", utf8Char[1]);
	printf("utf8Char[2]: %x\n", utf8Char[2]);*/
}
Пример #12
0
void DocWin::ConsoleWrite(
    REDIT *pREdit,
    LPARAM lParam
    ) {
    UString u;
    char * p = reinterpret_cast<char *>(lParam);
    int len = strlen(p);
    if (convert_to_utf8(p, len, &u) > 0) {
        pREdit->ConsoleWrite(u.len, (char *)u.str);
        UStr_free(&u);
    }
    else {
        pREdit->ConsoleWrite(len, p);
    }
}
Пример #13
0
void DocWin::ReadFile(
    HWND hwnd,
    const char *file
    ) {
    int size = get_file_size(file);
    FILE *fp = fopen(file, "rb");
    int eol_code = UNKNOWN_EOL;
    if (fp) {
        char *pbuf = new char[size + 1];
        int len = 0;
	if(pbuf) {
            len = fread(pbuf, 1, size + 1, fp);
	    pbuf[len] = '\0';
	    if((eol_code = get_eol_code(pbuf)) != UNKNOWN_EOL) {
	        m_pEdit->SetEOL(eol_code);
	    }
            set_filecode(pbuf, len);
            UString u;
            if (convert_to_utf8(pbuf, len, &u) > 0) {
                m_pEdit->AddText(u.len, (char *)u.str);
                UStr_free(&u);
            }
            else {
                m_pEdit->AddText(len, pbuf);
            }
	    delete [] pbuf;
	}
	else{
            char buf[blockSize];
            len = fread(buf, 1, sizeof(buf), fp);
            while(len > 0){
	        get_eol_code(buf);
		if (eol_code != UNKNOWN_EOL) {
	            if((eol_code = get_eol_code(buf)) != UNKNOWN_EOL) {
	                m_pEdit->SetEOL(eol_code);
	            }
		}
                m_pEdit->AddText(len, buf);
                len = fread(buf, 1, sizeof(buf), fp);
            }
	}
        fclose(fp);
        m_pEdit->SetSavePoint();
        m_pEdit->EmptyUndoBuffer();
        m_pEdit->GotoPos(0);
    }
}
Пример #14
0
static int handle_commit_msg(struct strbuf *line)
{
	static int still_looking = 1;

	if (!cmitmsg)
		return 0;

	if (still_looking) {
		strbuf_ltrim(line);
		if (!line->len)
			return 0;
		still_looking = check_header(line, s_hdr_data, 0);
		if (still_looking)
			return 0;
	}

	/* normalize the log message to UTF-8. */
	if (metainfo_charset)
		convert_to_utf8(line, charset.buf);

	if (use_scissors && is_scissors_line(line)) {
		int i;
		rewind(cmitmsg);
		ftruncate(fileno(cmitmsg), 0);
		still_looking = 1;

		/*
		 * We may have already read "secondary headers"; purge
		 * them to give ourselves a clean restart.
		 */
		for (i = 0; header[i]; i++) {
			if (s_hdr_data[i])
				strbuf_release(s_hdr_data[i]);
			s_hdr_data[i] = NULL;
		}
		return 0;
	}

	if (patchbreak(line)) {
		fclose(cmitmsg);
		cmitmsg = NULL;
		return 1;
	}

	fputs(line->buf, cmitmsg);
	return 0;
}
Пример #15
0
static status_t clip_manager(void *arg)
{
	for (;;) {

		// Receive message
		thread_id sender;
		uint32 code = receive_data(&sender, NULL, 0);
		D(bug("Clipboard manager received %08lx\n", code));
		switch (code) {
			case MSG_QUIT_CLIP_MANAGER:
				return 0;

			case MSG_PUT_TEXT:
				if (be_clipboard->Lock()) {
					be_clipboard->Clear();
					BMessage *clipper = be_clipboard->Data(); 
	
					// Convert text from Mac charset to UTF-8
					int32 dest_length = cm_length * 3;
					int32 state = 0;
					char *inbuf = new char[cm_length];
					memcpy(inbuf, cm_scrap, cm_length);	// Copy to user space
					char *outbuf = new char[dest_length];
					if (convert_to_utf8(B_MAC_ROMAN_CONVERSION, inbuf, &cm_length, outbuf, &dest_length, &state) == B_OK) {
						for (int i=0; i<dest_length; i++)
							if (outbuf[i] == 13)
								outbuf[i] = 10;
		
						// Add text to Be clipboard
						clipper->AddData("text/plain", B_MIME_TYPE, outbuf, dest_length); 
						be_clipboard->Commit();
					} else {
						D(bug(" text conversion failed\n"));
					}
					delete[] outbuf;
					delete[] inbuf;
					be_clipboard->Unlock();
				}
				break;
		}

		// Acknowledge
		release_sem(cm_done_sem);
	}
}
Пример #16
0
void
PDFWriter::ToUtf8(uint32 encoding, const char *string, BString &utf8)
{
	int32 len = strlen(string);
	int32 srcLen = len, destLen = 255;
	int32 state = 0;
	char buffer[256];
	int32 srcStart = 0;

	do {
		convert_to_utf8(encoding, &string[srcStart], &srcLen, buffer, &destLen,
			&state);
		srcStart += srcLen;
		len -= srcLen;
		srcLen = len;

		utf8.Append(buffer, destLen);
		destLen = 255;
	} while (len > 0);
};
Пример #17
0
void
BF_GUI_Text::ToUtf8(const char *pc_Source,char *pc_Dest)
{
	int32	iIN;
	int32	iOUT;
	int32	iStatus;
	uint32	iCP = GetConversion();

		if ( iCodePage == CP_UTF8 )
		{
			strcpy(pc_Dest,pc_Source);
		}
		else
		{
			iIN = strlen(pc_Source);
			iOUT = 255;
			convert_to_utf8(iCP,pc_Source,&iIN,pc_Dest,&iOUT,&iStatus);
			pc_Dest[iOUT] = 0;
		}
}
Пример #18
0
/*
 * Make it easier to use the convert_to_utf8 function for filenames
 * and directory names. Note, only one at a time because there's only
 * one buffer.
 * This isn't being freed as it stands now.
 */
char *
fname_to_utf8(char *fname)
{
    static char *fname_utf8_buf = NULL;
    static size_t fname_utf8_len = 0;
    char *converted_fname, *p;

    p = convert_to_utf8(fname, NULL, 0);
    if(p)
      converted_fname = p;
    else
      converted_fname = fname;

    if(converted_fname){
	if(strlen(converted_fname)+1 > fname_utf8_len){
	    if(fname_utf8_buf)
	      fs_give((void **) &fname_utf8_buf);

	    fname_utf8_len = strlen(converted_fname)+1;
	    fname_utf8_buf = (char *) fs_get(fname_utf8_len * sizeof(char));
	}

	strncpy(fname_utf8_buf, converted_fname, fname_utf8_len);
	fname_utf8_buf[fname_utf8_len-1] = '\0';
    }
    else{
	if(fname_utf8_len == 0){
	    fname_utf8_len = 1;
	    fname_utf8_buf = (char *) fs_get(fname_utf8_len * sizeof(char));
	}

	fname_utf8_buf[0] = '\0';
    }

    if(p)
      fs_give((void **) &p);

    return(fname_utf8_buf);
}
Пример #19
0
struct element *
xml_parse_document(fz_context *ctx, unsigned char *s, int n)
{
	struct parser parser;
	struct element root;
	char *p, *error;

	/* s is already null-terminated (see xps_new_part) */

	memset(&root, 0, sizeof(root));
	parser.head = &root;
	parser.ctx = ctx;

	p = convert_to_utf8(ctx, s, n);

	error = xml_parse_document_imp(&parser, p);
	if (error)
		fz_throw(ctx, "%s", error);

	if (p != (char*)s)
		fz_free(ctx, p);

	return root.down;
}
Пример #20
0
int
ACE_Svc_Conf_Lexer::yylex (YYSTYPE* ace_yylval,
                           ACE_Svc_Conf_Param* param)
{
#if defined (ACE_USES_WCHAR)
  bool look_for_bom = false;
  ACE_Encoding_Converter_Factory::Encoding_Hint hint =
                ACE_Encoding_Converter_Factory::ACE_NONE;
#endif /* ACE_USES_WCHAR */
  if (param->buffer == 0)
    {
#if defined (ACE_USES_WCHAR)
      look_for_bom = true;
#endif /* ACE_USES_WCHAR */
      ACE_NEW_RETURN (param->buffer,
                      ace_yy_buffer_state,
                      -1);
    }

  int token = ACE_NO_STATE;
  do {
    if (param->buffer->need_more_)
      {
#if defined (ACE_USES_WCHAR)
        size_t skip_bytes = 0;
#endif /* ACE_USES_WCHAR */
        param->buffer->need_more_ = false;
        size_t amount =
               input (param,
                      param->buffer->input_ + param->buffer->size_,
                      normalize (ACE_YY_BUF_SIZE -
                                 param->buffer->size_));
        if (amount == 0)
          {
            param->buffer->eof_ = true;
#if defined (ACE_USES_WCHAR)
            skip_bytes = param->buffer->size_;
#endif /* ACE_USES_WCHAR */
          }
        else
          {
#if defined (ACE_USES_WCHAR)
            if (look_for_bom)
              {
                size_t read_more = 0;

                look_for_bom = false;
                hint = locate_bom (param->buffer->input_, amount, read_more);

                if (read_more != 0)
                  {
                    input (param,
                           param->buffer->input_ + amount,
                           read_more);
                    ACE_OS::memmove (param->buffer->input_,
                                     param->buffer->input_ + read_more,
                                     amount);
                  }
              }
            skip_bytes = param->buffer->size_;
#endif /* ACE_USES_WCHAR */
            param->buffer->size_ += amount;
          }

#if defined (ACE_USES_WCHAR)
        if (!convert_to_utf8 (param, skip_bytes, hint))
          {
            ace_yyerror (++param->yyerrno,
                         param->yylineno,
                         ACE_TEXT ("Unable to convert input stream to UTF-8"));
            return ACE_NO_STATE;
          }
#endif /* ACE_USES_WCHAR */
      }

    token = scan (ace_yylval, param);
  } while (token == ACE_NO_STATE && param->buffer->need_more_);

  return token;
}
Пример #21
0
int	telnet_execute(ZBX_SOCKET socket_fd, const char *command, AGENT_RESULT *result, const char *encoding)
{
	const char	*__function_name = "telnet_execute";
	char		buf[MAX_BUFFER_LEN];
	size_t		sz, offset;
	int		rc, ret = FAIL;
	char		*command_lf = NULL, *command_crlf = NULL;
	size_t		i, offset_lf, offset_crlf;

	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);

	/* `command' with multiple lines may contain CR+LF from the browser;	*/
	/* it should be converted to plain LF to remove echo later on properly	*/
	offset_lf = strlen(command);
	command_lf = zbx_malloc(command_lf, offset_lf + 1);
	zbx_strlcpy(command_lf, command, offset_lf + 1);
	convert_telnet_to_unix_eol(command_lf, &offset_lf);

	/* telnet protocol requires that end-of-line is transferred as CR+LF	*/
	command_crlf = zbx_malloc(command_crlf, offset_lf * 2 + 1);
	convert_unix_to_telnet_eol(command_lf, offset_lf, command_crlf, &offset_crlf);

	telnet_socket_write(socket_fd, command_crlf, offset_crlf);
	telnet_socket_write(socket_fd, "\r\n", 2);

	sz = sizeof(buf);
	offset = 0;
	while (ZBX_TCP_ERROR != (rc = telnet_read(socket_fd, buf, &sz, &offset)))
	{
		if (prompt_char == telnet_lastchar(buf, offset))
			break;
	}

	convert_telnet_to_unix_eol(buf, &offset);
	zabbix_log(LOG_LEVEL_DEBUG, "%s() command output:'%.*s'", __function_name, offset, buf);

	if (ZBX_TCP_ERROR == rc)
	{
		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "No prompt: %s", zbx_tcp_strerror()));
		goto fail;
	}

	telnet_rm_echo(buf, &offset, command_lf, offset_lf);

	/* multi-line commands may have returned additional prompts;	*/
	/* this is not a perfect solution, because in case of multiple	*/
	/* multi-line shell statements these prompts might appear in	*/
	/* the middle of the output, but we still try to be helpful by	*/
	/* removing additional prompts at least from the beginning	*/
	for (i = 0; i < offset_lf; i++)
	{
		if ('\n' == command_lf[i])
			if (SUCCEED != telnet_rm_echo(buf, &offset, "$ ", 2) &&
				SUCCEED != telnet_rm_echo(buf, &offset, "# ", 2) &&
				SUCCEED != telnet_rm_echo(buf, &offset, "> ", 2) &&
				SUCCEED != telnet_rm_echo(buf, &offset, "% ", 2))
			{
				break;
			}
	}

	telnet_rm_echo(buf, &offset, "\n", 1);
	telnet_rm_prompt(buf, &offset);

	zabbix_log(LOG_LEVEL_DEBUG, "%s() stripped command output:'%.*s'", __function_name, offset, buf);

	if (MAX_BUFFER_LEN == offset)
		offset--;
	buf[offset] = '\0';

	SET_STR_RESULT(result, convert_to_utf8(buf, offset, encoding));
	ret = SUCCEED;
fail:
	zbx_free(command_lf);
	zbx_free(command_crlf);

	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));

	return ret;
}
Пример #22
0
_EXPORT status_t mail_convert_to_utf8 (
	uint32 srcEncoding,
	const char *src,
	int32 *srcLen,
	char *dst,
	int32 *dstLen,
	int32 *state,
	char substitute)
{
	int32    copyAmount;
	char    *originalDst = dst;
	status_t returnCode = -1;

	if (srcEncoding == B_MAIL_UTF8_CONVERSION) {
		copyAmount = *srcLen;
		if (*dstLen < copyAmount)
			copyAmount = *dstLen;
		memcpy (dst, src, copyAmount);
		*srcLen = copyAmount;
		*dstLen = copyAmount;
		returnCode = B_OK;
	} else if (srcEncoding == B_MAIL_US_ASCII_CONVERSION) {
		int32 i;
		unsigned char letter;
		copyAmount = *srcLen;
		if (*dstLen < copyAmount)
			copyAmount = *dstLen;
		for (i = 0; i < copyAmount; i++) {
			letter = *src++;
			if (letter > 0x80U)
				// Invalid, could also use substitute, but better to strip high bit.
				*dst++ = letter - 0x80U;
			else if (letter == 0x80U)
				// Can't convert to 0x00 since that's NUL, which would cause problems.
				*dst++ = substitute;
			else
				*dst++ = letter;
		}
		*srcLen = copyAmount;
		*dstLen = copyAmount;
		returnCode = B_OK;
	} else
		returnCode = convert_to_utf8 (srcEncoding, src, srcLen,
			dst, dstLen, state, substitute);

	if (returnCode == B_OK) {
		// Replace spurious NUL bytes, which should normally not be in the
		// output of the decoding (not normal UTF-8 characters, and no NULs are
		// in our usual input strings).  They happen for some odd ISO-2022-JP
		// byte pair combinations which are improperly handled by the BeOS
		// routines.  Like "\e$ByD\e(B" where \e is the ESC character $1B, the
		// first ESC $ B switches to a Japanese character set, then the next
		// two bytes "yD" specify a character, then ESC ( B switches back to
		// the ASCII character set.  The UTF-8 conversion yields a NUL byte.
		int32 i;
		for (i = 0; i < *dstLen; i++)
			if (originalDst[i] == 0)
				originalDst[i] = substitute;
	}
	return returnCode;
}
Пример #23
0
void
Renamer_Remove::RenameList(BList* FileList)
{
	Renamer::RenameList(FileList);

	bool FromRight1 = bool(fDirection1->Menu()
		->IndexOf(fDirection1->Menu()->FindMarked()));
	bool FromRight2 = bool(fDirection2->Menu()
		->IndexOf(fDirection2->Menu()->FindMarked()));

	FileListItem* ListItem;
	BString ResultString, Part2;
	int EndPart1, StartPart2;

	int32 Position1 = fPosition1->Value();
	int32 Position2 = fPosition2->Value();	
	int32 positionMaxValue = 0;
	int32 UTF_LengthOfFilename, LengthOfFilename;
		
	for (int i = 0; i < fNumberOfItems; i++) {
		ListItem = (FileListItem* )FileList->ItemAt(i);
		UTF_LengthOfFilename = LengthOfFilename = ListItem->fName.Length();

		if (LengthOfFilename > positionMaxValue)
			positionMaxValue = LengthOfFilename;

		char* tempStr = new char[UTF_LengthOfFilename + 1];

		convert_from_utf8(B_ISO1_CONVERSION, ListItem->fName.String(),
			&UTF_LengthOfFilename, tempStr, &LengthOfFilename, 0);
		tempStr[LengthOfFilename] = 0;

		if (FromRight1) {
			EndPart1 = (LengthOfFilename >= Position1)
				? LengthOfFilename - Position1 : 0;
		} else {
			EndPart1 = (LengthOfFilename >= Position1)
				? Position1 : LengthOfFilename;
		}

		if (FromRight2) {
			StartPart2 = (LengthOfFilename >= Position2)
				? LengthOfFilename - Position2 : 0;
		} else {
			StartPart2 = (LengthOfFilename >= Position2)
				? Position2 : LengthOfFilename;
		}

		if (StartPart2 < EndPart1)
			std::swap(StartPart2, EndPart1);

		ResultString.SetTo(tempStr, EndPart1);

		BString(tempStr).CopyInto(Part2, StartPart2,
			LengthOfFilename - StartPart2);
		ResultString.Append(Part2);

		LengthOfFilename = ResultString.Length();
		UTF_LengthOfFilename = LengthOfFilename*  2;
		char* utf_String = new char[UTF_LengthOfFilename + 1];
		
		convert_to_utf8(B_ISO1_CONVERSION, ResultString.String(),
			&LengthOfFilename, utf_String, &UTF_LengthOfFilename, 0);
		utf_String[UTF_LengthOfFilename] = 0;

		ListItem->SetNewName(utf_String);
	}
	fPosition1->SetMaxValue(positionMaxValue);
	fPosition2->SetMaxValue(positionMaxValue);
}
Пример #24
0
static bytea *
encrypt_internal(int is_pubenc, int is_text,
				 text *data, text *key, text *args)
{
	MBuf	   *src,
			   *dst;
	uint8		tmp[VARHDRSZ];
	uint8	   *restmp;
	bytea	   *res;
	int			res_len;
	PGP_Context *ctx;
	int			err;
	struct debug_expect ex;
	text	   *tmp_data = NULL;

	/*
	 * Add data and key info RNG.
	 */
	add_entropy(data, key, NULL);

	init_work(&ctx, is_text, args, &ex);

	if (is_text && pgp_get_unicode_mode(ctx))
	{
		tmp_data = convert_to_utf8(data);
		if (tmp_data == data)
			tmp_data = NULL;
		else
			data = tmp_data;
	}

	src = create_mbuf_from_vardata(data);
	dst = mbuf_create(VARSIZE(data) + 128);

	/*
	 * reserve room for header
	 */
	mbuf_append(dst, tmp, VARHDRSZ);

	/*
	 * set key
	 */
	if (is_pubenc)
	{
		MBuf	   *kbuf = create_mbuf_from_vardata(key);

		err = pgp_set_pubkey(ctx, kbuf,
							 NULL, 0, 0);
		mbuf_free(kbuf);
	}
	else
		err = pgp_set_symkey(ctx, (uint8 *) VARDATA(key),
							 VARSIZE(key) - VARHDRSZ);

	/*
	 * encrypt
	 */
	if (err >= 0)
		err = pgp_encrypt(ctx, src, dst);

	/*
	 * check for error
	 */
	if (err)
	{
		if (ex.debug)
			px_set_debug_handler(NULL);
		if (tmp_data)
			clear_and_pfree(tmp_data);
		pgp_free(ctx);
		mbuf_free(src);
		mbuf_free(dst);
		ereport(ERROR,
				(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
				 errmsg("%s", px_strerror(err))));
	}

	/* res_len includes VARHDRSZ */
	res_len = mbuf_steal_data(dst, &restmp);
	res = (bytea *) restmp;
	SET_VARSIZE(res, res_len);

	if (tmp_data)
		clear_and_pfree(tmp_data);
	pgp_free(ctx);
	mbuf_free(src);
	mbuf_free(dst);

	px_set_debug_handler(NULL);

	return res;
}
Пример #25
0
void 
CDDBQuery::_ReadLine(BString &buffer)
{
	buffer = "";
	unsigned char ch;

	for (;;) {
		if (fSocket.Receive(&ch, 1) <= 0)
			break;


		// This function is more work than it should have to be. FreeDB lookups can sometimes 
		// be in a non-ASCII encoding, such as Latin-1 or UTF8. The problem lies in Be's implementation
		// of BString, which does not support UTF8 string assignments. The Be Book says we have to
		// flatten the string and adjust the character counts manually. Man, this *really* sucks.
		if (ch > 0x7f) 	{
			// Obviously non-ASCII character detected. Let's see if it's Latin-1 or UTF8.
			unsigned char *string, *stringindex;
			int32 length = buffer.Length();

			// The first byte of a UTF8 string will be 110xxxxx
			if ( ((ch & 0xe0) == 0xc0)) {
				// This is UTF8. Get the next byte
				unsigned char ch2;
				if (fSocket.Receive(&ch2, 1) <= 0)
					break;

				if ( (ch2 & 0xc0) == 0x80) {
					string = (unsigned char *)buffer.LockBuffer(length + 10);
					stringindex = string + length;

					stringindex[0] = ch;
					stringindex[1] = ch2;
					stringindex[2] = 0;
					
					buffer.UnlockBuffer();
					
					// We've added the character, so go to the next iteration
					continue;
				}
			}

			// Nope. Just Latin-1. Convert to UTF8 and assign
			char srcstr[2], deststr[5];
			int32 srclen, destlen, state;

			srcstr[0] = ch;
			srcstr[1] = '\0';
			srclen = 1;
			destlen = 5;
			memset(deststr, 0, 5);

			if (convert_to_utf8(B_ISO1_CONVERSION, srcstr, &srclen, deststr, &destlen, &state) == B_OK) {
				// We succeeded. Amazing. Now we hack the string into having the character
				length = buffer.Length();

				string = (unsigned char *)buffer.LockBuffer(length + 10);
				stringindex = string + length;

				for (int i = 0; i < 5; i++) {
					stringindex[i] = deststr[i];
					if (!deststr[i])
						break;
				}

				buffer.UnlockBuffer();
			} else {
				// well, we tried. Append the character to the string and live with it
				buffer += ch;
			}
		} else
			buffer += ch;

		if (ch == '\n')
			break;
	}

	buffer.RemoveAll("\r");
	STRACE(("<%s", buffer.String()));
}
Пример #26
0
		std::string convert_to_utf8(const std::wstring& str) { return convert_to_utf8(str.c_str()); }; 
Пример #27
0
/*
 * Append a line of text to the buffer
 */
void html_text_buffer_append(GtkTextView *text_view, char *txt, int ignore)
{
	gchar *text = convert_to_utf8(txt);
	GtkTextIter iter;
	GtkTextMark *insert_mark;

	GdkRectangle iter_loc;
	GdkRectangle visible_rect;

	GtkTextBuffer *buffer = gtk_text_view_get_buffer(text_view);

	if (strcasestr(text, "<br>")) {
		char *c = text;
		while ((c = strchr(text, '\n')) != 0)
			*c = ' ';
		while ((c = strchr(text, '\r')) != 0)
			*c = ' ';
	} else if (strchr(text, '\r')) {
		char *c = text;
		if (strchr(text, '\n')) {
			while ((c = strchr(c, '\r')) != 0)
				*c = ' ';
		} else {
			while ((c = strchr(c, '\r')) != 0)
				*c = '\n';
		}
	}

	gtk_text_buffer_get_end_iter(buffer, &iter);

	insert_mark = gtk_text_buffer_get_mark(buffer, "real_end_mark");

	if (insert_mark) {
		GtkTextIter del;
		gtk_text_buffer_get_iter_at_mark(buffer, &del, insert_mark);
		gtk_text_buffer_delete(buffer, &del, &iter);
		gtk_text_buffer_get_end_iter(buffer, &iter);
	}
	else
		insert_mark = gtk_text_buffer_create_mark(buffer,
			"real_end_mark", &iter, TRUE);

	/* Decide first if we want to scroll the text to the end or not */
	gtk_text_view_get_iter_location(text_view, &iter, &iter_loc);
	gtk_text_view_get_visible_rect(text_view, &visible_rect);

	gtk_text_buffer_insert(buffer, &iter, text, -1);
	parse_html(text_view, *insert_mark, ignore);

	if (iter_loc.y <= visible_rect.y + visible_rect.height) {
		GtkTextMark *end_mark;

		gtk_text_buffer_get_end_iter(buffer, &iter);
		end_mark = gtk_text_buffer_create_mark(buffer, NULL, &iter,
			TRUE);

		gtk_text_view_scroll_mark_onscreen(text_view, end_mark);
		gtk_text_buffer_delete_mark(buffer, end_mark);
	}

	if (!(ignore & HTML_IGNORE_END))
		gtk_text_buffer_delete_mark(buffer, insert_mark);

	g_free(text);
}
Пример #28
0
static int decode_header_bq(struct strbuf *it)
{
	char *in, *ep, *cp;
	struct strbuf outbuf = STRBUF_INIT, *dec;
	struct strbuf charset_q = STRBUF_INIT, piecebuf = STRBUF_INIT;
	int rfc2047 = 0;

	in = it->buf;
	while (in - it->buf <= it->len && (ep = strstr(in, "=?")) != NULL) {
		int encoding;
		strbuf_reset(&charset_q);
		strbuf_reset(&piecebuf);
		rfc2047 = 1;

		if (in != ep) {
			/*
			 * We are about to process an encoded-word
			 * that begins at ep, but there is something
			 * before the encoded word.
			 */
			char *scan;
			for (scan = in; scan < ep; scan++)
				if (!isspace(*scan))
					break;

			if (scan != ep || in == it->buf) {
				/*
				 * We should not lose that "something",
				 * unless we have just processed an
				 * encoded-word, and there is only LWS
				 * before the one we are about to process.
				 */
				strbuf_add(&outbuf, in, ep - in);
			}
		}
		/* E.g.
		 * ep : "=?iso-2022-jp?B?GyR...?= foo"
		 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
		 */
		ep += 2;

		if (ep - it->buf >= it->len || !(cp = strchr(ep, '?')))
			goto decode_header_bq_out;

		if (cp + 3 - it->buf > it->len)
			goto decode_header_bq_out;
		strbuf_add(&charset_q, ep, cp - ep);

		encoding = cp[1];
		if (!encoding || cp[2] != '?')
			goto decode_header_bq_out;
		ep = strstr(cp + 3, "?=");
		if (!ep)
			goto decode_header_bq_out;
		strbuf_add(&piecebuf, cp + 3, ep - cp - 3);
		switch (tolower(encoding)) {
		default:
			goto decode_header_bq_out;
		case 'b':
			dec = decode_b_segment(&piecebuf);
			break;
		case 'q':
			dec = decode_q_segment(&piecebuf, 1);
			break;
		}
		if (metainfo_charset)
			convert_to_utf8(dec, charset_q.buf);

		strbuf_addbuf(&outbuf, dec);
		strbuf_release(dec);
		free(dec);
		in = ep + 2;
	}
	strbuf_addstr(&outbuf, in);
	strbuf_reset(it);
	strbuf_addbuf(it, &outbuf);
decode_header_bq_out:
	strbuf_release(&outbuf);
	strbuf_release(&charset_q);
	strbuf_release(&piecebuf);
	return rfc2047;
}
Пример #29
0
/* example ssh.run["ls /"] */
static int	ssh_run(DC_ITEM *item, AGENT_RESULT *result, const char *encoding)
{
	const char	*__function_name = "ssh_run";
	zbx_sock_t	s;
	LIBSSH2_SESSION	*session;
	LIBSSH2_CHANNEL	*channel;
	int		auth_pw = 0, rc, ret = NOTSUPPORTED,
			exitcode, bytecount = 0;
	char		buffer[MAX_BUFFER_LEN], buf[16], *userauthlist,
			*publickey = NULL, *privatekey = NULL, *ssherr;
	size_t		sz;

	zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);

	if (FAIL == zbx_tcp_connect(&s, CONFIG_SOURCE_IP, item->interface.addr, item->interface.port, 0))
	{
		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot connect to SSH server: %s",
				zbx_tcp_strerror()));
		goto close;
	}

	/* initializes an SSH session object */
	if (NULL == (session = libssh2_session_init()))
	{
		SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot initialize SSH session"));
		goto tcp_close;
	}

	/* set blocking mode on session */
	libssh2_session_set_blocking(session, 1);

	/* Create a session instance and start it up. This will trade welcome */
	/* banners, exchange keys, and setup crypto, compression, and MAC layers */
	if (0 != libssh2_session_startup(session, s.socket))
	{
		libssh2_session_last_error(session, &ssherr, NULL, 0);
		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot establish SSH session: %s", ssherr));
		goto session_free;
	}

	/* check what authentication methods are available */
	if (NULL != (userauthlist = libssh2_userauth_list(session, item->username, strlen(item->username))))
	{
		if (NULL != strstr(userauthlist, "password"))
			auth_pw |= 1;
		if (NULL != strstr(userauthlist, "keyboard-interactive"))
			auth_pw |= 2;
		if (NULL != strstr(userauthlist, "publickey"))
			auth_pw |= 4;
	}
	else
	{
		libssh2_session_last_error(session, &ssherr, NULL, 0);
		SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain authentication methods: %s", ssherr));
		goto session_close;
	}

	zabbix_log(LOG_LEVEL_DEBUG, "%s() supported authentication methods:'%s'", __function_name, userauthlist);

	switch (item->authtype)
	{
		case ITEM_AUTHTYPE_PASSWORD:
			if (auth_pw & 1)
			{
				/* we could authenticate via password */
				if (0 != libssh2_userauth_password(session, item->username, item->password))
				{
					libssh2_session_last_error(session, &ssherr, NULL, 0);
					SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Password authentication failed: %s",
							ssherr));
					goto session_close;
				}
				else
					zabbix_log(LOG_LEVEL_DEBUG, "%s() password authentication succeeded",
							__function_name);
			}
			else if (auth_pw & 2)
			{
				/* or via keyboard-interactive */
				password = item->password;
				if (0 != libssh2_userauth_keyboard_interactive(session, item->username, &kbd_callback))
				{
					libssh2_session_last_error(session, &ssherr, NULL, 0);
					SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Keyboard-interactive authentication"
							" failed: %s", ssherr));
					goto session_close;
				}
				else
					zabbix_log(LOG_LEVEL_DEBUG, "%s() keyboard-interactive authentication succeeded",
							__function_name);
			}
			else
			{
				SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Unsupported authentication method."
						" Supported methods: %s", userauthlist));
				goto session_close;
			}
			break;
		case ITEM_AUTHTYPE_PUBLICKEY:
			if (auth_pw & 4)
			{
				if (NULL == CONFIG_SSH_KEY_LOCATION)
				{
					SET_MSG_RESULT(result, zbx_strdup(NULL, "Authentication by public key failed."
							" SSHKeyLocation option is not set"));
					goto session_close;
				}

				/* or by public key */
				publickey = zbx_dsprintf(publickey, "%s/%s", CONFIG_SSH_KEY_LOCATION, item->publickey);
				privatekey = zbx_dsprintf(privatekey, "%s/%s", CONFIG_SSH_KEY_LOCATION,
						item->privatekey);

				if (SUCCEED != zbx_is_regular_file(publickey))
				{
					SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot access public key file %s",
							publickey));
					goto session_close;
				}

				if (SUCCEED != zbx_is_regular_file(privatekey))
				{
					SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot access private key file %s",
							privatekey));
					goto session_close;
				}

				rc = libssh2_userauth_publickey_fromfile(session, item->username, publickey,
						privatekey, item->password);
				zbx_free(publickey);
				zbx_free(privatekey);

				if (0 != rc)
				{
					libssh2_session_last_error(session, &ssherr, NULL, 0);
					SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Public key authentication failed:"
							" %s", ssherr));
					goto session_close;
				}
				else
					zabbix_log(LOG_LEVEL_DEBUG, "%s() authentication by public key succeeded",
							__function_name);
			}
			else
			{
				SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Unsupported authentication method."
						" Supported methods: %s", userauthlist));
				goto session_close;
			}
			break;
	}

	/* exec non-blocking on the remove host */
	while (NULL == (channel = libssh2_channel_open_session(session)))
	{
		switch (libssh2_session_last_error(session, NULL, NULL, 0))
		{
			/* marked for non-blocking I/O but the call would block. */
			case LIBSSH2_ERROR_EAGAIN:
				waitsocket(s.socket, session);
				continue;
			default:
				SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot establish generic session channel"));
				goto session_close;
		}
	}

	dos2unix(item->params);	/* CR+LF (Windows) => LF (Unix) */
	/* request a shell on a channel and execute command */
	while (0 != (rc = libssh2_channel_exec(channel, item->params)))
	{
		switch (rc)
		{
			case LIBSSH2_ERROR_EAGAIN:
				waitsocket(s.socket, session);
				continue;
			default:
				SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot request a shell"));
				goto channel_close;
		}
	}

	for (;;)
	{
		/* loop until we block */
		do
		{
			if (0 < (rc = libssh2_channel_read(channel, buf, sizeof(buf))))
			{
				sz = (size_t)rc;
				if (sz > MAX_BUFFER_LEN - (bytecount + 1))
					sz = MAX_BUFFER_LEN - (bytecount + 1);
				if (0 == sz)
					continue;

				memcpy(buffer + bytecount, buf, sz);
				bytecount += sz;
			}
		}
		while (rc > 0);

		/* this is due to blocking that would occur otherwise so we loop on
		 * this condition
		 */
		if (LIBSSH2_ERROR_EAGAIN == rc)
			waitsocket(s.socket, session);
		else if (rc < 0)
		{
			SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot read data from SSH server"));
			goto channel_close;
		}
		else
			break;
	}

	buffer[bytecount] = '\0';
	SET_STR_RESULT(result, convert_to_utf8(buffer, bytecount, encoding));

	ret = SYSINFO_RET_OK;

channel_close:
	/* close an active data channel */
	exitcode = 127;
	while (0 != (rc = libssh2_channel_close(channel)))
	{
		switch (rc)
		{
			case LIBSSH2_ERROR_EAGAIN:
				waitsocket(s.socket, session);
				continue;
			default:
				libssh2_session_last_error(session, &ssherr, NULL, 0);
				zabbix_log(LOG_LEVEL_WARNING, "%s() cannot close generic session channel: %s",
						__function_name, ssherr);
				break;
		}
	}

	if (0 == rc)
		exitcode = libssh2_channel_get_exit_status(channel);
	zabbix_log(LOG_LEVEL_DEBUG, "%s() exitcode: %d bytecount: %d",
			__function_name, exitcode, bytecount);

	libssh2_channel_free(channel);
	channel = NULL;

session_close:
	libssh2_session_disconnect(session, "Normal Shutdown");

session_free:
	libssh2_session_free(session);

tcp_close:
	zbx_tcp_close(&s);

close:
	zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));

	return ret;
}
Пример #30
0
Datum
convert_to_UTF8(PG_FUNCTION_ARGS)
{
    // things we need to deal with constructing our composite type
    TupleDesc   tupdesc;
    Datum       values[3];
    bool        nulls[3];
    HeapTuple   tuple;

    // for char_set_detect function returns
    text    *encoding = NULL;
    text    *lang = NULL;
    int32_t confidence = 0;

    UErrorCode status = U_ZERO_ERROR;

    // output buffer for conversion to Unicode
    UChar* uBuf = NULL;
    int32_t uBuf_len = 0;

    // output of this function
    text *text_out;
    bool converted = false;
    bool dropped_bytes = false;
    bool dropped_bytes_toU = false;
    bool dropped_bytes_fromU = false;

    // temporary buffer for converted string
    char* converted_buf = NULL;

    // input args
    const text  *buffer = PG_GETARG_TEXT_P(0);
    const bool  force   = PG_GETARG_BOOL(1);

    // C string of text* buffer
    const char* cbuffer = NULL;
    int cbuffer_len = 0;

    // Convert output values into a PostgreSQL composite type.
    if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
        ereport(ERROR,
            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
              errmsg("function returning record called in context "
                     "that cannot accept type record.\n")));

    // BlessTupleDesc for Datums
    BlessTupleDesc(tupdesc);

    // return if string to convert is NULL
    if (NULL == buffer)
    {
        // return input string,
        // converted to true,
        // dropped_bytes to false
        text_out = (text *) buffer;
        converted = true;
        dropped_bytes = false;
    }
    else
    {
        // extract string from text* to C string
        cbuffer = text_to_cstring(buffer);
        cbuffer_len = strlen(cbuffer);

        // bail on zero-length strings
        // return if cbuffer has zero length or contains a blank space
        if ((0 == cbuffer_len) || (0 == strcmp("", cbuffer)))
        {
            text_out = (text *) buffer;
            converted = true;
            dropped_bytes = false;
        }
        else
        {
            // UTF8 output can be up to 6 bytes per input byte
            // palloc0 allocates and zeros bytes in array
            int32_t converted_buf_len = cbuffer_len * 6 * sizeof(char);
            converted_buf = (char *) palloc0(converted_buf_len);
            // int32_t converted_len = 0;

            // detect encoding with ICU
            status = detect_ICU(buffer, &encoding, &lang, &confidence);

            ereport(DEBUG1,
                (errcode(ERRCODE_SUCCESSFUL_COMPLETION),
                 errmsg("ICU detection status: %d\n", status)));

            ereport(DEBUG1,
                (errcode(ERRCODE_SUCCESSFUL_COMPLETION),
                 errmsg("Detected encoding: %s, language: %s, confidence: %d\n",
                 text_to_cstring(encoding),
                 text_to_cstring(lang),
                         confidence)));

            // return without attempting a conversion if UTF8 is detected
            if (
                (0 == strcmp("UTF-8", text_to_cstring(encoding))) ||
                (0 == strcmp("utf-8", text_to_cstring(encoding))) ||
                (0 == strcmp("UTF8", text_to_cstring(encoding)))  ||
                (0 == strcmp("utf8", text_to_cstring(encoding)))
               )
            {
                ereport(DEBUG1,
                    (errcode(ERRCODE_SUCCESSFUL_COMPLETION),
                     errmsg("ICU detected %s.  No conversion necessary.\n", text_to_cstring(encoding))));

                text_out = (text *) buffer;
                converted = true;
                dropped_bytes = false;
            }
            else
            {
                // ICU uses UTF16 internally, so need to convert to Unicode first
                // then convert to UTF8

                if (U_SUCCESS(status))
                    status = convert_to_unicode(buffer, (const text*) encoding, &uBuf, (int32_t*) &uBuf_len, force, &dropped_bytes_toU);

                if (U_SUCCESS(status))
                    status = convert_to_utf8((const UChar*) uBuf, uBuf_len, &converted_buf, (int32_t*) &converted_buf_len, force, &dropped_bytes_fromU);

                if (U_SUCCESS(status))
                {
                    text_out = cstring_to_text(converted_buf);
                    converted = true;
                    dropped_bytes = (dropped_bytes_toU || dropped_bytes_fromU);
                }
                else
                {
                    ereport(WARNING,
                        (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
                            errmsg("ICU conversion failed - returning original input")));

                    text_out = (text *) buffer;
                    converted = false;
                    dropped_bytes = false;
                }
            } // already UTF8
        } // zero-length string
    }  // return if buffer is NULL

    values[0] = PointerGetDatum(text_out);
    values[1] = BoolGetDatum(converted);
    values[2] = BoolGetDatum(dropped_bytes);

    // check if pointers are still NULL; if so Datum is NULL and
    // confidence is meaningless (also NULL)
    (text_out  == NULL || ! VARSIZE_ANY_EXHDR(text_out)) ? (nulls[0] = true) : (nulls[0] = false);
    // converted will never be NULL
    nulls[1] = false;
    nulls[2] = false;

    // build tuple from datum array
    tuple = heap_form_tuple(tupdesc, values, nulls);

    // cleanup
    if (NULL != encoding)
        pfree((void *) encoding);

    if (NULL != lang)
        pfree((void *) lang);

    if (NULL != cbuffer)
        pfree((void *) cbuffer);

    if (NULL != converted_buf)
        pfree((void *) converted_buf);

    PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
}