]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/LPdir_vms.c
Make the fuzzers more reproducible
[thirdparty/openssl.git] / crypto / LPdir_vms.c
CommitLineData
aa6bb135
RS
1/*
2 * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
a2400fca
RL
10/*
11 * Copyright (c) 2004, Richard Levitte <richard@levitte.org>
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
0f113f3e 22 *
bb09fd2b
RL
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
a2400fca
RL
34 */
35
36#include <stddef.h>
37#include <stdlib.h>
38#include <string.h>
39#include <errno.h>
40#include <descrip.h>
41#include <namdef.h>
42#include <rmsdef.h>
43#include <libfildef.h>
44#include <lib$routines.h>
45#include <strdef.h>
46#include <str$routines.h>
47#include <stsdef.h>
48#ifndef LPDIR_H
0f113f3e 49# include "LPdir.h"
a2400fca 50#endif
537c9823 51#include "vms_rms.h"
a2400fca 52
537c9823 53/* Some compiler options hide EVMSERR. */
b0841348 54#ifndef EVMSERR
0f113f3e 55# define EVMSERR 65535 /* error for non-translatable VMS errors */
b0841348
RL
56#endif
57
0f113f3e
MC
58struct LP_dir_context_st {
59 unsigned long VMS_context;
60 char filespec[NAMX_MAXRSS + 1];
61 char result[NAMX_MAXRSS + 1];
62 struct dsc$descriptor_d filespec_dsc;
63 struct dsc$descriptor_d result_dsc;
a2400fca
RL
64};
65
66const char *LP_find_file(LP_DIR_CTX **ctx, const char *directory)
67{
0f113f3e
MC
68 int status;
69 char *p, *r;
70 size_t l;
71 unsigned long flags = 0;
537c9823
RL
72
73/* Arrange 32-bit pointer to (copied) string storage, if needed. */
74#if __INITIAL_POINTER_SIZE == 64
75# pragma pointer_size save
76# pragma pointer_size 32
0f113f3e 77 char *ctx_filespec_32p;
537c9823 78# pragma pointer_size restore
0f113f3e
MC
79 char ctx_filespec_32[NAMX_MAXRSS + 1];
80#endif /* __INITIAL_POINTER_SIZE == 64 */
537c9823 81
a2400fca 82#ifdef NAML$C_MAXRSS
0f113f3e 83 flags |= LIB$M_FIL_LONG_NAMES;
a2400fca
RL
84#endif
85
0f113f3e
MC
86 if (ctx == NULL || directory == NULL) {
87 errno = EINVAL;
88 return 0;
a2400fca
RL
89 }
90
0f113f3e
MC
91 errno = 0;
92 if (*ctx == NULL) {
93 size_t filespeclen = strlen(directory);
94 char *filespec = NULL;
95
96 if (filespeclen == 0) {
97 errno = ENOENT;
98 return 0;
99 }
100
101 /* MUST be a VMS directory specification! Let's estimate if it is. */
102 if (directory[filespeclen - 1] != ']'
103 && directory[filespeclen - 1] != '>'
104 && directory[filespeclen - 1] != ':') {
105 errno = EINVAL;
106 return 0;
107 }
108
109 filespeclen += 4; /* "*.*;" */
110
111 if (filespeclen > NAMX_MAXRSS) {
112 errno = ENAMETOOLONG;
113 return 0;
114 }
115
b4faea50 116 *ctx = malloc(sizeof(**ctx));
0f113f3e
MC
117 if (*ctx == NULL) {
118 errno = ENOMEM;
119 return 0;
120 }
16f8d4eb 121 memset(*ctx, 0, sizeof(**ctx));
0f113f3e
MC
122
123 strcpy((*ctx)->filespec, directory);
124 strcat((*ctx)->filespec, "*.*;");
537c9823
RL
125
126/* Arrange 32-bit pointer to (copied) string storage, if needed. */
127#if __INITIAL_POINTER_SIZE == 64
128# define CTX_FILESPEC ctx_filespec_32p
129 /* Copy the file name to storage with a 32-bit pointer. */
130 ctx_filespec_32p = ctx_filespec_32;
0f113f3e
MC
131 strcpy(ctx_filespec_32p, (*ctx)->filespec);
132#else /* __INITIAL_POINTER_SIZE == 64 */
537c9823 133# define CTX_FILESPEC (*ctx)->filespec
0f113f3e 134#endif /* __INITIAL_POINTER_SIZE == 64 [else] */
537c9823 135
0f113f3e
MC
136 (*ctx)->filespec_dsc.dsc$w_length = filespeclen;
137 (*ctx)->filespec_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
138 (*ctx)->filespec_dsc.dsc$b_class = DSC$K_CLASS_S;
139 (*ctx)->filespec_dsc.dsc$a_pointer = CTX_FILESPEC;
a2400fca
RL
140 }
141
0f113f3e
MC
142 (*ctx)->result_dsc.dsc$w_length = 0;
143 (*ctx)->result_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
144 (*ctx)->result_dsc.dsc$b_class = DSC$K_CLASS_D;
145 (*ctx)->result_dsc.dsc$a_pointer = 0;
a2400fca 146
0f113f3e
MC
147 status = lib$find_file(&(*ctx)->filespec_dsc, &(*ctx)->result_dsc,
148 &(*ctx)->VMS_context, 0, 0, 0, &flags);
a2400fca 149
0f113f3e
MC
150 if (status == RMS$_NMF) {
151 errno = 0;
152 vaxc$errno = status;
153 return NULL;
a2400fca
RL
154 }
155
0f113f3e
MC
156 if (!$VMS_STATUS_SUCCESS(status)) {
157 errno = EVMSERR;
158 vaxc$errno = status;
159 return NULL;
a2400fca
RL
160 }
161
0f113f3e
MC
162 /*
163 * Quick, cheap and dirty way to discard any device and directory, since
164 * we only want file names
165 */
166 l = (*ctx)->result_dsc.dsc$w_length;
167 p = (*ctx)->result_dsc.dsc$a_pointer;
168 r = p;
169 for (; *p; p++) {
170 if (*p == '^' && p[1] != '\0') { /* Take care of ODS-5 escapes */
171 p++;
172 } else if (*p == ':' || *p == '>' || *p == ']') {
173 l -= p + 1 - r;
174 r = p + 1;
175 } else if (*p == ';') {
176 l = p - r;
177 break;
178 }
a2400fca
RL
179 }
180
0f113f3e
MC
181 strncpy((*ctx)->result, r, l);
182 (*ctx)->result[l] = '\0';
183 str$free1_dx(&(*ctx)->result_dsc);
a2400fca 184
0f113f3e 185 return (*ctx)->result;
a2400fca
RL
186}
187
188int LP_find_file_end(LP_DIR_CTX **ctx)
189{
0f113f3e
MC
190 if (ctx != NULL && *ctx != NULL) {
191 int status = lib$find_file_end(&(*ctx)->VMS_context);
192
193 free(*ctx);
194
195 if (!$VMS_STATUS_SUCCESS(status)) {
196 errno = EVMSERR;
197 vaxc$errno = status;
198 return 0;
199 }
200 return 1;
a2400fca 201 }
0f113f3e
MC
202 errno = EINVAL;
203 return 0;
a2400fca 204}