+
+#ifdef WIN32
+#ifdef UNDER_CE
+#define _get_osfhandle(a) ((long) (a))
+#endif
+
+static
+ssize_t pread (int fd, void *buf, size_t count, off_t offset)
+{
+ HANDLE handle = (HANDLE)(intptr_t)_get_osfhandle (fd);
+ if (handle == INVALID_HANDLE_VALUE)
+ return -1;
+
+ OVERLAPPED olap; olap.Offset = offset; olap.OffsetHigh = (offset >> 32);
+ DWORD written;
+ /* This braindead API will override the file pointer even if we specify
+ * an explicit read offset... So do not expect this to mix well with
+ * regular read() calls. */
+ if (ReadFile (handle, buf, count, &written, &olap))
+ return written;
+ return -1;
+}
+#endif
+
+/**
+ * Loads a file into a block of memory. If possible a private file mapping is
+ * created. Otherwise, the file is read normally. On 32-bits platforms, this
+ * function will not work for very large files, due to memory space
+ * constraints. Cancellation point.
+ *
+ * @param fd file descriptor to load from
+ * @return a new block with the file content at p_buffer, and file length at
+ * i_buffer (release it with block_Release()), or NULL upon error (see errno).
+ */
+block_t *block_File (int fd)
+{
+ size_t length;
+ struct stat st;
+
+ /* First, get the file size */
+ if (fstat (fd, &st))
+ return NULL;
+
+ /* st_size is meaningful for regular files, shared memory and typed memory.
+ * It's also meaning for symlinks, but that's not possible with fstat().
+ * In other cases, it's undefined, and we should really not go further. */
+#ifndef S_TYPEISSHM
+# define S_TYPEISSHM( buf ) (0)
+#endif
+ if (S_ISDIR (st.st_mode))
+ {
+ errno = EISDIR;
+ return NULL;
+ }
+ if (!S_ISREG (st.st_mode) && !S_TYPEISSHM (&st))
+ {
+ errno = ESPIPE;
+ return NULL;
+ }
+
+ /* Prevent an integer overflow in mmap() and malloc() */
+ if (st.st_size >= SIZE_MAX)
+ {
+ errno = ENOMEM;
+ return NULL;
+ }
+ length = (size_t)st.st_size;
+
+#ifdef HAVE_MMAP
+ if (length > 0)
+ {
+ void *addr;
+
+ addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
+ if (addr != MAP_FAILED)
+ return block_mmap_Alloc (addr, length);
+ }
+#endif
+
+ /* If mmap() is not implemented by the OS _or_ the filesystem... */
+ block_t *block = block_Alloc (length);
+ if (block == NULL)
+ return NULL;
+ block_cleanup_push (block);
+
+ for (size_t i = 0; i < length;)
+ {
+ ssize_t len = pread (fd, block->p_buffer + i, length - i, i);
+ if (len == -1)
+ {
+ block_Release (block);
+ block = NULL;
+ break;
+ }
+ i += len;
+ }
+ vlc_cleanup_pop ();
+ return block;
+}
+
+/**
+ * @section Thread-safe block queue functions
+ */
+
+/**
+ * Internal state for block queues
+ */