2
0

dmap_parser.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. #include "dmap_parser.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #define DMAP_STRINGIFY_(x) #x
  7. #define DMAP_STRINGIFY(x) DMAP_STRINGIFY_(x)
  8. typedef enum {
  9. DMAP_UNKNOWN,
  10. DMAP_UINT,
  11. DMAP_INT,
  12. DMAP_STR,
  13. DMAP_DATA,
  14. DMAP_DATE,
  15. DMAP_VERS,
  16. DMAP_DICT,
  17. DMAP_ITEM
  18. } DMAP_TYPE;
  19. typedef struct {
  20. /**
  21. * The four-character code used in the encoded message.
  22. */
  23. const char *code;
  24. /**
  25. * The type of data associated with the content code.
  26. */
  27. DMAP_TYPE type;
  28. /**
  29. * For listings, the type of their listing item children.
  30. *
  31. * Listing items (mlit) can be of any type, and as with other content codes
  32. * their type information is not encoded in the message. Parsers must
  33. * determine the type of the listing items based on their parent context.
  34. */
  35. DMAP_TYPE list_item_type;
  36. /**
  37. * A human-readable name for the content code.
  38. */
  39. const char *name;
  40. } dmap_field;
  41. static const dmap_field dmap_fields[] = {
  42. #ifdef DMAP_FULL
  43. { "abal", DMAP_DICT, DMAP_STR, "daap.browsealbumlisting" },
  44. { "abar", DMAP_DICT, DMAP_STR, "daap.browseartistlisting" },
  45. { "abcp", DMAP_DICT, DMAP_STR, "daap.browsecomposerlisting" },
  46. { "abgn", DMAP_DICT, DMAP_STR, "daap.browsegenrelisting" },
  47. { "abpl", DMAP_UINT, 0, "daap.baseplaylist" },
  48. { "abro", DMAP_DICT, 0, "daap.databasebrowse" },
  49. { "adbs", DMAP_DICT, 0, "daap.databasesongs" },
  50. { "aeAD", DMAP_DICT, 0, "com.apple.itunes.adam-ids-array" },
  51. { "aeAI", DMAP_UINT, 0, "com.apple.itunes.itms-artistid" },
  52. { "aeCD", DMAP_DATA, 0, "com.apple.itunes.flat-chapter-data" },
  53. { "aeCF", DMAP_UINT, 0, "com.apple.itunes.cloud-flavor-id" },
  54. { "aeCI", DMAP_UINT, 0, "com.apple.itunes.itms-composerid" },
  55. { "aeCK", DMAP_UINT, 0, "com.apple.itunes.cloud-library-kind" },
  56. { "aeCM", DMAP_UINT, 0, "com.apple.itunes.cloud-match-type" },
  57. { "aeCR", DMAP_STR, 0, "com.apple.itunes.content-rating" } ,
  58. { "aeCS", DMAP_UINT, 0, "com.apple.itunes.artworkchecksum" },
  59. { "aeCU", DMAP_UINT, 0, "com.apple.itunes.cloud-user-id" },
  60. { "aeCd", DMAP_UINT, 0, "com.apple.itunes.cloud-id" },
  61. { "aeDE", DMAP_STR, 0, "com.apple.itunes.longest-content-description" },
  62. { "aeDL", DMAP_UINT, 0, "com.apple.itunes.drm-downloader-user-id" },
  63. { "aeDP", DMAP_UINT, 0, "com.apple.itunes.drm-platform-id" },
  64. { "aeDR", DMAP_UINT, 0, "com.apple.itunes.drm-user-id" },
  65. { "aeDV", DMAP_UINT, 0, "com.apple.itunes.drm-versions" },
  66. { "aeEN", DMAP_STR, 0, "com.apple.itunes.episode-num-str" },
  67. { "aeES", DMAP_UINT, 0, "com.apple.itunes.episode-sort" },
  68. { "aeFA", DMAP_UINT, 0, "com.apple.itunes.drm-family-id" },
  69. { "aeGD", DMAP_UINT, 0, "com.apple.itunes.gapless-enc-dr" } ,
  70. { "aeGE", DMAP_UINT, 0, "com.apple.itunes.gapless-enc-del" },
  71. { "aeGH", DMAP_UINT, 0, "com.apple.itunes.gapless-heur" },
  72. { "aeGI", DMAP_UINT, 0, "com.apple.itunes.itms-genreid" },
  73. { "aeGR", DMAP_UINT, 0, "com.apple.itunes.gapless-resy" },
  74. { "aeGU", DMAP_UINT, 0, "com.apple.itunes.gapless-dur" },
  75. { "aeGs", DMAP_UINT, 0, "com.apple.itunes.can-be-genius-seed" },
  76. { "aeHC", DMAP_UINT, 0, "com.apple.itunes.has-chapter-data" },
  77. { "aeHD", DMAP_UINT, 0, "com.apple.itunes.is-hd-video" },
  78. { "aeHV", DMAP_UINT, 0, "com.apple.itunes.has-video" },
  79. { "aeK1", DMAP_UINT, 0, "com.apple.itunes.drm-key1-id" },
  80. { "aeK2", DMAP_UINT, 0, "com.apple.itunes.drm-key2-id" },
  81. { "aeMC", DMAP_UINT, 0, "com.apple.itunes.playlist-contains-media-type-count" },
  82. { "aeMK", DMAP_UINT, 0, "com.apple.itunes.mediakind" },
  83. { "aeMX", DMAP_STR, 0, "com.apple.itunes.movie-info-xml" },
  84. { "aeMk", DMAP_UINT, 0, "com.apple.itunes.extended-media-kind" },
  85. { "aeND", DMAP_UINT, 0, "com.apple.itunes.non-drm-user-id" },
  86. { "aeNN", DMAP_STR, 0, "com.apple.itunes.network-name" },
  87. { "aeNV", DMAP_UINT, 0, "com.apple.itunes.norm-volume" },
  88. { "aePC", DMAP_UINT, 0, "com.apple.itunes.is-podcast" },
  89. { "aePI", DMAP_UINT, 0, "com.apple.itunes.itms-playlistid" },
  90. { "aePP", DMAP_UINT, 0, "com.apple.itunes.is-podcast-playlist" },
  91. { "aePS", DMAP_UINT, 0, "com.apple.itunes.special-playlist" },
  92. { "aeRD", DMAP_UINT, 0, "com.apple.itunes.rental-duration" },
  93. { "aeRP", DMAP_UINT, 0, "com.apple.itunes.rental-pb-start" },
  94. { "aeRS", DMAP_UINT, 0, "com.apple.itunes.rental-start" },
  95. { "aeRU", DMAP_UINT, 0, "com.apple.itunes.rental-pb-duration" },
  96. { "aeRf", DMAP_UINT, 0, "com.apple.itunes.is-featured" },
  97. { "aeSE", DMAP_UINT, 0, "com.apple.itunes.store-pers-id" },
  98. { "aeSF", DMAP_UINT, 0, "com.apple.itunes.itms-storefrontid" },
  99. { "aeSG", DMAP_UINT, 0, "com.apple.itunes.saved-genius" },
  100. { "aeSI", DMAP_UINT, 0, "com.apple.itunes.itms-songid" },
  101. { "aeSN", DMAP_STR, 0, "com.apple.itunes.series-name" },
  102. { "aeSP", DMAP_UINT, 0, "com.apple.itunes.smart-playlist" },
  103. { "aeSU", DMAP_UINT, 0, "com.apple.itunes.season-num" },
  104. { "aeSV", DMAP_VERS, 0, "com.apple.itunes.music-sharing-version" },
  105. { "aeXD", DMAP_STR, 0, "com.apple.itunes.xid" },
  106. { "aecp", DMAP_STR, 0, "com.apple.itunes.collection-description" },
  107. { "aels", DMAP_UINT, 0, "com.apple.itunes.liked-state" },
  108. { "aemi", DMAP_DICT, 0, "com.apple.itunes.media-kind-listing-item" },
  109. { "aeml", DMAP_DICT, 0, "com.apple.itunes.media-kind-listing" },
  110. { "agac", DMAP_UINT, 0, "daap.groupalbumcount" },
  111. { "agma", DMAP_UINT, 0, "daap.groupmatchedqueryalbumcount" },
  112. { "agmi", DMAP_UINT, 0, "daap.groupmatchedqueryitemcount" },
  113. { "agrp", DMAP_STR, 0, "daap.songgrouping" },
  114. { "ajAE", DMAP_UINT, 0, "com.apple.itunes.store.ams-episode-type" },
  115. { "ajAS", DMAP_UINT, 0, "com.apple.itunes.store.ams-episode-sort-order" },
  116. { "ajAT", DMAP_UINT, 0, "com.apple.itunes.store.ams-show-type" },
  117. { "ajAV", DMAP_UINT, 0, "com.apple.itunes.store.is-ams-video" },
  118. { "ajal", DMAP_UINT, 0, "com.apple.itunes.store.album-liked-state" },
  119. { "ajcA", DMAP_UINT, 0, "com.apple.itunes.store.show-composer-as-artist" },
  120. { "ajca", DMAP_UINT, 0, "com.apple.itunes.store.show-composer-as-artist" },
  121. { "ajuw", DMAP_UINT, 0, "com.apple.itunes.store.use-work-name-as-display-name" },
  122. { "amvc", DMAP_UINT, 0, "daap.songmovementcount" },
  123. { "amvm", DMAP_STR, 0, "daap.songmovementname" },
  124. { "amvn", DMAP_UINT, 0, "daap.songmovementnumber" },
  125. { "aply", DMAP_DICT, 0, "daap.databaseplaylists" },
  126. { "aprm", DMAP_UINT, 0, "daap.playlistrepeatmode" },
  127. { "apro", DMAP_VERS, 0, "daap.protocolversion" },
  128. { "apsm", DMAP_UINT, 0, "daap.playlistshufflemode" },
  129. { "apso", DMAP_DICT, 0, "daap.playlistsongs" },
  130. { "arif", DMAP_DICT, 0, "daap.resolveinfo" },
  131. { "arsv", DMAP_DICT, 0, "daap.resolve" },
  132. { "asaa", DMAP_STR, 0, "daap.songalbumartist" },
  133. { "asac", DMAP_UINT, 0, "daap.songartworkcount" },
  134. { "asai", DMAP_UINT, 0, "daap.songalbumid" },
  135. #endif
  136. { "asal", DMAP_STR, 0, "daap.songalbum" },
  137. { "asar", DMAP_STR, 0, "daap.songartist" },
  138. #ifdef DMAP_FULL
  139. { "asas", DMAP_UINT, 0, "daap.songalbumuserratingstatus" },
  140. { "asbk", DMAP_UINT, 0, "daap.bookmarkable" },
  141. { "asbo", DMAP_UINT, 0, "daap.songbookmark" },
  142. { "asbr", DMAP_UINT, 0, "daap.songbitrate" },
  143. { "asbt", DMAP_UINT, 0, "daap.songbeatsperminute" },
  144. { "ascd", DMAP_UINT, 0, "daap.songcodectype" },
  145. { "ascm", DMAP_STR, 0, "daap.songcomment" },
  146. { "ascn", DMAP_STR, 0, "daap.songcontentdescription" },
  147. { "asco", DMAP_UINT, 0, "daap.songcompilation" },
  148. { "ascp", DMAP_STR, 0, "daap.songcomposer" },
  149. { "ascr", DMAP_UINT, 0, "daap.songcontentrating" },
  150. { "ascs", DMAP_UINT, 0, "daap.songcodecsubtype" },
  151. { "asct", DMAP_STR, 0, "daap.songcategory" },
  152. { "asda", DMAP_DATE, 0, "daap.songdateadded" },
  153. { "asdb", DMAP_UINT, 0, "daap.songdisabled" },
  154. { "asdc", DMAP_UINT, 0, "daap.songdisccount" },
  155. { "asdk", DMAP_UINT, 0, "daap.songdatakind" },
  156. { "asdm", DMAP_DATE, 0, "daap.songdatemodified" },
  157. { "asdn", DMAP_UINT, 0, "daap.songdiscnumber" },
  158. { "asdp", DMAP_DATE, 0, "daap.songdatepurchased" },
  159. { "asdr", DMAP_DATE, 0, "daap.songdatereleased" },
  160. { "asdt", DMAP_STR, 0, "daap.songdescription" },
  161. { "ased", DMAP_UINT, 0, "daap.songextradata" },
  162. { "aseq", DMAP_STR, 0, "daap.songeqpreset" },
  163. { "ases", DMAP_UINT, 0, "daap.songexcludefromshuffle" },
  164. { "asfm", DMAP_STR, 0, "daap.songformat" },
  165. { "asgn", DMAP_STR, 0, "daap.songgenre" },
  166. { "asgp", DMAP_UINT, 0, "daap.songgapless" },
  167. { "asgr", DMAP_UINT, 0, "daap.supportsgroups" },
  168. { "ashp", DMAP_UINT, 0, "daap.songhasbeenplayed" },
  169. { "askd", DMAP_DATE, 0, "daap.songlastskipdate" },
  170. { "askp", DMAP_UINT, 0, "daap.songuserskipcount" },
  171. { "asky", DMAP_STR, 0, "daap.songkeywords" },
  172. { "aslc", DMAP_STR, 0, "daap.songlongcontentdescription" },
  173. { "aslr", DMAP_UINT, 0, "daap.songalbumuserrating" },
  174. { "asls", DMAP_UINT, 0, "daap.songlongsize" },
  175. { "aspc", DMAP_UINT, 0, "daap.songuserplaycount" },
  176. { "aspl", DMAP_DATE, 0, "daap.songdateplayed" },
  177. { "aspu", DMAP_STR, 0, "daap.songpodcasturl" },
  178. { "asri", DMAP_UINT, 0, "daap.songartistid" },
  179. { "asrs", DMAP_UINT, 0, "daap.songuserratingstatus" },
  180. { "asrv", DMAP_INT, 0, "daap.songrelativevolume" },
  181. { "assa", DMAP_STR, 0, "daap.sortartist" },
  182. { "assc", DMAP_STR, 0, "daap.sortcomposer" },
  183. { "assl", DMAP_STR, 0, "daap.sortalbumartist" },
  184. { "assn", DMAP_STR, 0, "daap.sortname" },
  185. { "assp", DMAP_UINT, 0, "daap.songstoptime" },
  186. { "assr", DMAP_UINT, 0, "daap.songsamplerate" },
  187. { "asss", DMAP_STR, 0, "daap.sortseriesname" },
  188. { "asst", DMAP_UINT, 0, "daap.songstarttime" },
  189. { "assu", DMAP_STR, 0, "daap.sortalbum" },
  190. { "assz", DMAP_UINT, 0, "daap.songsize" },
  191. { "astc", DMAP_UINT, 0, "daap.songtrackcount" },
  192. { "astm", DMAP_UINT, 0, "daap.songtime" },
  193. { "astn", DMAP_UINT, 0, "daap.songtracknumber" },
  194. { "asul", DMAP_STR, 0, "daap.songdataurl" },
  195. { "asur", DMAP_UINT, 0, "daap.songuserrating" },
  196. { "asvc", DMAP_UINT, 0, "daap.songprimaryvideocodec" },
  197. { "asyr", DMAP_UINT, 0, "daap.songyear" },
  198. { "ated", DMAP_UINT, 0, "daap.supportsextradata" },
  199. { "avdb", DMAP_DICT, 0, "daap.serverdatabases" },
  200. { "awrk", DMAP_STR, 0, "daap.songwork" },
  201. { "caar", DMAP_UINT, 0, "dacp.availablerepeatstates" },
  202. { "caas", DMAP_UINT, 0, "dacp.availableshufflestates" },
  203. { "caci", DMAP_DICT, 0, "caci" },
  204. { "cafe", DMAP_UINT, 0, "dacp.fullscreenenabled" },
  205. { "cafs", DMAP_UINT, 0, "dacp.fullscreen" },
  206. { "caia", DMAP_UINT, 0, "dacp.isactive" },
  207. { "cana", DMAP_STR, 0, "dacp.nowplayingartist" },
  208. { "cang", DMAP_STR, 0, "dacp.nowplayinggenre" },
  209. { "canl", DMAP_STR, 0, "dacp.nowplayingalbum" },
  210. { "cann", DMAP_STR, 0, "dacp.nowplayingname" },
  211. { "canp", DMAP_UINT, 0, "dacp.nowplayingids" },
  212. { "cant", DMAP_UINT, 0, "dacp.nowplayingtime" },
  213. { "capr", DMAP_VERS, 0, "dacp.protocolversion" },
  214. { "caps", DMAP_UINT, 0, "dacp.playerstate" },
  215. { "carp", DMAP_UINT, 0, "dacp.repeatstate" },
  216. { "cash", DMAP_UINT, 0, "dacp.shufflestate" },
  217. { "casp", DMAP_DICT, 0, "dacp.speakers" },
  218. { "cast", DMAP_UINT, 0, "dacp.songtime" },
  219. { "cavc", DMAP_UINT, 0, "dacp.volumecontrollable" },
  220. { "cave", DMAP_UINT, 0, "dacp.visualizerenabled" },
  221. { "cavs", DMAP_UINT, 0, "dacp.visualizer" },
  222. { "ceJC", DMAP_UINT, 0, "com.apple.itunes.jukebox-client-vote" },
  223. { "ceJI", DMAP_UINT, 0, "com.apple.itunes.jukebox-current" },
  224. { "ceJS", DMAP_UINT, 0, "com.apple.itunes.jukebox-score" },
  225. { "ceJV", DMAP_UINT, 0, "com.apple.itunes.jukebox-vote" },
  226. { "ceQR", DMAP_DICT, 0, "com.apple.itunes.playqueue-contents-response" },
  227. { "ceQa", DMAP_STR, 0, "com.apple.itunes.playqueue-album" },
  228. { "ceQg", DMAP_STR, 0, "com.apple.itunes.playqueue-genre" },
  229. { "ceQn", DMAP_STR, 0, "com.apple.itunes.playqueue-name" },
  230. { "ceQr", DMAP_STR, 0, "com.apple.itunes.playqueue-artist" },
  231. { "cmgt", DMAP_DICT, 0, "dmcp.getpropertyresponse" },
  232. { "cmmk", DMAP_UINT, 0, "dmcp.mediakind" },
  233. { "cmpr", DMAP_VERS, 0, "dmcp.protocolversion" },
  234. { "cmsr", DMAP_UINT, 0, "dmcp.serverrevision" },
  235. { "cmst", DMAP_DICT, 0, "dmcp.playstatus" },
  236. { "cmvo", DMAP_UINT, 0, "dmcp.volume" },
  237. { "f\215ch", DMAP_UINT, 0, "dmap.haschildcontainers" },
  238. { "ipsa", DMAP_DICT, 0, "dpap.iphotoslideshowadvancedoptions" },
  239. { "ipsl", DMAP_DICT, 0, "dpap.iphotoslideshowoptions" },
  240. { "mbcl", DMAP_DICT, 0, "dmap.bag" },
  241. { "mccr", DMAP_DICT, 0, "dmap.contentcodesresponse" },
  242. { "mcna", DMAP_STR, 0, "dmap.contentcodesname" },
  243. { "mcnm", DMAP_UINT, 0, "dmap.contentcodesnumber" },
  244. { "mcon", DMAP_DICT, 0, "dmap.container" },
  245. { "mctc", DMAP_UINT, 0, "dmap.containercount" },
  246. { "mcti", DMAP_UINT, 0, "dmap.containeritemid" },
  247. { "mcty", DMAP_UINT, 0, "dmap.contentcodestype" },
  248. { "mdbk", DMAP_UINT, 0, "dmap.databasekind" },
  249. { "mdcl", DMAP_DICT, 0, "dmap.dictionary" },
  250. { "mdst", DMAP_UINT, 0, "dmap.downloadstatus" },
  251. { "meds", DMAP_UINT, 0, "dmap.editcommandssupported" },
  252. { "meia", DMAP_UINT, 0, "dmap.itemdateadded" },
  253. { "meip", DMAP_UINT, 0, "dmap.itemdateplayed" },
  254. { "mext", DMAP_UINT, 0, "dmap.objectextradata" },
  255. { "miid", DMAP_UINT, 0, "dmap.itemid" },
  256. { "mikd", DMAP_UINT, 0, "dmap.itemkind" },
  257. { "mimc", DMAP_UINT, 0, "dmap.itemcount" },
  258. #endif
  259. { "minm", DMAP_STR, 0, "dmap.itemname" },
  260. #ifdef DMAP_FULL
  261. { "mlcl", DMAP_DICT, DMAP_DICT, "dmap.listing" },
  262. { "mlid", DMAP_UINT, 0, "dmap.sessionid" },
  263. { "mlit", DMAP_ITEM, 0, "dmap.listingitem" },
  264. { "mlog", DMAP_DICT, 0, "dmap.loginresponse" },
  265. { "mpco", DMAP_UINT, 0, "dmap.parentcontainerid" },
  266. { "mper", DMAP_UINT, 0, "dmap.persistentid" },
  267. { "mpro", DMAP_VERS, 0, "dmap.protocolversion" },
  268. { "mrco", DMAP_UINT, 0, "dmap.returnedcount" },
  269. { "mrpr", DMAP_UINT, 0, "dmap.remotepersistentid" },
  270. { "msal", DMAP_UINT, 0, "dmap.supportsautologout" },
  271. { "msas", DMAP_UINT, 0, "dmap.authenticationschemes" },
  272. { "msau", DMAP_UINT, 0, "dmap.authenticationmethod" },
  273. { "msbr", DMAP_UINT, 0, "dmap.supportsbrowse" },
  274. { "msdc", DMAP_UINT, 0, "dmap.databasescount" },
  275. { "msex", DMAP_UINT, 0, "dmap.supportsextensions" },
  276. { "msix", DMAP_UINT, 0, "dmap.supportsindex" },
  277. { "mslr", DMAP_UINT, 0, "dmap.loginrequired" },
  278. { "msma", DMAP_UINT, 0, "dmap.machineaddress" },
  279. { "msml", DMAP_DICT, 0, "msml" },
  280. { "mspi", DMAP_UINT, 0, "dmap.supportspersistentids" },
  281. { "msqy", DMAP_UINT, 0, "dmap.supportsquery" },
  282. { "msrs", DMAP_UINT, 0, "dmap.supportsresolve" },
  283. { "msrv", DMAP_DICT, 0, "dmap.serverinforesponse" },
  284. { "mstc", DMAP_DATE, 0, "dmap.utctime" },
  285. { "mstm", DMAP_UINT, 0, "dmap.timeoutinterval" },
  286. { "msto", DMAP_INT, 0, "dmap.utcoffset" },
  287. { "msts", DMAP_STR, 0, "dmap.statusstring" },
  288. { "mstt", DMAP_UINT, 0, "dmap.status" },
  289. { "msup", DMAP_UINT, 0, "dmap.supportsupdate" },
  290. { "mtco", DMAP_UINT, 0, "dmap.specifiedtotalcount" },
  291. { "mudl", DMAP_DICT, 0, "dmap.deletedidlisting" },
  292. { "mupd", DMAP_DICT, 0, "dmap.updateresponse" },
  293. { "musr", DMAP_UINT, 0, "dmap.serverrevision" },
  294. { "muty", DMAP_UINT, 0, "dmap.updatetype" },
  295. { "pasp", DMAP_STR, 0, "dpap.aspectratio" },
  296. { "pcmt", DMAP_STR, 0, "dpap.imagecomments" },
  297. { "peak", DMAP_UINT, 0, "com.apple.itunes.photos.album-kind" },
  298. { "peed", DMAP_DATE, 0, "com.apple.itunes.photos.exposure-date" },
  299. { "pefc", DMAP_DICT, 0, "com.apple.itunes.photos.faces" },
  300. { "peki", DMAP_UINT, 0, "com.apple.itunes.photos.key-image-id" },
  301. { "pekm", DMAP_DICT, 0, "com.apple.itunes.photos.key-image" },
  302. { "pemd", DMAP_DATE, 0, "com.apple.itunes.photos.modification-date" },
  303. { "pfai", DMAP_DICT, 0, "dpap.failureids" },
  304. { "pfdt", DMAP_DICT, 0, "dpap.filedata" },
  305. { "pfmt", DMAP_STR, 0, "dpap.imageformat" },
  306. { "phgt", DMAP_UINT, 0, "dpap.imagepixelheight" },
  307. { "picd", DMAP_DATE, 0, "dpap.creationdate" },
  308. { "pifs", DMAP_UINT, 0, "dpap.imagefilesize" },
  309. { "pimf", DMAP_STR, 0, "dpap.imagefilename" },
  310. { "plsz", DMAP_UINT, 0, "dpap.imagelargefilesize" },
  311. { "ppro", DMAP_VERS, 0, "dpap.protocolversion" },
  312. { "prat", DMAP_UINT, 0, "dpap.imagerating" },
  313. { "pret", DMAP_DICT, 0, "dpap.retryids" },
  314. { "pwth", DMAP_UINT, 0, "dpap.imagepixelwidth" }
  315. #endif
  316. };
  317. static const size_t dmap_field_count = sizeof(dmap_fields) / sizeof(dmap_field);
  318. typedef int (*sort_func) (const void *, const void *);
  319. int dmap_version(void) {
  320. return DMAP_VERSION;
  321. }
  322. const char *dmap_version_string(void) {
  323. return DMAP_STRINGIFY(DMAP_VERSION_MAJOR) "."
  324. DMAP_STRINGIFY(DMAP_VERSION_MINOR) "."
  325. DMAP_STRINGIFY(DMAP_VERSION_PATCH);
  326. }
  327. static int dmap_field_sort(const dmap_field *a, const dmap_field *b) {
  328. return memcmp(a->code, b->code, 4);
  329. }
  330. static const dmap_field *dmap_field_from_code(const char *code) {
  331. dmap_field key;
  332. key.code = code;
  333. return bsearch(&key, dmap_fields, dmap_field_count, sizeof(dmap_field), (sort_func)dmap_field_sort);
  334. }
  335. const char *dmap_name_from_code(const char *code) {
  336. const dmap_field *field;
  337. if (!code)
  338. return NULL;
  339. field = dmap_field_from_code(code);
  340. return field ? field->name : NULL;
  341. }
  342. static uint16_t dmap_read_u16(const char *buf) {
  343. return (uint16_t)(((buf[0] & 0xff) << 8) | (buf[1] & 0xff));
  344. }
  345. static int16_t dmap_read_i16(const char *buf) {
  346. return (int16_t)dmap_read_u16(buf);
  347. }
  348. static uint32_t dmap_read_u32(const char *buf) {
  349. return ((uint32_t)(buf[0] & 0xff) << 24) |
  350. ((uint32_t)(buf[1] & 0xff) << 16) |
  351. ((uint32_t)(buf[2] & 0xff) << 8) |
  352. ((uint32_t)(buf[3] & 0xff));
  353. }
  354. static int32_t dmap_read_i32(const char *buf) {
  355. return (int32_t)dmap_read_u32(buf);
  356. }
  357. static uint64_t dmap_read_u64(const char *buf) {
  358. return ((uint64_t)(buf[0] & 0xff) << 56) |
  359. ((uint64_t)(buf[1] & 0xff) << 48) |
  360. ((uint64_t)(buf[2] & 0xff) << 40) |
  361. ((uint64_t)(buf[3] & 0xff) << 32) |
  362. ((uint64_t)(buf[4] & 0xff) << 24) |
  363. ((uint64_t)(buf[5] & 0xff) << 16) |
  364. ((uint64_t)(buf[6] & 0xff) << 8) |
  365. ((uint64_t)(buf[7] & 0xff));
  366. }
  367. static int64_t dmap_read_i64(const char *buf) {
  368. return (int64_t)dmap_read_u64(buf);
  369. }
  370. static int dmap_parse_internal(const dmap_settings *settings, const char *buf, size_t len, const dmap_field *parent) {
  371. const dmap_field *field;
  372. DMAP_TYPE field_type;
  373. size_t field_len;
  374. const char *field_name;
  375. const char *p = buf;
  376. const char *end = buf + len;
  377. char code[5] = {0};
  378. if (!settings || !buf)
  379. return -1;
  380. while (end - p >= 8) {
  381. memcpy(code, p, 4);
  382. field = dmap_field_from_code(code);
  383. p += 4;
  384. field_len = dmap_read_u32(p);
  385. p += 4;
  386. if (p + field_len > end)
  387. return -1;
  388. if (field) {
  389. field_type = field->type;
  390. field_name = field->name;
  391. if (field_type == DMAP_ITEM) {
  392. if (parent != NULL && parent->list_item_type) {
  393. field_type = parent->list_item_type;
  394. } else {
  395. field_type = DMAP_DICT;
  396. }
  397. }
  398. } else {
  399. /* Make a best guess of the type */
  400. field_type = DMAP_UNKNOWN;
  401. field_name = code;
  402. if (field_len >= 8) {
  403. /* Look for a four char code followed by a length within the current field */
  404. if (isalpha(p[0] & 0xff) &&
  405. isalpha(p[1] & 0xff) &&
  406. isalpha(p[2] & 0xff) &&
  407. isalpha(p[3] & 0xff)) {
  408. if (dmap_read_u32(p + 4) < field_len)
  409. field_type = DMAP_DICT;
  410. }
  411. }
  412. #ifdef DMAP_FULL
  413. if (field_type == DMAP_UNKNOWN) {
  414. size_t i;
  415. int is_string = 1;
  416. for (i=0; i < field_len; i++) {
  417. if (!isprint(p[i] & 0xff)) {
  418. is_string = 0;
  419. break;
  420. }
  421. }
  422. field_type = is_string ? DMAP_STR : DMAP_UINT;
  423. }
  424. #endif
  425. }
  426. switch (field_type) {
  427. case DMAP_UINT:
  428. /* Determine the integer's type based on its size */
  429. switch (field_len) {
  430. case 1:
  431. if (settings->on_uint32)
  432. settings->on_uint32(settings->ctx, code, field_name, (unsigned char)*p);
  433. break;
  434. case 2:
  435. if (settings->on_uint32)
  436. settings->on_uint32(settings->ctx, code, field_name, dmap_read_u16(p));
  437. break;
  438. case 4:
  439. if (settings->on_uint32)
  440. settings->on_uint32(settings->ctx, code, field_name, dmap_read_u32(p));
  441. break;
  442. case 8:
  443. if (settings->on_uint64)
  444. settings->on_uint64(settings->ctx, code, field_name, dmap_read_u64(p));
  445. break;
  446. default:
  447. if (settings->on_data)
  448. settings->on_data(settings->ctx, code, field_name, p, field_len);
  449. break;
  450. }
  451. break;
  452. case DMAP_INT:
  453. switch (field_len) {
  454. case 1:
  455. if (settings->on_int32)
  456. settings->on_int32(settings->ctx, code, field_name, *p);
  457. break;
  458. case 2:
  459. if (settings->on_int32)
  460. settings->on_int32(settings->ctx, code, field_name, dmap_read_i16(p));
  461. break;
  462. case 4:
  463. if (settings->on_int32)
  464. settings->on_int32(settings->ctx, code, field_name, dmap_read_i32(p));
  465. break;
  466. case 8:
  467. if (settings->on_int64)
  468. settings->on_int64(settings->ctx, code, field_name, dmap_read_i64(p));
  469. break;
  470. default:
  471. if (settings->on_data)
  472. settings->on_data(settings->ctx, code, field_name, p, field_len);
  473. break;
  474. }
  475. break;
  476. case DMAP_STR:
  477. if (settings->on_string)
  478. settings->on_string(settings->ctx, code, field_name, p, field_len);
  479. break;
  480. case DMAP_DATA:
  481. if (settings->on_data)
  482. settings->on_data(settings->ctx, code, field_name, p, field_len);
  483. break;
  484. case DMAP_DATE:
  485. /* Seconds since epoch */
  486. if (settings->on_date)
  487. settings->on_date(settings->ctx, code, field_name, dmap_read_u32(p));
  488. break;
  489. case DMAP_VERS:
  490. if (settings->on_string && field_len >= 4) {
  491. char version[20];
  492. sprintf(version, "%u.%u", dmap_read_u16(p), dmap_read_u16(p+2));
  493. settings->on_string(settings->ctx, code, field_name, version, strlen(version));
  494. }
  495. break;
  496. case DMAP_DICT:
  497. if (settings->on_dict_start)
  498. settings->on_dict_start(settings->ctx, code, field_name);
  499. if (dmap_parse_internal(settings, p, field_len, field) != 0)
  500. return -1;
  501. if (settings->on_dict_end)
  502. settings->on_dict_end(settings->ctx, code, field_name);
  503. break;
  504. case DMAP_ITEM:
  505. /* Unreachable: listing item types are always mapped to another type */
  506. abort();
  507. case DMAP_UNKNOWN:
  508. break;
  509. }
  510. p += field_len;
  511. }
  512. if (p != end)
  513. return -1;
  514. return 0;
  515. }
  516. int dmap_parse(const dmap_settings *settings, const char *buf, size_t len) {
  517. return dmap_parse_internal(settings, buf, len, NULL);
  518. }