From 18e1e7fc456e1834f249ffa533aad69c6b34263e Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Fri, 31 Aug 2018 11:24:42 +0930 Subject: [PATCH] rtmp: simplify indexing This is a mechanical change with the addition of a constant: gofmt -w -r '*indxBytePtr(unsafe.Pointer(p), i)->(*[_Gi]byte)(unsafe.Pointer(p))[i]' {rtmp,parseurl,amf}.go --- rtmp/amf.go | 16 ++++++++-------- rtmp/parseurl.go | 4 ++-- rtmp/rtmp.go | 35 ++++++++++++++++++----------------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/rtmp/amf.go b/rtmp/amf.go index 5745ff70..5f3b6ac6 100644 --- a/rtmp/amf.go +++ b/rtmp/amf.go @@ -121,7 +121,7 @@ func C_AMF_DecodeNumber(data *byte) float64 { ci = (*uint8)(unsafe.Pointer(data)) co = (*uint8)(unsafe.Pointer(&dVal)) for i := 0; i < 8; i++ { - *indxBytePtr(unsafe.Pointer(co), i) = *indxBytePtr(unsafe.Pointer(ci), 7-i) + (*[_Gi]byte)(unsafe.Pointer(co))[i] = (*[_Gi]byte)(unsafe.Pointer(ci))[7-i] } return dVal } @@ -242,7 +242,7 @@ func C_AMF_EncodeNumber(output *byte, outend *byte, dVal float64) *byte { ci = (*uint8)(unsafe.Pointer(&dVal)) co = (*uint8)(unsafe.Pointer(output)) for i := 0; i < 8; i++ { - *indxBytePtr(unsafe.Pointer(co), i) = *indxBytePtr(unsafe.Pointer(ci), 7-i) + (*[_Gi]byte)(unsafe.Pointer(co))[i] = (*[_Gi]byte)(unsafe.Pointer(ci))[7-i] } return (*byte)(incBytePtr(unsafe.Pointer(output), 8)) } @@ -346,9 +346,9 @@ func C_AMF_PropEncode(p *C_AMFObjectProperty, pBuffer *byte, pBufEnd *byte) *byt } if p.p_type != AMF_NULL && p.p_name.av_len != 0 { - *indxBytePtr(unsafe.Pointer(pBuffer), 0) = byte(p.p_name.av_len >> 8) + (*[_Gi]byte)(unsafe.Pointer(pBuffer))[0] = byte(p.p_name.av_len >> 8) pBuffer = (*byte)(incBytePtr(unsafe.Pointer(pBuffer), 1)) - *indxBytePtr(unsafe.Pointer(pBuffer), 0) = byte(p.p_name.av_len & 0xff) + (*[_Gi]byte)(unsafe.Pointer(pBuffer))[0] = byte(p.p_name.av_len & 0xff) pBuffer = (*byte)(incBytePtr(unsafe.Pointer(pBuffer), 1)) memmove(unsafe.Pointer(pBuffer), unsafe.Pointer(p.p_name.av_val), uintptr(p.p_name.av_len)) @@ -399,10 +399,10 @@ func C_AMF3ReadInteger(data *byte, valp *int32) int32 { for i <= 2 { /* handle first 3 bytes */ - if *indxBytePtr(unsafe.Pointer(data), i)&0x80 != 0 { + if (*[_Gi]byte)(unsafe.Pointer(data))[i]&0x80 != 0 { /* byte used */ val <<= 7 /* shift up */ - val |= int32(*indxBytePtr(unsafe.Pointer(data), i) & 0x7f) /* add bits */ + val |= int32((*[_Gi]byte)(unsafe.Pointer(data))[i] & 0x7f) /* add bits */ i++ } else { break @@ -412,7 +412,7 @@ func C_AMF3ReadInteger(data *byte, valp *int32) int32 { if i > 2 { /* use 4th byte, all 8bits */ val <<= 8 - val |= int32(*indxBytePtr(unsafe.Pointer(data), 3)) + val |= int32((*[_Gi]byte)(unsafe.Pointer(data))[3]) /* range check */ if val > AMF3_INTEGER_MAX { @@ -421,7 +421,7 @@ func C_AMF3ReadInteger(data *byte, valp *int32) int32 { } else { /* use 7bits of last unparsed byte (0xxxxxxx) */ val <<= 7 - val |= int32(*indxBytePtr(unsafe.Pointer(data), i)) + val |= int32((*[_Gi]byte)(unsafe.Pointer(data))[i]) } *valp = val diff --git a/rtmp/parseurl.go b/rtmp/parseurl.go index 06b871db..cd85e2ca 100644 --- a/rtmp/parseurl.go +++ b/rtmp/parseurl.go @@ -316,12 +316,12 @@ func C_RTMP_ParsePlaypath(in, out *C_AVal) { if *p == '%' { var c uint32 fmt.Sscanf(cStrToGoStr((*byte)(incBytePtr(unsafe.Pointer(p), 1))), "%02x", &c) - *indxBytePtr(unsafe.Pointer(destptr), 0) = byte(c) + (*[_Gi]byte)(unsafe.Pointer(destptr))[0] = byte(c) destptr = (*byte)(incBytePtr(unsafe.Pointer(destptr), 1)) pplen -= 3 p = (*byte)(incBytePtr(unsafe.Pointer(p), 3)) } else { - *indxBytePtr(unsafe.Pointer(destptr), 0) = *p + (*[_Gi]byte)(unsafe.Pointer(destptr))[0] = *p destptr = (*byte)(incBytePtr(unsafe.Pointer(destptr), 1)) p = (*byte)(incBytePtr(unsafe.Pointer(p), 1)) pplen-- diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 119e3a43..f33981ca 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -58,6 +58,8 @@ import ( "github.com/chamaken/cgolmnl/inet" ) +const _Gi = 1 << 30 + const ( minDataSize = 11 debugMode = false @@ -274,9 +276,8 @@ func C_SocksSetup(r *C_RTMP, sockshost *C_AVal) { hostname := strdup((*byte)(unsafe.Pointer(sockshost.av_val))) if unsafe.Pointer(socksport) != nil { - *indxBytePtr(unsafe.Pointer(hostname), - int(uintptr(decBytePtr(unsafe.Pointer(socksport), - int(uintptr(unsafe.Pointer(sockshost.av_val))))))) = '\000' + (*[_Gi]byte)(unsafe.Pointer(hostname))[int(uintptr(decBytePtr(unsafe.Pointer(socksport), + int(uintptr(unsafe.Pointer(sockshost.av_val))))))] = '\000' r.Link.sockshost.av_val = (*byte)(unsafe.Pointer(hostname)) r.Link.sockshost.av_len = int32(strlen(hostname)) @@ -775,7 +776,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { //(*byte)(unsafe.Pointer(pend)), float64(r.m_numInvokes)))) enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) - *indxBytePtr(unsafe.Pointer(enc), 0) = AMF_OBJECT + (*[_Gi]byte)(unsafe.Pointer(enc))[0] = AMF_OBJECT enc = (*byte)(unsafe.Pointer(incBytePtr(unsafe.Pointer(enc), 1))) @@ -876,11 +877,11 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { return 0 } - *indxBytePtr(unsafe.Pointer(enc), 0) = 0 + (*[_Gi]byte)(unsafe.Pointer(enc))[0] = 0 enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - *indxBytePtr(unsafe.Pointer(enc), 0) = 0 + (*[_Gi]byte)(unsafe.Pointer(enc))[0] = 0 enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - *indxBytePtr(unsafe.Pointer(enc), 0) = AMF_OBJECT_END + (*[_Gi]byte)(unsafe.Pointer(enc))[0] = AMF_OBJECT_END enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) /* add auth string */ @@ -1390,7 +1391,7 @@ func C_HandleClientBW(r *C_RTMP, packet *C_RTMPPacket) { //r.m_nClientBW = int32(C.AMF_DecodeInt32((*byte)(unsafe.Pointer(packet.m_body)))) if packet.m_nBodySize > 4 { - r.m_nClientBW2 = (uint8)(*indxBytePtr(unsafe.Pointer(packet.m_body), 4)) + r.m_nClientBW2 = (uint8)((*[_Gi]byte)(unsafe.Pointer(packet.m_body))[4]) } else { r.m_nClientBW2 = 255 } @@ -1403,9 +1404,9 @@ func C_HandleClientBW(r *C_RTMP, packet *C_RTMPPacket) { // rtmp.c +3527 func C_DecodeInt32LE(data *byte) int32 { var c *uint8 = (*uint8)(data) - return int32((*indxBytePtr(unsafe.Pointer(c), 3) << 24) | - (*indxBytePtr(unsafe.Pointer(c), 2) << 16) | - (*indxBytePtr(unsafe.Pointer(c), 1) << 8) | + return int32(((*[_Gi]byte)(unsafe.Pointer(c))[3] << 24) | + ((*[_Gi]byte)(unsafe.Pointer(c))[2] << 16) | + ((*[_Gi]byte)(unsafe.Pointer(c))[1] << 8) | *c) } @@ -1414,11 +1415,11 @@ func C_DecodeInt32LE(data *byte) int32 { func C_EncodeInt32LE(output *byte, nVal int32) int32 { *output = byte(nVal) nVal >>= 8 - *indxBytePtr(unsafe.Pointer(output), 1) = byte(nVal) + (*[_Gi]byte)(unsafe.Pointer(output))[1] = byte(nVal) nVal >>= 8 - *indxBytePtr(unsafe.Pointer(output), 2) = byte(nVal) + (*[_Gi]byte)(unsafe.Pointer(output))[2] = byte(nVal) nVal >>= 8 - *indxBytePtr(unsafe.Pointer(output), 3) = byte(nVal) + (*[_Gi]byte)(unsafe.Pointer(output))[3] = byte(nVal) return 4 } @@ -1537,7 +1538,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { packet.m_nBytesRead = 0 if nSize > 6 { - packet.m_packetType = uint8(*indxBytePtr(unsafe.Pointer(header), 6)) + packet.m_packetType = uint8((*[_Gi]byte)(unsafe.Pointer(header))[6]) if nSize == 11 { // TODO: port this @@ -1667,7 +1668,7 @@ func C_HandShake(r *C_RTMP, FP9HandShake int32) int { memset(indxBytePtr(unsafe.Pointer(clientsig), 4), 0, 4) for i := 8; i < RTMP_SIG_SIZE; i++ { - *indxBytePtr(unsafe.Pointer(clientsig), i) = byte(rand.Intn(256)) + (*[_Gi]byte)(unsafe.Pointer(clientsig))[i] = byte(rand.Intn(256)) } if C_WriteN(r, unsafe.Pointer(&clientbuf[0]), RTMP_SIG_SIZE+1) == 0 { @@ -2235,7 +2236,7 @@ func memcmp(a, b unsafe.Pointer, size int) int { func memset(ptr *byte, val int, num int) { for i := 0; i < num; i++ { - *indxBytePtr(unsafe.Pointer(ptr), int(i)) = byte(uint8(val)) + (*[_Gi]byte)(unsafe.Pointer(ptr))[int(i)] = byte(uint8(val)) } }