1
0
Fork 0
mirror of https://github.com/mautrix/signal.git synced 2026-05-15 05:36:53 -04:00
mautrix-signal/pkg/libsignalgo/ciphertextmessage.go

93 lines
3 KiB
Go
Raw Permalink Normal View History

2023-12-17 15:54:35 +02:00
// mautrix-signal - A Matrix-signal puppeting bridge.
// Copyright (C) 2023 Sumner Evans
2025-01-17 17:49:37 +02:00
// Copyright (C) 2025 Tulir Asokan
2023-12-17 15:54:35 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package libsignalgo
/*
#include "./libsignal-ffi.h"
*/
import "C"
import "runtime"
type CiphertextMessageType uint8
const (
CiphertextMessageTypeWhisper CiphertextMessageType = 2
CiphertextMessageTypePreKey CiphertextMessageType = 3
CiphertextMessageTypeSenderKey CiphertextMessageType = 7
CiphertextMessageTypePlaintext CiphertextMessageType = 8
)
type CiphertextMessage struct {
nc noCopy
ptr *C.SignalCiphertextMessage
}
func wrapCiphertextMessage(ptr *C.SignalCiphertextMessage) *CiphertextMessage {
ciphertextMessage := &CiphertextMessage{ptr: ptr}
runtime.SetFinalizer(ciphertextMessage, (*CiphertextMessage).Destroy)
return ciphertextMessage
}
func NewCiphertextMessage(plaintext *PlaintextContent) (*CiphertextMessage, error) {
2025-01-17 17:49:37 +02:00
var ciphertextMessage C.SignalMutPointerCiphertextMessage
signalFfiError := C.signal_ciphertext_message_from_plaintext_content(
&ciphertextMessage,
plaintext.constPtr(),
)
if signalFfiError != nil {
return nil, wrapError(signalFfiError)
}
2025-01-17 17:49:37 +02:00
return wrapCiphertextMessage(ciphertextMessage.raw), nil
}
func (c *CiphertextMessage) mutPtr() C.SignalMutPointerCiphertextMessage {
return C.SignalMutPointerCiphertextMessage{c.ptr}
}
func (c *CiphertextMessage) constPtr() C.SignalConstPointerCiphertextMessage {
return C.SignalConstPointerCiphertextMessage{c.ptr}
}
func (c *CiphertextMessage) Destroy() error {
c.CancelFinalizer()
2025-01-17 17:49:37 +02:00
return wrapError(C.signal_ciphertext_message_destroy(c.mutPtr()))
}
func (c *CiphertextMessage) CancelFinalizer() {
runtime.SetFinalizer(c, nil)
}
func (c *CiphertextMessage) Serialize() ([]byte, error) {
var serialized C.SignalOwnedBuffer = C.SignalOwnedBuffer{}
2025-01-17 17:49:37 +02:00
signalFfiError := C.signal_ciphertext_message_serialize(&serialized, c.constPtr())
2024-01-03 20:31:27 +02:00
runtime.KeepAlive(c)
if signalFfiError != nil {
return nil, wrapError(signalFfiError)
}
return CopySignalOwnedBufferToBytes(serialized), nil
}
func (c *CiphertextMessage) MessageType() (CiphertextMessageType, error) {
var messageType C.uint8_t
2025-01-17 17:49:37 +02:00
signalFfiError := C.signal_ciphertext_message_type(&messageType, c.constPtr())
2024-01-03 20:31:27 +02:00
runtime.KeepAlive(c)
if signalFfiError != nil {
return 0, wrapError(signalFfiError)
}
return CiphertextMessageType(messageType), nil
}