Attribute VB_Name = "CodeGenerator"
Option Explicit

Public Const HDBARCODE_PORT As Long = 8877
Public Const UDP_QUERY      As String = "HDBARCODE?"
Public Const UDP_RESPONSE   As String = "HDBARCODE="

Public Enum SpecialPacketBytes
  EndPacketByte = &HAA
  EscPacketByte = &HAB
End Enum

Public Enum CodeGeneratorCommandCodes
  SetGeometry = &H11
  SetText = &H21
  AppendText = &H22
  SetBinary = &H31
  AppendBinary = &H32
  SetSecretPhrase = &H41
  SetIdentificationTag = &H42
  GeneratePublic = &H51
  GenerateCompanyPrivate = &H52
  GeneratePermanentAuth = &H53
  GenerateTemporaryAuth = &H54
  RetrieveFirstLine = &H61
  RetrieveNextLine = &H62
End Enum

Public Enum CodeGeneratorErrorCodes
  NoError = &H0
  CommandCodeNotValid = &H1
  TooLittlePacketData = &H2
  TooMuchPacketData = &H3
  InvalidTextString = &H4
  OutOfMemory = &H5
  KeyCardHasExpired = &H6
  RowsNotValid = &H11
  ColumnsNotValid = &H12
  ErrorCorrectionNotValid = &H13
  GridSizeNotValid = &H14
  StyleNotValid = &H15
  ThicknessNotValid = &H16
  FileTypeNotValid = &H31
  GeometryNotSet = &H51
  DataToEncodeNotSet = &H52
  SecretPhraseNotSet = &H53
  TooMuchDataForGeometry = &H54
  BitmapNotSet = &H61
  NoMoreLines = &H62
End Enum

Public Function StreamToPacket(Stream As String) As String

Dim Scan As Long
Dim Result As String

Result = Replace(Stream, Chr(EscPacketByte), Chr(EscPacketByte) & Chr(EscPacketByte Xor 255))
Result = Replace(Result, Chr(EndPacketByte), Chr(EscPacketByte) & Chr(EndPacketByte Xor 255))
StreamToPacket = Result & Chr(EndPacketByte)

'For Scan = 1 To Len(Result)
'  Debug.Print Right("0" & Hex(Asc(Mid(Result, Scan, 1))), 2); " ";
'Next Scan
'Debug.Print

End Function
