Compare commits

..

1 Commits

Author SHA1 Message Date
coah
65fcc26610 NWC24MsgCommit: 15/18 exact (worker fc42b29c) 2026-09-30 02:56:20 +00:00
80 changed files with 372 additions and 11292 deletions

View File

@@ -53,9 +53,9 @@ Progress
<!-- progress:start -->
| Decompiled | Matched | Linked | Data |
|:---:|:---:|:---:|:---:|
| 87.60% | 77.28% | 56.92% | 88.22% |
| 84.90% | 75.26% | 56.76% | 86.70% |
units 752/1027 complete, functions 11313/12563 matched.
units 748/1027 complete, functions 10978/12563 matched.
decompiled = code with a C/C++ implementation (objdiff fuzzy), matched = byte-exact code, linked = code actually linked into the DOL, data = byte-exact data
<!-- progress:end -->

View File

@@ -1682,11 +1682,11 @@ config.libs = [
]
),
RVLSDKLib("fa", [
Object(Matching, "fa/pf_clib.c"),
Object(Matching, "fa/pf_code.c"),
Object(Matching, "fa/pf_service.c"),
Object(Matching, "fa/pf_str.c"),
Object(Matching, "fa/pf_driver.c"),
Object(NonMatching, "fa/pf_clib.c"),
Object(NonMatching, "fa/pf_code.c"),
Object(NonMatching, "fa/pf_service.c"),
Object(NonMatching, "fa/pf_str.c"),
Object(NonMatching, "fa/pf_driver.c"),
Object(NonMatching, "fa/pdm_api.c"),
Object(NonMatching, "fa/pdm_bpb.c"),
Object(NonMatching, "fa/pdm_disk.c"),
@@ -1733,15 +1733,15 @@ config.libs = [
Object(Matching, "fa/pf_ferror.c"),
Object(NonMatching, "fa/pf_finfo.c"),
Object(Matching, "fa/pf_flock.c"),
Object(Matching, "fa/pf_fopen.c"),
Object(NonMatching, "fa/pf_fopen.c"),
Object(NonMatching, "fa/pf_format.c"),
Object(Matching, "fa/pf_fread.c"),
Object(Matching, "fa/pf_fseek.c"),
Object(NonMatching, "fa/pf_fseek.c"),
Object(NonMatching, "fa/pf_fsetclstlink.c"),
Object(NonMatching, "fa/pf_fsexec.c"),
Object(Matching, "fa/pf_fsfirst.c"),
Object(Matching, "fa/pf_fsnext.c"),
Object(Matching, "fa/pf_fstat.c"),
Object(NonMatching, "fa/pf_fsfirst.c"),
Object(NonMatching, "fa/pf_fsnext.c"),
Object(NonMatching, "fa/pf_fstat.c"),
Object(Matching, "fa/pf_fsync.c"),
Object(Matching, "fa/pf_fwrite.c"),
Object(NonMatching, "fa/pf_getdev.c"),

View File

@@ -20,7 +20,7 @@ namespace textinput {
virtual void moveNigaoeButton() {}
};
#if defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#ifdef MYTIINPUTFORM_IMPLEMENTATION
class ScrollButton;
#endif
@@ -48,7 +48,7 @@ namespace textinput {
#endif
~InputForm();
#if defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#ifdef MYTIINPUTFORM_IMPLEMENTATION
virtual void moveCursorUp();
virtual void moveCursorDown();
virtual void onCommand(INPUT_COMMAND command, void* data);
@@ -131,7 +131,7 @@ namespace textinput {
void setEditScrollDown(bool editScrollDown) { mbEditScrollUp = editScrollDown; }
void setCloseWithSend(bool closeWithSend) { mbCloseWithSend = closeWithSend; }
#if defined(MYTIMANAGER_IMPLEMENTATION) || defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#if defined(MYTIMANAGER_IMPLEMENTATION) || defined(MYTIINPUTFORM_IMPLEMENTATION)
void setEditMode(EditMode editMode);
#else
void setEditMode(EditMode editMode) { meEditMode = editMode; }
@@ -147,12 +147,8 @@ namespace textinput {
tistring::Decolated* getSendString() { return mpSendString; }
#ifdef MYTILETTERFORM_IMPLEMENTATION
protected:
#else
private:
#endif
#if defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#ifdef MYTIINPUTFORM_IMPLEMENTATION
nw4r::lyt::Pane* mpMemoPane;
nw4r::lyt::Pane* mpMemoRootPane;
nw4r::lyt::Pane* mpBoundPane;
@@ -176,7 +172,7 @@ namespace textinput {
nw4r::lyt::Size mDefaultBoundSize; // 0x358
Mtx mDrawMtx; // 0x360
Mtx mBoundMtx; // 0x390
#if defined(MYTIMANAGER_IMPLEMENTATION) || defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#if defined(MYTIMANAGER_IMPLEMENTATION) || defined(MYTIINPUTFORM_IMPLEMENTATION)
util::Animation mExScrollAnm; // 0x3C0
#else
u8 /*todo*/ mExScrollAnm[32]; // 0x3C0
@@ -190,7 +186,7 @@ namespace textinput {
bool mbEditScrollDown; // 0x3EF
bool mbCloseWithSend; // 0x3F0
u8 padding[3]; // 0x3F1
#if defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#ifdef MYTIINPUTFORM_IMPLEMENTATION
ScrollButton* mpScrollButton;
#else
undefined4* mpScrollButton;

View File

@@ -23,15 +23,6 @@ namespace textinput {
unk_0x400(0), mbPhotoDraw(false), meType(T_MailAddressSel) {}
#endif
#ifdef MYTILETTERFORM_IMPLEMENTATION
virtual ~InputForm();
virtual void create(MEMAllocator*, inputform::EditBuffer*);
virtual void drawBody();
virtual void drawFooter();
virtual void open();
virtual void close();
virtual bool isWholePaneInAnimation();
#endif
virtual nw4r::lyt::Material* getPhotoPaneMaterial();
virtual void onPhotoTrig();
@@ -47,11 +38,7 @@ namespace textinput {
void resizePhotoPane(f32 width, f32 height);
private:
#ifdef MYTILETTERFORM_IMPLEMENTATION
bool mbPhotoScaledUp;
#else
u8 unk_0x400;
#endif
bool mbPhotoDraw; // 0x401
Type meType; // 0x404
};

View File

@@ -6,135 +6,9 @@
#include <revolution/mem/allocator.h>
#include "keyboard/tiCpData.h"
#ifdef TIHKBMANAGER_IMPLEMENTATION
#include <revolution/kbd.h>
enum _KBDEc { KBD_EC_BUSY = 7 };
#endif
namespace textinput {
namespace input {
#ifdef TIHKBMANAGER_IMPLEMENTATION
class HKBManager {
public:
class KeyState_ {
public:
void NotifyEvent(u8 down, u8 code);
void Update();
void UpdateModState_();
void Clear() {
held = 0;
triggered = 0;
released = 0;
repeated = 0;
current = 0;
pending = 0;
modifiers &= 0x700;
forceMask = 0;
forceState = 0;
retryLeds = 0;
}
void Initialize(u8 channel) {
held = 0;
triggered = 0;
released = 0;
repeated = 0;
current = 0;
pending = 0;
forceMask = 0;
forceState = 0;
retryLeds = 0;
device = channel;
country = 0xf;
modifiers = 0;
}
u8 device;
u32 held, triggered, released, repeated;
u32 previous, current, pending;
u32 modifiers, forceState, forceMask;
s32 country;
u8 retryLeds;
u8 previousKeys[8], currentKeys[8], pendingKeys[8];
u32 repeatDelay[8];
};
class KeySet {
public:
KeySet() {}
KeySet(const HKBManager* owner, u8 kind)
: manager(owner), type(kind), index(-1), device(0), vcode(0) {}
u8 GetKey() const;
u32 GetWChar() const;
u32 GetVCode() const;
u32 IsValid() const;
bool CheckValidity() const {
if (!manager) return false;
s8 keyIndex = index;
if (keyIndex < 0 || keyIndex >= 8) return false;
u8 keyDevice = device;
if (keyDevice >= 2) return false;
switch (type) {
case 0: return (manager->states[keyDevice].held & (1 << keyIndex)) != 0;
case 1: return (manager->states[keyDevice].triggered & (1 << keyIndex)) != 0;
case 2: return (manager->states[keyDevice].released & (1 << keyIndex)) != 0;
case 3: return (manager->states[keyDevice].repeated & (1 << keyIndex)) != 0;
}
return false;
}
KeySet GetNext() const;
const HKBManager* manager;
u8 type;
s8 index;
u8 device;
mutable u16 vcode;
};
class KBDListenerOwn;
class KBDListenerLinks {
public:
KBDListenerLinks() : previous(0), next(0) {}
virtual ~KBDListenerLinks() {}
virtual void OnAttach(KBDDevEvent*) = 0;
virtual void OnDetach(KBDDevEvent*) = 0;
virtual void OnKeyEvent(KBDKeyEvent*) = 0;
KBDListenerOwn* previous;
KBDListenerOwn* next;
};
class KBDListenerOwn : public KBDListenerLinks {
public:
KBDListenerOwn(HKBManager* owner) : manager(owner) {}
virtual ~KBDListenerOwn();
virtual void OnAttach(KBDDevEvent* event);
virtual void OnDetach(KBDDevEvent* event);
virtual void OnKeyEvent(KBDKeyEvent* event);
HKBManager* manager;
};
HKBManager() __attribute__((never_inline));
~HKBManager();
static HKBManager& getInstance() { return sInstance; }
void Initialize();
void ClearState();
u32 GetModifierState() const;
void SetCountry(u8 country);
void SetModifierState(u32 state, u32 mask);
void SetForceModifierState(u32 state, u32 mask);
void Update();
KeySet GetTriggeredKeySet() const;
KeySet GetReleasedKeySet() const;
KeySet GetRepeatedKeySet() const;
static void SetLedCB(_KBDEc result, void* userData);
static void AttachCB(KBDDevEvent* event);
static void DetachCB(KBDDevEvent* event);
static void KeyEventCB(KBDKeyEvent* event);
private:
u8 initialized;
u8 attached[2];
u32 lockState;
u32 retryDevices;
const wchar_t* allowedCharacters;
u32 allowedCharacterCount;
KeyState_ states[2];
KBDListenerOwn listener;
static HKBManager sInstance;
};
#else
class HKBManager {
public:
#ifdef TIMANAGER_IMPLEMENTATION
@@ -182,7 +56,6 @@ namespace textinput {
static HKBManager sInstance;
};
#endif
} // namespace input
} // namespace textinput

View File

@@ -144,11 +144,6 @@ namespace textinput {
#ifdef TISIGNWINDOW_IMPLEMENTATION
bool isAtokActive() const;
#endif
#ifdef MYTILETTERFORM_IMPLEMENTATION
virtual void create(MEMAllocator* allocator);
virtual void init();
virtual void onCommand(INPUT_COMMAND command, void* data);
#endif
virtual ~Base();
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
@@ -289,7 +284,7 @@ namespace textinput {
virtual void makeUpCursorPos(CursorPos* cursorPos, u32 pos, s32 startLine, s32 endLine);
#if defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#if defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
protected:
#if !defined(TIINPUTFORM_IMPLEMENTATION) && !defined(TIMANAGER_IMPLEMENTATION)
void setCursorPos(tistring::Decolated* string, u32 pos);
@@ -299,7 +294,7 @@ namespace textinput {
#else
private:
#endif
#if defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#if defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
nw4r::ut::Rect mRect;
#else
Rect mRect;
@@ -384,13 +379,13 @@ namespace textinput {
virtual void setRootPaneScaleFor16x9();
virtual void setRootPaneScaleFor4x3();
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
virtual void onSE(sound::SE seId);
#endif
virtual void visibleSeparator(bool flag);
#if defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#if defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
protected:
const void* mpLayoutData;
const void* mpLanguageData;

View File

@@ -14,7 +14,7 @@ namespace textinput {
class TiEventHandler : public gui::EventHandler {
public:
typedef struct Input {
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TISIGNWINDOW_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TISIGNWINDOW_IMPLEMENTATION)
u32 controller;
f32 x;
f32 y;

View File

@@ -3,7 +3,7 @@
#include <revolution/types.h>
#include <revolution/mem/allocator.h>
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TISTRING_IMPLEMENTATION) || defined(TIZISTRING_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
#include <revolution/kpr.h>
#endif
@@ -39,7 +39,7 @@ namespace textinput {
virtual wchar_t* getWCString() const { return mpszString; }
virtual void setCandidate(wchar_t candidate) { mwcCandidate = candidate; }
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TISTRING_IMPLEMENTATION) || defined(TIZISTRING_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
virtual wchar_t getCandidate() const { return mwcCandidate; }
#else
virtual wchar_t getCandidate() { return mwcCandidate; }
@@ -48,11 +48,7 @@ namespace textinput {
virtual wchar_t getLastWChar();
#if defined(TISTRING_IMPLEMENTATION) || defined(TIZISTRING_IMPLEMENTATION)
protected:
#else
private:
#endif
u16 muMaxLength; // 0x04
u16 muLength; // 0x06
@@ -65,7 +61,7 @@ namespace textinput {
};
class KanaStream {
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TISTRING_IMPLEMENTATION) || defined(TIZISTRING_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
public:
KPRQueue mQueue;
wchar_t mPending;
@@ -78,13 +74,13 @@ namespace textinput {
class Decolated : public StringBase {
public:
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TISTRING_IMPLEMENTATION) || defined(TIZISTRING_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
Decolated(u16 maxLen) : StringBase(maxLen), mCursorStart(0), mCursorEnd(0), mbSustain(false), mTranslateMode(0) { initKanaConverter(); }
#else
Decolated(u16 maxLen) : StringBase(maxLen), field_0x18(0), field_0x1C(0), field_0x20(0), field_0x24(0) { initKanaConverter(); }
#endif
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TISTRING_IMPLEMENTATION) || defined(TIZISTRING_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
virtual ~Decolated();
enum TranslateMode { TM_Direct, TM_Kana, TM_Roman, TM_Hangul };
virtual void clear();
@@ -107,7 +103,7 @@ namespace textinput {
virtual u32 getCursorPos() const;
virtual void getCursorPos(u32* start, u32* end);
virtual bool canBackSpace();
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TISTRING_IMPLEMENTATION) || defined(TIZISTRING_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
virtual bool deleteForward();
#else
virtual void deleteForward();
@@ -165,21 +161,12 @@ namespace textinput {
#endif
#if defined(TISTRING_IMPLEMENTATION) || defined(TIZISTRING_IMPLEMENTATION)
protected:
#else
private:
#endif
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TISTRING_IMPLEMENTATION) || defined(TIZISTRING_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
u32 mCursorStart;
u32 mCursorEnd;
#if defined(TISTRING_IMPLEMENTATION) || defined(TIZISTRING_IMPLEMENTATION)
u8 mbSustain;
s32 mTranslateMode;
#else
bool mbSustain;
u32 mTranslateMode;
#endif
#else
u32 field_0x18; // 0x18
u32 field_0x1C; // 0x1C
@@ -189,7 +176,7 @@ namespace textinput {
KanaStream mKanaStream; // 0x28
};
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TISTRING_IMPLEMENTATION) || defined(TIZISTRING_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
class WithAtok : public Decolated {
public:
WithAtok(u16 maxLength) : Decolated(maxLength) {}

View File

@@ -6,13 +6,13 @@
#include <nw4r/ut/CharWriter.h>
#include <nw4r/ut/Rect.h>
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TITEXTDRAWER_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
#include <string.h>
#endif
namespace textinput {
namespace textdrawer {
#if defined(MYTIMANAGER_IMPLEMENTATION) || defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TITEXTDRAWER_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#if defined(MYTIMANAGER_IMPLEMENTATION) || defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
class Base : public nw4r::ut::CharWriter {
#else
class Base : nw4r::ut::CharWriter {
@@ -34,7 +34,7 @@ namespace textinput {
} CursorPos;
typedef struct DrawInfo {
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TITEXTDRAWER_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
nw4r::ut::Rect rect;
wchar_t character;
#else
@@ -94,7 +94,7 @@ namespace textinput {
virtual u32 getEndPos() const;
// todo
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TITEXTDRAWER_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
virtual void setDrawModifyScopeLine(s32 startLine, s32 endLine);
virtual void setDrawCacheScopeLine(s32 startLine, s32 endLine);
virtual void modifyCursorCache(s32 line, u32 position, f32 x, f32 y, f32 width, f32 height);
@@ -103,12 +103,12 @@ namespace textinput {
virtual void setDrawCacheScopeLine();
virtual void modifyCursorCache();
#endif
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TITEXTDRAWER_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
virtual bool isEnableCursorCache() const;
#else
virtual void isEnableCursorCache();
#endif
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TITEXTDRAWER_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
virtual u32 getDrawModifyStartLine() const;
virtual u32 getDrawModifyEndLine() const;
#else
@@ -119,42 +119,7 @@ namespace textinput {
virtual void dirtyDrawCache();
virtual void dirtyCursorCache();
#ifdef TITEXTDRAWER_IMPLEMENTATION
protected:
const wchar_t* mpDrawString;
ViewPort mSavedViewport;
f32 mSavedProjection[7];
nw4r::ut::Rect mProjectionRect;
f32 mfVIWidth;
bool mbAspect4x3;
bool mbDrawClipped;
s32 muLine;
f32 mfLineSpacing;
f32 mfCharacterSpacing;
f32 mfFontWidth;
f32 mfFontHeight;
f32 mfModifyStartY;
f32 mfMinScrollY;
u32 muDrawStartPos;
u32 muDrawEndPos;
u32 muDrawCacheStartPos;
bool mbSecretMode;
s32 muDrawModifyStartLine;
s32 muDrawModifyEndLine;
s32 muDrawModifyStartPos;
s32 muDrawModifyEndPos;
u32 muCachedStartPos;
u32 muCachedEndPos;
bool mbDrawCache;
f32 mfCachedCursorX;
f32 mfCachedCursorY;
f32 mfDrawScrollY;
s32 mnCachedCursorLine;
CursorPos mCachedCursor;
bool mbCursorCache;
bool mbMaintainCursorCache;
#else
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TITEXTDRAWER_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
protected:
MEMAllocator* mpDrawAllocator;
u8 mDrawConfiguration[0x34];
@@ -180,7 +145,7 @@ namespace textinput {
u8 unk_0x50[0x78];
#endif
bool mbSecretMode; // 0xC8
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) || defined(TITEXTDRAWER_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
u32 muDrawModifyStartLine;
u32 muDrawModifyEndLine;
u32 muDrawModifyStartPos;
@@ -202,7 +167,6 @@ namespace textinput {
u8 mDrawCacheState[0x14];
#else
u8 unk_0xCC[0x3C];
#endif
#endif
};
}

View File

@@ -58,7 +58,7 @@ namespace textinput {
CommandReceiver() { clearSender(); }
virtual ~CommandReceiver();
#endif
#if defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(MYTILETTERFORM_IMPLEMENTATION) || defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
#if defined(MYTIINPUTFORM_IMPLEMENTATION) || defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
struct Scroll {
bool absY;
f32 x;

View File

@@ -20,15 +20,9 @@ namespace textinput {
wchar_t KBD_ConvertSmall(wchar_t ch);
wchar_t KBD_ConvertAll(wchar_t ch);
#ifdef TISTRING_IMPLEMENTATION
u32 KBD_IsDakuten(wchar_t ch);
u32 KBD_IsHandaku(wchar_t ch);
u32 KBD_IsSmall(wchar_t ch);
#else
bool KBD_IsDakuten(wchar_t ch);
bool KBD_IsHandaku(wchar_t ch);
bool KBD_IsSmall(wchar_t ch);
#endif
bool strcmp(const char* s1, const char* s2);
void replaceChar(char* dest, u32 destLen, const char* src, int replaceIdx, char newCh);

View File

@@ -1,102 +1,14 @@
#ifndef TEXTINPUT_ZI_STRING_H
#define TEXTINPUT_ZI_STRING_H
#if defined(TIZISTRING_IMPLEMENTATION)
#include "tiString.h"
extern "C" {
#include <eztx.h>
}
#elif defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
#include "tiString.h"
#include <eztx.h>
#endif
namespace textinput {
namespace tistring {
#if defined(TIZISTRING_IMPLEMENTATION)
class WithZi : public Decolated {
public:
enum PredictLanguage {
PL_0,
PL_1,
PL_2,
PL_3,
PL_4,
PL_5,
PL_6,
PL_7,
PL_8,
PL_9,
PL_10,
PL_11
};
enum LetterMode {
LM_0,
LM_1,
LM_2,
LM_3
};
static u16 ElementBuffer[0x100];
static u16 CandidatesBuffer[0x300];
static u16 CandidatedWord[0xa00];
static u16 LatestWord[0x40];
virtual ~WithZi();
virtual void create(MEMAllocator*);
virtual void pushBack(wchar_t);
virtual void popBack();
virtual void clear();
virtual void inputChar(wchar_t);
virtual void backSpace();
virtual void confirm(const wchar_t*);
virtual bool moveCursorRight();
virtual bool moveCursorLeft();
virtual bool canBackSpace();
virtual void EnableKSXFilter(bool);
virtual void init();
virtual void setInputting(wchar_t);
virtual u32 getCurrentNumPredicted();
virtual void getPredicted(int, wchar_t*);
virtual wchar_t* getCurrentSelected();
virtual u16 getInputStringLength();
virtual void setSelectedCandidate(s32);
virtual bool isFix();
virtual void setPredictLaunguage(PredictLanguage);
virtual void changeLetterMode(LetterMode);
virtual void ChangeDictionaryLanguage(u8);
virtual void setCellPhoneHoldingkey(void*);
virtual u32 setElementBuffer();
virtual u8 getPredictLanguage();
void openDictionary(void*, void*);
void clearCandidates();
void partialConfirmForKR();
void update();
u32 complementsCandidates_(s32);
void setCurrentWord(const wchar_t*);
u32 getCurrentInput(wchar_t*, u32);
wchar_t* getCurrentInput();
private:
EZTXGetParam mSearch;
u32 mSearchState;
u8 mbDictionaryOpen;
u8 mbContextChanged;
u32 mCurrentWordLength;
EZTXLanguageEntry* mpDictionaries;
void* mpDictionaryWork;
u16 mInputLength;
u32 mCandidateCount;
u32 mOemDictionaryId;
u8 mDictionaryLanguage;
s32 mSelectedCandidate;
PredictLanguage mPredictLanguage;
LetterMode mLetterMode;
void* mpHoldingKey;
u8 mbKoreanPartialConfirm;
};
#elif defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
#if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION)
class WithZi : public Decolated {
public:
enum PredictLanguage { PL_Default };

View File

@@ -130,18 +130,11 @@ typedef struct EZTXGetParam {
wchar_t* candidates; // 0x18
u8 maxCandidates; // 0x1C
wchar_t firstCandidate; // 0x1E
#ifndef TIZISTRING_IMPLEMENTATION
u8 wordCandidates; // 0x1F
#endif
u8 count; // 0x20
u8 letters; // 0x21
#ifdef TIZISTRING_IMPLEMENTATION
u8 completion;
u8* scratch;
#else
u8* scratch; // 0x22
#endif
} EZTXGetParam;
u16 EZTXGetGlobalDataSize();

View File

@@ -1,53 +0,0 @@
#ifndef PF_FA_PDM_H
#define PF_FA_PDM_H
#include <private/vf/PrFILE2/dskmng/pdm_types.h>
typedef struct PDM_PARTITION PDM_PARTITION;
typedef struct PDM_CALLBACK {
void (*insert)(PDM_PARTITION*);
void (*eject)(PDM_PARTITION*);
} PDM_CALLBACK;
struct PDM_PARTITION {
pf_u32 status;
PDM_DISK* p_disk;
pf_u32 part_id;
pf_u32 start_sector;
pf_u32 total_sector;
pf_s32 driver_last_error;
pf_u32 lock_state[2];
PDM_CALLBACK callbacks;
};
struct PDM_DISK {
pf_u32 status;
PDM_DISK_TBL disk_tbl;
pf_s16 init_count;
pf_s16 mount_count;
PDM_DISK* lock_handle;
pf_u32 lock_count;
PDM_DISK_INFO disk_info;
PDM_PARTITION* p_cur_part;
};
typedef struct PDM_DISK_SET {
pf_u16 num_partition;
pf_u16 num_allocated_disk;
PDM_DISK disk[26];
PDM_PARTITION partition[26];
} PDM_DISK_SET;
extern PDM_DISK_SET pdm_disk_set;
extern PDM_DISK_TBL pdm_drv_tbl[26];
void pdm_part_set_driver_error_code(PDM_PARTITION*, pf_s32);
void pdm_part_set_change_media_state(PDM_DISK*, pf_bool);
pf_s32 pdm_disk_convert_sector_into_block(PDM_DISK*, pf_u32, pf_u32, pf_u32, pf_u32*, pf_u32*);
pf_s32 pdm_disk_convert_block_into_sector(PDM_DISK*, pf_u32, pf_u32, pf_u32, pf_u32*, pf_u32*);
pf_s32 pdm_disk_add_disk(PDM_INIT_DISK*, PDM_DISK**);
pf_s32 pdm_disk_del_disk(PDM_DISK*);
pf_s32 pdm_disk_set_disk(PDM_DISK*, PDM_PARTITION*);
pf_s32 pdm_disk_get_media_attribute(PDM_DISK*, PDM_DISK_INFO*);
pf_s32 pdm_disk_get_media_bps(PDM_DISK*, pf_u16*);
pf_s32 pdm_disk_init(PDM_DISK*);
pf_s32 pdm_disk_mount(PDM_DISK*);
pf_s32 pdm_disk_format(PDM_DISK*, const pf_u8*);
pf_s32 pdm_disk_physical_read(PDM_DISK*, pf_u8*, pf_u32, pf_u32, pf_u32, pf_u32*);
pf_s32 pdm_disk_physical_write(PDM_DISK*, const pf_u8*, pf_u32, pf_u32, pf_u32, pf_u32*);
pf_s32 pdm_disk_unmount(PDM_DISK*, pf_u32);
pf_s32 pdm_disk_finalize(PDM_DISK*);
#endif

View File

@@ -1,17 +0,0 @@
#ifndef PF_FA_SYSTEM_LAYOUT_H
#define PF_FA_SYSTEM_LAYOUT_H
#include <revolution/os.h>
typedef struct PFSYS_SYSTEM_SET {
u32 initialized;
s32 task_id;
s32 mailbox_id;
u8 task_stack[0x2000];
OSThread task;
OSMessageQueue mailbox;
OSMessage messages[20];
u32 flock_count;
} PFSYS_SYSTEM_SET;
extern PFSYS_SYSTEM_SET pf_sys_set;
#endif

View File

@@ -1,96 +0,0 @@
#ifndef PF_FA_VOLUME_LAYOUT_H
#define PF_FA_VOLUME_LAYOUT_H
#define PRFILE2_VFMOD_PF_VOLUME_H
#include <private/vf/PrFILE2/fatfs/pf_entry.h>
#include <private/vf/PrFILE2/driver/pf_driver.h>
#include <private/vf/PrFILE2/fatfs/pf_devinf.h>
typedef struct PF_CHARCODE {
pf_s32 (*oem2unicode)(const pf_s8*, pf_u16*);
pf_s32 (*unicode2oem)(const pf_u16*, pf_s8*);
pf_s32 (*oem_char_width)(const pf_s8*);
pf_bool (*is_oem_mb_char)(const pf_s8, pf_bool);
pf_s32 (*unicode_char_width)(const pf_u16*);
pf_bool (*is_unicode_mb_char)(const pf_u16, pf_bool);
} PF_CHARCODE;
typedef struct PF_BPB {
pf_u16 bytes_per_sector;
pf_u16 num_reserved_sectors;
pf_u16 num_root_dir_entries;
pf_u8 sectors_per_cluster;
pf_u8 num_FATs;
pf_u32 total_sectors;
pf_u32 sectors_per_FAT;
pf_u32 root_dir_cluster;
pf_u16 fs_info_sector;
pf_u16 backup_boot_sector;
pf_u16 ext_flags;
pf_u8 media;
PF_FAT_TYPE fat_type;
pf_u8 log2_bytes_per_sector;
pf_u8 log2_sectors_per_cluster;
pf_u8 num_active_FATs;
pf_u16 num_root_dir_sectors;
pf_u32 active_FAT_sector;
pf_u32 first_root_dir_sector;
pf_u32 first_data_sector;
pf_u32 num_clusters;
} PF_BPB;
typedef struct PF_CUR_DIR {
pf_u32 stat;
pf_s32 context_id;
PF_DIR_ENT directory;
} PF_CUR_DIR;
typedef pf_s32 (*PF_VOLUME_CB)(pf_s32);
struct PF_VOLUME {
PF_BPB bpb;
pf_u32 num_free_clusters;
pf_u32 last_free_cluster;
pf_u8 file_and_directory_state[0x1624 - 0x40];
PF_SECTOR_CACHE cache;
pf_s8 label[12];
PF_CUR_DIR current_dir[4];
struct {
pf_u32 tracker_size;
pf_u32 tracker_max;
pf_u32* tracker_bits;
} tail_entry;
pf_s32 last_error;
pf_s32 last_driver_error;
pf_u32 file_config;
pf_u16 flags;
pf_s8 drv_char;
pf_u16 fsi_flag;
PF_CLUSTER_LINK_VOL cluster_link;
void* p_part;
void (*p_callback)();
const pf_u8* format_param;
};
typedef struct PF_FA_CUR_VOLUME {
pf_u32 stat;
pf_s32 context_id;
PF_VOLUME* p_vol;
} PF_FA_CUR_VOLUME;
typedef struct PF_VOLUME_SET {
pf_u32 flags;
pf_u32 reserved;
PF_VOLUME* current_volume;
PF_FA_CUR_VOLUME context_volumes[3];
pf_s32 num_attached_drives;
pf_s32 num_mounted_volumes;
pf_u32 config;
void* param;
pf_s32 last_error;
pf_s32 last_driver_error;
PF_CHARCODE codeset;
pf_u32 setting;
pf_s32 context_id;
pf_u32 context_state[2];
struct { pf_u32 stat; pf_s32 context_id; } contexts[3];
PF_VOLUME volumes[26];
} PF_VOLUME_SET;
extern PF_VOLUME_SET pf_vol_set;
#endif

View File

@@ -30,21 +30,11 @@ typedef enum _KBDCountryCode {
} KBDCountryCode;
typedef struct _KBDKeyEvent {
#ifdef TIHKBMANAGER_IMPLEMENTATION
u8 device;
u8 key;
u32 flags;
#else
u8 unk_0x00[32];
#endif
} KBDKeyEvent;
typedef struct _KBDDevEvent {
#ifdef TIHKBMANAGER_IMPLEMENTATION
u8 device;
#else
u8 unk_0x00[32];
#endif
} KBDDevEvent;
#ifdef __cplusplus

View File

@@ -1,535 +0,0 @@
#include <private/fa/pdm.h>
#include <revolution/nand.h>
#include <revolution/types.h>
extern void* pf_memset(void*, u8, u32);
extern void* pf_memcpy(void*, const void*, u32);
extern s32 pf_strcmp(const char*, const char*);
extern char* pf_strcpy(char*, const char*);
extern u32 pf_strlen(const char*);
extern void pdm_disk_notify_media_insert(PDM_DISK*);
typedef struct NAND_DISK_INFO {
PDM_DISK* disk;
char path[80];
u32 file_size;
NANDFileInfo file;
u32 flags;
u32 reserved_sectors;
s32 last_error;
} NAND_DISK_INFO;
typedef struct NAND_SEMAPHORE { s32 count; s32 maximum; void* object; } NAND_SEMAPHORE;
extern s32 pfk_create_semaphore(NAND_SEMAPHORE*);
extern void pfk_get_semaphore(s32);
extern void pfk_release_semaphore(s32);
char NAND_Org_Path[64];
NAND_DISK_INFO nanddisk_info[23];
NAND_SEMAPHORE csem;
NAND_SEMAPHORE osem;
s32 Nanddisk_Internal_Info_Init;
s32 NAND_Init;
s32 fa_nanad_semid;
static inline s32 find_disk(PDM_DISK* disk, NAND_DISK_INFO** result) {
NAND_DISK_INFO* info;
u16 index;
for (index = 0; index < 23; index++) {
info = &nanddisk_info[index];
if (info->disk == disk) { *result = info; break; }
}
return index == 23 ? -22 : 0;
}
static inline s32 set_disk_error(PDM_DISK* disk, s32 error) {
u16 index;
for (index = 0; index < 23; index++) {
if (nanddisk_info[index].disk == disk) { nanddisk_info[index].last_error = error; break; }
}
return index == 23 ? -22 : 0;
}
static inline void store_disk_error(PDM_DISK* disk, s32 error) {
u16 index;
for (index = 0; index < 23; index++) {
if (nanddisk_info[index].disk == disk) { nanddisk_info[index].last_error = error; break; }
}
}
static inline NAND_DISK_INFO* find_unmounted_disk(PDM_DISK* disk) {
u16 index;
NAND_DISK_INFO* info;
for (index = 0; index < 23; index++) {
info = &nanddisk_info[index];
if (info->disk == disk) { if (!(info->flags & 2)) { return info; } }
}
for (index = 0; index < 23; index++) {
info = &nanddisk_info[index];
if (info->disk == 0) { if (info->path[0] != 0) { return info; } }
}
return 0;
}
static inline u16 read_u16(u8* buf, u32 offset) {
if ((u32)&buf[offset] & 1) { return (buf[offset + 1] << 8) | buf[offset]; }
return PF_SWAP_16(*(u16*)&buf[(offset + 1) & ~1]);
}
static inline void write_u16(u8* buf, u32 offset, u16 value) {
if ((u32)&buf[offset] & 1) { buf[offset] = value; buf[offset + 1] = value >> 8; }
else { *(u16*)&buf[(offset + 1) & ~1] = PF_SWAP_16(value); }
}
static inline void write_u32(u8* buf, u32 offset, u32 value) {
if ((u32)&buf[offset] & 3) { buf[offset] = value; buf[offset + 1] = value >> 8; buf[offset + 2] = value >> 16; buf[offset + 3] = value >> 24; }
else { *(u32*)&buf[offset] = PF_SWAP_32(value); }
}
static s32 fa_nanddrv_CreateNANDFileForDsk(NAND_DISK_INFO*);
static s32 fa_nanddrv_ParseCreateNANDFile(NAND_DISK_INFO*);
static s32 fa_nanddrv_VerifyBPB(u8*, u32*);
static s32 fa_nanddrv_BuildUpFSInfoSector(u8*);
static s32 fa_nanddrv_BuildUpBootSector(NAND_DISK_INFO*, u8*, PDM_FAT_TYPE*, u32);
static s32 fa_nanddrv_physical_read(u32, u8*, u32, u32, u32*, PDM_DISK*);
static s32 fa_nanddrv_physical_write(u32, const u8*, u32, u32, u32*, PDM_DISK*);
static s32 fa_nanddrv_RelateNANDFileToDsk(PDM_DISK* disk, NAND_DISK_INFO* info, s32 format) {
s32 error = NANDChangeDir(NAND_Org_Path);
if (error != 0) { return error; }
if (info->flags & 4) { error = 0; }
else { error = NANDOpen(info->path, &info->file, 3); }
if (format) { error = -12; }
switch (error) {
case -12:
error = fa_nanddrv_CreateNANDFileForDsk(info);
if (error == 0) { info->disk = disk; }
break;
case 0:
info->disk = disk;
error = 0;
break;
}
return error;
}
static s32 fa_nanddrv_CreateNANDFileForDsk(NAND_DISK_INFO* info) {
u8 zeros[512] __attribute__((aligned(32)));
u8 boot[512] __attribute__((aligned(32)));
u8 fsinfo[512] __attribute__((aligned(32)));
PDM_FAT_TYPE type;
u32 clusters;
s32 error;
u32 remaining;
error = fa_nanddrv_ParseCreateNANDFile(info);
if (error != 0) { return error; }
pf_memset(boot, 0, 512);
clusters = 0;
fa_nanddrv_BuildUpBootSector(info, boot, &type, 0);
if (type != fa_nanddrv_VerifyBPB(boot, &clusters)) { fa_nanddrv_BuildUpBootSector(info, boot, &type, clusters); }
error = NANDWrite(&info->file, boot, 512);
if (error < 0) { NANDClose(&info->file); NANDDelete(info->path); return error; }
remaining = info->file_size - 512;
if (type == PDM_FAT_32) {
fa_nanddrv_BuildUpFSInfoSector(fsinfo);
error = NANDWrite(&info->file, fsinfo, 512);
if (error < 0) { NANDClose(&info->file); NANDDelete(info->path); return error; }
remaining -= 512;
}
pf_memset(zeros, 0, 512);
for (; remaining >= 512; remaining -= 512) {
error = NANDWrite(&info->file, zeros, 512);
if (error < 0) { NANDClose(&info->file); NANDDelete(info->path); return error; }
}
if (remaining) {
error = NANDWrite(&info->file, zeros, remaining);
if (error < 0) { NANDClose(&info->file); NANDDelete(info->path); return error; }
}
return 0;
}
static s32 fa_nanddrv_ParseCreateNANDFile(NAND_DISK_INFO* info) {
u16 length = 0;
char* path;
u16 position = 0;
char name[16];
char* next;
s32 error;
if (info->flags & 4) { return 0; }
path = info->path;
pf_memset(name, 0, 16);
next = name;
for (;;) {
switch (*path) {
case '/':
if (length == 0) {
if (position == 0) { error = NANDChangeDir("/"); if (error != 0) { return error; } }
path++;
break;
}
if (path[1] == 0) {
if (length > 12) { return -20; }
error = NANDCreate(name, 0x30, 0);
if (error == 0 || error == -6) {
NANDOpen(name, &info->file, 3);
error = NANDChangeDir(NAND_Org_Path);
}
return error;
}
if (length > 11) { return -20; }
if (pf_strcmp(name, "..") != 0) { error = NANDCreateDir(name, 0x30, 0); }
else { error = 0; }
if (error == 0 || error == -6 || error == -1) {
error = NANDChangeDir(name);
if (error != 0) { return error; }
path++;
pf_memset(name, 0, 16);
next = name;
length = 0;
break;
}
return error;
case 0:
if (length > 12) { return -20; }
error = NANDCreate(name, 0x30, 0);
if (error == 0 || error == -6) {
NANDOpen(name, &info->file, 3);
error = NANDChangeDir(NAND_Org_Path);
}
return error;
}
*next = *path;
length++;
position++;
path++;
next++;
}
}
static s32 fa_nanddrv_VerifyBPB(u8* buf, u32* clusters) {
u8 sector_log = 0;
u8 cluster_log;
u16 value = 512;
u32 root;
u32 fat;
u32 total;
u16 root_sectors;
PDM_FAT_TYPE result;
u32 first;
while ((value >>= 1) != 0) { sector_log++; }
cluster_log = 0;
value = 1;
while ((value >>= 1) != 0) { cluster_log++; }
if ((u32)&buf[17] & 1) { root = (buf[18] << 8) | buf[17]; }
else { root = PF_SWAP_16(*(u16*)&buf[18]) << 5; }
root_sectors = (s32)(root + 511) >> sector_log;
fat = read_u16(buf, 22);
if (fat == 0) { fat = PF_SWAP_32(*(u32*)&buf[36]); }
first = root_sectors + fat * 2 + 1;
if ((u32)&buf[19] & 1) { total = (buf[20] << 8) | buf[19]; }
else { total = PF_SWAP_16(*(u16*)&buf[20]); }
if (total == 0) { total = PF_SWAP_32(*(u32*)&buf[32]); }
*clusters = total - first;
total = (u32)(total - first) >> cluster_log;
if (total < 4085) { return 0; }
result = PDM_FAT_32;
if ((u32)total < 65525) { result = PDM_FAT_16; }
return result;
}
#pragma dont_inline on
static s32 fa_nanddrv_BuildUpFSInfoSector(u8* buf) {
pf_memset(buf, 0, 512);
if ((u32)&buf[0] & 3) { buf[0] = 0x52; buf[1] = 0x52; buf[2] = 0x61; buf[3] = 0x41; }
else { *(u32*)&buf[0] = 0x52526141; }
if ((u32)&buf[484] & 3) { buf[484] = 0x72; buf[485] = 0x72; buf[486] = 0x41; buf[487] = 0x61; }
else { *(u32*)&buf[484] = 0x72724161; }
if ((u32)&buf[488] & 3) { buf[488] = 0xFF; buf[489] = 0xFF; buf[490] = 0xFF; buf[491] = 0xFF; }
else { *(u32*)&buf[488] = 0xFFFFFFFF; }
if ((u32)&buf[492] & 3) { buf[492] = 0xFF; buf[493] = 0xFF; buf[494] = 0xFF; buf[495] = 0xFF; }
else { *(u32*)&buf[492] = 0xFFFFFFFF; }
if ((u32)&buf[508] & 3) { buf[508] = 0x00; buf[509] = 0x00; buf[510] = 0x55; buf[511] = 0xAA; }
else { *(u32*)&buf[508] = 0x000055AA; }
return 0;
}
#pragma dont_inline reset
static s32 fa_nanddrv_BuildUpBootSector(NAND_DISK_INFO* info, u8* buf, PDM_FAT_TYPE* type, u32 clusters) {
u32 total;
u32 fat_sectors;
PDM_FAT_TYPE fat_type;
total = clusters ? clusters : info->file_size >> 9;
if (total < 4085) { fat_type = PDM_FAT_12; fat_sectors = (((total * 3) >> 1) + 511) >> 9; }
else if (total < 65525) { fat_type = PDM_FAT_16; fat_sectors = (total * 2 + 511) >> 9; }
else { fat_type = PDM_FAT_32; fat_sectors = (total * 4 + 511) >> 9; }
pf_memset(buf, 0, 512);
buf[0] = 0xEB; buf[2] = 0x90; buf[510] = 0x55; buf[511] = 0xAA;
write_u16(buf, 24, 63);
write_u16(buf, 26, 255);
write_u16(buf, 11, 512);
buf[13] = 1;
write_u16(buf, 14, 1);
buf[16] = 2;
write_u16(buf, 17, 128);
buf[21] = 0xF0;
if (total < 65536) { write_u16(buf, 19, total); write_u32(buf, 32, 0); }
else { write_u16(buf, 19, 0); write_u32(buf, 32, total); }
if (fat_type == PDM_FAT_32) {
write_u16(buf, 17, 0);
write_u32(buf, 36, fat_sectors);
write_u16(buf, 40, 0); write_u16(buf, 42, 0);
write_u32(buf, 44, 2); write_u16(buf, 48, 1);
buf[65] = 0; buf[66] = 0x29;
if ((u32)&buf[67] & 3) { buf[67] = 0x34; buf[68] = 0x12; buf[69] = 0; buf[70] = 0; }
else { *(u32*)&buf[76] = 0x34120000; }
} else {
write_u16(buf, 17, 128); write_u16(buf, 22, fat_sectors);
buf[37] = 0; buf[38] = 0x29;
if ((u32)&buf[39] & 3) { buf[39] = 0x34; buf[40] = 0x12; buf[41] = 0; buf[42] = 0; }
else { *(u32*)&buf[48] = 0x34120000; }
}
*type = fat_type;
return 0;
}
s32 fa_nanddrv_init(PDM_DISK* disk) {
if (disk == 0) { return -20; }
if (NAND_Init == 0) {
if (NANDInit() != 0) { return -21; }
if (NANDGetCurrentDir(NAND_Org_Path) != 0) { return -21; }
NAND_Init++;
}
pdm_disk_notify_media_insert(disk);
return 0;
}
s32 fa_nanddrv_mount(PDM_DISK* disk) {
NAND_DISK_INFO* info;
NAND_DISK_INFO* mounted;
s32 error;
pfk_get_semaphore(fa_nanad_semid);
if (disk == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; }
if (find_disk(disk, &mounted) == 0 && (mounted->flags & 2)) { pfk_release_semaphore(fa_nanad_semid); return -21; }
info = find_unmounted_disk(disk);
if (info == 0) { pfk_release_semaphore(fa_nanad_semid); return -22; }
if (info->disk != 0) { error = 0; }
else { error = fa_nanddrv_RelateNANDFileToDsk(disk, info, 0); }
if (error == 0) { info->flags |= 2; }
pfk_release_semaphore(fa_nanad_semid);
return error;
}
s32 fa_nanddrv_format(PDM_DISK* disk, const u8* param) {
NAND_DISK_INFO* info = 0;
s32 error;
pfk_get_semaphore(fa_nanad_semid);
if (disk == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; }
if (find_disk(disk, &info) != 0) { pfk_release_semaphore(fa_nanad_semid); return -20; }
info->flags |= 4;
error = fa_nanddrv_RelateNANDFileToDsk(disk, info, 1);
info->flags &= ~4;
pfk_release_semaphore(fa_nanad_semid);
return error;
}
s32 fa_nanddrv_pread(PDM_DISK* disk, u8* buf, u32 block, u32 count, u32* success) {
s32 error;
pfk_get_semaphore(fa_nanad_semid);
*success = 0;
if (disk == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; }
if (buf == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; }
error = fa_nanddrv_physical_read(count, buf, block, 512, success, disk);
if (error != 0) { pfk_release_semaphore(fa_nanad_semid); return error; }
pfk_release_semaphore(fa_nanad_semid);
return 0;
}
s32 fa_nanddrv_pwrite(PDM_DISK* disk, const u8* buf, u32 block, u32 count, u32* success) {
s32 error;
pfk_get_semaphore(fa_nanad_semid);
*success = 0;
if (disk == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; }
if (buf == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; }
error = fa_nanddrv_physical_write(count, buf, block, 512, success, disk);
if (error != 0) { pfk_release_semaphore(fa_nanad_semid); return error; }
pfk_release_semaphore(fa_nanad_semid);
return 0;
}
s32 fa_nanddrv_unmount(PDM_DISK* disk) {
NAND_DISK_INFO* info = 0;
u32 position;
s32 error;
s32 result;
pfk_get_semaphore(fa_nanad_semid);
if (disk == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; }
error = set_disk_error(disk, 0);
if (error == 0) {
if (find_disk(disk, &info) != 0) { pfk_release_semaphore(fa_nanad_semid); return -20; }
info->flags &= ~2;
error = NANDTell(&info->file, &position);
if (error != 0) { result = error; goto flush_done; }
error = NANDClose(&info->file);
if (error < 0) { result = error; goto flush_done; }
error = NANDOpen(info->path, &info->file, 3);
if (error < 0) { result = error; goto flush_done; }
error = NANDSeek(&info->file, position, 0);
result = error < 0 ? error : 0;
flush_done:
error = result;
if (error < 0) { set_disk_error(disk, error); pfk_release_semaphore(fa_nanad_semid); return error; }
}
pfk_release_semaphore(fa_nanad_semid);
return error;
}
s32 fa_nanddrv_finalize(PDM_DISK* disk) {
NAND_DISK_INFO* info;
s32 error = 0;
u16 index;
pfk_get_semaphore(fa_nanad_semid);
if (disk == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; }
for (index = 0; index < 23; index++) {
info = &nanddisk_info[index];
if (info->disk == disk) {
error = NANDClose(&info->file);
pf_memset(info, 0, sizeof(*info));
break;
}
}
pfk_release_semaphore(fa_nanad_semid);
if (error != 0) { return -20; }
return index == 23 ? -22 : 0;
}
s32 fa_nanddrv_get_disk_info(PDM_DISK* disk, PDM_DISK_INFO* disk_info) {
NAND_DISK_INFO* info = 0;
pfk_get_semaphore(fa_nanad_semid);
if (disk == 0 || disk_info == 0) { pfk_release_semaphore(fa_nanad_semid); return -20; }
if (find_disk(disk, &info) != 0) { pfk_release_semaphore(fa_nanad_semid); return -20; }
disk_info->total_sectors = info->file_size >> 9;
disk_info->cylinders = disk_info->total_sectors / 255 / 63 / 512;
disk_info->heads = 255;
disk_info->sectors_per_track = 63;
disk_info->bytes_per_sector = 512;
disk_info->media_attr = 0;
disk_info->format_param = 0;
pfk_release_semaphore(fa_nanad_semid);
return 0;
}
const PDM_FUNCTBL fa_nand_func = {
fa_nanddrv_init, fa_nanddrv_finalize, fa_nanddrv_mount, fa_nanddrv_unmount,
fa_nanddrv_format, fa_nanddrv_pread, fa_nanddrv_pwrite, fa_nanddrv_get_disk_info
};
s32 fa_nanddrv_init_drv_tbl(PDM_DISK_TBL* table, u32 extension) {
table->ui_ext = extension;
table->p_func = (PDM_FUNCTBL*)&fa_nand_func;
return 0;
}
s32 fa_nanddrv_NotifyNANDFile(const char* path, u32 size) {
NAND_DISK_INFO* info;
u16 index;
if (pf_strlen(path) >= 76) { return -20; }
if (Nanddisk_Internal_Info_Init == 0) {
pf_memset(nanddisk_info, 0, sizeof(nanddisk_info));
Nanddisk_Internal_Info_Init++;
csem.count = 1;
csem.maximum = 1;
csem.object = &osem;
fa_nanad_semid = pfk_create_semaphore(&csem);
if (fa_nanad_semid == 0) { return -21; }
}
for (index = 0; index < 23; index++) {
info = &nanddisk_info[index];
if (pf_strcmp(info->path, path) == 0) { return 0; }
if (nanddisk_info[index].disk == 0) {
pf_strcpy(info->path, path);
info->file_size = size;
break;
}
}
return index == 23 ? -22 : 0;
}
static s32 fa_nanddrv_physical_read(u32 count, u8* buf, u32 block, u32 bps, u32* success, PDM_DISK* disk) {
u32 file_size;
u32 wanted;
u32 remaining;
s32 error;
NAND_DISK_INFO* info = 0;
u16 index;
u32 offset;
u32 position;
u8 work[512] __attribute__((aligned(32)));
for (index = 0; index < 23; index++) {
if (nanddisk_info[index].disk == disk) { info = &nanddisk_info[index]; break; }
}
if ((index == 23 ? -22 : 0) != 0) { return -20; }
remaining = count * bps;
file_size = info->file_size;
wanted = block * bps;
if (NANDTell(&info->file, &position) != 0 || position != wanted) { error = NANDSeek(&info->file, wanted, 0); }
else { error = 0; }
if (error >= 0) {
if (wanted + remaining > file_size) {
remaining = file_size - wanted;
offset = 0;
while (remaining >= 512) {
error = NANDRead(&info->file, work, 512);
if (error < 0) { return -22; }
pf_memcpy(buf + offset, work, error);
offset += error; remaining -= error;
}
if (remaining) {
error = NANDRead(&info->file, work, remaining);
if (error < 0) { return -22; }
pf_memcpy(buf + offset, work, error);
}
return -22;
}
offset = 0;
while (remaining >= 512) {
error = NANDRead(&info->file, work, 512);
if (error < 0) { store_disk_error(disk, error); return error; }
pf_memcpy(buf + offset, work, error);
offset += error; remaining -= error;
}
if (remaining) {
error = NANDRead(&info->file, work, remaining);
if (error < 0) { store_disk_error(disk, error); return error; }
pf_memcpy(buf + offset, work, error);
}
*success = count;
return 0;
}
store_disk_error(disk, error);
return error;
}
static s32 fa_nanddrv_physical_write(u32 count, const u8* buf, u32 block, u32 bps, u32* success, PDM_DISK* disk) {
u32 remaining;
u32 wanted;
u32 file_size;
NAND_DISK_INFO* info = 0;
u16 index;
u32 offset;
u32 position;
s32 error;
u8 work[512] __attribute__((aligned(32)));
for (index = 0; index < 23; index++) {
if (nanddisk_info[index].disk == disk) { info = &nanddisk_info[index]; break; }
}
if ((index == 23 ? -22 : 0) != 0) { return -20; }
remaining = count * bps;
file_size = info->file_size;
wanted = block * bps;
if (NANDTell(&info->file, &position) != 0 || position != wanted) { error = NANDSeek(&info->file, wanted, 0); }
else { error = 0; }
if (error >= 0) {
if (wanted + remaining > file_size) {
remaining = file_size - wanted;
offset = 0;
while (remaining >= 512) {
pf_memcpy(work, buf + offset, 512);
error = NANDWrite(&info->file, work, 512);
if (error < 0) { return -22; }
remaining -= error; offset += error;
}
if (remaining) {
pf_memcpy(work, buf + offset, remaining);
error = NANDWrite(&info->file, work, remaining);
}
return -22;
}
offset = 0;
while (remaining >= 512) {
pf_memcpy(work, buf + offset, 512);
error = NANDWrite(&info->file, work, 512);
if (error < 0) { store_disk_error(disk, error); return error; }
remaining -= error; offset += error;
}
if (remaining) {
pf_memcpy(work, buf + offset, remaining);
error = NANDWrite(&info->file, work, remaining);
if (error < 0) { store_disk_error(disk, error); return error; }
}
*success = count;
return 0;
}
store_disk_error(disk, error);
return error;
}

View File

@@ -1,99 +0,0 @@
#define PF_FA_STR_LAYOUT
#include <private/fa/pf_volume.h>
#include <private/vf/PrFILE2/dskmng/pdm_bpb.h>
extern pf_bool pdm_part_is_boot_sector(pf_u8*);
static inline pf_u16 read_boot_u16(pf_u8* buf, pf_u32 offset) {
if ((pf_u32)&buf[offset] & 1) {
return (buf[offset + 1] << 8) | buf[offset];
}
return PF_SWAP_16(*(pf_u16*)&buf[(offset + 1) & ~1]);
}
void pdm_bpb_CalcBPBFields(PF_VOLUME* p_vol) {
pf_u32 num_data_sectors;
pf_u16 val;
p_vol->bpb.log2_bytes_per_sector = 0;
val = p_vol->bpb.bytes_per_sector;
while (val >>= 1) { p_vol->bpb.log2_bytes_per_sector++; }
p_vol->bpb.log2_sectors_per_cluster = 0;
val = p_vol->bpb.sectors_per_cluster;
while (val >>= 1) { p_vol->bpb.log2_sectors_per_cluster++; }
p_vol->bpb.num_root_dir_sectors = ((p_vol->bpb.num_root_dir_entries << 5) + (p_vol->bpb.bytes_per_sector - 1)) >> p_vol->bpb.log2_bytes_per_sector;
p_vol->bpb.first_data_sector = (p_vol->bpb.num_FATs * p_vol->bpb.sectors_per_FAT) + (p_vol->bpb.num_root_dir_sectors + p_vol->bpb.num_reserved_sectors);
num_data_sectors = p_vol->bpb.total_sectors - p_vol->bpb.first_data_sector;
p_vol->bpb.num_clusters = num_data_sectors >> p_vol->bpb.log2_sectors_per_cluster;
if (p_vol->bpb.num_clusters < 0xFF5) {
p_vol->bpb.fat_type = FAT_12;
p_vol->bpb.first_root_dir_sector = p_vol->bpb.num_reserved_sectors + p_vol->bpb.num_FATs * p_vol->bpb.sectors_per_FAT;
} else if (p_vol->bpb.num_clusters < 0xFFF5) {
p_vol->bpb.fat_type = FAT_16;
p_vol->bpb.first_root_dir_sector = p_vol->bpb.num_reserved_sectors + p_vol->bpb.num_FATs * p_vol->bpb.sectors_per_FAT;
} else {
p_vol->bpb.fat_type = FAT_32;
p_vol->bpb.first_root_dir_sector = 0;
}
}
pf_s32 pdm_bpb_InitVolumeWithBootSector(PF_VOLUME* p_vol, pf_u8* buf) {
pf_u32 total_sectors;
pf_s32 err = 0;
pf_u32 fat_sectors;
pf_u32 version;
if (!pdm_part_is_boot_sector(buf)) {
err = 3;
} else {
p_vol->bpb.bytes_per_sector = read_boot_u16(buf, 11);
p_vol->bpb.sectors_per_cluster = buf[13];
p_vol->bpb.num_reserved_sectors = read_boot_u16(buf, 14);
p_vol->bpb.num_FATs = buf[16];
p_vol->bpb.num_root_dir_entries = read_boot_u16(buf, 17);
p_vol->bpb.media = buf[21];
total_sectors = read_boot_u16(buf, 19);
p_vol->bpb.total_sectors = total_sectors == 0 ? PF_SWAP_32(*(pf_u32*)&buf[32]) : total_sectors;
fat_sectors = read_boot_u16(buf, 22);
p_vol->bpb.sectors_per_FAT = fat_sectors == 0 ? PF_SWAP_32(*(pf_u32*)&buf[36]) : fat_sectors;
pdm_bpb_CalcBPBFields(p_vol);
switch (p_vol->bpb.fat_type) {
case FAT_12:
case FAT_16:
if (fat_sectors == 0) { err = 4; }
break;
case FAT_32:
if (total_sectors != 0 || fat_sectors != 0) { err = 4; }
version = read_boot_u16(buf, 42);
p_vol->bpb.ext_flags = read_boot_u16(buf, 40);
p_vol->bpb.root_dir_cluster = PF_SWAP_32(*(pf_u32*)&buf[44]);
p_vol->bpb.fs_info_sector = read_boot_u16(buf, 48);
p_vol->bpb.backup_boot_sector = read_boot_u16(buf, 50);
p_vol->bpb.first_root_dir_sector = p_vol->bpb.first_data_sector + ((p_vol->bpb.root_dir_cluster - 2) << p_vol->bpb.log2_sectors_per_cluster);
if (version != 0) { err = 4; }
break;
default:
err = 4;
break;
}
if (p_vol->bpb.ext_flags & 0x80) {
p_vol->bpb.num_active_FATs = 1;
p_vol->bpb.active_FAT_sector = p_vol->bpb.num_reserved_sectors + ((p_vol->bpb.ext_flags & 7) * p_vol->bpb.sectors_per_FAT);
} else {
p_vol->bpb.num_active_FATs = p_vol->bpb.num_FATs;
p_vol->bpb.active_FAT_sector = p_vol->bpb.num_reserved_sectors;
}
}
return err;
}
pf_s32 pdm_bpb_InitVolumeWithFSINFOSector(PF_VOLUME* p_vol, pf_u8* buf, pf_u32* p_free_count, pf_u32* p_next_free) {
pf_u32 free_count;
pf_u32 next_free;
*p_free_count = -1;
*p_next_free = -1;
if (PF_SWAP_32(*(pf_u32*)&buf[0]) == 0x41615252 && PF_SWAP_32(*(pf_u32*)&buf[484]) == 0x61417272 && PF_SWAP_32(*(pf_u32*)&buf[508]) == 0xAA550000) {
free_count = PF_SWAP_32(*(pf_u32*)&buf[488]);
next_free = PF_SWAP_32(*(pf_u32*)&buf[492]);
if (free_count >= 2 && free_count < p_vol->bpb.num_clusters + 2) { *p_free_count = free_count; }
if (next_free >= 2 && next_free < p_vol->bpb.num_clusters + 2) { *p_next_free = next_free; }
}
return 0;
}

View File

@@ -1,161 +0,0 @@
#include <private/fa/pdm.h>
extern void* pf_memset(void*, pf_u8, pf_u32);
PDM_DISK_SET pdm_disk_set;
PDM_DISK_TBL pdm_drv_tbl[26];
pf_s32 pdm_disk_convert_sector_into_block(PDM_DISK* p_disk, pf_u32 start, pf_u32 count, pf_u32 bytes_per_sector, pf_u32* p_start, pf_u32* p_count) {
pf_u32 ratio;
pf_s32 err = 0;
*p_start = start;
*p_count = count;
if (bytes_per_sector > 512) {
ratio = (pf_u16)bytes_per_sector >> 9;
if (ratio == 2) { *p_start <<= 1; }
else if (ratio == 4) { *p_start <<= 2; }
else if (ratio == 8) { *p_start <<= 3; }
if (ratio == 2) { *p_count <<= 1; }
else if (ratio == 4) { *p_count <<= 2; }
else if (ratio == 8) { *p_count <<= 3; }
} else if (bytes_per_sector < 512) { err = 4; }
return err;
}
pf_s32 pdm_disk_convert_block_into_sector(PDM_DISK* p_disk, pf_u32 start, pf_u32 count, pf_u32 bytes_per_sector, pf_u32* p_start, pf_u32* p_count) {
pf_u32 ratio;
pf_s32 err = 0;
*p_start = start;
*p_count = count;
if (bytes_per_sector > 512) {
ratio = (pf_u16)bytes_per_sector >> 9;
if (ratio == 2) { *p_start >>= 1; }
else if (ratio == 4) { *p_start >>= 2; }
else if (ratio == 8) { *p_start >>= 3; }
if (ratio == 2) { *p_count >>= 1; }
else if (ratio == 4) { *p_count >>= 2; }
else if (ratio == 8) { *p_count >>= 3; }
} else if (bytes_per_sector < 512) { err = 4; }
return err;
}
pf_s32 pdm_disk_init_disk_manager(void) {
pf_memset(&pdm_disk_set, 0, sizeof(pdm_disk_set));
return 0;
}
pf_s32 pdm_disk_add_disk(PDM_INIT_DISK* p_init_disk_tbl, PDM_DISK** pp_disk) {
PDM_DISK_TBL* p_driver;
pf_u16 disk_no;
PDM_DISK* p_disk;
*pp_disk = PF_NULL;
if (pdm_disk_set.num_allocated_disk >= 26) { return 8; }
for (disk_no = 0; disk_no < 26; disk_no++) {
if (!(pdm_disk_set.disk[disk_no].status & 1)) { break; }
}
if (disk_no >= 26) { return 8; }
p_driver = &pdm_drv_tbl[disk_no];
p_init_disk_tbl->p_func(p_driver, p_init_disk_tbl->ui_ext);
p_disk = &pdm_disk_set.disk[disk_no];
p_disk->disk_tbl = *p_driver;
p_disk->status |= 1;
pdm_disk_set.num_allocated_disk++;
*pp_disk = p_disk;
return 0;
}
pf_s32 pdm_disk_del_disk(PDM_DISK* p_disk) {
if (p_disk->init_count != 0) { return 16; }
if (p_disk->mount_count != 0) { return 17; }
p_disk->status &= ~1;
pdm_disk_set.num_allocated_disk--;
return 0;
}
pf_s32 pdm_disk_set_disk(PDM_DISK* p_disk, PDM_PARTITION* p_part) {
p_disk->p_cur_part = p_part;
return 0;
}
pf_s32 pdm_disk_get_media_attribute(PDM_DISK* p_disk, PDM_DISK_INFO* p_info) {
*p_info = p_disk->disk_info;
return 0;
}
pf_s32 pdm_disk_get_media_bps(PDM_DISK* p_disk, pf_u16* p_bps) {
*p_bps = p_disk->disk_info.bytes_per_sector;
return 0;
}
pf_s32 pdm_disk_init(PDM_DISK* p_disk) {
pf_s32 err;
if (p_disk->init_count == 0) {
err = p_disk->disk_tbl.p_func->init(p_disk);
if (err != 0) { pdm_part_set_driver_error_code(p_disk->p_cur_part, err); return 21; }
}
p_disk->init_count++;
return 0;
}
pf_s32 pdm_disk_mount(PDM_DISK* p_disk) {
pf_s32 err;
if (p_disk->mount_count == 0) {
err = p_disk->disk_tbl.p_func->mount(p_disk);
if (err != 0) { pdm_part_set_driver_error_code(p_disk->p_cur_part, err); return 21; }
err = p_disk->disk_tbl.p_func->get_disk_info(p_disk, &p_disk->disk_info);
if (err != 0) {
pdm_part_set_driver_error_code(p_disk->p_cur_part, err);
p_disk->disk_tbl.p_func->unmount(p_disk);
return 21;
}
if (p_disk->disk_info.media_attr & 1) { p_disk->status |= 2; }
else { p_disk->status &= ~2; }
}
p_disk->mount_count++;
return 0;
}
pf_s32 pdm_disk_format(PDM_DISK* p_disk, const pf_u8* param) {
pf_s32 err;
err = p_disk->disk_tbl.p_func->format(p_disk, param);
if (err != 0) { pdm_part_set_driver_error_code(p_disk->p_cur_part, err); return 21; }
return 0;
}
pf_s32 pdm_disk_physical_read(PDM_DISK* p_disk, pf_u8* buf, pf_u32 sector, pf_u32 count, pf_u32 bps, pf_u32* p_success) {
pf_s32 err;
pf_u32 block;
pf_u32 num_blocks;
pf_u32 converted_sector;
err = pdm_disk_convert_sector_into_block(p_disk, sector, count, bps, &block, &num_blocks);
if (err != 0) { return err; }
err = p_disk->disk_tbl.p_func->physical_read(p_disk, buf, block, num_blocks, p_success);
pdm_disk_convert_block_into_sector(p_disk, block, *p_success, bps, &converted_sector, p_success);
if (err != 0) { pdm_part_set_driver_error_code(p_disk->p_cur_part, err); return 21; }
return 0;
}
pf_s32 pdm_disk_physical_write(PDM_DISK* p_disk, const pf_u8* buf, pf_u32 sector, pf_u32 count, pf_u32 bps, pf_u32* p_success) {
pf_s32 err;
pf_u32 block;
pf_u32 num_blocks;
pf_u32 converted_sector;
err = pdm_disk_convert_sector_into_block(p_disk, sector, count, bps, &block, &num_blocks);
if (err != 0) { return err; }
err = p_disk->disk_tbl.p_func->physical_write(p_disk, buf, block, num_blocks, p_success);
pdm_disk_convert_block_into_sector(p_disk, block, *p_success, bps, &converted_sector, p_success);
if (err != 0) { pdm_part_set_driver_error_code(p_disk->p_cur_part, err); return 21; }
return 0;
}
pf_s32 pdm_disk_unmount(PDM_DISK* p_disk, pf_u32 mode) {
pf_s32 err = 0;
if (p_disk->mount_count == 1) {
err = p_disk->disk_tbl.p_func->unmount(p_disk);
if (err != 0) { pdm_part_set_driver_error_code(p_disk->p_cur_part, err); err = 21; }
} else if (p_disk->mount_count == 0) { return 14; }
if (err == 0 || mode == 1) { p_disk->mount_count--; }
return err;
}
pf_s32 pdm_disk_finalize(PDM_DISK* p_disk) {
pf_s32 err;
if (p_disk->init_count == 1) {
err = p_disk->disk_tbl.p_func->finalize(p_disk);
if (err != 0) { pdm_part_set_driver_error_code(p_disk->p_cur_part, err); return 21; }
} else if (p_disk->init_count == 0) { return 13; }
p_disk->init_count--;
return 0;
}
void pdm_disk_notify_media_eject(PDM_DISK* p_disk) {
p_disk->status &= ~4;
pdm_part_set_change_media_state(p_disk, 0);
}
void pdm_disk_notify_media_insert(PDM_DISK* p_disk) {
p_disk->status |= 4;
pdm_part_set_change_media_state(p_disk, 1);
}

View File

@@ -1,287 +0,0 @@
#include <private/fa/pdm.h>
#include <revolution/types.h>
#define MBR_WORD(buf, offset) (((pf_u32)(buf)[(offset) + 2] << 16) + (buf)[offset] + (((pf_u32)(buf)[(offset) + 3] << 24) + ((pf_u32)(buf)[(offset) + 1] << 8)))
static inline pf_u16 read_boot_u16(pf_u8* buf, pf_u32 offset) {
if ((pf_u32)&buf[offset] & 1) { return (buf[offset + 1] << 8) | buf[offset]; }
return PF_SWAP_16(*(pf_u16*)&buf[(offset + 1) & ~1]);
}
pf_s32 pdm_part_is_master_boot_sector(pf_u8* buf, pf_u32 total, pf_bool* p_is_mbr) {
pf_s16 index;
pf_u32 start[4];
pf_u32 count[4];
*p_is_mbr = 0;
if (buf[510] != 0x55 || buf[511] != 0xAA) { return 2; }
for (index = 0; index < 4; index++) {
start[index] = 0;
start[index] = (((pf_u32)buf[456] << 16) + buf[454]) + (((pf_u32)buf[457] << 24) + ((pf_u32)buf[455] << 8));
count[index] = (((pf_u32)buf[460] << 16) + buf[458]) + (((pf_u32)buf[461] << 24) + ((pf_u32)buf[459] << 8));
if (start[index] != 0 && count[index] != 0) {
if (index == 0) { *p_is_mbr = 1; }
} else {
if (index == 0) { return 2; }
}
buf += 16;
}
for (index = 0; index < 4; index++) {
if (start[index] + count[index] > total) {
if (index == 0) { *p_is_mbr = 0; }
return 2;
}
}
return 0;
}
pf_bool pdm_part_is_boot_sector(pf_u8* buf) {
pf_u16 byte_per_sector;
pf_u16 sector_per_cluster;
pf_u8 media;
pf_bool is_boot;
is_boot = PF_TRUE;
if ((buf[0x00] != 0xEB || buf[0x2] != 0x90) && buf[0x0] != 0xE9) {
is_boot = PF_FALSE;
}
if (buf[0x1FE] != 0x55 || buf[0x1FF] != 0xAA) {
is_boot = PF_FALSE;
}
byte_per_sector = read_boot_u16(buf, 0xB);
if (byte_per_sector != 0x200 && byte_per_sector != 0x400 && byte_per_sector != 0x800 && byte_per_sector != 0x1000) {
is_boot = PF_FALSE;
}
sector_per_cluster = buf[0xD];
if (sector_per_cluster != 1 && sector_per_cluster != 2 && sector_per_cluster != 4 && sector_per_cluster != 8 && sector_per_cluster != 0x10 &&
sector_per_cluster != 0x20 && sector_per_cluster != 0x40 && sector_per_cluster != 0x80) {
is_boot = PF_FALSE;
}
media = buf[0x15];
if (media != 0xF0 && media != 0xF8 && media != 0xF9 && media != 0xFA && media != 0xFB && media != 0xFC && media != 0xFD && media != 0xFE &&
media != 0xFF) {
is_boot = PF_FALSE;
}
return is_boot;
}
pf_s32 pdm_part_get_start_sector(PDM_PARTITION* p_part) {
pf_u32 part_index = 0;
pf_u32 requested = p_part->part_id;
pf_u32 start[4];
pf_u32 count[4];
pf_u16 index;
pf_u16 extended_index;
pf_u32 base;
pf_u32 sector;
pf_u32 length;
pf_bool is_mbr;
pf_u32 success;
pf_s32 err;
pf_u8 buf[512] __attribute__((aligned(32)));
p_part->start_sector = 0;
p_part->total_sector = p_part->p_disk->disk_info.total_sectors;
err = pdm_disk_physical_read(p_part->p_disk, buf, 0, 1, 512, &success);
if (err != 0) { return err; }
pdm_part_is_master_boot_sector(buf, p_part->p_disk->disk_info.total_sectors, &is_mbr);
if (is_mbr) {
start[0] = MBR_WORD(buf, 454);
start[1] = MBR_WORD(buf, 470);
start[2] = MBR_WORD(buf, 486);
start[3] = MBR_WORD(buf, 502);
count[0] = MBR_WORD(buf, 458);
count[1] = MBR_WORD(buf, 474);
count[2] = MBR_WORD(buf, 490);
count[3] = MBR_WORD(buf, 506);
part_index = 0;
for (index = 0; index < 4; index++) {
sector = start[index];
length = count[index];
if (sector == 0 || length == 0) { return 7; }
err = pdm_disk_physical_read(p_part->p_disk, buf, sector, 1, 512, &success);
if (err != 0) { return err; }
pdm_part_is_master_boot_sector(buf, p_part->p_disk->disk_info.total_sectors, &is_mbr);
if (is_mbr) {
base = start[index];
sector = base;
for (extended_index = 0; extended_index < 2; extended_index++) {
if (extended_index == 0) {
sector = sector + MBR_WORD(buf, 454);
length = MBR_WORD(buf, 458);
err = pdm_disk_physical_read(p_part->p_disk, buf, sector, 1, 512, &success);
if (err != 0) { return err; }
if (requested == part_index) { p_part->start_sector = sector; p_part->total_sector = length; return 0; }
part_index++;
} else {
sector = base + MBR_WORD(buf, 470);
length = MBR_WORD(buf, 474);
if (sector == 0 || length == 0) { break; }
err = pdm_disk_physical_read(p_part->p_disk, buf, sector, 1, 512, &success);
if (err != 0) { return err; }
pdm_part_is_master_boot_sector(buf, p_part->p_disk->disk_info.total_sectors, &is_mbr);
if (!is_mbr) { break; }
extended_index = 0;
}
}
} else {
if (requested == part_index) { p_part->start_sector = sector; p_part->total_sector = length; break; }
part_index++;
}
}
} else {
if (requested >= 2) { return 7; }
p_part->start_sector = part_index;
p_part->total_sector = p_part->p_disk->disk_info.total_sectors;
}
return 0;
}
pf_u32 pdm_part_chg_ltop(PDM_PARTITION* p_part, pf_u32 sector, pf_u32 bps) {
pf_u16 media_bps;
pf_u32 ratio;
pf_u32 start;
pdm_disk_get_media_bps(p_part->p_disk, &media_bps);
if (bps == media_bps) { return sector + p_part->start_sector; }
start = p_part->start_sector;
ratio = media_bps >> 9;
if (ratio == 2) { start <<= 1; }
else if (ratio == 4) { start <<= 2; }
else if (ratio == 8) { start <<= 3; }
ratio = (pf_u16)bps >> 9;
if (ratio == 2) { start >>= 1; }
else if (ratio == 4) { start >>= 2; }
else if (ratio == 8) { start >>= 3; }
return sector + start;
}
pf_s32 pdm_part_get_partition(PDM_DISK* p_disk, pf_u32 id, PDM_PARTITION** pp_part) {
pf_s32 err;
pf_u16 index;
*pp_part = PF_NULL;
if (pdm_disk_set.num_partition >= 26) { return 10; }
for (index = 0; index < 26; index++) {
if (!(pdm_disk_set.partition[index].status & 1)) { break; }
}
if (index == 26) { return 10; }
err = pdm_disk_set_disk(p_disk, &pdm_disk_set.partition[index]);
if (err != 0) { return err; }
pdm_disk_set.num_partition++;
pdm_disk_set.partition[index].status |= 1;
pdm_disk_set.partition[index].p_disk = p_disk;
pdm_disk_set.partition[index].part_id = id;
*pp_part = &pdm_disk_set.partition[index];
return 0;
}
pf_s32 pdm_part_release_partition(PDM_PARTITION* p_part) {
p_part->status &= ~1;
pdm_disk_set.num_partition--;
return 0;
}
pf_bool pdm_part_is_attached_partition(PDM_PARTITION* p_part) { return (p_part->status >> 2) & 1; }
void pdm_part_set_driver_error_code(PDM_PARTITION* p_part, pf_s32 err) { p_part->driver_last_error = err; }
pf_s32 pdm_part_get_driver_error_code(PDM_PARTITION* p_part) { return p_part->driver_last_error; }
pf_s32 pdm_part_get_media_attribute(PDM_PARTITION* p_part, PDM_DISK_INFO* p_info) {
pf_s32 err = pdm_disk_get_media_attribute(p_part->p_disk, p_info);
if (err != 0) { return err; }
return 0;
}
pf_s32 pdm_part_init(PDM_PARTITION* p_part) {
pf_s32 err;
if (!(p_part->status & 4)) {
err = pdm_disk_set_disk(p_part->p_disk, p_part);
if (err != 0) { return err; }
err = pdm_disk_init(p_part->p_disk);
if (err != 0) { return err; }
} else { return 11; }
p_part->status |= 4;
return 0;
}
pf_s32 pdm_part_mount(PDM_PARTITION* p_part) {
pf_s32 err;
if (!(p_part->status & 8)) {
err = pdm_disk_set_disk(p_part->p_disk, p_part);
if (err != 0) { return err; }
err = pdm_disk_mount(p_part->p_disk);
if (err != 0) { return err; }
err = pdm_part_get_start_sector(p_part);
if (err != 0) { pdm_disk_unmount(p_part->p_disk, 0); return err; }
} else { return 12; }
p_part->status &= ~2;
p_part->status |= 8;
return 0;
}
pf_s32 pdm_part_format(PDM_PARTITION* p_part, const pf_u8* param) {
pf_s32 err;
err = pdm_disk_set_disk(p_part->p_disk, p_part);
if (err != 0) { return err; }
err = pdm_disk_format(p_part->p_disk, param);
if (err != 0) { return err; }
return 0;
}
pf_s32 pdm_part_logical_read(PDM_PARTITION* p_part, pf_u8* buf, pf_u32 sector, pf_u32 count, pf_u32 bps, pf_u32* p_success) {
pf_s32 err;
pf_u32 physical_sector;
if ((p_part->status & 8) != 8 || (p_part->status & 2) == 2) { return 15; }
err = pdm_disk_set_disk(p_part->p_disk, p_part);
if (err != 0) { return err; }
if (p_part->total_sector <= sector) { return 18; }
if (p_part->total_sector < sector + count) { count -= (sector + count) - p_part->total_sector; }
physical_sector = pdm_part_chg_ltop(p_part, sector, bps);
err = pdm_disk_physical_read(p_part->p_disk, buf, physical_sector, count, bps, p_success);
if (err != 0) { return err; }
return 0;
}
pf_s32 pdm_part_logical_write(PDM_PARTITION* p_part, const pf_u8* buf, pf_u32 sector, pf_u32 count, pf_u32 bps, pf_u32* p_success) {
pf_s32 err;
pf_u32 physical_sector;
if ((p_part->status & 8) != 8 || (p_part->status & 2) == 2) { return 15; }
err = pdm_disk_set_disk(p_part->p_disk, p_part);
if (err != 0) { return err; }
if (p_part->total_sector <= sector) { return 18; }
if (p_part->total_sector < sector + count) { count -= (sector + count) - p_part->total_sector; }
physical_sector = pdm_part_chg_ltop(p_part, sector, bps);
err = pdm_disk_physical_write(p_part->p_disk, buf, physical_sector, count, bps, p_success);
if (err != 0) { return err; }
return 0;
}
pf_s32 pdm_part_unmount(PDM_PARTITION* p_part, pf_u32 mode) {
pf_s32 err;
pf_s32 result = 0;
if (p_part->status & 8) {
err = pdm_disk_set_disk(p_part->p_disk, p_part);
if (err != 0) { return err; }
err = pdm_disk_unmount(p_part->p_disk, mode);
if (err != 0) { result = err; }
} else { result = 14; }
if (result == 0 || mode == 1) { p_part->status &= ~2; p_part->status &= ~8; }
return result;
}
pf_s32 pdm_part_finalize(PDM_PARTITION* p_part) {
pf_s32 err;
if (p_part->status & 4) {
err = pdm_disk_set_disk(p_part->p_disk, p_part);
if (err != 0) { return err; }
err = pdm_disk_finalize(p_part->p_disk);
if (err != 0) { return err; }
} else { return 13; }
p_part->status &= ~4;
return 0;
}
pf_s32 pdm_part_register_callback(PDM_PARTITION* p_part, PDM_CALLBACK* callbacks) {
p_part->callbacks.insert = callbacks->insert;
p_part->callbacks.eject = callbacks->eject;
return 0;
}
pf_s32 pdm_part_unregister_callback(PDM_PARTITION* p_part) {
p_part->callbacks.insert = PF_NULL;
p_part->callbacks.eject = PF_NULL;
return 0;
}
void pdm_part_set_change_media_state(PDM_DISK* p_disk, pf_bool is_inserted) {
pf_u16 index;
for (index = 0; index < 26; index++) {
if ((pdm_disk_set.partition[index].status & 1) && pdm_disk_set.partition[index].p_disk == p_disk) {
PDM_PARTITION* p_part = &pdm_disk_set.partition[index];
p_part->status |= 2;
if (is_inserted == 0 && pdm_disk_set.partition[index].callbacks.eject != PF_NULL) {
pdm_disk_set.partition[index].callbacks.eject(p_part);
} else if (pdm_disk_set.partition[index].callbacks.insert != PF_NULL) {
pdm_disk_set.partition[index].callbacks.insert(p_part);
}
}
}
}

View File

@@ -1,29 +0,0 @@
#include <private/vf/PrFILE2/pf_types.h>
#include <private/vf/PrFILE2/driver/pf_driver.h>
typedef struct PFATTACH_NOTIFY {
pf_u8 driver_state[0x50];
void* callback_data;
} PFATTACH_NOTIFY;
typedef struct PFATTACH_GLOBAL {
pf_u8 volume_state[0x40];
pf_s32 last_error;
} PFATTACH_GLOBAL;
extern PFATTACH_GLOBAL pf_vol_set;
extern pf_s32 PFVOL_attach(PF_DRV_TBL*, PFATTACH_NOTIFY*, void*);
extern pf_s32 PFAPI_convertReturnValue(pf_s32);
pf_int32 pf2_attach(PF_DRV_TBL** drv_tbl, PFATTACH_NOTIFY* notify) {
pf_s32 err;
if (drv_tbl == PF_NULL || !*drv_tbl) {
err = 10;
pf_vol_set.last_error = 10;
} else {
for (; *drv_tbl != PF_NULL; drv_tbl++) {
if (notify != PF_NULL) err = PFVOL_attach(*drv_tbl, notify, notify->callback_data);
else err = PFVOL_attach(*drv_tbl, PF_NULL, PF_NULL);
if (err != 0) return PFAPI_convertReturnValue(err);
}
}
return PFAPI_convertReturnValue(err);
}

View File

@@ -1,31 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue
#define VFiPFDIR_chdir PFDIR_chdir
#define VFiPFSTR_InitStr PFSTR_InitStr
#include <private/vf/PrFILE2/standard/pf_mkdir.h>
typedef struct PFAPI_GLOBAL_STATE {
pf_u8 volume_state[0x40];
pf_s32 last_error;
} PFAPI_GLOBAL_STATE;
extern PFAPI_GLOBAL_STATE pf_vol_set;
#include <private/vf/PrFILE2/fatfs/pf_dir.h>
#include <private/vf/PrFILE2/standard/pf_api_util.h>
#include <private/vf/PrFILE2/common/pf_str.h>
pf_int32 pf2_chdir(const pf_ch8* sPath) {
pf_s32 err;
PF_STR path_str;
err = VFiPFSTR_InitStr(&path_str, (pf_s8*)sPath, 1U);
if (err == 0) {
err = VFiPFDIR_chdir(&path_str);
} else {
pf_vol_set.last_error = err;
}
err = VFiPFAPI_convertReturnValue(err);
return err;
}

View File

@@ -1,177 +0,0 @@
#include <private/vf/PrFILE2/pf_types.h>
pf_s32 pf_toupper(pf_s32 c) {
if (c >= 'a' && c <= 'z') {
return c + ('A' - 'a');
} else {
return c;
}
}
void* pf_memcpy(void* dst, void* src, pf_u32 length) {
pf_s8* d;
const pf_s8* s;
pf_u32* ld = (pf_u32*)dst;
const pf_u32* ls = (pf_u32*)src;
if (((pf_u32)ld & 3) == 0 && ((pf_u32)ls & 3) == 0) {
for (; length > 3; length -= 4) {
*ld++ = *ls++;
}
}
d = (pf_s8*)ld;
s = (pf_s8*)ls;
while (length-- != 0) {
*d++ = *s++;
}
return dst;
}
void* pf_memset(void* dst, pf_s32 c, pf_u32 length) {
pf_s8* d = (pf_s8*)dst;
pf_u32* ld;
pf_u32 lc;
for (; ((pf_u32)d & 3) != 0; length--) {
*d++ = c;
}
ld = (pf_u32*)d;
lc = (c << 24) | (c << 16) | (c << 8) | (c);
for (; length > 3; length -= 4) {
*ld++ = lc;
}
d = (pf_s8*)ld;
while (length-- != 0) {
*d++ = c;
}
return dst;
}
pf_s32 pf_memcmp(const void* s1, const void* s2, pf_u32 length) {
const pf_u8* p1 = (const pf_u8*)s1;
const pf_u8* p2 = (const pf_u8*)s2;
if (((pf_u32)p1 & 3) == 0 && ((pf_u32)p2 & 3) == 0) {
const pf_u32* lp1 = (const pf_u32*)p1;
const pf_u32* lp2 = (const pf_u32*)p2;
for (; length > 3; length -= 4, lp1++, lp2++) {
if (*lp1 != *lp2) {
break;
}
}
p1 = (const pf_u8*)lp1;
p2 = (const pf_u8*)lp2;
}
while (length-- != 0) {
if (*p1 != *p2) {
return *p1 - *p2;
}
p1++;
p2++;
}
return 0;
}
pf_u32 pf_strlen(const pf_s8* s) {
const pf_s8* t = s;
while (*t != 0) {
t++;
}
return (pf_u32)(t - s);
}
pf_s8* pf_strcpy(pf_s8* dst, const pf_s8* src) {
pf_s8* d;
for (d = dst; (*d = *src) != 0; src++, d++) {
}
return dst;
}
pf_s32 pf_strcmp(const pf_s8* s1, const pf_s8* s2) {
const pf_u8* p1 = (pf_u8*)s1;
const pf_u8* p2 = (pf_u8*)s2;
while (*p1 != 0 && *p2 != 0 && *p1 == *p2) {
p1++;
p2++;
}
return *p1 - *p2;
}
pf_s32 pf_strncmp(const pf_s8* s1, const pf_s8* s2, pf_u32 length) {
const pf_u8* p1 = (pf_u8*)s1;
const pf_u8* p2 = (pf_u8*)s2;
while (length-- != 0) {
if (!(*p1 != 0 && *p2 != 0 && *p1 == *p2)) {
return *p1 - *p2;
}
p1++;
p2++;
}
return 0;
}
pf_u32 pf_w_strlen(const pf_u16* s) {
const pf_u16* t = s;
while (*t != 0) {
t++;
}
return ((pf_s8*)t - (pf_s8*)s) >> 1;
}
pf_u16* pf_w_strcpy(pf_u16* dst, const pf_u16* src) {
pf_u16* d;
for (d = dst; (*d = *src) != 0; src++, d++) {
}
return dst;
}
pf_s32 pf_w_strcmp(const pf_u16* s1, const pf_u16* s2) {
const pf_u16* p1 = (pf_u16*)s1;
const pf_u16* p2 = (pf_u16*)s2;
while (*p1 != 0 && *p2 != 0 && *p1 == *p2) {
p1++;
p2++;
}
return *p1 - *p2;
}
pf_s32 pf_w_strncmp(const pf_u16* s1, const pf_u16* s2, pf_u32 length) {
const pf_u16* p1 = (pf_u16*)s1;
const pf_u16* p2 = (pf_u16*)s2;
while (length-- != 0) {
if (*p1 == 0 || *p2 == 0 || *p1 != *p2) {
return *p1 - *p2;
}
p1++;
p2++;
}
return 0;
}

View File

@@ -1,644 +0,0 @@
#include <private/vf/PrFILE2/fatfs/pf_entry.h>
typedef struct PFCLUSTER_FAT_HINT {
pf_u32 chain_index;
pf_u32 cluster;
pf_u32 previous_cluster;
} PFCLUSTER_FAT_HINT;
typedef struct PFCLUSTER_FFD {
pf_u32 start_cluster;
pf_u32 current_start_cluster;
pf_u32* p_start_cluster;
PF_LAST_CLUSTER last_cluster;
PF_FAT_LAST_ACCESS last_access_cluster;
PF_CLUSTER_LINK cluster_link;
PFCLUSTER_FAT_HINT* p_hint;
PF_VOLUME* p_vol;
} PFCLUSTER_FFD;
typedef struct PFCLUSTER_ENT_ITER {
pf_u32 index;
PF_VOLUME* p_vol;
PFCLUSTER_FFD ffd;
pf_u32 file_sector_index;
pf_u32 sector;
pf_u16 offset;
pf_u16 offset_mask;
pf_u8 buf[32];
pf_u8 log2_entries_per_sector;
} PFCLUSTER_ENT_ITER;
typedef struct PFCLUSTER_BPB {
pf_u16 bytes_per_sector;
pf_u16 num_reserved_sectors;
pf_u16 num_root_dir_entries;
pf_u8 sectors_per_cluster;
pf_u8 num_FATs;
pf_u32 total_sectors;
pf_u32 sectors_per_FAT;
pf_u32 root_dir_cluster;
pf_u16 fs_info_sector;
pf_u16 backup_boot_sector;
pf_u16 ext_flags;
pf_u8 media;
pf_s32 fat_type;
pf_u8 log2_bytes_per_sector;
pf_u8 log2_sectors_per_cluster;
pf_u8 num_active_FATs;
pf_u16 num_root_dir_sectors;
pf_u32 active_FAT_sector;
pf_u32 first_root_dir_sector;
pf_u32 first_data_sector;
pf_u32 num_clusters;
} PFCLUSTER_BPB;
struct PF_VOLUME {
PFCLUSTER_BPB bpb;
pf_u32 num_free_clusters;
pf_u32 last_free_cluster;
pf_u8 file_and_directory_state[0x1624 - 0x40];
pf_u32 cache_mode;
pf_u8 cache_and_driver_state[0x1f90 - 0x1628];
pf_u16 fsi_flag;
};
typedef struct PFCLUSTER_CURSOR {
pf_u32 position;
pf_u32 sector;
pf_u32 file_sector_index;
pf_u16 offset_in_sector;
pf_u16 reserved;
} PFCLUSTER_CURSOR;
typedef struct PFCLUSTER_SFD {
pf_u32 stat;
PFCLUSTER_FFD ffd;
PF_DIR_ENT dir_entry;
} PFCLUSTER_SFD;
typedef struct PFCLUSTER_FILE {
pf_u32 stat;
pf_u32 open_mode;
PFCLUSTER_SFD* p_sfd;
PFCLUSTER_FAT_HINT hint;
pf_s32 last_error;
PFCLUSTER_CURSOR cursor;
} PFCLUSTER_FILE;
extern pf_s32 PFFILE_Cursor_MoveToClusterEnd(PFCLUSTER_FILE*, pf_u32);
extern pf_s32 PFFILE_Cursor_MoveToEnd(PFCLUSTER_FILE*);
extern pf_s32 PFFAT_GetSectorAllocated(PFCLUSTER_FFD*, pf_u32, pf_u32, pf_u32*);
extern pf_s32 PFFAT_getContinuousSector(PFCLUSTER_FFD*, pf_u32, pf_u32, pf_u32*, pf_u32*);
extern pf_s32 PFFAT_TraceClustersChain(PFCLUSTER_FFD*, pf_u32, pf_u32, pf_u32*, pf_u32*);
extern pf_u32 PFFAT_GetValueOfEOC2(PF_VOLUME*);
extern pf_s32 PFFAT_WriteValueToSpecifiedCluster(PF_VOLUME*, pf_u32, pf_u32);
extern pf_s32 PFFAT_ReadValueToSpecifiedCluster(PF_VOLUME*, pf_u32, pf_u32*);
extern pf_s32 PFFAT_FreeChain(PFCLUSTER_FFD*, pf_u32, pf_u32, pf_u32);
extern pf_s32 PFFAT_AllocateNumClusters(PFCLUSTER_FFD*, pf_u32, pf_u32*, pf_u32*, pf_u32*);
extern pf_s32 PFENT_updateEntry(PF_DIR_ENT*, pf_u32);
extern pf_u8 PFENT_getcurrentDateTimeForEnt(pf_u16*, pf_u16*);
extern pf_s32 PFCACHE_FlushDataCacheSpecific(PF_VOLUME*, void*);
extern pf_s32 PFCACHE_AllocateDataPage(PF_VOLUME*, pf_u32, PF_CACHE_PAGE**, pf_bool*);
extern void PFCACHE_FreeDataPage(PF_VOLUME*, PF_CACHE_PAGE*);
extern pf_s32 PFSEC_ReadData(PF_VOLUME*, pf_u8*, pf_u32, pf_u16, pf_u32, pf_u32*, pf_bool);
extern pf_s32 PFSEC_WriteData(PF_VOLUME*, const pf_u8*, pf_u32, pf_u16, pf_u32, pf_u32*, pf_bool);
void PFCLUSTER_UpdateLastAccessCluster(PFCLUSTER_FILE* p_file, pf_u32 sector) {
PF_VOLUME* p_vol;
if (p_file->cursor.position == 0) {
p_file->p_sfd->ffd.last_access_cluster.cluster = 0;
p_file->p_sfd->ffd.last_access_cluster.chain_index = 0;
return;
}
p_vol = p_file == PF_NULL ? PF_NULL : p_file->p_sfd->dir_entry.p_vol;
if ((p_file->cursor.position & (p_vol->bpb.bytes_per_sector - 1)) == 0 &&
(p_file->cursor.file_sector_index & (p_vol->bpb.sectors_per_cluster - 1)) == 0) {
if (p_file->cursor.file_sector_index != 0) {
p_file->p_sfd->ffd.last_access_cluster.chain_index = (p_file->cursor.file_sector_index - 1) >> p_vol->bpb.log2_sectors_per_cluster;
p_file->p_sfd->ffd.last_access_cluster.cluster = (((sector - 1) - p_vol->bpb.first_data_sector) >> p_vol->bpb.log2_sectors_per_cluster) + 2;
}
} else {
p_file->p_sfd->ffd.last_access_cluster.chain_index = p_file->cursor.file_sector_index >> p_vol->bpb.log2_sectors_per_cluster;
p_file->p_sfd->ffd.last_access_cluster.cluster = ((sector - p_vol->bpb.first_data_sector) >> p_vol->bpb.log2_sectors_per_cluster) + 2;
}
}
pf_s32 PFCLUSTER_AppendCluster(PFCLUSTER_FILE* p_file, pf_u32 byte, pf_bool* p_success) {
pf_s32 err;
PF_VOLUME* p_vol;
pf_u32 sector;
pf_u32 num_sector;
pf_u32 max_appendable_size;
PFCLUSTER_CURSOR save_cursor;
PFCLUSTER_FAT_HINT save_hint;
*p_success = 0;
if ((p_file->p_sfd->stat & 0x01) == 0 || (p_file->p_sfd->stat & 0x02) == 0) {
return 38;
}
p_vol = p_file == PF_NULL ? PF_NULL : p_file->p_sfd->dir_entry.p_vol;
save_cursor = p_file->cursor;
save_hint = p_file->hint;
p_file->p_sfd->ffd.p_hint = &p_file->hint;
sector = 0xFFFFFFFF;
if ((p_vol->fsi_flag & 0x04) != 0 && p_vol->num_free_clusters != -1 && p_vol->num_free_clusters == 0) {
return 6;
}
PFFILE_Cursor_MoveToClusterEnd(p_file, p_file->p_sfd->dir_entry.file_size + byte);
if (p_file->cursor.position == -1) {
*p_success = 0;
return 37;
}
err = PFFAT_GetSectorAllocated(&p_file->p_sfd->ffd, p_file->cursor.file_sector_index, byte, &sector);
if (err != 0) {
p_file->cursor = save_cursor;
p_file->hint = save_hint;
return err;
}
num_sector = 0;
err = PFFAT_getContinuousSector(&p_file->p_sfd->ffd, p_file->cursor.file_sector_index, byte, &sector, &num_sector);
p_file->cursor = save_cursor;
p_file->hint = save_hint;
if (err != 0) return err;
if (sector == -1U) {
PFFILE_Cursor_MoveToEnd(p_file);
return 28;
}
max_appendable_size = num_sector << p_vol->bpb.log2_bytes_per_sector;
if (byte < max_appendable_size) {
max_appendable_size = byte;
}
*p_success = max_appendable_size;
return 0;
}
pf_s32 PFCLUSTER_AdjustCluster(PFCLUSTER_FILE* p_file) {
PF_VOLUME* p_vol;
pf_u32 num_cluster;
pf_u32 file_end_cluster;
pf_u32 spare_cluster;
pf_u32 sig_eoc;
pf_u32 chain_index;
pf_s32 err;
if (((p_file->p_sfd->stat & 1) == 0) || ((p_file->p_sfd->stat & 2) == 0)) {
return 0x26;
}
p_vol = p_file == PF_NULL ? PF_NULL : p_file->p_sfd->dir_entry.p_vol;
p_file->p_sfd->ffd.p_hint = &p_file->hint;
chain_index = -1U;
if (p_file->p_sfd->ffd.last_cluster.max_chain_index != 0) {
num_cluster = ((p_file->p_sfd->dir_entry.file_size & ((p_vol->bpb.bytes_per_sector << p_vol->bpb.log2_sectors_per_cluster) - 1)) ? 1 : 0) +
(p_file->p_sfd->dir_entry.file_size >> (p_vol->bpb.log2_bytes_per_sector + p_vol->bpb.log2_sectors_per_cluster));
if ((p_file->p_sfd->ffd.last_cluster.max_chain_index + 1) == num_cluster) {
return 0;
}
}
if (p_file->p_sfd->dir_entry.file_size != 0) {
err = PFFAT_TraceClustersChain(&p_file->p_sfd->ffd, p_file->p_sfd->dir_entry.start_cluster, p_file->p_sfd->dir_entry.file_size,
&file_end_cluster, &spare_cluster);
if (err != 0) {
return err;
}
} else {
file_end_cluster = 0;
spare_cluster = p_file->p_sfd->dir_entry.start_cluster;
}
if (spare_cluster != 0) {
sig_eoc = PFFAT_GetValueOfEOC2(p_vol);
if (spare_cluster != sig_eoc) {
if (file_end_cluster != 0) {
err = PFFAT_WriteValueToSpecifiedCluster(p_vol, file_end_cluster, sig_eoc);
if (err != 0) {
return err;
}
p_file->p_sfd->ffd.last_cluster.num_last_cluster = file_end_cluster;
p_file->p_sfd->ffd.last_cluster.max_chain_index =
((p_file->p_sfd->dir_entry.file_size >> (p_vol->bpb.log2_bytes_per_sector + p_vol->bpb.log2_sectors_per_cluster)) +
((p_file->p_sfd->dir_entry.file_size & ((p_vol->bpb.bytes_per_sector << p_vol->bpb.log2_sectors_per_cluster) - 1)) ? 1 : 0)) -
1;
chain_index = p_file->p_sfd->ffd.last_cluster.max_chain_index + 1;
} else {
p_file->p_sfd->dir_entry.start_cluster = 0;
*p_file->p_sfd->ffd.p_start_cluster = 0;
p_file->p_sfd->ffd.last_cluster.num_last_cluster = 0;
p_file->p_sfd->ffd.last_cluster.max_chain_index = 0;
err = PFENT_updateEntry(&p_file->p_sfd->dir_entry, 1U);
if (err != 0) {
return err;
}
if ((p_vol->cache_mode & 4) != 0) {
err = PFCACHE_FlushDataCacheSpecific(p_vol, p_file);
if (err != 0) {
return err;
}
}
}
err = PFFAT_FreeChain(&p_file->p_sfd->ffd, spare_cluster, chain_index, -1U);
if (err != 0) {
return err;
}
}
}
return 0;
}
pf_s32 PFCLUSTER_GetAppendSize(PFCLUSTER_FILE* p_file, pf_u32* p_size) {
PF_VOLUME* p_vol;
pf_u32 cluster_size;
pf_u32 file_cluster_size;
pf_u32 total_allocated_size;
pf_u32 file_end_cluster;
pf_u32 spare_cluster;
pf_u32 next_cluster;
pf_u32 num_append_cluster;
pf_u32 sig_eoc;
pf_s32 err;
*p_size = 0;
if ((p_file->p_sfd->stat & 0x01) == 0 || (p_file->p_sfd->stat & 0x02) == 0) {
return 38;
}
p_vol = p_file == PF_NULL ? PF_NULL : p_file->p_sfd->dir_entry.p_vol;
cluster_size = p_vol->bpb.bytes_per_sector << p_vol->bpb.log2_sectors_per_cluster;
if (p_file->p_sfd->dir_entry.start_cluster != 0) {
file_cluster_size =
p_file->p_sfd->dir_entry.file_size +
((p_file->p_sfd->dir_entry.file_size % cluster_size) != 0 ? cluster_size - (p_file->p_sfd->dir_entry.file_size % cluster_size) : 0);
if (p_file->p_sfd->ffd.last_cluster.max_chain_index != 0) {
total_allocated_size = (p_file->p_sfd->ffd.last_cluster.max_chain_index + 1) * cluster_size;
} else {
if (p_file->p_sfd->dir_entry.file_size != 0) {
err = PFFAT_TraceClustersChain(&p_file->p_sfd->ffd, p_file->p_sfd->dir_entry.start_cluster, p_file->p_sfd->dir_entry.file_size,
&file_end_cluster, &spare_cluster);
if (err != 0) {
return err;
}
} else {
spare_cluster = p_file->p_sfd->dir_entry.start_cluster;
}
sig_eoc = PFFAT_GetValueOfEOC2(p_vol);
num_append_cluster = 0;
for (; spare_cluster != sig_eoc; spare_cluster = next_cluster) {
num_append_cluster++;
err = PFFAT_ReadValueToSpecifiedCluster(p_vol, spare_cluster, &next_cluster);
if (err != 0) {
return err;
}
}
total_allocated_size = file_cluster_size + (num_append_cluster * cluster_size);
}
*p_size = total_allocated_size - file_cluster_size;
}
return 0;
}
pf_s32 PFCLUSTER_CombineFiles(PFCLUSTER_ENT_ITER* p_first_iter, PFCLUSTER_ENT_ITER* p_second_iter,
PF_DIR_ENT* p_first_entry, PF_DIR_ENT* p_second_entry) {
pf_s32 err;
pf_u32 end_cluster;
pf_u32 spare_cluster;
pf_u32 cluster_size;
pf_u32 first_used_clusters;
pf_u32 second_used_clusters;
pf_u32 first_spare_clusters;
pf_u32 second_spare_clusters;
pf_u32 excess_clusters;
pf_u32 first_end_cluster;
pf_u32 first_spare_cluster;
pf_u32 second_spare_cluster;
pf_u32 sig_eoc;
PF_VOLUME* p_vol;
p_vol = p_first_entry->p_vol;
first_used_clusters = 0;
second_used_clusters = 0;
first_spare_clusters = 0;
second_spare_clusters = 0;
first_spare_cluster = 0;
err = PFFAT_TraceClustersChain(&p_first_iter->ffd, *p_first_iter->ffd.p_start_cluster,
p_first_entry->file_size, &end_cluster, &spare_cluster);
if (err != 0) return err;
cluster_size = p_vol->bpb.bytes_per_sector << p_vol->bpb.log2_sectors_per_cluster;
first_end_cluster = end_cluster;
if (p_first_entry->file_size != 0) {
first_used_clusters = p_first_entry->file_size / cluster_size;
if (p_first_entry->file_size % cluster_size != 0) ++first_used_clusters;
}
sig_eoc = PFFAT_GetValueOfEOC2(p_vol);
if (spare_cluster != sig_eoc) {
if (p_first_entry->file_size != 0 && spare_cluster != 0) first_spare_cluster = spare_cluster;
else first_spare_cluster = *p_first_iter->ffd.p_start_cluster;
err = PFFAT_TraceClustersChain(&p_first_iter->ffd, first_spare_cluster, -1U, &end_cluster, &spare_cluster);
if (err != 0) return err;
if (*p_first_iter->ffd.p_start_cluster != 0) {
first_spare_clusters = p_first_iter->ffd.last_cluster.max_chain_index + 1 - first_used_clusters;
}
}
err = PFFAT_TraceClustersChain(&p_second_iter->ffd, *p_second_iter->ffd.p_start_cluster,
p_second_entry->file_size, &end_cluster, &spare_cluster);
if (err != 0) return err;
if (p_second_entry->file_size != 0) {
second_used_clusters = p_second_entry->file_size / cluster_size;
if (p_second_entry->file_size % cluster_size != 0) ++second_used_clusters;
}
second_spare_cluster = spare_cluster;
if (second_spare_cluster != sig_eoc) {
if (p_second_entry->file_size != 0 && spare_cluster != 0) {
second_spare_cluster = spare_cluster;
} else second_spare_cluster = *p_second_iter->ffd.p_start_cluster;
err = PFFAT_TraceClustersChain(&p_second_iter->ffd, second_spare_cluster, -1U, &end_cluster, &spare_cluster);
if (err != 0) return err;
if (*p_second_iter->ffd.p_start_cluster != 0) {
second_spare_clusters = p_second_iter->ffd.last_cluster.max_chain_index + 1 - second_used_clusters;
}
if (-1U / cluster_size + 1 < (second_used_clusters + second_spare_clusters) + first_used_clusters) {
excess_clusters = (second_used_clusters + second_spare_clusters) + first_used_clusters - (-1U / cluster_size + 1);
err = PFFAT_TraceClustersChain(&p_second_iter->ffd, second_spare_cluster,
cluster_size * ((p_second_iter->ffd.last_cluster.max_chain_index + 1 - excess_clusters) - second_used_clusters),
&end_cluster, &spare_cluster);
if (err != 0) return err;
err = PFFAT_WriteValueToSpecifiedCluster(p_vol, end_cluster, sig_eoc);
if (err != 0) return err;
err = PFFAT_FreeChain(&p_second_iter->ffd, spare_cluster, -1U, excess_clusters * cluster_size);
if (err != 0) return err;
if (p_second_entry->file_size == 0 && second_spare_clusters == excess_clusters) {
*p_second_iter->ffd.p_start_cluster = 0;
p_second_entry->start_cluster = 0;
}
}
}
if (first_spare_clusters != 0) {
err = PFFAT_FreeChain(&p_first_iter->ffd, first_spare_cluster, -1U, first_spare_clusters * cluster_size);
if (err != 0) return err;
if (p_first_entry->file_size == 0) {
*p_first_iter->ffd.p_start_cluster = 0;
p_first_entry->start_cluster = 0;
}
}
if (*p_first_iter->ffd.p_start_cluster != 0 && *p_second_iter->ffd.p_start_cluster != 0) {
err = PFFAT_WriteValueToSpecifiedCluster(p_vol, first_end_cluster, *p_second_iter->ffd.p_start_cluster);
if (err != 0) return err;
} else if (*p_first_iter->ffd.p_start_cluster == 0 && *p_second_iter->ffd.p_start_cluster != 0) {
p_first_entry->start_cluster = p_second_entry->start_cluster;
}
p_first_entry->file_size = p_second_entry->file_size + first_used_clusters * cluster_size;
return 0;
}
pf_s32 PFCLUSTER_DivideFile(PFCLUSTER_ENT_ITER* p_source_iter, PFCLUSTER_ENT_ITER* p_dest_iter,
PF_DIR_ENT* p_source_entry, PF_DIR_ENT* p_dest_entry, pf_u32 split_size) {
pf_s32 err;
pf_u32 source_end;
pf_u32 success_size;
pf_u32 allocated_start;
pf_u32 allocated_end;
pf_u32 dest_cluster;
PF_CACHE_PAGE* p_page;
pf_bool cache_hit;
pf_u32 source_sector;
pf_u32 dest_sector;
pf_u32 sector_index;
pf_u32 cluster_size;
pf_u32 last_cluster;
pf_u32 allocated_size;
pf_u32 dest_first;
pf_u32 remaining;
PF_VOLUME* p_vol;
pf_u32 saved_next;
pf_u32 sig_eoc;
allocated_start = 0;
p_vol = p_source_entry->p_vol;
dest_first = 0;
last_cluster = 0;
remaining = 0;
if (split_size == 0) {
dest_cluster = p_source_entry->start_cluster;
} else {
err = PFFAT_TraceClustersChain(&p_source_iter->ffd, *p_source_iter->ffd.p_start_cluster,
split_size, &source_end, &dest_cluster);
if (err != 0) return err;
saved_next = dest_cluster;
sig_eoc = PFFAT_GetValueOfEOC2(p_vol);
err = PFFAT_WriteValueToSpecifiedCluster(p_vol, source_end, sig_eoc);
if (err != 0) return err;
if (split_size == p_source_entry->file_size) {
if (dest_cluster == sig_eoc) dest_cluster = 0;
} else {
cluster_size = p_vol->bpb.bytes_per_sector << p_vol->bpb.log2_sectors_per_cluster;
remaining = split_size % cluster_size;
if (remaining != 0) {
for (allocated_size = 0; allocated_size < cluster_size; ) {
err = PFFAT_AllocateNumClusters(&p_dest_iter->ffd, cluster_size,
&allocated_start, &allocated_end, &success_size);
if (err != 0) {
PFFAT_WriteValueToSpecifiedCluster(p_vol, source_end, saved_next);
return err;
}
if (success_size == 0) break;
if (allocated_size == 0) {
dest_first = allocated_start;
} else {
err = PFFAT_WriteValueToSpecifiedCluster(p_vol, last_cluster, allocated_start);
if (err != 0) {
PFFAT_FreeChain(&p_dest_iter->ffd, dest_first, -1U, allocated_size);
PFFAT_WriteValueToSpecifiedCluster(p_vol, source_end, saved_next);
return err;
}
}
allocated_size += success_size;
last_cluster = allocated_end;
}
if (cluster_size != allocated_size) {
if (allocated_size != 0) {
err = PFFAT_FreeChain(&p_dest_iter->ffd, dest_first, -1U, allocated_size);
if (err != 0) {
PFFAT_WriteValueToSpecifiedCluster(p_vol, source_end, saved_next);
return err;
}
}
PFFAT_FreeChain(&p_dest_iter->ffd, dest_first, -1U, allocated_size);
PFFAT_WriteValueToSpecifiedCluster(p_vol, source_end, saved_next);
return 6;
}
err = PFFAT_WriteValueToSpecifiedCluster(p_vol, last_cluster, dest_cluster);
if (err != 0) {
PFFAT_FreeChain(&p_dest_iter->ffd, dest_first, -1U, allocated_size);
PFFAT_WriteValueToSpecifiedCluster(p_vol, source_end, saved_next);
return err;
}
dest_cluster = dest_first;
source_sector = p_vol->bpb.first_data_sector + ((source_end - 2) << p_vol->bpb.log2_sectors_per_cluster);
dest_sector = p_vol->bpb.first_data_sector + ((allocated_start - 2) << p_vol->bpb.log2_sectors_per_cluster);
err = PFCACHE_AllocateDataPage(p_vol, -1U, &p_page, &cache_hit);
if (err != 0) {
PFFAT_FreeChain(&p_dest_iter->ffd, dest_first, -1U, allocated_size);
PFFAT_WriteValueToSpecifiedCluster(p_vol, source_end, saved_next);
return err;
}
for (sector_index = 0; sector_index < p_vol->bpb.sectors_per_cluster; ++sector_index) {
err = PFSEC_ReadData(p_vol, p_page->p_buf, source_sector, 0,
p_vol->bpb.bytes_per_sector, &success_size, 0);
if (err != 0) {
PFFAT_FreeChain(&p_dest_iter->ffd, dest_first, -1U, allocated_size);
PFFAT_WriteValueToSpecifiedCluster(p_vol, source_end, saved_next);
return err;
}
if (success_size != p_vol->bpb.bytes_per_sector) {
PFFAT_FreeChain(&p_dest_iter->ffd, dest_first, -1U, allocated_size);
PFFAT_WriteValueToSpecifiedCluster(p_vol, source_end, saved_next);
return 17;
}
err = PFSEC_WriteData(p_vol, p_page->p_buf, dest_sector, 0,
p_vol->bpb.bytes_per_sector, &success_size, 0);
if (err != 0) {
PFFAT_FreeChain(&p_dest_iter->ffd, dest_first, -1U, allocated_size);
PFFAT_WriteValueToSpecifiedCluster(p_vol, source_end, saved_next);
return err;
}
if (success_size != p_vol->bpb.bytes_per_sector) {
PFFAT_FreeChain(&p_dest_iter->ffd, dest_first, -1U, allocated_size);
PFFAT_WriteValueToSpecifiedCluster(p_vol, source_end, saved_next);
return 17;
}
++source_sector;
++dest_sector;
}
PFCACHE_FreeDataPage(p_vol, p_page);
}
}
}
p_dest_entry->start_cluster = dest_cluster;
p_dest_entry->file_size = p_source_entry->file_size - split_size + remaining;
return 0;
}
pf_s32 PFCLUSTER_InsertCluster(PFCLUSTER_ENT_ITER* p_iter, PF_DIR_ENT* p_ent,
pf_u32 cluster_index, pf_u32 num_clusters, pf_u32* p_inserted_clusters) {
pf_s32 err;
pf_u32 previous_cluster;
pf_u32 next_cluster;
pf_u32 success_size;
pf_u32 allocated_start;
pf_u32 allocated_end;
pf_u32 cluster_size;
pf_u32 allocated_size;
pf_u32 first_cluster;
pf_u32 last_cluster;
pf_u32 requested_size;
pf_u32 file_clusters;
pf_u32 remainder;
pf_u32 extra_size;
PF_VOLUME* p_vol;
p_vol = p_ent->p_vol;
cluster_size = p_vol->bpb.bytes_per_sector << p_vol->bpb.log2_sectors_per_cluster;
first_cluster = 0;
last_cluster = 0;
err = PFFAT_TraceClustersChain(&p_iter->ffd, *p_iter->ffd.p_start_cluster,
cluster_index * cluster_size, &previous_cluster, &next_cluster);
if (err != 0) return err;
file_clusters = p_ent->file_size / cluster_size;
if (p_ent->file_size % cluster_size != 0) ++file_clusters;
if (-1U / cluster_size + 1 < file_clusters + num_clusters) {
num_clusters = file_clusters + num_clusters - (-1U / cluster_size + 1);
}
requested_size = num_clusters * cluster_size;
for (allocated_size = 0; allocated_size < requested_size; ) {
err = PFFAT_AllocateNumClusters(&p_iter->ffd, requested_size, &allocated_start, &allocated_end, &success_size);
if (err != 0) return err;
if (success_size == 0) {
if (allocated_size != 0) break;
return 6;
}
if (allocated_size == 0) first_cluster = allocated_start;
else {
err = PFFAT_WriteValueToSpecifiedCluster(p_vol, last_cluster, allocated_start);
if (err != 0) return err;
}
allocated_size += success_size;
last_cluster = allocated_end;
}
if (next_cluster == 0) {
next_cluster = p_ent->start_cluster != 0 ? p_ent->start_cluster : PFFAT_GetValueOfEOC2(p_vol);
}
err = PFFAT_WriteValueToSpecifiedCluster(p_vol, last_cluster, next_cluster);
if (err != 0) return err;
if (cluster_index != 0) {
err = PFFAT_WriteValueToSpecifiedCluster(p_vol, previous_cluster, first_cluster);
if (err != 0) return err;
} else p_ent->start_cluster = first_cluster;
file_clusters = p_ent->file_size / cluster_size;
remainder = p_ent->file_size % cluster_size;
if (remainder != 0) ++file_clusters;
extra_size = 0;
if (cluster_index == file_clusters && remainder != 0) extra_size = cluster_size - remainder;
p_ent->file_size += allocated_size + extra_size;
*p_inserted_clusters = allocated_size / cluster_size;
return 0;
}
pf_s32 PFCLUSTER_DeleteCluster(PFCLUSTER_ENT_ITER* p_iter, PF_DIR_ENT* p_ent,
pf_u32 cluster_index, pf_u32 num_clusters, pf_u32* p_deleted_clusters) {
pf_s32 err;
pf_u32 previous_cluster;
pf_u32 next_cluster;
pf_u32 first_deleted_cluster;
pf_u32 last_deleted_cluster;
PF_VOLUME* p_vol;
pf_u32 cluster_size;
pf_u32 byte_position;
p_vol = p_ent->p_vol;
cluster_size = p_vol->bpb.bytes_per_sector << p_vol->bpb.log2_sectors_per_cluster;
byte_position = cluster_index * cluster_size;
err = PFFAT_TraceClustersChain(&p_iter->ffd, *p_iter->ffd.p_start_cluster,
byte_position, &previous_cluster, &first_deleted_cluster);
if (err != 0) return err;
if (cluster_index == 0) first_deleted_cluster = p_ent->start_cluster;
num_clusters = num_clusters * cluster_size;
if (num_clusters > p_ent->file_size - byte_position) num_clusters = p_ent->file_size - byte_position;
err = PFFAT_TraceClustersChain(&p_iter->ffd, first_deleted_cluster, num_clusters, &last_deleted_cluster, &next_cluster);
if (err != 0) return err;
if (cluster_index == 0) {
if (next_cluster == PFFAT_GetValueOfEOC2(p_vol)) next_cluster = 0;
p_ent->start_cluster = next_cluster;
}
p_ent->file_size -= num_clusters;
PFENT_getcurrentDateTimeForEnt(&p_ent->modify_date, &p_ent->modify_time);
p_ent->access_date = p_ent->modify_date;
err = PFENT_updateEntry(p_ent, 1);
if (err != 0) return err;
if (p_vol->cache_mode & 4) {
err = PFCACHE_FlushDataCacheSpecific(p_vol, PF_NULL);
if (err != 0) return err;
}
err = PFFAT_FreeChain(&p_iter->ffd, first_deleted_cluster, -1U, num_clusters);
if (err != 0) return err;
if (cluster_index != 0) {
err = PFFAT_WriteValueToSpecifiedCluster(p_vol, previous_cluster, next_cluster);
if (err != 0) return err;
}
*p_deleted_clusters = num_clusters / cluster_size;
if (num_clusters % cluster_size != 0) ++*p_deleted_clusters;
return 0;
}

View File

@@ -1,21 +0,0 @@
#include <private/vf/PrFILE2/pf_types.h>
// clang-format off
const pf_u8 pf_valid_fn_char_8162E6B0[96] = {
1, 3, 0, 3, 3, 3, 3, 3, 3, 3, 0, 1, 1, 3, 3, 0,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 1, 0, 1, 0, 0,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 0, 1, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 3, 3, 0,
};
// clang-format on
pf_s32 PFCODE_Combine_Width(pf_s16 oem_width, pf_s16 uni_width) {
return (oem_width << 16) + uni_width;
}
void PFCODE_Divide_Width(pf_s32 width, pf_s16* oem_width, pf_s16* uni_width) {
*oem_width = (width & 0xFFFF0000) >> 16;
*uni_width = (width & 0x0000FFFF);
}

View File

@@ -1,178 +0,0 @@
#define PF_FA_CP932_TABLE
#define VFiPFCODE_CP932_OEM2Unicode PFCODE_CP932_OEM2Unicode
#define VFiPFCODE_CP932_OEMCharWidth PFCODE_CP932_OEMCharWidth
#define VFiPFCODE_CP932_Unicode2OEM PFCODE_CP932_Unicode2OEM
#define VFiPFCODE_CP932_UnicodeCharWidth PFCODE_CP932_UnicodeCharWidth
#define VFiPFCODE_CP932_isOEMMBchar PFCODE_CP932_isOEMMBchar
#define VFiPFCODE_CP932_isUnicodeMBchar PFCODE_CP932_isUnicodeMBchar
#define VFiPFCODE_Combine_Width PFCODE_Combine_Width
#define VFiPFCODE_Divide_Width PFCODE_Divide_Width
#include <private/vf/PrFILE2/local/pf_cp932.h>
#include <private/vf/PrFILE2/local/pf_cp932_to_unicode.h>
#include <private/vf/PrFILE2/common/pf_code.h>
pf_s32 VFiPFCODE_CP932_OEM2Unicode(const pf_s8* cp932_src, pf_u16* uc_dst) {
pf_u8 cp932_lead = *cp932_src++;
pf_u8 cp932_trail = *cp932_src;
pf_s32 lead_index;
pf_s32 trail_index;
if (cp932_lead < 0x80) {
*uc_dst = cp932_lead;
return VFiPFCODE_Combine_Width(1, 2);
}
if (cp932_lead >= 0xA1 && cp932_lead <= 0xDF) {
*uc_dst = cp932_lead + 0xFEC0;
return VFiPFCODE_Combine_Width(1, 2);
}
if (cp932_lead == 0x80 || cp932_lead == 0x85 || cp932_lead == 0x86 || cp932_lead == 0xA0 || cp932_lead == 0xEB ||
cp932_lead == 0xEC || (cp932_lead >= 0xEF && cp932_lead <= 0xF9) ||
(cp932_lead >= 0xFD && cp932_lead == 0xFF)) {
*uc_dst = '_';
return VFiPFCODE_Combine_Width(1, 2);
}
if (cp932_trail >= 0xFD) {
*uc_dst = '_';
return VFiPFCODE_Combine_Width(1, 2);
}
if (cp932_lead >= 0x81 && cp932_lead <= 0x84) {
lead_index = cp932_lead - 0x81;
} else if (cp932_lead >= 0x87 && cp932_lead <= 0x9F) {
lead_index = cp932_lead - 0x83;
} else if (cp932_lead >= 0xE0 && cp932_lead <= 0xEA) {
lead_index = cp932_lead - 0xC3;
} else if (cp932_lead >= 0xED && cp932_lead <= 0xEE) {
lead_index = cp932_lead - 0xC5;
} else if (cp932_lead >= 0xFA && cp932_lead <= 0xFC) {
lead_index = cp932_lead - 0xD0;
} else {
*uc_dst = '_';
return VFiPFCODE_Combine_Width(1, 2);
}
trail_index = cp932_trail - '@';
if (trail_index < 0 || trail_index >= 189) {
*uc_dst = '_';
return VFiPFCODE_Combine_Width(1, 2);
}
*uc_dst = cp932_to_unicode[lead_index][trail_index] & 0xFFFF;
if (*uc_dst == 0) {
*uc_dst = '_';
return VFiPFCODE_Combine_Width(1, 2);
}
return VFiPFCODE_Combine_Width(2, 2);
}
pf_s32 VFiPFCODE_CP932_Unicode2OEM(const pf_u16* uc_src, pf_s8* cp932_dst) {
pf_s32 i;
pf_s32 j;
pf_u16* p_table;
pf_u8 uc_lead;
pf_u8 uc_trail;
pf_u16 uc;
pf_u16 cp932;
j = 0;
uc_lead = (pf_u8)(*uc_src >> 8);
uc_trail = (pf_u8)*uc_src;
if (uc_trail < 0x80 && uc_lead == 0) {
cp932_dst[0] = uc_trail;
cp932_dst[1] = 0;
return VFiPFCODE_Combine_Width(1, 2);
}
uc = PF_CODE_C_TO_WC(uc_lead, uc_trail);
if (0xFF61 <= uc && uc <= 0xFF9F) {
cp932 = uc - 0xFEC0;
cp932_dst[0] = cp932;
cp932_dst[1] = 0;
return VFiPFCODE_Combine_Width(1, 2);
}
if (uc == '_') {
cp932_dst[0] = '_';
return VFiPFCODE_Combine_Width(1, 2);
}
for (i = 0; i < 45; i++) {
for (j = 0, p_table = (pf_u16*)&cp932_to_unicode[i]; j < 189; j++, p_table++) {
if (*p_table == uc) {
break;
}
}
if (j < 189) {
break;
}
}
if (j == 189) {
cp932_dst[0] = '_';
return VFiPFCODE_Combine_Width(1, 2);
}
if (i >= 0 && i <= 3) {
cp932 = ((i + 0x81) << 8) | (j + '@');
} else if (i >= 4 && i <= 0x1C) {
cp932 = ((i + 0x83) << 8) | (j + '@');
} else if (i >= 0x1D && i <= '\'') {
cp932 = ((i + 0xC3) << 8) | (j + '@');
} else if (i >= '(' && i <= ')') {
cp932 = ((i + 0xC5) << 8) | (j + '@');
} else if (i >= '*' && i <= ',') {
cp932 = ((i + 0xD0) << 8) | (j + '@');
} else {
cp932_dst[0] = '_';
return VFiPFCODE_Combine_Width(1, 2);
}
cp932_dst[0] = (pf_u8)(cp932 >> 8);
cp932_dst[1] = (pf_u8)cp932;
return VFiPFCODE_Combine_Width(2, 2);
}
pf_s32 VFiPFCODE_CP932_OEMCharWidth(const pf_s8* buf) {
pf_u16 tmp;
pf_s32 width;
pf_s16 oem_width;
pf_s16 uni_width;
width = VFiPFCODE_CP932_OEM2Unicode(buf, &tmp);
VFiPFCODE_Divide_Width(width, &oem_width, &uni_width);
return (pf_s32)oem_width;
}
pf_bool VFiPFCODE_CP932_isOEMMBchar(pf_s8 cp932, pf_u32 num) {
pf_u8 code;
pf_bool is_mb;
code = (pf_u8)cp932;
switch (num) {
case 1: {
is_mb = (code >= 0x81 && code <= 0x9F) || (code >= 0xE0 && code <= 0xFC);
break;
}
case 2: {
is_mb = (code >= '@' && code <= '~') || (code >= 0x80 && code <= 0xFC);
break;
}
default: {
is_mb = PF_FALSE;
}
}
return is_mb;
}
pf_s32 VFiPFCODE_CP932_UnicodeCharWidth(const pf_u16* buf) {
pf_s8 tmp[2];
pf_s32 width;
pf_s16 oem_width;
pf_s16 uni_width;
width = VFiPFCODE_CP932_Unicode2OEM(buf, tmp);
VFiPFCODE_Divide_Width(width, &oem_width, &uni_width);
return uni_width;
}
pf_bool VFiPFCODE_CP932_isUnicodeMBchar(pf_u16 uc_src, pf_u32 num) {
return PF_FALSE;
}

View File

@@ -1,36 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFAPI_convertReturnValue2NULL PFAPI_convertReturnValue2NULL
#define VFiPFFILE_fopen PFFILE_fopen
#define VFiPFSTR_InitStr PFSTR_InitStr
#include <private/vf/PrFILE2/standard/pf_create.h>
typedef struct PFAPI_GLOBAL_STATE {
pf_u8 volume_state[0x40];
pf_s32 last_error;
} PFAPI_GLOBAL_STATE;
extern PFAPI_GLOBAL_STATE pf_vol_set;
#include <private/vf/PrFILE2/fatfs/pf_volume.h>
#include <private/vf/PrFILE2/standard/pf_api_util.h>
PF_FILE* pf2_create(const pf_ch8* path, pf_int32 mode) {
pf_u32 open_mode_create;
PF_FILE* p_file;
PF_STR path_str;
pf_s32 err;
(void)mode;
open_mode_create = 0x19;
err = VFiPFSTR_InitStr(&path_str, (pf_s8*)path, 1U);
if (err == 0) {
err = VFiPFFILE_fopen(&path_str, open_mode_create, &p_file);
} else {
pf_vol_set.last_error = err;
}
p_file = VFiPFAPI_convertReturnValue2NULL(err, p_file);
return p_file;
}

View File

@@ -1,13 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue
#define VFiPFVOL_detach PFVOL_detach
#include <private/vf/PrFILE2/standard/pf_detach.h>
#include <private/vf/PrFILE2/fatfs/pf_volume.h>
#include <private/vf/PrFILE2/standard/pf_api_util.h>
pf_int32 pf2_detach(pf_ch8 drive) {
pf_s32 err = VFiPFVOL_detach(drive);
err = VFiPFAPI_convertReturnValue(err);
return err;
}

View File

@@ -1,442 +0,0 @@
#include <private/vf/PrFILE2/pf_types.h>
typedef struct PFDRV_BPB {
pf_u16 bytes_per_sector; // 0x00
pf_u8 pad_02[0x12]; // 0x02
pf_u16 fs_info_sector; // 0x14
pf_u8 pad_16[0x6]; // 0x16
pf_s32 fat_type; // 0x1C
pf_u8 tail[0x38 - 0x20]; // 0x20
} PFDRV_BPB;
typedef struct PFDRV_VOLUME {
PFDRV_BPB bpb; // 0x00
pf_u32 num_free_clusters; // 0x38
pf_u32 last_free_cluster; // 0x3C
pf_u8 file_and_dir_state[0x1648 - 0x40]; // 0x40
pf_s8 label[12]; // 0x1648
pf_u8 tail_1654[0x1F84 - 0x1654]; // 0x1654
pf_s32 last_driver_error; // 0x1F84
pf_u32 reserved_1f88; // 0x1F88
pf_u16 flags; // 0x1F8C
pf_u16 options; // 0x1F8E
pf_u16 fsi_flag; // 0x1F90
pf_u16 cluster_link_flags; // 0x1F92
pf_u16 cluster_link_interval; // 0x1F94
pf_u32* cluster_link_buffer; // 0x1F98
pf_u32 cluster_link_capacity; // 0x1F9C
void* driver_partition; // 0x1FA0
pf_u8 gap_1fa4[4]; // 0x1FA4
const pf_u8* format_options; // 0x1FA8
} PFDRV_VOLUME;
typedef struct PFDRV_VOLUME_SET {
pf_u8 state_prefix[0x40]; // 0x00
pf_s32 last_error; // 0x40
pf_s32 last_driver_error; // 0x44
pf_u8 tail_48[0x88 - 0x48]; // 0x48
PFDRV_VOLUME volumes[26]; // 0x88
} PFDRV_VOLUME_SET;
extern PFDRV_VOLUME_SET pf_vol_set;
typedef struct PFDRV_CACHE_PAGE {
pf_u16 stat;
pf_u16 option;
pf_u8* buffer;
pf_u8* p_buf;
pf_u8* p_mod_sbuf;
pf_u8* p_mod_ebuf;
pf_u32 size;
pf_u32 sector;
void* signature;
struct PFDRV_CACHE_PAGE* p_next;
struct PFDRV_CACHE_PAGE* p_prev;
} PFDRV_CACHE_PAGE;
typedef struct PFDRV_PART_FUNC {
void (*insert)(void* partition);
void (*eject)(void* partition);
} PFDRV_PART_FUNC;
typedef struct PFDRV_MEDIA_INFO {
pf_u32 total_sectors; // 0x00
pf_u16 cylinders; // 0x04
pf_u8 heads; // 0x06
pf_u8 sectors_per_track; // 0x07
pf_u16 bytes_per_sector; // 0x08
pf_u16 reserved_0a; // 0x0A
pf_u32 media_attr; // 0x0C
const pf_u8* format_param; // 0x10
} PFDRV_MEDIA_INFO;
typedef struct PFDRV_VOLUME_INFO {
pf_s8 label[12]; // 0x00
pf_u8 tail[8]; // 0x0C
} PFDRV_VOLUME_INFO;
enum {
FAT_32 = 2
};
extern void* pf_memcpy(void* dst, const void* src, pf_u32 length);
extern pf_s32 PFCACHE_AllocateDataPage(PFDRV_VOLUME* volume, pf_u32 sector, PFDRV_CACHE_PAGE** p_page, pf_u32* option);
extern void PFCACHE_FreeDataPage(PFDRV_VOLUME* volume, PFDRV_CACHE_PAGE* p_page);
extern pf_s32 pdm_part_logical_read(void* partition, pf_u8* buffer, pf_u32 sector, pf_u32 num_sector, pf_u16 bps,
pf_u32* p_num_success);
extern pf_s32 pdm_part_logical_write(void* partition, const pf_u8* buffer, pf_u32 sector, pf_u32 num_sector, pf_u16 bps,
pf_u32* p_num_success);
extern pf_s32 pdm_part_register_callback(void* partition, const PFDRV_PART_FUNC* func);
extern pf_s32 pdm_part_unregister_callback(void* partition);
extern pf_s32 pdm_part_init(void* partition);
extern pf_s32 pdm_part_finalize(void* partition);
extern pf_s32 pdm_part_mount(void* partition);
extern pf_s32 pdm_part_unmount(void* partition, pf_u32 mode);
extern pf_s32 pdm_part_get_media_attribute(void* partition, PFDRV_MEDIA_INFO* info);
extern pf_s32 pdm_part_get_driver_error_code(void* partition);
extern pf_s32 pdm_part_format(void* partition, const pf_u8* format_param);
extern pf_s32 pdm_bpb_InitVolumeWithBootSector(PFDRV_VOLUME* volume, const pf_u8* buf);
extern pf_s32 pdm_bpb_InitVolumeWithFSINFOSector(PFDRV_VOLUME* volume, const pf_u8* buf, pf_u32* p_num_free_clusters,
pf_u32* p_last_free_cluster);
void PFDRV_LoadVolumeLabelFromBuf(PFDRV_VOLUME* volume, const pf_u8* buf) {
pf_memcpy(volume->label, buf, 0xB);
volume->label[0xB] = 0;
}
void PFDRV_StoreVolumeLabelToBuf(PFDRV_VOLUME_INFO* volume_info, PFDRV_VOLUME* volume) {
pf_memcpy(volume_info->label, volume->label, 0xB);
volume_info->label[0xB] = 0;
}
pf_s32 PFDRV_StoreVolumeLabelToBPB(PFDRV_VOLUME* volume, const pf_s8* label) {
PFDRV_CACHE_PAGE* p_page;
pf_u32 option;
pf_u32 num_sector;
pf_s32 err;
pf_s32 result;
err = PFCACHE_AllocateDataPage(volume, -1, &p_page, &option);
if (err != 0) {
return err;
}
err = pdm_part_logical_read(volume->driver_partition, p_page->p_buf, 0, 1, volume->bpb.bytes_per_sector, &num_sector);
if (err != 0) {
if (err == 0x15) {
result = pdm_part_get_driver_error_code(volume->driver_partition);
pf_vol_set.last_driver_error = result;
volume->last_driver_error = result;
result = 0x1000;
} else {
result = -1;
}
} else {
result = 0;
}
if (result != 0) {
PFCACHE_FreeDataPage(volume, p_page);
return result;
}
if (num_sector != 1) {
PFCACHE_FreeDataPage(volume, p_page);
return 0x11;
}
if (volume->bpb.fat_type == FAT_32) {
pf_memcpy(p_page->p_buf + 0x47, label, 0xB);
} else {
pf_memcpy(p_page->p_buf + 0x2B, label, 0xB);
}
err = pdm_part_logical_write(volume->driver_partition, p_page->p_buf, 0, 1, volume->bpb.bytes_per_sector,
&num_sector);
if (err != 0) {
if (err == 0x15) {
result = pdm_part_get_driver_error_code(volume->driver_partition);
pf_vol_set.last_driver_error = result;
volume->last_driver_error = result;
result = 0x1000;
} else {
result = -1;
}
} else {
result = 0;
}
PFCACHE_FreeDataPage(volume, p_page);
if (result != 0) {
return result;
}
if (num_sector != 1) {
return 0x11;
}
pf_memcpy(volume->label, label, 0xB);
volume->label[0xB] = 0;
return 0;
}
void PFDRV_MediaEject(void* partition) {
PFDRV_VOLUME* p_vol;
pf_u32 i;
p_vol = pf_vol_set.volumes;
for (i = 0; i < 26; p_vol++, i++) {
if (p_vol->driver_partition == partition) {
if ((p_vol->flags & 0x08) != 0) {
p_vol->flags |= 0x20;
}
p_vol->flags &= ~0x0C;
}
}
}
void PFDRV_MediaInsert(void* partition) {
PFDRV_VOLUME* p_vol;
pf_u32 i;
p_vol = pf_vol_set.volumes;
for (i = 0; i < 26; p_vol++, i++) {
if (p_vol->driver_partition == partition) {
p_vol->flags |= 0x14;
}
}
}
pf_s32 PFDRV_IsInserted(PFDRV_VOLUME* volume) {
return (volume->flags >> 2) & 1;
}
pf_s32 PFDRV_IsMountRequested(PFDRV_VOLUME* volume) {
return (volume->flags >> 4) & 1;
}
void PFDRV_ClearMountRequested(PFDRV_VOLUME* volume) {
volume->flags &= ~0x10;
}
pf_s32 PFDRV_IsUnmountRequested(PFDRV_VOLUME* volume) {
return (volume->flags >> 5) & 1;
}
void PFDRV_ClearUnmountRequested(PFDRV_VOLUME* volume) {
volume->flags &= ~0x20;
}
pf_s32 PFDRV_init(PFDRV_VOLUME* volume) {
PFDRV_PART_FUNC func;
pf_s32 err;
func.insert = PFDRV_MediaInsert;
func.eject = PFDRV_MediaEject;
err = pdm_part_register_callback(volume->driver_partition, &func);
if (err != 0) {
return -1;
}
err = pdm_part_init(volume->driver_partition);
if (err != 0) {
pdm_part_unregister_callback(volume->driver_partition);
if (err == 0x15) {
err = pdm_part_get_driver_error_code(volume->driver_partition);
pf_vol_set.last_driver_error = err;
volume->last_driver_error = err;
return 0x1000;
}
return -1;
}
return 0;
}
pf_s32 PFDRV_finalize(PFDRV_VOLUME* volume) {
pf_s32 err;
err = pdm_part_unregister_callback(volume->driver_partition);
if (err != 0) {
return -1;
}
err = pdm_part_finalize(volume->driver_partition);
if (err != 0) {
if (err == 0x15) {
err = pdm_part_get_driver_error_code(volume->driver_partition);
pf_vol_set.last_driver_error = err;
volume->last_driver_error = err;
return 0x1000;
}
return -1;
}
return 0;
}
pf_s32 PFDRV_mount(PFDRV_VOLUME* volume) {
PFDRV_MEDIA_INFO info;
pf_u32 option;
PFDRV_CACHE_PAGE* p_page;
pf_u32 num_sector;
pf_s32 err;
err = pdm_part_mount(volume->driver_partition);
if (err != 0) {
if (err == 0x15) {
err = pdm_part_get_driver_error_code(volume->driver_partition);
pf_vol_set.last_driver_error = err;
volume->last_driver_error = err;
return 0x1000;
}
return -1;
}
err = pdm_part_get_media_attribute(volume->driver_partition, &info);
if (err != 0) {
pdm_part_unmount(volume->driver_partition, 0);
return -1;
}
if ((info.media_attr & 0x01) != 0) {
volume->flags |= 0x02;
} else {
volume->flags &= ~0x02;
}
if ((info.media_attr & 0x02) != 0) {
const pf_u8* format_param = info.format_param;
volume->flags |= 0x40;
volume->format_options = format_param;
}
if ((info.media_attr & 0x08) != 0) {
volume->flags |= 0x80;
}
PFCACHE_AllocateDataPage(volume, -1, &p_page, &option);
err = pdm_part_logical_read(volume->driver_partition, p_page->buffer, 0, 1, 0x200, &num_sector);
if (err != 0) {
if (err == 0x15) {
err = pdm_part_get_driver_error_code(volume->driver_partition);
pf_vol_set.last_driver_error = err;
volume->last_driver_error = err;
err = 0x1000;
} else {
err = -1;
}
PFCACHE_FreeDataPage(volume, p_page);
pdm_part_unmount(volume->driver_partition, 0);
return err;
}
err = pdm_bpb_InitVolumeWithBootSector(volume, p_page->buffer);
if (err != 0) {
PFCACHE_FreeDataPage(volume, p_page);
pdm_part_unmount(volume->driver_partition, 0);
return 7;
}
volume->num_free_clusters = -1;
volume->last_free_cluster = -1;
if (volume->bpb.fat_type == FAT_32) {
err = pdm_part_logical_read(volume->driver_partition, p_page->buffer, volume->bpb.fs_info_sector, 1, 0x200,
&num_sector);
if (err != 0) {
if (err == 0x15) {
err = pdm_part_get_driver_error_code(volume->driver_partition);
pf_vol_set.last_driver_error = err;
volume->last_driver_error = err;
err = 0x1000;
} else {
err = -1;
}
PFCACHE_FreeDataPage(volume, p_page);
pdm_part_unmount(volume->driver_partition, 0);
return err;
}
err = pdm_bpb_InitVolumeWithFSINFOSector(volume, p_page->buffer, &volume->num_free_clusters,
&volume->last_free_cluster);
if (err != 0) {
PFCACHE_FreeDataPage(volume, p_page);
pdm_part_unmount(volume->driver_partition, 0);
return -1;
}
}
PFCACHE_FreeDataPage(volume, p_page);
return 0;
}
pf_s32 PFDRV_unmount(PFDRV_VOLUME* volume, pf_u32 mode) {
pf_s32 err;
err = pdm_part_unmount(volume->driver_partition, mode == 1);
if (err != 0) {
if (err == 0x15) {
err = pdm_part_get_driver_error_code(volume->driver_partition);
pf_vol_set.last_driver_error = err;
volume->last_driver_error = err;
return 0x1000;
}
return -1;
}
return 0;
}
pf_s32 PFDRV_format(PFDRV_VOLUME* volume, const pf_u8* format_options) {
pf_s32 err;
err = pdm_part_format(volume->driver_partition, format_options);
if (err != 0) {
if (err == 0x15) {
err = pdm_part_get_driver_error_code(volume->driver_partition);
pf_vol_set.last_driver_error = err;
volume->last_driver_error = err;
return 0x1000;
}
return -1;
}
return 0;
}
pf_s32 PFDRV_lread(PFDRV_VOLUME* volume, pf_u8* buffer, pf_u32 sector, pf_u32 num_sector, pf_u32* p_num_success) {
pf_s32 err;
err = pdm_part_logical_read(volume->driver_partition, buffer, sector, num_sector, volume->bpb.bytes_per_sector,
p_num_success);
if (err != 0) {
if (err == 0x15) {
err = pdm_part_get_driver_error_code(volume->driver_partition);
pf_vol_set.last_driver_error = err;
volume->last_driver_error = err;
return 0x1000;
}
return -1;
}
return 0;
}
pf_s32 PFDRV_lwrite(PFDRV_VOLUME* volume, const pf_u8* buffer, pf_u32 sector, pf_u32 num_sector, pf_u32* p_num_success) {
pf_s32 err;
err = pdm_part_logical_write(volume->driver_partition, buffer, sector, num_sector, volume->bpb.bytes_per_sector,
p_num_success);
if (err != 0) {
if (err == 0x15) {
err = pdm_part_get_driver_error_code(volume->driver_partition);
pf_vol_set.last_driver_error = err;
volume->last_driver_error = err;
return 0x1000;
}
return -1;
}
return 0;
}

View File

@@ -1,759 +0,0 @@
#include <private/vf/PrFILE2/fatfs/pf_entry.h>
typedef struct PFENTRY_FAT_HINT {
pf_u32 chain_index;
pf_u32 cluster;
pf_u32 previous_cluster;
} PFENTRY_FAT_HINT;
typedef struct PFENTRY_FFD {
pf_u32 start_cluster;
pf_u32 current_start_cluster;
pf_u32* p_start_cluster;
PF_LAST_CLUSTER last_cluster;
PF_FAT_LAST_ACCESS last_access_cluster;
PF_CLUSTER_LINK cluster_link;
PFENTRY_FAT_HINT* p_hint;
PF_VOLUME* p_vol;
} PFENTRY_FFD;
typedef struct PFENTRY_ENT_ITER {
pf_u32 index;
PF_VOLUME* p_vol;
PFENTRY_FFD ffd;
pf_u32 file_sector_index;
pf_u32 sector;
pf_u16 offset;
pf_u16 offset_mask;
pf_u8 buf[32];
pf_u8 log2_entries_per_sector;
} PFENTRY_ENT_ITER;
typedef struct PFENTRY_BPB {
pf_u16 bytes_per_sector;
pf_u16 num_reserved_sectors;
pf_u16 num_root_dir_entries;
pf_u8 sectors_per_cluster;
pf_u8 num_FATs;
pf_u32 total_sectors;
pf_u32 sectors_per_FAT;
pf_u32 root_dir_cluster;
pf_u16 fs_info_sector;
pf_u16 backup_boot_sector;
pf_u16 ext_flags;
pf_u8 media;
pf_s32 fat_type;
pf_u8 log2_bytes_per_sector;
pf_u8 log2_sectors_per_cluster;
pf_u8 num_active_FATs;
pf_u16 num_root_dir_sectors;
pf_u32 active_FAT_sector;
pf_u32 first_root_dir_sector;
pf_u32 first_data_sector;
pf_u32 num_clusters;
} PFENTRY_BPB;
struct PF_VOLUME {
PFENTRY_BPB bpb;
pf_u32 num_free_clusters;
pf_u32 last_free_cluster;
pf_u8 file_and_directory_state[0x1f74 - 0x40];
struct {
pf_u32 tracker_size;
pf_u32 tracker_max;
pf_u32* tracker_bits;
} tail_entry;
};
typedef struct PFENTRY_CONTEXT_VOLUME {
pf_u32 stat;
pf_s32 context_id;
PF_VOLUME* p_vol;
} PFENTRY_CONTEXT_VOLUME;
typedef struct PFENTRY_VOLUME_SET {
pf_u32 flags;
pf_u32 reserved;
PF_VOLUME* current_volume;
PFENTRY_CONTEXT_VOLUME context_volumes[3];
pf_s32 num_attached;
pf_s32 num_mounted;
pf_u32 config;
void* user_data;
pf_s32 last_error;
pf_s32 last_driver_error;
void (*code_callbacks[6])(void);
pf_u32 setting;
} PFENTRY_VOLUME_SET;
typedef struct PFENTRY_SYS_DATE { pf_u32 sys_year, sys_month, sys_day; } PFENTRY_SYS_DATE;
typedef struct PFENTRY_SYS_TIME { pf_u32 sys_hour, sys_min, sys_sec, sys_ms; } PFENTRY_SYS_TIME;
extern PFENTRY_VOLUME_SET pf_vol_set;
void* pf_memcpy(void* dst, void* src, pf_u32 length);
void* pf_memset(void* dst, pf_s32 c, pf_u32 length);
void PF_LE16_TO_U16_STR(pf_u8* sSrc, pf_u32 num);
pf_s32 PFPATH_cmpNameUni(const pf_u16* p_name, PF_STR* sPattern);
pf_s32 PFPATH_cmpName(const pf_s8* sShort, PF_STR* p_pattern, pf_bool is_short_search);
pf_s32 PFPATH_cmpTailSFN(const pf_s8* sfn_name, const pf_s8* pattern);
pf_s32 PFPATH_putShortName(pf_u8* pDirEntry, const pf_s8* short_name, pf_u8 attr);
pf_s32 PFPATH_getShortName(pf_s8* short_name, const pf_u8* pDirEntry, pf_u8 attr);
void PFPATH_getLongNameformShortName(pf_s8* short_name, pf_s8* long_name, pf_u8 flag);
pf_s32 PFPATH_transformInUnicode(pf_u16* sDestStr, const pf_s8* sSrcStr);
pf_s32 PFPATH_parseShortNameNumeric(pf_s8* p_char, pf_u32 count);
pf_u32 PFPATH_CheckExtShortNameSignature(PF_STR* p_str);
pf_bool PFPATH_GetExtShortNameIndex(PF_STR* p_str, pf_u32* p_index);
pf_s32 PFSEC_WriteData(PF_VOLUME* p_vol, const pf_u8* p_buf, pf_u32 sector, pf_u16 offset, pf_u32 size, pf_u32* p_success_size, pf_bool set_sig);
pf_s32 PFCACHE_AllocateDataPage(PF_VOLUME*, pf_u32, PF_CACHE_PAGE**, pf_bool*);
void PFCACHE_FreeDataPage(PF_VOLUME*, PF_CACHE_PAGE*);
pf_s32 PFCACHE_FlushDataCacheSpecific(PF_VOLUME*, void*);
pf_s32 PFFAT_InitFFD(PFENTRY_FFD*, PFENTRY_FAT_HINT*, PF_VOLUME*, pf_u32*);
pf_s32 PFFAT_GetSectorSpecified(PFENTRY_FFD*, pf_u32, pf_u32, pf_u32*);
pf_s32 PFFAT_MakeRootDir(PF_VOLUME*);
pf_s32 PFENT_ITER_IteratorInitialize(PFENTRY_ENT_ITER*, pf_u32);
pf_s32 PFENT_ITER_Advance(PFENTRY_ENT_ITER*, pf_u32);
pf_s32 PFENT_ITER_Retreat(PFENTRY_ENT_ITER*, pf_u32);
pf_bool PFENT_ITER_IsAtLogicalEnd(PFENTRY_ENT_ITER*);
void PFDRV_LoadVolumeLabelFromBuf(PF_VOLUME*, const pf_u8*);
void PFSYS_TimeStamp(PFENTRY_SYS_DATE*, PFENTRY_SYS_TIME*);
void PFENT_LoadShortNameFromBuf(PF_DIR_ENT*, const pf_u8*);
pf_u8 PFENT_CalcCheckSum(PF_DIR_ENT*);
void PFENT_loadEntryNumericFieldsFromBuf(PF_DIR_ENT*, const pf_u8*);
pf_s32 PFENT_LoadLFNEntryFieldsFromBuf(PF_DIR_ENT*, const pf_u8*);
static void PFENT_storeShortNameToBuf(pf_u8* buf, const PF_DIR_ENT* p_ent) {
PFPATH_putShortName(buf, p_ent->short_name, p_ent->attr);
if (*buf == 0xE5) {
*buf = 5;
}
}
pf_s32 PFENT_compareEntry(PF_DIR_ENT* p_ent, const pf_u8* buf, PF_STR* p_pattern, pf_s32 attr,
pf_u32 attr_required, pf_u32 attr_forbidden, pf_u32* p_lpos) {
pf_s32 is_match = 1;
pf_u32 is_valid = 1;
if ((attr & 15) == 15) {
is_match = 1;
} else {
if (attr == 0) attr = 0x40;
if (attr_required & 0x80) {
attr_required &= 0x7f;
attr_forbidden &= 0x7f;
if ((attr_required && (pf_s32)(attr_required & attr) != (pf_s32)attr_required) ||
(attr_forbidden && (pf_s32)(attr_forbidden & attr) == (pf_s32)attr_forbidden)) is_valid = 0;
} else if (attr_required != 0x7f && (pf_u32)attr != attr_required &&
(!(attr & attr_required) || (attr & attr_forbidden))) is_valid = 0;
if (is_valid == 0) {
is_match = 1;
} else {
if (attr & 8) {
p_ent->num_entry_LFNs = 0;
p_ent->long_name[0] = 0;
}
PFENT_LoadShortNameFromBuf(p_ent, buf);
if (p_ent->num_entry_LFNs && p_ent->ordinal == 1 && p_ent->check_sum == PFENT_CalcCheckSum(p_ent)) {
is_match = PFPATH_cmpNameUni(p_ent->long_name, p_pattern) != 0;
}
if (is_match == 1) {
if ((pf_vol_set.setting & 2) == 2 && !(attr & 8)) {
if (PFPATH_cmpName(p_ent->short_name, p_pattern, 0) == 0) is_match = 0;
} else {
if (PFPATH_cmpName(p_ent->short_name, p_pattern, 1) == 0) is_match = 0;
}
}
if (is_match == 1) ++*p_lpos;
}
}
return is_match;
}
pf_s32 PFENT_getEntry(PF_DIR_ENT* p_ent, PFENTRY_ENT_ITER* p_iter, PF_STR* p_pattern,
pf_u32 attr_required, pf_u32 attr_forbidden, pf_u32* p_lpos) {
pf_s32 err;
pf_u8 attr = p_iter->buf[11];
if (PFENT_compareEntry(p_ent, p_iter->buf, p_pattern, attr, attr_required, attr_forbidden, p_lpos) == 1) {
if ((attr & 15) == 15) {
err = PFENT_LoadLFNEntryFieldsFromBuf(p_ent, p_iter->buf);
if (err != 0) {
p_ent->num_entry_LFNs = 0;
p_ent->long_name[0] = 0;
}
} else {
p_ent->num_entry_LFNs = 0;
p_ent->long_name[0] = 0;
}
return -1;
}
if (p_ent->num_entry_LFNs == 0 && (p_iter->buf[12] & 0x18)) {
pf_s8 filename[13];
pf_s32 length;
PFPATH_getLongNameformShortName(p_ent->short_name, filename, p_iter->buf[12]);
length = PFPATH_transformInUnicode(p_ent->long_name, filename);
p_ent->num_entry_LFNs = length / 13 + ((length % 13) != 0);
p_ent->check_sum = PFENT_CalcCheckSum(p_ent);
p_ent->ordinal = 1;
}
PFENT_loadEntryNumericFieldsFromBuf(p_ent, p_iter->buf);
p_ent->entry_sector = p_iter->sector;
p_ent->entry_offset = p_iter->offset;
if ((p_ent->attr & 0x10) && p_ent->start_cluster == 0) p_ent->start_cluster = 1;
return 0;
}
pf_s32 PFENT_searchEmptyTailSFN(PFENTRY_FFD* p_ffd, pf_u32 tail_index, const pf_s8* pattern, pf_u32* p_tail_bit) {
PFENTRY_ENT_ITER iter;
pf_u8 attr;
pf_s32 err = 0;
pf_u32 bit_pos;
pf_u32 sfn_taillen;
pf_u32 sfn_baselen;
pf_u32 i;
pf_s8 sfnbuf[13];
pf_s8 patbuf[13];
PF_VOLUME* p_vol = p_ffd->p_vol;
pf_memset(p_tail_bit, 0, p_vol->tail_entry.tracker_size * 4);
iter.ffd = *p_ffd;
for (err = PFENT_ITER_IteratorInitialize(&iter, 0); PFENT_ITER_IsAtLogicalEnd(&iter) == PF_FALSE;
err = PFENT_ITER_Advance(&iter, PF_FALSE)) {
if (err != 0) {
return err;
}
if (*iter.buf == 0) {
break;
}
if (*iter.buf != 0xE5) {
attr = iter.buf[11];
if (((attr & (0x01 | 0x02 | 0x04 | 0x08)) != (0x01 | 0x02 | 0x04 | 0x08)) && ((attr & 8) == 0)) {
PFPATH_getShortName((pf_s8*)&sfnbuf, (pf_u8*)&iter.buf, 0);
for (sfn_taillen = 1; (sfnbuf[sfn_taillen] != 0x7E) && (sfnbuf[sfn_taillen] != 0) && (sfn_taillen < 7U); sfn_taillen++) {
}
if (sfn_taillen < 7 && sfnbuf[sfn_taillen] == 0x7E) {
for (sfn_baselen = sfn_taillen + 1; sfnbuf[sfn_baselen] >= 0x30 && sfnbuf[sfn_baselen] <= 0x39; sfn_baselen++) {
}
if (sfnbuf[sfn_baselen] == '.' || sfnbuf[sfn_baselen] == 0) {
i = (sfn_baselen - sfn_taillen) - 1;
bit_pos = 0;
for (; i != 0; i--) {
bit_pos *= 10;
bit_pos += (sfnbuf[sfn_baselen - i]) - 0x30;
}
pf_strcpy((pf_s8*)&patbuf, pattern);
PFPATH_parseShortNameNumeric((pf_s8*)&patbuf, bit_pos);
if (PFPATH_cmpTailSFN((pf_s8*)&sfnbuf, (pf_s8*)&patbuf) == 0 && bit_pos >= tail_index &&
bit_pos < (tail_index + (p_vol->tail_entry.tracker_size << 5))) {
bit_pos -= tail_index;
p_tail_bit[bit_pos >> 5] |= 1 << bit_pos;
}
}
}
}
}
}
return 0;
}
pf_s32 PFENT_findEmptyTailSFN(PF_DIR_ENT* p_ent_containig_dir, const pf_s8* name, pf_u32* p_tails) {
PFENTRY_FFD ffd;
PFENTRY_FAT_HINT hint;
pf_s32 err;
pf_u32 num;
PF_VOLUME* p_vol = p_ent_containig_dir->p_vol;
pf_u32 track_num;
*p_tails = 1;
PFFAT_InitFFD(&ffd, &hint, p_ent_containig_dir->p_vol, &p_ent_containig_dir->start_cluster);
for (num = 1; num <= (1000000 - 1); num += p_vol->tail_entry.tracker_size << 5) {
err = PFENT_searchEmptyTailSFN(&ffd, num, name, p_vol->tail_entry.tracker_bits);
if (err != 0) {
return err;
}
for (track_num = 0; track_num < p_vol->tail_entry.tracker_size; track_num += 1) {
if (p_vol->tail_entry.tracker_bits[track_num] != -1) {
for (; (p_vol->tail_entry.tracker_bits[track_num] & 1) != 0; p_vol->tail_entry.tracker_bits[track_num] >>= 1, (*p_tails)++) {
}
num = 1000000;
break;
} else {
*p_tails += 0x20;
}
}
}
return 0;
}
pf_u8 PFENT_CalcCheckSum(PF_DIR_ENT* p_ent) {
pf_u16 i;
pf_u8 sum;
pf_u8 buf[13];
PFPATH_putShortName((pf_u8*)buf, p_ent->short_name, 0);
sum = 0;
for (i = 0; i < 11; i++) {
sum = ((sum & 1) != 0 ? 0x80 : 0) + (sum >> 1) + buf[i];
}
return sum;
}
void PFENT_LoadShortNameFromBuf(PF_DIR_ENT* p_ent, const pf_u8* buf) {
PFPATH_getShortName(p_ent->short_name, buf, buf[11]);
if (p_ent->short_name[0] == 5) {
p_ent->short_name[0] = -0x1B;
}
}
void PFENT_loadEntryNumericFieldsFromBuf(PF_DIR_ENT* p_ent, const pf_u8* buf) {
p_ent->attr = buf[0xB];
p_ent->small_letter_flag = buf[0xC];
p_ent->create_time_ms = buf[0xD];
p_ent->create_time = PF_SWAP_16(*(pf_u16*)&buf[0xE]);
p_ent->create_date = PF_SWAP_16(*(pf_u16*)&buf[0x10]);
p_ent->access_date = PF_SWAP_16(*(pf_u16*)&buf[0x12]);
p_ent->modify_time = PF_SWAP_16(*(pf_u16*)&buf[0x16]);
p_ent->modify_date = PF_SWAP_16(*(pf_u16*)&buf[0x18]);
p_ent->file_size = PF_SWAP_32(*(pf_u32*)&buf[0x1C]);
p_ent->start_cluster = ((pf_u16)PF_SWAP_16(*(pf_u16*)&buf[0x14]) << 16) | (pf_u16)(PF_SWAP_16(*(pf_u16*)&buf[0x1A]));
}
void PFENT_StoreEntryNumericFieldsToBuf(pf_u8* buf, const PF_DIR_ENT* p_ent) {
buf[0x0B] = p_ent->attr;
buf[0x0C] = p_ent->small_letter_flag;
buf[0x0D] = p_ent->create_time_ms;
*(pf_u16*)&buf[0x0E] = PF_SWAP_16(p_ent->create_time);
*(pf_u16*)&buf[0x10] = PF_SWAP_16(p_ent->create_date);
*(pf_u16*)&buf[0x12] = PF_SWAP_16(p_ent->access_date);
*(pf_u16*)&buf[0x16] = PF_SWAP_16(p_ent->modify_time);
*(pf_u16*)&buf[0x18] = PF_SWAP_16(p_ent->modify_date);
*(pf_u16*)&buf[0x14] = PF_SWAP_16((pf_u16)(p_ent->start_cluster >> 16));
*(pf_u16*)&buf[0x1A] = PF_SWAP_16((pf_u16)(p_ent->start_cluster));
*(pf_u32*)&buf[0x1C] = PF_SWAP_32(p_ent->file_size);
}
pf_s32 PFENT_LoadLFNEntryFieldsFromBuf(PF_DIR_ENT* p_ent, const pf_u8* buf) {
pf_u8 ordinal;
pf_u8 check_sum;
pf_u32 is_last_LFN_ent;
pf_u8* p;
pf_u16* q;
pf_u16* q_after;
ordinal = buf[0];
check_sum = buf[13];
if ((ordinal & ~0x40U) > 0x14U) {
p_ent->ordinal = 0;
p_ent->check_sum = 0;
p_ent->num_entry_LFNs = 0;
return 33;
}
if ((buf[0] & 0x40) != 0) {
ordinal &= ~0x40;
p_ent->num_entry_LFNs = 0;
is_last_LFN_ent = 1;
} else {
is_last_LFN_ent = 0;
if (p_ent->num_entry_LFNs == 0) return 33;
if (ordinal != p_ent->ordinal - 1 || check_sum != p_ent->check_sum) {
p_ent->ordinal = 0;
p_ent->check_sum = 0;
p_ent->num_entry_LFNs = 0;
return 33;
}
}
p_ent->ordinal = ordinal;
p_ent->check_sum = check_sum;
p = (pf_u8*)p_ent->long_name;
p += (ordinal - 1) * 0x1A;
pf_memcpy(&p[0x0], (pf_s8*)&buf[0x1], 10);
pf_memcpy(&p[10], (pf_s8*)&buf[0xE], 0xC);
pf_memcpy(&p[0x16], (pf_s8*)&buf[0x1C], 4U);
PF_LE16_TO_U16_STR(&p[0x0], 10);
PF_LE16_TO_U16_STR(&p[10], 0xC);
PF_LE16_TO_U16_STR(&p[0x16], 4U);
if (is_last_LFN_ent != 0) {
((pf_u16*)p)[13] = 0;
q = (pf_u16*)p;
q_after = (pf_u16*)&p[0x1A];
for (; q < q_after; q++) {
if (*q == 0) {
q++;
break;
}
}
for (; q < q_after; q++) {
if (*q != 0xFFFF) {
p_ent->num_entry_LFNs = 0;
return 33;
}
}
}
p_ent->num_entry_LFNs++;
return 0;
}
void PFENT_storeLFNEntryFieldsToBuf(pf_u8* buf, PF_DIR_ENT* p_ent, pf_u8 ord, pf_u8 sum, pf_bool is_last) {
pf_u8* p_seg;
pf_u16* p;
pf_u16* p_after;
if (is_last) {
*buf = ord | 0x40;
} else {
*buf = ord;
}
buf[0xB] = (0x01 | 0x02 | 0x04 | 0x08);
buf[0xD] = sum;
buf[0xC] = 0;
*(pf_u16*)&buf[0x1A] = 0;
p_seg = (pf_u8*)p_ent->long_name;
p_seg += (ord - 1) * 0x1A;
if (is_last != 0) {
p = (pf_u16*)p_seg;
p_after = (pf_u16*)&p_seg[0x1A];
for (; p < p_after; p++) {
if (*p == 0x0000) {
p++;
break;
}
}
for (; p < p_after; p++) {
*p = 0xFFFF;
}
}
pf_memcpy(&buf[1], p_seg, 10);
pf_memcpy(&buf[0xE], &p_seg[10], 0xC);
pf_memcpy(&buf[0x1C], &p_seg[0x16], 4);
PF_LE16_TO_U16_STR(&buf[1], 10);
PF_LE16_TO_U16_STR(&buf[0xE], 0xC);
PF_LE16_TO_U16_STR(&buf[0x1C], 4);
}
pf_s32 PFENT_findEntryPos(PFENTRY_FFD* p_ffd, PF_DIR_ENT* p_ent, pf_u32 index_search_from, PF_STR* p_pattern,
pf_u32 attr_required, pf_u32 attr_forbidden, pf_u32* p_lpos, pf_u32* p_ppos) {
pf_s32 err;
PFENTRY_ENT_ITER iter;
pf_u32 lpos;
pf_bool is_extsfn = 0;
*p_lpos = 0;
*p_ppos = 0;
p_ent->num_entry_LFNs = 0;
p_ent->ordinal = 0;
p_ent->check_sum = 0;
p_ent->long_name[0] = 0;
lpos = 0;
if ((pf_vol_set.setting & 2) == 2) is_extsfn = PFPATH_GetExtShortNameIndex(p_pattern, &index_search_from);
iter.ffd = *p_ffd;
if (is_extsfn == 1) {
err = PFENT_ITER_IteratorInitialize(&iter, index_search_from - 1);
{
if (err != 0) {
*p_lpos = 999999;
*p_ppos = 999999;
return err;
}
if (PFENT_getEntry(p_ent, &iter, p_pattern, attr_required, attr_forbidden, &lpos) == 0) {
*p_lpos = 999999;
*p_ppos = 999999;
return 3;
}
if (iter.buf[0] & 0x40) {
err = PFENT_ITER_IteratorInitialize(&iter, index_search_from);
if (err != 0) {
*p_lpos = 999999;
*p_ppos = 999999;
return err;
}
if (PFENT_getEntry(p_ent, &iter, p_pattern, attr_required, attr_forbidden, &lpos) == 0) {
p_ent->p_vol = p_ffd->p_vol;
*p_lpos = lpos;
*p_ppos = iter.index;
return 0;
}
}
}
} else {
for (err = PFENT_ITER_IteratorInitialize(&iter, index_search_from); !PFENT_ITER_IsAtLogicalEnd(&iter); err = PFENT_ITER_Advance(&iter, 0)) {
if (err != 0) {
*p_lpos = 999999;
*p_ppos = 999999;
return err;
}
if (iter.buf[0] == 0) break;
if (iter.buf[0] == 0xe5) {
p_ent->num_entry_LFNs = 0;
p_ent->long_name[0] = 0;
} else if (PFENT_getEntry(p_ent, &iter, p_pattern, attr_required, attr_forbidden, &lpos) == 0) {
p_ent->p_vol = p_ffd->p_vol;
*p_lpos = lpos;
*p_ppos = iter.index;
return 0;
}
}
}
*p_lpos = 999999;
*p_ppos = 999999;
return 3;
}
pf_s32 PFENT_findEntry(PFENTRY_FFD* p_ffd, PF_DIR_ENT* p_ent, pf_u32 index_search_from, PF_STR* p_pattern,
pf_u32 attr_required, pf_u32 attr_forbidden) {
pf_u32 lpos;
pf_u32 ppos;
return PFENT_findEntryPos(p_ffd, p_ent, index_search_from, p_pattern, attr_required, attr_forbidden, &lpos, &ppos);
}
pf_s32 PFENT_allocateEntryPos(PF_DIR_ENT* p_ent, pf_u32 num_entries, PFENTRY_FFD* p_ffd, pf_u32* p_prev_chain,
PF_STR* p_filename, pf_u32* p_pos) {
pf_s32 err;
PFENTRY_ENT_ITER iter;
PF_DIR_ENT current_ent;
PF_VOLUME* p_vol;
pf_u32 next_sector;
pf_u32 logical_scratch;
pf_u32 saved_sector;
pf_u16 saved_offset;
pf_u32 current_sector;
pf_u32 previous_sector0;
pf_u32 previous_sector1;
pf_bool is_found;
pf_u32 num_free_entries;
pf_u32 logical_position;
if ((pf_vol_set.setting & 2) == 2 && PFPATH_CheckExtShortNameSignature(p_filename) == 1) return 1;
p_vol = p_ffd->p_vol;
saved_sector = -1U;
p_prev_chain[0] = p_prev_chain[1] = saved_sector;
num_free_entries = 0;
logical_position = 0;
saved_offset = 0;
current_sector = -1U;
previous_sector1 = -1U;
previous_sector0 = -1U;
is_found = 0;
iter.sector = 0;
iter.index = 0;
iter.ffd = *p_ffd;
for (err = PFENT_ITER_IteratorInitialize(&iter, 0);; err = PFENT_ITER_Advance(&iter, 1)) {
if (err != 0) {
if (err == 16) break;
return err;
}
if (iter.sector == -1U) break;
if (is_found == 0 && num_free_entries == 0) {
saved_offset = iter.offset;
saved_sector = iter.sector;
current_sector = iter.sector;
}
if (is_found == 0) {
if (iter.buf[0] == 0 || iter.buf[0] == 0xe5) {
if (current_sector != iter.sector) {
pf_bool is_first = previous_sector0 == -1U;
if (is_first) previous_sector0 = iter.sector;
if (!is_first) previous_sector1 = iter.sector;
current_sector = iter.sector;
}
++num_free_entries;
} else {
num_free_entries = 0;
previous_sector0 = -1U;
previous_sector1 = -1U;
}
}
if (num_free_entries >= num_entries) {
if (is_found == 0) {
logical_position = iter.index;
is_found = 1;
}
if (iter.buf[0] == 0) break;
}
if (iter.buf[0] != 0 && iter.buf[0] != 0xe5 && PFENT_getEntry(&current_ent, &iter, p_filename, 0x7f, 0, &logical_scratch) == 0) {
*p_ent = current_ent;
p_ent->p_vol = p_ffd->p_vol;
return 8;
}
if (iter.offset + 32 == p_vol->bpb.bytes_per_sector) {
err = PFFAT_GetSectorSpecified(p_ffd, iter.file_sector_index + 1, 0, &next_sector);
if (err != 0) return err;
if (next_sector == -1U && is_found == 1) break;
}
}
if (is_found == 0) return 5;
p_prev_chain[0] = previous_sector0;
p_prev_chain[1] = previous_sector1;
p_ent->entry_sector = saved_sector;
p_ent->entry_offset = saved_offset;
*p_pos = logical_position;
return 0;
}
pf_s32 PFENT_allocateEntry(PF_DIR_ENT* p_ent, pf_u32 num_entries, PFENTRY_FFD* p_ffd, pf_u32* p_prev_chain, PF_STR* p_filename) {
pf_u32 position;
return PFENT_allocateEntryPos(p_ent, num_entries, p_ffd, p_prev_chain, p_filename, &position);
}
pf_s32 PFENT_GetRootDir(PF_VOLUME* p_vol, PF_DIR_ENT* p_ent) {
p_ent->long_name[0] = 0x5C;
p_ent->long_name[1] = 0;
p_ent->num_entry_LFNs = 0;
p_ent->ordinal = 0;
p_ent->check_sum = 0;
p_ent->short_name[0] = 0x5C;
p_ent->short_name[1] = 0;
p_ent->small_letter_flag = 0;
p_ent->attr = 0x10;
p_ent->create_time_ms = 0;
p_ent->create_time = 0;
p_ent->create_date = 0;
p_ent->access_date = 0;
p_ent->modify_time = 0;
p_ent->modify_date = 0;
p_ent->file_size = 0;
p_ent->p_vol = p_vol;
p_ent->path_len = 3;
p_ent->start_cluster = 1;
p_ent->entry_sector = -1;
p_ent->entry_offset = 0;
return 0;
}
pf_s32 PFENT_MakeRootDir(PF_VOLUME* p_vol) {
PF_CACHE_PAGE* p_page;
pf_u32 sector;
pf_u32 success_size;
pf_bool hit;
pf_s32 err;
switch (p_vol->bpb.fat_type) {
case FAT_32:
PFFAT_MakeRootDir(p_vol);
break;
case FAT_12:
case FAT_16:
err = PFCACHE_AllocateDataPage(p_vol, -1U, &p_page, &hit);
if (err != 0) return err;
pf_memset(p_page->p_buf, 0, p_vol->bpb.bytes_per_sector);
for (sector = p_vol->bpb.first_root_dir_sector; sector < p_vol->bpb.first_data_sector; ++sector) {
err = PFSEC_WriteData(p_vol, p_page->p_buf, sector, 0, p_vol->bpb.bytes_per_sector, &success_size, 0);
if (err != 0) {
PFCACHE_FreeDataPage(p_vol, p_page);
return err;
}
if (success_size != p_vol->bpb.bytes_per_sector) {
PFCACHE_FreeDataPage(p_vol, p_page);
return 17;
}
}
PFCACHE_FreeDataPage(p_vol, p_page);
break;
default:
return 7;
}
PFDRV_LoadVolumeLabelFromBuf(p_vol, (pf_u8*)"NO NAME ");
return 0;
}
pf_s32 PFENT_updateEntry(PF_DIR_ENT* p_ent, pf_u32 flag) {
PF_VOLUME* p_vol;
pf_u32 success_size;
pf_u8 buf[32];
pf_s32 err;
p_vol = p_ent->p_vol;
if (p_ent->start_cluster == 1) {
return 0xE;
}
if (flag == 1) {
p_ent->attr |= (pf_u8)0x20;
}
PFENT_storeShortNameToBuf(buf, p_ent);
PFENT_StoreEntryNumericFieldsToBuf(buf, p_ent);
err = PFSEC_WriteData(p_vol, buf, p_ent->entry_sector, p_ent->entry_offset, 32, &success_size, 0U);
if (err != 0) {
return err;
}
if (success_size != 0x20) {
return 0x11;
}
return 0;
}
pf_s32 PFENT_AdjustSFN(PF_DIR_ENT* p_ent, pf_s8* p_short_name) {
pf_u32 i = 0;
pf_u32 tail_num;
pf_s32 err = 0;
for (i = 1; (p_short_name[i] != 0x7E) && (p_short_name[i] != 0) && (i < 7U); i++) {
}
if ((i < 7U) && (p_short_name[i] == 0x7E)) {
for (i++; (p_short_name[i] >= 0x30) && (p_short_name[i] <= 0x39); i++) {
}
if ((p_short_name[i] == '.') || (p_short_name[i] == 0)) {
err = PFENT_findEmptyTailSFN(p_ent, p_short_name, &tail_num);
if (err != 0) {
return err;
}
if (tail_num != 1) {
PFPATH_parseShortNameNumeric(p_short_name, tail_num);
}
}
}
return 0;
}
pf_u8 PFENT_getcurrentDateTimeForEnt(pf_u16* p_date, pf_u16* p_time) {
PFENTRY_SYS_DATE sys_date;
PFENTRY_SYS_TIME sys_time;
PFSYS_TimeStamp(&sys_date, &sys_time);
*p_date = ((sys_date.sys_day & 0x1F) | ((sys_date.sys_month << 5) & 0x1E0)) | (((sys_date.sys_year - 0x7BC) << 9) & 0xFE00);
*p_time = (((sys_time.sys_sec >> 1) & 0x1F) | ((sys_time.sys_min << 5) & 0x7E0)) | ((sys_time.sys_hour << 0xB) & 0xF800);
return sys_time.sys_ms;
}
pf_s32 PFENT_RemoveEntry(PF_DIR_ENT* p_ent, PFENTRY_ENT_ITER* p_iter) {
pf_u32 success_size;
pf_s32 err = 0;
pf_u32 entry_sector;
pf_u16 entry_offset;
pf_u32 i;
PF_VOLUME* p_vol = PF_NULL;
const pf_u8 dir_fb_free[1] = {0xE5};
p_vol = p_ent->p_vol;
entry_sector = p_ent->entry_sector;
entry_offset = p_ent->entry_offset;
if ((p_ent->num_entry_LFNs != 0) && ((p_ent->small_letter_flag & 0x18) == 0)) {
for (i = 1; i <= p_ent->num_entry_LFNs; i++) {
err = PFENT_ITER_Retreat(p_iter, 0);
if (err != 0) {
return err;
}
entry_sector = p_iter->sector;
entry_offset = p_iter->offset;
err = PFSEC_WriteData(p_vol, (pf_u8*)&dir_fb_free, entry_sector, entry_offset, 1, &success_size, 0);
if (err != 0) {
return err;
}
if (success_size != 1) {
return 17;
}
}
}
err = PFSEC_WriteData(p_vol, (pf_u8*)&dir_fb_free, p_ent->entry_sector, p_ent->entry_offset, 1, &success_size, 0);
if (err != 0) {
return err;
}
if (success_size != 1) {
return 17;
}
err = PFCACHE_FlushDataCacheSpecific(p_vol, PF_NULL);
if (err != 0) {
return err;
}
return 0;
}

View File

@@ -1,248 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFCACHE_ReadFATPage PFCACHE_ReadFATPage
#define VFiPFCACHE_UpdateModifiedSector PFCACHE_UpdateModifiedSector
#define VFiPFFAT12_ReadFATEntry PFFAT12_ReadFATEntry
#define VFiPFFAT12_ReadFATEntryPage PFFAT12_ReadFATEntryPage
#define VFiPFFAT12_WriteFATEntry PFFAT12_WriteFATEntry
#define VFiPFFAT12_WriteFATEntryPage PFFAT12_WriteFATEntryPage
#define VFiPFFAT_UpdateFATEntry PFFAT_UpdateFATEntry
#define VFiPFSEC_ReadFAT PFSEC_ReadFAT
#define VFiPFSEC_WriteFAT PFSEC_WriteFAT
#include <private/fa/pf_volume.h>
#include <private/vf/PrFILE2/fatfs/pf_fat.h>
#include <private/vf/PrFILE2/fatfs/pf_fat12.h>
#include <private/vf/PrFILE2/fatfs/pf_sector.h>
#include <private/vf/PrFILE2/fatfs/pf_volume.h>
#include <private/vf/PrFILE2/common/pf_code.h>
pf_s32 VFiPFFAT12_ReadFATEntry(PF_VOLUME* p_vol, pf_u16 cluster, pf_u32* p_value) {
pf_u16 fat_sector;
pf_s32 err;
pf_u16 fat_offset;
pf_u16 offset_in_sector;
pf_u16 word;
pf_u8 buf[sizeof(pf_u16)];
pf_u32 current_fat;
pf_s32 result;
fat_offset = cluster + (cluster >> 1);
fat_sector = p_vol->bpb.active_FAT_sector + (fat_offset >> p_vol->bpb.log2_bytes_per_sector);
offset_in_sector = fat_offset & (p_vol->bpb.bytes_per_sector - 1);
if ((p_vol->bpb.ext_flags & 0x80) != 0) {
current_fat = p_vol->bpb.ext_flags & (0x01 | 0x02 | 0x04);
} else {
current_fat = 1;
}
do {
if (offset_in_sector < (p_vol->bpb.bytes_per_sector - 1)) {
err = VFiPFSEC_ReadFAT(p_vol, &buf[0], (pf_u32)fat_sector, offset_in_sector, 2);
} else {
err = VFiPFSEC_ReadFAT(p_vol, &buf[0], (pf_u32)fat_sector, offset_in_sector, 1);
if (err == 0) {
err = VFiPFSEC_ReadFAT(p_vol, &buf[1], fat_sector + 1, 0U, 1);
}
}
if (err == 0x1000 && p_vol->p_callback != PF_NULL) {
result = ((PF_VOLUME_CB)p_vol->p_callback)(p_vol->last_driver_error);
if (result == 0) {
continue;
}
if (result == 1 && p_vol->bpb.num_active_FATs >= 2 && current_fat < p_vol->bpb.num_active_FATs) {
current_fat++;
fat_sector += (pf_u16)p_vol->bpb.sectors_per_FAT;
continue;
}
}
if (err != 0) {
*p_value = -1;
return err;
}
} while (err != 0);
word = PF_SWAP_16(*(pf_u16*)buf);
if ((cluster & 0x01) != 0) {
*p_value = (word >> 4);
} else {
*p_value = word & 0xFFF;
}
return 0;
}
pf_s32 VFiPFFAT12_WriteFATEntry(PF_VOLUME* p_vol, pf_u16 cluster, pf_u16 value) {
pf_s32 err;
pf_u16 fat_offset;
pf_u16 fat_sector;
pf_u16 offset_in_sector;
pf_u16 fat_sector2;
pf_u16 offset_in_sector2;
pf_u16 word;
pf_u16 current_fat;
pf_s32 result;
pf_u8 buf[sizeof(pf_u16)];
fat_offset = cluster + (cluster >> 1);
fat_sector = p_vol->bpb.active_FAT_sector + (fat_offset >> p_vol->bpb.log2_bytes_per_sector);
offset_in_sector = fat_offset & (p_vol->bpb.bytes_per_sector - 1);
if ((cluster & 0x01) != 0) {
err = VFiPFSEC_ReadFAT(p_vol, &buf[0], fat_sector, offset_in_sector, 1);
if (err != 0) {
return err;
}
word = (pf_u16)((value << 4) + (buf[0] & 0xF));
} else {
fat_sector2 = p_vol->bpb.active_FAT_sector + ((fat_offset + 1) >> p_vol->bpb.log2_bytes_per_sector);
offset_in_sector2 = (fat_offset + 1) & (p_vol->bpb.bytes_per_sector - 1);
err = VFiPFSEC_ReadFAT(p_vol, &buf[0], fat_sector2, offset_in_sector2, 1);
if (err != 0) {
return err;
}
word = (pf_u16)buf[0] << 8;
word = (word & 0xF000) + (pf_u16)(value & 0xFFF);
}
*(pf_u16*)buf = PF_SWAP_16(word);
result = 0;
for (current_fat = 0; current_fat < p_vol->bpb.num_active_FATs; current_fat++) {
if (offset_in_sector < (p_vol->bpb.bytes_per_sector - 1)) {
err = VFiPFSEC_WriteFAT(p_vol, &buf[0], fat_sector, offset_in_sector, 2);
if (err != 0 && result == 0) { result = err; }
} else {
err = VFiPFSEC_WriteFAT(p_vol, &buf[0], fat_sector, offset_in_sector, 1U);
if (err != 0 && result == 0) { result = err; }
err = VFiPFSEC_WriteFAT(p_vol, &buf[1], fat_sector + 1, 0U, 1);
if (err != 0 && result == 0) { result = err; }
}
fat_sector += p_vol->bpb.sectors_per_FAT;
}
return result;
}
pf_s32 PFFAT12_WriteFATEntryWithBuf(PF_VOLUME* p_vol, pf_s16 cluster, pf_u16 value, PF_CACHE_PAGE* p_page) {
pf_s32 err;
pf_u32 offset_in_sector;
pf_u16 current_fat;
pf_s32 fat_offset;
pf_u32 fat_sector;
pf_s32 result;
fat_offset = (pf_u16)(cluster + (cluster >> 1));
fat_sector = (pf_u16)(p_vol->bpb.active_FAT_sector + (fat_offset >> p_vol->bpb.log2_bytes_per_sector));
offset_in_sector = fat_offset & (p_vol->bpb.bytes_per_sector - 1);
err = 0;
result = 0;
#define FLUSH_FAT_BUFFER() \
for (current_fat = 0; current_fat < p_vol->bpb.num_active_FATs; current_fat++) { \
err = VFiPFSEC_WriteFAT(p_vol, p_page->p_buf, p_page->sector + current_fat * p_vol->bpb.sectors_per_FAT, 0, p_vol->bpb.bytes_per_sector); \
if (err != 0 && result == 0) { result = err; } \
}
if (p_page->sector != fat_sector) {
FLUSH_FAT_BUFFER();
err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, fat_sector, 0, p_vol->bpb.bytes_per_sector);
if (err != 0) { return err; }
p_page->sector = fat_sector;
}
if (cluster & 1) {
p_page->p_buf[(pf_u16)offset_in_sector] = (p_page->p_buf[(pf_u16)offset_in_sector] & 0xF) | ((value << 4) & 0xF0);
if ((pf_u16)offset_in_sector == (pf_u32)(p_vol->bpb.bytes_per_sector - 1)) {
FLUSH_FAT_BUFFER();
offset_in_sector = fat_sector + 1;
err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, offset_in_sector, 0, p_vol->bpb.bytes_per_sector);
if (err != 0) { return err; }
p_page->sector = offset_in_sector;
p_page->p_buf[0] = value >> 4;
} else {
((pf_u16)offset_in_sector + p_page->p_buf)[1] = value >> 4;
}
} else {
p_page->p_buf[(pf_u16)offset_in_sector] = value;
if ((pf_u16)offset_in_sector == (pf_u32)(p_vol->bpb.bytes_per_sector - 1)) {
FLUSH_FAT_BUFFER();
offset_in_sector = fat_sector + 1;
err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, offset_in_sector, 0, p_vol->bpb.bytes_per_sector);
if (err != 0) { return err; }
p_page->sector = offset_in_sector;
p_page->p_buf[0] = (p_page->p_buf[0] & 0xF0) | ((pf_u8)(value >> 8));
} else {
((pf_u16)offset_in_sector + p_page->p_buf)[1] = (((pf_u16)offset_in_sector + p_page->p_buf)[1] & 0xF0) | ((pf_u8)(value >> 8));
}
}
if (err != 0) { result = err; }
return result;
#undef FLUSH_FAT_BUFFER
}
pf_s32 PFFAT12_ReadFATEntryWithBuf(PF_VOLUME* p_vol , pf_u16 cluster , pf_u32* p_value ,
PF_CACHE_PAGE* p_page ) {
pf_s32 err;
pf_u32 current_fat;
pf_u32 sector;
pf_u32 offset;
pf_s32 result;
#define LOAD_FAT_SECTOR(next_sector) \
if (p_page->sector != sector + (next_sector)) { \
if (p_page->option == 1) { \
for (err = 0; (pf_u16)err < p_vol->bpb.num_active_FATs; err++) { \
VFiPFSEC_WriteFAT(p_vol, p_page->p_buf, p_page->sector + (pf_u16)err * p_vol->bpb.sectors_per_FAT, 0, p_vol->bpb.bytes_per_sector); \
} \
} \
err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, sector + (next_sector), 0, p_vol->bpb.bytes_per_sector); \
if (err != 0) { continue; } \
p_page->sector = sector + (next_sector); \
}
offset = (pf_u16)(cluster + (cluster >> 1));
sector = p_vol->bpb.active_FAT_sector + (offset >> p_vol->bpb.log2_bytes_per_sector) & 0xFFFF;
if ((p_vol->bpb.ext_flags & 0x80) != 0) {
current_fat = p_vol->bpb.ext_flags & (0x01 | 0x02 | 0x04);
} else {
current_fat = 1;
}
err = 0;
while (PF_TRUE) {
if (err == 0x1000 && p_vol->p_callback != PF_NULL) {
result = ((PF_VOLUME_CB)p_vol->p_callback)(p_vol->last_driver_error);
if (result == 0) {
err = 0;
continue;
}
if (result == 1 && p_vol->bpb.num_active_FATs >= 2 && current_fat < p_vol->bpb.num_active_FATs) {
current_fat++;
sector += p_vol->bpb.sectors_per_FAT;
err = 0;
continue;
}
} else {
LOAD_FAT_SECTOR(0);
offset &= p_vol->bpb.bytes_per_sector - 1;
if (offset == (p_vol->bpb.bytes_per_sector - 1)) {
*p_value = *(p_page->p_buf + offset);
if (p_page->option == 1) {
for (err = 0; (pf_u16)err < p_vol->bpb.num_active_FATs; err++) {
VFiPFSEC_WriteFAT(p_vol, p_page->p_buf, p_page->sector + (pf_u16)err * p_vol->bpb.sectors_per_FAT, 0, p_vol->bpb.bytes_per_sector);
}
}
err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, sector + 1, 0, p_vol->bpb.bytes_per_sector);
if (err != 0) { continue; }
p_page->sector = sector + 1;
*p_value += (pf_u16)(*p_page->p_buf) << 8;
} else {
*p_value = ((pf_u16)p_page->p_buf[offset + 1] << 8) + *(p_page->p_buf + offset);
}
}
break;
}
if (err != 0) {
*p_value = -1;
return err;
}
if ((cluster & 0x01) != 0) {
*p_value = *p_value >> 4;
} else {
*p_value &= 0xFFF;
}
return 0;
#undef LOAD_FAT_SECTOR
}

View File

@@ -1,158 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFCACHE_ReadFATPage PFCACHE_ReadFATPage
#define VFiPFCACHE_UpdateModifiedSector PFCACHE_UpdateModifiedSector
#define VFiPFFAT16_ReadFATEntry PFFAT16_ReadFATEntry
#define VFiPFFAT16_ReadFATEntryPage PFFAT16_ReadFATEntryPage
#define VFiPFFAT16_WriteFATEntry PFFAT16_WriteFATEntry
#define VFiPFFAT16_WriteFATEntryPage PFFAT16_WriteFATEntryPage
#define VFiPFFAT_UpdateFATEntry PFFAT_UpdateFATEntry
#define VFiPFSEC_ReadFAT PFSEC_ReadFAT
#define VFiPFSEC_WriteFAT PFSEC_WriteFAT
#include <private/fa/pf_volume.h>
#include <private/vf/PrFILE2/fatfs/pf_fat.h>
#include <private/vf/PrFILE2/fatfs/pf_fat16.h>
#include <private/vf/PrFILE2/fatfs/pf_sector.h>
#include <private/vf/PrFILE2/fatfs/pf_volume.h>
pf_s32 VFiPFFAT16_ReadFATEntry(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32* p_value) {
pf_u32 fat_offset;
pf_u32 fat_sector;
pf_u16 offset_in_sector;
pf_u8 buf[sizeof(pf_u16)];
pf_s32 err;
pf_u32 current_fat;
pf_s32 result;
fat_offset = cluster * sizeof(pf_u16);
fat_sector = (pf_u16)(p_vol->bpb.active_FAT_sector + ((cluster * sizeof(pf_u16)) >> p_vol->bpb.log2_bytes_per_sector));
offset_in_sector = fat_offset & (p_vol->bpb.bytes_per_sector - 1);
if ((p_vol->bpb.ext_flags & 0x80) != 0) {
current_fat = p_vol->bpb.ext_flags & (0x01 | 0x02 | 0x04);
} else {
current_fat = 1;
}
do {
err = VFiPFSEC_ReadFAT(p_vol, buf, fat_sector, offset_in_sector, 2);
if (err == 0x1000 && p_vol->p_callback != PF_NULL) {
result = ((PF_VOLUME_CB)p_vol->p_callback)(p_vol->last_driver_error);
if (result == 0) {
continue;
}
if (result == 1 && p_vol->bpb.num_active_FATs >= 2 && current_fat < p_vol->bpb.num_active_FATs) {
current_fat++;
fat_sector += p_vol->bpb.sectors_per_FAT;
continue;
}
}
if (err != 0) {
*p_value = -1;
return err;
}
} while (err != 0);
*p_value = PF_SWAP_16(*(pf_u16*)buf);
return 0;
}
pf_s32 VFiPFFAT16_WriteFATEntry(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32 value) {
pf_s32 err;
pf_u32 fat_offset;
pf_u16 fat_sector;
pf_u16 offset_in_sector;
pf_u32 current_fat;
pf_s32 result;
pf_u8 buf[sizeof(pf_u16)];
fat_offset = cluster * sizeof(pf_u16);
fat_sector = p_vol->bpb.active_FAT_sector + ((cluster * sizeof(pf_u16)) >> p_vol->bpb.log2_bytes_per_sector);
offset_in_sector = fat_offset & (p_vol->bpb.bytes_per_sector - 1);
*(pf_u16*)buf = PF_SWAP_16(value);
result = 0;
for (current_fat = 0; current_fat < p_vol->bpb.num_active_FATs; current_fat++) {
err = VFiPFSEC_WriteFAT(p_vol, buf, fat_sector, offset_in_sector, 2);
if (err != 0 && result == 0) {
result = err;
}
fat_sector += p_vol->bpb.sectors_per_FAT;
}
return result;
}
pf_s32 PFFAT16_WriteFATEntryWithBuf(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32 value, PF_CACHE_PAGE* p_page) {
pf_u16 offset_in_sector;
pf_s32 err;
pf_u32 fat_offset;
pf_u16 fat_sector;
pf_u32 current_fat;
pf_s32 result;
fat_offset = cluster * sizeof(pf_u16);
fat_sector = (pf_u16)(p_vol->bpb.active_FAT_sector + (fat_offset >> p_vol->bpb.log2_bytes_per_sector));
offset_in_sector = (pf_u16)(fat_offset & (p_vol->bpb.bytes_per_sector - 1));
err = 0;
result = 0;
if (p_page->sector != fat_sector) {
for (current_fat = 0; current_fat < p_vol->bpb.num_active_FATs; current_fat++) {
err = VFiPFSEC_WriteFAT(p_vol, p_page->p_buf, p_page->sector + current_fat * p_vol->bpb.sectors_per_FAT, 0, p_vol->bpb.bytes_per_sector);
if (err != 0 && result == 0) {
result = err;
}
}
p_page->sector = fat_sector;
err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, fat_sector, 0, p_vol->bpb.bytes_per_sector);
}
p_page->p_buf[offset_in_sector] = value;
(offset_in_sector + p_page->p_buf)[1] = value >> 8;
if (err != 0) {
result = err;
}
return result;
}
pf_s32 PFFAT16_ReadFATEntryWithBuf(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32* p_value, PF_CACHE_PAGE* p_page) {
pf_u32 sector;
pf_s32 err;
pf_u32 offset;
pf_u32 current_fat;
pf_s32 result;
offset = cluster * sizeof(pf_u16);
sector = (pf_u16)(p_vol->bpb.active_FAT_sector + (offset >> p_vol->bpb.log2_bytes_per_sector));
current_fat = 1;
if ((p_vol->bpb.ext_flags & 0x80) != 0) {
current_fat = p_vol->bpb.ext_flags & 7;
}
if (p_page->sector != sector) {
if (p_page->option == 1) {
pf_u16 fat;
for (fat = 0; fat < p_vol->bpb.num_active_FATs; fat++) {
VFiPFSEC_WriteFAT(p_vol, p_page->p_buf, p_page->sector + fat * p_vol->bpb.sectors_per_FAT, 0, p_vol->bpb.bytes_per_sector);
}
}
do {
err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, sector, 0, p_vol->bpb.bytes_per_sector);
if (err != 0x1000 || p_vol->p_callback == PF_NULL) {
goto block_22;
}
result = ((PF_VOLUME_CB)p_vol->p_callback)(p_vol->last_driver_error);
if (result != 0) {
if (result == 1 && p_vol->bpb.num_active_FATs >= 2 && current_fat < p_vol->bpb.num_active_FATs) {
current_fat++;
sector += p_vol->bpb.sectors_per_FAT;
} else {
goto block_22;
}
}
continue;
block_22:
if (err != 0) {
return err;
}
p_page->sector = sector;
} while (err != 0);
}
offset &= p_vol->bpb.bytes_per_sector - 1;
*p_value = PF_SWAP_16(*((pf_u16*)(p_page->p_buf + offset)));
return 0;
}

View File

@@ -1,168 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFCACHE_ReadFATPage PFCACHE_ReadFATPage
#define VFiPFCACHE_UpdateModifiedSector PFCACHE_UpdateModifiedSector
#define VFiPFFAT32_ReadFATEntry PFFAT32_ReadFATEntry
#define VFiPFFAT32_ReadFATEntryPage PFFAT32_ReadFATEntryPage
#define VFiPFFAT32_WriteFATEntry PFFAT32_WriteFATEntry
#define VFiPFFAT32_WriteFATEntryPage PFFAT32_WriteFATEntryPage
#define VFiPFFAT_UpdateFATEntry PFFAT_UpdateFATEntry
#define VFiPFSEC_ReadFAT PFSEC_ReadFAT
#define VFiPFSEC_WriteFAT PFSEC_WriteFAT
#include <private/fa/pf_volume.h>
#include <private/vf/PrFILE2/fatfs/pf_fat.h>
#include <private/vf/PrFILE2/fatfs/pf_fat32.h>
#include <private/vf/PrFILE2/fatfs/pf_sector.h>
#include <private/vf/PrFILE2/fatfs/pf_volume.h>
pf_s32 VFiPFFAT32_ReadFATEntry(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32* p_value) {
pf_u32 fat_offset;
pf_u32 fat_sector;
pf_u16 offset_in_sector;
pf_u8 buf[sizeof(pf_u32)];
pf_s32 err;
pf_u32 current_fat;
pf_s32 result;
fat_offset = cluster * sizeof(pf_u32);
fat_sector = (pf_u16)(p_vol->bpb.active_FAT_sector + (fat_offset >> p_vol->bpb.log2_bytes_per_sector));
offset_in_sector = fat_offset & (p_vol->bpb.bytes_per_sector - 1);
if ((p_vol->bpb.ext_flags & 0x80) != 0) {
current_fat = p_vol->bpb.ext_flags & (0x01 | 0x02 | 0x04);
} else {
current_fat = 1;
}
do {
err = VFiPFSEC_ReadFAT(p_vol, buf, fat_sector, offset_in_sector, 4);
if (err == 0x1000 && p_vol->p_callback != PF_NULL) {
result = ((PF_VOLUME_CB)p_vol->p_callback)(p_vol->last_driver_error);
if (result == 0) {
continue;
}
if (result == 1 && p_vol->bpb.num_active_FATs >= 2 && current_fat < p_vol->bpb.num_active_FATs) {
current_fat++;
fat_sector += p_vol->bpb.sectors_per_FAT;
continue;
}
}
if (err != 0) {
*p_value = -1;
return err;
}
} while (err != 0);
*p_value = PF_SWAP_32(*(pf_u32*)buf) & 0x0FFFFFFF;
return 0;
}
pf_s32 VFiPFFAT32_WriteFATEntry(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32 value) {
pf_s32 err;
pf_u32 fat_offset;
pf_u32 fat_sector;
pf_u16 offset_in_sector;
pf_u16 current_fat;
pf_s32 result;
pf_u8 buf[sizeof(pf_u32)];
pf_u32 dword;
fat_offset = cluster * sizeof(pf_u32);
fat_sector = (pf_u16)(p_vol->bpb.active_FAT_sector + (fat_offset >> p_vol->bpb.log2_bytes_per_sector));
offset_in_sector = fat_offset & (p_vol->bpb.bytes_per_sector - 1);
err = VFiPFSEC_ReadFAT(p_vol, buf, fat_sector, offset_in_sector, 4);
if (err != 0) {
return err;
}
dword = (value & 0x0FFFFFFF) | (PF_SWAP_32(*(pf_u32*)buf) & 0xF0000000);
*(pf_u32*)buf = PF_SWAP_32(dword);
result = 0;
for (current_fat = 0; current_fat < p_vol->bpb.num_active_FATs; current_fat++) {
err = VFiPFSEC_WriteFAT(p_vol, buf, fat_sector, offset_in_sector, 4);
if (err != 0 && result == 0) {
result = err;
}
fat_sector += p_vol->bpb.sectors_per_FAT;
}
return result;
}
pf_s32 PFFAT32_WriteFATEntryWithBuf(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32 value, PF_CACHE_PAGE* p_page) {
pf_u32 merged_value;
pf_u16 offset_in_sector;
pf_s32 err;
pf_u32 fat_offset;
pf_u32 fat_sector;
pf_u16 current_fat;
pf_s32 result;
fat_offset = cluster * sizeof(pf_u32);
fat_sector = (pf_u16)(p_vol->bpb.active_FAT_sector + (fat_offset >> p_vol->bpb.log2_bytes_per_sector));
offset_in_sector = (pf_u16)(fat_offset & (p_vol->bpb.bytes_per_sector - 1));
err = 0;
result = 0;
if (p_page->sector != fat_sector) {
for (current_fat = 0; current_fat < p_vol->bpb.num_active_FATs; current_fat++) {
err = VFiPFSEC_WriteFAT(p_vol, p_page->p_buf, p_page->sector + current_fat * p_vol->bpb.sectors_per_FAT, 0, p_vol->bpb.bytes_per_sector);
if (err != 0 && result == 0) {
result = err;
}
}
err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, fat_sector, 0, p_vol->bpb.bytes_per_sector);
if (err != 0) {
return err;
}
p_page->sector = fat_sector;
}
merged_value = (value & 0x0FFFFFFF) | (PF_SWAP_32(*(pf_u32*)&p_page->p_buf[offset_in_sector]) & 0xF0000000);
*(pf_u32*)&p_page->p_buf[offset_in_sector] = PF_SWAP_32(merged_value);
if (err != 0) {
result = err;
}
return result;
}
pf_s32 PFFAT32_ReadFATEntryWithBuf(PF_VOLUME* p_vol, pf_u32 cluster, pf_u32* p_value, PF_CACHE_PAGE* p_page) {
pf_u32 sector;
pf_s32 err;
pf_u32 offset;
pf_u32 current_fat;
pf_s32 result;
offset = cluster * sizeof(pf_u32);
sector = (pf_u16)(p_vol->bpb.active_FAT_sector + (offset >> p_vol->bpb.log2_bytes_per_sector));
current_fat = 1;
if ((p_vol->bpb.ext_flags & 0x80) != 0) {
current_fat = p_vol->bpb.ext_flags & 7;
}
if (p_page->sector != sector) {
if (p_page->option == 1) {
pf_u16 fat;
for (fat = 0; fat < p_vol->bpb.num_active_FATs; fat++) {
VFiPFSEC_WriteFAT(p_vol, p_page->p_buf, p_page->sector + fat * p_vol->bpb.sectors_per_FAT, 0, p_vol->bpb.bytes_per_sector);
}
}
do {
err = VFiPFSEC_ReadFAT(p_vol, p_page->p_buf, sector, 0, p_vol->bpb.bytes_per_sector);
if (err != 0x1000 || p_vol->p_callback == PF_NULL) {
goto block_22;
}
result = ((PF_VOLUME_CB)p_vol->p_callback)(p_vol->last_driver_error);
if (result != 0) {
if (result == 1 && p_vol->bpb.num_active_FATs >= 2 && current_fat < p_vol->bpb.num_active_FATs) {
current_fat++;
sector += p_vol->bpb.sectors_per_FAT;
} else {
goto block_22;
}
}
continue;
block_22:
if (err != 0) {
return err;
}
p_page->sector = sector;
} while (err != 0);
}
offset &= p_vol->bpb.bytes_per_sector - 1;
*p_value = PF_SWAP_32(*((pf_u32*)(p_page->p_buf + offset))) & 0x0FFFFFFF;
return 0;
}

View File

@@ -3,13 +3,6 @@
#include <private/vf/PrFILE2/fatfs/pf_path.h>
#include <private/vf/PrFILE2/pf_types.h>
typedef struct PFDIR_STR {
const pf_s8* p_head;
const pf_s8* p_tail;
const pf_s8* p_current;
pf_u32 code_mode;
} PFDIR_STR;
typedef struct PFFILE_FAT_HINT {
pf_u32 chain_index;
pf_u32 cluster;
@@ -151,27 +144,27 @@ extern pf_s32 PFCLUSTER_AdjustCluster(PFFILE_FILE* file);
extern pf_s32 PFCLUSTER_GetAppendSize(PFFILE_FILE* file, pf_u32* append_size);
extern pf_s32 PFFAT_InitFFD(PFFILE_FFD* ffd, void* hint, PF_VOLUME* volume, pf_u32* start_cluster);
extern void* pf_memset(void* destination, pf_s32 value, pf_u32 size);
extern pf_u16 PFSTR_StrNumChar(PFDIR_STR* path_string, pf_u32 mode);
extern pf_u32 PFSTR_GetCodeMode(PFDIR_STR* path_string);
extern void* PFSTR_GetStrPos(PFDIR_STR* path_string, pf_u32 mode);
extern pf_s32 PFPATH_parseShortName(pf_s8* short_name, PFDIR_STR* path_string);
extern pf_u32 PFSTR_StrNumChar(PF_STR* path_string, pf_u32 mode);
extern pf_u32 PFSTR_GetCodeMode(PF_STR* path_string);
extern void* PFSTR_GetStrPos(PF_STR* path_string, pf_u32 mode);
extern pf_s32 PFPATH_parseShortName(pf_s8* short_name, PF_STR* path_string);
extern pf_s32 PFENT_AdjustSFN(PF_DIR_ENT* entry, pf_s8* short_name);
extern void PFPATH_transformInUnicode(PF_DIR_ENT* entry, void* unicode_text);
extern pf_u16* pf_w_strcpy(pf_u16* destination, const pf_u16* source);
extern pf_u8 PFENT_getcurrentDateTimeForEnt(pf_u16* date, pf_u16* time);
extern pf_s32 PFENT_allocateEntry(PF_DIR_ENT* entry, pf_u32 count, PFFILE_FFD* ffd,
pf_u32* previous_sectors, PFDIR_STR* filename);
pf_u32* previous_sectors, PF_STR* filename);
extern pf_s32 PFENT_allocateEntryPos(PF_DIR_ENT* entry, pf_u32 count, PFFILE_FFD* ffd,
pf_u32* previous_sectors, PFDIR_STR* filename,
pf_u32* previous_sectors, PF_STR* filename,
pf_u32* position);
extern void PFPATH_AdjustExtShortName(pf_s8* short_name, pf_u32 position);
extern pf_u8 PFENT_CalcCheckSum(PF_DIR_ENT* entry);
extern void PFENT_storeLFNEntryFieldsToBuf(pf_u8* buffer, PF_DIR_ENT* entry, pf_u8 ordinal,
pf_u8 checksum, pf_bool is_last);
extern pf_s32 PFENT_updateEntry(PF_DIR_ENT* entry, pf_u32 update_archive);
extern pf_s32 PFPATH_SplitPath(PFDIR_STR* path, PFDIR_STR* directory, PFDIR_STR* filename);
extern pf_s32 PFPATH_SplitPath(PF_STR* path, PF_STR* directory, PF_STR* filename);
extern pf_s32 PFENT_ITER_GetEntryOfPath(PFFILE_ENT_ITER* iter, PF_DIR_ENT* entry,
PF_VOLUME* volume, PFDIR_STR* path, pf_u32 no_look_last);
PF_VOLUME* volume, PF_STR* path, pf_u32 no_look_last);
extern pf_s32 PFENT_RemoveEntry(PF_DIR_ENT* entry, PFFILE_ENT_ITER* iter);
extern pf_s32 PFFAT_FreeChain(PFFILE_FFD* ffd, pf_u32 start_cluster, pf_u32 chain_index,
pf_u32 size);
@@ -179,7 +172,7 @@ extern pf_s32 PFCACHE_AllocateDataPage(PF_VOLUME* volume, pf_u32 sector,
PF_CACHE_PAGE** page, pf_u32* option);
extern void PFCACHE_FreeDataPage(PF_VOLUME* volume, PF_CACHE_PAGE* page);
extern pf_s32 PFENT_findEntry(PFFILE_FFD* ffd, PF_DIR_ENT* entry, pf_u32 start,
PFDIR_STR* pattern, pf_u8 attr_required, pf_u32 flags);
PF_STR* pattern, pf_u8 attr_required, pf_u32 flags);
extern pf_s32 PFCLUSTER_CombineFiles(PFFILE_ENT_ITER* first_iter,
PFFILE_ENT_ITER* second_iter_copy,
PF_DIR_ENT* first_entry, PF_DIR_ENT* second_entry);
@@ -196,9 +189,9 @@ extern pf_s32 PFCACHE_FlushDataCacheSpecific(PF_VOLUME* volume, void* signature)
extern pf_s32 PFCACHE_FlushFATCache(PF_VOLUME* volume);
extern void PFFAT_InitHint(void* hint);
extern void PFFAT_FinalizeFFD(PFFILE_FFD* ffd);
extern void PFSTR_SetLocalStr(PFDIR_STR* path_string, const pf_s8* converted_path);
extern void PFSTR_SetLocalStr(PF_STR* path_string, const pf_s8* converted_path);
extern pf_s32 PFPATH_transformFromUnicodeToNormal(pf_s8* destination, const pf_u16* source);
extern PF_VOLUME* PFPATH_GetVolumeFromPath(PFDIR_STR* path_string);
extern PF_VOLUME* PFPATH_GetVolumeFromPath(PF_STR* path_string);
extern pf_s32 PFVOL_CheckForWrite(PF_VOLUME* volume);
extern pf_s32 PFVOL_CheckForRead(PF_VOLUME* volume);
extern pf_s32 PFDRV_IsWProtected(PF_VOLUME* volume);
@@ -421,29 +414,16 @@ pf_s32 PFFILE_Cursor_Read(PFFILE_FILE* file, pf_u8* buffer, pf_u32 size, pf_u32*
return 0;
}
static pf_bool PFFILE_Cursor_AdvanceToWrite(PFFILE_FILE* file, pf_u32 size, pf_u32 sector) {
pf_bool result = PF_TRUE;
PF_VOLUME* volume = file == PF_NULL ? PF_NULL : file->p_sfd->dir_entry.p_vol;
pf_u32 work_sector = sector + ((file->cursor.offset_in_sector + size) >> volume->bpb.log2_bytes_per_sector);
if (((volume->bpb.bytes_per_sector - 1) & (file->cursor.offset_in_sector + size)) != 0) {
file->cursor.sector = sector + ((file->cursor.offset_in_sector + size) >> volume->bpb.log2_bytes_per_sector);
} else {
file->cursor.sector = -1;
}
file->cursor.position += size;
if (file->cursor.position > file->p_sfd->dir_entry.file_size) {
file->p_sfd->dir_entry.file_size = file->cursor.position;
}
PFFILE_Cursor_Recalc(file);
PFCLUSTER_UpdateLastAccessCluster(file, work_sector);
return result;
}
pf_s32 PFFILE_Cursor_WriteHeadSector(PFFILE_FILE* file, const pf_u8* buffer, pf_u32 size,
pf_u32* size_written) {
pf_s32 error;
pf_u32 success_size;
pf_u32 next_position;
pf_u32 new_position;
pf_u32 current_sector;
pf_u32 written_size;
pf_u32 sector_advance;
pf_u32 next_sector;
PF_VOLUME* volume;
*size_written = 0;
@@ -472,7 +452,24 @@ pf_s32 PFFILE_Cursor_WriteHeadSector(PFFILE_FILE* file, const pf_u8* buffer, pf_
return error;
}
*size_written = success_size;
PFFILE_Cursor_AdvanceToWrite(file, success_size, file->cursor.sector);
current_sector = file->cursor.sector;
written_size = success_size;
volume = file == PF_NULL ? PF_NULL : file->p_sfd->dir_entry.p_vol;
next_position = file->cursor.offset_in_sector + written_size;
sector_advance = next_position >> volume->bpb.log2_bytes_per_sector;
next_sector = current_sector + sector_advance;
if (((volume->bpb.bytes_per_sector - 1) & next_position) != 0) {
file->cursor.sector = next_sector;
} else {
file->cursor.sector = -1;
}
new_position = file->cursor.position + success_size;
file->cursor.position = new_position;
if (new_position > file->p_sfd->dir_entry.file_size) {
file->p_sfd->dir_entry.file_size = new_position;
}
PFFILE_Cursor_Recalc(file);
PFCLUSTER_UpdateLastAccessCluster(file);
if (success_size != size) {
return error;
}
@@ -484,6 +481,7 @@ pf_s32 PFFILE_Cursor_WriteBodySectors(PFFILE_FILE* file, const pf_u8* buffer, pf
pf_s32 error;
pf_u32 num_sectors;
pf_u32 success_size;
pf_u32 next_position;
PF_VOLUME* volume;
*size_written = 0;
@@ -508,7 +506,18 @@ pf_s32 PFFILE_Cursor_WriteBodySectors(PFFILE_FILE* file, const pf_u8* buffer, pf
return error;
}
*size_written = success_size;
PFFILE_Cursor_AdvanceToWrite(file, success_size, file->cursor.sector);
next_position = file->cursor.offset_in_sector + success_size;
if (((volume->bpb.bytes_per_sector - 1) & next_position) != 0) {
file->cursor.sector += next_position >> volume->bpb.log2_bytes_per_sector;
} else {
file->cursor.sector = -1;
}
file->cursor.position += success_size;
if (file->cursor.position > file->p_sfd->dir_entry.file_size) {
file->p_sfd->dir_entry.file_size = file->cursor.position;
}
PFFILE_Cursor_Recalc(file);
PFCLUSTER_UpdateLastAccessCluster(file);
if (success_size != size - (size & (volume->bpb.bytes_per_sector - 1))) {
return error;
}
@@ -519,6 +528,12 @@ pf_s32 PFFILE_Cursor_WriteTailSector(PFFILE_FILE* file, const pf_u8* buffer, pf_
pf_u32* size_written) {
pf_s32 error;
pf_u32 success_size;
pf_u32 next_position;
pf_u32 current_sector;
pf_u32 written_size;
pf_u32 sector_advance;
pf_u32 next_sector;
pf_u32 new_position;
PF_VOLUME* volume;
*size_written = 0;
@@ -548,7 +563,24 @@ pf_s32 PFFILE_Cursor_WriteTailSector(PFFILE_FILE* file, const pf_u8* buffer, pf_
return error;
}
*size_written = success_size;
PFFILE_Cursor_AdvanceToWrite(file, success_size, file->cursor.sector);
current_sector = file->cursor.sector;
written_size = success_size;
volume = file == PF_NULL ? PF_NULL : file->p_sfd->dir_entry.p_vol;
next_position = file->cursor.offset_in_sector + written_size;
sector_advance = next_position >> volume->bpb.log2_bytes_per_sector;
next_sector = current_sector + sector_advance;
if (((volume->bpb.bytes_per_sector - 1) & next_position) != 0) {
file->cursor.sector = next_sector;
} else {
file->cursor.sector = -1;
}
new_position = file->cursor.position + success_size;
file->cursor.position = new_position;
if (new_position > file->p_sfd->dir_entry.file_size) {
file->p_sfd->dir_entry.file_size = new_position;
}
PFFILE_Cursor_Recalc(file);
PFCLUSTER_UpdateLastAccessCluster(file);
if (success_size != size) {
return error;
}
@@ -657,28 +689,23 @@ void PFFILE_Cursor_MoveToClusterEnd(PFFILE_FILE* file, pf_u32 size) {
}
}
PFFILE_SFD* PFFILE_GetSFD(PFFILE_VOLUME_DIRS* volume_dirs, PF_DIR_ENT* entry) {
pf_u32 index;
pf_u32 first_free_index;
PFFILE_SFD* first_free_sfd;
PFFILE_SFD* PFFILE_GetSFD(PF_VOLUME* volume, PF_DIR_ENT* entry) {
PFFILE_VOLUME_DIRS* volume_dirs = (PFFILE_VOLUME_DIRS*)volume;
PFFILE_SFD* first_free_sfd = PF_NULL;
PFFILE_SFD* sfd = volume_dirs->sfds;
pf_u32 first_free_index = 0;
pf_u32 index = 0;
first_free_index = 0;
first_free_sfd = PF_NULL;
for (index = 0; index < 5; index++) {
if ((volume_dirs->sfds[index].stat & 1) == 0 ||
((volume_dirs->sfds[index].stat & 1) != 0 &&
((volume_dirs->sfds[index].stat == 0) & 2) != 0)) {
for (; index < 5; index++, sfd++) {
if ((sfd->stat & 1) == 0 || (sfd->stat & 2) == 0) {
if (first_free_sfd == PF_NULL) {
first_free_sfd = &volume_dirs->sfds[index];
first_free_sfd = sfd;
first_free_index = index;
}
continue;
}
if (entry->p_vol == volume_dirs->sfds[index].dir_entry.p_vol &&
entry->entry_sector == volume_dirs->sfds[index].dir_entry.entry_sector &&
entry->entry_offset == volume_dirs->sfds[index].dir_entry.entry_offset) {
return &volume_dirs->sfds[index];
} else if (entry->p_vol == sfd->dir_entry.p_vol &&
entry->entry_sector == sfd->dir_entry.entry_sector &&
entry->entry_offset == sfd->dir_entry.entry_offset) {
return sfd;
}
}
@@ -714,27 +741,23 @@ PFFILE_SFD* PFFILE_GetSFD(PFFILE_VOLUME_DIRS* volume_dirs, PF_DIR_ENT* entry) {
return first_free_sfd;
}
pf_s32 PFFILE_createEmptyFile(PF_VOLUME* volume, PF_DIR_ENT* entry, PFDIR_STR* filename) {
pf_s32 PFFILE_createEmptyFile(PF_VOLUME* volume, PF_DIR_ENT* entry, PF_STR* filename) {
PF_DIR_ENT parent_entry = *entry;
PFFILE_FFD ffd;
PFFILE_FAT_HINT hint;
PF_FAT_HINT hint;
pf_u32 previous_sectors[2];
pf_u32* previous_sector_ptr;
pf_u32 success_size;
pf_s32 num_chars;
pf_u8 num_entries;
pf_u32 entry_position;
pf_u32 num_chars;
pf_u32 num_entries;
pf_u32 ordinal;
pf_u32 sector;
pf_u32 entry_position;
pf_u8 checksum;
pf_u32 success_size;
pf_s32 error;
pf_u8 short_entry_buffer[13];
pf_u8 long_entry_buffer[32];
num_chars = PFSTR_StrNumChar(filename, 1);
if (num_chars + parent_entry.path_len > 0x103) {
return 1;
}
if (num_chars > 0xFF) {
num_chars = PFSTR_StrNumChar(filename, 1) & 0xFFFF;
if (num_chars + parent_entry.path_len >= 0x104 || num_chars >= 0x100) {
return 1;
}
PFFAT_InitFFD(&ffd, &hint, parent_entry.p_vol, &parent_entry.start_cluster);
@@ -745,7 +768,9 @@ pf_s32 PFFILE_createEmptyFile(PF_VOLUME* volume, PF_DIR_ENT* entry, PFDIR_STR* f
if (error != 0 && entry->short_name[0] == 0) {
return 1;
}
if (error != 0) {
if (error == 0) {
entry->long_name[0] = 0;
} else {
error = PFENT_AdjustSFN(&parent_entry, entry->short_name);
if (error != 0) {
return error;
@@ -755,14 +780,8 @@ pf_s32 PFFILE_createEmptyFile(PF_VOLUME* volume, PF_DIR_ENT* entry, PFDIR_STR* f
} else {
pf_w_strcpy(entry->long_name, (const pf_u16*)PFSTR_GetStrPos(filename, 1));
}
} else {
entry->long_name[0] = 0;
}
if (volume->bpb.fat_type == FAT_32) {
entry->start_cluster = 1;
} else {
entry->start_cluster = 0;
}
entry->start_cluster = volume->bpb.fat_type == FAT_32 ? 1 : 0;
entry->file_size = 0;
entry->p_vol = volume;
entry->small_letter_flag = 0;
@@ -779,16 +798,14 @@ pf_s32 PFFILE_createEmptyFile(PF_VOLUME* volume, PF_DIR_ENT* entry, PFDIR_STR* f
if (error != 0) {
return error;
}
if ((pf_vol_set.file_settings & 2) == 2) {
if ((pf_vol_set.file_settings & 2) != 0) {
PFPATH_AdjustExtShortName(entry->short_name, entry_position);
}
entry->num_entry_LFNs = num_entries;
sector = entry->entry_sector;
checksum = PFENT_CalcCheckSum(entry);
previous_sector_ptr = previous_sectors;
for (ordinal = num_entries; ordinal >= 1; ordinal--) {
for (ordinal = num_entries; ordinal != 0; ordinal--) {
PFENT_storeLFNEntryFieldsToBuf(long_entry_buffer, entry, ordinal,
checksum, ordinal == num_entries);
PFENT_CalcCheckSum(entry), ordinal == num_entries);
error = PFSEC_WriteData(volume, long_entry_buffer, sector, entry->entry_offset,
0x20, &success_size, 0);
if (error != 0) {
@@ -800,8 +817,8 @@ pf_s32 PFFILE_createEmptyFile(PF_VOLUME* volume, PF_DIR_ENT* entry, PFDIR_STR* f
entry->entry_offset += 0x20;
if (entry->entry_offset >= volume->bpb.bytes_per_sector) {
entry->entry_offset = 0;
sector = *previous_sector_ptr;
previous_sector_ptr++;
sector = previous_sectors[0];
previous_sectors[0] = previous_sectors[1];
}
}
entry->entry_sector = sector;
@@ -817,30 +834,14 @@ pf_s32 PFFILE_createEmptyFile(PF_VOLUME* volume, PF_DIR_ENT* entry, PFDIR_STR* f
return PFENT_updateEntry(entry, 1);
}
static pf_bool PFFILE_IsEntryOpened(PFFILE_VOLUME_DIRS* volume_dirs, PF_DIR_ENT* entry) {
PFFILE_SFD* sfd;
pf_u32 index;
for (index = 0, sfd = volume_dirs->sfds; index < 5; index++, sfd++) {
if ((sfd->stat & 1) != 0 && (sfd->stat & 2) != 0 &&
entry->p_vol == sfd->dir_entry.p_vol &&
entry->entry_sector == sfd->dir_entry.entry_sector &&
entry->entry_offset == sfd->dir_entry.entry_offset) {
return PF_TRUE;
}
}
return PF_FALSE;
}
pf_s32 PFFILE_p_remove(PF_VOLUME* volume, PFDIR_STR* path) {
PFDIR_STR directory;
PFDIR_STR filename;
pf_s32 PFFILE_p_remove(PF_VOLUME* volume, PF_STR* path) {
PF_STR directory;
PF_STR filename;
PF_DIR_ENT entry;
PFFILE_ENT_ITER iter;
PFFILE_FAT_HINT hint;
PFFILE_VOLUME_DIRS* volume_dirs;
pf_u32 start_cluster;
PFFILE_VOLUME_DIRS* volume_dirs = (PFFILE_VOLUME_DIRS*)volume;
pf_s32 error;
pf_u32 index;
error = PFPATH_SplitPath(path, &directory, &filename);
if (error != 0) {
@@ -850,35 +851,37 @@ pf_s32 PFFILE_p_remove(PF_VOLUME* volume, PFDIR_STR* path) {
if (error != 0) {
return error;
}
hint = *(PFFILE_FAT_HINT*)iter.ffd.p_hint;
if ((entry.attr & 0x19) != 0) {
return 0xB;
}
volume_dirs = (PFFILE_VOLUME_DIRS*)entry.p_vol;
if (PFFILE_IsEntryOpened(volume_dirs, &entry)) {
return 0x13;
for (index = 0; index < 5; index++) {
if ((volume_dirs->sfds[index].stat & 1) != 0 &&
(volume_dirs->sfds[index].stat & 2) != 0 &&
entry.p_vol == volume_dirs->sfds[index].dir_entry.p_vol &&
entry.entry_sector == volume_dirs->sfds[index].dir_entry.entry_sector &&
entry.entry_offset == volume_dirs->sfds[index].dir_entry.entry_offset) {
return 0x13;
}
}
iter.ffd.p_hint = (PF_FAT_HINT*)&hint;
start_cluster = entry.start_cluster;
error = PFENT_RemoveEntry(&entry, &iter);
if (error != 0) {
return error;
}
return PFFAT_FreeChain(&iter.ffd, start_cluster, 0xFFFFFFFF, entry.file_size);
return PFFAT_FreeChain(&iter.ffd, entry.start_cluster, 0xFFFFFFFF, entry.file_size);
}
pf_s32 PFFILE_p_fopen(PF_VOLUME* volume, PFDIR_STR* path, pf_u32 open_mode,
pf_s32 PFFILE_p_fopen(PF_VOLUME* volume, PF_STR* path, pf_u32 open_mode,
PFFILE_FILE** result) {
PFDIR_STR filename;
PFDIR_STR directory;
PF_STR directory;
PF_STR filename;
PF_DIR_ENT entry;
PFFILE_ENT_ITER iter;
PFFILE_FFD ffd;
PFFILE_FAT_HINT hint;
PFFILE_FILE* file;
PF_FAT_HINT hint;
PFFILE_SFD* sfd;
PFFILE_FILE* file;
pf_s8 normalized_filename[0x200];
pf_s32 num_chars;
pf_u32 num_chars;
pf_u32 index;
pf_u32 success_size;
pf_u16 access_time;
@@ -897,7 +900,7 @@ pf_s32 PFFILE_p_fopen(PF_VOLUME* volume, PFDIR_STR* path, pf_u32 open_mode,
return 1;
}
if (PFSTR_GetCodeMode(&filename) == 2) {
num_chars = PFSTR_StrNumChar(&filename, 1);
num_chars = PFSTR_StrNumChar(&filename, 1) & 0xFFFF;
if (num_chars > 0xFF) {
return 1;
}
@@ -905,7 +908,49 @@ pf_s32 PFFILE_p_fopen(PF_VOLUME* volume, PFDIR_STR* path, pf_u32 open_mode,
(const pf_u16*)PFSTR_GetStrPos(&filename, 1));
}
PFSTR_SetLocalStr(&filename, normalized_filename);
if ((open_mode & 2) != 0) {
if ((open_mode & 2) == 0) {
error = PFFILE_createEmptyFile(volume, &entry, &filename);
if (error == 8) {
if ((open_mode & 0x10) != 0) {
return 8;
}
if ((open_mode & 1) != 0) {
for (index = 0; index < 5; index++) {
if ((((PFFILE_VOLUME_DIRS*)entry.p_vol)->sfds[index].stat & 3) == 3 &&
entry.p_vol ==
((PFFILE_VOLUME_DIRS*)entry.p_vol)->sfds[index].dir_entry.p_vol &&
entry.entry_sector ==
((PFFILE_VOLUME_DIRS*)entry.p_vol)->sfds[index].dir_entry.entry_sector &&
entry.entry_offset ==
((PFFILE_VOLUME_DIRS*)entry.p_vol)->sfds[index].dir_entry.entry_offset) {
return 8;
}
}
}
if ((entry.attr & 1) != 0 &&
((open_mode & 1) != 0 || (open_mode & 4) != 0 || (open_mode & 8) != 0)) {
return 0xA;
}
if ((entry.attr & 0x10) != 0) {
return 0x17;
}
if ((open_mode & 1) != 0) {
PFFAT_InitFFD(&ffd, &hint, entry.p_vol, &entry.start_cluster);
if (entry.start_cluster > 1 && entry.start_cluster != 0xFFFFFFFF &&
entry.file_size != 0) {
PFFAT_FreeChain(&ffd, entry.start_cluster, 0xFFFFFFFF, entry.file_size);
}
entry.start_cluster = 0;
entry.file_size = 0;
PFENT_getcurrentDateTimeForEnt(&entry.modify_date, &entry.modify_time);
entry.access_date = entry.modify_date;
} else {
PFENT_getcurrentDateTimeForEnt(&entry.access_date, &access_time);
}
} else if (error != 0) {
return error;
}
} else {
PFFAT_InitFFD(&ffd, &hint, entry.p_vol, &entry.start_cluster);
error = PFENT_findEntry(&ffd, &entry, 0, &filename, 0x7F, 0);
if (error != 0) {
@@ -918,41 +963,9 @@ pf_s32 PFFILE_p_fopen(PF_VOLUME* volume, PFDIR_STR* path, pf_u32 open_mode,
return 0x17;
}
PFENT_getcurrentDateTimeForEnt(&entry.access_date, &access_time);
} else {
error = PFFILE_createEmptyFile(volume, &entry, &filename);
if (error == 8) {
if ((open_mode & 0x10) != 0) {
return 8;
}
if ((open_mode & 1) != 0 && PFFILE_IsOpened(&entry)) {
return 8;
}
if ((entry.attr & 1) != 0 &&
((open_mode & 1) != 0 || (open_mode & 4) != 0 || (open_mode & 8) != 0)) {
return 0xA;
}
if ((entry.attr & 0x10) != 0) {
return 0x17;
}
if ((open_mode & 1) != 0) {
if (entry.start_cluster >= 2 && entry.start_cluster != 0xFFFFFFFF &&
entry.file_size != 0) {
PFFAT_FreeChain(&iter.ffd, entry.start_cluster, 0xFFFFFFFF,
entry.file_size);
}
entry.start_cluster = 0;
entry.file_size = 0;
PFENT_getcurrentDateTimeForEnt(&entry.modify_date, &entry.modify_time);
entry.access_date = entry.modify_date;
} else {
PFENT_getcurrentDateTimeForEnt(&entry.access_date, &access_time);
}
} else if (error != 0) {
return error;
}
}
volume = entry.p_vol;
sfd = PFFILE_GetSFD((PFFILE_VOLUME_DIRS*)volume, &entry);
sfd = PFFILE_GetSFD(volume, &entry);
if (sfd == PF_NULL) {
return 0x15;
}
@@ -969,17 +982,18 @@ pf_s32 PFFILE_p_fopen(PF_VOLUME* volume, PFDIR_STR* path, pf_u32 open_mode,
file->p_sfd = sfd;
file->stat = 1;
file->open_mode = open_mode;
file->last_access_data.state.last_access_chain_index = 0;
file->last_access_data.state.last_error = 0;
file->lock_count = 0;
PFFAT_InitHint(&file->hint);
file->cursor.sector = 0xFFFFFFFF;
file->cursor.position = 0;
file->cursor.sector = 0xFFFFFFFF;
PFFILE_Cursor_Recalc(file);
if ((open_mode & 1) != 0) {
file->p_sfd->stat |= 4;
sfd->stat |= 4;
}
if ((file->open_mode & 4) != 0) {
file->cursor.position = file->p_sfd->dir_entry.file_size;
if ((open_mode & 4) != 0) {
file->cursor.position = entry.file_size;
PFFILE_Cursor_Recalc(file);
}
sfd->handler_count++;
@@ -987,29 +1001,8 @@ pf_s32 PFFILE_p_fopen(PF_VOLUME* volume, PFDIR_STR* path, pf_u32 open_mode,
return 0;
}
#pragma dont_inline on
pf_s32 PFFILE_p_fread(PF_VOLUME* volume, pf_u8* buffer, pf_u32 size, pf_u32 count,
PFFILE_FILE* file, pf_u32* count_read) {
pf_u32 bytes_read = 0;
pf_s32 error;
*count_read = 0;
if ((file->open_mode & 8) == 0 &&
((file->open_mode & 1) != 0 || (file->open_mode & 4) != 0)) {
return 0xA;
}
if ((file->p_sfd->lock.mode & 3) != 0 && file->lock_count == 0) {
return 0x19;
}
if (file->cursor.position >= file->p_sfd->dir_entry.file_size) {
return 0x1C;
}
error = PFFILE_Cursor_Read(file, buffer, size * count, &bytes_read);
*count_read = bytes_read / size;
error &= (-error | error) >> 31;
return error;
}
#pragma dont_inline reset
PFFILE_FILE* file, pf_u32* count_read);
pf_s32 PFFILE_p_fwrite(PF_VOLUME* volume, const pf_u8* buffer, pf_u32 size, pf_u32 count,
PFFILE_FILE* file, pf_u32* count_written) {
@@ -1035,37 +1028,32 @@ pf_s32 PFFILE_p_fwrite(PF_VOLUME* volume, const pf_u8* buffer, pf_u32 size, pf_u
if (file->cursor.position > file->p_sfd->dir_entry.file_size) {
remaining = file->cursor.position - file->p_sfd->dir_entry.file_size;
file->cursor.position = file->p_sfd->dir_entry.file_size;
file->cursor.file_sector_index = file->cursor.position >> volume->bpb.log2_bytes_per_sector;
file->cursor.offset_in_sector = file->cursor.position & (volume->bpb.bytes_per_sector - 1);
PFFILE_Cursor_Recalc(file);
error = PFCACHE_AllocateDataPage(volume, 0xFFFFFFFF, &page, &allocation_option);
if (error != 0) {
return error;
}
pf_memset(page->p_buf, 0, volume->bpb.bytes_per_sector);
while (remaining != 0) {
if (remaining > volume->bpb.bytes_per_sector) {
error = PFFILE_Cursor_Write(file, page->p_buf, volume->bpb.bytes_per_sector,
&bytes_written);
if (error != 0) {
return error;
}
} else {
if (remaining <= volume->bpb.bytes_per_sector) {
error = PFFILE_Cursor_Write(file, page->p_buf, remaining, &bytes_written);
if (error != 0) {
return error;
}
break;
}
error = PFFILE_Cursor_Write(file, page->p_buf, volume->bpb.bytes_per_sector,
&bytes_written);
if (error != 0) {
return error;
}
remaining -= volume->bpb.bytes_per_sector;
}
PFCACHE_FreeDataPage(volume, page);
}
error = PFFILE_Cursor_Write(file, buffer, size * count, &bytes_written);
*count_written = bytes_written / size;
if (error != 0) {
return error;
}
return 0;
return error;
}
pf_s32 PFFILE_p_fappend(PFFILE_FILE* file, pf_u32 size, pf_u32* size_appended) {
@@ -1132,14 +1120,14 @@ pf_s32 PFFILE_p_finfo(PFFILE_FILE* file, PF_INFO* info) {
return 0;
}
pf_s32 PFFILE_p_combine(PF_VOLUME* volume, PFDIR_STR* first_path, PFDIR_STR* second_path) {
pf_s32 PFFILE_p_combine(PF_VOLUME* volume, PF_STR* first_path, PF_STR* second_path) {
PFFILE_ENT_ITER first_iter;
PFFILE_ENT_ITER second_iter;
PFFILE_ENT_ITER second_iter_copy;
PF_DIR_ENT first_entry;
PF_DIR_ENT second_entry;
PFFILE_FAT_HINT first_hint;
PFFILE_FAT_HINT second_hint;
PF_FAT_HINT first_hint;
PF_FAT_HINT second_hint;
pf_u32 index;
pf_s32 found;
pf_s32 error;
@@ -1158,14 +1146,41 @@ pf_s32 PFFILE_p_combine(PF_VOLUME* volume, PFDIR_STR* first_path, PFDIR_STR* sec
if ((first_entry.attr & 1) != 0 || (second_entry.attr & 1) != 0) {
return 0x18;
}
if (PFFILE_IsOpened(&first_entry) || PFFILE_IsOpened(&second_entry)) {
found = 0;
for (index = 0; index < 5; index++) {
PFFILE_SFD* sfd = &((PFFILE_VOLUME_DIRS*)first_entry.p_vol)->sfds[index];
if ((sfd->stat & 1) != 0 && (sfd->stat & 2) != 0 &&
first_entry.p_vol == sfd->dir_entry.p_vol &&
first_entry.entry_sector == sfd->dir_entry.entry_sector &&
first_entry.entry_offset == sfd->dir_entry.entry_offset) {
found = 1;
break;
}
}
if (found != 0) {
return 0x13;
}
found = 0;
for (index = 0; index < 5; index++) {
PFFILE_SFD* sfd = &((PFFILE_VOLUME_DIRS*)second_entry.p_vol)->sfds[index];
if ((sfd->stat & 1) != 0 && (sfd->stat & 2) != 0 &&
second_entry.p_vol == sfd->dir_entry.p_vol &&
second_entry.entry_sector == sfd->dir_entry.entry_sector &&
second_entry.entry_offset == sfd->dir_entry.entry_offset) {
found = 1;
break;
}
}
if (found != 0) {
return 0x13;
}
if (first_entry.entry_sector == second_entry.entry_sector &&
first_entry.entry_offset == second_entry.entry_offset) {
return 0xA;
}
if (0xFFFFFFFF - first_entry.file_size < second_entry.file_size) {
if (0xFFFFFFFF - second_entry.file_size < first_entry.file_size) {
return 0x25;
}
PFFAT_InitHint(&first_hint);
@@ -1187,21 +1202,25 @@ pf_s32 PFFILE_p_combine(PF_VOLUME* volume, PFDIR_STR* first_path, PFDIR_STR* sec
return error & (-error | error) >> 31;
}
pf_s32 PFFILE_p_divide(PF_VOLUME* volume, PFDIR_STR* source_path, PFDIR_STR* destination_path,
pf_s32 PFFILE_p_divide(PF_VOLUME* volume, PF_STR* source_path, PF_STR* destination_path,
pf_u32 split_size) {
PFDIR_STR destination_filename;
PFDIR_STR destination_directory;
PF_STR destination_directory;
PF_STR destination_filename;
PF_DIR_ENT source_entry;
PF_DIR_ENT destination_entry;
PF_DIR_ENT source_entry_copy;
PF_DIR_ENT destination_entry;
PFFILE_ENT_ITER source_iter;
PFFILE_ENT_ITER destination_iter;
PFFILE_FAT_HINT source_hint;
PFFILE_FAT_HINT destination_hint;
PFFILE_ENT_ITER destination_file_iter;
PFFILE_FFD source_ffd;
PFFILE_FFD destination_ffd;
PF_FAT_HINT source_hint;
PF_FAT_HINT destination_hint;
pf_s8 normalized_filename[0x200];
pf_u32 original_size;
pf_u32 original_start_cluster;
pf_s32 namelength;
pf_u32 original_size;
pf_u32 index;
pf_u16 current_time;
pf_s32 error;
error = PFENT_ITER_GetEntryOfPath(&source_iter, &source_entry, volume, source_path, 0);
@@ -1214,8 +1233,16 @@ pf_s32 PFFILE_p_divide(PF_VOLUME* volume, PFDIR_STR* source_path, PFDIR_STR* des
if ((source_entry.attr & 1) != 0) {
return 0x18;
}
if (PFFILE_IsOpened(&source_entry)) {
return 0x13;
for (index = 0; index < 5; index++) {
if ((((PFFILE_VOLUME_DIRS*)source_entry.p_vol)->sfds[index].stat & 3) == 3 &&
source_entry.p_vol ==
((PFFILE_VOLUME_DIRS*)source_entry.p_vol)->sfds[index].dir_entry.p_vol &&
source_entry.entry_sector ==
((PFFILE_VOLUME_DIRS*)source_entry.p_vol)->sfds[index].dir_entry.entry_sector &&
source_entry.entry_offset ==
((PFFILE_VOLUME_DIRS*)source_entry.p_vol)->sfds[index].dir_entry.entry_offset) {
return 0x13;
}
}
if (source_entry.file_size == 0) {
return 0xA;
@@ -1228,8 +1255,7 @@ pf_s32 PFFILE_p_divide(PF_VOLUME* volume, PFDIR_STR* source_path, PFDIR_STR* des
return 1;
}
if (PFSTR_GetCodeMode(&destination_filename) == 2) {
namelength = PFSTR_StrNumChar(&destination_filename, 1);
if (namelength > 0xFF) {
if ((PFSTR_StrNumChar(&destination_filename, 1) & 0xFFFF) > 0xFF) {
return 1;
}
PFPATH_transformFromUnicodeToNormal(
@@ -1248,14 +1274,13 @@ pf_s32 PFFILE_p_divide(PF_VOLUME* volume, PFDIR_STR* source_path, PFDIR_STR* des
if (error != 0) {
return error;
}
PFFAT_InitHint(&source_hint);
PFFAT_InitFFD(&source_iter.ffd, &source_hint, volume, &source_entry.start_cluster);
PFFAT_InitHint(&destination_hint);
PFFAT_InitFFD(&destination_iter.ffd, &destination_hint, volume,
&destination_entry.start_cluster);
PFFAT_InitFFD(&destination_ffd, &destination_hint, volume, &destination_entry.start_cluster);
PFFAT_InitHint(&source_hint);
PFFAT_InitFFD(&source_ffd, &source_hint, volume, &source_entry.start_cluster);
pf_memcpy(&source_entry_copy, &source_entry, sizeof(source_entry_copy));
original_size = source_entry.file_size;
original_start_cluster = source_entry.start_cluster;
original_size = source_entry.file_size;
PFENT_getcurrentDateTimeForEnt(&source_entry.modify_date, &source_entry.modify_time);
source_entry.access_date = source_entry.modify_date;
if (split_size == 0) {
@@ -1263,36 +1288,35 @@ pf_s32 PFFILE_p_divide(PF_VOLUME* volume, PFDIR_STR* source_path, PFDIR_STR* des
}
source_entry.file_size = split_size;
error = PFENT_updateEntry(&source_entry, 1);
if (error != 0) {
return error;
}
if ((((PFFILE_VOLUME_DIRS*)volume)->cache_control & 4) != 0) {
error = PFCACHE_FlushDataCacheSpecific(volume, 0);
if (error != 0) {
return error;
if (error == 0 &&
((((PFFILE_VOLUME_DIRS*)volume)->cache_control & 4) == 0 ||
PFCACHE_FlushDataCacheSpecific(volume, 0) == 0)) {
error = PFCLUSTER_DivideFile(&source_iter, &destination_iter, &source_entry_copy,
&destination_entry, split_size);
if (error == 0) {
destination_entry.create_time = source_entry.modify_time;
destination_entry.create_date = source_entry.modify_date;
destination_entry.modify_time = source_entry.modify_time;
destination_entry.modify_date = source_entry.modify_date;
destination_entry.access_date = source_entry.access_date;
destination_entry.attr = source_entry.attr;
error = PFENT_updateEntry(&destination_entry, 1);
return error & (-error | error) >> 31;
}
PFCACHE_FlushFATCache(volume);
source_entry.start_cluster = original_start_cluster;
source_entry.file_size = original_size;
PFENT_updateEntry(&source_entry, 1);
error = PFENT_ITER_GetEntryOfPath(&destination_file_iter, &destination_entry, volume,
destination_path, 0);
if (error == 0) {
PFENT_RemoveEntry(&destination_entry, &destination_file_iter);
}
}
error = PFCLUSTER_DivideFile(&source_iter, &destination_iter, &source_entry_copy,
&destination_entry, split_size);
if (error != 0) {
PFCACHE_FlushFATCache(volume);
source_entry.file_size = original_size;
source_entry.start_cluster = original_start_cluster;
PFENT_updateEntry(&source_entry, 1);
PFENT_ITER_GetEntryOfPath(&destination_iter, &destination_entry, volume,
destination_path, 0);
PFENT_RemoveEntry(&destination_entry, &destination_iter);
return error;
}
destination_entry.modify_date = source_entry.modify_date;
destination_entry.modify_time = source_entry.modify_time;
destination_entry.access_date = source_entry.access_date;
destination_entry.attr = source_entry.attr;
error = PFENT_updateEntry(&destination_entry, 1);
return error & (-error | error) >> 31;
return error;
}
pf_s32 PFFILE_p_cinsert(PF_VOLUME* volume, PFDIR_STR* path, pf_u32 cluster_index,
pf_s32 PFFILE_p_cinsert(PF_VOLUME* volume, PF_STR* path, pf_u32 cluster_index,
pf_u32 count, pf_u32 mode) {
PFFILE_ENT_ITER iter;
PF_DIR_ENT entry;
@@ -1336,7 +1360,7 @@ pf_s32 PFFILE_p_cinsert(PF_VOLUME* volume, PFDIR_STR* path, pf_u32 cluster_index
return error & (-error | error) >> 31;
}
pf_s32 PFFILE_p_cdelete(PF_VOLUME* volume, PFDIR_STR* path, pf_u32 cluster_index,
pf_s32 PFFILE_p_cdelete(PF_VOLUME* volume, PF_STR* path, pf_u32 cluster_index,
pf_u32 count, pf_u32 mode) {
PFFILE_ENT_ITER iter;
PF_DIR_ENT entry;
@@ -1379,22 +1403,20 @@ pf_s32 PFFILE_p_cdelete(PF_VOLUME* volume, PFDIR_STR* path, pf_u32 cluster_index
pf_s32 PFFILE_p_flock(PFFILE_FILE* file, pf_u32 mode) {
if ((mode & 3) != 0) {
if ((file->p_sfd->lock.mode & 3) != 0 && (mode & 8) != 0) {
if ((file->p_sfd->lock.mode & 3) != 0 && (mode & 0x10) != 0) {
if ((mode & 1) != 0) {
if ((file->p_sfd->lock.mode & 2) != 0) {
goto lock_error;
return 0x27;
}
}
if ((mode & 2) != 0 && (file->p_sfd->lock.mode & 1) != 0) {
goto lock_error;
} else if ((mode & 2) != 0 && (file->p_sfd->lock.mode & 1) != 0) {
return 0x27;
}
if (file->p_sfd->lock.owner != 0 && file->p_sfd->lock.owner != (PF_FILE*)file) {
lock_error:
return 0x27;
}
}
if ((mode & 1) != 0) {
if (file->p_sfd->lock.count == 0 || (file->p_sfd->lock.mode & 2) != 0) {
if (file->p_sfd->lock.count == 0 || (file->p_sfd->lock.mode & 2) == 0) {
file->p_sfd->lock.wcount++;
PF_LockFile((PF_FILE*)file, 0);
file->p_sfd->lock.wcount--;
@@ -1410,22 +1432,30 @@ pf_s32 PFFILE_p_flock(PFFILE_FILE* file, pf_u32 mode) {
}
file->p_sfd->lock.count++;
file->lock_count++;
} else {
if (file->lock_count == 0 || file->p_sfd->lock.count == 0) {
return 0x27;
}
if ((file->p_sfd->lock.mode & 2) != 0 && file->p_sfd->lock.owner != (PF_FILE*)file) {
return 0x27;
}
file->lock_count--;
file->p_sfd->lock.count--;
return 0;
}
if (file->lock_count == 0 || file->p_sfd->lock.count == 0) {
return 0x27;
}
if ((file->p_sfd->lock.mode & 2) != 0 && file->p_sfd->lock.owner != (PF_FILE*)file) {
return 0x27;
}
if ((file->p_sfd->lock.mode & 1) != 0) {
file->p_sfd->lock.count -= file->lock_count;
file->lock_count = 0;
if (file->p_sfd->lock.count == 0) {
PF_UnLockFile((PF_FILE*)file);
if ((file->p_sfd->lock.mode & 2) != 0) {
file->p_sfd->lock.owner = 0;
}
file->p_sfd->lock.mode &= ~3;
}
file->p_sfd->lock.mode &= ~3;
} else {
if (file->p_sfd->lock.owner != (PF_FILE*)file) {
return 0x27;
}
file->p_sfd->lock.count = 0;
file->lock_count = 0;
file->p_sfd->lock.owner = 0;
PF_UnLockFile((PF_FILE*)file);
file->p_sfd->lock.mode &= ~3;
}
return 0;
}
@@ -1473,7 +1503,7 @@ pf_s32 PFFILE_FsexecOpenFile(PF_DIR_ENT* entry, PFFILE_ENT_ITER* iter,
} else {
PFENT_getcurrentDateTimeForEnt(&entry->access_date, &access_time);
}
sfd = PFFILE_GetSFD((PFFILE_VOLUME_DIRS*)volume, entry);
sfd = PFFILE_GetSFD(volume, entry);
if (sfd == PF_NULL) {
return 0x15;
}
@@ -1533,7 +1563,7 @@ void PFFILE_FinalizeAllFiles(PF_VOLUME* volume) {
volume_dirs->num_opened_files = 0;
}
pf_s32 PFFILE_remove(PFDIR_STR* path) {
pf_s32 PFFILE_remove(PF_STR* path) {
PF_VOLUME* volume;
pf_s32 error;
@@ -1577,7 +1607,7 @@ pf_s32 PFFILE_ferror(PFFILE_FILE* file) {
return file->last_access_data.state.last_error;
}
pf_s32 PFFILE_fopen(PFDIR_STR* path, pf_s32 mode, PFFILE_FILE** result) {
pf_s32 PFFILE_fopen(PF_STR* path, pf_s32 mode, PFFILE_FILE** result) {
PF_VOLUME* volume;
pf_s32 error;
@@ -1678,12 +1708,10 @@ close_file_complete:
pf_s32 PFFILE_fread(pf_u8* buffer, pf_u32 size, pf_u32 count,
PFFILE_FILE* file, pf_u32* count_read) {
PF_VOLUME* volume;
pf_u32 bytes_read;
PF_VOLUME* volume = file == PF_NULL ? PF_NULL : file->p_sfd->dir_entry.p_vol;
pf_s32 error;
*count_read = 0;
volume = file == PF_NULL ? PF_NULL : file->p_sfd->dir_entry.p_vol;
error = PFVOL_CheckForRead(volume);
if (error != 0) {
pf_vol_set.last_error = error;
@@ -1696,8 +1724,7 @@ pf_s32 PFFILE_fread(pf_u8* buffer, pf_u32 size, pf_u32 count,
return 0x26;
}
((PFFILE_VOLUME_DIRS*)volume)->cache_signature = file;
error = PFFILE_p_fread(volume, buffer, size, count, file, &bytes_read);
*count_read = bytes_read;
error = PFFILE_p_fread(volume, buffer, size, count, file, count_read);
((PFFILE_VOLUME_DIRS*)volume)->cache_signature = PF_NULL;
if (error != 0) {
pf_vol_set.last_error = error;
@@ -1842,8 +1869,8 @@ pf_s32 PFFILE_fsetclstlink(PFFILE_FILE* file, pf_u32 mode,
file->p_sfd->ffd.cluster_link.interval_offset = 0;
file->p_sfd->ffd.cluster_link.position = 0;
file->p_sfd->ffd.cluster_link.save_index = 0;
if (file->p_sfd->dir_entry.file_size != 0 && file->p_sfd->ffd.cluster_link.max_count != 0) {
file->p_sfd->ffd.cluster_link.buffer[0] = *file->p_sfd->ffd.p_start_cluster;
if (file->p_sfd->dir_entry.file_size != 0 && settings->link_max != 0) {
settings->buffer[0] = *file->p_sfd->ffd.p_start_cluster;
file->p_sfd->ffd.cluster_link.position++;
}
if (file->p_sfd->ffd.p_hint != PF_NULL) {
@@ -2024,7 +2051,7 @@ pf_s32 PFFILE_finfo(PFFILE_FILE* file, PF_INFO* info) {
return 0;
}
pf_s32 PFFILE_combine(PFDIR_STR* first_path, PFDIR_STR* second_path) {
pf_s32 PFFILE_combine(PF_STR* first_path, PF_STR* second_path) {
PF_VOLUME* volume = PFPATH_GetVolumeFromPath(first_path);
PF_VOLUME* second_volume = PFPATH_GetVolumeFromPath(second_path);
pf_s32 error;
@@ -2061,7 +2088,7 @@ pf_s32 PFFILE_combine(PFDIR_STR* first_path, PFDIR_STR* second_path) {
return error;
}
pf_s32 PFFILE_divide(PFDIR_STR* source_path, PFDIR_STR* destination_path, pf_u32 split_size) {
pf_s32 PFFILE_divide(PF_STR* source_path, PF_STR* destination_path, pf_u32 split_size) {
PF_VOLUME* volume = PFPATH_GetVolumeFromPath(source_path);
PF_VOLUME* destination_volume = PFPATH_GetVolumeFromPath(destination_path);
pf_s32 error;
@@ -2098,7 +2125,7 @@ pf_s32 PFFILE_divide(PFDIR_STR* source_path, PFDIR_STR* destination_path, pf_u32
return error;
}
pf_s32 PFFILE_cinsert(PFDIR_STR* path, pf_u32 cluster_index, pf_u32 count,
pf_s32 PFFILE_cinsert(PF_STR* path, pf_u32 cluster_index, pf_u32 count,
pf_u32* cluster_result) {
PF_VOLUME* volume;
pf_s32 error;
@@ -2133,7 +2160,7 @@ pf_s32 PFFILE_cinsert(PFDIR_STR* path, pf_u32 cluster_index, pf_u32 count,
return error;
}
pf_s32 PFFILE_cdelete(PFDIR_STR* path, pf_u32 cluster_index, pf_u32 count,
pf_s32 PFFILE_cdelete(PF_STR* path, pf_u32 cluster_index, pf_u32 count,
pf_u32* cluster_result) {
PF_VOLUME* volume;
pf_s32 error;
@@ -2191,3 +2218,24 @@ pf_s32 PFFILE_flock(PFFILE_FILE* file, pf_u32 mode) {
return error;
}
pf_s32 PFFILE_p_fread(PF_VOLUME* volume, pf_u8* buffer, pf_u32 size, pf_u32 count,
PFFILE_FILE* file, pf_u32* count_read) {
pf_u32 bytes_read = 0;
pf_s32 error;
*count_read = 0;
if ((file->open_mode & 8) == 0 &&
((file->open_mode & 1) != 0 || (file->open_mode & 4) != 0)) {
return 0xA;
}
if ((file->p_sfd->lock.mode & 3) != 0 && file->lock_count == 0) {
return 0x19;
}
if (file->cursor.position >= file->p_sfd->dir_entry.file_size) {
return 0x1C;
}
error = PFFILE_Cursor_Read(file, buffer, size * count, &bytes_read);
*count_read = bytes_read / size;
error &= (-error | error) >> 31;
return error;
}

View File

@@ -1,56 +0,0 @@
#include <private/fa/pf_system.h>
#include <private/vf/PrFILE2/fatfs/pf_entry.h>
typedef struct PFFILELOCK_SFD {
pf_u8 descriptor[0x3c];
PF_DIR_ENT dir_entry;
pf_u16 lock_mode;
pf_u16 lock_count;
pf_u32 writer_count;
void* owner;
pf_s32 resource;
OSSemaphore semaphore;
} PFFILELOCK_SFD;
typedef struct PFFILELOCK_FILE {
pf_u32 stat;
pf_u32 open_mode;
PFFILELOCK_SFD* p_sfd;
} PFFILELOCK_FILE;
typedef struct PFFILELOCK_SEMAPHORE_PARAM {
pf_u32 initial_count;
pf_u32 max_count;
OSSemaphore* semaphore;
} PFFILELOCK_SEMAPHORE_PARAM;
extern pf_s32 pfk_create_semaphore(PFFILELOCK_SEMAPHORE_PARAM*);
extern pf_s32 pfk_get_semaphore(pf_s32);
extern pf_s32 pfk_release_semaphore(pf_s32);
extern pf_s32 pfk_delete_semaphore(pf_s32);
void PF_InitLockFile(void) {
pf_sys_set.flock_count = 0;
}
pf_s32 PF_LockFile(PFFILELOCK_FILE* p_file) {
pf_s32 result = -1;
PFFILELOCK_SEMAPHORE_PARAM param;
if (p_file->p_sfd->lock_count == 0 && p_file->p_sfd->resource == 0) {
if (pf_sys_set.flock_count < -1U) {
param.initial_count = 1;
param.max_count = 1;
param.semaphore = &p_file->p_sfd->semaphore;
p_file->p_sfd->resource = pfk_create_semaphore(&param);
result = 0;
++pf_sys_set.flock_count;
} else result = -1;
}
pfk_get_semaphore(p_file->p_sfd->resource);
return result;
}
pf_s32 PF_UnLockFile(PFFILELOCK_FILE* p_file) {
pfk_release_semaphore(p_file->p_sfd->resource);
if (p_file->p_sfd->lock_count == 0 && p_file->p_sfd->writer_count == 0 && pf_sys_set.flock_count != 0) {
pfk_delete_semaphore(p_file->p_sfd->resource);
p_file->p_sfd->resource = 0;
--pf_sys_set.flock_count;
}
return 0;
}

View File

@@ -1,13 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue
#define VFiPFFILE_finfo PFFILE_finfo
#include <private/vf/PrFILE2/standard/pf_finfo.h>
#include <private/vf/PrFILE2/fatfs/pf_volume.h>
#include <private/vf/PrFILE2/standard/pf_api_util.h>
pf_int32 pf2_finfo(PF_FILE* p_file, PF_INFO* p_info) {
pf_s32 err = VFiPFFILE_finfo(p_file, p_info);
err = VFiPFAPI_convertReturnValue(err);
return err;
}

View File

@@ -1,41 +0,0 @@
#include <revolution/fa.h>
typedef struct PFF2_STR {
const s8* p_head;
const s8* p_tail;
const s8* p_current;
u32 code_mode;
} PFF2_STR;
typedef struct PFF2_VOL_SET {
u8 reserved[0x40];
s32 last_error;
} PFF2_VOL_SET;
extern PFF2_VOL_SET pf_vol_set;
extern s32 PFAPI_ParseOpenModeString(const char* mode_str);
extern s32 PFSTR_InitStr(PFF2_STR* path, const s8* text, u32 code_mode);
extern s32 PFFILE_fopen(PFF2_STR* path, s32 mode, FAFILE** result);
extern FAFILE* PFAPI_convertReturnValue2NULL(s32 err, FAFILE* p_stream);
FAFILE* pf2_fopen(const char* path, const char* mode) {
u32 open_mode;
FAFILE* p_file;
PFF2_STR path_str;
s32 err;
open_mode = PFAPI_ParseOpenModeString(mode);
if (open_mode == 0) {
pf_vol_set.last_error = 10;
p_file = NULL;
} else {
err = PFSTR_InitStr(&path_str, (const s8*)path, 1);
if (err == 0) {
err = PFFILE_fopen(&path_str, open_mode, &p_file);
} else {
pf_vol_set.last_error = err;
}
p_file = PFAPI_convertReturnValue2NULL(err, p_file);
}
return p_file;
}

View File

@@ -1,13 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue
#define VFiPFVOL_format PFVOL_format
#include <private/vf/PrFILE2/standard/pf_format.h>
#include <private/vf/PrFILE2/fatfs/pf_volume.h>
#include <private/vf/PrFILE2/standard/pf_api_util.h>
pf_int32 pf2_format(pf_ch8 drive, const pf_ch8* param) {
pf_s32 err = VFiPFVOL_format(drive, (pf_u8*)param);
err = VFiPFAPI_convertReturnValue(err);
return err;
}

View File

@@ -1,10 +0,0 @@
#include <revolution/fa.h>
extern s32 PFFILE_fseek(FAFILE* stream, s32 offset, int origin);
extern s32 PFAPI_convertReturnValue(s32 err);
FAError pf2_fseek(FAFILE* stream, s32 offset, int origin) {
s32 err = PFFILE_fseek(stream, offset, origin);
err = PFAPI_convertReturnValue(err);
return err;
}

View File

@@ -1,32 +0,0 @@
#include <revolution/fa.h>
typedef struct PFF2_STR {
const s8* p_head;
const s8* p_tail;
const s8* p_current;
u32 code_mode;
} PFF2_STR;
typedef struct PFF2_VOL_SET {
u8 reserved[0x40];
s32 last_error;
} PFF2_VOL_SET;
extern PFF2_VOL_SET pf_vol_set;
extern s32 PFSTR_InitStr(PFF2_STR* path, const s8* text, u32 code_mode);
extern s32 PFDIR_fsfirst(PFF2_STR* path, u8 attributes, FADta* data);
extern s32 PFAPI_convertReturnValue(s32 err);
FAError pf2_fsfirst(const char* dirName, u8 attr, FADta* dta) {
s32 err;
PFF2_STR path_str;
err = PFSTR_InitStr(&path_str, (const s8*)dirName, 1);
if (err == 0) {
err = PFDIR_fsfirst(&path_str, attr, dta);
} else {
pf_vol_set.last_error = err;
}
err = PFAPI_convertReturnValue(err);
return err;
}

View File

@@ -1,10 +0,0 @@
#include <revolution/fa.h>
extern s32 PFDIR_fsnext(FADta* data);
extern s32 PFAPI_convertReturnValue(s32 err);
FAError pf2_fsnext(FADta* dta) {
s32 err = PFDIR_fsnext(dta);
err = PFAPI_convertReturnValue(err);
return err;
}

View File

@@ -1,32 +0,0 @@
#include <revolution/fa.h>
typedef struct PFF2_STR {
const s8* p_head;
const s8* p_tail;
const s8* p_current;
u32 code_mode;
} PFF2_STR;
typedef struct PFF2_VOL_SET {
u8 reserved[0x40];
s32 last_error;
} PFF2_VOL_SET;
extern PFF2_VOL_SET pf_vol_set;
extern s32 PFSTR_InitStr(PFF2_STR* path, const s8* text, u32 code_mode);
extern s32 PFDIR_fstat(PFF2_STR* path, FAFileStat* file_stat);
extern s32 PFAPI_convertReturnValue(s32 err);
FAError pf2_fstat(const char* fileName, FAFileStat* stat) {
s32 err;
PFF2_STR path_str;
err = PFSTR_InitStr(&path_str, (const s8*)fileName, 1);
if (err == 0) {
err = PFDIR_fstat(&path_str, stat);
} else {
pf_vol_set.last_error = err;
}
err = PFAPI_convertReturnValue(err);
return err;
}

View File

@@ -1,13 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue
#define VFiPFVOL_getdev PFVOL_getdev
#include <private/vf/PrFILE2/standard/pf_getdev.h>
#include <private/vf/PrFILE2/fatfs/pf_volume.h>
#include <private/vf/PrFILE2/standard/pf_api_util.h>
pf_int32 pf2_devinf(pf_ch8 drv_char, PF_DEV_INF* dev_inf) {
pf_s32 err = VFiPFVOL_getdev(drv_char, dev_inf);
err = VFiPFAPI_convertReturnValue(err);
return err;
}

View File

@@ -1,19 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue
#define VFiPFFATFS_initializeFATFS PFFATFS_initializeFATFS
#define VFiPFSYS_initializeSYS PFSYS_initializeSYS
#include <private/vf/PrFILE2/standard/pf_init_prfile2.h>
#include <private/vf/PrFILE2/fatfs/pf_fatfs.h>
#include <private/vf/PrFILE2/standard/pf_api_util.h>
#include <private/vf/PrFILE2/system/pf_system.h>
pf_int32 pf2_init_prfile2(pf_s32 config, void* param) {
pf_s32 err = VFiPFFATFS_initializeFATFS(config, param);
if (err == 0) {
VFiPFSYS_initializeSYS();
}
err = VFiPFAPI_convertReturnValue(err);
return err;
}

View File

@@ -1,31 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue
#define VFiPFDIR_mkdir PFDIR_mkdir
#define VFiPFSTR_InitStr PFSTR_InitStr
#include <private/vf/PrFILE2/standard/pf_mkdir.h>
typedef struct PFAPI_GLOBAL_STATE {
pf_u8 volume_state[0x40];
pf_s32 last_error;
} PFAPI_GLOBAL_STATE;
extern PFAPI_GLOBAL_STATE pf_vol_set;
#include <private/vf/PrFILE2/fatfs/pf_dir.h>
#include <private/vf/PrFILE2/standard/pf_api_util.h>
#include <private/vf/PrFILE2/common/pf_str.h>
pf_int32 pf2_mkdir(const pf_ch8* sPath) {
pf_s32 err;
PF_STR path_str;
err = VFiPFSTR_InitStr(&path_str, (pf_s8*)sPath, 1U);
if (err == 0) {
err = VFiPFDIR_mkdir(&path_str);
} else {
pf_vol_set.last_error = err;
}
err = VFiPFAPI_convertReturnValue(err);
return err;
}

View File

@@ -1,31 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue
#define VFiPFFILE_remove PFFILE_remove
#define VFiPFSTR_InitStr PFSTR_InitStr
#include <private/vf/PrFILE2/standard/pf_remove.h>
typedef struct PFAPI_GLOBAL_STATE {
pf_u8 volume_state[0x40];
pf_s32 last_error;
} PFAPI_GLOBAL_STATE;
extern PFAPI_GLOBAL_STATE pf_vol_set;
#include <private/vf/PrFILE2/fatfs/pf_file.h>
#include <private/vf/PrFILE2/standard/pf_api_util.h>
#include <private/vf/PrFILE2/common/pf_str.h>
pf_int32 pf2_remove(const pf_ch8* path) {
pf_s32 err;
PF_STR path_str;
err = VFiPFSTR_InitStr(&path_str, (pf_s8*)path, 1U);
if (err == 0) {
err = VFiPFFILE_remove(&path_str);
} else {
pf_vol_set.last_error = err;
}
err = VFiPFAPI_convertReturnValue(err);
return err;
}

View File

@@ -1,31 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue
#define VFiPFDIR_rmdir PFDIR_rmdir
#define VFiPFSTR_InitStr PFSTR_InitStr
#include <private/vf/PrFILE2/standard/pf_rmdir.h>
typedef struct PFAPI_GLOBAL_STATE {
pf_u8 volume_state[0x40];
pf_s32 last_error;
} PFAPI_GLOBAL_STATE;
extern PFAPI_GLOBAL_STATE pf_vol_set;
#include <private/vf/PrFILE2/fatfs/pf_dir.h>
#include <private/vf/PrFILE2/standard/pf_api_util.h>
#include <private/vf/PrFILE2/common/pf_str.h>
pf_int32 pf2_rmdir(const pf_ch8* sPath) {
pf_s32 err;
PF_STR path_str;
err = VFiPFSTR_InitStr(&path_str, (pf_s8*)sPath, 1U);
if (err == 0) {
err = VFiPFDIR_rmdir(&path_str);
} else {
pf_vol_set.last_error = err;
}
err = VFiPFAPI_convertReturnValue(err);
return err;
}

View File

@@ -1,216 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFCACHE_AllocateDataPage PFCACHE_AllocateDataPage
#define VFiPFCACHE_ReadDataNumSector PFCACHE_ReadDataNumSector
#define VFiPFCACHE_ReadDataPage PFCACHE_ReadDataPage
#define VFiPFCACHE_ReadDataPageAndFlushIfNeeded PFCACHE_ReadDataPageAndFlushIfNeeded
#define VFiPFCACHE_ReadFATPage PFCACHE_ReadFATPage
#define VFiPFCACHE_SearchDataCache PFCACHE_SearchDataCache
#define VFiPFCACHE_UpdateModifiedSector PFCACHE_UpdateModifiedSector
#define VFiPFCACHE_WriteDataNumSectorAndFreeIfNeeded PFCACHE_WriteDataNumSectorAndFreeIfNeeded
#define VFiPFCACHE_WriteDataPage PFCACHE_WriteDataPage
#define VFiPFCACHE_WriteFATPage PFCACHE_WriteFATPage
#define VFiPFDRV_IsInserted PFDRV_IsInserted
#define VFiPFDRV_IsWProtected PFDRV_IsWProtected
#define VFiPFSEC_ReadData PFSEC_ReadData
#define VFiPFSEC_ReadDataSector PFSEC_ReadDataSector
#define VFiPFSEC_ReadFAT PFSEC_ReadFAT
#define VFiPFSEC_WriteData PFSEC_WriteData
#define VFiPFSEC_WriteDataSector PFSEC_WriteDataSector
#define VFiPFSEC_WriteFAT PFSEC_WriteFAT
#define VFipf_memcpy pf_memcpy
#include <private/fa/pf_volume.h>
#include <private/vf/PrFILE2/fatfs/pf_cache.h>
#include <private/vf/PrFILE2/common/pf_clib.h>
#include <private/vf/PrFILE2/driver/pf_driver.h>
#include <private/vf/PrFILE2/fatfs/pf_volume.h>
pf_s32 VFiPFSEC_ReadFAT(PF_VOLUME* p_vol, pf_u8* p_buf, pf_u32 sector, pf_u16 offset, pf_u16 size) {
PF_CACHE_PAGE* p_page;
pf_s32 err;
if ((p_vol->flags & 0x01) == 0 || VFiPFDRV_IsInserted(p_vol) == PF_FALSE) {
return 9;
}
if (sector >= p_vol->bpb.total_sectors || offset >= p_vol->bpb.bytes_per_sector || offset + size > p_vol->bpb.bytes_per_sector) {
return 10;
}
err = VFiPFCACHE_ReadFATPage(p_vol, sector, &p_page);
if (err != 0) {
return err;
}
VFipf_memcpy(p_buf, &p_page->p_buf[offset], size);
return 0;
}
pf_s32 VFiPFSEC_ReadData(PF_VOLUME* p_vol, pf_u8* p_buf, pf_u32 sector, pf_u16 offset, pf_u32 size, pf_u32* p_success_size, pf_u32 set_sig) {
PF_CACHE_PAGE* p_page;
pf_u32 num_success;
pf_u32 num_sector;
pf_u32 adjust_sector;
pf_u32 rest_sector;
pf_s32 err;
*p_success_size = 0;
if ((p_vol->flags & 0x01) == 0 || VFiPFDRV_IsInserted(p_vol) == PF_FALSE) {
return 9;
}
if ((sector >= p_vol->bpb.total_sectors || offset >= p_vol->bpb.bytes_per_sector || offset + size > p_vol->bpb.bytes_per_sector) && offset != 0) {
return 10;
}
if (offset != 0 || size < p_vol->bpb.bytes_per_sector) {
err = VFiPFCACHE_ReadDataPage(p_vol, sector, &p_page, set_sig);
if (err != 0) {
return err;
}
VFipf_memcpy(p_buf, &p_page->p_buf[offset], size);
*p_success_size = size;
} else if (offset == 0 && (size & (p_vol->bpb.bytes_per_sector - 1)) == 0) {
err = VFiPFCACHE_ReadDataNumSector(p_vol, p_buf, sector, size >> p_vol->bpb.log2_bytes_per_sector, &num_success);
if (err != 0) {
return err;
}
*p_success_size += num_success << p_vol->bpb.log2_bytes_per_sector;
if (num_success != (size >> p_vol->bpb.log2_bytes_per_sector)) {
return 17;
}
} else if (offset == 0) {
p_page = VFiPFCACHE_SearchDataCache(p_vol, sector);
if (p_page != PF_NULL) {
num_sector = (p_page->sector + p_page->size) - sector;
if (num_sector <= (size >> p_vol->bpb.log2_bytes_per_sector)) {
VFipf_memcpy(p_buf, p_page->p_buf, num_sector << p_vol->bpb.log2_bytes_per_sector);
*p_success_size += num_sector << p_vol->bpb.log2_bytes_per_sector;
size -= num_sector << p_vol->bpb.log2_bytes_per_sector;
sector += num_sector;
} else if (num_sector > (size >> p_vol->bpb.log2_bytes_per_sector)) {
VFipf_memcpy(p_buf, p_page->p_buf, size);
*p_success_size += size;
size = 0;
}
}
if (size != 0) {
adjust_sector = 0;
num_sector = size >> p_vol->bpb.log2_bytes_per_sector;
rest_sector = (sector + num_sector) % p_vol->cache.data_buff_size;
if (num_sector > rest_sector) {
adjust_sector = num_sector - rest_sector;
err = VFiPFCACHE_ReadDataNumSector(p_vol, &p_buf[*p_success_size], sector, adjust_sector, &num_success);
if (err != 0) {
return err;
}
*p_success_size += num_success << p_vol->bpb.log2_bytes_per_sector;
if (num_success != adjust_sector) {
return 17;
}
sector += adjust_sector;
num_sector -= adjust_sector;
}
if (num_sector != 0) {
err = VFiPFCACHE_ReadDataPage(p_vol, sector, &p_page, set_sig);
if (err != 0) {
return err;
}
VFipf_memcpy(&p_buf[*p_success_size], p_page->p_buf, num_sector << p_vol->bpb.log2_bytes_per_sector);
*p_success_size += num_sector << p_vol->bpb.log2_bytes_per_sector;
}
}
} else {
return 10;
}
return 0;
}
pf_s32 VFiPFSEC_WriteFAT(PF_VOLUME* p_vol, const pf_u8* p_buf, pf_u32 sector, pf_u16 offset, pf_u16 size) {
PF_CACHE_PAGE* p_page;
pf_s32 err;
if ((p_vol->flags & 0x01) == 0 || !VFiPFDRV_IsInserted(p_vol) || (p_vol->flags & 2)) {
return 9;
}
if (sector >= p_vol->bpb.total_sectors || offset >= p_vol->bpb.bytes_per_sector || offset + size > p_vol->bpb.bytes_per_sector) {
return 10;
}
err = VFiPFCACHE_ReadFATPage(p_vol, sector, &p_page);
if (err != 0) {
return err;
}
VFipf_memcpy(&p_page->p_buf[offset], (pf_u8*)p_buf, size);
err = VFiPFCACHE_WriteFATPage(p_vol, p_page);
if (err != 0) {
return err;
}
return 0;
}
pf_s32 VFiPFSEC_WriteData(PF_VOLUME* p_vol, const pf_u8* p_buf, pf_u32 sector, pf_u16 offset, pf_u32 size, pf_u32* p_success_size, pf_bool set_sig) {
pf_s32 err;
PF_CACHE_PAGE* p_page;
pf_u32 num_success;
pf_u32 num_sector;
pf_u32 adjust_sector;
pf_u32 rest_sector;
*p_success_size = 0;
if ((p_vol->flags & 0x01) == 0 || !VFiPFDRV_IsInserted(p_vol) || (p_vol->flags & 2)) {
return 9;
}
if ((sector >= p_vol->bpb.total_sectors || offset >= p_vol->bpb.bytes_per_sector || offset + size > p_vol->bpb.bytes_per_sector) && offset != 0) {
return 10;
}
if (offset != 0 || size < p_vol->bpb.bytes_per_sector) {
err = VFiPFCACHE_ReadDataPageAndFlushIfNeeded(p_vol, sector, &p_page, set_sig);
if (err != 0) {
return err;
}
VFipf_memcpy(&p_page->p_buf[offset], (void*)p_buf, size);
err = VFiPFCACHE_WriteDataPage(p_vol, p_page, set_sig);
if (err != 0) {
return err;
}
*p_success_size = size;
} else if (offset == 0 && (size & (p_vol->bpb.bytes_per_sector - 1)) == 0) {
err = VFiPFCACHE_WriteDataNumSectorAndFreeIfNeeded(p_vol, p_buf, sector, size >> p_vol->bpb.log2_bytes_per_sector, &num_success);
if (err != 0) {
return err;
}
*p_success_size = num_success << p_vol->bpb.log2_bytes_per_sector;
if (num_success != (size >> p_vol->bpb.log2_bytes_per_sector)) {
return 17;
}
} else if (offset == 0) {
adjust_sector = 0;
num_sector = size >> p_vol->bpb.log2_bytes_per_sector;
rest_sector = (sector + num_sector) % p_vol->cache.data_buff_size;
if (num_sector > rest_sector) {
adjust_sector = num_sector - rest_sector;
err = VFiPFCACHE_WriteDataNumSectorAndFreeIfNeeded(p_vol, p_buf, sector, adjust_sector, &num_success);
if (err != 0) {
return err;
}
*p_success_size = num_success << p_vol->bpb.log2_bytes_per_sector;
if (num_success != adjust_sector) {
return 17;
}
sector += adjust_sector;
num_sector -= adjust_sector;
}
if (num_sector != 0) {
err = VFiPFCACHE_ReadDataPageAndFlushIfNeeded(p_vol, sector, &p_page, set_sig);
if (err != 0) {
return err;
}
VFipf_memcpy(p_page->p_buf, (pf_u8*)&p_buf[*p_success_size], num_sector << p_vol->bpb.log2_bytes_per_sector);
err = VFiPFCACHE_WriteDataPage(p_vol, p_page, set_sig);
if (err != 0) {
return err;
}
*p_success_size += num_sector << p_vol->bpb.log2_bytes_per_sector;
}
} else {
return 10;
}
return 0;
}

View File

@@ -1,24 +0,0 @@
#define VFiPF_GET_LE_U16 PF_GET_LE_U16
#define VFiPF_LE16_TO_U16_STR PF_LE16_TO_U16_STR
#include <private/vf/PrFILE2/pf_types.h>
pf_u16 VFiPF_GET_LE_U16(const pf_u8* buf) {
return (pf_u16)((pf_u16)buf[1] << 8 | (pf_u16)buf[0]);
}
void VFiPF_LE16_TO_U16_STR(pf_u8* sSrc, pf_u32 num) {
pf_u32 cnt;
pf_s8 tmp_src;
for (cnt = 0; cnt < num; cnt += sizeof(pf_u16)) {
pf_s8* p = (pf_s8*)sSrc + cnt;
tmp_src = *p;
#ifdef DEBUG
*((pf_u8*)sSrc + cnt) = *((pf_u8*)sSrc + cnt + 1);
*((pf_u8*)sSrc + cnt + 1) = tmp_src;
#else
sSrc[cnt] = sSrc[cnt + 1];
sSrc[cnt + 1] = tmp_src;
#endif
}
}

View File

@@ -1,229 +0,0 @@
#include <private/vf/PrFILE2/pf_types.h>
typedef struct PF_STR {
const pf_s8* p_head;
const pf_s8* p_tail;
const pf_s8* p_current;
pf_u32 code_mode;
} PF_STR;
typedef struct PFSTR_CHARCODE {
pf_s32 (*oem2unicode)(const pf_s8*, pf_u16*);
pf_s32 (*unicode2oem)(const pf_u16*, pf_s8*);
pf_s32 (*oem_char_width)(const pf_s8*);
pf_bool (*is_oem_mb_char)(pf_s8, pf_bool);
pf_s32 (*unicode_char_width)(const pf_u16*);
pf_bool (*is_unicode_mb_char)(pf_u16, pf_bool);
} PFSTR_CHARCODE;
typedef struct PFSTR_VOLUME_SET {
pf_u8 state_prefix[0x48];
PFSTR_CHARCODE codeset;
} PFSTR_VOLUME_SET;
extern PFSTR_VOLUME_SET pf_vol_set;
extern pf_s32 pf_toupper(pf_s32 c);
extern pf_u32 pf_strlen(const pf_s8* s);
extern pf_u32 pf_w_strlen(const pf_u16* s);
extern pf_s32 pf_strcmp(const pf_s8* s1, const pf_s8* s2);
extern pf_s32 pf_strncmp(const pf_s8* s1, const pf_s8* s2, pf_u32 length);
void PFSTR_SetCodeMode(PF_STR* p_str, pf_u32 code_mode) {
p_str->code_mode = code_mode;
}
pf_u32 PFSTR_GetCodeMode(PF_STR* p_str) {
return p_str->code_mode;
}
void PFSTR_SetLocalStr(PF_STR* p_str, const pf_s8* local_str) {
if (p_str->code_mode == 1 || local_str == PF_NULL) {
p_str->p_current = p_str->p_head;
} else {
p_str->p_current = local_str;
}
}
pf_s8* PFSTR_GetStrPos(PF_STR* p_str, pf_u32 target) {
pf_s8* p_pos;
if (target == 1) {
p_pos = (pf_s8*)p_str->p_head;
} else if (target == 2) {
p_pos = (pf_s8*)p_str->p_tail;
} else {
p_pos = (pf_s8*)p_str->p_current;
}
return p_pos;
}
void PFSTR_MoveStrPos(PF_STR* p_str, pf_s16 num_char) {
pf_s16 cnt;
pf_s16 offset = 0;
pf_s32 width;
pf_s8* p;
pf_u16* wp;
if (PFSTR_GetCodeMode(p_str) == 1) {
p = (pf_s8*)p_str->p_head;
while (num_char != 0) {
if (pf_vol_set.codeset.is_oem_mb_char((pf_s8)*p, PF_TRUE)) {
offset++;
}
offset++;
num_char--;
}
} else {
wp = (pf_u16*)p_str->p_head;
for (cnt = 0; cnt < num_char; cnt++) {
width = pf_vol_set.codeset.unicode_char_width(wp);
wp += width;
offset += (pf_s16)width;
}
}
p_str->p_head = &p_str->p_head[offset];
}
pf_s32 PFSTR_InitStr(PF_STR* p_str, const pf_s8* s, pf_u32 code_mode) {
if (code_mode == 1) {
p_str->p_head = s;
p_str->p_tail = &s[pf_strlen(s)];
} else if (code_mode == 2) {
p_str->p_head = s;
p_str->p_tail = s + (pf_w_strlen((pf_u16*)s) * sizeof(pf_u16));
}
PFSTR_SetCodeMode(p_str, code_mode);
return 0;
}
pf_u16 PFSTR_StrLen(PF_STR* p_str) {
return (pf_u16)(p_str->p_tail - p_str->p_head);
}
pf_u16 PFSTR_StrNumChar(PF_STR* p_str, pf_u32 target) {
pf_s8* p;
pf_u16 cnt;
pf_s32 width;
if (target == 1) {
p = (pf_s8*)p_str->p_head;
} else {
p = (pf_s8*)p_str->p_tail;
}
if (p_str->code_mode == 1) {
for (cnt = 0; (pf_s8)*p != 0; cnt++) {
if (pf_vol_set.codeset.is_oem_mb_char((pf_s8)*p, PF_TRUE)) {
p++;
}
p++;
}
} else {
for (cnt = 0; (pf_s8)p[0] != 0 || (pf_s8)p[1] != 0; cnt++) {
width = pf_vol_set.codeset.unicode_char_width((pf_u16*)p);
p += width;
}
}
return cnt;
}
pf_s32 PFSTR_StrCmp(const PF_STR* p_str, const pf_s8* s) {
pf_u16 wc;
const pf_u16* wp;
const pf_s8* p1;
const pf_s8* p2;
pf_s32 ret;
if (p_str->code_mode == 1) {
p1 = p_str->p_head;
p2 = s;
ret = pf_strcmp(p1, p2);
} else {
wp = (pf_u16*)p_str->p_head;
do {
pf_vol_set.codeset.oem2unicode((pf_s8*)s, &wc);
s++;
if (*wp++ != wc) {
break;
}
} while (*(wp - 1) != 0 && wc != 0);
wp--;
ret = *wp - wc;
}
return ret;
}
pf_s32 PFSTR_StrNCmp(PF_STR* p_str, const pf_s8* s, pf_u32 target, pf_s16 offset, pf_u16 num) {
pf_u16 wc;
const pf_u16* wp;
const pf_s8* p1;
const pf_s8* p2;
pf_s32 ret;
if (p_str->code_mode == 1 || target == 3) {
if (target == 1) {
p1 = &p_str->p_head[offset];
} else if (target == 2) {
p1 = &p_str->p_tail[offset];
} else {
p1 = &p_str->p_current[offset];
}
p2 = s;
ret = pf_strncmp(p1, p2, num);
} else {
if (target == 1) {
wp = (pf_u16*)p_str->p_head + offset;
} else {
wp = (pf_u16*)p_str->p_tail + offset;
}
do {
pf_vol_set.codeset.oem2unicode((pf_s8*)s, &wc);
s++;
num--;
if (*wp++ != wc || num == 0) {
break;
}
} while (*(wp - 1) != 0 && wc != 0);
wp--;
ret = *wp - wc;
}
return ret;
}
void PFSTR_ToUpperNStr(PF_STR* p_str, pf_u16 num, pf_s8* dest) {
pf_u16 wc;
const pf_u16* wp;
const pf_s8* p;
if (p_str->code_mode == 1) {
p = (pf_s8*)p_str->p_head;
for (; num != 0 && *p != 0; p++, num--) {
*dest++ = pf_toupper(*p);
}
} else {
wp = (pf_u16*)p_str->p_head;
for (; num != 0 && *wp != 0; wp++, num--) {
wc = (*wp >= 'a' && *wp <= 'z') ? *wp + ('A' - 'a') : *wp;
*(dest) = (pf_u8)wc;
*(dest + 1) = (pf_u8)(wc >> 8);
dest += sizeof(pf_u16);
}
*dest = 0;
}
*dest = 0;
}

View File

@@ -1,13 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFAPI_convertReturnValue PFAPI_convertReturnValue
#define VFiPFVOL_sync PFVOL_sync
#include <private/vf/PrFILE2/standard/pf_sync.h>
#include <private/vf/PrFILE2/fatfs/pf_volume.h>
#include <private/vf/PrFILE2/standard/pf_api_util.h>
pf_int32 pf2_sync(pf_ch8 drv_char, pf_s32 mode) {
pf_s32 err = VFiPFVOL_sync(drv_char, mode);
err = VFiPFAPI_convertReturnValue(err);
return err;
}

View File

@@ -1,30 +0,0 @@
#include <private/fa/pf_system.h>
typedef struct PFSYS_DATE { u32 sys_year, sys_month, sys_day; } PFSYS_DATE;
typedef struct PFSYS_TIME { u32 sys_hour, sys_min, sys_sec, sys_ms; } PFSYS_TIME;
typedef struct PFSYS_VOLUME_SET {
u8 volume_state[0x64];
s32 context_id;
} PFSYS_VOLUME_SET;
extern PFSYS_VOLUME_SET pf_vol_set;
extern void pfstub_init_stub(void);
PFSYS_SYSTEM_SET pf_sys_set;
void PFSYS_initializeSYS(void) {
pfstub_init_stub();
}
s32 PFSYS_GetCurrentContextID(s32* context_id) {
*context_id = pf_vol_set.context_id;
return 0;
}
void PFSYS_TimeStamp(PFSYS_DATE* sdate, PFSYS_TIME* stime) {
OSCalendarTime calendar;
OSTicksToCalendarTime(OSGetTime(), &calendar);
sdate->sys_year = calendar.year;
sdate->sys_month = calendar.mon + 1;
sdate->sys_day = calendar.mday;
stime->sys_hour = calendar.hour;
stime->sys_min = calendar.min;
stime->sys_sec = calendar.sec;
stime->sys_ms = 1;
}

View File

@@ -1,13 +0,0 @@
#define PF_FA_STR_LAYOUT
#define VFiPFAPI_convertReturnValue4unmount PFAPI_convertReturnValue4unmount
#define VFiPFVOL_unmount PFVOL_unmount
#include <private/vf/PrFILE2/standard/pf_unmount.h>
#include <private/vf/PrFILE2/fatfs/pf_volume.h>
#include <private/vf/PrFILE2/standard/pf_api_util.h>
pf_int32 pf2_unmount(pf_ch8 drive, pf_u32 mode) {
pf_s32 err = VFiPFVOL_unmount(drive, mode);
err = VFiPFAPI_convertReturnValue4unmount(err);
return err;
}

View File

@@ -21,9 +21,7 @@ typedef struct NWC24MsgObjPrivate {
u32 type; // 0x04
u32 length; // 0x08
u32 appId; // 0x0C
#ifdef NWC24_MSG_READ
u32 headerSize;
#elif defined(NWC24_MBOX_CTRL) || defined(NWC24_MSG_COMMIT)
#if defined(NWC24_MBOX_CTRL) || defined(NWC24_MSG_COMMIT)
u32 unk_0x10;
#else
u8 unk_0x10[0x4];

View File

@@ -7,9 +7,6 @@
typedef struct PF_STR {
const pf_s8* p_head; // 0x00
const pf_s8* p_tail; // 0x04
#ifdef PF_FA_STR_LAYOUT
const pf_s8* p_current;
#endif
pf_u32 code_mode; // 0x08
} PF_STR;

View File

@@ -128,19 +128,7 @@ static const pf_u16 cp932_to_unicode[45][189] = {
0x6A5F, 0x5E30, 0x6BC5, 0x6C17, 0x6C7D, 0x757F, 0x7948, 0x5B63, 0x7A00, 0x7D00, 0x5FBD, 0x898F, 0x8A18, 0x8CB4, 0x8D77, 0x8ECC,
0x8F1D, 0x98E2, 0x9A0E, 0x9B3C, 0x4E80, 0x507D, 0x5100, 0x5993, 0x5B9C, 0x622F, 0x6280, 0x64EC, 0x6B3A, 0x72A0, 0x7591, 0x7947,
0x7FA9, 0x87FB, 0x8ABC, 0x8B70, 0x63AC, 0x83CA, 0x97A0, 0x5409, 0x5403, 0x55AB, 0x6854, 0x6A58, 0x8A70, 0x7827, 0x6775, 0x9ECD,
0x5374, 0x5BA2,
#ifdef PF_FA_CP932_TABLE
0x0000
#else
0x811A
#endif
,
#ifdef PF_FA_CP932_TABLE
0x0000
#else
0x8650
#endif
, 0x9006, 0x4E18, 0x4E45, 0x4EC7, 0x4F11, 0x53CA, 0x5438, 0x5BAE, 0x5F13, 0x6025, 0x6551, 0x0000,
0x5374, 0x5BA2, 0x811A, 0x8650, 0x9006, 0x4E18, 0x4E45, 0x4EC7, 0x4F11, 0x53CA, 0x5438, 0x5BAE, 0x5F13, 0x6025, 0x6551, 0x0000,
0x673D, 0x6C42, 0x6C72, 0x6CE3, 0x7078, 0x7403, 0x7A76, 0x7AAE, 0x7B08, 0x7D1A, 0x7CFE, 0x7D66, 0x65E7, 0x725B, 0x53BB, 0x5C45,
0x5DE8, 0x62D2, 0x62E0, 0x6319, 0x6E20, 0x865A, 0x8A31, 0x8DDD, 0x92F8, 0x6F01, 0x79A6, 0x9B5A, 0x4EA8, 0x4EAB, 0x4EAC, 0x4F9B,
0x4FA0, 0x50D1, 0x5147, 0x7AF6, 0x5171, 0x51F6, 0x5354, 0x5321, 0x537F, 0x53EB, 0x55AC, 0x5883, 0x5CE1, 0x5F37, 0x5F4A, 0x602F,
@@ -248,20 +236,8 @@ static const pf_u16 cp932_to_unicode[45][189] = {
0x6065, 0x667A, 0x6C60, 0x75F4, 0x7A1A, 0x7F6E, 0x81F4, 0x8718, 0x9045, 0x99B3, 0x7BC9, 0x755C, 0x7AF9, 0x7B51, 0x84C4, 0x0000,
0x9010, 0x79E9, 0x7A92, 0x8336, 0x5AE1, 0x7740, 0x4E2D, 0x4EF2, 0x5B99, 0x5FE0, 0x62BD, 0x663C, 0x67F1, 0x6CE8, 0x866B, 0x8877,
0x8A3B, 0x914E, 0x92F3, 0x99D0, 0x6A17, 0x7026, 0x732A, 0x82E7, 0x8457, 0x8CAF, 0x4E01, 0x5146, 0x51CB, 0x558B, 0x5BF5, 0x5E16,
0x5E33, 0x5E81, 0x5F14, 0x5F35, 0x5F6B, 0x5FB4, 0x61F2, 0x6311, 0x66A2, 0x671D, 0x6F6E, 0x7252, 0x753A, 0x773A, 0x8074,
#ifdef PF_FA_CP932_TABLE
0x0000
#else
0x8139
#endif
,
#ifdef PF_FA_CP932_TABLE
0x0000
#else
0x8178
#endif
, 0x8776, 0x8ABF, 0x8ADC, 0x8D85, 0x8DF3, 0x929A, 0x9577, 0x9802, 0x9CE5, 0x52C5, 0x6357, 0x76F4, 0x6715, 0x6C88, 0x73CD,
0x5E33, 0x5E81, 0x5F14, 0x5F35, 0x5F6B, 0x5FB4, 0x61F2, 0x6311, 0x66A2, 0x671D, 0x6F6E, 0x7252, 0x753A, 0x773A, 0x8074, 0x8139,
0x8178, 0x8776, 0x8ABF, 0x8ADC, 0x8D85, 0x8DF3, 0x929A, 0x9577, 0x9802, 0x9CE5, 0x52C5, 0x6357, 0x76F4, 0x6715, 0x6C88, 0x73CD,
0x8CC3, 0x93AE, 0x9673, 0x6D25, 0x589C, 0x690E, 0x69CC, 0x8FFD, 0x939A, 0x75DB, 0x901A, 0x585A, 0x6802, 0x63B4, 0x69FB, 0x4F43,
0x6F2C, 0x67D8, 0x8FBB, 0x8526, 0x7DB4, 0x9354, 0x693F, 0x6F70, 0x576A, 0x58F7, 0x5B2C, 0x7D2C, 0x722A, 0x540A, 0x91E3, 0x9DB4,
0x4EAD, 0x4F4E, 0x505C, 0x5075, 0x5243, 0x8C9E, 0x5448, 0x5824, 0x5B9A, 0x5E1D, 0x5E95, 0x5EAD, 0x5EF7, 0x5F1F, 0x608C, 0x62B5,
@@ -524,19 +500,7 @@ static const pf_u16 cp932_to_unicode[45][189] = {
},
{
0x968B, 0x8146, 0x813E, 0x8153, 0x8151, 0x80FC, 0x8171, 0x816E, 0x8165,
#ifdef PF_FA_CP932_TABLE
0x0000
#else
0x8166
#endif
,
#ifdef PF_FA_CP932_TABLE
0x0000
#else
0x8174
#endif
, 0x8183, 0x8188, 0x818A, 0x8180, 0x8182,
0x968B, 0x8146, 0x813E, 0x8153, 0x8151, 0x80FC, 0x8171, 0x816E, 0x8165, 0x8166, 0x8174, 0x8183, 0x8188, 0x818A, 0x8180, 0x8182,
0x81A0, 0x8195, 0x81A4, 0x81A3, 0x815F, 0x8193, 0x81A9, 0x81B0, 0x81B5, 0x81BE, 0x81B8, 0x81BD, 0x81C0, 0x81C2, 0x81BA, 0x81C9,
0x81CD, 0x81D1, 0x81D9, 0x81D8, 0x81C8, 0x81DA, 0x81DF, 0x81E0, 0x81E7, 0x81FA, 0x81FB, 0x81FE, 0x8201, 0x8202, 0x8205, 0x8207,
0x820A, 0x820D, 0x8210, 0x8216, 0x8229, 0x822B, 0x8238, 0x8233, 0x8240, 0x8259, 0x8258, 0x825D, 0x825A, 0x825F, 0x8264, 0x0000,

View File

@@ -104,12 +104,8 @@ NWC24Err NWC24ReadMsgMBOptOutFlag(const NWC24MsgObj* msg, BOOL* mbOptOutFlag, u3
NWC24Err NWC24ReadMsgFromAddr(const NWC24MsgObj* msg, char* addr, u32 addrLen);
NWC24Err NWC24ReadMsgSubject(const NWC24MsgObj* msg, char* subject, u32 subjectLen);
NWC24Err NWC24ReadMsgText(const NWC24MsgObj* msg, char* text, u32 textLen, NWC24Charset* charset, NWC24Encoding* encoding);
#if defined(NWC24_MSG_SUBJECT) || defined(NWC24_MSG_READ)
NWC24Err NWC24ReadMsgTextEx(const NWC24MsgObj* msg, char* text, u32 textLen, char* name, u32 nameLen);
#else
NWC24Err NWC24ReadMsgTextEx(const NWC24MsgObj* msg, char* text, u32 textLen, const char* name, u32 nameLen, NWC24Charset* charset,
NWC24Encoding* encoding);
#endif
NWC24Err NWC24ReadMsgAttached(const NWC24MsgObj* msg, u32 attachIndex, u8* attachData, u32 attachSize);
NWC24Err NWC24ReadMsgSubjectPublic(const NWC24MsgObj* msg, u16* subject, u32* subjectLen, NWC24EncodingRegion encRegion, u16 unk, u8* work,

View File

@@ -1,236 +0,0 @@
#include <private/nwc24/NWC24Utils.h>
static const u8 DAYS_OF_MONTH[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static const u16 DAYS_OF_YEAR[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
s32 ConvertDateToDays(u16 year, u8 month, u8 day);
void ConvertDaysToDate(u16* year, u8* month, u8* day, s32 days);
static inline BOOL IsLeapYear(u16 year) {
BOOL leap = FALSE;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
leap = TRUE;
return leap;
}
NWC24Err NWC24iDateToMinutes(s32* minutes, const NWC24Date* date) {
s32 days;
s32 minutesInDay;
NWC24Err result;
u8 hour;
u8 minute;
days = ConvertDateToDays(date->year, date->month, date->day);
if (days == -1) {
result = NWC24_ERR_FAILED;
} else {
hour = date->hour;
minute = date->min;
if (hour > 23 || minute > 59) {
minutesInDay = -1;
} else {
minutesInDay = minute + hour * 60;
}
if (minutesInDay == -1 || date->sec > 60) {
result = NWC24_ERR_FAILED;
} else {
result = NWC24_OK;
*minutes = minutesInDay + days * 1440;
}
}
return result;
}
NWC24Err NWC24iMinutesToDate(NWC24Date* date, s32 minutes) {
s32 days;
if (minutes < 0) {
minutes = 0;
}
days = minutes / 1440;
minutes %= 1440;
date->hour = minutes / 60;
date->min = minutes % 60;
ConvertDaysToDate(&date->year, &date->month, &date->day, days);
return NWC24_OK;
}
NWC24Err NWC24iEpochSecondsToDate(NWC24Date* date, s64 seconds) {
s64 elapsedMinutes;
s32 minutes;
s32 days;
if (seconds + 2208988800LL < 0) {
seconds = -2208988800LL;
}
date->sec = (seconds + 2208988800LL) % 60;
elapsedMinutes = (seconds + 2208988800LL) / 60;
if ((s32)elapsedMinutes < 0) {
elapsedMinutes = 0;
}
minutes = elapsedMinutes;
days = minutes / 1440;
minutes %= 1440;
date->hour = minutes / 60;
date->min = minutes % 60;
ConvertDaysToDate(&date->year, &date->month, &date->day, days);
return NWC24_OK;
}
NWC24Err NWC24iMinutesToOSCalendarTime(OSCalendarTime* calendar, s32 minutes) {
s32 days;
NWC24Date date;
BOOL isLeapYear;
if (minutes < 0) {
minutes = 0;
}
days = minutes / 1440;
minutes %= 1440;
date.hour = minutes / 60;
date.min = minutes % 60;
ConvertDaysToDate(&date.year, &date.month, &date.day, days);
isLeapYear = 0;
calendar->year = date.year;
calendar->mon = date.month - 1;
calendar->mday = date.day;
calendar->hour = date.hour;
calendar->min = date.min;
calendar->sec = 0;
calendar->msec = 0;
calendar->usec = 0;
calendar->yday = date.day + DAYS_OF_YEAR[date.month - 1] - 1;
if (((date.year % 4 == 0 && date.year % 100 != 0) || date.year % 400 == 0)) {
isLeapYear = 1;
}
if (isLeapYear && date.month > 2) {
calendar->yday++;
}
calendar->wday = (days + 1) % 7;
return NWC24_OK;
}
NWC24Err NWC24iDateToOSCalendarTime(OSCalendarTime* calendar, const NWC24Date* date) {
BOOL isLeapYear;
s32 days;
isLeapYear = 0;
calendar->year = date->year;
calendar->mon = date->month - 1;
calendar->mday = date->day;
calendar->hour = date->hour;
calendar->min = date->min;
calendar->sec = date->sec;
calendar->msec = 0;
calendar->usec = 0;
calendar->yday = date->day + DAYS_OF_YEAR[date->month - 1] - 1;
if (((date->year % 4 == 0 && date->year % 100 != 0) || date->year % 400 == 0)) {
isLeapYear = 1;
}
if (isLeapYear && date->month > 2) {
calendar->yday++;
}
days = ConvertDateToDays(date->year, date->month, date->day);
calendar->wday = (days + 1) % 7;
return NWC24_OK;
}
NWC24Err NWC24iIsValidDate(u16 year, u8 month, u8 day) {
return ConvertDateToDays(year, month, day) == -1 ? NWC24_ERR_INVALID_VALUE : NWC24_OK;
}
s32 ConvertDateToDays(u16 year, u8 month, u8 day) {
s32 daysOfYear;
s32 yearOffset;
s32 centuryLeapDays;
s32 commonLeapDays;
BOOL isLeapYear;
if (year < 1900 || month < 1 || month > 12) {
return -1;
}
if (month == 2) {
isLeapYear = 0;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
isLeapYear = 1;
}
if (isLeapYear) {
if (day < 1 || day > 29) {
return -1;
}
goto validDate;
}
}
if (day < 1 || DAYS_OF_MONTH[month - 1] < day) {
return -1;
}
validDate:
daysOfYear = day - 1 + DAYS_OF_YEAR[month - 1];
if (month >= 3) {
isLeapYear = 0;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
isLeapYear = 1;
}
if (isLeapYear) {
daysOfYear++;
}
}
yearOffset = year - 1900;
centuryLeapDays = (yearOffset + 299) / 400;
daysOfYear += yearOffset * 365;
daysOfYear += centuryLeapDays;
commonLeapDays = (yearOffset - 1) / 4 - (yearOffset - 1) / 100;
return daysOfYear + commonLeapDays;
}
void ConvertDaysToDate(u16* year, u8* month, u8* day, s32 days) {
*year = 1900;
*month = 1;
*day = 1;
if (days < 0) {
return;
}
for (;;) {
u16 currentYear = *year;
s32 previousDays = days;
BOOL leapYear = (currentYear % 4 == 0 && currentYear % 100 != 0) || currentYear % 400 == 0;
days -= 365 + (leapYear != FALSE);
if (days < 0) {
days = previousDays;
break;
}
*year = *year + 1;
}
for (;;) {
s32 previousDays = days;
if (*month == 2) {
BOOL leapYear = IsLeapYear(*year);
if (leapYear) {
days -= 29;
} else {
days -= DAYS_OF_MONTH[*month - 1];
}
} else {
days -= DAYS_OF_MONTH[*month - 1];
}
if (days < 0) {
*day += previousDays;
return;
}
*month = *month + 1;
}
}

View File

@@ -1,767 +0,0 @@
#include <private/nwc24.h>
#include <revolution/nwc24.h>
static const char* MIMEEncStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
static NWC24Err Base64Encode(u8* decoded, int decodedSize, u32* stringSize, u8* encoded, int encodedSize, u32* encodedSizeOut) {
NWC24Err result = NWC24_OK;
s32 textIdx;
s32 dataIdx = 0;
BOOL hasThird;
BOOL hasSecond;
u32 combined;
for (textIdx = 0; dataIdx < decodedSize; dataIdx += 3) {
if (textIdx + 3 >= encodedSize) {
result = NWC24_ERR_OVERFLOW;
break;
}
hasThird = FALSE;
hasSecond = FALSE;
combined = (u32)decoded[dataIdx] << 8;
if (dataIdx + 1 < decodedSize) {
hasSecond = TRUE;
combined |= (u8)decoded[dataIdx + 1];
}
combined <<= 8;
if (dataIdx + 2 < decodedSize) {
hasThird = TRUE;
combined |= (u8)decoded[dataIdx + 2];
}
encoded[textIdx + 0] = MIMEEncStr[combined >> 18 & (64 - 1)];
encoded[textIdx + 1] = MIMEEncStr[combined >> 12 & (64 - 1)];
encoded[textIdx + 2] = MIMEEncStr[hasSecond ? (combined >> 6 & (64 - 1)) : 64];
encoded[textIdx + 3] = MIMEEncStr[hasThird ? (combined & (64 - 1)) : 64];
textIdx += 4;
}
*encodedSizeOut = textIdx;
if (stringSize != NULL) {
textIdx = decodedSize;
if (decodedSize < dataIdx) {
dataIdx = decodedSize;
}
*stringSize = dataIdx;
}
return result;
}
NWC24Err NWC24Base64Encode(u8* decoded, int decodedSize, u8* encoded, int encodedSize, u32* stringSize) {
return Base64Encode(decoded, decodedSize, NULL, encoded, encodedSize, stringSize);
}
NWC24Err NWC24Base64Decode(u8* encoded, int encodedSize, u8* decoded, int decodedSize, u32* stringSize) {
int bitCount = 0;
u32 bitBuffer = 0;
u32 decodedOffset = 0;
s8* table = (s8*)NWC24WorkP->base64Work;
BOOL produceOutput;
int i;
for (i = 0; i < encodedSize; i++) {
if ((s32)table[encoded[i]] >= 0) {
if ((s32)table[encoded[i]] < 0x40) {
bitCount += 6;
bitBuffer <<= 6;
produceOutput = bitCount >= 8;
bitBuffer |= (u32)table[encoded[i]];
if (produceOutput) {
if (decodedOffset >= (u32)decodedSize) {
*stringSize = decodedSize;
return NWC24_ERR_OVERFLOW;
}
bitCount -= 8;
*decoded++ = (u8)((s32)bitBuffer >> bitCount);
decodedOffset++;
}
}
}
}
*stringSize = decodedOffset;
return NWC24_OK;
}
void NWC24InitBase64Table(u8* table) {
s8* signedTable = (s8*)table;
int i;
for (i = 0; i < 256; i++) {
signedTable[i] = -1;
}
for (i = 'A'; i <= 'Z'; i++) {
table[i] = i - 'A';
}
for (i = 0; i < 26; i++) {
table['a' + i] = i + 26;
}
for (i = 0; i < 10; i++) {
table['0' + i] = i + 52;
}
table['+'] = 62;
table['/'] = 63;
}
static NWC24Err QEncode(char* encoded, u32 encodedCapacity, u32* encodedSize, u8* decoded, u32 decodedSize, u32* decodedSizeOut, s32 maxLineLength) {
char* output;
u32 textWritten;
u32 dataIdx;
NWC24Err result;
s32 lineLength;
u8 octets[3];
BOOL lineBreak;
s32 emitCount;
s32 bytesRead;
s16 currentByte;
s32 i;
result = NWC24_OK;
if (encoded == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
if (decoded == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
if (decodedSize <= 0) {
return NWC24_ERR_OVERFLOW;
}
if (decodedSizeOut != NULL) {
*decodedSizeOut = 0;
}
if (encodedSize != NULL) {
*encodedSize = 0;
}
output = encoded;
lineLength = 0;
textWritten = 0;
dataIdx = 0;
while (dataIdx < decodedSize) {
currentByte = *decoded;
lineBreak = FALSE;
if ((char)currentByte >= '!' && (char)currentByte <= '~' && (char)currentByte != '=') {
bytesRead = 1;
emitCount = 1;
octets[0] = *decoded;
} else if (dataIdx + 1 < decodedSize && (char)currentByte == '\r' && (char)decoded[1] == '\n') {
bytesRead = 2;
emitCount = 1;
octets[0] = *decoded;
octets[1] = decoded[1];
} else {
octets[0] = '=';
if ((*decoded >> 4) >= 10) {
octets[1] = (*decoded >> 4) % 10 + 'A';
} else {
octets[1] = (*decoded >> 4) % 10 + '0';
}
if ((*decoded & 0x0F) >= 10) {
octets[2] = (*decoded & 0x0F) % 10 + 'A';
} else {
octets[2] = (*decoded & 0x0F) % 10 + '0';
}
bytesRead = 1;
emitCount = 3;
}
if (maxLineLength == 0 && lineLength + emitCount >= 75) {
lineBreak = TRUE;
emitCount += 3;
}
if (textWritten + emitCount >= encodedCapacity) {
result = NWC24_ERR_OVERFLOW;
break;
}
textWritten += emitCount;
if (lineBreak) {
output[0] = '=';
output[1] = '\r';
output[2] = '\n';
output += 3;
emitCount -= 3;
lineLength = 0;
}
lineLength += emitCount;
for (i = 0; i < emitCount; i++) {
*(output++) = octets[i];
}
dataIdx += bytesRead;
decoded += bytesRead;
}
if (decodedSizeOut != NULL) {
*decodedSizeOut = dataIdx;
}
if (encodedSize != NULL) {
*encodedSize = textWritten;
}
return result;
}
static NWC24Err QDecode(char* decoded, u32 decodedCapacity, u32* decodedSize, char* encoded, u32 encodedSize, u32* encodedSizeOut, int flags) {
u32 decodedOffset = 0;
u32 encodedOffset;
char* input;
char* output;
u32 value = 0;
NWC24Err result = NWC24_OK;
if (decoded == NULL)
return NWC24_ERR_INVALID_VALUE;
if (encoded == NULL)
return NWC24_ERR_INVALID_VALUE;
if (decodedCapacity == 0) {
return NWC24_ERR_OVERFLOW;
}
if (decodedSize != NULL) {
*decodedSize = 0;
}
if (encodedSizeOut != NULL) {
*encodedSizeOut = 0;
}
input = encoded;
output = decoded;
for (encodedOffset = 0; encodedOffset < encodedSize; encodedOffset++) {
if (decodedCapacity <= decodedOffset) {
result = NWC24_ERR_OVERFLOW;
break;
}
if (flags != 0 && *input == '_') {
*output++ = ' ';
input++;
decodedOffset++;
} else if (*input == '=' && encodedOffset + 2 < encodedSize) {
u32 inputAfterEqual = encodedOffset + 1;
char* current = ++input;
if (*current == ' ' || *current == '\t') {
char* previous = current;
u32 scan = 1;
while (inputAfterEqual + scan + 1 < encodedSize) {
char* next = previous + 1;
if (*next == '\r' && previous[2] == '\n') {
inputAfterEqual += scan;
current = next;
break;
}
if (*next != ' ' && *next != '\t') {
break;
}
previous = next;
scan++;
}
}
if (*current == '\r' && current[1] == '\n') {
current += 2;
encodedOffset = inputAfterEqual + 1;
input = current;
} else {
int high = Util_xtoi(*current);
int low;
if (high >= 0) {
value = (high & 0x0F) << 4;
}
low = Util_xtoi(current[1]);
if (low >= 0) {
value |= low & 0x0F;
}
current += 2;
if (high >= 0 && low >= 0) {
*output++ = (char)value;
decodedOffset++;
}
encodedOffset = inputAfterEqual + 1;
input = current;
}
} else {
*output++ = *input++;
decodedOffset++;
}
}
if (decodedSize != NULL) {
*decodedSize = decodedOffset;
}
if (encodedSizeOut != NULL) {
*encodedSizeOut = encodedOffset;
}
return result;
}
NWC24Err NWC24EncodeQuotedPrintable(char* encoded, u32 encodedCapacity, u32* encodedSize, u8* decoded, u32 decodedSize, u32* decodedSizeOut) {
return QEncode(encoded, encodedCapacity, encodedSize, decoded, decodedSize, decodedSizeOut, 0);
}
NWC24Err NWC24DecodeQuotedPrintable(char* decoded, u32 decodedCapacity, u32* decodedSize, u8* encoded, u32 encodedSize, u32* encodedSizeOut) {
return QDecode(decoded, decodedCapacity, decodedSize, (char*)encoded, encodedSize, encodedSizeOut, 0);
}
static NWC24Err ConcatEncodedText(char* encoded, u32 encodedCapacity, u32* encodedSize, char encoding, u8* decoded, u32 decodedSize,
u32* decodedSizeOut);
static NWC24Err EncodeWord(char* encoded, u32 encodedCapacity, u32* encodedSize, char* charset, u32 charsetSize, char encoding, u8* decoded,
u32 decodedSize, u32* decodedSizeOut) {
NWC24Err result = NWC24_OK;
BOOL encodeOverflow = FALSE;
if (decodedSizeOut != NULL) {
*decodedSizeOut = 0;
}
if (encodedCapacity == 0) {
if (decodedSizeOut != NULL) {
*decodedSizeOut = 0;
}
return NWC24_ERR_INVALID_VALUE;
}
*encoded = '\0';
if (encodedSize != NULL) {
*encodedSize = 1;
}
if (Mail_strlen(encoded) + 2 < encodedCapacity) {
Mail_strncat(encoded, "=?", 2);
} else {
result = NWC24_ERR_OVERFLOW;
}
if (result == NWC24_OK) {
if (charset == NULL) {
result = NWC24_ERR_INVALID_VALUE;
} else if (charsetSize + Mail_strlen(encoded) < encodedCapacity) {
u32 length = Mail_strlen(charset);
Mail_strncat(encoded, charset, length);
} else {
result = NWC24_ERR_OVERFLOW;
}
}
if (result == NWC24_OK) {
if (Mail_strlen(encoded) + 2 < encodedCapacity) {
Mail_strncat(encoded, "?", 1);
} else {
result = NWC24_ERR_OVERFLOW;
}
}
if (result == NWC24_OK) {
char value[1];
value[0] = encoding;
if (encoding == 'B' || encoding == 'b' || encoding == 'Q' || encoding == 'q') {
u32 length = Mail_strlen(encoded);
if (length + 1 < encodedCapacity) {
Mail_strncat(encoded, value, 1);
} else {
result = NWC24_ERR_OVERFLOW;
}
} else {
result = NWC24_ERR_INVALID_VALUE;
}
}
if (result == NWC24_OK) {
if (Mail_strlen(encoded) + 2 < encodedCapacity) {
Mail_strncat(encoded, "?", 1);
} else {
result = NWC24_ERR_OVERFLOW;
}
}
if (result == NWC24_OK) {
result = ConcatEncodedText(encoded, encodedCapacity, encodedSize, encoding, decoded, decodedSize, decodedSizeOut);
if (result == NWC24_OK || result == NWC24_ERR_OVERFLOW) {
encodeOverflow = result == NWC24_ERR_OVERFLOW;
if (Mail_strlen(encoded) + 2 < encodedCapacity) {
Mail_strncat(encoded, "?=", 2);
result = NWC24_OK;
if (encodedSize != NULL) {
*encodedSize = Mail_strlen(encoded) + 1;
}
if (encodeOverflow) {
result = NWC24_ERR_OVERFLOW;
}
} else {
*encoded = '\0';
if (decodedSizeOut != NULL) {
*decodedSizeOut = 1;
}
result = NWC24_ERR_OVERFLOW;
}
} else {
*encoded = '\0';
if (decodedSizeOut != NULL) {
*decodedSizeOut = 1;
}
}
} else {
*encoded = '\0';
}
return result;
}
NWC24Err NWC24EncodeWord(u8* encoded, u32 encodedCapacity, u32* encodedSize, char* charset, u32 charsetSize, char encoding, u8* decoded,
u32 decodedSize) {
NWC24Err result;
if (encodedSize == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
if (encoded == NULL || encodedCapacity == 0) {
return NWC24_ERR_INVALID_VALUE;
}
if (charset == NULL || charsetSize == 0) {
return NWC24_ERR_INVALID_VALUE;
}
if (encoding == '\0') {
return NWC24_ERR_INVALID_VALUE;
}
if (decoded == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
*encoded = 0;
*encodedSize = 1;
result = EncodeWord((char*)encoded, encodedCapacity, encodedSize, charset, charsetSize, encoding, decoded, decodedSize, NULL);
if (result == NWC24_OK || result == NWC24_ERR_OVERFLOW) {
encoded[*encodedSize - 1] = 0;
}
return result;
}
static NWC24Err ExtractCharset(char* charset, u32 charsetCapacity, u32* charsetSize, char* encoded, u32 encodedSize);
static NWC24Err ExtractEncodedText(char* decoded, u32 decodedCapacity, u32* decodedSize, char encoding, char* encoded, u32 encodedSize,
u32* encodedSizeOut);
static BOOL CopyWithoutLinearWhiteSpaces(char* output, int* outputSize, char* input, int inputSize) {
BOOL afterNewline = FALSE;
u32 outputOffset = 0;
s32 capacity;
if (output == NULL || outputSize == NULL || *outputSize == 0) {
return FALSE;
}
capacity = *outputSize;
if (input == NULL) {
*output = '\0';
*outputSize = 1;
return TRUE;
}
while (outputOffset < capacity - 1U && inputSize != 0 && *input != '\0') {
char value = *input;
switch (value) {
case '\r':
case '\n':
if (!afterNewline) {
*output++ = ' ';
outputOffset++;
}
afterNewline = TRUE;
break;
case '\t':
case ' ':
if (!afterNewline) {
*output++ = value;
outputOffset++;
}
break;
default:
*output = value;
afterNewline = FALSE;
output++;
outputOffset++;
break;
}
input++;
inputSize--;
}
*output = '\0';
*outputSize = outputOffset + 1;
return inputSize == 0 || *input == '\0';
}
static inline char* FindMarker(char* input, u32 size, const char* marker) {
u32 markerLength = Mail_strlen(marker);
u32 offset;
for (offset = 0; offset <= size; offset++) {
char* current = input + offset;
if (Mail_strncmp(current, marker, markerLength) == 0)
return current;
}
return NULL;
}
static NWC24Err DecodeWord(char* charsetData, u32 charsetCapacity, char* decoded, u32 decodedCapacity, u32* decodedSize, char* encoded,
u32 encodedSize, u32* encodedSizeOut) {
int encodedWordPosition = 0;
int encodedLength;
u32 consumedSize;
u32 decodedLength;
NWC24Err result = NWC24_OK;
if (encodedSizeOut != NULL) {
*encodedSizeOut = 0;
}
if (decodedCapacity == 0) {
if (encodedSizeOut != NULL)
*encodedSizeOut = 0;
return NWC24_ERR_INVALID_VALUE;
}
*decoded = '\0';
if (decodedSize != NULL) {
*decodedSize = 1;
}
if (encodedSizeOut != NULL) {
*encodedSizeOut = 0;
}
{
u32 markerLength = Mail_strlen("=?");
u32 offset;
for (offset = 0; offset <= encodedSize; offset++) {
encodedWordPosition = (int)(encoded + offset);
if (Mail_strncmp((char*)encodedWordPosition, "=?", markerLength) == 0) {
break;
}
}
if (offset > encodedSize) {
encodedWordPosition = 0;
}
}
if (encodedWordPosition == (int)encoded) {
result = ExtractCharset(charsetData, charsetCapacity, &consumedSize, (char*)encoded, encodedSize);
if (result != NWC24_OK) {
return result;
}
encodedLength = consumedSize;
{
u32 markerLength = Mail_strlen("?");
u32 offset;
char* delimiter = NULL;
char* current = (char*)encodedWordPosition + encodedLength;
for (offset = 0; offset <= encodedSize - encodedLength; offset++) {
if (Mail_strncmp((char*)current, "?", markerLength) == 0) {
delimiter = current;
break;
}
current++;
}
if (delimiter == 0) {
return NWC24_ERR_INVALID_VALUE;
}
{
char encoding = delimiter[1];
consumedSize = 2;
encodedLength += consumedSize;
result = ExtractEncodedText(decoded, decodedCapacity, &decodedLength, encoding, encoded + encodedLength, encodedSize - encodedLength,
&consumedSize);
if (result != NWC24_OK) {
return result;
}
encodedLength += consumedSize;
}
}
} else {
u32 localSize = decodedCapacity;
if (encodedWordPosition == 0) {
encodedLength = STD_strnlen(encoded, encodedSize);
} else {
encodedLength = encodedWordPosition - (int)encoded;
}
if (!CopyWithoutLinearWhiteSpaces(decoded, (int*)&localSize, encoded, encodedLength)) {
*decodedSize = localSize;
if (encodedSizeOut != NULL) {
*encodedSizeOut = encodedLength;
}
return NWC24_ERR_OVERFLOW;
}
decodedLength = localSize;
if (encodedWordPosition != 0) {
BOOL whitespaceOnly = TRUE;
u32 offset;
for (offset = 0; offset < localSize && decoded[offset] != '\0'; offset++) {
char value = decoded[offset];
if (value != ' ' && value != '\t' && value != '\r' && value != '\n') {
whitespaceOnly = FALSE;
}
}
if (whitespaceOnly) {
*decoded = '\0';
decodedLength = 1;
}
}
}
if (decodedSize != NULL) {
*decodedSize = decodedLength;
}
if (encodedSizeOut != NULL) {
*encodedSizeOut = encodedLength;
}
return result;
}
NWC24Err NWC24DecodeMIMEHeaderFieldBody(u8* charsetData, u32 charsetDataSize, u8* decoded, u32 decodedCapacity, u32* decodedSize, u8* encoded,
u32 encodedSize) {
int result = 0;
u8* output;
u32 inputSize;
if (decodedSize == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
if (charsetData == NULL || charsetDataSize == 0) {
return NWC24_ERR_INVALID_VALUE;
}
if (decoded == NULL || decodedCapacity == 0) {
return NWC24_ERR_INVALID_VALUE;
}
if (encoded == NULL)
return NWC24_ERR_INVALID_VALUE;
if (encoded == NULL)
return NWC24_ERR_INVALID_VALUE;
*decoded = 0;
*charsetData = 0;
*decodedSize = 1;
inputSize = encodedSize;
output = decoded;
while ((s32)inputSize > 0 && (s32)decodedCapacity > 0 && *encoded != '\0') {
u32 copiedSize = 0;
u32 consumedSize;
result =
DecodeWord((char*)charsetData, charsetDataSize, (char*)output, decodedCapacity, &copiedSize, (char*)encoded, inputSize, &consumedSize);
if (copiedSize == 0) {
break;
}
copiedSize--;
output += copiedSize;
decodedCapacity -= copiedSize;
*decodedSize += copiedSize;
encoded += consumedSize;
inputSize -= consumedSize;
if (result != NWC24_OK) {
break;
}
}
if (result == NWC24_OK && inputSize > 0 && *encoded != '\0') {
result = NWC24_ERR_OVERFLOW;
}
decoded[*decodedSize - 1] = '\0';
return result;
}
static NWC24Err ConcatEncodedText(char* encoded, u32 encodedCapacity, u32* encodedSize, char encoding, u8* decoded, u32 decodedSize,
u32* decodedSizeOut) {
u32 prefixSize = Mail_strlen(encoded);
int available = encodedCapacity - prefixSize - 2;
NWC24Err result;
if (available > 0) {
if (encoding == 'B' || encoding == 'b') {
result = Base64Encode(decoded, decodedSize, decodedSizeOut, (u8*)encoded + prefixSize, (u32)(available - 1), encodedSize);
} else if (encoding == 'Q' || encoding == 'q') {
result = QEncode(encoded + prefixSize, (u32)(available - 1), encodedSize, decoded, decodedSize, decodedSizeOut, 1);
} else {
result = NWC24_ERR_NOT_SUPPORTED;
}
if (result == NWC24_OK || result == NWC24_ERR_OVERFLOW) {
encoded[prefixSize + *encodedSize] = '\0';
*encodedSize += 1;
}
} else {
result = NWC24_ERR_OVERFLOW;
}
return result;
}
static NWC24Err ExtractCharset(char* charset, u32 charsetCapacity, u32* charsetSize, char* encoded, u32 encodedSize) {
char* start;
char* end;
u32 charsetLength;
if (charset == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
start = FindMarker(encoded, encodedSize, "=?");
if (start == NULL) {
return NWC24_ERR_NOT_SUPPORTED;
}
start += 2;
end = FindMarker(start, encodedSize - (u32)(start - encoded), "?");
if (end == NULL) {
return NWC24_ERR_NOT_SUPPORTED;
}
charsetLength = (u32)(end - start);
if (charsetLength >= charsetCapacity) {
return NWC24_ERR_OVERFLOW;
}
Mail_strncpy(charset, start, charsetLength);
charset[charsetLength] = '\0';
*charsetSize = charsetLength + 2;
return NWC24_OK;
}
static NWC24Err ExtractEncodedText(char* decoded, u32 decodedCapacity, u32* decodedSize, char encoding, char* encoded, u32 encodedSize,
u32* encodedSizeOut) {
char* start;
char* end;
int length;
NWC24Err result;
if (encoded == NULL || decodedSize == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
*decodedSize = 0;
if (encodedSizeOut == NULL) {
return NWC24_ERR_INVALID_VALUE;
}
*encodedSizeOut = 0;
start = FindMarker(encoded, encodedSize, "?");
if (start == NULL) {
return NWC24_ERR_NOT_SUPPORTED;
}
start++;
end = FindMarker(start, encodedSize - (u32)(start - encoded), "?=");
if (end == NULL) {
return NWC24_ERR_NOT_SUPPORTED;
}
length = end - start;
if (encoding == 'B' || encoding == 'b') {
result = NWC24Base64Decode((u8*)start, length, (u8*)decoded, decodedCapacity - 1, decodedSize);
if (result == NWC24_OK || result == NWC24_ERR_OVERFLOW) {
decoded[*decodedSize] = '\0';
*decodedSize += 1;
}
} else if (encoding == 'Q' || encoding == 'q') {
result = QDecode(decoded, decodedCapacity - 1, decodedSize, start, length, NULL, 1);
if (result == NWC24_OK || result == NWC24_ERR_OVERFLOW) {
decoded[*decodedSize] = '\0';
*decodedSize += 1;
}
} else {
return NWC24_ERR_NOT_SUPPORTED;
}
*encodedSizeOut = (u32)(end - encoded) + 2;
return result;
}

View File

@@ -1,599 +0,0 @@
#define NWC24_MSG_READ
#include <private/nwc24.h>
NWC24Err NWC24iMBoxOpenStoredMsg(NWC24MBoxType type, u32 id, NWC24File* file);
NWC24Err NWC24iMBoxCloseMsg(NWC24File* file);
BOOL Mail_isdigit(int character);
static NWC24Err ReadMsgTextInternal(const NWC24MsgObj* msg, char* text, u32 capacity, char* charset, u32 charsetCapacity, NWC24Encoding* encoding);
NWC24Err ReadBase64Data(NWC24File* file, const NWC24Data* data, u8* output, u32 capacity, u32* outputSize);
NWC24Err ReadQPText(const NWC24MsgObjPrivate* msg, NWC24File* file, char* output, u32 capacity);
static inline NWC24Err SelectMBox(const NWC24MsgObjPrivate* msg, NWC24MBoxType* type) {
if (msg->type & 0x10)
*type = NWC24_MBOX_TYPE_SEND;
else if (msg->type & 0x20)
*type = NWC24_MBOX_TYPE_RECV;
else
return NWC24_ERR_INVALID_VALUE;
return NWC24_OK;
}
NWC24Err NWC24ReadMsgField(const NWC24MsgObj* msg, char* fieldName, u8* output, u32 capacity) {
const NWC24MsgObjPrivate* privateMsg = (const NWC24MsgObjPrivate*)msg;
NWC24File file;
NWC24FileStream stream;
char* field;
u32 offset;
u32 length;
NWC24MBoxType type;
NWC24Err result;
NWC24Err closeResult;
if (!NWC24IsMsgLibOpened() && !NWC24IsMsgLibOpenedByTool())
return NWC24_ERR_LIB_NOT_OPENED;
if (!(privateMsg->type & 0x200))
return NWC24_ERR_PROTECTED;
result = SelectMBox(privateMsg, &type);
switch (result) {
case NWC24_OK:
break;
default:
return result;
}
result = NWC24iMBoxOpenStoredMsg(type, privateMsg->msgId, &file);
switch (result) {
case NWC24_OK:
break;
default:
return result;
}
result = NWC24FStreamInit(&stream, &file, 0, privateMsg->length, NWC24WorkP->mainWork, 1024);
if (result == NWC24_OK) {
result = NWC24iSearchHeaderF(&stream, fieldName, privateMsg->headerSize, &offset, &length);
if (result == NWC24_OK) {
if (length > capacity)
result = NWC24_ERR_OVERFLOW;
else {
result = NWC24FStreamSeek(&stream, offset);
if (result == NWC24_OK) {
result = NWC24FStreamGetPtr(&stream, &field, length);
if (result == NWC24_OK) {
Mail_strncpy((char*)output, field, length);
output[length - 2] = 0;
}
}
}
}
}
closeResult = NWC24iMBoxCloseMsg(&file);
if (result == NWC24_OK)
result = closeResult;
return result;
}
NWC24Err NWC24ReadMsgFaceData(const NWC24MsgObj* msg, u8* faceData) {
const NWC24MsgObjPrivate* privateMsg = (const NWC24MsgObjPrivate*)msg;
char* buffer;
u32 length;
NWC24Err result;
if ((privateMsg->type & 2) && !(privateMsg->type & 0x1000))
return NWC24_ERR_NOT_FOUND;
buffer = NWC24WorkP->stringWork;
Mail_memset(buffer, 0, 1024);
result = NWC24ReadMsgField(msg, "x-wiiface", (u8*)buffer, 1024);
switch (result) {
default:
return result;
case NWC24_OK:
result = NWC24Base64Decode((u8*)buffer, STD_strnlen(buffer, 1024), faceData, 74, &length);
switch (result) {
default:
return result;
case NWC24_OK:
if (length != 74)
return NWC24_ERR_FORMAT;
return NWC24_OK;
}
}
return result;
}
NWC24Err NWC24ReadMsgAltName(const NWC24MsgObj* msg, u16* name, u32 capacity) {
char* buffer = NWC24WorkP->stringWork;
u32 length;
NWC24Err result;
NWC24Err decodeResult;
Mail_memset(buffer, 0, 1024);
result = NWC24ReadMsgField(msg, "x-wii-altname:", (u8*)buffer, 1024);
switch (result) {
default:
return result;
case NWC24_OK:
decodeResult = NWC24Base64Decode((u8*)buffer, STD_strnlen(buffer, 1024), (u8*)name, capacity * 2, &length);
result = NWC24_OK;
if (decodeResult != NWC24_OK)
result = decodeResult;
}
return result;
}
NWC24Err NWC24ReadMsgMBNoReply(const NWC24MsgObj* msg, BOOL* noReply) {
char* buffer;
NWC24Err result;
*noReply = FALSE;
buffer = NWC24WorkP->stringWork;
Mail_memset(buffer, 0, 1024);
result = NWC24ReadMsgField(msg, "x-wii-mb-noreply:", (u8*)buffer, 1024);
if (result == NWC24_OK)
*noReply = TRUE;
else if (result == NWC24_ERR_NOT_FOUND)
result = NWC24_OK;
return result;
}
NWC24Err NWC24ReadMsgMBRegDate(const NWC24MsgObj* msg, u16* year, u8* month, u8* day) {
NWC24Err result;
u32 offset = 0;
u32 digits = 0;
u32 value = 0;
s32 digit;
char* buffer = NWC24WorkP->stringWork;
Mail_memset(buffer, 0, 1024);
result = NWC24ReadMsgField(msg, "x-wii-mb-regdate:", (u8*)buffer, 1023);
switch (result) {
default:
return result;
case NWC24_OK:
break;
}
while (*buffer == '\t' || *buffer == ' ') {
buffer++;
offset++;
}
for (; offset < 1024; offset++, digits++) {
digit = Util_xtoi(*buffer);
if (digit < 0)
break;
value = (value << 4) | digit;
buffer++;
}
if (digits != 4)
result = NWC24_ERR_FORMAT;
*year = ((value >> 9) & 127) + 2000;
*month = (value >> 5) & 15;
*day = value & 31;
return result;
}
NWC24Err NWC24ReadMsgMBDelay(const NWC24MsgObj* msg, u8* delay) {
NWC24Err result;
u32 offset = 0;
u32 digits = 0;
u32 value = 0;
s32 digit;
char* buffer = NWC24WorkP->stringWork;
Mail_memset(buffer, 0, 1024);
result = NWC24ReadMsgField(msg, "x-wii-mb-delay:", (u8*)buffer, 1023);
switch (result) {
default:
return result;
case NWC24_OK:
break;
}
while (*buffer == '\t' || *buffer == ' ') {
buffer++;
offset++;
}
for (; offset < 1024; offset++, digits++) {
digit = Util_xtoi(*buffer);
if (digit < 0)
break;
value = (value << 4) | digit;
buffer++;
}
if (digits != 2)
result = NWC24_ERR_FORMAT;
*delay = value;
return result;
}
NWC24Err NWC24ReadMsgMBUpdateSW(const NWC24MsgObj* msg, u32* update) {
NWC24Err result;
const NWC24MsgObjPrivate* privateMsg = (const NWC24MsgObjPrivate*)msg;
u32 value = 0;
char* buffer;
if (!(privateMsg->type & 0x1000)) {
*update = 0;
return NWC24_ERR_NOT_FOUND;
}
buffer = NWC24WorkP->stringWork;
Mail_memset(buffer, 0, 1024);
result = NWC24ReadMsgField(msg, "x-wii-mb-updatesw:", (u8*)buffer, 1023);
switch (result) {
default:
return result;
case NWC24_OK:
while (*buffer == '\t' || *buffer == ' ')
buffer++;
if (Mail_isdigit(*buffer))
value = *buffer - '0';
*update = value;
}
return result;
}
NWC24Err NWC24ReadMsgMBOptOutFlag(const NWC24MsgObj* msg, BOOL* optOut, u32* appId) {
NWC24Err result;
u32 offset = 0;
u32 digits = 0;
u32 value = 0;
s32 digit;
char* buffer = NWC24WorkP->stringWork;
Mail_memset(buffer, 0, 1024);
*optOut = FALSE;
*appId = 0;
result = NWC24ReadMsgField(msg, "x-wii-mb-optout:", (u8*)buffer, 1023);
if (result == NWC24_ERR_NOT_FOUND)
return NWC24_OK;
switch (result) {
default:
return result;
case NWC24_OK:
while (*buffer == '\t' || *buffer == ' ') {
buffer++;
offset++;
}
if (*buffer == '1')
*optOut = TRUE;
{
char separator = buffer[1];
buffer += 2;
if (separator != '-')
return NWC24_OK;
{
offset += 2;
for (; offset < 1024; offset++, digits++) {
digit = Util_xtoi(*buffer);
if (digit < 0)
break;
value = (value << 4) | digit;
buffer++;
}
if (digits != 8)
result = NWC24_ERR_FORMAT;
*appId = value;
}
}
}
return result;
}
NWC24Err NWC24ReadMsgFromAddr(const NWC24MsgObj* msg, char* address, u32 capacity) {
const NWC24MsgObjPrivate* privateMsg = (const NWC24MsgObjPrivate*)msg;
NWC24MBoxType type;
NWC24File file;
NWC24FileStream stream;
char* buffer;
NWC24Err result, closeResult;
if (!NWC24IsMsgLibOpened() && !NWC24IsMsgLibOpenedByTool())
return NWC24_ERR_LIB_NOT_OPENED;
if (!(privateMsg->type & 0x200))
return NWC24_ERR_PROTECTED;
buffer = NWC24WorkP->stringWork;
Mail_memset(buffer, 0, 1024);
if (!(privateMsg->type & 2))
return NWC24_ERR_NOT_SUPPORTED;
result = SelectMBox(privateMsg, &type);
switch (result) {
case NWC24_OK:
break;
default:
return result;
}
if (privateMsg->unk_0x30.size == 0)
return NWC24_ERR_NULL;
result = NWC24iMBoxOpenStoredMsg(type, privateMsg->msgId, &file);
switch (result) {
case NWC24_OK:
break;
default:
return result;
}
result = NWC24FStreamInit(&stream, &file, 0, privateMsg->length, buffer, 1024);
if (result == NWC24_OK) {
result = NWC24FStreamSeek(&stream, (u32)privateMsg->unk_0x30.ptr);
if (result == NWC24_OK)
result = NWC24iExtractAddrSpec(&stream, privateMsg->unk_0x30.size, 0, address, capacity);
}
closeResult = NWC24iMBoxCloseMsg(&file);
if (result != NWC24_OK)
closeResult = result;
return closeResult;
return result;
}
NWC24Err NWC24ReadMsgSubject(const NWC24MsgObj* msg, char* subject, u32 capacity) {
const NWC24MsgObjPrivate* privateMsg = (const NWC24MsgObjPrivate*)msg;
NWC24MBoxType type;
NWC24File file;
u32 length;
NWC24Err result, closeResult;
if (!NWC24IsMsgLibOpened() && !NWC24IsMsgLibOpenedByTool())
return NWC24_ERR_LIB_NOT_OPENED;
if (!(privateMsg->type & 0x200))
return NWC24_ERR_PROTECTED;
result = SelectMBox(privateMsg, &type);
switch (result) {
case NWC24_OK:
break;
default:
return result;
}
length = privateMsg->subject.size;
if (length == 0)
return NWC24_ERR_NULL;
result = NWC24iMBoxOpenStoredMsg(type, privateMsg->msgId, &file);
switch (result) {
case NWC24_OK:
break;
default:
return result;
}
if (length > capacity - 1)
length = capacity - 1;
NWC24FSeek(&file, (u32)privateMsg->subject.ptr, NWC24_SEEK_BEG);
result = NWC24FRead(subject, length, &file);
if (result == NWC24_OK) {
subject[length] = 0;
if (privateMsg->subject.size > capacity - 1)
result = NWC24_ERR_OVERFLOW;
}
closeResult = NWC24iMBoxCloseMsg(&file);
if (result == NWC24_OK)
result = closeResult;
return result;
}
NWC24Err NWC24ReadMsgText(const NWC24MsgObj* msg, char* text, u32 capacity, NWC24Charset* charset, NWC24Encoding* encoding) {
char* name = NWC24WorkP->stringWork + 32;
NWC24Err result = ReadMsgTextInternal(msg, text, capacity, name, 64, encoding);
if (result == NWC24_OK)
result = NWC24ParseCharsetStr(charset, name);
return result;
}
NWC24Err NWC24ReadMsgTextEx(const NWC24MsgObj* msg, char* text, u32 capacity, char* charset, u32 charsetCapacity) {
u32 i;
NWC24Encoding encoding;
NWC24Err result = ReadMsgTextInternal(msg, text, capacity, charset, charsetCapacity, &encoding);
if (result == NWC24_OK) {
for (i = 0; i < charsetCapacity; i++) {
switch (*charset) {
case ' ':
case '\t':
case '\n':
case '\r':
case ';':
case '"':
*charset = 0;
break;
}
if (*charset == 0)
break;
charset++;
}
}
return result;
}
static NWC24Err ReadMsgTextInternal(const NWC24MsgObj* msg, char* text, u32 capacity, char* charset, u32 charsetCapacity, NWC24Encoding* encoding) {
const NWC24MsgObjPrivate* privateMsg = (const NWC24MsgObjPrivate*)msg;
NWC24MBoxType type;
NWC24File file;
NWC24Err result, closeResult;
NWC24Err overflow = NWC24_OK;
u32 length;
u32 decodedSize;
char* buffer;
if (!NWC24IsMsgLibOpened() && !NWC24IsMsgLibOpenedByTool())
return NWC24_ERR_LIB_NOT_OPENED;
if (!(privateMsg->type & 0x200))
return NWC24_ERR_PROTECTED;
buffer = NWC24WorkP->stringWork;
*encoding = 0;
result = SelectMBox(privateMsg, &type);
switch (result) {
case NWC24_OK:
break;
default:
return result;
}
length = privateMsg->textSize;
if (length == 0)
length = privateMsg->text.size;
if (length == 0)
return NWC24_ERR_NULL;
if (length > capacity - 1) {
overflow = NWC24_ERR_OVERFLOW;
length = capacity - 1;
}
result = NWC24iMBoxOpenStoredMsg(type, privateMsg->msgId, &file);
switch (result) {
case NWC24_OK:
break;
default:
return result;
}
Mail_memset(charset, 0, charsetCapacity);
if (privateMsg->unk_0x50.size >= charsetCapacity)
result = NWC24_ERR_FORMAT;
else {
if (privateMsg->unk_0x50.size != 0) {
NWC24FSeek(&file, (u32)privateMsg->unk_0x50.ptr, NWC24_SEEK_BEG);
result = NWC24FRead(charset, privateMsg->unk_0x50.size, &file);
if (result != NWC24_OK)
goto close;
}
if (privateMsg->unk_0x58.size != 0 && privateMsg->unk_0x58.size < 32) {
Mail_memset(buffer, 0, 32);
NWC24FSeek(&file, (u32)privateMsg->unk_0x58.ptr, NWC24_SEEK_BEG);
result = NWC24FRead(buffer, privateMsg->unk_0x58.size, &file);
if (result != NWC24_OK)
goto close;
result = NWC24ParseEncodingStr(encoding, buffer);
if (result != NWC24_OK)
goto close;
}
NWC24FSeek(&file, (u32)privateMsg->text.ptr, NWC24_SEEK_BEG);
switch (*encoding) {
case 0:
case 1:
result = NWC24FRead(text, length, &file);
text[length] = 0;
break;
case 2:
decodedSize = 0;
result = ReadBase64Data(&file, &privateMsg->text, (u8*)text, length, &decodedSize);
if (result == NWC24_OK && decodedSize != privateMsg->textSize)
result = NWC24_ERR_FORMAT;
text[length] = 0;
break;
case 3:
result = ReadQPText(privateMsg, &file, text, length);
break;
default:
result = NWC24_ERR_NOT_SUPPORTED;
*text = 0;
break;
}
}
close:
closeResult = NWC24iMBoxCloseMsg(&file);
if (result == NWC24_OK)
result = closeResult;
if (result == NWC24_OK)
result = overflow;
return result;
}
NWC24Err NWC24ReadMsgAttached(const NWC24MsgObj* msg, u32 index, u8* output, u32 capacity) {
const NWC24MsgObjPrivate* privateMsg = (const NWC24MsgObjPrivate*)msg;
NWC24File file;
NWC24MBoxType type;
u32 decodedSize;
NWC24Err result, closeResult;
if (!NWC24IsMsgLibOpened() && !NWC24IsMsgLibOpenedByTool())
return NWC24_ERR_LIB_NOT_OPENED;
if (!(privateMsg->type & 0x200))
return NWC24_ERR_PROTECTED;
if (index >= privateMsg->numAttached)
return NWC24_ERR_NOT_FOUND;
result = SelectMBox(privateMsg, &type);
switch (result) {
case NWC24_OK:
break;
default:
return result;
}
if (privateMsg->attached[index].size == 0 || privateMsg->attachedSize[index] == 0)
return NWC24_ERR_NULL;
result = NWC24iMBoxOpenStoredMsg(type, privateMsg->msgId, &file);
switch (result) {
case NWC24_OK:
break;
default:
return result;
}
decodedSize = 0;
result = ReadBase64Data(&file, &privateMsg->attached[index], output, capacity, &decodedSize);
if (result == NWC24_OK && decodedSize != privateMsg->attachedSize[index])
result = NWC24_ERR_FORMAT;
closeResult = NWC24iMBoxCloseMsg(&file);
if (result == NWC24_OK)
result = closeResult;
return result;
}
NWC24Err ReadBase64Data(NWC24File* file, const NWC24Data* data, u8* output, u32 capacity, u32* outputSize) {
int remaining = data->size;
int offset = (u32)data->ptr;
char* buffer = NWC24WorkP->mainWork;
u8* outPtr = output;
int total = 0;
int readSize;
int end;
char* cursor;
int i;
u32 decodedSize;
NWC24Err result = NWC24_OK;
BOOL finished = FALSE;
while (remaining > 0 && !finished) {
readSize = remaining > 1024 ? 1024 : remaining;
NWC24FSeek(file, offset, NWC24_SEEK_BEG);
result = NWC24FRead(buffer, readSize, file);
if (result != NWC24_OK)
break;
end = 0;
cursor = buffer;
for (i = 0; i < readSize; cursor++, i++) {
if (*cursor == '\r')
end = i;
if (*cursor == '=') {
end = i + 1;
finished = TRUE;
result = NWC24_OK;
break;
}
}
if (end == 0)
break;
result = NWC24Base64Decode((u8*)buffer, end, outPtr, capacity - total, &decodedSize);
if (result != NWC24_OK)
break;
offset += end;
remaining -= end;
total += decodedSize;
outPtr += decodedSize;
}
if (result == NWC24_OK)
*outputSize = total;
return result;
}
NWC24Err ReadQPText(const NWC24MsgObjPrivate* msg, NWC24File* file, char* output, u32 capacity) {
int remaining = msg->text.size;
int offset = (u32)msg->text.ptr;
char* buffer = NWC24WorkP->mainWork;
char* cursor = output;
int readSize;
u32 total = 0;
u32 consumedSize;
u32 decodedSize;
NWC24Err result = NWC24_OK;
while (remaining > 0) {
readSize = remaining > 1024 ? 1024 : remaining;
NWC24FSeek(file, offset, NWC24_SEEK_BEG);
result = NWC24FRead(buffer, readSize, file);
if (result != NWC24_OK)
break;
consumedSize = readSize;
if (readSize == 1024) {
if (buffer[readSize - 1] == '=')
consumedSize = readSize - 1;
else if (buffer[readSize - 2] == '=')
consumedSize = readSize - 2;
}
result = NWC24DecodeQuotedPrintable(cursor, capacity - total, &decodedSize, (u8*)buffer, consumedSize, &consumedSize);
if (result != NWC24_OK && result != NWC24_ERR_OVERFLOW)
break;
total += decodedSize;
cursor += decodedSize;
if (result == NWC24_ERR_OVERFLOW)
break;
offset += consumedSize;
remaining -= consumedSize;
}
*cursor = 0;
if (result == NWC24_OK && total > msg->textSize)
result = NWC24_ERR_FORMAT;
return result;
}

View File

@@ -1,877 +0,0 @@
#define NWC24_MSG_SUBJECT
#include <private/enc.h>
#include <private/nwc24.h>
#include <revolution/enc.h>
#include <revolution/nwc24.h>
#include <string.h>
static char CharsetUsAscii[] = "us-ascii";
static char CharsetIso2022Jp[] = "iso-2022-jp";
static char CharsetUtf8[] = "utf-8";
static char CharsetEucKr[] = "euc-kr";
static char CharsetGb2312[] = "gb2312";
static const char* JapaneseCharsets[] = {CharsetUsAscii, CharsetIso2022Jp, CharsetUtf8};
static const char* ChineseCharsets[] = {CharsetUsAscii, CharsetGb2312};
static char CharsetIso88591[] = "iso-8859-1";
static char CharsetWindows1252[] = "windows-1252";
static char CharsetIso88592[] = "iso-8859-2";
static char CharsetIso88597[] = "iso-8859-7";
static char CharsetIso885910[] = "iso-8859-10";
char CharsetIso88593[] = "iso-8859-3";
static const char* WesternCharsets[] = {CharsetUsAscii, CharsetIso88591, CharsetWindows1252, CharsetIso88592,
CharsetIso88597, CharsetIso885910, CharsetIso88593, CharsetUtf8};
static const char* KoreanCharsets[] = {CharsetUsAscii, CharsetEucKr, CharsetUtf8};
static char DefaultCharsetJp[] = "iso-2022-jp";
static char DefaultCharsetKr[] = "euc-kr";
NWC24Err NWC24iGetDefaultCharset(char* charset, u32 charsetSize, NWC24EncodingRegion region);
NWC24Err NWC24iConvertToInternalEncoding(u16* dst, u32* dstSize, const u8* src, u32* srcSize, char* charset, u32 charsetSize,
NWC24EncodingRegion region, u16 alternative);
NWC24Err NWC24iConvertFromInternalEncoding(u8* dst, u32* dstSize, const u16* src, u32* srcSize, char* charset, u32 charsetSize,
NWC24EncodingRegion region, u16 alternative);
NWC24Err NWC24iDetectEncodingToSend(char* charset, u32 charsetSize, const u16* subject, u32 subjectSize, const u16* text, u32 textSize,
NWC24EncodingRegion region);
NWC24Err NWC24iDetectBreakPoint(u32* breakPoint, NWC24Charset charset, const u8* text, u32 textSize, u32 maxSize);
NWC24Err NWC24iSetMsgSubjectPlain(NWC24MsgObj* msg, const u16* subject, u32 subjectSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize, NWC24Charset charset, char* charsetName);
NWC24Err NWC24iSetMsgSubjectQP(NWC24MsgObj* msg, const u16* subject, u32 subjectSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize, NWC24Charset charset, char* charsetName);
NWC24Err NWC24iSetMsgSubjectBase64(NWC24MsgObj* msg, const u16* subject, u32 subjectSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize, NWC24Charset charset, char* charsetName);
NWC24Err NWC24ReadMsgSubjectPublic(const NWC24MsgObj* msg, u16* subject, u32* subjectSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize) {
BOOL truncated;
u8* decodedData;
u32 decodedCapacity;
u32 originalSize;
u32 subjectDataSize;
u32 workHalf;
u32 decodedSize;
char* charsetBuffer;
NWC24Err result;
u32 i;
if (subjectSize == NULL) {
result = NWC24_ERR_INVALID_VALUE;
} else {
originalSize = *subjectSize;
*subjectSize = 0;
truncated = FALSE;
if (work == NULL) {
result = NWC24_ERR_INVALID_VALUE;
} else {
workHalf = workSize >> 1;
decodedData = work + workHalf;
decodedCapacity = workSize - workHalf;
result = NWC24GetMsgSubjectSize(msg, &subjectDataSize);
switch (result) {
case NWC24_OK:
if (subjectDataSize > workHalf) {
subjectDataSize = workHalf;
truncated = TRUE;
}
result = NWC24ReadMsgSubject(msg, (char*)work, subjectDataSize);
if (result != NWC24_OK) {
if (result == NWC24_ERR_OVERFLOW) {
truncated = TRUE;
} else {
goto done;
}
}
charsetBuffer = NWC24WorkP->stringWork;
result =
NWC24DecodeMIMEHeaderFieldBody((u8*)charsetBuffer, 0x40, decodedData, decodedCapacity, &decodedSize, work, subjectDataSize);
if (result != NWC24_OK) {
if (result == NWC24_ERR_OVERFLOW) {
truncated = TRUE;
} else {
goto done;
}
}
*subjectSize = originalSize;
result =
NWC24iConvertToInternalEncoding(subject, subjectSize, decodedData, &decodedSize, charsetBuffer, 0x40, region, alternative);
if ((result == NWC24_OK) || (result == NWC24_ERR_OVERFLOW)) {
for (i = 0; (i < *subjectSize) && (subject[i] != 0); i++) {
if ((subject[i] == 0x0D) || (subject[i] == 0x0A)) {
subject[i] = 0x20;
}
}
if ((result == NWC24_OK) && truncated) {
result = NWC24_ERR_OVERFLOW;
}
}
break;
default:
goto done;
}
}
}
done:
return result;
}
NWC24Err NWC24ReadMsgTextPublic(const NWC24MsgObj* msg, u16* text, u32* textSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize) {
char* charsetBuffer;
BOOL truncated;
u32 originalSize;
u32 textDataSize;
NWC24Err result;
if (textSize == NULL) {
result = NWC24_ERR_INVALID_VALUE;
} else {
originalSize = *textSize;
*textSize = 0;
truncated = FALSE;
if (work == NULL) {
result = NWC24_ERR_INVALID_VALUE;
} else {
result = NWC24GetMsgTextSize(msg, &textDataSize);
switch (result) {
case NWC24_OK:
if (textDataSize > workSize) {
textDataSize = workSize;
truncated = TRUE;
}
charsetBuffer = NWC24WorkP->stringWork + 0x20;
result = NWC24ReadMsgTextEx(msg, (char*)work, textDataSize, charsetBuffer, 0x40);
if (result != NWC24_OK) {
if (result != NWC24_ERR_OVERFLOW) {
goto done;
}
truncated = TRUE;
}
*textSize = originalSize;
result = NWC24iConvertToInternalEncoding(text, textSize, work, &textDataSize, charsetBuffer, 0x40, region, alternative);
if ((result == NWC24_OK) && truncated) {
result = NWC24_ERR_OVERFLOW;
}
break;
default:
goto done;
}
}
}
done:
return result;
}
NWC24Err NWC24SetMsgSubjectPublic(NWC24MsgObj* msg, const u16* subject, u32 subjectSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize) {
NWC24MsgObjPrivate* privateMsg = (NWC24MsgObjPrivate*)msg;
NWC24Work* nwcWork;
NWC24Charset charset;
NWC24Err result;
if (((privateMsg->type & 0x100) == 0) || ((privateMsg->type & 0x200) != 0)) {
result = NWC24_ERR_PROTECTED;
goto done;
}
if ((subject == NULL) || (*subject == 0)) {
result = NWC24_ERR_NULL;
goto done;
}
if (subject[subjectSize] != 0) {
result = NWC24_ERR_STRING_END;
goto done;
}
if (work == NULL) {
result = NWC24_ERR_INVALID_VALUE;
goto done;
}
nwcWork = NWC24WorkP;
result = NWC24iDetectEncodingToSend(nwcWork->stringWork, 0x40, subject, subjectSize, NULL, 0, region);
switch (result) {
case NWC24_OK:
break;
default:
goto done;
}
if (NWC24ParseCharsetStr(&charset, nwcWork->stringWork) != NWC24_OK) {
charset = 0;
}
if (charset == 0) {
result = NWC24iSetMsgSubjectPlain(msg, subject, subjectSize, region, alternative, work, workSize, charset, nwcWork->stringWork);
goto done;
}
switch (region) {
case 0:
result = 2;
break;
case 1:
case 2:
result = 3;
break;
case 3:
case 4:
result = 2;
break;
default:
result = 2;
break;
}
if (result == 3) {
result = NWC24iSetMsgSubjectQP(msg, subject, subjectSize, region, alternative, work, workSize, charset, nwcWork->stringWork);
} else {
result = NWC24iSetMsgSubjectBase64(msg, subject, subjectSize, region, alternative, work, workSize, charset, nwcWork->stringWork);
}
done:
return result;
}
NWC24Err NWC24SetMsgSubjectAndTextPublic(NWC24MsgObj* msg, const u16* subject, u32 subjectSize, const u16* text, u32 textSize,
NWC24EncodingRegion region, u16 alternative, u8* work, u32 workSize) {
NWC24MsgObjPrivate* privateMsg = (NWC24MsgObjPrivate*)msg;
NWC24Work* nwcWork;
u32 textSourceSize;
u32 textWorkSize;
BOOL is7Bit;
NWC24Charset charset;
u32 subjectWorkSize;
u8* subjectWork;
NWC24Err result;
if (((privateMsg->type & 0x100) == 0) || ((privateMsg->type & 0x200) != 0)) {
result = NWC24_ERR_PROTECTED;
goto done;
}
if ((subject == NULL) || (*subject == 0)) {
result = NWC24_ERR_NULL;
goto done;
}
if ((text == NULL) || (textSize == 0)) {
NWC24Data_Init(&privateMsg->text);
result = NWC24SetMsgSubjectPublic(msg, subject, subjectSize, region, alternative, work, workSize);
goto done;
}
if ((text[textSize] != 0) || (subject[subjectSize] != 0)) {
result = NWC24_ERR_STRING_END;
goto done;
}
if (work == NULL) {
result = NWC24_ERR_INVALID_VALUE;
goto done;
}
nwcWork = NWC24WorkP;
result = NWC24iDetectEncodingToSend(nwcWork->stringWork, 0x40, subject, subjectSize, text, textSize, region);
switch (result) {
case NWC24_OK:
break;
default:
goto done;
}
if (NWC24ParseCharsetStr(&charset, nwcWork->stringWork) != NWC24_OK) {
charset = 0;
}
textWorkSize = (workSize * textSize) / (textSize + subjectSize * 4);
subjectWorkSize = workSize - textWorkSize;
subjectWork = work + textWorkSize;
textSourceSize = textSize;
result = NWC24iConvertFromInternalEncoding(work, &textWorkSize, text, &textSourceSize, nwcWork->stringWork, 0x40, region, alternative);
switch (result) {
case NWC24_OK:
break;
default:
goto done;
}
result = ENCIs7BitEncoding(&is7Bit, nwcWork->stringWork);
if (!is7Bit || (result != ENC_OK)) {
switch (region) {
case 0:
result = 2;
break;
case 1:
case 2:
result = 3;
break;
case 3:
case 4:
result = 2;
break;
default:
result = 2;
break;
}
} else {
result = 0;
}
result = NWC24SetMsgText(msg, (char*)work, textWorkSize - 1, charset, result);
switch (result) {
case NWC24_OK:
break;
default:
goto done;
}
if (charset == 0) {
result = NWC24iSetMsgSubjectPlain(msg, subject, subjectSize, region, alternative, subjectWork, subjectWorkSize, charset, nwcWork->stringWork);
goto done;
}
switch (region) {
case 0:
result = 2;
break;
case 1:
case 2:
result = 3;
break;
case 3:
case 4:
result = 2;
break;
default:
result = 2;
break;
}
if (result == 3) {
result = NWC24iSetMsgSubjectQP(msg, subject, subjectSize, region, alternative, subjectWork, subjectWorkSize, charset, nwcWork->stringWork);
} else {
result =
NWC24iSetMsgSubjectBase64(msg, subject, subjectSize, region, alternative, subjectWork, subjectWorkSize, charset, nwcWork->stringWork);
}
done:
return result;
}
NWC24Err NWC24iGetDefaultCharset(char* charset, u32 charsetSize, NWC24EncodingRegion region) {
NWC24Err result = NWC24_OK;
switch (region) {
case 0:
strncpy(charset, DefaultCharsetJp, charsetSize - 1);
charset[charsetSize - 1] = 0;
goto done;
case 1:
strncpy(charset, "iso-8859-1", charsetSize - 1);
charset[charsetSize - 1] = 0;
goto done;
case 2:
strncpy(charset, "iso-8859-1", charsetSize - 1);
charset[charsetSize - 1] = 0;
goto done;
case 3:
strncpy(charset, DefaultCharsetKr, charsetSize - 1);
charset[charsetSize - 1] = 0;
goto done;
case 4:
strncpy(charset, "gb2312", charsetSize - 1);
charset[charsetSize - 1] = 0;
goto done;
default:
strncpy(charset, "iso-8859-1", charsetSize - 1);
charset[charsetSize - 1] = 0;
result = NWC24_ERR_INVALID_VALUE;
}
done:
return result;
}
NWC24Err NWC24iConvertToInternalEncoding(u16* dst, u32* dstSize, const u8* src, u32* srcSize, char* charset, u32 charsetSize,
NWC24EncodingRegion region, u16 alternative) {
ENCContext context;
u32 dstBytes;
u32 convertedBytes;
NWC24Err result;
dstBytes = *dstSize * 2;
*dstSize = 0;
if (dstBytes < 2) {
result = NWC24_ERR_OVERFLOW;
goto done;
}
if (charset[0] == '\0') {
result = NWC24iGetDefaultCharset(charset, charsetSize, region);
switch (result) {
case NWC24_OK:
break;
default:
goto done;
}
}
result = ENCInitContext(&context);
if ((result == ENC_OK) && ((result = ENCSetExternalEncoding(&context, charset)) == ENC_OK) &&
((result = ENCSetBreakType(&context, ENC_BR_LF)) == ENC_OK) &&
((alternative == 0) || ((result = ENCSetAlternativeCharacter(&context, alternative, alternative)) == ENC_OK))) {
convertedBytes = dstBytes - 2;
result = ENCConvertToInternalEncoding(&context, dst, (s32*)&convertedBytes, src, (s32*)srcSize);
dst[convertedBytes >> 1] = 0;
*dstSize = (convertedBytes >> 1) + 1;
}
if (result == ENC_ERR_NO_MAP_RULE) {
goto invalidCharacter;
}
if (result < ENC_ERR_NO_MAP_RULE) {
if (result == ENC_ERR_INVALID_FORMAT) {
goto invalidCharacter;
}
if (result >= ENC_ERR_INVALID_FORMAT) {
goto invalidValue;
}
if (result >= ENC_ERR_NOT_LOADED) {
goto notSupported;
}
} else {
if (result == ENC_OK) {
goto success;
}
if (result >= ENC_OK) {
goto fatal;
}
goto overflow;
}
goto fatal;
success:
result = ENC_OK;
goto done;
invalidValue:
result = NWC24_ERR_INVALID_VALUE;
goto done;
overflow:
result = NWC24_ERR_OVERFLOW;
goto done;
invalidCharacter:
result = NWC24_ERR_INVALID_CHAR;
goto done;
notSupported:
result = NWC24_ERR_NOT_SUPPORTED;
goto done;
fatal:
result = NWC24_ERR_FATAL;
done:
return result;
}
NWC24Err NWC24iConvertFromInternalEncoding(u8* dst, u32* dstSize, const u16* src, u32* srcSize, char* charset, u32 charsetSize,
NWC24EncodingRegion region, u16 alternative) {
ENCContext context;
u32 outputCapacity;
u32 outputBytes;
u32 inputBytes;
NWC24Err result;
outputCapacity = *dstSize;
*dstSize = 0;
if (outputCapacity < 1) {
result = NWC24_ERR_OVERFLOW;
goto done;
}
if (charset[0] == '\0') {
result = NWC24iGetDefaultCharset(charset, charsetSize, region);
switch (result) {
case NWC24_OK:
break;
default:
goto done;
}
}
result = ENCInitContext(&context);
if ((result == ENC_OK) && ((result = ENCSetExternalEncoding(&context, charset)) == ENC_OK) &&
((result = ENCSetBreakType(&context, ENC_BR_CRLF)) == ENC_OK) &&
((alternative == 0) || ((result = ENCSetAlternativeCharacter(&context, alternative, alternative)) == ENC_OK))) {
outputBytes = outputCapacity - 1;
inputBytes = *srcSize << 1;
result = ENCConvertFromInternalEncoding(&context, dst, (s32*)&outputBytes, src, (s32*)&inputBytes);
dst[outputBytes] = 0;
*srcSize = inputBytes >> 1;
*dstSize = outputBytes + 1;
}
if (result == ENC_ERR_NO_MAP_RULE) {
goto invalidCharacter;
}
if (result < ENC_ERR_NO_MAP_RULE) {
if (result == ENC_ERR_INVALID_FORMAT) {
goto invalidCharacter;
}
if (result >= ENC_ERR_INVALID_FORMAT) {
goto invalidValue;
}
if (result >= ENC_ERR_NOT_LOADED) {
goto notSupported;
}
goto fatal;
}
if (result == ENC_OK) {
goto success;
}
if (result >= ENC_OK) {
goto fatal;
}
goto overflow;
success:
result = ENC_OK;
goto done;
invalidValue:
result = NWC24_ERR_INVALID_VALUE;
goto done;
overflow:
result = NWC24_ERR_OVERFLOW;
goto done;
invalidCharacter:
result = NWC24_ERR_INVALID_CHAR;
goto done;
notSupported:
result = NWC24_ERR_NOT_SUPPORTED;
goto done;
fatal:
result = NWC24_ERR_FATAL;
done:
return result;
}
NWC24Err NWC24iDetectEncodingToSend(char* charset, u32 charsetSize, const u16* subject, u32 subjectSize, const u16* text, u32 textSize,
NWC24EncodingRegion region) {
int endIndex;
u32 subjectBytes;
u32 textBytes;
int startIndex;
int lastIndex;
int index;
const char** names;
int numNames;
int result;
index = 0;
startIndex = 0;
lastIndex = -1;
switch (region) {
case 0:
names = JapaneseCharsets;
numNames = 3;
break;
case 1:
case 2:
names = WesternCharsets;
numNames = 8;
break;
case 3:
names = KoreanCharsets;
numNames = 3;
break;
case 4:
names = ChineseCharsets;
numNames = 2;
break;
default:
names = WesternCharsets;
numNames = 8;
break;
}
subjectBytes = subjectSize << 1;
textBytes = textSize << 1;
endIndex = numNames - 1;
while (index < endIndex) {
result = ENCCheckEncoding(&index, names + startIndex, endIndex - startIndex, subject, subjectBytes);
if (result != ENC_OK) {
return NWC24_ERR_NOT_SUPPORTED;
}
if (index == ENC_CHECK_ENCODING_NOT_FOUND) {
index = numNames - 1;
break;
}
startIndex += index;
if ((text == NULL) || (lastIndex == startIndex)) {
index = startIndex;
break;
}
result = ENCCheckEncoding(&index, names + startIndex, endIndex - startIndex, text, textBytes);
if (result != ENC_OK) {
return NWC24_ERR_NOT_SUPPORTED;
}
if (index == ENC_CHECK_ENCODING_NOT_FOUND) {
index = numNames - 1;
break;
}
lastIndex = startIndex + index;
if (lastIndex == startIndex) {
index = lastIndex;
break;
}
startIndex = lastIndex;
}
strncpy(charset, names[index], charsetSize - 1);
charset[charsetSize - 1] = '\0';
return NWC24_OK;
}
NWC24Err NWC24iDetectBreakPoint(u32* breakPoint, NWC24Charset charset, const u8* text, u32 textSize, u32 maxSize) {
const u8* ptr;
BOOL byteEncoding;
u32 i;
u32 lastBreak;
i = 0;
lastBreak = 0;
if ((breakPoint == NULL) || (text == NULL)) {
return NWC24_ERR_INVALID_VALUE;
}
if (charset == 0) {
byteEncoding = TRUE;
} else if (charset == NWC24_UTF_8) {
byteEncoding = FALSE;
} else if (((charset & 0xFFFF0000) == 0x00080000) || (charset == NWC24_WINDOWS_1252)) {
byteEncoding = TRUE;
} else {
return NWC24_ERR_NOT_SUPPORTED;
}
if (textSize <= maxSize) {
*breakPoint = textSize;
return NWC24_OK;
}
ptr = text;
while ((i < maxSize) && (*text != 0)) {
if (*ptr == 0x20) {
lastBreak = i;
}
i++;
ptr++;
}
if (lastBreak != 0) {
*breakPoint = lastBreak;
return NWC24_OK;
}
if (byteEncoding) {
*breakPoint = maxSize;
return NWC24_OK;
}
{
u32 utf8Index = 0;
ptr = text;
while (utf8Index < maxSize && *text != 0) {
if (((u32)*ptr & 0xC0) != 0x80U)
lastBreak = utf8Index;
utf8Index++;
ptr++;
}
}
*breakPoint = lastBreak;
return NWC24_OK;
}
NWC24Err NWC24iSetMsgSubjectPlain(NWC24MsgObj* msg, const u16* subject, u32 subjectSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize, NWC24Charset charset, char* charsetName) {
u32 workHalf;
u32 secondSize;
u32 sourceOffset;
u8* second;
u32 subjectLength;
u32 lineLength;
s32 copied;
u32 i;
NWC24Err result;
secondSize = workSize - (workSize >> 1);
second = work + (workSize >> 1);
subjectLength = subjectSize;
lineLength = 0;
workHalf = workSize >> 1;
result = NWC24iConvertFromInternalEncoding(work, &workHalf, subject, &subjectLength, charsetName, 0x40, region, alternative);
switch (result) {
default:
goto done;
case NWC24_OK:
subjectLength = workHalf - 1;
for (i = 0; i < workHalf; i++) {
if ((work[i] == '\n') || (work[i] == '\r')) {
work[i] = ' ';
}
}
NWC24iDetectBreakPoint(&lineLength, charset, work, subjectLength, 0x3F);
if (lineLength == subjectLength) {
result = NWC24SetMsgSubject(msg, (char*)work, subjectLength);
} else {
workHalf = secondSize;
sourceOffset = lineLength;
if (workHalf <= sourceOffset) {
result = NWC24_ERR_OVERFLOW;
goto done;
}
copied = NWC24iStrLCpy((char*)second, (char*)work, lineLength + 1);
for (; sourceOffset < subjectLength; sourceOffset += lineLength) {
if (workHalf - copied < 3) {
result = NWC24_ERR_OVERFLOW;
goto done;
}
second[copied++] = '\r';
second[copied++] = '\n';
if (work[sourceOffset] != ' ') {
second[copied] = ' ';
copied++;
}
NWC24iDetectBreakPoint(&lineLength, charset, work + sourceOffset, subjectLength - sourceOffset, 0x47);
if (workHalf - copied <= lineLength) {
result = NWC24_ERR_OVERFLOW;
goto done;
}
copied += NWC24iStrLCpy((char*)second + copied, (char*)work + sourceOffset, lineLength + 1);
}
second[copied] = 0;
result = NWC24SetMsgSubject(msg, (char*)second, copied);
}
}
done:
return result;
}
NWC24Err NWC24iSetMsgSubjectQP(NWC24MsgObj* msg, const u16* subject, u32 subjectSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize, NWC24Charset charset, char* charsetName) {
u32 workHalf;
u32 sourceOffset;
u32 secondSize;
u8* second;
u32 subjectLength;
u32 lineLength;
u32 outputLength;
u32 total;
u32 combinedLength;
u32 i;
u32 charsetLength;
NWC24Err result;
secondSize = workSize - (workSize >> 1);
second = work + (workSize >> 1);
subjectLength = subjectSize;
lineLength = 0;
workHalf = workSize >> 1;
result = NWC24iConvertFromInternalEncoding(work, &workHalf, subject, &subjectLength, charsetName, 0x40, region, alternative);
switch (result) {
default:
goto done;
case NWC24_OK:
subjectLength = workHalf - 1;
for (i = 0; i < workHalf; i++) {
if ((work[i] == '\n') || (work[i] == '\r')) {
work[i] = ' ';
}
}
charsetLength = strlen(charsetName);
NWC24iDetectBreakPoint(&lineLength, charset, work, subjectLength, (0x38 - charsetLength) / 3);
result = NWC24EncodeWord(second, secondSize, &outputLength, charsetName, 0x40, 'Q', work, lineLength);
switch (result) {
default:
goto done;
case NWC24_OK:
sourceOffset = lineLength;
if (lineLength == subjectLength) {
result = NWC24SetMsgSubject(msg, (char*)second, outputLength - 1);
} else {
charsetLength = (0x40 - strlen(charsetName)) / 3;
total = outputLength - 1;
workHalf = secondSize;
for (; sourceOffset < subjectLength;) {
if (workHalf - total < 3) {
result = NWC24_ERR_OVERFLOW;
goto done;
}
second[total++] = '\r';
second[total++] = '\n';
second[total++] = ' ';
NWC24iDetectBreakPoint(&lineLength, charset, work + sourceOffset, subjectLength - sourceOffset, charsetLength);
result = NWC24EncodeWord(second + total, workHalf - total, &outputLength, charsetName, 0x40, 'Q', work + sourceOffset,
lineLength);
switch (result) {
case NWC24_OK:
break;
default:
goto done;
}
combinedLength = total + outputLength;
sourceOffset += lineLength;
total = combinedLength - 1;
}
result = NWC24SetMsgSubject(msg, (char*)second, total);
}
}
}
done:
return result;
}
NWC24Err NWC24iSetMsgSubjectBase64(NWC24MsgObj* msg, const u16* subject, u32 subjectSize, NWC24EncodingRegion region, u16 alternative, u8* work,
u32 workSize, NWC24Charset charset, char* charsetName) {
u32 workHalf;
u32 secondSize;
u8* second;
u32 subjectLength;
u32 lineLength;
u32 outputLength;
u32 sourceOffset;
u32 total;
u32 charsetLength;
NWC24Err result;
workHalf = workSize >> 1;
secondSize = workSize - workHalf;
second = work + workHalf;
charsetLength = strlen(charsetName);
lineLength = ((0x36 - charsetLength) * 3) >> 2;
if (workHalf < lineLength) {
result = NWC24_ERR_OVERFLOW;
} else {
outputLength = lineLength + 1;
subjectLength = subjectSize;
result = NWC24iConvertFromInternalEncoding(work, &outputLength, subject, &subjectLength, charsetName, 0x40, region, alternative);
if (((result == NWC24_OK) || (result == NWC24_ERR_OVERFLOW)) &&
((result = NWC24EncodeWord(second, secondSize, &outputLength, charsetName, 0x40, 'B', work, outputLength - 1)) == NWC24_OK)) {
sourceOffset = subjectLength;
if (sourceOffset == subjectSize) {
result = NWC24SetMsgSubject(msg, (char*)second, outputLength - 1);
} else {
charsetLength = strlen(charsetName);
lineLength = (((0x3E - charsetLength) * 3) >> 2) + 1;
total = outputLength;
for (; sourceOffset < subjectSize; sourceOffset += subjectLength) {
subjectLength = subjectSize - sourceOffset;
workHalf = lineLength;
if (secondSize - (total - 1) < 3) {
result = NWC24_ERR_OVERFLOW;
goto done;
}
second[total - 1] = '\r';
second[total] = '\n';
second[total + 1] = ' ';
total += 2;
result = NWC24iConvertFromInternalEncoding(work, &workHalf, subject + sourceOffset, &subjectLength, charsetName, 0x40, region,
alternative);
if (((result != NWC24_OK) && (result != NWC24_ERR_OVERFLOW)) ||
((result = NWC24EncodeWord(second + total, secondSize - total, &outputLength, charsetName, 0x40, 'B', work, workHalf - 1)) !=
NWC24_OK)) {
goto done;
}
total += outputLength;
}
result = NWC24SetMsgSubject(msg, (char*)second, total - 1);
}
}
}
done:
return result;
}

View File

@@ -1,112 +0,0 @@
#include <private/nwc24.h>
#include <private/os/OSStateTM.h>
#include <revolution/os.h>
#include <revolution/sc.h>
#include <stdlib.h>
static s32 nwc24ShtFd = -1;
static s32 nwc24ShtRetryRest = 0;
static OSShutdownFunctionInfo ShutdownFuncInfo;
BOOL NWC24Shutdown(BOOL final, u32 event);
NWC24Err NWC24iMBoxSetLastUIDL(NWC24MBoxType type, const char* uidl);
NWC24Err NWC24EnableLedNotification(BOOL enable) {
u8 led = 0;
SCIdleModeInfo idleMode;
u32 status;
if (enable) {
do {
status = SCCheckStatus();
if (status == 2)
return NWC24_ERR_FATAL;
} while (status == 1);
SCGetIdleMode(&idleMode);
led = idleMode.led;
}
__OSSetIdleLEDMode(led);
return NWC24_OK;
}
NWC24Err NWC24iPrepareShutdown(void) {
NWC24Err result = NWC24_OK;
SCIdleModeInfo idleMode;
u32 status;
NWC24iRegister();
ShutdownFuncInfo.func = NWC24Shutdown;
ShutdownFuncInfo.priority = 110;
OSRegisterShutdownFunction(&ShutdownFuncInfo);
if (nwc24ShtFd < 0)
result = NWC24OpenResourceManager("/dev/net/kd/request", &nwc24ShtFd, 1);
nwc24ShtRetryRest = 5;
do {
status = SCCheckStatus();
if (status == 2)
goto ready;
} while (status == 1);
SCGetIdleMode(&idleMode);
__OSSetIdleLEDMode(idleMode.led);
ready:
if (OSGetAppType() == 0x40)
NWC24iSetScriptMode(1);
return result;
}
static inline NWC24Err NWC24iRequestShutdown(u32 event, NWC24Err* result) {
static u32 shtBuffer[8] ALIGN32;
static u32 shtResult[8] ALIGN32;
shtBuffer[0] = event;
return NWC24IoctlResourceManagerAsync(nwc24ShtFd, NWC24_IOCTL_SHUTDOWN, shtBuffer, sizeof(shtBuffer), shtResult, sizeof(shtResult), (s32*)result);
}
BOOL NWC24Shutdown(BOOL final, u32 event) {
static BOOL shuttingdown = FALSE;
static NWC24Err result = NWC24_OK;
if (final)
return TRUE;
if (shuttingdown) {
if (NWC24IsAsyncRequestPending())
return FALSE;
if (result >= 0)
return TRUE;
if (nwc24ShtRetryRest > 0) {
shuttingdown = FALSE;
nwc24ShtRetryRest--;
} else
return TRUE;
} else if (NWC24iRequestShutdown(event, &result) >= 0)
shuttingdown = TRUE;
return FALSE;
}
NWC24Err NWC24DoDailyTasks(void* work) {
NWC24Err result = NWC24_OK;
SCIdleModeInfo idleMode;
u32 status;
u32 restrictions;
u32 tick = OSGetTick();
u32 randomValue = rand();
if ((randomValue + tick) % 10 != 0)
return NWC24_OK;
if (NWC24IsMsgLibOpened())
return NWC24_ERR_LIB_OPENED;
do {
status = SCCheckStatus();
if (status == 2)
return NWC24_ERR_FATAL;
OSSleepTicks(OSMillisecondsToTicks((OSTime)100));
} while (status != 0);
if (!SCGetIdleMode(&idleMode))
return NWC24_ERR_NOT_FOUND;
restrictions = SCGetNetContentRestrictions() & 2;
if (idleMode.standby == 0) {
if (restrictions == 0) {
result = NWC24OpenLib(work);
if (result >= 0) {
result = NWC24iMBoxSetLastUIDL(NWC24_MBOX_TYPE_RECV, "");
NWC24CloseLib();
}
} else
result = NWC24AdjustUniversalTime();
}
return result;
}

View File

@@ -1,310 +0,0 @@
#define MYTILETTERFORM_IMPLEMENTATION
#include "keyboard/MyTiLetterForm.h"
#include "keyboard/tiUtil.h"
#include <nw4r/lyt/textbox.h>
#include <new>
namespace textinput {
namespace inputform {
class EventHandler : public nw4rmanager::TiEventHandler {
public:
EventHandler(textinput::InputForm* form) : mpInputForm(form) {}
virtual ~EventHandler() {}
virtual void onTiEvent(gui::PaneComponent*, u32, Input*);
protected:
textinput::InputForm* mpInputForm;
};
}
namespace extend {
namespace memo {
class EventHandler : public inputform::EventHandler {
public:
EventHandler(InputForm* form) : inputform::EventHandler(form), mpMemoForm(form) {}
virtual ~EventHandler() {}
virtual void onTiEvent(gui::PaneComponent*, u32, Input*);
protected:
InputForm* mpMemoForm;
};
}
namespace letter {
class EventHandler : public memo::EventHandler {
public:
EventHandler(InputForm* form) : memo::EventHandler(form) {}
virtual ~EventHandler();
virtual void onTiEvent(gui::PaneComponent*, u32, Input*);
};
class AnmPane : public nw4rmanager::AnmPane {
protected:
enum KeyType { KT_Whole, KT_Pic };
u32 meState;
KeyType meKeyType;
public:
AnmPane(nw4r::lyt::Pane* pane, nw4rmanager::AnmObserver* observer)
: nw4rmanager::AnmPane(pane, observer), meState(0), meKeyType(KT_Whole) {}
virtual ~AnmPane();
virtual void init();
virtual void changeAnimation(u32 id);
virtual KeyType getKeyType() const;
virtual u32 getState();
};
class WholePane : public AnmPane {
public:
WholePane(nw4r::lyt::Pane* pane, nw4rmanager::AnmObserver* observer)
: AnmPane(pane, observer) { meKeyType = KT_Whole; }
virtual ~WholePane();
virtual void onAnmEvent(AnmPaneEvent paneEvent);
};
class PicPane : public AnmPane {
public:
PicPane(nw4r::lyt::Pane* pane, nw4rmanager::AnmObserver* observer)
: AnmPane(pane, observer) { meKeyType = KT_Pic; }
virtual ~PicPane();
virtual void onAnmEvent(AnmPaneEvent paneEvent);
};
static const char* paneNames[] = {
"N_Header", "N_Body", "B_Body", "N_Footer", "Nigaoe",
"T_Nigaoe", "B_Nigaoe", "T_Letter", "T_TouchLetter"
};
struct AnimationFile { u32 id; char fileName[64]; };
static const AnimationFile animationFiles[] = {
{0, "my_LetterL_PicFocusIn.brlan"},
{1, "my_LetterL_SelectPic.brlan"},
{2, "my_LetterL_ExitPic.brlan"},
{3, "my_LetterL_PicFocusOut.brlan"},
{4, "my_LetterL_ReturnIn.brlan"},
{5, "my_LetterL_SendOut.brlan"},
{6, "my_LetterL_MailIn.brlan"},
{7, "my_LetterL_MailOut.brlan"},
};
struct PaneToAnimationGroup {
s32 type;
const char* paneName;
u32 count;
const AnimationFile* animations[12];
};
static const PaneToAnimationGroup animationGroups[] = {
{0, "N_MemoRoot", 4, {&animationFiles[4], &animationFiles[5], &animationFiles[6], &animationFiles[7]}},
{0, "SendPic", 4, {&animationFiles[4], &animationFiles[5], &animationFiles[6], &animationFiles[7]}},
{1, "N_Pic", 5, {&animationFiles[0], &animationFiles[1], &animationFiles[2], &animationFiles[3], &animationFiles[4]}},
{0, "N_SendMes", 1, {&animationFiles[5]}}
};
void InputForm::create(MEMAllocator* allocator, textinput::inputform::EditBuffer* editBuffer) {
memo::InputForm::create(allocator, editBuffer);
MEMFreeToAllocator(allocator, mpInputEventHandler);
mpInputEventHandler = new (MEMAllocFromAllocator(allocator, sizeof(EventHandler))) EventHandler(this);
mpInputEventHandler->setEventObserver(mpEventObserver);
mpPaneManager->changeEventHandler(mpInputEventHandler);
for (u16 i = 0; i < 4; i++) {
const PaneToAnimationGroup* group = &animationGroups[i];
AnmPane* pane = NULL;
switch (group->type) {
case 1:
pane = new (MEMAllocFromAllocator(allocator, sizeof(PicPane))) PicPane(
getPane(group->paneName), NULL);
break;
case 0:
pane = new (MEMAllocFromAllocator(allocator, sizeof(WholePane))) WholePane(
getPane(group->paneName), NULL);
break;
}
nw4r::ut::List_Append(&mAnmPanes, pane);
for (u16 j = 0; j < group->count; j++) {
void* resource = mpMultiArcResourceAccessor->GetResource(0, group->animations[j]->fileName);
AnimTransformPane* transform = static_cast<AnimTransformPane*>(
getLayout()->CreateAnimTransform(resource, mpMultiArcResourceAccessor));
pane->addAnimation(allocator, group->animations[j]->id, transform, false, true);
}
}
searchAnmPane("N_Pic")->changeAnimation(3);
mpLayout->Animate(0);
mpLayout->CalculateMtx(mDrawInfo);
mbPhotoScaledUp = 0;
mbPhotoDraw = false;
mbCloseWithSend = false;
}
AnmPane::~AnmPane() {}
void InputForm::drawBody() {
nw4r::lyt::Pane* bodyPane = getPane(paneNames[1]);
f32 y = 0.0f;
for (int row = 0; row < (mnLine - 1) / 4 + 1; row++) {
bodyPane->SetTranslate(nw4r::math::VEC2(0.0f, y));
bodyPane->CalculateMtx(mDrawInfo);
bodyPane->Draw(mDrawInfo);
y -= bodyPane->GetSize().height;
}
if (!mbEdited) {
getPane("T_TouchLetter")->Draw(mDrawInfo);
}
getPane("SendPic")->Draw(mDrawInfo);
getPane("N_SendMes")->Draw(mDrawInfo);
nw4r::lyt::Pane* scrollPane = getPane("N_txt_scrl");
f32 x = scrollPane->GetTranslate().x;
getPane("N_txt_scrl")->SetTranslate(nw4r::math::VEC2(x, -6.0f));
}
void InputForm::drawFooter() {
nw4r::lyt::Pane* bodyPane = getPane(paneNames[1]);
int row = (mnLine - 1) / 4;
f32 y = static_cast<f32>(row) * (bodyPane->GetSize().height * -1.0f);
nw4r::lyt::Pane* footerPane = getPane(paneNames[3]);
footerPane->SetTranslate(nw4r::math::VEC2(0.0f, y));
footerPane->CalculateMtx(mDrawInfo);
footerPane->Draw(mDrawInfo);
if (mbPhotoDraw) {
getPane("N_Pic")->Draw(mDrawInfo);
}
}
nw4r::lyt::Material* InputForm::getPhotoPaneMaterial() {
return getPane("Pic")->FindMaterialByName("Pic", true);
}
void InputForm::onPhotoTrig() {
if (getEditMode() != memo::InputForm::EM_Disp || !mbPhotoDraw) {
return;
}
}
void InputForm::onPhotoPoint() {
if (getEditMode() != memo::InputForm::EM_Disp || mbPhotoScaledUp || !mbPhotoDraw) {
return;
}
}
void InputForm::onPhotoLeft() {
if (getEditMode() != memo::InputForm::EM_Disp || mbPhotoScaledUp || !mbPhotoDraw) {
return;
}
}
void InputForm::open() {
memo::InputForm::open();
switch (meType) {
case T_MailAddressSel:
searchAnmPane("N_MemoRoot")->changeAnimation(6);
break;
case T_Address:
case T_Picture:
case T_Reply:
default:
searchAnmPane("N_MemoRoot")->changeAnimation(4);
break;
}
}
void InputForm::close() {
memo::InputForm::close();
if (mbCloseWithSend) {
searchAnmPane("N_MemoRoot")->changeAnimation(5);
searchAnmPane("SendPic")->changeAnimation(5);
searchAnmPane("N_SendMes")->changeAnimation(5);
} else {
searchAnmPane("N_MemoRoot")->changeAnimation(7);
searchAnmPane("SendPic")->changeAnimation(7);
}
}
bool InputForm::isWholePaneInAnimation() {
return searchAnmPane("N_MemoRoot")->isInAnimation();
}
void InputForm::resizePhotoPane(f32 width, f32 height) {
nw4r::lyt::Pane* pane = getPane("Pic");
pane->SetSize(nw4r::lyt::Size(412.0f, 309.0f));
nw4r::lyt::Size bounds(pane->GetSize());
nw4r::lyt::Size result(bounds);
if (0.0f == width) {
return;
}
if (0.0f == height) {
return;
}
if (width > height) {
result.height = bounds.width * height / width;
if (result.height > bounds.height) {
result.width = bounds.width * (bounds.height / result.height);
result.height = bounds.height;
}
} else {
result.width = bounds.height * width / height;
if (result.width > bounds.width) {
result.height = bounds.height * (bounds.width / result.width);
result.width = bounds.width;
}
}
pane->SetSize(result);
}
void InputForm::setSendOutMessage(const wchar_t* sendOutMessage) {
nw4r::ut::DynamicCast<nw4r::lyt::TextBox*>(
mpLayout->GetRootPane()->FindPaneByName("T_SendMes", true))->SetString(sendOutMessage);
}
void EventHandler::onTiEvent(gui::PaneComponent* paneComponent, u32 event, Input* input) {
if (!static_cast<InputForm*>(mpMemoForm)->isPhotoScaledUp()) {
memo::EventHandler::onTiEvent(paneComponent, event, input);
}
nw4r::lyt::Pane* pane = paneComponent->getPane();
const char* paneName = pane->GetName();
if (paneName[0] == 'B') {
if (event == gui::GUIComponent::EVENT_TRIG && (input->trigger & 0x800) &&
util::strcmp("B_Pic", paneName)) {
static_cast<InputForm*>(mpMemoForm)->onPhotoTrig();
}
if (event == 0 && util::strcmp("B_Pic", paneName)) {
static_cast<InputForm*>(mpMemoForm)->onPhotoPoint();
}
if (event == 1 && util::strcmp("B_Pic", paneName)) {
static_cast<InputForm*>(mpMemoForm)->onPhotoLeft();
}
}
}
bool InputForm::isPhotoScaledUp() {
return mbPhotoScaledUp;
}
u32 AnmPane::getState() {
return meState;
}
void AnmPane::changeAnimation(u32 id) {
meState = id;
nw4rmanager::AnmPane::changeAnimation(id);
}
AnmPane::KeyType AnmPane::getKeyType() const {
return meKeyType;
}
void AnmPane::init() {
meState = 0;
}
PicPane::~PicPane() {}
void PicPane::onAnmEvent(AnmPaneEvent paneEvent) {}
WholePane::~WholePane() {}
void WholePane::onAnmEvent(AnmPaneEvent paneEvent) {}
InputForm::~InputForm() {}
EventHandler::~EventHandler() {}
}
}
}

View File

@@ -1,534 +0,0 @@
#define TIHKBMANAGER_IMPLEMENTATION
#include "keyboard/tiHKBManager.h"
#include <revolution/os.h>
extern "C" {
_KBDEc KBDSetLedsAsync(u8, u8, void (*)(_KBDEc, void*), void*);
void KBDSetLockProcessing(u8, int);
int KBDSetCountry(u8, u32);
int KBDSetModState(u8, u32);
_KBDEc KBDSetLeds(u8, u8);
u16 KBDTranslateHidCode(u8, u32, u32);
void KBDInitRegionUS();
void KBDInit();
void KBDSetAttachCallback(void (*)(KBDDevEvent*));
void KBDSetDetachCallback(void (*)(KBDDevEvent*));
void KBDSetKeyCallback(void (*)(KBDKeyEvent*));
}
namespace textinput {
namespace input {
HKBManager HKBManager::sInstance;
// KBD callbacks encode the device in the first byte of the opaque pointer value.
struct LedCallbackData {
union {
void* pointer;
u8 device;
};
u32 leds;
};
void HKBManager::SetLedCB(_KBDEc result, void* userData) {
if (result == KBD_EC_BUSY) {
LedCallbackData received;
received.pointer = userData;
LedCallbackData callback;
callback.device = received.device;
const u8 leds = callback.leds = 0;
u32 interrupts = OSDisableInterrupts();
u32 mask = 1 << received.device;
sInstance.retryDevices &= ~mask;
OSRestoreInterrupts(interrupts);
if (KBDSetLedsAsync(received.device, leds, SetLedCB, callback.pointer) == KBD_EC_BUSY) {
interrupts = OSDisableInterrupts();
sInstance.retryDevices |= mask;
OSRestoreInterrupts(interrupts);
}
}
}
void HKBManager::KBDListenerOwn::OnAttach(KBDDevEvent* event) {
HKBManager* owner;
u8 device;
if (event->device >= 2) {
KBDSetLockProcessing(event->device, 0);
device = event->device;
owner = manager;
LedCallbackData callback;
callback.device = device;
const u8 leds = callback.leds = 0;
u32 interrupts = OSDisableInterrupts();
u32 mask = 1 << device;
owner->retryDevices &= ~mask;
OSRestoreInterrupts(interrupts);
if (KBDSetLedsAsync(device, leds, SetLedCB, callback.pointer) == KBD_EC_BUSY) {
interrupts = OSDisableInterrupts();
owner->retryDevices |= mask;
OSRestoreInterrupts(interrupts);
}
} else {
manager->attached[event->device] = 1;
KBDSetLockProcessing(event->device, 0);
manager->states[event->device].retryLeds = 1;
}
}
void HKBManager::KBDListenerOwn::OnDetach(KBDDevEvent* event) {
u8 device = event->device;
if (device >= 2) return;
HKBManager* owner = manager;
owner->attached[device] = 0;
owner->states[device].Clear();
}
void HKBManager::KBDListenerOwn::OnKeyEvent(KBDKeyEvent* event) {
if (event->device >= 2) return;
if (event->flags & 2) return;
manager->states[event->device].NotifyEvent(event->flags & 1, event->key);
}
void HKBManager::AttachCB(KBDDevEvent* event) {
KBDListenerOwn* current = &sInstance.listener;
while (current != 0) {
current->OnAttach(event);
current = current->next;
if (current == &sInstance.listener) break;
}
}
void HKBManager::DetachCB(KBDDevEvent* event) {
KBDListenerOwn* current = &sInstance.listener;
while (current != 0) {
current->OnDetach(event);
current = current->next;
if (current == &sInstance.listener) break;
}
}
void HKBManager::KeyEventCB(KBDKeyEvent* event) {
KBDListenerOwn* current = &sInstance.listener;
while (current != 0) {
current->OnKeyEvent(event);
current = current->next;
if (current == &sInstance.listener) break;
}
}
HKBManager::HKBManager()
: initialized(0), lockState(0), retryDevices(0), allowedCharacters(0),
allowedCharacterCount(0), listener(this) {
for (u32 device = 0; device < 2; device++) {
attached[device] = 0;
states[static_cast<u8>(device)].Initialize(static_cast<u8>(device));
}
}
HKBManager::KBDListenerOwn::~KBDListenerOwn() {}
void HKBManager::Initialize() {
if (initialized == 0) {
initialized = 1;
KBDInitRegionUS();
KBDInit();
if (listener.previous == 0) {
KBDSetAttachCallback(AttachCB);
KBDSetDetachCallback(DetachCB);
KBDSetKeyCallback(KeyEventCB);
}
u32 device = 0;
do {
KBDSetCountry(static_cast<u8>(device), states[static_cast<u8>(device)].country);
device++;
} while (device < 2);
}
}
void HKBManager::ClearState() {
for (u32 device = 0; device < 2; device++) {
states[static_cast<u8>(device)].Clear();
}
}
u32 HKBManager::GetModifierState() const {
u32 modifiers = states[0].modifiers;
u32 mask = states[0].forceMask;
modifiers &= ~mask;
modifiers |= states[0].forceState & mask;
u32 forceMask;
u32 force;
force = states[1].modifiers;
forceMask = states[1].forceMask;
force &= ~forceMask;
force |= states[1].forceState & forceMask;
return modifiers | force;
}
void HKBManager::SetCountry(u8 country) {
KeyState_* state;
u32 device = 0;
do {
state = &states[static_cast<u8>(device)];
state->country = country;
KBDSetCountry(device & 0xff, country);
KBDSetModState(state->device, state->modifiers);
u32 modifiers = state->modifiers;
u8 leds = 0;
if ((modifiers & 0x100) != 0) {
leds |= 1;
}
if ((modifiers & 0x200) != 0) {
leds |= 2;
}
if ((modifiers & 0x400) != 0) {
leds |= 4;
}
if (KBDSetLeds(state->device, leds) == 7) {
state->retryLeds = 1;
}
device++;
} while (device < 2);
}
void HKBManager::SetModifierState(u32 state, u32 mask) {
lockState = lockState & ~mask | state & mask;
KeyState_* slot;
u32 modifiers;
u32 global;
u32 device = 0;
do {
slot = &states[static_cast<u8>(device)];
modifiers = slot->modifiers;
global = lockState & 0x700;
bool changed = global != (modifiers & 0x700);
slot->modifiers &= ~0x700;
slot->modifiers |= lockState;
if (changed &&
attached[static_cast<u8>(device)] != 0) {
KBDSetModState(slot->device, slot->modifiers);
modifiers = slot->modifiers;
u8 leds = 0;
if ((modifiers & 0x100) != 0) {
leds |= 1;
}
if ((modifiers & 0x200) != 0) {
leds |= 2;
}
if ((modifiers & 0x400) != 0) {
leds |= 4;
}
if (KBDSetLeds(slot->device, leds) == 7) {
slot->retryLeds = 1;
}
}
device++;
} while (device < 2);
}
void HKBManager::SetForceModifierState(u32 state, u32 mask) {
states[0].forceState = state;
states[0].forceMask = mask;
states[1].forceState = state;
states[1].forceMask = mask;
}
void HKBManager::Update() {
if (initialized != 0) {
u32 mask;
u32 device = 2;
do {
u8 shiftIndex = static_cast<u8>(device);
mask = 1 << shiftIndex;
if ((retryDevices & mask) != 0) {
LedCallbackData callback;
callback.device = static_cast<u8>(device);
const u8 leds = callback.leds = 0;
u32 interrupts = OSDisableInterrupts();
retryDevices &= ~mask;
OSRestoreInterrupts(interrupts);
if (KBDSetLedsAsync(device & 0xff, leds,
SetLedCB,
callback.pointer) == 7) {
interrupts = OSDisableInterrupts();
retryDevices |= mask;
OSRestoreInterrupts(interrupts);
}
}
device++;
} while (device < 4);
u32 updateIndex = 0;
do {
states[updateIndex & 0xff].Update();
updateIndex++;
} while (updateIndex < 2);
}
}
HKBManager::KeySet HKBManager::GetTriggeredKeySet() const {
KeySet keys;
keys.manager = this;
keys.type = 1;
keys.index = -1;
keys.device = 0;
keys.vcode = 0;
return keys.GetNext();
}
HKBManager::KeySet HKBManager::GetReleasedKeySet() const {
KeySet keys;
keys.manager = this;
keys.type = 2;
keys.index = -1;
keys.device = 0;
keys.vcode = 0;
return keys.GetNext();
}
HKBManager::KeySet HKBManager::GetRepeatedKeySet() const {
KeySet keys;
keys.manager = this;
keys.type = 3;
keys.index = -1;
keys.device = 0;
keys.vcode = 0;
return keys.GetNext();
}
void HKBManager::KeyState_::NotifyEvent(u8 down, u8 code) {
u32 index;
s32 found = -1;
if (down == 1) {
for (index = 0; index < 8; index++) {
if ((pending & (1 << index)) != 0) {
if (code == pendingKeys[index]) { found = index; break; }
} else {
found = index;
break;
}
}
if (found >= 0) {
pendingKeys[found] = code;
pending |= 1 << found;
}
} else {
for (index = 0; index < 8; index++) {
if ((pending & (1 << index)) && code == pendingKeys[index]) {
found = index;
break;
}
}
if (found >= 0) pending &= ~(1 << found);
}
}
void HKBManager::KeyState_::Update() {
u32 interrupts = OSDisableInterrupts();
previous = current;
current = pending;
for (u32 slot = 0; slot < 8; slot++) {
previousKeys[slot] = currentKeys[slot];
currentKeys[slot] = pendingKeys[slot];
}
OSRestoreInterrupts(interrupts);
UpdateModState_();
u32 bit;
u32 index;
u32 heldKeys = current;
triggered = 0;
held = heldKeys;
for (index = 0; index < 8; index++) {
bit = 1 << index;
if ((current & bit) != 0) {
if ((previous & bit) == 0 || previousKeys[index] != currentKeys[index]) {
triggered |= bit;
}
}
}
released = 0;
for (index = 0; index < 8; index++) {
bit = 1 << index;
if ((previous & bit) != 0) {
if ((current & bit) == 0 || previousKeys[index] != currentKeys[index]) {
released |= bit;
}
}
}
repeated = 0;
for (index = 0; index < 8; index++) {
bit = 1 << index;
if ((current & bit) != 0) {
if ((triggered & bit) != 0) {
repeatDelay[index] = 30;
repeated |= bit;
}
repeatDelay[index]--;
if (repeatDelay[index] == 0) {
repeatDelay[index] = 8;
repeated |= bit;
}
}
}
}
void HKBManager::KeyState_::UpdateModState_() {
u32 index;
u32 oldModifiers = modifiers;
modifiers = oldModifiers & 0xffffffd0;
index = 0;
for (; index < 8; index++) {
u32 keySet;
u8 slot;
keySet = current;
slot = index;
if ((keySet & (1 << slot)) != 0) {
u8 code = currentKeys[slot];
if (code == 0xe5 || code == 0xe1) {
modifiers |= 2;
} else if (code == 0xe4 || code == 0xe0) {
modifiers |= 1;
} else if (code == 0xe7 || code == 0xe3) {
modifiers |= 8;
} else if (code == 0xe2) {
modifiers |= 4;
} else if (code == 0xe6) {
if (static_cast<s32>(country) == 0xf ||
static_cast<s32>(country) == 0x21) {
modifiers |= 4;
} else {
modifiers |= 0x20;
}
}
}
}
if ((oldModifiers & 0x700) != (modifiers & 0x700) || retryLeds != 0) {
retryLeds = 0;
KBDSetModState(device, modifiers);
u32 modifiers = this->modifiers;
u8 leds = 0;
if ((modifiers & 0x100) != 0) {
leds |= 1;
}
if ((modifiers & 0x200) != 0) {
leds |= 2;
}
if ((modifiers & 0x400) != 0) {
leds |= 4;
}
if (KBDSetLeds(device, leds) == 7) {
retryLeds = 1;
}
}
}
u8 HKBManager::KeySet::GetKey() const {
if (!CheckValidity()) return 0;
switch (type) {
case 0: return manager->states[device].currentKeys[index];
case 1: return manager->states[device].currentKeys[index];
case 2: return manager->states[device].previousKeys[index];
case 3: return manager->states[device].currentKeys[index];
}
return 0;
}
u32 HKBManager::KeySet::GetWChar() const {
s32 code = GetVCode();
if (static_cast<u16>(code) >= 0xf130 && static_cast<u16>(code) <= 0xf139) {
code = code - 0xf100 & 0xffff;
} else {
u32 lowCode = code & 0xffff;
if (lowCode == 0xf10d) {
code = 0xf1cd;
} else if (lowCode >= 0xf100 && lowCode <= 0xf13f) {
code = code - 0xf100 & 0xffff;
} else if ((static_cast<u32>(code) & 0xffff) >= 0xf140 && (static_cast<u32>(code) & 0xffff) <= 0xf17f) {
code = code + 0x40 & 0xffff;
}
}
u16 character = code;
if ((character & 0xf000) == 0xf000) {
code = 0;
} else if ((static_cast<u32>(code) & 0xffff) == 0xeeee) {
code = 0;
} else if ((static_cast<u32>(code) & 0xffff) < 0x20) {
code = 0;
}
u32 index = 0;
const HKBManager* owner = manager;
if (owner->allowedCharacters == 0) return code;
u32 count = owner->allowedCharacterCount;
if (count == 0) return code;
for (; index < count; index++) {
if ((static_cast<u32>(code) & 0xffff) == owner->allowedCharacters[index]) return code;
}
return 0;
}
u32 HKBManager::KeySet::GetVCode() const {
u32 modifiers;
if (!CheckValidity()) return 0;
if (vcode) return vcode;
u32 first = manager->states[0].modifiers;
u32 mask = manager->states[0].forceMask;
first &= ~mask;
first |= manager->states[0].forceState & mask;
u32 secondMask;
u32 second;
second = manager->states[1].modifiers;
secondMask = manager->states[1].forceMask;
second &= ~secondMask;
second |= manager->states[1].forceState & secondMask;
modifiers = first | second;
if ((modifiers & 0xd) != 0 && (modifiers & 5) != 5) return 0;
switch (type) {
case 0:
case 1:
case 3:
if ((manager->states[device].current & (1 << index)) != 0) {
vcode = KBDTranslateHidCode(manager->states[device].currentKeys[index], modifiers,
manager->states[device].country);
}
break;
case 2:
if ((manager->states[device].previous & (1 << index)) != 0) {
vcode = KBDTranslateHidCode(manager->states[device].previousKeys[index], modifiers,
manager->states[device].country);
}
break;
}
return vcode;
}
u32 HKBManager::KeySet::IsValid() const {
return CheckValidity();
}
HKBManager::KeySet HKBManager::KeySet::GetNext() const {
KeySet next(manager, type);
if (manager != 0) {
next.device = device;
while (next.device < 2) {
u32 keyFlags = 0;
switch (type) {
case 0: keyFlags = manager->states[next.device].held; break;
case 1: keyFlags = manager->states[next.device].triggered; break;
case 2: keyFlags = manager->states[next.device].released; break;
case 3: keyFlags = manager->states[next.device].repeated; break;
}
next.index = index + 1;
while (next.index < 8) {
if ((keyFlags & (1 << next.index)) != 0) return next;
next.index++;
}
next.index = -1;
next.device++;
}
}
return next;
}
HKBManager::~HKBManager() {}
}
}

View File

@@ -1,495 +0,0 @@
#define TISTRING_IMPLEMENTATION
#include "keyboard/tiString.h"
#include "keyboard/tiUtil.h"
#include <wchar.h>
extern "C" {
u8 KPRLookAhead(KPRQueue*, wchar_t*, u32);
u32 KPRPutChar(KPRQueue*, wchar_t);
wchar_t KPRGetChar(KPRQueue*);
void KPRClearQueue(KPRQueue*);
void KPRInitQueue(KPRQueue*);
void KPRSetMode(KPRQueue*, u32);
}
namespace textinput {
namespace tistring {
Decolated::~Decolated() {}
void StringBase::create(MEMAllocator* allocator) {
mpAllocator = allocator;
mpszString = static_cast<wchar_t*>(MEMAllocFromAllocator(allocator, muMaxLength << 1));
wmemset(mpszString, 0, muMaxLength);
muLength = 0;
mpszTmpString = static_cast<wchar_t*>(MEMAllocFromAllocator(allocator, muMaxLength << 1));
wmemset(mpszTmpString, 0, muMaxLength);
}
StringBase::~StringBase() {
MEMFreeToAllocator(mpAllocator, mpszString);
MEMFreeToAllocator(mpAllocator, mpszTmpString);
}
void StringBase::pushBack(wchar_t ch) {
wchar_t buffer[10];
buffer[0] = ch;
buffer[1] = 0;
u32 length = wcslen(buffer);
wcsncat(mpszString, buffer, length);
muLength++;
}
void StringBase::popBack() {
if (muLength != 0) {
muLength--;
}
mpszString[muLength] = 0;
}
bool StringBase::append(const wchar_t* string) {
u32 inputLength = wcslen(string);
u16 length = getLength();
if (inputLength + length > muMaxLength) {
return false;
}
if (string == 0) {
return false;
}
while (*string != 0) {
mpszString[muLength] = *string;
muLength++;
string++;
}
mpszString[muLength] = 0;
return true;
}
bool StringBase::insert(u16 position, const wchar_t* string) {
u32 inputLength = wcslen(string);
u32 length = getLength();
s32 total = (inputLength & 0xffff) + (length & 0xffff);
bool success;
if (total > muMaxLength) {
success = false;
} else if (string == 0 || static_cast<u32>(position) > static_cast<u32>(getLength() & 0xffff)) {
success = false;
} else {
wmemset(mpszTmpString, 0, muMaxLength);
wcsncpy(mpszTmpString, mpszString + position, (getLength() & 0xffff) - position);
muLength = position;
mpszString[position] = 0;
append(string);
append(mpszTmpString);
success = true;
}
return success;
}
void StringBase::remove(u16 position, u16 count) {
u16 length = getLength();
if (length != 0 && position <= getLength()) {
s32 currentLength = getLength() & 0xffff;
s32 end = position + count;
if (end >= currentLength) {
count = getLength() - position;
}
wmemset(mpszTmpString, 0, muMaxLength);
wcsncpy(mpszTmpString, mpszString + position + count, (getLength() & 0xffff) - (position + count));
muLength = position;
mpszString[position] = 0;
append(mpszTmpString);
}
}
void StringBase::replace(u16 position, u16 count, const wchar_t* value) {
s32 length = getLength();
if (position + count >= length) {
count = getLength() - position;
}
remove(position, count);
insert(position, value);
}
void StringBase::set(const wchar_t* string) {
clear();
append(string);
}
void StringBase::setAt(u16 index, wchar_t ch) {
if (muMaxLength <= index) {
return;
}
mpszString[index] = ch;
}
void StringBase::setLength(u16 length) {
for (u16 index = length; index < muLength; index++) {
setAt(index, 0);
}
muLength = length;
}
void StringBase::clear() {
wmemset(mpszString, 0, muMaxLength);
muLength = 0;
}
wchar_t StringBase::getLastWChar() {
s32 index = muLength - 1;
if (index >= 0) {
return mpszString[index];
}
return 0;
}
void Decolated::inputChar(wchar_t ch) {
if (ch != 0xfffe) {
wchar_t input[8];
input[4] = 0;
input[3] = 0;
input[2] = 0;
input[1] = 0;
input[0] = 0;
u16 count;
if (static_cast<u32>(mTranslateMode) - 1 > 1) {
goto nonKana;
}
{
mKanaStream.mOutput[4] = 0;
mKanaStream.mOutput[3] = 0;
mKanaStream.mOutput[2] = 0;
mKanaStream.mOutput[1] = 0;
mKanaStream.mOutput[0] = 0;
if (ch == 10 && KPRLookAhead(&mKanaStream.mQueue, 0, 0)) {
count = KPRPutChar(&mKanaStream.mQueue, 0xffff) & 0xff;
} else {
ch = util::toWLower(ch);
count = KPRPutChar(&mKanaStream.mQueue, ch) & 0xff;
}
for (u32 inputIndex = 0; (inputIndex & 0xff) < count; inputIndex++) {
input[static_cast<u8>(inputIndex)] = KPRGetChar(&mKanaStream.mQueue);
}
KPRLookAhead(&mKanaStream.mQueue, mKanaStream.mOutput, 5);
goto inputReady;
}
nonKana:
if (mTranslateMode == 3) {
input[0] = ch;
input[1] = 0;
count = 1;
mKanaStream.mOutput[0] = 0;
} else {
count = 1;
input[0] = ch;
input[1] = 0;
}
inputReady:
setCandidate(0);
u32 start;
u32 end;
getSelected(start, end);
if (start == end) {
insert(mCursorStart, input);
u32 currentCursor = mCursorStart;
mCursorStart = currentCursor + static_cast<u16>(count);
} else {
replace(start, end - start, input);
mCursorStart = start + static_cast<u16>(count);
}
mCursorEnd = mCursorStart;
}
}
void Decolated::confirmKana() {
inputChar(10);
}
void Decolated::clearKana() {
KPRClearQueue(&mKanaStream.mQueue);
mKanaStream.mOutput[0] = 0;
}
void Decolated::inputString(const wchar_t* string) {
if (string != 0) {
const wchar_t* current = string;
for (; *current != 0; current++) {
inputChar(*current);
}
}
}
void Decolated::inputString(const wchar_t* string, TranslateMode mode) {
TranslateMode oldMode = static_cast<TranslateMode>(mTranslateMode);
if (oldMode != mode) {
if (!isKanaFix() && mode == 0) {
confirmKana();
}
mTranslateMode = mode;
switch (mode) {
case 1:
mKanaStream.mQueue.mode = static_cast<KPRMode>(4);
break;
case 2:
mKanaStream.mQueue.mode = static_cast<KPRMode>(8);
break;
default:
break;
}
}
inputString(string);
if (mTranslateMode != oldMode) {
if (!isKanaFix() && oldMode == 0) {
confirmKana();
}
mTranslateMode = oldMode;
switch (oldMode) {
case 1:
mKanaStream.mQueue.mode = static_cast<KPRMode>(4);
break;
case 2:
mKanaStream.mQueue.mode = static_cast<KPRMode>(8);
break;
default:
break;
}
}
}
void Decolated::backSpace() {
if (static_cast<u32>(mTranslateMode) - 1 <= 1 && KPRLookAhead(&mKanaStream.mQueue, 0, 0)) {
mKanaStream.mQueue.iCount--;
u32 count = KPRLookAhead(&mKanaStream.mQueue, 0, 0);
mKanaStream.mOutput[count] = 0;
} else {
wchar_t empty[1];
u32 start;
u32 end;
getSelected(start, end);
if (start != end || start != 0) {
if (start == end) {
if (hasCandidate()) {
setCandidate(0);
} else {
if (mCursorStart != 0) {
mCursorStart--;
}
remove(mCursorStart, 1);
}
} else {
empty[0] = 0;
replace(start, end - start, empty);
mCursorStart = start;
}
mCursorEnd = mCursorStart;
}
}
}
void Decolated::confirm(const wchar_t* string) {
if (string != 0) {
insert(mCursorStart, string);
u32 length = wcslen(string);
mCursorStart += length;
mCursorEnd = mCursorStart;
}
}
bool Decolated::moveCursorRight() {
if (mCursorStart < getLength()) goto moveRight;
return mbSustain == 0;
moveRight:
mCursorStart++;
if (mbSustain == 0) {
mCursorEnd = mCursorStart;
}
return false;
}
bool Decolated::moveCursorLeft() {
if (mCursorStart == 0) {
return false;
}
mCursorStart--;
if (mbSustain == 0) {
mCursorEnd = mCursorStart;
}
return true;
}
void Decolated::setCursorPos(u32 position) {
u8 sustain = mbSustain;
mCursorStart = position;
if (sustain == 0) {
mCursorEnd = position;
}
}
void Decolated::onSustain() {
mbSustain = 1;
mCursorEnd = mCursorStart;
}
void Decolated::offSustain() {
mbSustain = 0;
}
void Decolated::getCursorPos(u32* start, u32* end) {
*start = mCursorStart;
*end = mCursorEnd;
}
bool Decolated::canBackSpace() {
if (mCursorStart != 0) goto canBackSpaceTrue;
if (mCursorStart != 0) goto checkKanaFix;
if (mCursorStart == mCursorEnd) goto checkKanaFix;
canBackSpaceTrue:
return true;
checkKanaFix:
if (isKanaFix() == 0) {
return true;
}
return false;
}
bool Decolated::deleteForward() {
u32 cursor = mCursorStart;
if (cursor < getLength()) goto removeChar;
return false;
removeChar:
remove(cursor, 1);
return true;
}
void Decolated::getSelected(u32& start, u32& end) {
if (mCursorStart > mCursorEnd) {
start = mCursorEnd;
end = mCursorStart;
} else {
start = mCursorStart;
end = mCursorEnd;
}
}
wchar_t Decolated::getWCharAtCursor() {
u32 start;
u32 end;
getSelected(start, end);
if (end == 0) {
return 0;
}
return mpszString[end - 1];
}
void Decolated::replaceAtCursor(wchar_t ch) {
u32 start;
u32 end;
getSelected(start, end);
if (end != 0) {
mpszString[end - 1] = ch;
}
}
bool Decolated::isDakuten() {
return util::KBD_IsDakuten(getWCharAtCursor());
}
void Decolated::converDakuten() {
wchar_t ch = getWCharAtCursor();
replaceAtCursor(util::KBD_ConvertDakuten(ch));
}
bool Decolated::isHandaku() {
return util::KBD_IsHandaku(getWCharAtCursor());
}
void Decolated::converHandaku() {
wchar_t ch = getWCharAtCursor();
replaceAtCursor(util::KBD_ConvertHandaku(ch));
}
void Decolated::convertAll() {
wchar_t ch = getWCharAtCursor();
replaceAtCursor(util::KBD_ConvertAll(ch));
}
bool Decolated::isSmall() {
return util::KBD_IsSmall(getWCharAtCursor());
}
void Decolated::converSmall() {
wchar_t ch = getWCharAtCursor();
replaceAtCursor(util::KBD_ConvertSmall(ch));
}
bool Decolated::atTheBeginningOfASentence() {
if (mCursorStart == 0) {
return false;
}
if (getLength() < 1) {
return false;
}
u32 start;
u32 end;
getSelected(start, end);
if (start != end) {
return false;
}
s32 ch = getWCharAtCursor();
switch (ch) {
case 0x2e:
case 0x21:
case 0x3f:
return true;
default:
return false;
}
}
void Decolated::setTranslateMode(TranslateMode mode) {
if (mTranslateMode != mode) {
if (!isKanaFix() && mode == 0) {
confirmKana();
}
mTranslateMode = mode;
switch (mode) {
case 1:
mKanaStream.mQueue.mode = static_cast<KPRMode>(4);
break;
case 2:
mKanaStream.mQueue.mode = static_cast<KPRMode>(8);
break;
default:
break;
}
}
}
void Decolated::initKanaConverter() {
KPRInitQueue(&mKanaStream.mQueue);
KPRSetMode(&mKanaStream.mQueue, 0);
mKanaStream.mOutput[0] = 0;
s32 mode = mTranslateMode;
switch (mode) {
case 1:
mKanaStream.mQueue.mode = static_cast<KPRMode>(4);
break;
case 2:
mKanaStream.mQueue.mode = static_cast<KPRMode>(8);
break;
default:
break;
}
}
void Decolated::setLength(u16 length) {
for (u32 index = length; (index & 0xffff) < muLength; index++) {
setAt(static_cast<u16>(index), 0);
}
muLength = length;
if (mCursorStart > length) {
mCursorEnd = length;
mCursorStart = length;
}
}
}
}

View File

@@ -1,315 +0,0 @@
#define TITEXTDRAWER_IMPLEMENTATION
#include "keyboard/tiTextDrawer.h"
#include "keyboard/tiUtil.h"
#include <revolution/gx/GXCull.h>
#include <revolution/gx/GXGet.h>
#include <revolution/gx/GXTransform.h>
#include <revolution/mtx.h>
namespace textinput {
namespace textdrawer {
s32 snWidthTable[0x160];
void Base::create(MEMAllocator*) {
for (s32 index = 0; index < 0x160; ++index) {
snWidthTable[index] = -1;
}
mfVIWidth = 640.0f;
}
void Base::setAspectRatio(bool aspect4x3) {
mbAspect4x3 = aspect4x3;
if (aspect4x3) {
util::getProjectionRect4x3(&mProjectionRect);
} else {
util::getProjectionRect16x9(&mProjectionRect);
}
}
void Base::setDrawString(const wchar_t* wcString, u32 startPos, u32 endPos) {
mpDrawString = wcString;
muDrawStartPos = startPos;
muDrawEndPos = endPos;
}
void Base::modifyCursorCache(s32 line, u32 pos, f32 left, f32 top, f32 right, f32 bottom) {
f32 height = mfDrawScrollY;
mnCachedCursorLine = line;
mCachedCursor.uCursorPos = pos;
mCachedCursor.fCursorX = left;
mCachedCursor.fCursorY = top - height;
mfCachedCursorX = right;
mfCachedCursorY = bottom - height;
mbCursorCache = true;
}
void Base::setDrawModifyScopeLine(s32 startLine, s32 endLine) {
muDrawModifyStartLine = startLine;
muDrawModifyEndLine = endLine;
}
void Base::setDrawCacheScopeLine(s32 startLine, s32 endLine) {
if (startLine < 0) {
startLine = 0;
}
if (muDrawModifyStartPos != startLine) {
dirtyDrawCache();
}
muDrawModifyStartPos = startLine;
muDrawModifyEndPos = endLine;
}
void Base::procCursor(CursorPos* cursorPos, s32) {
GetCursorX();
GetCursorY();
if (!onCursor(cursorPos)) {
cursorPos->fCursorX = GetCursorX();
cursorPos->fCursorY = GetCursorY();
}
}
bool Base::onCursor(CursorPos*) {
return false;
}
void Base::makeUpCursorPos(CursorPos* cursorPos, u32 pos, s32 line, s32) {
const wchar_t* drawString = mpDrawString;
if (drawString != NULL) {
const wchar_t* ch = drawString + pos;
f32 y = GetCursorY();
while (*ch != 0) {
DrawInfo drawInfo;
drawInfo.rect.left = 0.0f;
drawInfo.rect.top = 0.0f;
drawInfo.rect.right = 0.0f;
drawInfo.rect.bottom = 0.0f;
drawInfo.character = *ch;
calcRect(drawInfo);
doBeforeDrawProcess(drawString, pos, drawInfo);
if (cursorPos != NULL && cursorPos->uCursorPos == pos) {
procCursor(cursorPos, line + (y == GetCursorY() ? 0 : 1));
}
if (*ch == L'\n') {
doLineFeed();
break;
}
MoveCursorX((drawInfo.rect.right - drawInfo.rect.left) * getScale().x);
doAfterDrawProcess(drawString, pos, drawInfo);
if (y != GetCursorY()) {
line++;
}
y = GetCursorY();
++ch;
++pos;
}
if (cursorPos != NULL && *ch == 0 && cursorPos->uCursorPos == pos) {
procCursor(cursorPos, line);
}
}
}
void Base::doBeforeDrawProcess(const wchar_t*, u32, const DrawInfo&) {}
void Base::doLineFeed() {
++muLine;
}
void Base::doAfterDrawProcess(const wchar_t*, u32, const DrawInfo&) {}
void Base::put(wchar_t wc) {
if (!mbSecretMode) {
Print(wc);
} else {
Print(L'*');
}
}
void Base::draw(CursorPos* cursorPos) {
u32 startPos;
const wchar_t* drawString = mpDrawString;
u32 endPos;
if (GetFont() != NULL) {
startPos = muDrawStartPos;
endPos = muDrawEndPos;
muCachedStartPos = startPos;
muDrawCacheStartPos = startPos;
preDraw(startPos);
drawString += startPos;
SetFontSize(mfFontWidth * getScale().x,
mfFontHeight * getScale().y);
SetupGX();
while (*drawString != 0 && muDrawCacheStartPos < endPos) {
DrawInfo drawInfo;
drawInfo.rect.left = 0.0f;
drawInfo.rect.top = 0.0f;
drawInfo.rect.right = 0.0f;
drawInfo.rect.bottom = 0.0f;
drawInfo.character = *drawString;
calcRect(drawInfo);
doBeforeDrawProcess(mpDrawString,
muDrawCacheStartPos,
drawInfo);
if (cursorPos != NULL && cursorPos->uCursorPos == muDrawCacheStartPos) {
procCursor(cursorPos, 0);
}
if (*drawString == L'\n') {
nw4r::ut::Color color = GetTextColor();
SetTextColor(nw4r::ut::Color(200, 200, 200, color.a));
Print(0xe056);
SetTextColor(color);
doLineFeed();
} else if (*drawString != static_cast<wchar_t>(-1)) {
put(*drawString);
MoveCursorX(mfCharacterSpacing * getScale().x);
}
doAfterDrawProcess(mpDrawString,
muDrawCacheStartPos,
drawInfo);
++drawString;
++muDrawCacheStartPos;
}
if (cursorPos != NULL && cursorPos->uCursorPos == muDrawCacheStartPos) {
procCursor(cursorPos, 0);
}
finishDraw(muDrawCacheStartPos);
}
}
void Base::preDraw(u32) {}
void Base::finishDraw(u32) {}
f32 Base::getLineHeight() {
if (getScale().y == 0.0f) {
return 1.0f;
}
f32 height = getScale().y * (mfFontHeight +
mfLineSpacing);
return height >= 1.0f ? height : 1.0f;
}
void Base::setFont(const nw4r::ut::Font& font) {
nw4r::ut::CharWriter::SetFont(font);
for (s32 index = 0; index < 0x160; ++index) {
snWidthTable[index] = -1;
}
u32 ch = 0x20;
do {
DrawInfo drawInfo;
drawInfo.rect.left = 0.0f;
drawInfo.rect.top = 0.0f;
drawInfo.rect.right = 0.0f;
drawInfo.rect.bottom = 0.0f;
drawInfo.character = static_cast<wchar_t>(ch);
calcRect(drawInfo);
u16 index = static_cast<u16>(ch);
snWidthTable[index - 0x20] = static_cast<s32>(drawInfo.rect.right - drawInfo.rect.left);
ch++;
} while (ch <= 0x17f);
}
void Base::calcRect(DrawInfo& drawInfo) {
drawInfo.rect.left = 0.0f;
u16 ch = drawInfo.character;
drawInfo.rect.right = 0.0f;
drawInfo.rect.top = 0.0f;
drawInfo.rect.bottom = getLineHeight();
f32 width = 0.0f;
width += mfCharacterSpacing;
if (IsWidthFixed()) {
width += GetFixedWidth();
} else {
f32 height = getScale().x;
if (height == 0.0f) {
height = 0.0001f;
}
f32 scale = GetScaleH();
const nw4r::ut::Font* font = GetFont();
s32 charWidth = font->GetCharWidth(ch);
f32 widthOffset = static_cast<f32>(charWidth) * scale / height;
width += widthOffset;
}
drawInfo.rect.right = drawInfo.rect.right < width ? width : drawInfo.rect.right;
}
f32 Base::getWidth(const wchar_t* sz) {
f32 width = 0.0f;
DrawInfo drawInfo;
drawInfo.rect.left = 0.0f;
drawInfo.rect.top = 0.0f;
drawInfo.rect.right = 0.0f;
drawInfo.rect.bottom = 0.0f;
u16 ch;
goto check;
body:
drawInfo.character = ch;
calcRect(drawInfo);
width += drawInfo.rect.right - drawInfo.rect.left;
++sz;
check:
ch = *sz;
if (ch != 0) {
goto body;
}
return width;
}
void Base::beginDraw(const nw4r::ut::Rect& rect) {
Mtx44 projection;
Mtx matrix;
f32 viewport[6];
muDrawCacheStartPos = 0;
GXGetViewportv(&mSavedViewport.xOrig);
GXGetProjectionv(mSavedProjection);
C_MTXOrtho(projection, 0.0f, rect.top - rect.bottom, -50.0f, 50.0f + (rect.right - rect.left), -1.0f, 1.0f);
GXSetProjection(projection, GX_ORTHOGRAPHIC);
f32 width = mProjectionRect.right -
mProjectionRect.left;
f32 scale = mfVIWidth / width;
f32 halfWidth = width;
halfWidth *= 0.5f;
f32 left = (rect.left + halfWidth) - 50.0f;
f32 xOffset = left;
xOffset *= scale - 1.0f;
viewport[0] = left + xOffset;
f32 height = (mProjectionRect.bottom -
mProjectionRect.top) * 0.5f;
viewport[1] = rect.bottom + height;
viewport[2] = scale * (100.0f + (rect.right - rect.left));
viewport[3] = rect.top - rect.bottom;
viewport[4] = mSavedViewport.nearZ;
viewport[5] = mSavedViewport.farZ;
GXSetViewport(viewport[0], viewport[1], viewport[2], viewport[3], viewport[4], viewport[5]);
u32 x = static_cast<u32>(viewport[0]);
u32 y = static_cast<u32>(viewport[1]);
u32 viewportWidth = static_cast<u32>(viewport[2]);
if (viewportWidth >= 0x5dc) {
viewportWidth = 0x5dc;
}
u32 viewportHeight = static_cast<u32>(viewport[3]);
if (viewportHeight >= 0x5dc) {
viewportHeight = 0x5dc;
}
GXSetScissor(x, y, viewportWidth, viewportHeight);
PSMTXIdentity(matrix);
GXLoadPosMtxImm(matrix, 0);
mfMinScrollY = GetCursorY();
muLine = 0;
}
void Base::endDraw() {
f32* viewport = &mSavedViewport.xOrig;
GXSetViewport(viewport[0], viewport[1], viewport[2], viewport[3], viewport[4], viewport[5]);
GXSetScissor(static_cast<u32>(viewport[0]), static_cast<u32>(viewport[1]),
static_cast<u32>(viewport[2]), static_cast<u32>(viewport[3]));
mfModifyStartY = GetCursorX();
mfMinScrollY =
(mfMinScrollY - GetCursorY()) + getLineHeight();
}
void Base::drawCursor(f32, f32) {}
void Base::draw() {}
}
}

View File

@@ -1,601 +0,0 @@
#define TIZISTRING_IMPLEMENTATION
#include "keyboard/tiZiString.h"
#include "keyboard/tiUtil.h"
#include "keyboard/tiCpData.h"
#include <wchar.h>
#include <string.h>
namespace textinput {
namespace tistring {
inline Decolated::~Decolated() {}
struct OEMDictionary {
s32 count;
s32 offsets[1];
};
u16 WithZi::ElementBuffer[0x100];
u16 WithZi::CandidatesBuffer[0x300];
u16 WithZi::CandidatedWord[0xa00];
u16 WithZi::LatestWord[0x40];
static const wchar_t kKoreanFinalLetters[12] = {
L'y', L'u', L'i', L'o', L'p', L'h', L'j', L'k', L'l', L'b', L'n', L'm'
};
static const wchar_t kKoreanLeadingLetters[16] = L"qwertasdfgzxcv";
static bool SearchOEMDictionary(u16, u16*, u8, void*);
static u8 LatinSearchOrder[10] = {3, 3, 2, 1, 4, 7, 6, 5, 8, 0};
static bool SearchOEMDictionary(u16 index, u16* output, u8 maxLength, void* dictionary) {
OEMDictionary* entries = static_cast<OEMDictionary*>(dictionary);
if (static_cast<s32>(index) >= entries->count) {
return false;
}
s32 offset = entries->offsets[index];
u8* dictionaryBytes = static_cast<u8*>(dictionary);
wchar_t* word = reinterpret_cast<wchar_t*>(dictionaryBytes + offset);
u32 length = wcslen(word);
if (static_cast<u32>(maxLength) <= length) {
return false;
}
wcscpy((wchar_t*)output, word);
return true;
}
void WithZi::ChangeDictionaryLanguage(u8 language) {
if (mDictionaryLanguage != language) {
mDictionaryLanguage = language;
if (mOemDictionaryId != 0) {
EZTXDetachOEMDict(mOemDictionaryId & 0xff, mpDictionaryWork);
}
EZTXLanguageEntry* entry;
EZTXLanguageEntry* dictionaryTable;
s32 entryIndex;
s32 baseIndex;
u32* dictionaryEntry;
u32 attached;
entryIndex = -1;
baseIndex = 0;
dictionaryTable = (EZTXLanguageEntry*)mpDictionaries;
entry = dictionaryTable;
for (s32 groups = 2; groups != 0; --groups) {
for (s32 column = 0; column < 6; ++column) {
if (language == entry->language) entryIndex = baseIndex;
++entry;
++baseIndex;
}
}
if (entryIndex != -1) {
dictionaryEntry = reinterpret_cast<u32*>(dictionaryTable[entryIndex].table);
if (dictionaryEntry == NULL) goto noDictionary;
attached = EZTXAttachOEMDict(
(u8*)SearchOEMDictionary, *dictionaryEntry & 0xffff,
dictionaryEntry, mpDictionaryWork);
mOemDictionaryId = attached & 0xff;
goto dictionaryReady;
noDictionary:
mOemDictionaryId = 0;
dictionaryReady:
;
}
}
}
WithZi::~WithZi() {
MEMFreeToAllocator(mpAllocator, mpDictionaryWork);
}
void WithZi::create(MEMAllocator* allocator) {
StringBase::create(allocator);
void* globalData = MEMAllocFromAllocator(allocator, EZTXGetGlobalDataSize());
EZTXGetGlobalDataSize();
mpDictionaryWork = globalData;
mOemDictionaryId = 0;
init();
}
void WithZi::init() {
if (mbDictionaryOpen != 0) {
mbContextChanged = 0;
clearCandidates();
memset(LatestWord, 0, 0x80);
LatestWord[0] = 0x20;
if (getPredictLanguage() == 1) {
update();
}
}
}
void WithZi::clearCandidates() {
if (mbDictionaryOpen != 0) {
mInputLength = 0;
mCandidateCount = 0;
memset(ElementBuffer, 0, 0x1fe);
memset(CandidatesBuffer, 0, 0x200);
memset(CandidatesBuffer + 0x100, 0, 0x1fe);
memset(CandidatesBuffer + 0x200, 0, 0x200);
memset(CandidatedWord, 0, 0x1400);
memset(&mSearch, 0, sizeof(mSearch) + sizeof(mSearchState));
mSearch.language = getPredictLanguage();
mSearch.subLanguage = 7;
mSearch.getMode = 0;
mSearch.context = 1;
mSearch.getOptions = 0x81;
mSearch.elements = reinterpret_cast<wchar_t*>(ElementBuffer);
mSearch.candidates = reinterpret_cast<wchar_t*>(&CandidatesBuffer[0x200]);
mSearch.maxCandidates = 0x28;
mSearch.elementCount = 0;
mSearch.firstCandidate = 0;
if (getPredictLanguage() == 1) {
mSearch.getMode = 1;
mSearch.context = 0x10;
}
EZTXGetCandidates(&mSearch, mpDictionaryWork);
}
}
void WithZi::openDictionary(void* ziDictionary, void* oemDictionary) {
EZTXInitialize(ziDictionary, mpDictionaryWork);
mbDictionaryOpen = 1;
mpDictionaries = static_cast<EZTXLanguageEntry*>(oemDictionary);
EZTXSetLatinSearchOrder(LatinSearchOrder, 10, mpDictionaryWork);
mOemDictionaryId = 0;
}
void WithZi::pushBack(wchar_t) {}
void WithZi::popBack() {}
void WithZi::inputChar(wchar_t character) {
if (hasCandidate()) {
if (mInputLength != 0) {
--mInputLength;
CandidatesBuffer[mInputLength] = 0;
}
mwcCandidate = 0;
}
CandidatesBuffer[mInputLength] = character;
u16 index = mInputLength++;
CandidatesBuffer[(index + 1) & 0xffff] = 0;
update();
}
void WithZi::setInputting(wchar_t character) {
if (hasCandidate()) {
if (mInputLength != 0) {
--mInputLength;
CandidatesBuffer[mInputLength] = 0;
}
mwcCandidate = 0;
}
inputChar(character);
mwcCandidate = character;
}
void WithZi::backSpace() {
if (hasCandidate()) {
if (mInputLength != 0) {
--mInputLength;
CandidatesBuffer[mInputLength] = 0;
}
mwcCandidate = 0;
}
else {
if (mInputLength != 0) {
--mInputLength;
CandidatesBuffer[mInputLength] = 0;
update();
}
}
}
void WithZi::confirm(const wchar_t*) {
update();
}
bool WithZi::moveCursorRight() {
return false;
}
bool WithZi::moveCursorLeft() {
return false;
}
void WithZi::getPredicted(int index, wchar_t* output) {
const wchar_t* candidate = reinterpret_cast<const wchar_t*>(CandidatedWord + index * 0x40);
s32 length = 0;
while (candidate[length] != 0) {
output[length] = candidate[length];
++length;
}
output[length] = 0;
}
wchar_t* WithZi::getCurrentSelected() {
if ((mSelectedCandidate < 0) || (mSelectedCandidate >= 0x28)) {
return NULL;
}
return reinterpret_cast<wchar_t*>(CandidatedWord + mSelectedCandidate * 0x40);
}
u32 WithZi::getCurrentInput(wchar_t* output, u32 maxLength) {
u16* source;
wchar_t* destination;
s32 count = 0;
if (0x100 < maxLength) {
maxLength = 0x100;
}
source = CandidatesBuffer;
destination = output;
u32 index = 0;
for (index = 0; index < maxLength; index++) {
wchar_t character = *source;
*destination = character;
if (*destination == 0) {
break;
}
if (mLetterMode == LM_2) {
*destination = util::toWUpper(*destination);
}
++count;
++source;
++destination;
}
if (mLetterMode == LM_0) {
*output = util::toWUpper(*output);
}
return count;
}
wchar_t* WithZi::getCurrentInput() {
return (wchar_t*)CandidatesBuffer;
}
void WithZi::partialConfirmForKR() {
if (mInputLength <= 1) return;
bool match;
s32 index;
const wchar_t* letters;
wchar_t value = util::toWLower(CandidatesBuffer[mInputLength - 1]);
letters = kKoreanFinalLetters;
for (index = 0; index < 12; ++index) {
if (value == *letters) {
match = true;
goto finalLetterChecked;
}
++letters;
}
match = false;
finalLetterChecked:
if (!match) goto keepLastLetter;
value = util::toWLower(CandidatesBuffer[mInputLength - 2]);
index = 0;
letters = kKoreanLeadingLetters;
for (; index < 14; ++index) {
if (value == *letters) {
match = true;
goto leadingLetterChecked;
}
++letters;
}
match = false;
leadingLetterChecked:
if (!match) goto keepLastLetter;
CandidatesBuffer[0] = CandidatesBuffer[mInputLength - 2];
CandidatesBuffer[1] = CandidatesBuffer[mInputLength - 1];
mInputLength = 2;
goto confirmLetters;
keepLastLetter:
CandidatesBuffer[0] = CandidatesBuffer[mInputLength - 1];
mInputLength = 1;
confirmLetters:
CandidatesBuffer[mInputLength] = 0;
update();
}
void WithZi::update() {
if (mbDictionaryOpen == 0) {
return;
}
mSelectedCandidate = 0;
s32 elementCount = static_cast<u16>(setElementBuffer());
mSearch.language = getPredictLanguage();
mSearch.subLanguage = 7;
mSearch.getMode = 0;
mSearch.context = 1;
mSearch.getOptions = 0x81;
mSearch.elements = reinterpret_cast<wchar_t*>(ElementBuffer);
mSearch.candidates = reinterpret_cast<wchar_t*>(&CandidatesBuffer[0x200]);
mSearch.maxCandidates = 0x28;
mSearch.elementCount = static_cast<u8>(elementCount);
mSearch.firstCandidate = 0;
mSearch.currentWord = 0;
mSearch.wordCharCount = 0;
mSearch.count = 0;
mSearch.letters = 0;
mSearch.completion = 0;
mSearch.scratch = 0;
if (getPredictLanguage() == 1) {
mSearch.getMode = 1;
mSearch.context = 0x11;
if (mbKoreanPartialConfirm != 0) {
mSearch.context |= 0x20;
mSearch.getOptions |= 0x10;
}
mSearch.currentWord = reinterpret_cast<wchar_t*>(LatestWord);
mSearch.wordCharCount = wcslen((wchar_t*)LatestWord);
}
if (elementCount == 0 && mSearch.wordCharCount == 0) {
init();
}
else if (elementCount == 1 &&
(0xeff1 <= ElementBuffer[0] && ElementBuffer[0] <= 0xeff9) &&
mpHoldingKey != NULL && getPredictLanguage() != 0x12) {
CandidatedWord[0] = 0x3e;
CandidatedWord[1] = 0;
mCandidateCount = 1;
u16* output = CandidatedWord + 0x40;
for (s32 keyIndex = 1; keyIndex < 16; ++keyIndex) {
u16 candidate = static_cast<keyboard::cellphonetype::PaneNameToCharCode*>(mpHoldingKey)->wc[keyIndex - 1];
if (candidate != 0) {
output[0] = candidate;
output[1] = 0;
++mCandidateCount;
}
output += 0x40;
}
EZTXGetCandidates(&mSearch, mpDictionaryWork);
}
else {
memcpy(&CandidatesBuffer[0x200], CandidatesBuffer, 0x200);
ChangeDictionaryLanguage(getPredictLanguage());
u32 count = EZTXGetCandidates(&mSearch, mpDictionaryWork) & 0xff;
if (getPredictLanguage() == 1 && count == 0x61) {
mSearch.firstCandidate = 0x61;
mSearch.candidates = reinterpret_cast<wchar_t*>(&CandidatesBuffer[0x2c2]);
mSearch.maxCandidates = 199;
count = EZTXGetCandidates(&mSearch, mpDictionaryWork) & 0xff;
mSearch.firstCandidate = 0;
count += 0x61;
mSearch.candidates = reinterpret_cast<wchar_t*>(&CandidatesBuffer[0x200]);
}
if (static_cast<s32>(count) > 0x28) {
count = 0x28;
}
if (getPredictLanguage() == 1 && mbContextChanged != 0 &&
(mbContextChanged = 0, mSearch.wordCharCount == 0)) {
u32 length = mCurrentWordLength;
u32 wordLength = wcslen((wchar_t*)LatestWord) & 0xff;
u32 copied;
u16* source = LatestWord + wordLength - length;
for (copied = 0; copied < length; ++copied) {
LatestWord[copied] = source[copied];
}
LatestWord[copied] = 0;
mSearch.wordCharCount = wcslen((wchar_t*)LatestWord);
count = EZTXGetCandidates(&mSearch, mpDictionaryWork) & 0xff;
}
if (count == 0) {
if (getPredictLanguage() != 1) {
if (getPredictLanguage() == 0x12) {
backSpace();
return;
}
count = complementsCandidates_(elementCount);
}
if (count == 0) return;
}
memset(CandidatedWord, 0, 0x1400);
mCandidateCount = 0;
if (elementCount != 0 || mSearch.wordCharCount != 0) {
u16* source = reinterpret_cast<u16*>(mSearch.candidates);
u16* destination = CandidatedWord;
for (s32 index = 0; index < static_cast<s32>(count); index++) {
if (getPredictLanguage() == 1) {
s32 length = 0;
u16* output = destination;
for (; *source != 0x20 && *source != 0; source++) {
*output++ = *source;
++length;
}
++source;
destination[length] = 0;
} else if (mPredictLanguage == PL_11) {
*destination = *source++;
destination[1] = 0;
} else if (getPredictLanguage() == 0x12) {
u16* output = destination;
while (*source >= 0x100) {
*output++ = *source++;
}
} else if (EZTXCopy(reinterpret_cast<wchar_t*>(destination), &mSearch, index & 0xff, mpDictionaryWork) == 0) {
goto nextCandidate;
}
++mCandidateCount;
{
s32 position = 0;
for (u16* candidate = destination; *candidate != 0; ++candidate) {
switch (mLetterMode) {
case LM_1:
*candidate = util::toWLower(*candidate);
break;
case LM_2:
*candidate = util::toWUpper(*candidate);
break;
case LM_0:
if (position == 0) *candidate = util::toWUpper(*candidate);
else *candidate = util::toWLower(*candidate);
break;
default:
break;
}
++position;
}
}
nextCandidate:
destination += 0x40;
}
}
}
}
u32 WithZi::complementsCandidates_(s32) {
bool useSentinel;
u16 value;
u16* scan = ElementBuffer;
goto scanCheck;
scanBody:
if (0xeff1 <= value && value <= 0xeff9) {
useSentinel = true;
goto scanDone;
}
++scan;
scanCheck:
value = *scan;
if (value != 0) goto scanBody;
useSentinel = false;
scanDone:
if (useSentinel) {
CandidatedWord[mInputLength - 1] = 0xfffe;
CandidatedWord[mInputLength] = 0;
}
else {
u16* character;
u16* destination;
u32 length;
u16* source;
length = 0;
destination = CandidatedWord;
source = ElementBuffer;
character = source;
while (*source != 0) {
s32 mode = mLetterMode;
if (mode == 2) goto modeTwo;
if (mode >= 2) goto modeThree;
if (mode == 0) goto modeZero;
if (mode >= 0) goto modeOne;
goto modeDone;
modeThree:
if (mode >= 4) goto modeDone;
goto modeCopy;
modeOne:
*destination = util::toWLower(*character);
goto modeDone;
modeTwo:
*destination = util::toWUpper(*character);
goto modeDone;
modeZero:
if (length == 0) {
*destination = util::toWUpper(*character);
}
else {
*destination = util::toWLower(*character);
}
goto modeDone;
modeCopy:
*destination = *character;
modeDone:
++source;
++character;
++destination;
++length;
}
CandidatedWord[length] = 0;
}
mCandidateCount = 1;
return 0;
}
u32 WithZi::setElementBuffer() {
u32 count = 0;
u16* elements = ElementBuffer;
u16* candidates = CandidatesBuffer;
memset(elements, 0, 0x1fe);
memset(candidates + 0x100, 0, 0x1fe);
u16 inputCharacter;
while ((inputCharacter = candidates[static_cast<u16>(count)]) != 0 && static_cast<u16>(count) < 0xff) {
u16 index = count;
elements[index] = inputCharacter;
if (getPredictLanguage() == 1) {
s32 character = elements[index];
if (0x30 <= character && character <= 0x39) elements[index] = character + 0xf300;
else if ((0x61 <= character && character <= 0x7a) || (0x41 <= character && character <= 0x5a)) elements[index] += 0xf300;
}
++count;
}
return count;
}
bool WithZi::isFix() {
return mInputLength == 0;
}
u8 WithZi::getPredictLanguage() {
switch (mPredictLanguage) {
case 0: return 0x3b;
case 1: return 0x40;
case 2: return 0x41;
case 3: return 0x3a;
case 4: return 5;
case 5: return 0x3f;
case 6: return 0x42;
case 7: return 0x2f;
case 8: return 0xd;
case 9: return 1;
case 10: return 0x12;
case 11: return 0x12;
default: return 0x3b;
}
}
void WithZi::setCurrentWord(const wchar_t* word) {
if (word == NULL) {
LatestWord[0] = 0x20;
LatestWord[1] = 0;
return;
}
mbContextChanged = 1;
wchar_t* currentWord;
u32 sourceOffset;
u32 length;
s32 copied;
length = 0;
currentWord = reinterpret_cast<wchar_t*>(LatestWord);
for (; *currentWord != 0 && *currentWord != 0x20;
currentWord++, ++length) {
if (length == 0x3f) {
LatestWord[length] = 0;
return;
}
}
copied = 0;
sourceOffset = 0;
currentWord = const_cast<wchar_t*>(word);
for (; *currentWord != 0 && length < 0x3f;) {
wchar_t character = word[sourceOffset];
++length;
++currentWord;
++copied;
LatestWord[length - 1] = character;
++sourceOffset;
}
LatestWord[length] = 0;
mCurrentWordLength = copied;
}
void WithZi::EnableKSXFilter(bool) {}
bool WithZi::canBackSpace() {
return mInputLength != 0;
}
void WithZi::clear() {}
}
}

View File

@@ -1,11 +0,0 @@
# MyTiLetterForm attempts
- Initial reconstruction: 24/26 exact; create had three constructor-store ordering differences, drawBody four literal-offset differences. No source existed at the configured path.
- Pool: the fourth animation group is in the neighboring extracted tiHwKeyboard .rodata range, starting four bytes after 0x81616B88. Its real N_SendMes entry has one animation, animationFiles[5]. Reconstructing all four ordinary groups and correcting drawBody's conditional T_TouchLetter and unconditional N_SendMes references made the 13-string pool identical and drawBody exact.
- create attempt 1: two direct EventHandler members produced three reordered stores (179/179 instructions).
- create attempt 2: memo EventHandler base with the second pointer in letter EventHandler reduced the difference to two stores.
- create attempt 3: both pointers initialized by the memo constructor before the letter vtable store produced zero differences. Restoring the actual inputform EventHandler -> memo EventHandler -> letter EventHandler hierarchy preserves zero differences.
- Vtables: typed shared declarations behind MYTILETTERFORM_IMPLEMENTATION corrected create, scrolling, sound, cursor-cache signatures. Ordering class declarations EventHandler, AnmPane, WholePane, PicPane reproduced the four owned vtable positions without forced data.
- All 26 functions are instruction exact. The owned .data bytes are identical after clearing relocations; the only differing relocation is the malformed concatenated isEnableCursorCache/getStartPos name in the target, plus four relocations in the ordinary weak gui EventHandler vtable, which is zero-filled in the extracted original.
- .rodata is 784 bytes versus extracted 720; all 720 prefix bytes and normalized relocations agree. The remaining 64 bytes are the final null of group three and all of group four, visibly misassigned to tiHwKeyboard in splits.txt. No split, symbol or neighboring source changes were made.
- .sdata is 66 bytes versus extracted 72; every prefix byte agrees. The six trailing target zeros were not recreated with padding. .sdata2 is 32/32 identical. Pool is identical.

View File

@@ -1,46 +0,0 @@
# NWC24DateParser matching attempts
Baseline: no source; 0/8 exact functions, 0/2372 code, 0/40 data.
Initial readable C reference in /tmp was checked against target assembly.
Calendar fields reuse NWC24Date and OSCalendarTime. Month-length and
year-day tables follow original .rodata order, with all 40 bytes identical.
Epoch conversion adds 2208988800 seconds before clamping, as in the target.
The stack date workspace reproduces calendar conversion aliasing and offsets.
## Source attempts
NWC24iEpochSecondsToDate | clamp epoch after signed epoch offset addition | (100.0, 0) | src 0x13c base 0x13c insns 79/79
NWC24iMinutesToOSCalendarTime | calendar conversion uses date structure workspace | (0, -9999) | [1/1] MWCC build/43U/src/libs/RevoEX/src/nwc24/NWC24DateParser.o
NWC24iMinutesToOSCalendarTime | workspace and byte leap flag | (0, -9999) | [1/1] MWCC build/43U/src/libs/RevoEX/src/nwc24/NWC24DateParser.o
NWC24iMinutesToOSCalendarTime | workspace with leap flag declared before date | (0, -9999) | [1/1] MWCC build/43U/src/libs/RevoEX/src/nwc24/NWC24DateParser.o
NWC24iDateToOSCalendarTime | explicit local year before leap flag | (96.588234, -19) | src 0x154 base 0x154 insns 85/85
NWC24iDateToOSCalendarTime | byte leap flag | (99.17647, -13) | src 0x154 base 0x154 insns 85/85
NWC24iDateToOSCalendarTime | leap flag initialized at declaration | (99.17647, -13) | src 0x154 base 0x154 insns 85/85
ConvertDateToDays | shared non-leap day validation and leap-year predicate | (83.07079, -21) | src 0x1c4 base 0x1c4 insns 113/113
ConvertDateToDays | signed leap flag with reordered annual sum | (83.07079, -21) | src 0x1c4 base 0x1c4 insns 113/113
ConvertDateToDays | unsigned leap flag and shared validation | (83.07079, -21) | src 0x1c4 base 0x1c4 insns 113/113
ConvertDaysToDate | subtract annual and monthly spans before testing remaining days | (79.32039, -9999) | src 0x178 base 0x19c insns 94/103
ConvertDaysToDate | annual leap result stored in byte | (79.32039, -9999) | src 0x178 base 0x19c insns 94/103
ConvertDaysToDate | cache signed current year | (79.32039, -9999) | src 0x178 base 0x19c insns 94/103
NWC24iMinutesToOSCalendarTime | date structure workspace with named calendar fields | (100.0, 0) | src 0x1d8 base 0x1d8 insns 118/118
ConvertDateToDays | subtract one from day before adding month prefix | (83.07079, -21) | src 0x1c4 base 0x1c4 insns 113/113
ConvertDateToDays | annual sum before leap correction | (90.33628, -21) | src 0x1c4 base 0x1c4 insns 113/113
ConvertDateToDays | annual base computed in named temporary | (90.33628, -21) | src 0x1c4 base 0x1c4 insns 113/113
NWC24iMinutesToOSCalendarTime | shared leap-year predicate | (93.05085, -9999) | src 0x1e8 base 0x1d8 insns 122/118
NWC24iDateToOSCalendarTime | shared leap-year predicate | (94.329414, -9999) | src 0x164 base 0x154 insns 89/85
ConvertDateToDays | shared leap-year predicate | (90.33628, -21) | src 0x1c4 base 0x1c4 insns 113/113
NWC24iDateToOSCalendarTime | cache year after leap flag initialization | (99.17647, -13) | src 0x154 base 0x154 insns 85/85
NWC24iDateToOSCalendarTime | leap check through shared year helper | (94.329414, -9999) | src 0x164 base 0x154 insns 89/85
NWC24iDateToOSCalendarTime | leap result assigned after calendar stores | (94.44706, -9999) | src 0x158 base 0x154 insns 86/85
ConvertDateToDays | day offset prepared before month table access | (90.95575, -18) | src 0x1c4 base 0x1c4 insns 113/113
ConvertDateToDays | annual day and leap counts use independent temporaries | (95.02655, -19) | src 0x1c4 base 0x1c4 insns 113/113
ConvertDateToDays | annual days calculated before leap century count | (95.02655, -19) | src 0x1c4 base 0x1c4 insns 113/113
ConvertDaysToDate | monthly leap flag materialized before subtraction | (86.35922, -9999) | src 0x1a8 base 0x19c insns 106/103
ConvertDaysToDate | annual leap calculation uses year helper | (75.23301, -9999) | src 0x168 base 0x19c insns 90/103
ConvertDaysToDate | monthly and annual named leap predicates | (82.31068, -9999) | src 0x198 base 0x19c insns 102/103
## Remaining functions
NWC24iDateToOSCalendarTime, 99.17647%: 85/85 instructions; thirteen differences exchanging year and leap-flag registers.
ConvertDateToDays, 95.02655%: 113/113 instructions; nineteen differences in month-prefix addition and annual arithmetic scheduling.
ConvertDaysToDate, 86.35922%: 106/103 instructions; annual/monthly leap branches and register allocation differ.

View File

@@ -1,129 +0,0 @@
# NWC24Mime matching attempts
Baseline: no source, 0/16 functions, 0/6124 code bytes, 0/88 data bytes.
All functions are implemented in object order, with ordinary literals.
The encoding alphabet, pointer and small marker strings match all 88 data bytes.
The initial implementation was adapted from readable C references available
in /tmp, then checked against the target object and object-exported Ghidra C.
Oversized Ghidra stack arrays were replaced with actual octet/scalar storage.
## Source-level experiments
Base64Encode | canonical three-byte loop from SDK | (96.233765, -37) | src 0x134 base 0x134 insns 77/77
Base64Encode | mask initial sextet | (80.19481, -9999) | src 0x144 base 0x134 insns 81/77
Base64Encode | pointer-relative second byte | (80.38961, -9999) | src 0x144 base 0x134 insns 81/77
NWC24InitBase64Table | unsigned scan index | (97.41304, -51) | src 0x2e0 base 0x2e0 insns 184/184
NWC24InitBase64Table | one character scan with explicit bounds | (100.0, 0) | src 0x2e0 base 0x2e0 insns 184/184
QEncode | signed copy count | (73.83251, -9999) | src 0x324 base 0x32c insns 201/203
QEncode | credit output before optional line break | (72.28571, -9999) | src 0x2fc base 0x32c insns 191/203
QEncode | initialize encoded byte before classification | (69.3202, -9999) | src 0x2f4 base 0x32c insns 189/203
QDecode | signed cursors | (74.9315, -9999) | src 0x234 base 0x248 insns 141/146
QDecode | predicate return order | (74.863014, -9999) | src 0x234 base 0x248 insns 141/146
QDecode | remaining input while loop | (74.9315, -9999) | src 0x234 base 0x248 insns 141/146
QEncode | SDK signed copy scan and octet operations | (100.0, 0) | src 0x32c base 0x32c insns 203/203
EncodeWord | unsigned intermediate string length | (69.91919, -9999) | src 0x2c0 base 0x318 insns 176/198
EncodeWord | capacity condition reversed | (69.86869, -9999) | src 0x2c0 base 0x318 insns 176/198
EncodeWord | defer overflow flag initialization | (69.91919, -9999) | src 0x2c0 base 0x318 insns 176/198
CopyWithoutLinearWhiteSpaces | switch classification | (8.835616, -55) | src 0x124 base 0x124 insns 73/73
CopyWithoutLinearWhiteSpaces | signed output offset | (14.315068, -9999) | src 0x114 base 0x124 insns 69/73
CopyWithoutLinearWhiteSpaces | cache input value across capacity check | (10.068493, -9999) | src 0x110 base 0x124 insns 68/73
DecodeWord | repeat consumed initialization before capacity return | (77.37222, -161) | src 0x2d0 base 0x2d0 insns 180/180
DecodeWord | independent whitespace scan pointer | (74.02778, -9999) | src 0x2c0 base 0x2d0 insns 176/180
DecodeWord | unconditional decoded size on copy overflow | (74.094444, -9999) | src 0x2b8 base 0x2d0 insns 174/180
NWC24DecodeMIMEHeaderFieldBody | check decode status before consumed count | (74.13043, -9999) | src 0x158 base 0x170 insns 86/92
NWC24DecodeMIMEHeaderFieldBody | signed remaining input | (81.195656, -9999) | src 0x158 base 0x170 insns 86/92
NWC24DecodeMIMEHeaderFieldBody | direct input size parameter scan | (81.195656, -9999) | src 0x158 base 0x170 insns 86/92
ConcatEncodedText | signed prefix length | (93.22222, -43) | src 0xfc base 0xfc insns 63/63
ConcatEncodedText | single remaining capacity expression | (93.492065, -41) | src 0xfc base 0xfc insns 63/63
ConcatEncodedText | positive capacity branch | (90.84127, -9999) | src 0x100 base 0xfc insns 64/63
ExtractCharset | signed scan indices | (74.04762, -9999) | src 0x148 base 0x150 insns 82/84
ExtractCharset | signed extracted length | (75.34524, -9999) | src 0x144 base 0x150 insns 81/84
ExtractCharset | reuse marker length for span | (75.34524, -9999) | src 0x144 base 0x150 insns 81/84
ExtractEncodedText | signed scan index | (85.4, -9999) | src 0x208 base 0x21c insns 130/135
ExtractEncodedText | unsigned extracted length | (87.437035, -9999) | src 0x204 base 0x21c insns 129/135
ExtractEncodedText | merge invalid pointer predicates | (87.36296, -9999) | src 0x204 base 0x21c insns 129/135
CopyWithoutLinearWhiteSpaces | switch with loop continuation predicate | (98.287674, -20) | src 0x124 base 0x124 insns 73/73
CopyWithoutLinearWhiteSpaces | unsigned cached capacity | (98.42466, -18) | src 0x124 base 0x124 insns 73/73
CopyWithoutLinearWhiteSpaces | capacity before output counter | (98.287674, -20) | src 0x124 base 0x124 insns 73/73
Base64Encode | combine shifted initial octet | (100.0, 0) | src 0x134 base 0x134 insns 77/77
CopyWithoutLinearWhiteSpaces | declare character before flags | (98.42466, -18) | src 0x124 base 0x124 insns 73/73
CopyWithoutLinearWhiteSpaces | separate default pointer advancement | (98.561646, -17) | src 0x124 base 0x124 insns 73/73
CopyWithoutLinearWhiteSpaces | cache capacity minus terminator | (96.9863, -10) | src 0x124 base 0x124 insns 73/73
NWC24DecodeMIMEHeaderFieldBody | bounded signed input and output cursors | (85.92391, -9999) | src 0x16c base 0x170 insns 91/92
NWC24DecodeMIMEHeaderFieldBody | avoid initial consumed-count clear | (82.065216, -9999) | src 0x154 base 0x170 insns 85/92
NWC24DecodeMIMEHeaderFieldBody | fold status into loop condition | (83.36957, -9999) | src 0x16c base 0x170 insns 91/92
QDecode | moving input/output cursors | (81.99315, -9999) | src 0x230 base 0x248 insns 140/146
QDecode | late moving cursor initialization | (81.03425, -9999) | src 0x230 base 0x248 insns 140/146
QDecode | decode high nibble before low conversion | (86.20548, -9999) | src 0x230 base 0x248 insns 140/146
QDecode | independent pointer validation | (81.10274, -9999) | src 0x238 base 0x248 insns 142/146
NWC24DecodeMIMEHeaderFieldBody | independent encoded-pointer validation | (88.20652, -9999) | src 0x174 base 0x170 insns 93/92
NWC24DecodeMIMEHeaderFieldBody | consumed count written by decoder | (86.95652, -9999) | src 0x168 base 0x170 insns 90/92
NWC24DecodeMIMEHeaderFieldBody | defer remaining input initialization | (88.097824, -9999) | src 0x16c base 0x170 insns 91/92
## Retained result
8/16 instruction-exact and objdiff-exact functions; 2240/6124 code bytes;
88/88 data bytes. Base64 encoding uses the canonical three-byte scan and
one initial shifted-octet expression. Alphabet initialization scans actual
character bounds. Quoted-printable encoding uses a signed byte-copy scan.
All remaining functions have at least three logged distinct experiments.
Remaining differences: QDecode cursor/error control flow; EncodeWord append
status propagation; CopyWithoutLinearWhiteSpaces register allocation;
DecodeWord search/status allocation; MIME header validation and cursor
allocation; ConcatEncodedText allocation; charset/text search loop scheduling.
Combination of moving QDecode cursor initialization and sequential nibble
conversion gives 86.54794%. Combining bounded MIME-header scanning, independent
input validation and decoder-owned consumed count gives 86.03261%.
No other unit or shared header was changed.
## Continuation 2026-09-30
Baseline: 8/16 exact functions; 2240/6124 code bytes; 88/88 data bytes.
CopyWithoutLinearWhiteSpaces | signed output count with unsigned bound comparison | (98.561646, -17) | src 0x124 base 0x124 insns 73/73
CopyWithoutLinearWhiteSpaces | declare capacity before newline state | (98.561646, -17) | src 0x124 base 0x124 insns 73/73
CopyWithoutLinearWhiteSpaces | for scan with advancing input in iteration expression | (98.561646, -17) | src 0x124 base 0x124 insns 73/73
CopyWithoutLinearWhiteSpaces | signed output-size count with character declared early | (98.561646, -17) | src 0x124 base 0x124 insns 73/73
ConcatEncodedText | defer result before remaining-byte count | (93.492065, -41) | src 0xfc base 0xfc insns 63/63
ConcatEncodedText | unsigned available-byte count with signed exhaustion check | (93.492065, -41) | src 0xfc base 0xfc insns 63/63
ConcatEncodedText | increment encoded size before terminator store | (91.42857, -41) | src 0xfc base 0xfc insns 63/63
ConcatEncodedText | positive capacity body before overflow case | (100.0, 0) | src 0xfc base 0xfc insns 63/63
ExtractEncodedText | inline marker search with pointer-valued results | (91.25926, -9999) | src 0x208 base 0x21c insns 130/135
ExtractEncodedText | independent invalid pointer checks | (86.85185, -9999) | src 0x20c base 0x21c insns 131/135
ExtractEncodedText | cache encoded-word span before decoding | (84.36296, -9999) | src 0x204 base 0x21c insns 129/135
QDecode | separate null input and output checks | (87.60959, -9999) | src 0x238 base 0x248 insns 142/146
QDecode | unsigned nibble values | (86.54794, -9999) | src 0x230 base 0x248 insns 140/146
QDecode | reverse valid-nibble conjunction | (86.58219, -9999) | src 0x230 base 0x248 insns 140/146
NWC24DecodeMIMEHeaderFieldBody | late input count initialization | (88.36957, -9999) | src 0x17c base 0x170 insns 95/92
NWC24DecodeMIMEHeaderFieldBody | use signed remaining count and final test | (86.08696, -9999) | src 0x17c base 0x170 insns 95/92
NWC24DecodeMIMEHeaderFieldBody | fold error status into loop continuation | (83.58696, -9999) | src 0x17c base 0x170 insns 95/92
DecodeWord | pointer scan for inter-word whitespace | (77.26111, -161) | src 0x2d0 base 0x2d0 insns 180/180
DecodeWord | permissive output size updated unconditionally on copy failure | (78.833336, -9999) | src 0x2c8 base 0x2d0 insns 178/180
DecodeWord | signed marker-match position length | (0, -9999) | [1/1] MWCC build/43U/src/libs/RevoEX/src/nwc24/NWC24Mime.o
ExtractCharset | inline marker search with pointer-valued results | (83.75, -9999) | src 0x148 base 0x150 insns 82/84
ExtractCharset | comparison expressed as positive capacity test | (75.34524, -9999) | src 0x144 base 0x150 insns 81/84
ExtractCharset | delay initial delimiter variables | (76.72619, -9999) | src 0x144 base 0x150 insns 81/84
EncodeWord | store encoding character before validation | (71.681816, -9999) | src 0x2c0 base 0x318 insns 176/198
EncodeWord | defer encoded overflow flag until concat result | (69.91919, -9999) | src 0x2c0 base 0x318 insns 176/198
EncodeWord | positive charset pointer branch first | (69.81313, -9999) | src 0x2c4 base 0x318 insns 177/198
ExtractCharset | moving marker cursor with base-plus-index return | (84.64286, -9999) | src 0x154 base 0x150 insns 85/84
ExtractEncodedText | moving marker cursor with base-plus-index return | (89.85185, -9999) | src 0x214 base 0x21c insns 133/135
DecodeWord | consumed count declared before decoded count | (78.88333, -9999) | src 0x2c8 base 0x2d0 insns 178/180
Retained continuation: positive-capacity ConcatEncodedText is instruction-exact.
FindMarker provides shared bounded scans with real pointer results.
Header decoding initializes its remaining input after argument validation;
QDecode validates each pointer separately. DecodeWord copies its size output
unconditionally on the copy-overflow path, as in the target.
All existing exact functions and all data bytes are preserved.
## Remaining functions
CopyWithoutLinearWhiteSpaces, 98.561646%: 73/73 instructions; saved-register allocation and scheduling differ.
ExtractEncodedText, 91.25926%: bounded-marker scans and decoder dispatch still have different control flow.
NWC24DecodeMIMEHeaderFieldBody, 88.36957%: error handling and loop termination emit different branches.
QDecode, 87.60959%: hex-digit classification and input-loop branches differ.
ExtractCharset, 83.75%: 82/84 instructions; second delimiter scan and register lifetimes differ.
DecodeWord, 78.88333%: 178/180 instructions; delimiter search, whitespace handling, and error exits differ.
EncodeWord, 71.681816%: 176/198 instructions; append-capacity checks and overflow exits differ.

View File

@@ -1,89 +0,0 @@
# NWC24MsgRead matching attempts
Baseline: no source; 0/16 exact functions, 0/4660 code, 0/128 data.
All functions implemented in object order from target assembly and object-exported Ghidra.
Pool and .data are identical, using ordinary literals. The first implementation
used the older reference only for file-reading conventions; signatures and
behavior come from the target. Format errors use NWC24_ERR_FORMAT (-24).
## Source attempts
NWC24ReadMsgField | protection exit through common result label | (98.82353, -9999) | src 0x194 base 0x198 insns 101/102
NWC24ReadMsgField | mailbox helper reads public message view | (98.92157, -9999) | src 0x194 base 0x198 insns 101/102
NWC24ReadMsgField | protection test after type local snapshot | (98.92157, -9999) | src 0x194 base 0x198 insns 101/102
NWC24ReadMsgFaceData | field and decoder success switches | (100.0, 0) | src 0xd4 base 0xd4 insns 53/53
NWC24ReadMsgAltName | status switch and independent decoder result | (100.0, 0) | src 0xb8 base 0xb8 insns 46/46
NWC24ReadMsgMBRegDate | work cursor declared after counts and explicit scan progress | (98.257576, -19) | src 0x108 base 0x108 insns 66/66
NWC24ReadMsgMBRegDate | status declaration before cursor and scan counts | (99.545456, -3) | src 0x108 base 0x108 insns 66/66
NWC24ReadMsgMBRegDate | advance byte cursor in hexadecimal conversion call | (95.22727, -24) | src 0x108 base 0x108 insns 66/66
NWC24ReadMsgMBDelay | work cursor declared after counts and explicit scan progress | (98.18965, -17) | src 0xe8 base 0xe8 insns 58/58
NWC24ReadMsgMBDelay | status declaration before cursor and scan counts | (99.48276, -3) | src 0xe8 base 0xe8 insns 58/58
NWC24ReadMsgMBDelay | advance byte cursor in hexadecimal conversion call | (94.74138, -22) | src 0xe8 base 0xe8 insns 58/58
NWC24ReadMsgTextEx | unsigned bounded charset scan | (100.0, 0) | src 0xb8 base 0xb8 insns 46/46
NWC24ReadMsgMBRegDate | scan offset and input pointer in for increment | (99.69697, -2) | src 0x108 base 0x108 insns 66/66
NWC24ReadMsgMBRegDate | scan offset and digit count in for increment | (99.545456, -3) | src 0x108 base 0x108 insns 66/66
NWC24ReadMsgMBRegDate | scan index uses remaining count condition | (99.69697, -2) | src 0x108 base 0x108 insns 66/66
NWC24ReadMsgMBDelay | scan offset and input pointer in for increment | (99.655174, -2) | src 0xe8 base 0xe8 insns 58/58
NWC24ReadMsgMBDelay | scan offset and digit count in for increment | (99.48276, -3) | src 0xe8 base 0xe8 insns 58/58
NWC24ReadMsgMBDelay | scan index uses remaining count condition | (99.655174, -2) | src 0xe8 base 0xe8 insns 58/58
NWC24ReadMsgMBUpdateSW | initialize work buffer after flag validation and switch status | (95.53571, -14) | src 0xe0 base 0xe0 insns 56/56
NWC24ReadMsgMBUpdateSW | work buffer declaration after result | (90.71429, -9999) | src 0xdc base 0xe0 insns 55/56
NWC24ReadMsgMBUpdateSW | signed update digit | (89.01786, -9999) | src 0xdc base 0xe0 insns 55/56
NWC24ReadMsgMBOptOutFlag | cursor lifetime and explicit separator case | (92.14286, -9999) | src 0x138 base 0x134 insns 78/77
NWC24ReadMsgMBOptOutFlag | signed bounded hexadecimal scan counters | (90.58442, -9999) | src 0x138 base 0x134 insns 78/77
NWC24ReadMsgMBOptOutFlag | app id accumulated before cursor advance | (92.14286, -9999) | src 0x138 base 0x134 insns 78/77
NWC24ReadMsgFromAddr | preserve stream status when closing fails | (98.804344, -9999) | src 0x16c base 0x170 insns 91/92
NWC24ReadMsgFromAddr | mailbox selection status via positive body | (97.5, -56) | src 0x170 base 0x170 insns 92/92
NWC24ReadMsgFromAddr | close result declared before stream result | (97.5, -56) | src 0x170 base 0x170 insns 92/92
NWC24ReadMsgSubject | read length initialized before mailbox selection | (96.14634, -9999) | src 0x144 base 0x148 insns 81/82
NWC24ReadMsgSubject | signed file read length | (98.65854, -9999) | src 0x144 base 0x148 insns 81/82
NWC24ReadMsgSubject | protection error stored in result | (98.65854, -9999) | src 0x144 base 0x148 insns 81/82
NWC24ReadMsgMBRegDate | scan counter increments explicitly after shift temporary | (99.545456, -3) | src 0x108 base 0x108 insns 66/66
NWC24ReadMsgMBRegDate | input cursor advanced through next-pointer temporary | (99.545456, -3) | src 0x108 base 0x108 insns 66/66
NWC24ReadMsgMBRegDate | signed hex digit count | (98.78788, -3) | src 0x108 base 0x108 insns 66/66
NWC24ReadMsgMBDelay | scan counter increments explicitly after shift temporary | (99.48276, -3) | src 0xe8 base 0xe8 insns 58/58
NWC24ReadMsgMBDelay | input cursor advanced through next-pointer temporary | (99.48276, -3) | src 0xe8 base 0xe8 insns 58/58
NWC24ReadMsgMBDelay | signed hex digit count | (98.62069, -3) | src 0xe8 base 0xe8 insns 58/58
NWC24ReadMsgMBUpdateSW | result declared before working pointer | (97.41071, -7) | src 0xe0 base 0xe0 insns 56/56
NWC24ReadMsgMBUpdateSW | message alias introduced after local result | (95.53571, -14) | src 0xe0 base 0xe0 insns 56/56
NWC24ReadMsgMBUpdateSW | result first then digit then cursor | (100.0, 0) | src 0xe0 base 0xe0 insns 56/56
ReadMsgTextInternal | separate encoding string and charset capacities | (93.735954, -9999) | src 0x2c0 base 0x2c8 insns 176/178
ReadMsgTextInternal | decoded count declared before truncation status | (93.735954, -9999) | src 0x2c0 base 0x2c8 insns 176/178
ReadMsgTextInternal | error status switches before closing | (91.88202, -9999) | src 0x2cc base 0x2c8 insns 179/178
NWC24ReadMsgAttached | attachment index converted to signed array index | (96.04395, -9999) | src 0x164 base 0x16c insns 89/91
NWC24ReadMsgAttached | decoded status declared before attachment length | (96.04395, -9999) | src 0x164 base 0x16c insns 89/91
NWC24ReadMsgAttached | alias attachment descriptor with real data pointer | (93.17583, -9999) | src 0x164 base 0x16c insns 89/91
ReadBase64Data | output cursor copied to explicit working pointer | (99.55696, -7) | src 0x13c base 0x13c insns 79/79
ReadBase64Data | source scan index declared before cursor | (97.974686, -26) | src 0x13c base 0x13c insns 79/79
ReadBase64Data | completion flag declared before status | (98.03797, -26) | src 0x13c base 0x13c insns 79/79
ReadQPText | use actual read extent for partial escape check | (98.6375, -24) | src 0x140 base 0x140 insns 80/80
ReadQPText | swap consumed and decoded count declarations | (98.725, -17) | src 0x140 base 0x140 insns 80/80
ReadQPText | output cursor copied to explicit working pointer | (99.3875, -15) | src 0x140 base 0x140 insns 80/80
ReadBase64Data | scan cursor declared before integer index | (99.620255, -4) | src 0x13c base 0x13c insns 79/79
ReadBase64Data | initialize status before completion flag | (99.68355, -5) | src 0x13c base 0x13c insns 79/79
ReadBase64Data | cursor-first scan and status-first initialization | (99.74683, -2) | src 0x13c base 0x13c insns 79/79
ReadQPText | remaining and offset lifetimes declared in input order | (99.9125, -7) | src 0x140 base 0x140 insns 80/80
ReadQPText | swap byte count slots | (99.475, -8) | src 0x140 base 0x140 insns 80/80
ReadQPText | input-order offsets and byte count slots | (100.0, 0) | src 0x140 base 0x140 insns 80/80
ReadBase64Data | cursor increment before index increment | (100.0, 0) | src 0x13c base 0x13c insns 79/79
NWC24ReadMsgMBRegDate | digit count before cursor in for increment | (99.545456, -3) | src 0x108 base 0x108 insns 66/66
NWC24ReadMsgMBRegDate | digit count in condition after consuming character | (100.0, 0) | src 0x108 base 0x108 insns 66/66
NWC24ReadMsgMBDelay | digit count before cursor in for increment | (99.48276, -3) | src 0xe8 base 0xe8 insns 58/58
NWC24ReadMsgMBDelay | digit count in condition after consuming character | (100.0, 0) | src 0xe8 base 0xe8 insns 58/58
NWC24ReadMsgMBOptOutFlag | return absent app id before hexadecimal scan | (99.61039, -3) | src 0x134 base 0x134 insns 77/77
NWC24ReadMsgMBOptOutFlag | explicit hexadecimal progress after reading each digit | (92.14286, -9999) | src 0x138 base 0x134 insns 78/77
NWC24ReadMsgMBOptOutFlag | separator early exit plus scan progress | (99.61039, -3) | src 0x134 base 0x134 insns 77/77
SelectMBox | mailbox selector with local error status | (2172, 96.739914)
SelectMBox | mailbox selector direct switch on status flags | (2172, 94.90558)
NWC24ReadMsgMBOptOutFlag | digit count in loop increment and cursor in body | (100.0, 0) | src 0x134 base 0x134 insns 77/77
## Remaining functions
NWC24ReadMsgField, 98.92157%: 101/102 instructions; protected-type load is reused across inlined mailbox selection.
NWC24ReadMsgFromAddr, 98.804344%: 91/92 instructions; type-load reuse and close-result control flow differ.
NWC24ReadMsgSubject, 98.65854%: 81/82 instructions; inlined mailbox selection reuses the protected-type load.
NWC24ReadMsgAttached, 96.04395%: 89/91 instructions; attachment addressing and saved-register allocation differ.
ReadMsgTextInternal, 93.735954%: 176/178 instructions; type-load reuse, empty-length handling, and saved-register allocation differ.
Shared-header changes are guarded by NWC24_MSG_READ, defined only by this unit.
No other unit output changes.

View File

@@ -1,179 +0,0 @@
# NWC24MsgSubject matching attempts
Baseline: no source, 0/12 functions, 0/5352 code bytes, 0/232 data bytes.
All twelve functions are implemented in object order. The initial readable C
reference from /tmp was checked against target assembly and Ghidra output.
Charset strings and pointer tables retain source order; .data and .sdata
match 100%, with an identical pool.
## Source-level experiments
NWC24ReadMsgSubjectPublic | switch write overflow status | (92.11539, -10) | src 0x1a0 base 0x1a0 insns 104/104
NWC24ReadMsgSubjectPublic | signed subject whitespace index | (96.15385, -9999) | src 0x190 base 0x1a0 insns 100/104
NWC24ReadMsgSubjectPublic | late original-size read | (96.15385, -9999) | src 0x190 base 0x1a0 insns 100/104
NWC24ReadMsgTextPublic | switch read overflow status | (94.14286, -5) | src 0x118 base 0x118 insns 70/70
NWC24ReadMsgTextPublic | truncation flag after size temporaries | (96.71429, -9999) | src 0x110 base 0x118 insns 68/70
NWC24ReadMsgTextPublic | explicit error status declaration first | (97.14286, -9999) | src 0x110 base 0x118 insns 68/70
NWC24SetMsgSubjectPublic | regional encoding switch | (94.59649, -9999) | src 0x1b0 base 0x1c8 insns 108/114
NWC24SetMsgSubjectPublic | charset initialized before detection | (87.89474, -9999) | src 0x1bc base 0x1c8 insns 111/114
NWC24SetMsgSubjectPublic | private message view used directly | (95.74561, -9999) | src 0x1b8 base 0x1c8 insns 110/114
NWC24SetMsgSubjectAndTextPublic | regional subject encoding switch | (0, -9999) | [1/1] MWCC build/43U/src/libs/RevoEX/src/nwc24/NWC24MsgSubject.o
NWC24SetMsgSubjectAndTextPublic | unsigned intermediate text size | (0, -9999) | [1/1] MWCC build/43U/src/libs/RevoEX/src/nwc24/NWC24MsgSubject.o
NWC24SetMsgSubjectAndTextPublic | initialize charset before charset parser | (86.23158, -9999) | src 0x2d0 base 0x2f8 insns 180/190
NWC24ReadMsgSubjectPublic | switch only exceptional statuses | (94.03846, -9999) | src 0x198 base 0x1a0 insns 102/104
NWC24ReadMsgTextPublic | switch only exceptional statuses | (95.57143, -9999) | src 0x114 base 0x118 insns 69/70
NWC24SetMsgSubjectAndTextPublic | regional subject switch at final choice | (91.405266, -9999) | src 0x2e8 base 0x2f8 insns 186/190
NWC24SetMsgSubjectAndTextPublic | defer subject workspace pointer calculation | (86.6579, -9999) | src 0x2c4 base 0x2f8 insns 177/190
NWC24SetMsgSubjectAndTextPublic | reorder text source/work count declarations | (90.052635, -9999) | src 0x2cc base 0x2f8 insns 179/190
NWC24iDetectEncodingToSend | continue when text charset advances subject charset | (72.376144, -9999) | src 0x18c base 0x1b4 insns 99/109
NWC24iDetectEncodingToSend | signed charset region switch | (58.19266, -9999) | src 0x154 base 0x1b4 insns 85/109
NWC24iDetectEncodingToSend | result index before loop bound check | (65.39449, -9999) | src 0x164 base 0x1b4 insns 89/109
NWC24iDetectBreakPoint | signed scan counter | (77.75676, -53) | src 0x128 base 0x128 insns 74/74
NWC24iDetectBreakPoint | direct size fit return | (77.554054, -53) | src 0x128 base 0x128 insns 74/74
NWC24iDetectBreakPoint | cache leading byte for scan | (73.432434, -9999) | src 0x120 base 0x128 insns 72/74
NWC24iSetMsgSubjectPlain | signed folding scan index | (88.424, -9999) | src 0x1e8 base 0x1f4 insns 122/125
NWC24iSetMsgSubjectPlain | moving newline normalization cursor | (87.904, -9999) | src 0x1e8 base 0x1f4 insns 122/125
NWC24iSetMsgSubjectPlain | defer folded buffer pointer calculation | (81.744, -9999) | src 0x1e8 base 0x1f4 insns 122/125
NWC24iSetMsgSubjectQP | signed folding scan index | (76.75, -9999) | src 0x258 base 0x240 insns 150/144
NWC24iSetMsgSubjectQP | moving newline normalization cursor | (76.298615, -9999) | src 0x258 base 0x240 insns 150/144
NWC24iSetMsgSubjectQP | defer folded buffer pointer calculation | (71.02778, -9999) | src 0x258 base 0x240 insns 150/144
NWC24iSetMsgSubjectBase64 | signed folding scan index | (70.0, -9999) | src 0x230 base 0x220 insns 140/136
NWC24iSetMsgSubjectBase64 | moving newline normalization cursor | (70.0, -9999) | src 0x230 base 0x220 insns 140/136
NWC24iSetMsgSubjectBase64 | defer folded buffer pointer calculation | (70.0, -9999) | src 0x230 base 0x220 insns 140/136
NWC24ReadMsgSubjectPublic | success/default status switch | (96.15385, -9999) | src 0x190 base 0x1a0 insns 100/104
NWC24ReadMsgTextPublic | success/default status switch | (97.14286, -9999) | src 0x110 base 0x118 insns 68/70
NWC24SetMsgSubjectPublic | individual regional switch cases | (97.149124, -9999) | src 0x1d4 base 0x1c8 insns 117/114
NWC24iSetMsgSubjectPlain | begin folding after first line and reuse capacity slot | (90.496, -96) | src 0x1f4 base 0x1f4 insns 125/125
NWC24iSetMsgSubjectPlain | conversion status switch | (89.304, -9999) | src 0x1ec base 0x1f4 insns 123/125
NWC24iSetMsgSubjectPlain | folded capacity comparison direction | (88.384, -9999) | src 0x1e8 base 0x1f4 insns 122/125
NWC24iSetMsgSubjectQP | unsigned charset-size arithmetic | (78.13194, -9999) | src 0x250 base 0x240 insns 148/144
NWC24iSetMsgSubjectQP | separate running and per-word output counts | (86.270836, -9999) | src 0x230 base 0x240 insns 140/144
NWC24iSetMsgSubjectQP | conversion status switch | (77.513885, -9999) | src 0x25c base 0x240 insns 151/144
NWC24iSetMsgSubjectBase64 | signed line size and unsigned charset length | (71.43382, -9999) | src 0x230 base 0x220 insns 140/136
NWC24iSetMsgSubjectBase64 | separate total and encoded word sizes | (79.70588, -9999) | src 0x208 base 0x220 insns 130/136
NWC24iSetMsgSubjectBase64 | folded output capacity cached in input capacity | (70.0, -9999) | src 0x230 base 0x220 insns 140/136
NWC24iDetectBreakPoint | supported charset branch and early breakpoint returns | (98.64865, -8) | src 0x128 base 0x128 insns 74/74
NWC24ReadMsgSubjectPublic | break from overflowing switch case | (96.15385, -9999) | src 0x190 base 0x1a0 insns 100/104
NWC24ReadMsgTextPublic | break from overflowing switch case | (97.14286, -9999) | src 0x110 base 0x118 insns 68/70
NWC24iDetectBreakPoint | pointer declared before byte-encoding flag | (100.0, 0) | src 0x128 base 0x128 insns 74/74
NWC24SetMsgSubjectPublic | merge Korean and Chinese base64 cases | (100.0, 0) | src 0x1c8 base 0x1c8 insns 114/114
NWC24SetMsgSubjectAndTextPublic | merge second base64 regional cases | (93.947365, -9999) | src 0x2dc base 0x2f8 insns 183/190
NWC24SetMsgSubjectAndTextPublic | regional text encoding switch | (94.789474, -125) | src 0x2f8 base 0x2f8 insns 190/190
NWC24SetMsgSubjectAndTextPublic | test external conversion status before seven-bit flag | (90.378944, -9999) | src 0x2e8 base 0x2f8 insns 186/190
NWC24SetMsgSubjectAndTextPublic | work pointer before message view | (94.789474, -125) | src 0x2f8 base 0x2f8 insns 190/190
NWC24SetMsgSubjectAndTextPublic | detection status switch | (95.36842, -9999) | src 0x2fc base 0x2f8 insns 191/190
NWC24SetMsgSubjectAndTextPublic | work pointer declared after size variables | (94.789474, -125) | src 0x2f8 base 0x2f8 insns 190/190
## Retained corrections
Grouped Korean/Chinese base64 cases match NWC24SetMsgSubjectPublic.
Breakpoint detection uses the supported-charset branch, separate UTF-8 scan,
early returns, and unsigned masked-byte comparison; it matches exactly.
Both internal-encoding conversion functions and the default charset selector
also match exactly. Plain subject folding starts after the initial line.
Charset detection repeats while the text charset advances the subject charset.
QP/base64 folding uses distinct running and per-word lengths; the short
single-word branch uses its initialized word length. Unsigned charset-length
arithmetic reproduces the target division. Removing unused base64 temporaries
and fixing those single-word branches gives QP 88.4375% and base64 81.94853%.
The detection-status switch gives SubjectAndText 95.36842%.
NWC24_MSG_SUBJECT is defined only by this source. Its header gate supplies the
five-argument, writable-name NWC24ReadMsgTextEx declaration seen in the target
callee, which overwrites r8 on entry. Every other translation unit keeps the
existing declaration. The gate reports zero regressions.
Several temporary variants failed compilation and were discarded. Unchanged
source variants in the raw log are not counted as distinct attempts. Every
remaining function has at least three actual source edits recorded.
## Remaining functions
NWC24ReadMsgSubjectPublic 96.15385%: 104 target instructions versus 100; overflow-handling branch structure
NWC24ReadMsgTextPublic 97.14286%: 70 target instructions versus 68; overflow-handling branch structure
NWC24SetMsgSubjectAndTextPublic 95.36842%: regional dispatch, detection-status flow and register allocation
NWC24iDetectEncodingToSend 72.376144%: charset scan state and search-loop scheduling/allocation
NWC24iSetMsgSubjectPlain 90.496%: folded-line control flow and allocation
NWC24iSetMsgSubjectQP 88.4375%: word-size accounting, folding control flow and allocation
NWC24iSetMsgSubjectBase64 81.94853%: conversion/encoding status flow and folding allocation
Retained: 5/12 exact functions; 1840/5352 code bytes; 232/232 data bytes.
## Continuation 2026-09-30
Baseline: 5/12 exact functions; 1840/5352 code bytes; 232/232 data bytes.
NWC24ReadMsgTextPublic | inline status-acceptance helper | (97.14286, -9999) | src 0x110 base 0x118 insns 68/70
NWC24ReadMsgTextPublic | successful reader branch before exceptional branch | (97.14286, -9999) | src 0x110 base 0x118 insns 68/70
NWC24ReadMsgTextPublic | subject conversion status separate from reader status | (97.14286, -9999) | src 0x110 base 0x118 insns 68/70
NWC24ReadMsgSubjectPublic | inline status-acceptance helper | (96.15385, -9999) | src 0x190 base 0x1a0 insns 100/104
NWC24ReadMsgSubjectPublic | walk subject through separate character pointer | (95.94231, -9999) | src 0x190 base 0x1a0 insns 100/104
NWC24ReadMsgSubjectPublic | signed decoded capacity temporary | (96.15385, -9999) | src 0x190 base 0x1a0 insns 100/104
NWC24SetMsgSubjectAndTextPublic | status switches | (96.52631, -9999) | src 0x304 base 0x2f8 insns 193/190
NWC24SetMsgSubjectAndTextPublic | grouped regional cases | (97.07895, -9999) | src 0x2f0 base 0x2f8 insns 188/190
NWC24SetMsgSubjectAndTextPublic | switches and grouped regional cases | (98.23684, -65) | src 0x2f8 base 0x2f8 insns 190/190
NWC24SetMsgSubjectAndTextPublic | work pointer declared before message alias | (98.23684, -65) | src 0x2f8 base 0x2f8 insns 190/190
NWC24SetMsgSubjectAndTextPublic | late work pointer initialization | (0, -9999) | [1/1] MWCC build/43U/src/libs/RevoEX/src/nwc24/NWC24MsgSubject.o
NWC24SetMsgSubjectAndTextPublic | derive subject buffer after text conversion | (98.23684, -65) | src 0x2f8 base 0x2f8 insns 190/190
NWC24SetMsgSubjectAndTextPublic | retain charset pointer instead of work object | (98.23684, -65) | src 0x2f8 base 0x2f8 insns 190/190
NWC24SetMsgSubjectAndTextPublic | declare input buffer partitions before work pointer | (98.23684, -65) | src 0x2f8 base 0x2f8 insns 190/190
NWC24SetMsgSubjectAndTextPublic | assign work pointer inside detection argument | (98.23684, -65) | src 0x2f8 base 0x2f8 insns 190/190
NWC24iSetMsgSubjectPlain | conversion status switch | (0, -9999) | [1/1] MWCC build/43U/src/libs/RevoEX/src/nwc24/NWC24MsgSubject.o
NWC24iSetMsgSubjectPlain | prepare fold capacity before fit test | (92.096, -96) | src 0x1f4 base 0x1f4 insns 125/125
NWC24iSetMsgSubjectPlain | capture consumed first line before copy | (91.296, -9999) | src 0x1f0 base 0x1f4 insns 124/125
NWC24iSetMsgSubjectQP | signed encoded word length | (88.4375, -9999) | src 0x22c base 0x240 insns 139/144
NWC24iSetMsgSubjectQP | capture fold capacity before charset length | (88.4375, -9999) | src 0x22c base 0x240 insns 139/144
NWC24iSetMsgSubjectQP | normalize via moving source pointer | (87.986115, -9999) | src 0x22c base 0x240 insns 139/144
NWC24iSetMsgSubjectBase64 | signed encoded word length | (81.94853, -9999) | src 0x20c base 0x220 insns 131/136
NWC24iSetMsgSubjectBase64 | multiply line length before shifting | (81.42647, -9999) | src 0x20c base 0x220 insns 131/136
NWC24iSetMsgSubjectBase64 | capture remaining input after fold capacity check | (79.94118, -9999) | src 0x20c base 0x220 insns 131/136
NWC24iDetectEncodingToSend | text status stored in search convergence index | (72.23853, -9999) | src 0x18c base 0x1b4 insns 99/109
NWC24iDetectEncodingToSend | encoding checks use status switches | (72.376144, -9999) | src 0x18c base 0x1b4 insns 99/109
NWC24iDetectEncodingToSend | region table selected with switch | (82.55963, -9999) | src 0x198 base 0x1b4 insns 102/109
NWC24iDetectEncodingToSend | store selected index at each search exit | (96.04587, -46) | src 0x1b4 base 0x1b4 insns 109/109
NWC24iDetectEncodingToSend | exit index plus cached byte sizes | (93.99083, -47) | src 0x1b4 base 0x1b4 insns 109/109
NWC24iSetMsgSubjectPlain | explicit fold overflow first and postincrement writes | (99.144, -20) | src 0x1f4 base 0x1f4 insns 125/125
NWC24iSetMsgSubjectPlain | postincrement writes with initial source cursor | (94.944, -9999) | src 0x1f0 base 0x1f4 insns 124/125
NWC24iDetectEncodingToSend | initialize search start before region dispatch | (96.60551, -32) | src 0x1b4 base 0x1b4 insns 109/109
NWC24iDetectEncodingToSend | byte count locals after region selection | (96.27523, -47) | src 0x1b4 base 0x1b4 insns 109/109
NWC24iDetectEncodingToSend | order table count and search locals by lifetime | (96.04587, -46) | src 0x1b4 base 0x1b4 insns 109/109
NWC24iSetMsgSubjectPlain | source offset declared before second buffer | (99.904, -2) | src 0x1f4 base 0x1f4 insns 125/125
NWC24iSetMsgSubjectPlain | line length initialized before buffer capacity | (99.0, -23) | src 0x1f4 base 0x1f4 insns 125/125
NWC24iSetMsgSubjectPlain | fold pointer declared after counters | (99.904, -2) | src 0x1f4 base 0x1f4 insns 125/125
NWC24iSetMsgSubjectPlain | zero break length before source size initialization | (99.904, -2) | src 0x1f4 base 0x1f4 insns 125/125
NWC24iSetMsgSubjectPlain | zero break length before partition pointer initialization | (99.904, -2) | src 0x1f4 base 0x1f4 insns 125/125
NWC24iSetMsgSubjectPlain | initialize source and break sizes before work capacity | (99.76, -6) | src 0x1f4 base 0x1f4 insns 125/125
NWC24iDetectEncodingToSend | cache byte lengths before end index | (98.073395, -34) | src 0x1b4 base 0x1b4 insns 109/109
NWC24iDetectEncodingToSend | byte lengths and table local order | (98.073395, -34) | src 0x1b4 base 0x1b4 insns 109/109
NWC24iDetectEncodingToSend | search start variable declared after previous index | (96.60551, -32) | src 0x1b4 base 0x1b4 insns 109/109
NWC24iSetMsgSubjectPlain | break length initialized at declaration | (99.76, -6) | src 0x1f4 base 0x1f4 insns 125/125
NWC24iSetMsgSubjectPlain | break length declared first | (99.768, -20) | src 0x1f4 base 0x1f4 insns 125/125
NWC24iSetMsgSubjectPlain | input length declared after break length | (99.808, -14) | src 0x1f4 base 0x1f4 insns 125/125
NWC24iSetMsgSubjectPlain | work capacity initialized at declaration | (99.904, -2) | src 0x1f4 base 0x1f4 insns 125/125
NWC24iDetectEncodingToSend | locals ordered by independent search roles | (100.0, 0) | src 0x1b4 base 0x1b4 insns 109/109
NWC24iSetMsgSubjectPlain | partition capacity computed after break initialization | (100.0, 0) | src 0x1f4 base 0x1f4 insns 125/125
NWC24iSetMsgSubjectQP | running output count excludes terminator and cached fold limit | (96.30556, -28) | src 0x240 base 0x240 insns 144/144
NWC24iSetMsgSubjectQP | source cursor lifetime and partition initialization | (99.34028, -16) | src 0x240 base 0x240 insns 144/144
NWC24iSetMsgSubjectQP | fold limit before source offset declaration | (99.34028, -16) | src 0x240 base 0x240 insns 144/144
NWC24iSetMsgSubjectQP | source cursor declared before capacity variable | (99.34028, -16) | src 0x240 base 0x240 insns 144/144
NWC24iSetMsgSubjectQP | running output computed through temporary before cursor advance | (99.548615, -12) | src 0x240 base 0x240 insns 144/144
NWC24iSetMsgSubjectQP | fold limit replaces exhausted partition capacity | (99.548615, -12) | src 0x240 base 0x240 insns 144/144
NWC24iSetMsgSubjectQP | source offset declared after output word size | (99.270836, -20) | src 0x240 base 0x240 insns 144/144
NWC24iSetMsgSubjectQP | capacity declared after folded buffer | (99.548615, -12) | src 0x240 base 0x240 insns 144/144
Retained continuation: encoding detection stores each selected charset index
at its actual search exit and caches input byte lengths. Plain folding uses
the explicit overflow branch and increments its output cursor for CR/LF.
Both functions are instruction-exact. SubjectAndText has matching control
flow but different saved-register allocation. QP caches its fold limit and
tracks the output length without the terminator; 144/144 instructions remain,
with twelve differences from the capacity/source-cursor register exchange.
All prior exact functions and all charset tables and strings are preserved.
## Remaining functions
NWC24iSetMsgSubjectQP, 99.548615%: 144/144 instructions; twelve register differences from capacity/source-offset allocation.
NWC24SetMsgSubjectAndTextPublic, 98.23684%: 190/190 instructions; 65 saved-register differences.
NWC24ReadMsgTextPublic, 97.14286%: 68/70 instructions; overflow-status branch lowering omits two target branches.
NWC24ReadMsgSubjectPublic, 96.15385%: 100/104 instructions; two overflow-status blocks each omit two target branches.
NWC24iSetMsgSubjectBase64, 81.94853%: 131/136 instructions; conversion-status branches and folded-output accumulation differ.

View File

@@ -1,15 +0,0 @@
# NWC24System matching attempts
Baseline: no source; 0/4 exact functions, 0/808 code, 0/192 data.
The SDK shutdown reference was adapted to this target using its assembly
and object-exported Ghidra output. LED state uses SCIdleModeInfo; IPC
shutdown input/output arrays use required 32-byte alignment. The inline
shutdown request retains its own function-name literal through the IPC macro.
All functions, .data, .sdata, .bss, and .sbss match. Unit remains NonMatching.
## Source attempts
NWC24DoDailyTasks | random value before tick in sampling sum | (99.875, -1) | src 0x140 base 0x140 insns 80/80
NWC24DoDailyTasks | unsigned random value retained in sampling temporary | (100.0, 0) | src 0x140 base 0x140 insns 80/80
No remaining non-matching functions.

View File

@@ -1,15 +0,0 @@
# FA batch remaining instruction differences
- PFSYS_TimeStamp: vf date structure and u16 outputs were replaced with the actual OS calendar and u32 outputs; first 29 instructions became identical. Explicit OSTime temporary plus explicit return produced the same 31 instructions. Restored direct expression. Target symbol ends after mtlr and excludes two actual epilogue instructions; symbol boundaries were preserved.
- PFFAT12_WriteFATEntryWithBuf: replaced vf page range logic with a single buffered sector and real mirrored writes (175/175, 19 differences). Signed cluster/offset arithmetic and counter declaration order reduced this to eight register and addition operand differences. A u16 sector offset introduced two truncation instructions (177/175); restored u32. Pointer-first versus offset-first indexing did not change addition operand selection.
- PFFAT12_ReadFATEntryWithBuf: replaced vf multi-sector cache range macro with single sector flush/read (155/155, 34 register differences). Initialized error in its declaration moved one instruction (35 differences); restored assignment at loop entry. Reordered error/current FAT/sector/offset declarations preserved 155 instructions with 34 register differences.
- PFFAT16_WriteFATEntryWithBuf: single buffered sector implementation produced 66/66 instructions with seven differences, two swapped registers and one addition operand order. Moving sector/offset declarations did not change allocation. Moving offset first and using offset-first pointer indexing did not change allocation. u16 offset and u16 sector types also preserved the same seven differences.
pdm_disk_convert_block_into_sector: the target name contains a trailing backtick, which is not a C identifier. The ordinary C spelling has 44 instructions identical to the target. Tried the VF conversion expression, explicit 16-bit ratio extraction, and a 32-bit byte-size argument; instruction bytes remain identical. No symbol metadata or assembly alias was added.
pdm_part_is_master_boot_sector: indexed buffer offsets produced 90/84 instructions; advancing the buffer produced 86/84; high-to-low byte addition and paired addition forms both retained 86/84 with an extra saved register. Kept paired little-endian additions.
pdm_part_get_start_sector: separate start/count assignments produced 276/276 with 151 differences; interleaved assignments produced 146 differences; a decode loop reduced the function to 243/276 and was reverted. Remaining scheduling and register allocation differ.
pdm_part_chg_ltop: initial ratio/start temporaries produced 51/51 with 16 register differences; reversed declarations and loading start before ratio both retained those differences. The compiler exchanges r0 and r3 throughout the conversion.
fa_nanddrv_ParseCreateNANDFile: direct error conjunctions and early path initialization produced 132/130 instructions; moving path initialization below the format check and switching on NANDCreate errors produced 134/130; success branches around file creation reduced this to 131/130; nesting the directory success path produced 130/130 with 10 branch/scheduling differences. The remaining block places the directory-error exit differently.
fa_nanddrv_NotifyNANDFile: indexed lookup produced 72/72 with 15 register differences; advancing a record pointer produced 74/72 and was reverted; testing the indexed disk field after strcmp retained the offset across the call and reduced differences to 12; reversing pointer/index declarations left those 12 differences. Saved arguments, record index, and record offset occupy different registers.
fa_nanddrv_physical_write: the shared lookup and error helpers produced 184/175 instructions; direct indexed lookup produced 183/175; a void error-store helper eliminated unused helper-result comparisons, giving 175/175 with 38 register differences; reordering remaining-size, file-size, and seek-offset declarations gave 42 differences and was reverted; moving the error declaration also left 42 differences. The retained version has 38 saved-register differences.

View File

@@ -1,26 +0,0 @@
# fa/pf_cluster matching attempts
All eight functions are implemented. The five exact functions have zero instruction differences. The three open functions have identical instruction counts to the target and differ in register allocation or arithmetic scheduling. The unit has no target data section.
## PFCLUSTER_CombineFiles (207 instructions)
1. Initial C reconstruction: 206 instructions; swapped stack addresses and one missing branch in the second spare-chain selection.
2. Separate second-chain selection assignment, reversed stack declarations, and grouped cluster totals: 207 instructions, four differences in the overflow comparison's registers and addition order. Retained.
3. Explicit total-cluster temporary, then an explicit maximum-cluster temporary: seven differences in the same comparison; the compiler retained different temporary registers.
4. Reversed temporary initialization order: seven differences; restored attempt 2.
## PFCLUSTER_InsertCluster (126 instructions)
1. Initial C reconstruction: 28 register differences; stack locations and instruction count match. Retained.
2. Reused the incoming cluster count as the allocation-loop byte count: the same 28 differences.
3. Copied the incoming count to an allocation local before the first call: 29 differences, including changed argument scheduling; no improved register assignment.
4. Initialized the allocation count before tracing and removed the loop initializer: 36 differences, with the same total instruction count; restored attempt 1.
## PFCLUSTER_DeleteCluster (108 instructions)
1. Initial C reconstruction: 51 differences, including reversed stack locations and saved-register assignments.
2. Reordered output locals, moved volume before size locals, and reused the incoming count for the deletion byte count: 35 register differences; all stack locations and instruction count match. Retained.
3. Reversed the volume/size declaration order: 42 register differences; no instruction-count improvement.
4. Scoped the size computations in a nested block with a separate deletion-size local: 35 differences, with a different count register; restored attempt 2.
The forbidden-pattern scan initially classified the real FAT spare-chain variables as dummy objects because their inherited names contained `unused`. Renamed them to `spare`; these variables are consumed by chain tracing and freeing.

View File

@@ -1,176 +0,0 @@
# tiHKBManager attempts
Baseline: no source, 0/29 instruction-exact functions and 0/6116 code bytes.
The pool is empty and identical from the first object build. All fields and
calls use typed keyboard declarations. Only TIHKBMANAGER_IMPLEMENTATION selects
the added layouts; existing keyboard guard branches remain unchanged.
## First pass attempts, in object order
### SetLedCB
1. Initialized pointer union followed by device byte: 41/41 instructions,
six differences in stack offsets and store scheduling.
2. Typed LED request containing the device and LED mask, using its mask as
the asynchronous call argument: 42/41, one extra load and mask conversion.
3. Snapshot the request mask before disabling interrupts: 42/41, unchanged.
4. Make the snapshot const: 42/41, unchanged. Retained this readable request.
A literal mask enum reached 41/41 with zero differences, but left the
request's mask unused; that experiment was discarded.
### KBDListenerOwn::OnAttach
1. Pointer union initialized to zero: 59/61 instructions; saved-register
allocation and callback stores differed.
2. LED request and mask argument: 60/61; allocation still differed.
3. Declare owner/device outside the branch: request with a literal mask
reached 61/61 and zero differences.
4. Read the actual request mask into a local, then make that local const:
both 62/61, an extra load and mask conversion. Retained the const snapshot
so both request fields have a real use.
### KBDListenerOwn::OnDetach
1. Owner alias and typed Clear helper: 22/22, four register differences.
2. Declare a state pointer before clearing attached: 22/22, same four.
3. Access manager directly twice: 23/22, an additional owner load.
4. State reference before owner declaration: 22/22, six differences.
5. Owner declared before device: 22/22, six differences. Retained attempt 1.
### HKBManager::Update
1. Zero-initialized pointer union: 55/55, two callback-store differences.
2. Typed LED request used by the asynchronous call: 55/55, one argument
conversion difference.
3. Literal LED mask enum: 55/55, zero differences, but unused request mask.
4. Snapshot the request mask, then make it const: 55/55, one conversion
difference. Retained the const snapshot with both request fields used.
### KeyState_::Update
1. Explicit groups of two event/repeat slots: 183/183, 63 differences.
2. Ordinary loops over eight slots: 183/183, 51 differences.
3. Snapshot held keys before clearing triggers: 183/183, 47 differences.
4. Hoist shared bit/index declarations: 183/183, unchanged.
5. Cache previous/current array pointers: 247/183, rejected.
6. Reverse bit/index declaration order: 183/183, 44 differences, retained.
Registers and repeat-array address calculation still differ.
### KeyState_::UpdateModState_
1. Loop counter before old modifier snapshot: 104/104, seven differences.
2. Move old modifier snapshot before counter: 104/104, ten differences.
3. Use a byte slot index: 104/104, seven differences.
4. Separate slot/key-mask declarations and assignments: 104/104, unchanged.
5. Scope the counter inside for: 104/104, ten differences. Retained attempt 4.
### KeySet::GetWChar
1. Original nested descending range tests: 70/77 instructions.
2. Ascending range branches, u32 virtual-code return, direct whitelist
indexing and early exits: 74/77.
3. Signed code temporary: 82/77, rejected.
4. Separate u16 character for the private-code mask: 75/77, retained.
5. Inline range predicate accepting u16: 89/77, rejected.
The retained version lacks two repeated narrowing instructions and uses
different whitelist-loop registers.
### KeySet::IsValid
1. Direct guard/switch with owner alias: 70/70, 18 register differences.
2. Hoist index/device declarations before owner: 70/70, 12 differences.
3. Reverse those declaration positions: 70/70, unchanged.
4. Widen index/device temporaries: 70/70, 14 differences.
5. Inline the same CheckValidity helper used by GetKey/GetVCode:
70/70, 12 differences, retained. Index/device registers are exchanged.
## First pass exact functions
All other functions are C++ and instruction-exact. Important successful
changes: sequential Clear stores; callback traversal compares its next node
with the owning listener; a polymorphic listener base initializes its links
before the derived vtable; NotifyEvent shares its loop index outside both
branches; SetModifierState computes its comparison before changing the field
and compares new locks against old locks; KeySet uses typed validity and
separate switch cases; GetVCode declares the second modifier mask before its
state value. The constructor uses the compiler
never_inline attribute so static initialization calls the real constructor.
Both the constructor and its static initializer are exact. A scoped pragma
experiment was rejected by the source gate and removed. Changing the
constructor loop to do/while produced 35/45 constructor and 43/18 initializer
instructions; the ordinary for loop was restored.
## Data and files
The singleton and compiler-generated destructor registration supply .bss.
The compiler supplies the constructor entry and listener vtable. No literal
pool, pinned addresses, hand-built vtable, or forced sections are added.
A weak abstract listener-base vtable is also compiler-generated.
Changed files:
- src/keyboard/tiHKBManager.cpp
- include/keyboard/tiHKBManager.h
- libs/RVL_SDK/include/revolution/kbd.h
- tools/decomp-assist/tiHKBManager.attempts.md
## Continuation at 21/29 exact
Baseline code 3664/6116, data 292/292. The empty string pool remains identical.
The five still-open functions were visited closest first; each has at least
three new source variations in this pass.
### KeyState_::UpdateModState_ — still open
1. Signed loop counter: 104/104 instructions, same seven register differences.
2. Initialize key mask and widened byte slot in one declaration each:
104/104, ten differences.
3. Hoist key mask and byte slot outside the loop: 104/104, ten differences.
Retained the original seven-difference version.
### KeySet::IsValid — still open
1. Direct validity body with the shifted bit as the left operand:
70/70, 18 register differences.
2. Declare the signed index before the owner alias: 70/70, 12 differences.
3. Widen device/index to signed words and compare device as unsigned:
70/70, 14 differences.
Retained the shared CheckValidity helper, 12 differences.
### KBDListenerOwn::OnDetach — still open
1. Widen device to u32: 22/22, four register differences, unchanged.
2. Inline DetachDevice helper combining attached flag and state clearing:
22/22, five differences.
3. Name the state reference inside that helper before flag clearing:
22/22, six differences.
Removed the experimental helper and retained the four-difference version.
### KeyState_::Update — still open
1. Replace the eight explicit key snapshots with a normal loop:
183/183, same 44 register differences. Retained for readability.
2. Signed event/repeat loop index: 183/183, same 44 differences.
3. Local bit temporaries and negated unchanged-key predicates:
183/183, 47 differences.
### KeySet::GetWChar — improved, still open
1. Signed virtual code with explicitly unsigned mask comparisons:
76/77 instructions, one repeated narrowing still absent.
2. Inline normalization helper returning each normalized value:
75/77, unchanged from baseline.
3. u16 intermediate character and do/while whitelist traversal:
74/77, rejected.
4. Distinguish the initial 16-bit key range from the signed normalization
intermediate and subsequent unsigned masks: 77/77, nine whitelist-loop
register differences. Retained.
5. Hoist whitelist index declaration: 77/77, unchanged.
6. Initialize that index before the owner alias: 77/77, unchanged.
7. Signed whitelist index: 77/77, unchanged.
8. Count down remaining whitelist entries: 76/77, rejected.
9. Unsigned main code and signed normalization intermediate:
77/77, same nine differences, rejected in favor of attempt 4.
### LED operations — three new exact functions
1. Widen the asynchronous LED argument and snapshot to u32:
SetLedCB 41/41 with two differences, OnAttach 61/61 with three,
HKBManager::Update 55/55 with three.
2. Move the LED mask outside the opaque-pointer/device union, making the
request a struct: identical differences and counts.
3. Snapshot the value assigned when clearing the request's LED mask:
all three functions reach zero differences.
4. Restore the original byte API argument and byte snapshot:
all three remain exact, without emitted LED constant objects.
The mask field supplies the call argument through the assignment result;
there is no separate literal argument or unused request-mask initialization.
Data sections remain 100%. No shared header changes are retained in this
continuation. A quick gate reports 24/29 exact, code 4292/6116,
data 292/292, fuzzy 99.7057, and zero regressions or source warnings.

View File

@@ -1,17 +0,0 @@
# tiString attempts
Initial reconstruction uses the sibling tiString and tiUtil headers, real member definitions and compiler-generated vtables. The unit has no string literals; both pools are empty and identical. The normal derived destructor emits its vtable before the base vtable, restoring all 288 data bytes. The extracted target omits the derived destructor body; this additional ordinary destructor will need consideration during the later linking phase.
## insert
1. Separate getLength temporary and typed string indexing: 79/79 instructions, eight scheduling/register differences.
2. Precompute a tail pointer before getLength: 76/79 instructions, additional callee-saved register and address lifetime differences.
3. Precompute an element index: 79/79, same eight differences.
4. Put getLength directly in wcsncpy's length argument: 79/79, zero differences; retained.
## inputChar
1. Remove inherited scratch source's volatile buffer pointer, use an ordinary pointer and u32 count: 126/136. Kana conversion matches; Hangul path folds indexed stores and cursor additions lose two narrowing instructions.
2. u16 count and separate newline/ordinary character branches: 126/136, same differences. Compiler folds the equivalent branches; discarded the redundant condition.
3. Direct array indexing with no buffer alias, u16 count: 125/136. Hangul output collapses to direct stores and constant count; retained the readable version. Remaining mismatch is the Hangul output indexing/count lifetime and subsequent cursor-add narrowing, not strings or data.
## character predicates
The sibling util declarations return bool, while target callers normalize an integer result. Guarded u32 declarations preserve every other translation unit and make all three predicates 16/16 with zero differences.

View File

@@ -1,11 +0,0 @@
# tiTextDrawer attempts
No source existed. Reconstructed all 24 functions using tiTextDrawer.h and tiUtil.h, with a private implementation macro preserving every existing guard branch. Replaced raw scratch offsets with viewport/projection/string/cache fields and typed DrawInfo members. Ordinary table initialization loops reproduce the compiler's 32-store unrolling. Pools are empty and identical.
## beginDraw
1. Direct scale * viewport width expression: 109/109 instructions, one difference at instruction 59, fmuls f7,f11,f7 versus fmuls f7,f7,f11.
2. Reverse multiplication operands: unchanged single difference; compiler canonicalizes this expression.
3. Separate scaledWidth temporary with compound multiplication: 109/109, sixteen floating scheduling/register differences. Discarded; retained attempt 1.
## data
BSS is 1408/1408. The .sdata2 constants have identical order and bytes for all 44 emitted bytes. The extracted object has four additional zero bytes at its section end; no padding object was added. The vtable has one relocation-name discrepancy: the target references isEnableCursorCache__Q39textinput10textdrawer4BaseCFvgetStartPos__Q39textinput10textdrawer4BaseCFv, a concatenated symbol name present in symbols.txt, whereas normal C++ references isEnableCursorCache__Q39textinput10textdrawer4BaseCFv. Kept ordinary declarations rather than renaming a real function or introducing a fabricated alias. Other data bytes and vtable entries match.

View File

@@ -1,45 +0,0 @@
# tiZiString attempts
Created readable members from the extracted object and the existing tiString, tiZiString, tiUtil and tiCpData declarations. No source existed at task start. Replaced old scratch source's raw field casts, fabricated vtables, address labels and casts to an incompatible EZTXGetCandidates prototype. Reused PaneNameToCharCode::wc for holding-key characters; dictionary offsets are the actual serialized OEM dictionary format. Both string pools are empty. All 7680 data bytes match: BSS buffers in original order, ordinary Korean letter tables, ten Latin search-order entries, compiler switch table and vtable. The second Korean lookup table has the target's sixteen wchar_t slots, initialized with its ordinary fourteen-character literal and terminator.
## destructor
1. Normal destructor with only the dictionary allocation release: 29/31 instructions. Base destructor relocation was Decolated, whereas target calls StringBase.
2. Supply the ordinary inline Decolated destructor: 31/31, zero differences. Retained; the compiler supplies the additional null test and base destruction.
## clearCandidates
1. Typed EZTX fields and actual CandidatesBuffer subarrays instead of out-of-bounds ElementBuffer aliases: 84/81, 88.5679%. An additional pooled-buffer base register is live.
2. Reorder elementCount before firstCandidate as in the target: still 84/81, improved to 91.049385%. Retained.
3. Initialize typed elements/candidates pointers before the open-dictionary test: 80/81, 83.34568%, with additional saved registers and changed address lifetimes. Discarded.
Remaining: pooled buffer addresses and their register lifetimes; no raw cross-object offset alias is used.
## partialConfirmForKR
1. Explicit grouped comparisons inherited from scratch: 153/147; duplicated one-character fallback and missing loop-index maintenance.
2. Ordinary bounded indexed loops with initialized match flag and shared fallback: 148/147; flag lifetime differed.
3. Assign match only on loop exit, share fallback via labels: 147/147, 23 register differences.
4. Share loop index across both scans: unchanged 23 differences.
5. Pointer scans with postincrement: 147/147, 21 differences including scheduling.
6. Increment pointers after the unsuccessful comparison: 147/147, twelve differences, all r4/r5 in the second scan. Retained.
7. Initialize the second index before assigning its table pointer: unchanged twelve differences. These are allocator tie-breaks.
## update
1. Replace raw EZTX bytes with fields and holding-key offsets with typed records: 441/446, 77.3296%. This exposed incorrect scratch semantics and a wrong parameter-field layout.
2. Reset selected candidate, use a zero-extended element count, correct EZTX byte fields, use context |= 0x20 and inclusive element bounds; use a real bounded key-character loop and normal two-argument library call: 455/446. Corrected state/parameter writes and removed the incompatible function-pointer call.
3. Replace the manually expanded latest-word copy with an ordinary loop: 456/446.
4. Make the zero-extended element count a signed comparison operand: 456/446, 84.84529%.
5. Reuse sibling PaneNameToCharCode::wc; preserve the destination base with separate copy cursors; share successful candidate counting and use the target's letter-mode switch: 448/446, 91.4305%. Retained.
Remaining: buffer-base lifetimes, phone-loop scheduling, context-copy loop and saved-register allocation. No artificial extra library arguments retained.
## setElementBuffer
1. Typed indices replacing raw byte offsets: 67/65, 88.58462%; an extra index narrowing and secondary buffer base.
2. u32 element index rather than u16: 67/65; same count, different shift/narrowing.
3. Direct typed indices in condition/body: 66/65, with an extra load of the input character.
4. Initialize typed pointers/count before memset and use the condition's loaded character in the body: 65/65, twenty-four instruction differences; 87.815384%. Retained as the closest instruction structure.
5. Initialize pointers after memset and keep a u32 masked index: 66/65, 87.49231%. Discarded.
Remaining: pooled buffer base setup and callee-saved register assignments, with identical instruction count.
## setCurrentWord
1. Normal element indexing in the existing copy loops: 56/56, twenty-one differences; compiler strength-reduces indexed accesses to moving pointers.
2. Load source character then advance length/cursors before storing: 56/56, same differences.
3. Unsigned source/destination indices, store via length - 1: 56/56, same differences.
4. Remove the now-unused destination index: retained readable implementation; no output change.
Remaining: copy-loop address strength reduction, scheduling and caller-register assignments; 86.69643%.