using System.Text.Json; namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; /// Disjoint local identifiers for mapped nodes. Decoding never establishes authorization. public static class ProjectedDataIds { public const string RawNamespaceUri = "https://zb.com/otopcua/mapped/raw"; public const string UnsNamespaceUri = "https://zb.com/otopcua/mapped/uns"; private const string Prefix = "p1."; private const int MaximumIdentifierLength = 16_384; private sealed record Identity(string MappingId, DataAddress Origin); public static string NamespaceUri(DataRealm realm) => realm switch { DataRealm.Raw => RawNamespaceUri, DataRealm.Uns => UnsNamespaceUri, _ => throw new ArgumentOutOfRangeException(nameof(realm)) }; public static bool IsMappedNamespace(string namespaceUri) => namespaceUri is RawNamespaceUri or UnsNamespaceUri; /// A portable dependency key, distinct from ordinary unqualified local paths. public static string DependencyReference(DataRealm realm, string localIdentifier) { if (!TryDecodeIdentifier(localIdentifier, out _, out _)) throw new ArgumentException("Invalid projected identifier.", nameof(localIdentifier)); return "nsu=" + NamespaceUri(realm) + ";s=" + localIdentifier; } public static bool TryParseDependencyReference(string reference, out DataRealm realm, out string localIdentifier) { realm = DataRealm.Unspecified; localIdentifier = string.Empty; if (reference is null) return false; foreach (var candidate in new[] { DataRealm.Raw, DataRealm.Uns }) { var prefix = "nsu=" + NamespaceUri(candidate) + ";s="; if (!reference.StartsWith(prefix, StringComparison.Ordinal)) continue; var identifier = reference[prefix.Length..]; if (!TryDecodeIdentifier(identifier, out _, out _)) return false; realm = candidate; localIdentifier = identifier; return true; } return false; } public static string Identifier(string mappingId, DataAddress origin) { ArgumentException.ThrowIfNullOrWhiteSpace(mappingId); if (mappingId.Length > 256) throw new ArgumentOutOfRangeException(nameof(mappingId)); ArgumentNullException.ThrowIfNull(origin); origin.Validate(); var payload = JsonSerializer.SerializeToUtf8Bytes(new Identity(mappingId, origin)); if (payload.Length > (MaximumIdentifierLength - Prefix.Length) * 3 / 4) throw new ArgumentOutOfRangeException(nameof(origin)); return Prefix + Convert.ToBase64String(payload).TrimEnd('=').Replace('+', '-').Replace('/', '_'); } public static bool TryDecodeIdentifier(string identifier, out string mappingId, out DataAddress? origin) { mappingId = string.Empty; origin = null; if (identifier is null || identifier.Length > MaximumIdentifierLength || !identifier.StartsWith(Prefix, StringComparison.Ordinal)) return false; var encoded = identifier[Prefix.Length..]; if (encoded.Length == 0 || encoded.Any(c => !char.IsAsciiLetterOrDigit(c) && c is not '-' and not '_')) return false; try { var padded = encoded.Replace('-', '+').Replace('_', '/'); padded = padded.PadRight((padded.Length + 3) / 4 * 4, '='); var parsed = JsonSerializer.Deserialize(Convert.FromBase64String(padded)); if (parsed?.Origin is null || !string.Equals(Identifier(parsed.MappingId, parsed.Origin), identifier, StringComparison.Ordinal)) return false; mappingId = parsed.MappingId; origin = parsed.Origin; return true; } catch (Exception error) when (error is JsonException or FormatException or ArgumentException) { return false; } } }