PageRenderTime 92ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/sys/vfs/isofs/cd9660/cd9660_util.c

https://gitlab.com/HowTheStoryEnds/freebsd11-psm-port
C | 216 lines | 146 code | 16 blank | 54 comment | 53 complexity | 559c44a2bbb16f3e026eb1984140f73e MD5 | raw file
  1. /*-
  2. * Copyright (c) 1994
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley
  6. * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
  7. * Support code is derived from software contributed to Berkeley
  8. * by Atsushi Murai (amurai@spec.co.jp). Joliet support was added by
  9. * Joachim Kuebart (joki@kuebart.stuttgart.netsurf.de).
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * @(#)cd9660_util.c 8.3 (Berkeley) 12/5/94
  36. * $FreeBSD: src/sys/isofs/cd9660/cd9660_util.c,v 1.13.2.1 2001/02/27 12:36:34 sobomax Exp $
  37. */
  38. #include <sys/param.h>
  39. #include <sys/mount.h>
  40. #include <sys/vnode.h>
  41. #include <sys/iconv.h>
  42. #include "iso.h"
  43. #include "cd9660_mount.h"
  44. extern struct iconv_functions *cd9660_iconv;
  45. /*
  46. * Get one character out of an iso filename
  47. * Obey joliet_level
  48. * Return number of bytes consumed
  49. */
  50. int
  51. isochar(u_char *isofn, u_char *isoend, int joliet_level, u_short *c,
  52. int *clen, int flags, void *handle)
  53. {
  54. size_t i, j, len;
  55. char inbuf[3], outbuf[3], *inp, *outp;
  56. *c = *isofn++;
  57. if (clen) *clen = 1;
  58. if (joliet_level == 0 || isofn == isoend) {
  59. /* (00) and (01) are one byte in Joliet, too */
  60. return 1;
  61. }
  62. if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
  63. i = j = len = 2;
  64. inbuf[0]=(char)*(isofn - 1);
  65. inbuf[1]=(char)*isofn;
  66. inbuf[2]='\0';
  67. inp = inbuf;
  68. outp = outbuf;
  69. cd9660_iconv->convchr(handle, (const char **)(void *)&inp,
  70. &i, &outp, &j);
  71. len -= j;
  72. if (clen) *clen = len;
  73. *c = '\0';
  74. while(len--)
  75. *c |= (*(outp - len - 1) & 0xff) << (len << 3);
  76. } else {
  77. switch (*c) {
  78. default:
  79. *c = '?';
  80. break;
  81. case '\0':
  82. *c = *isofn;
  83. break;
  84. }
  85. }
  86. return 2;
  87. }
  88. /*
  89. * translate and compare a filename
  90. * returns (fn - isofn)
  91. * Note: Version number plus ';' may be omitted.
  92. */
  93. int
  94. isofncmp(u_char *fn, int fnlen, u_char *isofn, int isolen, int joliet_level,
  95. int flags, void *handle, void *lhandle)
  96. {
  97. int i, j;
  98. u_short c, d;
  99. u_char *fnend = fn + fnlen, *isoend = isofn + isolen;
  100. for (; fn < fnend; ) {
  101. d = sgetrune(fn, fnend - fn, (char const **)&fn, flags, lhandle);
  102. if (isofn == isoend)
  103. return d;
  104. isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
  105. if (c == ';') {
  106. if (d != ';')
  107. return d;
  108. for (i = 0; fn < fnend; i = i * 10 + *fn++ - '0') {
  109. if (*fn < '0' || *fn > '9') {
  110. return -1;
  111. }
  112. }
  113. for (j = 0; isofn != isoend; j = j * 10 + c - '0')
  114. isofn += isochar(isofn, isoend,
  115. joliet_level, &c,
  116. NULL, flags, handle);
  117. return i - j;
  118. }
  119. if (c != d) {
  120. if (c >= 'A' && c <= 'Z') {
  121. if (c + ('a' - 'A') != d) {
  122. if (d >= 'a' && d <= 'z')
  123. return d - ('a' - 'A') - c;
  124. else
  125. return d - c;
  126. }
  127. } else
  128. return d - c;
  129. }
  130. }
  131. if (isofn != isoend) {
  132. isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
  133. switch (c) {
  134. default:
  135. return -c;
  136. case '.':
  137. if (isofn != isoend) {
  138. isochar(isofn, isoend, joliet_level, &c,
  139. NULL, flags, handle);
  140. if (c == ';')
  141. return 0;
  142. }
  143. return -1;
  144. case ';':
  145. return 0;
  146. }
  147. }
  148. return 0;
  149. }
  150. /*
  151. * translate a filename of length > 0
  152. */
  153. void
  154. isofntrans(u_char *infn, int infnlen, u_char *outfn, u_short *outfnlen, int original,
  155. int assoc, int joliet_level, int flags, void *handle)
  156. {
  157. u_short c, d = '\0';
  158. u_char *outp = outfn, *infnend = infn + infnlen;
  159. int clen;
  160. if (assoc) {
  161. *outp++ = ASSOCCHAR;
  162. }
  163. for (; infn != infnend; ) {
  164. infn += isochar(infn, infnend, joliet_level, &c, &clen, flags, handle);
  165. if (!original && !joliet_level && c >= 'A' && c <= 'Z')
  166. c += ('a' - 'A');
  167. else if (!original && c == ';') {
  168. outp -= (d == '.');
  169. break;
  170. }
  171. d = c;
  172. while(clen--)
  173. *outp++ = c >> (clen << 3);
  174. }
  175. *outfnlen = outp - outfn;
  176. }
  177. /*
  178. * same as sgetrune(3)
  179. */
  180. u_short
  181. sgetrune(const char *string, size_t n, char const **result,
  182. int flags, void *handle)
  183. {
  184. size_t i, j, len;
  185. char outbuf[3], *outp;
  186. u_short c = '\0';
  187. len = i = (n < 2) ? n : 2;
  188. j = 2;
  189. outp = outbuf;
  190. if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
  191. cd9660_iconv->convchr(handle, &string, &i, &outp, &j);
  192. len -= i;
  193. } else {
  194. len = 1;
  195. string++;
  196. }
  197. if (result) *result = string;
  198. while(len--) c |= (*(string - len - 1) & 0xff) << (len << 3);
  199. return (c);
  200. }