flipbip_string.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 1988 Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include "flipbip_string.h"
  30. #include <ctype.h>
  31. #include <stdint.h>
  32. #include <string.h>
  33. // From: lib/crypto
  34. #include <memzero.h>
  35. #include <rc4.h>
  36. char* flipbip_strtok(char* s, const char* delim) {
  37. static char* last;
  38. return flipbip_strtok_r(s, delim, &last);
  39. }
  40. char* flipbip_strtok_r(char* s, const char* delim, char** last) {
  41. char* spanp;
  42. int c, sc;
  43. char* tok;
  44. if(s == NULL && (s = *last) == NULL) return (NULL);
  45. /*
  46. * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
  47. */
  48. cont:
  49. c = *s++;
  50. for(spanp = (char*)delim; (sc = *spanp++) != 0;) {
  51. if(c == sc) goto cont;
  52. }
  53. if(c == 0) { /* no non-delimiter characters */
  54. *last = NULL;
  55. return (NULL);
  56. }
  57. tok = s - 1;
  58. /*
  59. * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
  60. * Note that delim must have one NUL; we stop if we see that, too.
  61. */
  62. for(;;) {
  63. c = *s++;
  64. spanp = (char*)delim;
  65. do {
  66. if((sc = *spanp++) == c) {
  67. if(c == 0)
  68. s = NULL;
  69. else
  70. s[-1] = 0;
  71. *last = s;
  72. return (tok);
  73. }
  74. } while(sc != 0);
  75. }
  76. /* NOTREACHED */
  77. }
  78. void flipbip_btox(const unsigned char* in, int in_len, char* str) {
  79. for(int i = 0; i < in_len; i++) {
  80. unsigned char n;
  81. unsigned char x = in[i];
  82. str += 2;
  83. *(str + (i * 2)) = '\0';
  84. for(n = 2; n != 0; --n) {
  85. *(--str + (i * 2)) = "0123456789abcdef"[x & 0x0F];
  86. x >>= 4;
  87. }
  88. }
  89. }
  90. void flipbip_xtob(const char* str, unsigned char* out, int out_len) {
  91. int len = strlen(str) / 2;
  92. if(len > out_len) len = out_len;
  93. for(int i = 0; i < len; i++) {
  94. char c = 0;
  95. if(str[i * 2] >= '0' && str[i * 2] <= '9') c += (str[i * 2] - '0') << 4;
  96. if((str[i * 2] & ~0x20) >= 'A' && (str[i * 2] & ~0x20) <= 'F')
  97. c += (10 + (str[i * 2] & ~0x20) - 'A') << 4;
  98. if(str[i * 2 + 1] >= '0' && str[i * 2 + 1] <= '9') c += (str[i * 2 + 1] - '0');
  99. if((str[i * 2 + 1] & ~0x20) >= 'A' && (str[i * 2 + 1] & ~0x20) <= 'F')
  100. c += (10 + (str[i * 2 + 1] & ~0x20) - 'A');
  101. out[i] = c;
  102. }
  103. }
  104. void flipbip_cipher(
  105. const unsigned char* key_in,
  106. const unsigned int key_len,
  107. const char* in,
  108. char* out,
  109. const unsigned int io_len) {
  110. if(io_len > 512) return;
  111. RC4_CTX ctx;
  112. uint8_t buf[256];
  113. memzero(buf, 256);
  114. flipbip_xtob(in, buf, io_len / 2);
  115. rc4_init(&ctx, key_in, key_len);
  116. rc4_encrypt(&ctx, buf, 256);
  117. flipbip_btox(buf, io_len / 2, out);
  118. memzero(buf, 256);
  119. }