The `encoded_dup[temp.length-1] != "\\"` part won’t work. The problem here is to identify incomplete octal escapes. As `Rex::Text::to_octal` returns the shortest octal representation, an octal escape can be from 2 to 4 characters (i. e., `\0`…`\377`). That’s why I tried to find the last octal escape in the last four characters and removed it unless it was at the begin. But since the octal escapes can also be shorter than 4 characters, `index` might not get the last but the second last octal escape, especially when it started four characters before the end of the string. But `rindex` instead of `index` should do it:

          # remove the last octal escape if it may be imcomplete, i. e., shorter than 4 characters
          pos = temp.rindex('\\')
          if encoded_dup.length > temp.length and pos > temp.length-4
            temp.slice!(pos..temp.length-1)
          end

What do you think?