[UBTRFS] Upgrade to 1.4
[reactos.git] / sdk / lib / fslib / btrfslib / btrfslib.c
index 902e808..ec9fe38 100644 (file)
 #include <ata.h>
 #include <mountmgr.h>
 #ifdef __REACTOS__
+#include <winnls.h>
 #include "btrfs.h"
 #include "btrfsioctl.h"
 #else
+#include <stringapiset.h>
 #include "../btrfs.h"
 #include "../btrfsioctl.h"
 #endif
@@ -62,9 +64,6 @@ NTSTATUS NTAPI NtWriteFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcR
 
 NTSTATUS NTAPI NtReadFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer,
                           ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key);
-
-NTSYSAPI NTSTATUS NTAPI RtlUnicodeToUTF8N(PCHAR UTF8StringDestination, ULONG UTF8StringMaxByteCount, PULONG UTF8StringActualByteCount,
-                                          PCWCH UnicodeStringSource, ULONG  UnicodeStringByteCount);
 #ifdef __cplusplus
 }
 #endif
@@ -89,9 +88,9 @@ typedef struct {
 #define FORMAT_FLAG_INTEGRITY_DISABLE   0x00000100
 
 typedef struct {
-    UINT16 unk1;
-    UINT16 unk2;
-    UINT32 flags;
+    uint16_t unk1;
+    uint16_t unk2;
+    uint32_t flags;
     DSTRING* label;
 } options;
 
@@ -111,33 +110,27 @@ FORCEINLINE VOID InsertTailList(PLIST_ENTRY ListHead, PLIST_ENTRY Entry) {
 }
 #endif
 
-#if defined(__REACTOS__) && (NTDDI_VERSION < NTDDI_WIN7)
-NTSTATUS NTAPI RtlUnicodeToUTF8N(CHAR *utf8_dest, ULONG utf8_bytes_max,
-                                 ULONG *utf8_bytes_written,
-                                 const WCHAR *uni_src, ULONG uni_bytes);
-#endif /* defined(__REACTOS__) && (NTDDI_VERSION < NTDDI_WIN7) */
-
 #ifdef __REACTOS__
 ULONG NTAPI NtGetTickCount(VOID);
 #endif
 
 typedef struct {
     KEY key;
-    UINT16 size;
+    uint16_t size;
     void* data;
     LIST_ENTRY list_entry;
 } btrfs_item;
 
 typedef struct {
-    UINT64 offset;
+    uint64_t offset;
     CHUNK_ITEM* chunk_item;
-    UINT64 lastoff;
-    UINT64 used;
+    uint64_t lastoff;
+    uint64_t used;
     LIST_ENTRY list_entry;
 } btrfs_chunk;
 
 typedef struct {
-    UINT64 id;
+    uint64_t id;
     tree_header header;
     btrfs_chunk* c;
     LIST_ENTRY items;
@@ -146,7 +139,7 @@ typedef struct {
 
 typedef struct {
     DEV_ITEM dev_item;
-    UINT64 last_alloc;
+    uint64_t last_alloc;
 } btrfs_dev;
 
 #define keycmp(key1, key2)\
@@ -160,7 +153,7 @@ typedef struct {
 
 HMODULE module;
 ULONG def_sector_size = 0, def_node_size = 0;
-UINT64 def_incompat_flags = BTRFS_INCOMPAT_FLAGS_EXTENDED_IREF | BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA;
+uint64_t def_incompat_flags = BTRFS_INCOMPAT_FLAGS_EXTENDED_IREF | BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA;
 
 // the following definitions come from fmifs.h in ReactOS
 
@@ -218,7 +211,7 @@ typedef enum {
 
 typedef BOOLEAN (NTAPI* PFMIFSCALLBACK)(CALLBACKCOMMAND Command, ULONG SubAction, PVOID ActionInfo);
 
-static const UINT32 crctable[] = {
+static const uint32_t crctable[] = {
     0x00000000, 0xf26b8303, 0xe13b70f7, 0x1350f3f4, 0xc79a971f, 0x35f1141c, 0x26a1e7e8, 0xd4ca64eb,
     0x8ad958cf, 0x78b2dbcc, 0x6be22838, 0x9989ab3b, 0x4d43cfd0, 0xbf284cd3, 0xac78bf27, 0x5e133c24,
     0x105ec76f, 0xe235446c, 0xf165b798, 0x030e349b, 0xd7c45070, 0x25afd373, 0x36ff2087, 0xc494a384,
@@ -253,8 +246,8 @@ static const UINT32 crctable[] = {
     0x79b737ba, 0x8bdcb4b9, 0x988c474d, 0x6ae7c44e, 0xbe2da0a5, 0x4c4623a6, 0x5f16d052, 0xad7d5351,
 };
 
-static UINT32 calc_crc32c(UINT32 seed, UINT8* msg, ULONG msglen) {
-    UINT32 rem;
+static uint32_t calc_crc32c(uint32_t seed, uint8_t* msg, ULONG msglen) {
+    uint32_t rem;
     ULONG i;
 
     rem = seed;
@@ -286,7 +279,7 @@ NTSTATUS NTAPI BtrfsChkdskEx(PUNICODE_STRING DriveRoot, BOOLEAN FixErrors, BOOLE
     return STATUS_SUCCESS;
 }
 
-static btrfs_root* add_root(LIST_ENTRY* roots, UINT64 id) {
+static btrfs_root* add_root(LIST_ENTRY* roots, uint64_t id) {
     btrfs_root* root;
 
 #ifdef __REACTOS__
@@ -362,7 +355,7 @@ static void free_chunks(LIST_ENTRY* chunks) {
     }
 }
 
-static void add_item(btrfs_root* r, UINT64 obj_id, UINT8 obj_type, UINT64 offset, void* data, UINT16 size) {
+static void add_item(btrfs_root* r, uint64_t obj_id, uint8_t obj_type, uint64_t offset, void* data, uint16_t size) {
     LIST_ENTRY* le;
     btrfs_item* item;
 
@@ -403,8 +396,8 @@ static void add_item(btrfs_root* r, UINT64 obj_id, UINT8 obj_type, UINT64 offset
     InsertTailList(&r->items, &item->list_entry);
 }
 
-static UINT64 find_chunk_offset(UINT64 size, UINT64 offset, btrfs_dev* dev, btrfs_root* dev_root, BTRFS_UUID* chunkuuid) {
-    UINT64 off;
+static uint64_t find_chunk_offset(uint64_t size, uint64_t offset, btrfs_dev* dev, btrfs_root* dev_root, BTRFS_UUID* chunkuuid) {
+    uint64_t off;
     DEV_EXTENT de;
 
     off = dev->last_alloc;
@@ -423,9 +416,9 @@ static UINT64 find_chunk_offset(UINT64 size, UINT64 offset, btrfs_dev* dev, btrf
     return off;
 }
 
-static btrfs_chunk* add_chunk(LIST_ENTRY* chunks, UINT64 flags, btrfs_root* chunk_root, btrfs_dev* dev, btrfs_root* dev_root, BTRFS_UUID* chunkuuid, UINT32 sector_size) {
-    UINT64 off, size;
-    UINT16 stripes, i;
+static btrfs_chunk* add_chunk(LIST_ENTRY* chunks, uint64_t flags, btrfs_root* chunk_root, btrfs_dev* dev, btrfs_root* dev_root, BTRFS_UUID* chunkuuid, uint32_t sector_size) {
+    uint64_t off, size;
+    uint16_t stripes, i;
     btrfs_chunk* c;
     LIST_ENTRY* le;
     CHUNK_ITEM_STRIPE* cis;
@@ -497,16 +490,16 @@ static btrfs_chunk* add_chunk(LIST_ENTRY* chunks, UINT64 flags, btrfs_root* chun
     return c;
 }
 
-static BOOL superblock_collision(btrfs_chunk* c, UINT64 address) {
+static BOOL superblock_collision(btrfs_chunk* c, uint64_t address) {
     CHUNK_ITEM_STRIPE* cis = (CHUNK_ITEM_STRIPE*)&c->chunk_item[1];
-    UINT64 stripe = (address - c->offset) / c->chunk_item->stripe_length;
-    UINT16 i, j;
+    uint64_t stripe = (address - c->offset) / c->chunk_item->stripe_length;
+    uint16_t i, j;
 
     for (i = 0; i < c->chunk_item->num_stripes; i++) {
         j = 0;
         while (superblock_addrs[j] != 0) {
             if (superblock_addrs[j] >= cis[i].offset) {
-                UINT64 stripe2 = (superblock_addrs[j] - cis[i].offset) / c->chunk_item->stripe_length;
+                uint64_t stripe2 = (superblock_addrs[j] - cis[i].offset) / c->chunk_item->stripe_length;
 
                 if (stripe2 == stripe)
                     return TRUE;
@@ -518,8 +511,8 @@ static BOOL superblock_collision(btrfs_chunk* c, UINT64 address) {
     return FALSE;
 }
 
-static UINT64 get_next_address(btrfs_chunk* c) {
-    UINT64 addr;
+static uint64_t get_next_address(btrfs_chunk* c) {
+    uint64_t addr;
 
     addr = c->lastoff;
 
@@ -535,18 +528,18 @@ static UINT64 get_next_address(btrfs_chunk* c) {
 
 typedef struct {
     EXTENT_ITEM ei;
-    UINT8 type;
+    uint8_t type;
     TREE_BLOCK_REF tbr;
 } EXTENT_ITEM_METADATA;
 
 typedef struct {
     EXTENT_ITEM ei;
     EXTENT_ITEM2 ei2;
-    UINT8 type;
+    uint8_t type;
     TREE_BLOCK_REF tbr;
 } EXTENT_ITEM_METADATA2;
 
-static void assign_addresses(LIST_ENTRY* roots, btrfs_chunk* sys_chunk, btrfs_chunk* metadata_chunk, UINT32 node_size,
+static void assign_addresses(LIST_ENTRY* roots, btrfs_chunk* sys_chunk, btrfs_chunk* metadata_chunk, uint32_t node_size,
                              btrfs_root* root_root, btrfs_root* extent_root, BOOL skinny) {
     LIST_ENTRY* le;
 
@@ -619,9 +612,9 @@ static void assign_addresses(LIST_ENTRY* roots, btrfs_chunk* sys_chunk, btrfs_ch
     }
 }
 
-static NTSTATUS write_data(HANDLE h, UINT64 address, btrfs_chunk* c, void* data, ULONG size) {
+static NTSTATUS write_data(HANDLE h, uint64_t address, btrfs_chunk* c, void* data, ULONG size) {
     NTSTATUS Status;
-    UINT16 i;
+    uint16_t i;
     IO_STATUS_BLOCK iosb;
     LARGE_INTEGER off;
     CHUNK_ITEM_STRIPE* cis;
@@ -639,10 +632,10 @@ static NTSTATUS write_data(HANDLE h, UINT64 address, btrfs_chunk* c, void* data,
     return STATUS_SUCCESS;
 }
 
-static NTSTATUS write_roots(HANDLE h, LIST_ENTRY* roots, UINT32 node_size, BTRFS_UUID* fsuuid, BTRFS_UUID* chunkuuid) {
+static NTSTATUS write_roots(HANDLE h, LIST_ENTRY* roots, uint32_t node_size, BTRFS_UUID* fsuuid, BTRFS_UUID* chunkuuid) {
     LIST_ENTRY *le, *le2;
     NTSTATUS Status;
-    UINT8* tree;
+    uint8_t* tree;
 
 #ifndef __REACTOS__
     tree = malloc(node_size);
@@ -653,9 +646,9 @@ static NTSTATUS write_roots(HANDLE h, LIST_ENTRY* roots, UINT32 node_size, BTRFS
     le = roots->Flink;
     while (le != roots) {
         btrfs_root* r = CONTAINING_RECORD(le, btrfs_root, list_entry);
-        UINT8* dp;
+        uint8_t* dp;
         leaf_node* ln;
-        UINT32 crc32;
+        uint32_t crc32;
 
         memset(tree, 0, node_size);
 
@@ -694,8 +687,8 @@ static NTSTATUS write_roots(HANDLE h, LIST_ENTRY* roots, UINT32 node_size, BTRFS
 
         memcpy(tree, &r->header, sizeof(tree_header));
 
-        crc32 = ~calc_crc32c(0xffffffff, (UINT8*)&((tree_header*)tree)->fs_uuid, node_size - sizeof(((tree_header*)tree)->csum));
-        memcpy(tree, &crc32, sizeof(UINT32));
+        crc32 = ~calc_crc32c(0xffffffff, (uint8_t*)&((tree_header*)tree)->fs_uuid, node_size - sizeof(((tree_header*)tree)->csum));
+        memcpy(tree, &crc32, sizeof(uint32_t));
 
         Status = write_data(h, r->header.address, r->c, tree, node_size);
         if (!NT_SUCCESS(Status)) {
@@ -724,7 +717,7 @@ static void get_uuid(BTRFS_UUID* uuid) {
 #else
 static void get_uuid(BTRFS_UUID* uuid, ULONG* seed) {
 #endif
-    UINT8 i;
+    uint8_t i;
 
     for (i = 0; i < 16; i+=2) {
 #ifndef __REACTOS__
@@ -739,9 +732,9 @@ static void get_uuid(BTRFS_UUID* uuid, ULONG* seed) {
 }
 
 #ifndef __REACTOS__
-static void init_device(btrfs_dev* dev, UINT64 id, UINT64 size, BTRFS_UUID* fsuuid, UINT32 sector_size) {
+static void init_device(btrfs_dev* dev, uint64_t id, uint64_t size, BTRFS_UUID* fsuuid, uint32_t sector_size) {
 #else
-static void init_device(btrfs_dev* dev, UINT64 id, UINT64 size, BTRFS_UUID* fsuuid, UINT32 sector_size, ULONG* seed) {
+static void init_device(btrfs_dev* dev, uint64_t id, uint64_t size, BTRFS_UUID* fsuuid, uint32_t sector_size, ULONG* seed) {
 #endif
     dev->dev_item.dev_id = id;
     dev->dev_item.num_bytes = size;
@@ -765,126 +758,16 @@ static void init_device(btrfs_dev* dev, UINT64 id, UINT64 size, BTRFS_UUID* fsuu
     dev->last_alloc = 0x100000; // skip first megabyte
 }
 
-#ifdef __REACTOS__
-NTSTATUS NTAPI RtlUnicodeToUTF8N(CHAR *utf8_dest, ULONG utf8_bytes_max,
-                                 ULONG *utf8_bytes_written,
-                                 const WCHAR *uni_src, ULONG uni_bytes)
-{
-    NTSTATUS status;
-    ULONG i;
-    ULONG written;
-    ULONG ch;
-    BYTE utf8_ch[4];
-    ULONG utf8_ch_len;
-
-    if (!uni_src)
-        return STATUS_INVALID_PARAMETER_4;
-    if (!utf8_bytes_written)
-        return STATUS_INVALID_PARAMETER;
-    if (utf8_dest && uni_bytes % sizeof(WCHAR))
-        return STATUS_INVALID_PARAMETER_5;
-
-    written = 0;
-    status = STATUS_SUCCESS;
-
-    for (i = 0; i < uni_bytes / sizeof(WCHAR); i++)
-    {
-        /* decode UTF-16 into ch */
-        ch = uni_src[i];
-        if (ch >= 0xdc00 && ch <= 0xdfff)
-        {
-            ch = 0xfffd;
-            status = STATUS_SOME_NOT_MAPPED;
-        }
-        else if (ch >= 0xd800 && ch <= 0xdbff)
-        {
-            if (i + 1 < uni_bytes / sizeof(WCHAR))
-            {
-                ch -= 0xd800;
-                ch <<= 10;
-                if (uni_src[i + 1] >= 0xdc00 && uni_src[i + 1] <= 0xdfff)
-                {
-                    ch |= uni_src[i + 1] - 0xdc00;
-                    ch += 0x010000;
-                    i++;
-                }
-                else
-                {
-                    ch = 0xfffd;
-                    status = STATUS_SOME_NOT_MAPPED;
-                }
-            }
-            else
-            {
-                ch = 0xfffd;
-                status = STATUS_SOME_NOT_MAPPED;
-            }
-        }
-
-        /* encode ch as UTF-8 */
-        ASSERT(ch <= 0x10ffff);
-        if (ch < 0x80)
-        {
-            utf8_ch[0] = ch & 0x7f;
-            utf8_ch_len = 1;
-        }
-        else if (ch < 0x800)
-        {
-            utf8_ch[0] = 0xc0 | (ch >>  6 & 0x1f);
-            utf8_ch[1] = 0x80 | (ch >>  0 & 0x3f);
-            utf8_ch_len = 2;
-        }
-        else if (ch < 0x10000)
-        {
-            utf8_ch[0] = 0xe0 | (ch >> 12 & 0x0f);
-            utf8_ch[1] = 0x80 | (ch >>  6 & 0x3f);
-            utf8_ch[2] = 0x80 | (ch >>  0 & 0x3f);
-            utf8_ch_len = 3;
-        }
-        else if (ch < 0x200000)
-        {
-            utf8_ch[0] = 0xf0 | (ch >> 18 & 0x07);
-            utf8_ch[1] = 0x80 | (ch >> 12 & 0x3f);
-            utf8_ch[2] = 0x80 | (ch >>  6 & 0x3f);
-            utf8_ch[3] = 0x80 | (ch >>  0 & 0x3f);
-            utf8_ch_len = 4;
-        }
-
-        if (!utf8_dest)
-        {
-            written += utf8_ch_len;
-            continue;
-        }
-
-        if (utf8_bytes_max >= utf8_ch_len)
-        {
-            memcpy(utf8_dest, utf8_ch, utf8_ch_len);
-            utf8_dest += utf8_ch_len;
-            utf8_bytes_max -= utf8_ch_len;
-            written += utf8_ch_len;
-        }
-        else
-        {
-            utf8_bytes_max = 0;
-            status = STATUS_BUFFER_TOO_SMALL;
-        }
-    }
-
-    *utf8_bytes_written = written;
-    return status;
-}
-#endif
-
 static NTSTATUS write_superblocks(HANDLE h, btrfs_dev* dev, btrfs_root* chunk_root, btrfs_root* root_root, btrfs_root* extent_root,
-                                  btrfs_chunk* sys_chunk, UINT32 node_size, BTRFS_UUID* fsuuid, UINT32 sector_size, PUNICODE_STRING label, UINT64 incompat_flags) {
+                                  btrfs_chunk* sys_chunk, uint32_t node_size, BTRFS_UUID* fsuuid, uint32_t sector_size, PUNICODE_STRING label, uint64_t incompat_flags) {
     NTSTATUS Status;
     IO_STATUS_BLOCK iosb;
     ULONG sblen;
     int i;
-    UINT32 crc32;
+    uint32_t crc32;
     superblock* sb;
     KEY* key;
-    UINT64 bytes_used;
+    uint64_t bytes_used;
     LIST_ENTRY* le;
 
     sblen = sizeof(*sb);
@@ -932,10 +815,16 @@ static NTSTATUS write_superblocks(HANDLE h, btrfs_dev* dev, btrfs_root* chunk_ro
     memcpy(&sb->dev_item, &dev->dev_item, sizeof(DEV_ITEM));
 
     if (label->Length > 0) {
-        int i;
-        ULONG utf8len;
+#ifdef __REACTOS__
+        ANSI_STRING as;
+        unsigned int i;
 
         for (i = 0; i < label->Length / sizeof(WCHAR); i++) {
+#else
+        ULONG utf8len;
+
+        for (unsigned int i = 0; i < label->Length / sizeof(WCHAR); i++) {
+#endif
             if (label->Buffer[i] == '/' || label->Buffer[i] == '\\') {
 #ifndef __REACTOS__
                 free(sb);
@@ -946,34 +835,29 @@ static NTSTATUS write_superblocks(HANDLE h, btrfs_dev* dev, btrfs_root* chunk_ro
             }
         }
 
-        Status = RtlUnicodeToUTF8N(NULL, 0, &utf8len, label->Buffer, label->Length);
-        if (!NT_SUCCESS(Status)) {
 #ifndef __REACTOS__
-            free(sb);
-#else
-            RtlFreeHeap(RtlGetProcessHeap(), 0, sb);
-#endif
-            return Status;
-        }
+        utf8len = WideCharToMultiByte(CP_UTF8, 0, label->Buffer, label->Length, NULL, 0, NULL, NULL);
 
-        if (utf8len > MAX_LABEL_SIZE) {
-#ifndef __REACTOS__
+        if (utf8len == 0 || utf8len > MAX_LABEL_SIZE) {
             free(sb);
-#else
-            RtlFreeHeap(RtlGetProcessHeap(), 0, sb);
-#endif
             return STATUS_INVALID_VOLUME_LABEL;
         }
 
-        Status = RtlUnicodeToUTF8N((PCHAR)&sb->label, MAX_LABEL_SIZE, &utf8len, label->Buffer, label->Length);
-        if (!NT_SUCCESS(Status)) {
-#ifndef __REACTOS__
+        if (WideCharToMultiByte(CP_UTF8, 0, label->Buffer, label->Length, sb->label, utf8len, NULL, NULL) == 0) {
             free(sb);
+            return STATUS_INVALID_VOLUME_LABEL;
+        }
 #else
+        as.Buffer = sb->label;
+        as.Length = 0;
+        as.MaximumLength = MAX_LABEL_SIZE;
+
+        if (!NT_SUCCESS(RtlUnicodeStringToAnsiString(&as, label, FALSE)))
+        {
             RtlFreeHeap(RtlGetProcessHeap(), 0, sb);
-#endif
-            return Status;
+            return STATUS_INVALID_VOLUME_LABEL;
         }
+#endif
     }
     sb->cache_generation = 0xffffffffffffffff;
 
@@ -992,8 +876,8 @@ static NTSTATUS write_superblocks(HANDLE h, btrfs_dev* dev, btrfs_root* chunk_ro
 
         sb->sb_phys_addr = superblock_addrs[i];
 
-        crc32 = ~calc_crc32c(0xffffffff, (UINT8*)&sb->uuid, (ULONG)sizeof(superblock) - sizeof(sb->checksum));
-        memcpy(&sb->checksum, &crc32, sizeof(UINT32));
+        crc32 = ~calc_crc32c(0xffffffff, (uint8_t*)&sb->uuid, (ULONG)sizeof(superblock) - sizeof(sb->checksum));
+        memcpy(&sb->checksum, &crc32, sizeof(uint32_t));
 
         off.QuadPart = superblock_addrs[i];
 
@@ -1045,7 +929,7 @@ GetSystemTimeAsFileTime(OUT PFILETIME lpFileTime)
 }
 #endif
 
-static void init_fs_tree(btrfs_root* r, UINT32 node_size) {
+static void init_fs_tree(btrfs_root* r, uint32_t node_size) {
     INODE_ITEM ii;
     INODE_REF* ir;
     FILETIME filetime;
@@ -1108,8 +992,7 @@ static NTSTATUS clear_first_megabyte(HANDLE h) {
     NTSTATUS Status;
     IO_STATUS_BLOCK iosb;
     LARGE_INTEGER zero;
-    UINT8* mb;
-
+    uint8_t* mb;
 
 #ifndef __REACTOS__
     mb = malloc(0x100000);
@@ -1157,7 +1040,7 @@ static BOOL is_ssd(HANDLE h) {
     Status = NtDeviceIoControlFile(h, NULL, NULL, NULL, &iosb, IOCTL_ATA_PASS_THROUGH, apte, aptelen, apte, aptelen);
 
     if (NT_SUCCESS(Status)) {
-        idd = (IDENTIFY_DEVICE_DATA*)((UINT8*)apte + sizeof(ATA_PASS_THROUGH_EX));
+        idd = (IDENTIFY_DEVICE_DATA*)((uint8_t*)apte + sizeof(ATA_PASS_THROUGH_EX));
 
         if (idd->NominalMediaRotationRate == 1) {
 #ifndef __REACTOS__
@@ -1178,7 +1061,7 @@ static BOOL is_ssd(HANDLE h) {
     return FALSE;
 }
 
-static NTSTATUS write_btrfs(HANDLE h, UINT64 size, PUNICODE_STRING label, UINT32 sector_size, UINT32 node_size, UINT64 incompat_flags) {
+static NTSTATUS write_btrfs(HANDLE h, uint64_t size, PUNICODE_STRING label, uint32_t sector_size, uint32_t node_size, uint64_t incompat_flags) {
     NTSTATUS Status;
     LIST_ENTRY roots, chunks;
     btrfs_root *root_root, *chunk_root, *extent_root, *dev_root, *fs_root, *reloc_root;
@@ -1186,13 +1069,13 @@ static NTSTATUS write_btrfs(HANDLE h, UINT64 size, PUNICODE_STRING label, UINT32
     btrfs_dev dev;
     BTRFS_UUID fsuuid, chunkuuid;
     BOOL ssd;
-    UINT64 metadata_flags;
+    uint64_t metadata_flags;
 #ifdef __REACTOS__
     ULONG seed;
 #endif
 
 #ifndef __REACTOS__
-    srand(time(0));
+    srand((unsigned int)time(0));
     get_uuid(&fsuuid);
     get_uuid(&chunkuuid);
 #else
@@ -1264,14 +1147,14 @@ static NTSTATUS write_btrfs(HANDLE h, UINT64 size, PUNICODE_STRING label, UINT32
 }
 
 static BOOL look_for_device(btrfs_filesystem* bfs, BTRFS_UUID* devuuid) {
-    UINT32 i;
+    uint32_t i;
     btrfs_filesystem_device* dev;
 
     for (i = 0; i < bfs->num_devices; i++) {
         if (i == 0)
             dev = &bfs->device;
         else
-            dev = (btrfs_filesystem_device*)((UINT8*)dev + offsetof(btrfs_filesystem_device, name[0]) + dev->name_length);
+            dev = (btrfs_filesystem_device*)((uint8_t*)dev + offsetof(btrfs_filesystem_device, name[0]) + dev->name_length);
 
         if (RtlCompareMemory(&dev->uuid, devuuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID))
             return TRUE;
@@ -1280,14 +1163,14 @@ static BOOL look_for_device(btrfs_filesystem* bfs, BTRFS_UUID* devuuid) {
     return FALSE;
 }
 
-static BOOL is_mounted_multi_device(HANDLE h, UINT32 sector_size) {
+static BOOL is_mounted_multi_device(HANDLE h, uint32_t sector_size) {
     NTSTATUS Status;
     superblock* sb;
     ULONG sblen;
     IO_STATUS_BLOCK iosb;
     LARGE_INTEGER off;
     BTRFS_UUID fsuuid, devuuid;
-    UINT32 crc32;
+    uint32_t crc32;
     UNICODE_STRING us;
     OBJECT_ATTRIBUTES atts;
     HANDLE h2;
@@ -1328,8 +1211,8 @@ static BOOL is_mounted_multi_device(HANDLE h, UINT32 sector_size) {
         return FALSE;
     }
 
-    crc32 = ~calc_crc32c(0xffffffff, (UINT8*)&sb->uuid, (ULONG)sizeof(superblock) - sizeof(sb->checksum));
-    if (crc32 != *((UINT32*)sb)) {
+    crc32 = ~calc_crc32c(0xffffffff, (uint8_t*)&sb->uuid, (ULONG)sizeof(superblock) - sizeof(sb->checksum));
+    if (crc32 != *((uint32_t*)sb)) {
 #ifndef __REACTOS__
         free(sb);
 #else
@@ -1347,7 +1230,7 @@ static BOOL is_mounted_multi_device(HANDLE h, UINT32 sector_size) {
     RtlFreeHeap(RtlGetProcessHeap(), 0, sb);
 #endif
 
-    us.Length = us.MaximumLength = wcslen(btrfs) * sizeof(WCHAR);
+    us.Length = us.MaximumLength = (USHORT)(wcslen(btrfs) * sizeof(WCHAR));
     us.Buffer = btrfs;
 
     InitializeObjectAttributes(&atts, &us, 0, NULL, NULL);
@@ -1395,7 +1278,7 @@ static BOOL is_mounted_multi_device(HANDLE h, UINT32 sector_size) {
             if (bfs2->next_entry == 0)
                 break;
             else
-                bfs2 = (btrfs_filesystem*)((UINT8*)bfs2 + bfs2->next_entry);
+                bfs2 = (btrfs_filesystem*)((uint8_t*)bfs2 + bfs2->next_entry);
         }
     }
 
@@ -1446,14 +1329,14 @@ NTSTATUS NTAPI BtrfsFormatEx(PUNICODE_STRING DriveRoot, FMIFS_MEDIA_FLAG MediaFl
     IO_STATUS_BLOCK iosb;
     GET_LENGTH_INFORMATION gli;
     DISK_GEOMETRY dg;
-    UINT32 sector_size, node_size;
+    uint32_t sector_size, node_size;
     UNICODE_STRING btrfsus;
 #ifndef __REACTOS__
     HANDLE token;
     TOKEN_PRIVILEGES tp;
     LUID luid;
 #endif
-    UINT64 incompat_flags;
+    uint64_t incompat_flags;
     UNICODE_STRING empty_label;
 
     static WCHAR btrfs[] = L"\\Btrfs";
@@ -1561,7 +1444,7 @@ end:
 
     if (NT_SUCCESS(Status)) {
         btrfsus.Buffer = btrfs;
-        btrfsus.Length = btrfsus.MaximumLength = wcslen(btrfs) * sizeof(WCHAR);
+        btrfsus.Length = btrfsus.MaximumLength = (USHORT)(wcslen(btrfs) * sizeof(WCHAR));
 
         InitializeObjectAttributes(&attr, &btrfsus, 0, NULL, NULL);
 
@@ -1604,18 +1487,18 @@ end:
     return Status;
 }
 
-BOOL __stdcall FormatEx(DSTRING* root, STREAM_MESSAGE* message, options* opts, UINT32 unk1) {
+BOOL __stdcall FormatEx(DSTRING* root, STREAM_MESSAGE* message, options* opts, uint32_t unk1) {
     UNICODE_STRING DriveRoot, Label;
     NTSTATUS Status;
 
     if (!root || !root->string)
         return FALSE;
 
-    DriveRoot.Length = DriveRoot.MaximumLength = wcslen(root->string) * sizeof(WCHAR);
+    DriveRoot.Length = DriveRoot.MaximumLength = (USHORT)(wcslen(root->string) * sizeof(WCHAR));
     DriveRoot.Buffer = root->string;
 
     if (opts && opts->label && opts->label->string) {
-        Label.Length = Label.MaximumLength = wcslen(opts->label->string) * sizeof(WCHAR);
+        Label.Length = Label.MaximumLength = (USHORT)(wcslen(opts->label->string) * sizeof(WCHAR));
         Label.Buffer = opts->label->string;
     } else {
         Label.Length = Label.MaximumLength = 0;
@@ -1639,11 +1522,11 @@ void __stdcall SetSizes(ULONG sector, ULONG node) {
         def_node_size = node;
 }
 
-void __stdcall SetIncompatFlags(UINT64 incompat_flags) {
+void __stdcall SetIncompatFlags(uint64_t incompat_flags) {
     def_incompat_flags = incompat_flags;
 }
 
-BOOL __stdcall GetFilesystemInformation(UINT32 unk1, UINT32 unk2, void* unk3) {
+BOOL __stdcall GetFilesystemInformation(uint32_t unk1, uint32_t unk2, void* unk3) {
     // STUB - undocumented
 
     return TRUE;