escape_registry_key.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Escaping/unescaping functions to translate between a saved session
  3. * name, and the key name used to represent it in the Registry area
  4. * where we store saved sessions.
  5. *
  6. * The basic technique is to %-escape characters we can't use in
  7. * Registry keys.
  8. */
  9. #include "putty.h"
  10. void escape_registry_key(const char *in, strbuf *out)
  11. {
  12. bool candot = false;
  13. static const char hex[16] = "0123456789ABCDEF";
  14. while (*in) {
  15. if (*in == ' ' || *in == '\\' || *in == '*' || *in == '?' ||
  16. *in == '%' || *in < ' ' || *in > '~' || (*in == '.'
  17. && !candot)) {
  18. put_byte(out, '%');
  19. put_byte(out, hex[((unsigned char) *in) >> 4]);
  20. put_byte(out, hex[((unsigned char) *in) & 15]);
  21. } else
  22. put_byte(out, *in);
  23. in++;
  24. candot = true;
  25. }
  26. }
  27. void unescape_registry_key(const char *in, strbuf *out)
  28. {
  29. while (*in) {
  30. if (*in == '%' && in[1] && in[2]) {
  31. int i, j;
  32. i = in[1] - '0';
  33. i -= (i > 9 ? 7 : 0);
  34. j = in[2] - '0';
  35. j -= (j > 9 ? 7 : 0);
  36. put_byte(out, (i << 4) + j);
  37. in += 3;
  38. } else {
  39. put_byte(out, *in++);
  40. }
  41. }
  42. }