func main() { var fname string if len(os.Args) != 2 { fname = "test.txt" } else { fname = os.Args[1] } f, err := os.Open(fname) if err != nil { fmt.Printf("Error:%v\n", err) return } defer f.Close() rf := bufio.NewReader(f) for { s, err := rf.ReadString('\n') if err != nil && err != io.EOF { fmt.Printf("Error:%v\n", err) return } if s == "" { break } sbak := make([]byte, len(s)*2) iLen := 0 if err := iconv.ConvertGBKToUTF8([]byte(s), len(s), sbak, &iLen); err != nil { // if err := iconv.ConvertUTF8ToGBK([]byte(s), len(s), sbak, &iLen); err != nil { fmt.Printf("Error:%v\n", err) return } fmt.Printf("%s", string(sbak[:iLen])) } }
func main() { var rf *os.File var iLen int var Ver byte if len(os.Args) != 2 { fmt.Printf("Please input mp3 filename\n") return } if path.Ext(os.Args[1]) != ".mp3" { fmt.Printf("Do not support this file format [%s]\n", path.Ext(os.Args[1])) return } if v, err := os.Open(os.Args[1]); err != nil { fmt.Printf("Error: %v\n", err) } else { rf = v } defer rf.Close() buffer := make([]byte, 10) if n, err := rf.Read(buffer); err != nil { fmt.Printf("Error: %v\n", err) return } else { iLen = n } Ver = '1' if isAtHeadOfMP3(buffer[:iLen]) { Ver = '2' } fi, err := rf.Stat() if err != nil { fmt.Printf("Error: %v\n", err) return } if Ver == '2' { if buffer[3] != 0x03 { fmt.Printf("Only support Version IDV3 Version 2.3\n") return } iLen := uint32(buffer[6]&0x7f)<<21 | uint32(buffer[7]&0x7f)<<14 | uint32(buffer[8]&0x7f)<<7 | uint32(buffer[9]&0x7f) - 10 bufferIn := make([]byte, int(iLen)) if n, err := rf.Read(bufferIn); err != nil { fmt.Printf("Error: %v\n", err) return } else { if n != len(bufferIn) { fmt.Printf("Error: File is Bad\n") return } i := 0 iCount := 0 // println("#####", n, "#####") // k := 0 for i < n { if bufferIn[i] == 0x0 { i++ continue } j := i + 4 iCount = i iLen := uint32(bufferIn[j])<<24 | uint32(bufferIn[j+1])<<16 | uint32(bufferIn[j+2])<<8 | uint32(bufferIn[j+3]) x := string(bufferIn[iCount : iCount+4]) tLen := int(iLen) bufferTmp := bytes.TrimLeftFunc(bufferIn[i+10:i+10+tLen], func(r rune) bool { return r == 0x00 || r == 0x01 }) if bufferTmp == nil { i += 10 + tLen continue } switch x { case "TIT2": outLen := 0 bufferOut := make([]byte, len(bufferTmp)*4) if err := iconv.ConvertGBKToUTF8(bufferTmp, len(bufferTmp), bufferOut, &outLen); err != nil { byteCopy(bufferOut, bufferTmp, len(bufferTmp)) outLen = len(bufferTmp) } fmt.Printf("标题: %s\n", string(bufferOut[:outLen])) case "TPE1": outLen := 0 bufferOut := make([]byte, len(bufferTmp)*4) if err := iconv.ConvertGBKToUTF8(bufferTmp, len(bufferTmp), bufferOut, &outLen); err != nil { byteCopy(bufferOut, bufferTmp, len(bufferTmp)) outLen = len(bufferTmp) } fmt.Printf("作者: %s\n", string(bufferOut[:outLen])) case "TALB": outLen := 0 bufferOut := make([]byte, len(bufferTmp)*4) if err := iconv.ConvertGBKToUTF8(bufferTmp, len(bufferTmp), bufferOut, &outLen); err != nil { byteCopy(bufferOut, bufferTmp, len(bufferTmp)) outLen = len(bufferTmp) } fmt.Printf("专辑名: %s\n", string(bufferOut[:outLen])) case "TYER": outLen := 0 bufferOut := make([]byte, len(bufferTmp)*4) if err := iconv.ConvertGBKToUTF8(bufferTmp, len(bufferTmp), bufferOut, &outLen); err != nil { byteCopy(bufferOut, bufferTmp, len(bufferTmp)) outLen = len(bufferTmp) } fmt.Printf("发行年份: %s\n", string(bufferOut[:outLen])) case "TCON": outLen := 0 bufferOut := make([]byte, len(bufferTmp)*4) if err := iconv.ConvertGBKToUTF8(bufferTmp, len(bufferTmp), bufferOut, &outLen); err != nil { byteCopy(bufferOut, bufferTmp, len(bufferTmp)) outLen = len(bufferTmp) } fmt.Printf("类型: %s\n", string(bufferOut[:outLen])) case "COMM": outLen := 0 bufferOut := make([]byte, len(bufferTmp)*4) if err := iconv.ConvertGBKToUTF8(bufferTmp, len(bufferTmp), bufferOut, &outLen); err != nil { byteCopy(bufferOut, bufferTmp, len(bufferTmp)) outLen = len(bufferTmp) } fmt.Printf("备注信息: %s\n", string(bufferOut[:outLen])) } i += 10 + tLen /*println(i, "|", iLen) k++ if k == 25 { break }*/ } } } else { lPos := fi.Size() - 128 bufferIn := make([]byte, 128) byteSet(bufferIn, 0, len(bufferIn)) bufferOut := make([]byte, 1024) if n, err := rf.ReadAt(bufferIn, lPos); err != nil { fmt.Printf("Error: %v\n", err) return } else { iLen = n } if !bytes.Equal(bufferIn[:3], []byte("TAG")) { fmt.Printf("This file no idv3 information\n") return } outLen := 0 if err := iconv.ConvertGBKToUTF8(bufferIn[3:33], 30, bufferOut, &outLen); err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("标题: %s\n", string(bufferOut[:outLen])) outLen = 0 byteSet(bufferOut, 0, len(bufferOut)) if err := iconv.ConvertGBKToUTF8(bufferIn[33:63], 30, bufferOut, &outLen); err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("演唱/奏者: %s\n", string(bufferOut[:outLen])) outLen = 0 byteSet(bufferOut, 0, len(bufferOut)) if err := iconv.ConvertGBKToUTF8(bufferIn[63:93], 30, bufferOut, &outLen); err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("专辑名: %s\n", string(bufferOut[:outLen])) outLen = 0 byteSet(bufferOut, 0, len(bufferOut)) if err := iconv.ConvertGBKToUTF8(bufferIn[93:97], 4, bufferOut, &outLen); err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("发行年份: %s\n", string(bufferOut[:outLen])) outLen = 0 byteSet(bufferOut, 0, len(bufferOut)) if err := iconv.ConvertGBKToUTF8(bufferIn[97:127], 30, bufferOut, &outLen); err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("备注信息: %s\n", string(bufferOut[:outLen])) if v, ok := LPInfo[bufferIn[127]]; !ok { fmt.Printf("流派: %s\n", "未知") } else { fmt.Printf("流派: %s\n", v) } } }
func main() { tcp_chan := make(chan bool) udp_chan := make(chan bool) // 初始化日志打印级别 hlog.InitLogLevel(hlog.DEBUG_LEVEL) // 初始化日志 myLog := hlog.InitLog("FQ.log") myLog.ChgLogFuncStyle(hlog.SHORT_FUNC) go func(myLog Logger, udp_chan chan bool) { defer func(c chan bool) { c <- true }(udp_chan) uc, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4zero, Port: 2425}) if err != nil { fmt.Printf("Error:%v\n", err) return } defer uc.Close() myLog.WriteLog(hlog.DEBUG_LEVEL, "UDP Listen 2425 success", hlog.RPT_TO_FILE, nil, 0) buffer := make([]byte, 4096) recvBuffer := make([]byte, 8192) for { n, raddr, err := uc.ReadFrom(buffer) if err != nil { myLog.WriteLog(hlog.ERR_LEVEL, err.Error(), hlog.RPT_TO_FILE, nil, 0) break } iLen := 0 if err := iconv.ConvertGBKToUTF8(buffer, n, recvBuffer, &iLen); err != nil { myLog.WriteLog(hlog.ERR_LEVEL, err.Error(), hlog.RPT_TO_FILE, buffer, n) break } myLog.WriteLog(hlog.DEBUG_LEVEL, fmt.Sprintf("UDP收到%v", raddr), hlog.RPT_TO_FILE, recvBuffer, iLen) byteSet(buffer, 0, len(buffer)) byteSet(recvBuffer, 0, len(recvBuffer)) } }(myLog, udp_chan) go func(myLog Logger, tcp_chan chan bool) { defer func(c chan bool) { c <- true }(tcp_chan) tc, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IPv4zero, Port: 2425}) if err != nil { fmt.Printf("Error:%v\n", err) return } defer tc.Close() myLog.WriteLog(hlog.DEBUG_LEVEL, "TCP Listen 2425 success", hlog.RPT_TO_FILE, nil, 0) for { if client, err := tc.AcceptTCP(); err != nil { myLog.WriteLog(hlog.ERR_LEVEL, err.Error(), hlog.RPT_TO_FILE, nil, 0) break } else { go func(cln *net.TCPConn) { defer cln.Close() buffer := make([]byte, 4096) for { n, err := cln.Read(buffer) if err != nil { if err == io.EOF { myLog.WriteLog(hlog.ERR_LEVEL, "对方关闭了连接", hlog.RPT_TO_FILE, nil, 0) } else { myLog.WriteLog(hlog.ERR_LEVEL, err.Error(), hlog.RPT_TO_FILE, nil, 0) } break } myLog.WriteLog(hlog.DEBUG_LEVEL, fmt.Sprintf("TCP收到%v", cln.RemoteAddr()), hlog.RPT_TO_FILE, buffer, n) if _, err := cln.Write(buffer[:n]); err != nil { myLog.WriteLog(hlog.ERR_LEVEL, err.Error(), hlog.RPT_TO_FILE, nil, 0) break } byteSet(buffer, 0, len(buffer)) } }(client) } } }(myLog, tcp_chan) iCount := 0 for iCount < 2 { select { case c := <-udp_chan: if c { myLog.WriteLog(hlog.ERR_LEVEL, "UDP Listen quit", hlog.RPT_TO_FILE, nil, 0) iCount++ } case c := <-tcp_chan: if c { myLog.WriteLog(hlog.ERR_LEVEL, "TCP Listen quit", hlog.RPT_TO_FILE, nil, 0) iCount++ } } } }