PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/src/freebsd/sys/fs/cd9660/cd9660_util.c

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
C | 243 lines | 173 code | 17 blank | 53 comment | 53 complexity | e60b083c3432ae77e04e439b482453bd MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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. * 4. 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. */
  37. #include <sys/cdefs.h>
  38. __FBSDID("$FreeBSD$");
  39. #include <sys/param.h>
  40. #include <sys/systm.h>
  41. #include <sys/mount.h>
  42. #include <sys/vnode.h>
  43. #include <sys/iconv.h>
  44. #include <fs/cd9660/iso.h>
  45. #include <fs/cd9660/cd9660_mount.h>
  46. extern struct iconv_functions *cd9660_iconv;
  47. /*
  48. * Get one character out of an iso filename
  49. * Obey joliet_level
  50. * Return number of bytes consumed
  51. */
  52. int
  53. isochar(isofn, isoend, joliet_level, c, clen, flags, handle)
  54. u_char *isofn;
  55. u_char *isoend;
  56. int joliet_level;
  57. u_short *c;
  58. int *clen;
  59. int flags;
  60. void *handle;
  61. {
  62. size_t i, j, len;
  63. char inbuf[3], outbuf[3], *inp, *outp;
  64. *c = *isofn++;
  65. if (clen) *clen = 1;
  66. if (joliet_level == 0 || isofn == isoend)
  67. /* (00) and (01) are one byte in Joliet, too */
  68. return 1;
  69. if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
  70. i = j = len = 2;
  71. inbuf[0]=(char)*(isofn - 1);
  72. inbuf[1]=(char)*isofn;
  73. inbuf[2]='\0';
  74. inp = inbuf;
  75. outp = outbuf;
  76. cd9660_iconv->convchr(handle, (const char **)&inp, &i, &outp, &j);
  77. len -= j;
  78. if (clen) *clen = len;
  79. *c = '\0';
  80. while(len--)
  81. *c |= (*(outp - len - 1) & 0xff) << (len << 3);
  82. } else {
  83. switch (*c) {
  84. default:
  85. *c = '?';
  86. break;
  87. case '\0':
  88. *c = *isofn;
  89. break;
  90. }
  91. }
  92. return 2;
  93. }
  94. /*
  95. * translate and compare a filename
  96. * returns (fn - isofn)
  97. * Note: Version number plus ';' may be omitted.
  98. */
  99. int
  100. isofncmp(fn, fnlen, isofn, isolen, joliet_level, flags, handle, lhandle)
  101. u_char *fn;
  102. int fnlen;
  103. u_char *isofn;
  104. int isolen;
  105. int joliet_level;
  106. int flags;
  107. void *handle;
  108. void *lhandle;
  109. {
  110. int i, j;
  111. u_short c, d;
  112. u_char *fnend = fn + fnlen, *isoend = isofn + isolen;
  113. for (; fn < fnend; ) {
  114. d = sgetrune(fn, fnend - fn, (char const **)&fn, flags, lhandle);
  115. if (isofn == isoend)
  116. return d;
  117. isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
  118. if (c == ';') {
  119. if (d != ';')
  120. return d;
  121. for (i = 0; fn < fnend; i = i * 10 + *fn++ - '0') {
  122. if (*fn < '0' || *fn > '9') {
  123. return -1;
  124. }
  125. }
  126. for (j = 0; isofn != isoend; j = j * 10 + c - '0')
  127. isofn += isochar(isofn, isoend,
  128. joliet_level, &c,
  129. NULL, flags, handle);
  130. return i - j;
  131. }
  132. if (c != d) {
  133. if (c >= 'A' && c <= 'Z') {
  134. if (c + ('a' - 'A') != d) {
  135. if (d >= 'a' && d <= 'z')
  136. return d - ('a' - 'A') - c;
  137. else
  138. return d - c;
  139. }
  140. } else
  141. return d - c;
  142. }
  143. }
  144. if (isofn != isoend) {
  145. isofn += isochar(isofn, isoend, joliet_level, &c, NULL, flags, handle);
  146. switch (c) {
  147. default:
  148. return -c;
  149. case '.':
  150. if (isofn != isoend) {
  151. isochar(isofn, isoend, joliet_level, &c,
  152. NULL, flags, handle);
  153. if (c == ';')
  154. return 0;
  155. }
  156. return -1;
  157. case ';':
  158. return 0;
  159. }
  160. }
  161. return 0;
  162. }
  163. /*
  164. * translate a filename of length > 0
  165. */
  166. void
  167. isofntrans(infn, infnlen, outfn, outfnlen, original, assoc, joliet_level, flags, handle)
  168. u_char *infn;
  169. int infnlen;
  170. u_char *outfn;
  171. u_short *outfnlen;
  172. int original;
  173. int assoc;
  174. int joliet_level;
  175. int flags;
  176. void *handle;
  177. {
  178. u_short c, d = '\0';
  179. u_char *outp = outfn, *infnend = infn + infnlen;
  180. int clen;
  181. if (assoc) {
  182. *outp++ = ASSOCCHAR;
  183. }
  184. for (; infn != infnend; ) {
  185. infn += isochar(infn, infnend, joliet_level, &c, &clen, flags, handle);
  186. if (!original && !joliet_level && c >= 'A' && c <= 'Z')
  187. c += ('a' - 'A');
  188. else if (!original && c == ';') {
  189. outp -= (d == '.');
  190. break;
  191. }
  192. d = c;
  193. while(clen--)
  194. *outp++ = c >> (clen << 3);
  195. }
  196. *outfnlen = outp - outfn;
  197. }
  198. /*
  199. * same as sgetrune(3)
  200. */
  201. u_short
  202. sgetrune(string, n, result, flags, handle)
  203. const char *string;
  204. size_t n;
  205. char const **result;
  206. int flags;
  207. void *handle;
  208. {
  209. size_t i, j, len;
  210. char outbuf[3], *outp;
  211. u_short c = '\0';
  212. len = i = (n < 2) ? n : 2;
  213. j = 2;
  214. outp = outbuf;
  215. if (flags & ISOFSMNT_KICONV && cd9660_iconv) {
  216. cd9660_iconv->convchr(handle, (const char **)&string,
  217. &i, &outp, &j);
  218. len -= i;
  219. } else {
  220. len = 1;
  221. string++;
  222. }
  223. if (result) *result = string;
  224. while(len--) c |= (*(string - len - 1) & 0xff) << (len << 3);
  225. return (c);
  226. }