** Copyright (C) 2001-2025 Zabbix SIA
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//size_t call_iconv(iconv_t cd, char *inbuf, size_t *inbytesleft, char *outbuf, size_t *outbytesleft) {
// return iconv(cd, &inbuf, inbytesleft, &outbuf, outbytesleft);
// #cgo windows LDFLAGS: -liconv
"golang.zabbix.com/sdk/log"
func findEncodingFromBOM(encoding string, inbuf []byte, bytecount int) string {
/* try to guess encoding using Byte Order Mark (BOM) if it exists */
if bytecount > 3 && 0xef == inbuf[0] && 0xbb == inbuf[1] && 0xbf == inbuf[2] {
} else if bytecount > 2 && 0xff == inbuf[0] && 0xfe == inbuf[1] {
} else if bytecount > 2 && 0xfe == inbuf[0] && 0xff == inbuf[1] {
func decodeToUTF8(encoding string, inbuf []byte, bytecount int) (outbuf []byte, outbytecount int) {
tocode := C.CString("UTF-8")
log.Tracef("Calling C function \"free()\"")
defer C.free(unsafe.Pointer(tocode))
fromcode := C.CString(encoding)