#ifndef SPCAT_INTERNAL_H #define SPCAT_INTERNAL_H #include #include #include #include "spcat.h" #include "scorpiox-http.h" #include "scorpiox-http-util.h" #include "yyjson.h" #include "miniz.h" #define SPCAT_HOST_DEFAULT "https://gcp.scorpioplayer.com" #define SPCAT_VERSION_STR "1.0.0" #define SPCAT_ERRBUF 256 #define SPCAT_MAX_BODY (32u * 1024u * 1024u) /* wire in; refuse bigger */ #define SPCAT_MAX_INFLATED (64u * 1024u * 1024u) /* JSON out; bomb guard */ #define SPCAT_MAX_RATIO 64 /* inflated:compressed floor */ #define SPCAT_HOME_TTL_DEFAULT 900 #define SPCAT_PERSON_TTL_DEFAULT 21600 #define SPCAT_POPULAR_TTL_DEFAULT 1560 #define SPCAT_COLL_TTL_DEFAULT 1560 /* ---- zero-copy section indexes (pointers into the yyjson arena) ---- */ typedef struct { const char *id; const char *value; } spcat_idval; typedef struct { const char *imdb; int season, episode; double value; long long size; const char *duration, *size_str; int is_duration; const char *updated; } spcat_continue; typedef struct { const char *imdb; long long size; const char *filename; } spcat_level30; typedef struct { const char *logo, *items, *title; int order; } spcat_spotlight; typedef struct { const char *imdb, *title, *tagline, *plot, *poster, *picture; int id, year, duration, is_tv; /* -1 absent / -1 unknown */ const char **genres; size_t n_genres; const char **directors; size_t n_directors; const char **rating_src; double *rating_val; size_t n_ratings; } spcat_home_basic; typedef struct { int id; const char *name, *knowsfor, *profileurl, *imdbid; } spcat_person; typedef struct { const char *name, *origin, *logo; const char **imdb; size_t n_imdb; } spcat_playlist; typedef struct { int id; const char *backdrop, *logo, *cover, *name; int is_auto; /* -1 absent */ spcat_playlist *playlists; size_t n_playlists; } spcat_collection; typedef struct { int type; const char *name; int active; const char *expiration; } spcat_membership; typedef struct { int id; const char *name, *poster; int deleted, enabled, empty; } spcat_profile; typedef struct { const char *id, *email; spcat_membership *members; size_t n_members; spcat_profile *profiles; size_t n_profiles; } spcat_userinfo; typedef struct { spcat_idval *list; size_t n_list; spcat_continue *continues; size_t n_continues; spcat_level30 *level30; size_t n_level30; spcat_spotlight *spotlights; size_t n_spotlights; spcat_idval *extent; size_t n_extent; spcat_home_basic *basics; size_t n_basics; spcat_person *peoples; size_t n_peoples; spcat_collection *collections; size_t n_collections; spcat_userinfo user; int has_user; } spcat_home_index; /* ---- process state ---- */ typedef struct { const scorpiox_http *http; int have_http; char host[256]; char err[SPCAT_ERRBUF]; long http_code; long long last_content_hash; /* -1 = absent */ /* homepage: one retained doc + index (SPEC D10) */ yyjson_doc *home_doc; yyjson_val *home_root; char *home_json; size_t home_json_len; spcat_home_index home; long long home_hash; /* stable hash of the last fetched JSON */ long long send_hash; /* 0 = don't send MD5HomePage */ char home_key[160]; time_t home_at; int home_ttl; int home_valid; /* collections endpoint */ yyjson_doc *coll_doc; yyjson_val *coll_arr; spcat_collection *colls; size_t n_colls; size_t cap_colls; char coll_lang[32]; time_t coll_at; int coll_ttl; int coll_valid; /* people detail (single slot) */ yyjson_doc *person_doc; yyjson_val *p_root; char person_key[80]; time_t person_at; int person_ttl; int person_valid; /* people popular (single slot) */ yyjson_doc *pop_doc; yyjson_val *pop_arr; size_t n_pop; char pop_key[80]; time_t pop_at; int pop_ttl; int pop_valid; } spcat_state; extern spcat_state g_sp; /* util (spcat_util.c) */ void spcat_set_err(const char *fmt, ...); char *spcat_strdup(const char *s); char *spcat_strndup(const char *s, size_t n); char *spcat_url_encode(const char *s); /* djb2 over UTF-16 code units — the C# GetStringStableHashCode (Core/System.cs:620), identical to spmhash_stable_utf8 (media-hash). Returns non-negative int32 range. */ long long spcat_stable_hash(const char *utf8, size_t len); /* inflate an RFC1950 zlib stream (wbits 15). out malloc'd, NUL-terminated. 0 ok. */ int spcat_inflate_zlib(const unsigned char *in, size_t in_len, unsigned char **out, size_t *out_len, char *errbuf, size_t errcap); /* case-agnostic key lookup (SPEC D15): tries pascal, then camel, then a case-insensitive linear scan (root objects are small). NULL-safe. */ yyjson_val *spcat_geti(yyjson_val *obj, const char *pascal, const char *camel); /* scalar loaders: 1 = present, 0 = absent/null/wrong-type (never an error) */ int spcat_js(const char **out, yyjson_val *obj, const char *pascal, const char *camel); int spcat_ji(int *out, yyjson_val *obj, const char *pascal, const char *camel); int spcat_jll(long long *out, yyjson_val *obj, const char *pascal, const char *camel); int spcat_jd(double *out, yyjson_val *obj, const char *pascal, const char *camel); int spcat_jb(int *out, yyjson_val *obj, const char *pascal, const char *camel); /* load a string array zero-copy (arena pointers). 0 ok, -1 OOM. */ int spcat_load_strarr(yyjson_val *arr, const char ***out, size_t *n); int spcat_cache_expired(time_t at, int ttl); int spcat_cache_entries_count(void); /* http (spcat_http.c) */ /* GET through g_sp.http; extra_headers = "Name: value" lines (NULL-terminated, may be NULL). Whole bounded body + final response header arena. 0 = response arrived (code in *out_code); -1 = transport (*errbuf). */ int spcat_http_get(const char *url, const char *const *extra_headers, unsigned char **out_body, size_t *out_len, scorpiox_buf *out_hdrs, long *out_code, char *errbuf, size_t errcap); /* index_playlists: shared by the collections endpoint and the homepage * CollectionPlaylists section (defined in spcat_homepage.c) */ int spcat_index_playlists(yyjson_val *arr, spcat_playlist **out, size_t *n); void spcat_free_home_index(spcat_home_index *h); void spcat_free_home(void); void spcat_free_colls(void); void spcat_free_person(void); void spcat_free_pop(void); /* sections */ void spcat_free_all(void); int spcat_index_home_doc(yyjson_val *root, spcat_home_index *out); int spcat_home_fetch(const char *user_id, int profile_id, const char *lang, unsigned opts); int spcat_home_retain(yyjson_doc *doc, const char *json, size_t json_len); int spcat_colls_fetch(const char *lang); int spcat_person_fetch(const char *id, const char *lang); int spcat_pop_fetch(int page, int page_size, int page_numbers, const char *lang); /* fake (spcat_fake.c) */ const scorpiox_http *spcat_fake_backend(const char *corpus_dir); void spcat_fake_install(void); long long spcat_fake_calls_internal(void); long long spcat_fake_fail_internal(void); const char *spcat_fake_last_url_internal(void); void spcat_fake_set_mode(int mode); void spcat_fake_wipe_stats(void); #endif