From f1b1537c613260de4c7e0bb7f11bac11ca96a759 Mon Sep 17 00:00:00 2001 From: hikarupsp Date: Wed, 3 Jul 2013 03:07:59 +0900 Subject: [PATCH] =?utf8?q?chnlib:CHNLIB=5FUUID(UUIDv4),=20=E3=83=A1?= =?utf8?q?=E3=83=AB=E3=82=BB=E3=83=B3=E3=83=8C=E3=83=BB=E3=83=84=E3=82=A4?= =?utf8?q?=E3=82=B9=E3=82=BF=E3=83=BC=E3=82=92=E5=88=A9=E7=94=A8=E3=81=97?= =?utf8?q?=E3=81=9F=E4=B9=B1=E6=95=B0=E7=94=9F=E6=88=90=E5=99=A8CHNLIB=5FR?= =?utf8?q?andomGeneratorMT=E3=82=92=E8=BF=BD=E5=8A=A0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- CHNOSProject/chn/chn.xcodeproj/project.pbxproj | 8 + .../xcschemes/xcschememanagement.plist | 2 +- CHNOSProject/chn/chnlib.h | 39 ++- CHNOSProject/chn/chnlib02.c | 15 ++ CHNOSProject/chn/chnlib05.c | 289 +++++++++++++++++++++ CHNOSProject/chn/chnlib06.c | 266 +++++++++++++++++++ .../libtest/libtest.xcodeproj/project.pbxproj | 6 + .../xcschemes/xcschememanagement.plist | 2 +- CHNOSProject/libtest/libtest/main.c | 53 ++-- 9 files changed, 652 insertions(+), 28 deletions(-) create mode 100644 CHNOSProject/chn/chnlib05.c create mode 100644 CHNOSProject/chn/chnlib06.c diff --git a/CHNOSProject/chn/chn.xcodeproj/project.pbxproj b/CHNOSProject/chn/chn.xcodeproj/project.pbxproj index 13eff5a..9799eb9 100644 --- a/CHNOSProject/chn/chn.xcodeproj/project.pbxproj +++ b/CHNOSProject/chn/chn.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 174CE470177FEF23004FE674 /* chnlib05.c in Sources */ = {isa = PBXBuildFile; fileRef = 174CE46F177FEF23004FE674 /* chnlib05.c */; }; + 174CE472177FFE16004FE674 /* chnlib06.c in Sources */ = {isa = PBXBuildFile; fileRef = 174CE471177FFE16004FE674 /* chnlib06.c */; }; 17D0F40116DA313700C62B79 /* chnlib.c in Sources */ = {isa = PBXBuildFile; fileRef = 17D0F3FA16DA313700C62B79 /* chnlib.c */; }; 17D0F40216DA313700C62B79 /* chnlib.h in Headers */ = {isa = PBXBuildFile; fileRef = 17D0F3FB16DA313700C62B79 /* chnlib.h */; settings = {ATTRIBUTES = (Public, ); }; }; 17D0F40316DA313700C62B79 /* chnlib00.c in Sources */ = {isa = PBXBuildFile; fileRef = 17D0F3FC16DA313700C62B79 /* chnlib00.c */; }; @@ -17,6 +19,8 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 174CE46F177FEF23004FE674 /* chnlib05.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = chnlib05.c; sourceTree = ""; }; + 174CE471177FFE16004FE674 /* chnlib06.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = chnlib06.c; sourceTree = ""; }; 17D0F3ED16DA305000C62B79 /* libchn.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libchn.a; sourceTree = BUILT_PRODUCTS_DIR; }; 17D0F3FA16DA313700C62B79 /* chnlib.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = chnlib.c; sourceTree = ""; }; 17D0F3FB16DA313700C62B79 /* chnlib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 2; path = chnlib.h; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; @@ -48,6 +52,8 @@ 17D0F3FE16DA313700C62B79 /* chnlib02.c */, 17D0F3FF16DA313700C62B79 /* chnlib03.c */, 17D0F40016DA313700C62B79 /* chnlib04.c */, + 174CE46F177FEF23004FE674 /* chnlib05.c */, + 174CE471177FFE16004FE674 /* chnlib06.c */, 17D0F3EE16DA305000C62B79 /* Products */, ); sourceTree = ""; @@ -128,6 +134,8 @@ 17D0F40516DA313700C62B79 /* chnlib02.c in Sources */, 17D0F40616DA313700C62B79 /* chnlib03.c in Sources */, 17D0F40716DA313700C62B79 /* chnlib04.c in Sources */, + 174CE470177FEF23004FE674 /* chnlib05.c in Sources */, + 174CE472177FFE16004FE674 /* chnlib06.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/CHNOSProject/chn/chn.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist b/CHNOSProject/chn/chn.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist index 9cc691f..8362af2 100644 --- a/CHNOSProject/chn/chn.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/CHNOSProject/chn/chn.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,7 +7,7 @@ chn.xcscheme orderHint - 2 + 3 SuppressBuildableAutocreation diff --git a/CHNOSProject/chn/chnlib.h b/CHNOSProject/chn/chnlib.h index 88d235b..c5d7b62 100755 --- a/CHNOSProject/chn/chnlib.h +++ b/CHNOSProject/chn/chnlib.h @@ -49,6 +49,15 @@ struct CHNLIB_STRUCTURE_HEADER { uint (*getHash)(const void *structure); }; +//@chnlib00.c +typedef struct CHNLIB_UIPARRAY CHNLIB_UIPArray; +//@chnlib01.c +typedef struct CHNLIB_STRING CHNLIB_String; +//@chnlib05.c +typedef struct CHNLIB_RANDOM_GENERATOR_MT CHNLIB_RandomGeneratorMT; +//@chnlib06.c +typedef struct CHNLIB_UniversallyUniqueIDentifier CHNLIB_UUID; + // //Define library values // @@ -58,9 +67,11 @@ struct CHNLIB_STRUCTURE_HEADER { #define False 0 #define True 1 -#define CHNLIB_STRUCT_ID_Null 0 -#define CHNLIB_STRUCT_ID_UIPArray 1 -#define CHNLIB_STRUCT_ID_String 2 +#define CHNLIB_STRUCT_ID_Null 0 +#define CHNLIB_STRUCT_ID_UIPArray 1 +#define CHNLIB_STRUCT_ID_String 2 +#define CHNLIB_STRUCT_ID_RandomGeneratorMT 3 +#define CHNLIB_STRUCT_ID_UUID 4 #define CHNLIB_MAX_STRING_LENGTH 4096 @@ -86,8 +97,6 @@ void CHNLIB_Retain(void **structure); void CHNLIB_Release(void **structure); void *CHNLIB_AutoRelease(void *structure); //@chnlib00.c -typedef struct CHNLIB_UIPARRAY CHNLIB_UIPArray; -struct CHNLIB_UIP_ARRAY; //----配列の初期化 CHNLIB_UIPArray *CHNLIB_UIPArray_Initialize(void); //----データの追加 @@ -117,8 +126,6 @@ CHNLIB_UIPArray *CHNLIB_UIPArray_CopyArray(const CHNLIB_UIPArray *source); CHNLIB_UIPArray *CHNLIB_UIPArray_GetSortedInDescendingOrderByData32(const CHNLIB_UIPArray *array); //@chnlib01.c -typedef struct CHNLIB_STRING CHNLIB_String; -struct CHNLIB_STRING; //----文字列の初期化 CHNLIB_String *CHNLIB_String_Initialize(const char str[]); CHNLIB_String *CHNLIB_String_InitializeWithFormat(const char format[], ...); @@ -159,12 +166,13 @@ int CHNLIB_String_Search_UIPArrayStringLocation(const CHNLIB_String *s, int s_st int CHNLIB_UIPArray_GetSeparatedStringByUIPArray(CHNLIB_UIPArray **separated, const CHNLIB_UIPArray *list, const CHNLIB_String *s); int CHNLIB_UIPArray_GetSeparatedUTF8Character(CHNLIB_UIPArray **separated, const CHNLIB_String *s); CHNLIB_String *CHNLIB_ReadLine(FILE *fp); +CHNLIB_UUID *CHNLIB_GenerateUUIDVersion4WithRandomGeneratorMT(CHNLIB_RandomGeneratorMT *mt); //@chnlib03.c void CHNLIB_Environment_SetCurrentWorkingDirectory(const char apppath[]); const char *CHNLIB_Environment_GetFilenameFromPath(const char path[]); -//@chnlib05.c +//@chnlib04.c int CHNLIB_UTF8_GetCharacterType(char c); int CHNLIB_UTF8_GetStringLengthByCharacter(const char s[]); uint CHNLIB_UTF8_GetNextUnicodeOfCharacter(const char s[], const char **next); @@ -173,4 +181,19 @@ int CHNLIB_UTF8_CompareString_LeftHand(const char s[], const char search[]); int CHNLIB_UTF8_GetByteSizeFromLengthByCharacter(const char s[], int start, int end); uint CHNLIB_UTF8_GetCountOfContain(const char s[], const char search[]); +//@chnlib05.c +CHNLIB_RandomGeneratorMT *CHNLIB_RandomGeneratorMT_Initialise(void); +void CHNLIB_RandomGeneratorMT_Free(CHNLIB_RandomGeneratorMT *env); +void CHNLIB_RandomGeneratorMT_SetSeed(CHNLIB_RandomGeneratorMT *env, unsigned long s); +void CHNLIB_RandomGeneratorMT_SetSeedFromArray(CHNLIB_RandomGeneratorMT *env, unsigned long init_key[], int key_length); +uint CHNLIB_RandomGeneratorMT_GetRandomUnsignedInteger32(CHNLIB_RandomGeneratorMT *env); + +//@chnlib06.c +CHNLIB_UUID *CHNLIB_UUID_Initialise(void); +void CHNLIB_UUID_Free(CHNLIB_UUID *uuid); +void CHNLIB_UUID_Print(const CHNLIB_UUID *uuid); +int CHNLIB_UUID_IsEqualToUUID(const CHNLIB_UUID *uuid1, const CHNLIB_UUID *uuid2); +void CHNLIB_UUID_SetValueAsUUIDVersion4CompatibleWithRFC4122(CHNLIB_UUID *uuid, uint random1, uint random2, uint random3, uint random4); +void CHNLIB_UUID_SetNullUUID(CHNLIB_UUID *uuid); +CHNLIB_UUID *CHNLIB_UUID_Copy(const CHNLIB_UUID *uuidBase); #endif diff --git a/CHNOSProject/chn/chnlib02.c b/CHNOSProject/chn/chnlib02.c index ba92bbd..39d5d22 100644 --- a/CHNOSProject/chn/chnlib02.c +++ b/CHNOSProject/chn/chnlib02.c @@ -139,4 +139,19 @@ CHNLIB_String *CHNLIB_ReadLine(FILE *fp) return CHNLIB_String_Initialize(s); } +CHNLIB_UUID *CHNLIB_GenerateUUIDVersion4WithRandomGeneratorMT(CHNLIB_RandomGeneratorMT *mt) +{ + //mtが有効でない場合、NullUUIDを返す。 + CHNLIB_UUID *uuid; + + uuid = CHNLIB_UUID_Initialise(); + + if(CHNLIB_StructureHeader_GetTypeID(mt) != CHNLIB_STRUCT_ID_RandomGeneratorMT){ + return uuid; + } + + CHNLIB_UUID_SetValueAsUUIDVersion4CompatibleWithRFC4122(uuid, CHNLIB_RandomGeneratorMT_GetRandomUnsignedInteger32(mt), CHNLIB_RandomGeneratorMT_GetRandomUnsignedInteger32(mt), CHNLIB_RandomGeneratorMT_GetRandomUnsignedInteger32(mt), CHNLIB_RandomGeneratorMT_GetRandomUnsignedInteger32(mt)); + + return uuid; +} diff --git a/CHNOSProject/chn/chnlib05.c b/CHNOSProject/chn/chnlib05.c new file mode 100644 index 0000000..9decc40 --- /dev/null +++ b/CHNOSProject/chn/chnlib05.c @@ -0,0 +1,289 @@ +// +// chnlib05.c +// AI003 +// +// Created by 西田 耀 on 13/06/30. +// Copyright (c) 2013年 Hikaru Nishida. All rights reserved. +// A C-program for MT19937, modified by hikarupsp for CHNLIB. + +// MT19937を利用した乱数生成器 + +/* + A C-program for MT19937, with initialization improved 2002/1/26. + Coded by Takuji Nishimura and Makoto Matsumoto. + + Before using, initialize the state by using init_genrand(seed) + or init_by_array(init_key, key_length). + + Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. The names of its contributors may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + + Any feedback is very welcome. + http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html + email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) +*/ +/* + Modified by hikarupsp on 2013/06/30 for CHNLIB. +*/ + +// +//Include headers +// +#include +#include "chnlib.h" + +// +//Define values +// +/* Period parameters */ +#define N 624 +#define M 397 +#define MATRIX_A 0x9908b0dfUL /* constant vector a */ +#define UPPER_MASK 0x80000000UL /* most significant w-r bits */ +#define LOWER_MASK 0x7fffffffUL /* least significant r bits */ + +// +//Declare internal functions +// +CHNLIB_RandomGeneratorMT *CHNLIB_RandomGeneratorMT_Internal_Allocate(void); +void CHNLIB_RandomGeneratorMT_Internal_Destruct(void **structure); +uint CHNLIB_RandomGeneratorMT_Internal_GetHash(const void *structure); + +// +//Define types +// +struct CHNLIB_RANDOM_GENERATOR_MT { + //MT19937を利用した乱数生成器 + CHNLIB_StructureHeader header; + unsigned long mt[N]; /* the array for the state vector */ + int mti; /* mti==N+1 means mt[N] is not initialized */ +}; + +// +//Functions +// + +CHNLIB_RandomGeneratorMT *CHNLIB_RandomGeneratorMT_Initialise(void) +{ + CHNLIB_RandomGeneratorMT *env; + + env = CHNLIB_RandomGeneratorMT_Internal_Allocate(); + env->header.destructor = &CHNLIB_RandomGeneratorMT_Internal_Destruct; + env->header.getHash = &CHNLIB_RandomGeneratorMT_Internal_GetHash; + env->mti = N + 1; /* mti==N+1 means mt[N] is not initialized */ + return env; +} + +void CHNLIB_RandomGeneratorMT_Free(CHNLIB_RandomGeneratorMT *env) +{ + //envを解放する。 + if(CHNLIB_StructureHeader_GetTypeID(env) != CHNLIB_STRUCT_ID_RandomGeneratorMT){ + return; + } + + env->header.typeid = CHNLIB_STRUCT_ID_Null; + env->header.signature = 0; + CHNLIB_System_FreeMemory(env, CHNLIB_DEBUG_ARGUMENTS); + + return; +} + + +/* initializes mt[N] with a seed */ +void CHNLIB_RandomGeneratorMT_SetSeed(CHNLIB_RandomGeneratorMT *env, unsigned long s) +{ + if(CHNLIB_StructureHeader_GetTypeID(env) != CHNLIB_STRUCT_ID_RandomGeneratorMT){ + return; + } + + env->mt[0]= s & 0xffffffffUL; + for (env->mti=1; env->mtimti++) { + env->mt[env->mti] = + (1812433253UL * (env->mt[env->mti-1] ^ (env->mt[env->mti-1] >> 30)) + env->mti); + /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ + /* In the previous versions, MSBs of the seed affect */ + /* only MSBs of the array mt[]. */ + /* 2002/01/09 modified by Makoto Matsumoto */ + env->mt[env->mti] &= 0xffffffffUL; + /* for >32 bit machines */ + } +} + +/* initialize by an array with array-length */ +/* init_key is the array for initializing keys */ +/* key_length is its length */ +/* slight change for C++, 2004/2/26 */ +void CHNLIB_RandomGeneratorMT_SetSeedFromArray(CHNLIB_RandomGeneratorMT *env, unsigned long init_key[], int key_length) +{ + int i, j, k; + + if(CHNLIB_StructureHeader_GetTypeID(env) != CHNLIB_STRUCT_ID_RandomGeneratorMT){ + return; + } + + CHNLIB_RandomGeneratorMT_SetSeed(env, 19650218UL); + i=1; j=0; + k = (N>key_length ? N : key_length); + for (; k; k--) { + env->mt[i] = (env->mt[i] ^ ((env->mt[i-1] ^ (env->mt[i-1] >> 30)) * 1664525UL)) + + init_key[j] + j; /* non linear */ + env->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ + i++; j++; + if (i>=N) { env->mt[0] = env->mt[N-1]; i=1; } + if (j>=key_length) j=0; + } + for (k=N-1; k; k--) { + env->mt[i] = (env->mt[i] ^ ((env->mt[i-1] ^ (env->mt[i-1] >> 30)) * 1566083941UL)) + - i; /* non linear */ + env->mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ + i++; + if (i>=N) { env->mt[0] = env->mt[N-1]; i=1; } + } + + env->mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ +} + +/* generates a random number on [0,0xffffffff]-interval */ +uint CHNLIB_RandomGeneratorMT_GetRandomUnsignedInteger32(CHNLIB_RandomGeneratorMT *env) +{ + unsigned long y; + static unsigned long mag01[2]={0x0UL, MATRIX_A}; + /* mag01[x] = x * MATRIX_A for x=0,1 */ + + if(CHNLIB_StructureHeader_GetTypeID(env) != CHNLIB_STRUCT_ID_RandomGeneratorMT){ + return 0; + } + + if (env->mti >= N) { /* generate N words at one time */ + int kk; + + if (env->mti == N+1) /* if init_genrand() has not been called, */ + CHNLIB_RandomGeneratorMT_SetSeed(env, 5489UL); /* a default initial seed is used */ + + for (kk=0;kkmt[kk]&UPPER_MASK)|(env->mt[kk+1]&LOWER_MASK); + env->mt[kk] = env->mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL]; + } + for (;kkmt[kk]&UPPER_MASK)|(env->mt[kk+1]&LOWER_MASK); + env->mt[kk] = env->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL]; + } + y = (env->mt[N-1]&UPPER_MASK)|(env->mt[0]&LOWER_MASK); + env->mt[N-1] = env->mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; + + env->mti = 0; + } + + y = env->mt[env->mti++]; + + /* Tempering */ + y ^= (y >> 11); + y ^= (y << 7) & 0x9d2c5680UL; + y ^= (y << 15) & 0xefc60000UL; + y ^= (y >> 18); + + return (uint)y; +} + +/* generates a random number on [0,1]-real-interval */ +//double genrand_real1(void) +//{ +// return genrand_int32()*(1.0/4294967295.0); +// /* divided by 2^32-1 */ +//} + +/* generates a random number on [0,1)-real-interval */ +//double genrand_real2(void) +//{ +// return genrand_int32()*(1.0/4294967296.0); +// /* divided by 2^32 */ +//} + +/* generates a random number on (0,1)-real-interval */ +//double genrand_real3(void) +//{ +// return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0); +// /* divided by 2^32 */ +//} + +/* generates a random number on [0,1) with 53-bit resolution*/ +//double genrand_res53(void) +//{ +// unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6; +// return(a*67108864.0+b)*(1.0/9007199254740992.0); +//} + +// +//Internal functions +// + +CHNLIB_RandomGeneratorMT *CHNLIB_RandomGeneratorMT_Internal_Allocate(void) +{ + CHNLIB_RandomGeneratorMT *tag; + + tag = (CHNLIB_RandomGeneratorMT *)CHNLIB_System_AllocateMemory_Strict(sizeof(CHNLIB_RandomGeneratorMT), CHNLIB_DEBUG_ARGUMENTS); + + CHNLIB_StructureHeader_Initialize(&tag->header, CHNLIB_STRUCT_ID_RandomGeneratorMT); + + return tag; +} + +void CHNLIB_RandomGeneratorMT_Internal_Destruct(void **structure) +{ + //デストラクタ(実際にRelease->freeされる時に呼ばれる) + if(structure == NULL){ + return; + } + +#ifdef DEBUG_MEMORY_REFERENCE_COUNT + CHNLIB_Debug("Release(with free)[%p].", CHNLIB_DEBUG_ARGUMENTS, *structure); +#endif + + CHNLIB_RandomGeneratorMT_Free(*structure); + + *structure = NULL; + + return; +} + +uint CHNLIB_RandomGeneratorMT_Internal_GetHash(const void *structure) +{ + //構造体の値が静的な意味を持たない場合は、構造体のアドレスの下位32bit分を返し、Hashの代わりとする。 + uint hash; + + hash = 0; + + if(CHNLIB_StructureHeader_GetTypeID(structure) == CHNLIB_STRUCT_ID_RandomGeneratorMT){ + hash = (uint)&structure; + } + + return hash; +} diff --git a/CHNOSProject/chn/chnlib06.c b/CHNOSProject/chn/chnlib06.c new file mode 100644 index 0000000..5177f2a --- /dev/null +++ b/CHNOSProject/chn/chnlib06.c @@ -0,0 +1,266 @@ +// +// chnlib06.c +// chn +// +// Created by 西田 耀 on 13/06/30. +// Copyright (c) 2013年 Hikaru Nishida. All rights reserved. +// + +// UUIDを格納する構造体 + +// +//Include headers +// +#include +#include "chnlib.h" + +// +//Define values +// + +// +//Declare internal functions +// +CHNLIB_UUID *CHNLIB_UUID_Internal_Allocate(void); +void CHNLIB_UUID_Internal_Destruct(void **structure); +uint CHNLIB_UUID_Internal_GetHash(const void *structure); + +// +//Define types +// +struct CHNLIB_UniversallyUniqueIDentifier { + //UUID based on RFC4122 + //各フィールドはビッグエンディアンとして扱われるべき。 + CHNLIB_StructureHeader header; + uchar time_low[4]; + uchar time_mid[2]; + uchar time_hi_and_version[2]; + uchar clock_seq_hi_and_reserved[1]; + uchar clock_seq_low[1]; + uchar node[6]; +}; + +// +//Functions +// + +CHNLIB_UUID *CHNLIB_UUID_Initialise(void) +{ + CHNLIB_UUID *uuid; + + uuid = CHNLIB_UUID_Internal_Allocate(); + uuid->header.destructor = &CHNLIB_UUID_Internal_Destruct; + uuid->header.getHash = &CHNLIB_UUID_Internal_GetHash; + + CHNLIB_UUID_SetNullUUID(uuid); + + return uuid; +} + +void CHNLIB_UUID_Free(CHNLIB_UUID *uuid) +{ + //uuidを解放する。 + if(CHNLIB_StructureHeader_GetTypeID(uuid) != CHNLIB_STRUCT_ID_UUID){ + return; + } + + CHNLIB_UUID_SetNullUUID(uuid); + + uuid->header.typeid = CHNLIB_STRUCT_ID_Null; + uuid->header.signature = 0; + CHNLIB_System_FreeMemory(uuid, CHNLIB_DEBUG_ARGUMENTS); + + return; +} + +void CHNLIB_UUID_Print(const CHNLIB_UUID *uuid) +{ + if(CHNLIB_StructureHeader_GetTypeID(uuid) != CHNLIB_STRUCT_ID_UUID){ + return; + } + + printf("%02x%02x%02x%02x-", uuid->time_low[0], uuid->time_low[1], uuid->time_low[2], uuid->time_low[3]); + printf("%02x%02x-", uuid->time_mid[0], uuid->time_mid[1]); + printf("%02x%02x-", uuid->time_hi_and_version[0], uuid->time_hi_and_version[1]); + printf("%02x%02x-", uuid->clock_seq_hi_and_reserved[0], uuid->clock_seq_low[0]); + printf("%02x%02x%02x%02x%02x%02x", uuid->node[0], uuid->node[1], uuid->node[2], uuid->node[3], uuid->node[4], uuid->node[5]); +} + +int CHNLIB_UUID_IsEqualToUUID(const CHNLIB_UUID *uuid1, const CHNLIB_UUID *uuid2) +{ + int i; + + if(CHNLIB_StructureHeader_GetTypeID(uuid1) != CHNLIB_STRUCT_ID_UUID){ + return False; + } + if(CHNLIB_StructureHeader_GetTypeID(uuid2) != CHNLIB_STRUCT_ID_UUID){ + return False; + } + + for(i = 0; i < 4; i++){ + if(uuid1->time_low[i] != uuid2->time_low[i]){ + return False; + } + } + for(i = 0; i < 2; i++){ + if(uuid1->time_mid[i] != uuid2->time_mid[i]){ + return False; + } + } + for(i = 0; i < 2; i++){ + if(uuid1->time_hi_and_version[i] != uuid2->time_hi_and_version[i]){ + return False; + } + } + if(uuid1->clock_seq_hi_and_reserved[i] != uuid2->clock_seq_hi_and_reserved[i]){ + return False; + } + if(uuid1->clock_seq_low[i] != uuid2->clock_seq_low[i]){ + return False; + } + for(i = 0; i < 6; i++){ + if(uuid1->node[i] != uuid2->node[i]){ + return False; + } + } + return True; +} + +void CHNLIB_UUID_SetValueAsUUIDVersion4CompatibleWithRFC4122(CHNLIB_UUID *uuid, uint random1, uint random2, uint random3, uint random4) +{ + int i; + + if(CHNLIB_StructureHeader_GetTypeID(uuid) != CHNLIB_STRUCT_ID_UUID){ + return; + } + + //random1 + for(i = 0; i < 4; i++){ + uuid->time_low[i] = ((random1 >> (i * 8)) & 0xff); + } + //random2 + for(i = 0; i < 2; i++){ + uuid->time_mid[i] = ((random2 >> (i * 8)) & 0xff); + } + for(i = 0; i < 2; i++){ + uuid->time_hi_and_version[i] = ((random2 >> ((i + 2) * 8)) & 0xff); + } + //random3 + uuid->clock_seq_hi_and_reserved[0] = ((random3 >> (1 * 8)) & 0xff); + uuid->clock_seq_low[0] = ((random3 >> (2 * 8)) & 0xff); + for(i = 0; i < 2; i++){ + uuid->node[i] = ((random3 >> ((i + 2) * 8)) & 0xff); + } + //random4 + for(i = 2; i < 6; i++){ + uuid->node[i] = ((random4 >> ((i - 2) * 8)) & 0xff); + } + //Fixed field + uuid->clock_seq_hi_and_reserved[0] &= 0x3f; + uuid->clock_seq_hi_and_reserved[0] |= 0x80; + + uuid->time_hi_and_version[0] &= 0x0f; + uuid->time_hi_and_version[0] |= 0x40; + + return; +} + +void CHNLIB_UUID_SetNullUUID(CHNLIB_UUID *uuid) +{ + int i; + + if(CHNLIB_StructureHeader_GetTypeID(uuid) != CHNLIB_STRUCT_ID_UUID){ + return; + } + + for(i = 0; i < 4; i++){ + uuid->time_low[i] = 0x00; + } + for(i = 0; i < 2; i++){ + uuid->time_mid[i] = 0x00; + } + for(i = 0; i < 2; i++){ + uuid->time_hi_and_version[i] = 0x00; + } + uuid->clock_seq_hi_and_reserved[0] = 0x00; + uuid->clock_seq_low[0] = 0x00; + for(i = 0; i < 6; i++){ + uuid->node[i] = 0x00; + } + return; +} + +CHNLIB_UUID *CHNLIB_UUID_Copy(const CHNLIB_UUID *uuidBase) +{ + CHNLIB_UUID *uuidCopy; + + uuidCopy = CHNLIB_UUID_Initialise(); + + int i; + + if(CHNLIB_StructureHeader_GetTypeID(uuidBase) == CHNLIB_STRUCT_ID_UUID){ + for(i = 0; i < 4; i++){ + uuidCopy->time_low[i] = uuidBase->time_low[i]; + } + for(i = 0; i < 2; i++){ + uuidCopy->time_mid[i] = uuidBase->time_mid[i]; + } + for(i = 0; i < 2; i++){ + uuidCopy->time_hi_and_version[i] = uuidBase->time_hi_and_version[i]; + } + uuidCopy->clock_seq_hi_and_reserved[0] = uuidBase->clock_seq_hi_and_reserved[0]; + uuidCopy->clock_seq_low[0] = uuidBase->clock_seq_low[0]; + for(i = 0; i < 6; i++){ + uuidCopy->node[i] = uuidBase->node[i]; + } + } + return uuidCopy; + +} + +// +//Internal functions +// +CHNLIB_UUID *CHNLIB_UUID_Internal_Allocate(void) +{ + CHNLIB_UUID *tag; + + tag = (CHNLIB_UUID *)CHNLIB_System_AllocateMemory_Strict(sizeof(CHNLIB_UUID), CHNLIB_DEBUG_ARGUMENTS); + + CHNLIB_StructureHeader_Initialize(&tag->header, CHNLIB_STRUCT_ID_UUID); + + return tag; +} + +void CHNLIB_UUID_Internal_Destruct(void **structure) +{ + //デストラクタ(実際にRelease->freeされる時に呼ばれる) + if(structure == NULL){ + return; + } + +#ifdef DEBUG_MEMORY_REFERENCE_COUNT + CHNLIB_Debug("Release(with free)[%p].", CHNLIB_DEBUG_ARGUMENTS, *structure); +#endif + + CHNLIB_UUID_Free(*structure); + + *structure = NULL; + + return; +} + +uint CHNLIB_UUID_Internal_GetHash(const void *structure) +{ + uint hash; + CHNLIB_UUID *uuid; + + hash = 0; + + if(CHNLIB_StructureHeader_GetTypeID(structure) == CHNLIB_STRUCT_ID_UUID){ + uuid = (CHNLIB_UUID *)structure; + hash = ((uuid->time_low[3] << 24) | (uuid->time_mid[1] << 16) | (uuid->clock_seq_low[0] << 8) | (uuid->node[5])); + } + + return hash; +} diff --git a/CHNOSProject/libtest/libtest.xcodeproj/project.pbxproj b/CHNOSProject/libtest/libtest.xcodeproj/project.pbxproj index 5eb0138..55c7904 100644 --- a/CHNOSProject/libtest/libtest.xcodeproj/project.pbxproj +++ b/CHNOSProject/libtest/libtest.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 174CE46C177FE6A8004FE674 /* libelchnos.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 174CE46B177FE6A8004FE674 /* libelchnos.a */; }; 17EE90A216DB67DB005238B2 /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = 17EE90A116DB67DB005238B2 /* main.c */; }; 17EE90A416DB67DB005238B2 /* libtest.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = 17EE90A316DB67DB005238B2 /* libtest.1 */; }; 17EE90B616DB6898005238B2 /* libchn.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 17EE90B516DB6898005238B2 /* libchn.a */; }; @@ -36,6 +37,8 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 174CE46B177FE6A8004FE674 /* libelchnos.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libelchnos.a; path = ../DerivedData/CHNOSProject/Build/Products/Debug/libelchnos.a; sourceTree = ""; }; + 174CE46E177FE6F5004FE674 /* elctest.elc */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; fileEncoding = 4; path = elctest.elc; sourceTree = ""; }; 17EE909E16DB67DB005238B2 /* libtest */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = libtest; sourceTree = BUILT_PRODUCTS_DIR; }; 17EE90A116DB67DB005238B2 /* main.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = ""; }; 17EE90A316DB67DB005238B2 /* libtest.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = libtest.1; sourceTree = ""; }; @@ -48,6 +51,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 174CE46C177FE6A8004FE674 /* libelchnos.a in Frameworks */, 17EE90B616DB6898005238B2 /* libchn.a in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; @@ -66,6 +70,7 @@ 17EE909516DB67DB005238B2 = { isa = PBXGroup; children = ( + 174CE46B177FE6A8004FE674 /* libelchnos.a */, 17EE90B516DB6898005238B2 /* libchn.a */, 17EE90B416DB6890005238B2 /* chn.xcodeproj */, 17EE90A016DB67DB005238B2 /* libtest */, @@ -86,6 +91,7 @@ children = ( 17EE90A116DB67DB005238B2 /* main.c */, 17EE90A316DB67DB005238B2 /* libtest.1 */, + 174CE46E177FE6F5004FE674 /* elctest.elc */, ); path = libtest; sourceTree = ""; diff --git a/CHNOSProject/libtest/libtest.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist b/CHNOSProject/libtest/libtest.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist index 086924b..6e1663e 100644 --- a/CHNOSProject/libtest/libtest.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/CHNOSProject/libtest/libtest.xcodeproj/xcuserdata/hikaru.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,7 +7,7 @@ libtest.xcscheme orderHint - 4 + 6 SuppressBuildableAutocreation diff --git a/CHNOSProject/libtest/libtest/main.c b/CHNOSProject/libtest/libtest/main.c index 95079f4..76e2119 100644 --- a/CHNOSProject/libtest/libtest/main.c +++ b/CHNOSProject/libtest/libtest/main.c @@ -8,32 +8,49 @@ #include #include "chnlib.h" +#include int main(int argc, const char * argv[]) { - CHNLIB_String *str1, *str2, *str; - CHNLIB_UIPArray *array; + int i; + CHNLIB_RandomGeneratorMT *mt; + CHNLIB_UUID *uuid, *uuid2; - str = CHNLIB_String_Initialize("あいうえおかきs"); - printf("0x%X:%s\n", CHNLIB_StructureHeader_GetHash(str), CHNLIB_String_GetReferencePointerOfCString(str)); + mt = CHNLIB_RandomGeneratorMT_Initialise(); + CHNLIB_RandomGeneratorMT_SetSeed(mt, (int)&mt ^ time(NULL)); - str1 = CHNLIB_String_Initialize("あい"); - printf("0x%X:%s\n", CHNLIB_StructureHeader_GetHash(str1), CHNLIB_String_GetReferencePointerOfCString(str1)); - str2 = CHNLIB_String_Initialize("かき"); - printf("0x%X:%s\n", CHNLIB_StructureHeader_GetHash(str2), CHNLIB_String_GetReferencePointerOfCString(str2)); - - str = CHNLIB_String_Concatenate(str1, str2); - printf("0x%X:%s\n", CHNLIB_StructureHeader_GetHash(str), CHNLIB_String_GetReferencePointerOfCString(str)); + for(i = 0; i < 1024; i++){ + uuid = CHNLIB_GenerateUUIDVersion4WithRandomGeneratorMT(mt); + CHNLIB_UUID_Print(uuid); + printf(":0x%08X\n", CHNLIB_StructureHeader_GetHash(uuid)); + release(uuid); + } + + uuid = CHNLIB_GenerateUUIDVersion4WithRandomGeneratorMT(mt); + putc('\n', stdout); + CHNLIB_UUID_Print(uuid); + + uuid2 = CHNLIB_UUID_Copy(uuid); + putc('\n', stdout); + CHNLIB_UUID_Print(uuid2); - str = CHNLIB_String_Concatenate(str, str); - printf("0x%X:%s\n", CHNLIB_StructureHeader_GetHash(str), CHNLIB_String_GetReferencePointerOfCString(str)); + if(CHNLIB_UUID_IsEqualToUUID(uuid, uuid2)){ + printf("\neq\n"); + } else{ + printf("\nNOTeq\n"); + } + release(uuid2); - array = CHNLIB_UIPArray_Initialize(); - CHNLIB_UIPArray_AppendLast(&array, 0, str); - printf("0x%X", CHNLIB_StructureHeader_GetHash(array)); + uuid2 = CHNLIB_GenerateUUIDVersion4WithRandomGeneratorMT(mt); + putc('\n', stdout); + CHNLIB_UUID_Print(uuid2); - CHNLIB_UIPArray_AppendLast(&array, 0, str); - printf("0x%X", CHNLIB_StructureHeader_GetHash(array)); + if(CHNLIB_UUID_IsEqualToUUID(uuid, uuid2)){ + printf("\neq\n"); + } else{ + printf("\nNOTeq\n"); + } + release(uuid2); return 0; } -- 2.11.0