key.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Key to pathname encoder
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/slab.h>
  12. #include "internal.h"
  13. static const char cachefiles_charmap[64] =
  14. "0123456789" /* 0 - 9 */
  15. "abcdefghijklmnopqrstuvwxyz" /* 10 - 35 */
  16. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" /* 36 - 61 */
  17. "_-" /* 62 - 63 */
  18. ;
  19. static const char cachefiles_filecharmap[256] = {
  20. /* we skip space and tab and control chars */
  21. [33 ... 46] = 1, /* '!' -> '.' */
  22. /* we skip '/' as it's significant to pathwalk */
  23. [48 ... 127] = 1, /* '0' -> '~' */
  24. };
  25. /*
  26. * turn the raw key into something cooked
  27. * - the raw key should include the length in the two bytes at the front
  28. * - the key may be up to 514 bytes in length (including the length word)
  29. * - "base64" encode the strange keys, mapping 3 bytes of raw to four of
  30. * cooked
  31. * - need to cut the cooked key into 252 char lengths (189 raw bytes)
  32. */
  33. char *cachefiles_cook_key(const u8 *raw, int keylen, uint8_t type)
  34. {
  35. unsigned char csum, ch;
  36. unsigned int acc;
  37. char *key;
  38. int loop, len, max, seg, mark, print;
  39. _enter(",%d", keylen);
  40. BUG_ON(keylen < 2 || keylen > 514);
  41. csum = raw[0] + raw[1];
  42. print = 1;
  43. for (loop = 2; loop < keylen; loop++) {
  44. ch = raw[loop];
  45. csum += ch;
  46. print &= cachefiles_filecharmap[ch];
  47. }
  48. if (print) {
  49. /* if the path is usable ASCII, then we render it directly */
  50. max = keylen - 2;
  51. max += 2; /* two base64'd length chars on the front */
  52. max += 5; /* @checksum/M */
  53. max += 3 * 2; /* maximum number of segment dividers (".../M")
  54. * is ((514 + 251) / 252) = 3
  55. */
  56. max += 1; /* NUL on end */
  57. } else {
  58. /* calculate the maximum length of the cooked key */
  59. keylen = (keylen + 2) / 3;
  60. max = keylen * 4;
  61. max += 5; /* @checksum/M */
  62. max += 3 * 2; /* maximum number of segment dividers (".../M")
  63. * is ((514 + 188) / 189) = 3
  64. */
  65. max += 1; /* NUL on end */
  66. }
  67. max += 1; /* 2nd NUL on end */
  68. _debug("max: %d", max);
  69. key = kmalloc(max, GFP_KERNEL);
  70. if (!key)
  71. return NULL;
  72. len = 0;
  73. /* build the cooked key */
  74. sprintf(key, "@%02x%c+", (unsigned) csum, 0);
  75. len = 5;
  76. mark = len - 1;
  77. if (print) {
  78. acc = *(uint16_t *) raw;
  79. raw += 2;
  80. key[len + 1] = cachefiles_charmap[acc & 63];
  81. acc >>= 6;
  82. key[len] = cachefiles_charmap[acc & 63];
  83. len += 2;
  84. seg = 250;
  85. for (loop = keylen; loop > 0; loop--) {
  86. if (seg <= 0) {
  87. key[len++] = '\0';
  88. mark = len;
  89. key[len++] = '+';
  90. seg = 252;
  91. }
  92. key[len++] = *raw++;
  93. ASSERT(len < max);
  94. }
  95. switch (type) {
  96. case FSCACHE_COOKIE_TYPE_INDEX: type = 'I'; break;
  97. case FSCACHE_COOKIE_TYPE_DATAFILE: type = 'D'; break;
  98. default: type = 'S'; break;
  99. }
  100. } else {
  101. seg = 252;
  102. for (loop = keylen; loop > 0; loop--) {
  103. if (seg <= 0) {
  104. key[len++] = '\0';
  105. mark = len;
  106. key[len++] = '+';
  107. seg = 252;
  108. }
  109. acc = *raw++;
  110. acc |= *raw++ << 8;
  111. acc |= *raw++ << 16;
  112. _debug("acc: %06x", acc);
  113. key[len++] = cachefiles_charmap[acc & 63];
  114. acc >>= 6;
  115. key[len++] = cachefiles_charmap[acc & 63];
  116. acc >>= 6;
  117. key[len++] = cachefiles_charmap[acc & 63];
  118. acc >>= 6;
  119. key[len++] = cachefiles_charmap[acc & 63];
  120. ASSERT(len < max);
  121. }
  122. switch (type) {
  123. case FSCACHE_COOKIE_TYPE_INDEX: type = 'J'; break;
  124. case FSCACHE_COOKIE_TYPE_DATAFILE: type = 'E'; break;
  125. default: type = 'T'; break;
  126. }
  127. }
  128. key[mark] = type;
  129. key[len++] = 0;
  130. key[len] = 0;
  131. _leave(" = %p %d", key, len);
  132. return key;
  133. }